UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
text.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

unsigned char * base64_decode (const char *in, unsigned int *length)
 
bool draw_line (char *buf, int pitch, const char *text, uint32_t color, bool solid)
 
size_t urldecode (char *out, size_t max_len, const char *in)
 
size_t urlencode (char *out, size_t max_len, const char *in, int(*eval_pass)(int c), bool space_plus_replace)
 
int urlencode_html5_eval (int c)
 
int urlencode_rfc3986_eval (int c)
 
char * wrap_paragraph (char *text)
 

Detailed Description

Author
Martin Pulec pulec.nosp@m.@ces.nosp@m.net.c.nosp@m.z

Definition in file text.h.

Function Documentation

◆ base64_decode()

unsigned char * base64_decode ( const char * in,
unsigned int * length )

C-adapted version of https://stackoverflow.com/a/34571089

As the output is a generic binary string, it is not NULL-terminated.

Caller is obliged to free the returned string.

Definition at line 168 of file text.c.

168 {
169 unsigned int allocated = 128;
170 unsigned char *out = (unsigned char *) malloc(allocated);
171 *length = 0;
172
173 int T[256];
174 for (unsigned int i = 0; i < sizeof T / sizeof T[0]; i++) {
175 T[i] = -1;
176 }
177 for (int i=0; i<64; i++) T[(int) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
178
179 int val=0, valb=-8;
180 unsigned char c = 0;
181 while ((c = *in++) != '\0') {
182 if (T[c] == -1) break;
183 val = (val << 6) + T[c];
184 valb += 6;
185 if (valb >= 0) {
186 if (allocated == *length) {
187 allocated *= 2;
188 out = (unsigned char *) realloc(out, allocated);
189 assert(out != NULL);
190 }
191 out[(*length)++] = (val>>valb)&0xFF;
192 valb -= 8;
193 }
194 }
195 return out;
196}
int void * val

References val.

Referenced by get_nals().

◆ draw_line()

bool draw_line ( char * buf,
int pitch,
const char * text,
uint32_t color,
bool solid )

draws a line with built-in bitmap 12x7 bitmap font separated by 1 px space, RGBA

< adding 1 pix space between letters

Definition at line 271 of file text.c.

271 {
272 static unsigned char font_data[(FONT_W * FONT_COUNT + 7) / 8 * FONT_H];
273 static bool font_data_initialized = false;
274 if (!font_data_initialized) {
275 if (!draw_line_init(font_data)) {
276 return false;
277 }
278 font_data_initialized = true;
279 }
280 int idx = 0;
281 enum {
282 WIDTH = FONT_W + 1,
283 };
284 while (*text) {
285 char c = *text;
286 if (c < ' ' || c > '~') {
287 c = '?';
288 }
289 c -= ' ';
290 for (int j = 0; j < FONT_H; ++j) {
291 for (int i = 0; i < FONT_W; ++i) {
292 int pos_x = (FONT_W * c + i) / 8;
293 int mask = 1 << (FONT_W - ((FONT_W * c + i) % 8));
294 int offset = (FONT_W * FONT_COUNT + 7) / 8 * j;
295 if (font_data[offset + pos_x] & mask) {
296 buf[j * pitch + 4 * (i + idx * WIDTH)] = color & 0xFFU;
297 buf[j * pitch + 4 * (i + idx * WIDTH) + 1] = (color >> 8U) & 0xFFU;
298 buf[j * pitch + 4 * (i + idx * WIDTH) + 2] = (color >> 16U) & 0xFFU;
299 buf[j * pitch + 4 * (i + idx * WIDTH) + 3] = (color >> 24U) & 0xFFU;
300 } else if (solid) {
301 buf[j * pitch + 4 * (i + idx * WIDTH)] =
302 buf[j * pitch + 4 * (i + idx * WIDTH) + 1] =
303 buf[j * pitch + 4 * (i + idx * WIDTH) + 2] = 0;
304 buf[j * pitch + 4 * (i + idx * WIDTH) + 3] = 0xFFU;
305 }
306 }
307 if (solid) { // fill space between characters
308 buf[j * pitch + 4 * ((WIDTH-1) + idx * WIDTH)] =
309 buf[j * pitch + 4 * ((WIDTH-1) + idx * WIDTH) + 1] =
310 buf[j * pitch + 4 * ((WIDTH-1) + idx * WIDTH) + 2] = 0;
311 buf[j * pitch + 4 * ((WIDTH-1) + idx * WIDTH) + 3] = 0xFFU;
312 }
313 }
314 if ((++idx + 1) * WIDTH * 4 > pitch) {
315 return true;
316 }
317 ++text;
318 }
319
320 return true;
321}
@ FONT_H
Definition bitmap_font.h:8
@ FONT_W
Definition bitmap_font.h:7
@ FONT_COUNT
Definition bitmap_font.h:9
static __device__ const float offset
Definition cuda_dxt.cu:139
static bool draw_line_init(unsigned char *out)
Definition text.c:242

References draw_line_init(), FONT_COUNT, FONT_H, FONT_W, and offset.

Referenced by image_pattern_bars::fill(), and image_pattern_text::fill().

Here is the call graph for this function:

◆ urldecode()

size_t urldecode ( char * out,
size_t max_len,
const char * in )

URL decodes input string (replaces all "%XX" sequences with ASCII representation of 0xXX)

Parameters
ininput
outoutput array
max_lenmaximal lenght to be written (including terminating NUL)
Returns
bytes written, 0 on error
Note
Symbol '+' is not treated specially (unlike in classic URL decoding which translates it to ' '.

Definition at line 129 of file text.c.

130{
131 if (max_len == 0) { // avoid (uint) -1 cast
132 return 0;
133 }
134 size_t len = 0;
135 while (*in && len < max_len - 1) {
136 if (*in == '+') {
137 *out++ = ' ';
138 in++;
139 } else if (*in != '%') {
140 *out++ = *in++;
141 } else {
142 in++; // skip '%'
143 if (!ishex(in[0]) || !ishex(in[1])) {
144 return 0;
145 }
146 unsigned int c = 0;
147 if (sscanf(in, "%2x", &c) != 1) {
148 return 0;
149 }
150 *out++ = c;
151 in += 2;
152 }
153 len++;
154 }
155 *out = '\0';
156 len++;
157
158 return len;
159}
int void size_t * len
static int ishex(int x)
Definition text.c:111

References ishex(), and len.

Referenced by vidcap_spout_init().

Here is the call graph for this function:

◆ urlencode()

size_t urlencode ( char * out,
size_t max_len,
const char * in,
int(* eval_pass )(int c),
bool space_plus_replace )

Replaces all occurences where eval() evaluates to true with %-encoding

Parameters
ininput
outoutput array
max_lenmaximal lenght to be written (including terminating NUL)
eval_passpredictor if an input character should be kept (functions from ctype.h may be used)
space_plus_replacereplace spaces (' ') with ASCII plus sign - should be true for HTML5 URL encoding, false for RFC 3986
Returns
bytes written to out
Note
Symbol ' ' is not treated specially (unlike in classic URL encoding which translates it to '+'.
Todo
There may be a LUT as in https://rosettacode.org/wiki/URL_encoding#C

Definition at line 81 of file text.c.

83{
84 if (max_len == 0 || max_len >= INT_MAX) { // prevent overflow
85 return 0;
86 }
87 size_t len = 0;
88 while (*in && len < max_len - 1) {
89 if (*in == ' ' && space_plus_replace) {
90 *out++ = '+';
91 in++;
92 } else if (eval_pass(*in) != 0) {
93 *out++ = *in++;
94 len++;
95 } else {
96 if ((int) len < (int) max_len - 3 - 1) {
97 int ret = sprintf(out, "%%%02X", *in++);
98 out += ret;
99 len += ret;
100 } else {
101 break;
102 }
103 }
104 }
105 *out = '\0';
106 len++;
107
108 return len;
109}

References len.

Referenced by vidcap_spout_probe().

◆ urlencode_html5_eval()

int urlencode_html5_eval ( int c)

Definition at line 54 of file text.c.

55{
56 return isalnum(c) || c == '*' || c == '-' || c == '.' || c == '_';
57}

◆ urlencode_rfc3986_eval()

int urlencode_rfc3986_eval ( int c)

Definition at line 59 of file text.c.

60{
61 return isalnum(c) || c == '~' || c == '-' || c == '.' || c == '_';
62}

Referenced by vidcap_spout_probe().

◆ wrap_paragraph()

char * wrap_paragraph ( char * text)

Indents paragraph (possibly with ANSI colors) to (currently only) the width of 80. Inplace (just replaces spaces with newlines).

Returns
text

Definition at line 204 of file text.c.

205{
206 char *pos = text;
207 char *last_space = NULL;
208
209 char *next = NULL;
210 int line_len = 0;
211 while ((next = strpbrk(pos, " \n")) != NULL) {
212 int len_raw = next - pos;
213 char str_in[len_raw + 1];
214 memcpy(str_in, pos, len_raw);
215 str_in[len_raw] = '\0';
216 int len_net = strlen(prune_ansi_sequences_inplace_cstr(str_in));
217 if (line_len + len_net > 80) {
218 if (line_len == 0) { // |word|>80 starting a line
219 *next = '\n';
220 pos = next + 1;
221 } else {
222 *last_space = '\n';
223 pos = last_space + 1;
224 }
225 line_len = 0;
226 continue;
227 }
228 if (next[0] == '\n') {
229 pos = next + 1;
230 line_len = 0;
231 continue;
232 }
233 last_space = next;
234 pos += len_raw + 1;
235 line_len += len_net + 1;
236 }
237 return text;
238}
char * prune_ansi_sequences_inplace_cstr(char *cstr)

References prune_ansi_sequences_inplace_cstr().

Referenced by df_usage(), display_dummy_init(), init(), playback_usage(), set_pixfmt_conv_policy(), show_help(), show_help(), text_init(), usage(), usage(), usage(), and vidcap_screen_linux_init().

Here is the call graph for this function: