font-bdf.c

filter: Statementstmt 18.1% 48 / 264 Branchbran 10.1% 26 / 256 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 14.2% 74 / 520 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Image/Caption.pm ↑ Summary ppm-lite.c »
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 font-bdf.c --- simple, self-contained code for manipulating BDF fonts.
3  
4 Copyright © 2001 Jamie Zawinski <jwz@jwz.org>
5  
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation. No representations are made about the suitability of this
11 software for any purpose. It is provided "as is" without express or
12 implied warranty.
13 */
14  
15 #include "config.h"
16 #include "ppm-lite.h"
17 #include "font-bdf.h"
18  
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22  
23 #undef countof
24 #define countof(x) (sizeof((x))/sizeof(*(x)))
25  
26  
27 /* for parsing hex numbers fast */
28 static const char hex[128] = {16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
29 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
30 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
31 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,16,16,16,16,16,16,
32 16,10,11,12,13,14,15,16,16,16,16,16,16,16,16,16,
33 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
34 16,10,11,12,13,14,15,16,16,16,16,16,16,16,16,16,
35 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16};
36  
37 struct font *
38 0 read_bdf (const char *file)
39 {
40 0 int stdin_p = !strcmp(file, "-");
41 FILE *in;
42 char buf [1024];
43 0 struct font *font = (struct font *) calloc (1, sizeof(*font));
44 0 int line = 1;
45 0 int current_char = -1;
46 0 int current_char_height = -1;
47 0 int current_char_descent = -1;
48  
49 0 int overall_bitmap_height = -1;
50 0 int overall_bitmap_descent = -1;
51  
52 0 ▶ if (stdin_p)
Branch: 1 branch
BranchTrueFalseTotal
if (stdin_p) 0 0 0
53 0 in = stdin;
54 else
55 {
56 0 in = fopen (file, "r");
57 0 ▶ if (!in)
Branch: 1 branch
BranchTrueFalseTotal
if (!in) 0 0 0
58 {
59 0 sprintf(buf, "%.255s: %.255s", "progname", file);
60 0 perror(buf);
61 0 exit (1);
62 }
63 }
64  
65 0 ▶ if (!fgets (buf, sizeof(buf)-1, in))
Branch: 1 branch
BranchTrueFalseTotal
if (!fgets (buf, sizeof(buf)-1, in)) 0 0 0
66 {
67 0 fprintf (stderr, "%s: %s: %d: premature EOF\n",
68 "progname", file, line);
69 0 exit (1);
70 }
71  
72 0 ▶ if (!!strncmp (buf, "STARTFONT 2.", 12))
Branch: 1 branch
BranchTrueFalseTotal
if (!!strncmp (buf, "STARTFONT 2.", 12)) 0 0 0
73 {
74 0 fprintf (stderr, "%s: %s: %d: not a BDF 2 file\n",
75 "progname", file, line);
76 0 exit (1);
77 }
78 0 line++;
79  
80  
81 0 ▶ while (fgets (buf, sizeof(buf)-1, in))
Branch: 1 branch
BranchTrueFalseTotal
while (fgets (buf, sizeof(buf)-1, in)) 0 0 0
82 {
83 char dummy;
84 int dummyn;
85 0 ▶ if (!strncmp (buf, "FACE_NAME ", 10) && !font->name)
Branch: 2 branches
BranchTrueFalseTotal
if (!strncmp (buf, "FACE_NAME ", 10) && !font->name) 0 0 0
if (!strncmp (buf, "FACE_NAME ", 10) && !font->name) 0 0 0
86 0 {
87 char *ss;
88 0 font->name = strdup (buf + 10);
89 0 ▶ for (ss = font->name; *ss; ss++)
Branch: 1 branch
BranchTrueFalseTotal
for (ss = font->name; *ss; ss++) 0 0 0
90 0 ▶ if (*ss == '\"' || *ss == '\r' || *ss == '\n')
Branch: 3 branches
BranchTrueFalseTotal
if (*ss == '\"' || *ss == '\r' || *ss == '\n') 0 0 0
if (*ss == '\"' || *ss == '\r' || *ss == '\n') 0 0 0
if (*ss == '\"' || *ss == '\r' || *ss == '\n') 0 0 0
91 {
92 0 strcpy (ss, ss+1);
93 0 ▶ if (ss != font->name) ss--;
Branch: 1 branch
BranchTrueFalseTotal
if (ss != font->name) ss--; 0 0 0
94 }
95 }
96 0 ▶ else if (!strncmp (buf, "FONT_ASCENT ", 12))
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp (buf, "FONT_ASCENT ", 12)) 0 0 0
97 {
98 0 ▶ if (1 != sscanf (buf+12, "%d %c", &font->ascent, &dummy))
Branch: 1 branch
BranchTrueFalseTotal
if (1 != sscanf (buf+12, "%d %c", &font->ascent, &dummy)) 0 0 0
99 0 goto FAIL;
100 }
101 0 ▶ else if (!strncmp (buf, "FONT_DESCENT ", 12))
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp (buf, "FONT_DESCENT ", 12)) 0 0 0
102 {
103 0 ▶ if (1 != sscanf (buf+12, "%d %c", &font->descent, &dummy))
Branch: 1 branch
BranchTrueFalseTotal
if (1 != sscanf (buf+12, "%d %c", &font->descent, &dummy)) 0 0 0
104 0 goto FAIL;
105 }
106 0 ▶ else if (!strncmp (buf, "FONTBOUNDINGBOX ", 16))
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp (buf, "FONTBOUNDINGBOX ", 16)) 0 0 0
107 {
108 int w, h, x, y;
109 0 ▶ if (4 != sscanf (buf+16, "%d %d %d %d %c", &w, &h, &x, &y, &dummy))
Branch: 1 branch
BranchTrueFalseTotal
if (4 != sscanf (buf+16, "%d %d %d %d %c", &w, &h, &x, &y, &dummy)) 0 0 0
110 0 goto FAIL;
111 0 overall_bitmap_height = h;
112 0 overall_bitmap_descent = y;
113 }
114 0 ▶ else if (!strncmp (buf, "ENCODING ", 9))
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp (buf, "ENCODING ", 9)) 0 0 0
115 {
116 0 ▶ if (1 != sscanf (buf+9, "%d %c", &current_char, &dummy) &&
Branch: 1 branch
BranchTrueFalseTotal
if (1 != sscanf (buf+9, "%d %c", &current_char, &dummy) && 0 0 0
117 0 ▶ current_char <= 255 &&
Branch: 1 branch
BranchTrueFalseTotal
current_char <= 255 && 0 0 0
118 0 ▶ current_char >= -1)
Branch: 1 branch
BranchTrueFalseTotal
current_char >= -1) 0 0 0
119 0 goto FAIL;
120 }
121 0 ▶ else if (!strncmp (buf, "STARTCHAR", 9) ||
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp (buf, "STARTCHAR", 9) || 0 0 0
122 0 ▶ !strncmp (buf, "ENDCHAR", 7))
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "ENDCHAR", 7)) 0 0 0
123 {
124 0 current_char = -1;
125 }
126 0 ▶ else if (!strncmp (buf, "DWIDTH ", 7))
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp (buf, "DWIDTH ", 7)) 0 0 0
127 {
128 int w;
129 0 ▶ if (2 != sscanf (buf+7, "%d %d %c", &w, &dummyn, &dummy))
Branch: 1 branch
BranchTrueFalseTotal
if (2 != sscanf (buf+7, "%d %d %c", &w, &dummyn, &dummy)) 0 0 0
130 0 goto FAIL;
131 0 ▶ if (current_char != -1)
Branch: 1 branch
BranchTrueFalseTotal
if (current_char != -1) 0 0 0
132 0 font->chars[current_char].width = w;
133 }
134 0 ▶ else if (!strncmp (buf, "BBX ", 4))
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp (buf, "BBX ", 4)) 0 0 0
135 {
136 int w, h, x, y;
137 struct ppm *ppm;
138 0 ▶ if (4 != sscanf (buf+4, "%d %d %d %d %c", &w, &h, &x, &y, &dummy))
Branch: 1 branch
BranchTrueFalseTotal
if (4 != sscanf (buf+4, "%d %d %d %d %c", &w, &h, &x, &y, &dummy)) 0 0 0
139 0 goto FAIL;
140  
141 /* Make all ppm data be the same height, so that when scaling,
142 all characters experience proportional roundoff artifacts.
143 */
144 0 current_char_height = h;
145 0 current_char_descent = y;
146 0 h = overall_bitmap_height;
147 0 y = overall_bitmap_descent;
148  
149 0 ▶ if (current_char != -1)
Branch: 1 branch
BranchTrueFalseTotal
if (current_char != -1) 0 0 0
150 {
151 0 font->chars[current_char].lbearing = x;
152 0 font->chars[current_char].descent = y;
153 0 ppm = (struct ppm *) calloc (1, sizeof(struct ppm));
154 0 ppm->type = 4;
155 0 ppm->width = ((w + 7) / 8) * 8;
156 0 ppm->height = h;
157  
158 0 ppm->rgba = (unsigned char *)
159 0 calloc (1, (ppm->width * ppm->height * 4));
160 0 ▶ if (!ppm->rgba)
Branch: 1 branch
BranchTrueFalseTotal
if (!ppm->rgba) 0 0 0
161 {
162 0 fprintf (stderr, "%s: out of memory (%d x %d)\n",
163 "progname", ppm->width, ppm->height);
164 0 exit (1);
165 }
166  
167 0 font->chars[current_char].ppm = ppm;
168 }
169 }
170 0 ▶ else if (!strncmp (buf, "BITMAP ", 4))
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp (buf, "BITMAP ", 4)) 0 0 0
171 {
172 int y, yoff;
173  
174 0 ▶ if (font->chars[current_char].width == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (font->chars[current_char].width == 0) 0 0 0
175 {
176 0 fprintf (stderr,
177 "%s: %s: %d: zero-width char ('%c') with bits?\n",
178 "progname", file, line, current_char);
179 0 exit (1);
180 }
181  
182 0 yoff = ((overall_bitmap_height - current_char_height) +
183 0 + (overall_bitmap_descent - current_char_descent));
184  
185 0 ▶ for (y = 0; y < current_char_height; y++)
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < current_char_height; y++) 0 0 0
186 {
187 0 ▶ if (!fgets (buf, sizeof(buf)-1, in))
Branch: 1 branch
BranchTrueFalseTotal
if (!fgets (buf, sizeof(buf)-1, in)) 0 0 0
188 {
189 0 fprintf (stderr, "%s: %s: %d: premature EOF\n",
190 "progname", file, line);
191 0 exit (1);
192 }
193  
194 0 ▶ if (current_char > 0 && current_char <= 255)
Branch: 2 branches
BranchTrueFalseTotal
if (current_char > 0 && current_char <= 255) 0 0 0
if (current_char > 0 && current_char <= 255) 0 0 0
195 {
196 0 int yy = (y + yoff);
197 0 struct ppm *ppm = font->chars[current_char].ppm;
198 0 unsigned char *o = (ppm->rgba + (yy * ppm->width * 4));
199 char *s;
200  
201 0 ▶ if (yy < 0 || yy >= ppm->height) abort();
Branch: 2 branches
BranchTrueFalseTotal
if (yy < 0 || yy >= ppm->height) abort(); 0 0 0
if (yy < 0 || yy >= ppm->height) abort(); 0 0 0
202  
203 0 ▶ for (s = buf; *s; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = buf; *s; s++) 0 0 0
204 {
205 0 int h = hex[(int) *s];
206 int i;
207 0 ▶ if (h != 16)
Branch: 1 branch
BranchTrueFalseTotal
if (h != 16) 0 0 0
208 0 ▶ for (i = 3; i >= 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i >= 0; i--) 0 0 0
209 {
210 /* ink bits are 0 (black) and opaque (255).
211 non-ink bits are 0 (black) and clear (0).
212 */
213 0 ▶ o[3] = ((h & (1<<i)) ? 255 : 0);
Branch: 1 branch
BranchTrueFalseTotal
o[3] = ((h & (1<<i)) ? 255 : 0); 0 0 0
214 0 o += 4;
215 }
216 }
217 }
218 0 line++;
219 }
220 }
221 0 ▶ else if (!strncmp (buf, "FONT ", 5) ||
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp (buf, "FONT ", 5) || 0 0 0
222 0 ▶ !strncmp (buf, "SIZE ", 5) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "SIZE ", 5) || 0 0 0
223 0 ▶ !strncmp (buf, "COMMENT", 7) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "COMMENT", 7) || 0 0 0
224 0 ▶ !strncmp (buf, "STARTPROPERTIES ", 7) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "STARTPROPERTIES ", 7) || 0 0 0
225 0 ▶ !strncmp (buf, "FOUNDRY ", 8) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "FOUNDRY ", 8) || 0 0 0
226 0 ▶ !strncmp (buf, "FAMILY_NAME ", 12) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "FAMILY_NAME ", 12) || 0 0 0
227 0 ▶ !strncmp (buf, "WEIGHT_NAME ", 12) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "WEIGHT_NAME ", 12) || 0 0 0
228 0 ▶ !strncmp (buf, "SLANT ", 6) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "SLANT ", 6) || 0 0 0
229 0 ▶ !strncmp (buf, "SETWIDTH_NAME ", 14) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "SETWIDTH_NAME ", 14) || 0 0 0
230 0 ▶ !strncmp (buf, "ADD_STYLE_NAME ", 15) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "ADD_STYLE_NAME ", 15) || 0 0 0
231 0 ▶ !strncmp (buf, "PIXEL_SIZE ", 11) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "PIXEL_SIZE ", 11) || 0 0 0
232 0 ▶ !strncmp (buf, "POINT_SIZE ", 11) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "POINT_SIZE ", 11) || 0 0 0
233 0 ▶ !strncmp (buf, "RESOLUTION_X ", 13) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "RESOLUTION_X ", 13) || 0 0 0
234 0 ▶ !strncmp (buf, "RESOLUTION_Y ", 13) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "RESOLUTION_Y ", 13) || 0 0 0
235 0 ▶ !strncmp (buf, "SPACING ", 8) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "SPACING ", 8) || 0 0 0
236 0 ▶ !strncmp (buf, "AVERAGE_WIDTH ", 14) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "AVERAGE_WIDTH ", 14) || 0 0 0
237 0 ▶ !strncmp (buf, "CHARSET_REGISTRY ", 17) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "CHARSET_REGISTRY ", 17) || 0 0 0
238 0 ▶ !strncmp (buf, "CHARSET_ENCODING ", 17) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "CHARSET_ENCODING ", 17) || 0 0 0
239 0 ▶ !strncmp (buf, "FONTNAME_REGISTRY ", 18) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "FONTNAME_REGISTRY ", 18) || 0 0 0
240 0 ▶ !strncmp (buf, "CAP_HEIGHT ", 11) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "CAP_HEIGHT ", 11) || 0 0 0
241 0 ▶ !strncmp (buf, "X_HEIGHT ", 9) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "X_HEIGHT ", 9) || 0 0 0
242 0 ▶ !strncmp (buf, "COPYRIGHT ", 10) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "COPYRIGHT ", 10) || 0 0 0
243 0 ▶ !strncmp (buf, "NOTICE ", 7) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "NOTICE ", 7) || 0 0 0
244 0 ▶ !strncmp (buf, "_DEC_", 5) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "_DEC_", 5) || 0 0 0
245 0 ▶ !strncmp (buf, "MULE_", 5) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "MULE_", 5) || 0 0 0
246 0 ▶ !strncmp (buf, "DEFAULT_CHAR ", 13) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "DEFAULT_CHAR ", 13) || 0 0 0
247 0 ▶ !strncmp (buf, "RELATIVE_SETWIDTH ", 18) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "RELATIVE_SETWIDTH ", 18) || 0 0 0
248 0 ▶ !strncmp (buf, "RELATIVE_WEIGHT ", 16) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "RELATIVE_WEIGHT ", 16) || 0 0 0
249 0 ▶ !strncmp (buf, "CHARSET_COLLECTIONS ", 20) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "CHARSET_COLLECTIONS ", 20) || 0 0 0
250 0 ▶ !strncmp (buf, "FULL_NAME ", 10) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "FULL_NAME ", 10) || 0 0 0
251 0 ▶ !strncmp (buf, "ENDPROPERTIES", 13) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "ENDPROPERTIES", 13) || 0 0 0
252 0 ▶ !strncmp (buf, "CHARS ", 5) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "CHARS ", 5) || 0 0 0
253 0 ▶ !strncmp (buf, "SWIDTH ", 7) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "SWIDTH ", 7) || 0 0 0
254 0 ▶ !strncmp (buf, "DWIDTH ", 7) ||
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "DWIDTH ", 7) || 0 0 0
255 0 ▶ !strncmp (buf, "ENDFONT", 7))
Branch: 1 branch
BranchTrueFalseTotal
!strncmp (buf, "ENDFONT", 7)) 0 0 0
256 {
257 /* ignore */
258 }
259 else
260 {
261 0 FAIL:
262 0 fprintf (stderr, "%s: %s: %d: unparsable line: %s\n",
263 "progname", file, line, buf);
264 0 exit (1);
265 }
266  
267 0 line++;
268 }
269  
270 0 ▶ if (!font->name)
Branch: 1 branch
BranchTrueFalseTotal
if (!font->name) 0 0 0
271 0 font->name = strdup ("<unknown>");
272  
273 0 ▶ if (!stdin_p)
Branch: 1 branch
BranchTrueFalseTotal
if (!stdin_p) 0 0 0
274 0 fclose (in);
275  
276 0 font->monochrome_p = 1;
277  
278 0 return font;
279 }
280  
281  
282 struct font *
283 1 copy_font (struct font *font)
284 {
285 int i;
286 1 struct font *f2 = (struct font *) malloc (sizeof(*font));
287 1 memcpy (f2, font, sizeof(*font));
288 1 font = f2;
289 1 font->name = strdup (font->name);
290 257 ▶ for (i = 0; i < countof(font->chars); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < countof(font->chars); i++) 256 1 257
291 256 ▶ if (font->chars[i].ppm)
Branch: 1 branch
BranchTrueFalseTotal
if (font->chars[i].ppm) 191 65 256
292 191 font->chars[i].ppm = copy_ppm (font->chars[i].ppm);
293 1 return font;
294 }
295  
296  
297 void
298 0 free_font (struct font *font)
299 {
300 int i;
301 // free (font->name);
302 0 ▶ for (i = 0; i < countof(font->chars); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < countof(font->chars); i++) 0 0 0
303 0 ▶ if (font->chars[i].ppm)
Branch: 1 branch
BranchTrueFalseTotal
if (font->chars[i].ppm) 0 0 0
304 0 free_ppm (font->chars[i].ppm);
305 0 memset (font, 0xDE, sizeof(*font));
306 0 free (font);
307 0 }
308  
309  
310 static int
311 3168 draw_char (struct font *font, const unsigned char c,
312 struct ppm *into, int x, int y,
313 unsigned long fg, unsigned long bg,
314 int alpha)
315 {
316 3168 int w = font->chars[(int) c].width;
317 3168 struct ppm *from = font->chars[(int) c].ppm;
318  
319 3168 ▶ if (from && c != ' ')
Branch: 2 branches
BranchTrueFalseTotal
if (from && c != ' ') 3168 0 3168
if (from && c != ' ') 2772 396 3168
320 {
321 2772 x += font->chars[(int) c].lbearing;
322 2772 y -= from->height + font->chars[(int) c].descent;
323 2772 y += font->ascent;
324 2772 paste_ppm (into, x, y,
325 from, 0, 0, from->width, from->height,
326 fg, bg, alpha);
327 }
328 3168 return w;
329 }
330  
331  
332 /* draw a string with origin XY.
333 Alignment 0 means center on the Y axis;
334 -1 means flushright; 1 means flushleft.
335 Alpha ranges from 0-255.
336 Newlines are allowed; tabs are not handled specially.
337 */
338 void
339 99 draw_string (struct font *font, const unsigned char *string,
340 struct ppm *into, int x, int y,
341 int alignment,
342 unsigned long fg, unsigned long bg,
343 int alpha)
344 {
345 99 int ox = x;
346 int w;
347 const unsigned char *s2;
348  
349 99 LINE:
350 99 x = ox;
351 99 w = 0;
352  
353 99 ▶ if (alignment <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (alignment <= 0) 99 0 99
354 {
355 3267 ▶ for (s2 = string; *s2 && *s2 != '\n'; s2++)
Branch: 2 branches
BranchTrueFalseTotal
for (s2 = string; *s2 && *s2 != '\n'; s2++) 3168 99 3267
for (s2 = string; *s2 && *s2 != '\n'; s2++) 3168 0 3168
356 3168 w += font->chars[(int) *s2].width;
357 99 ▶ if (alignment < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (alignment < 0) 99 0 99
358 99 x -= w;
359 0 ▶ else if (alignment == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (alignment == 0) 0 0 0
360 0 x -= w/2;
361 99 x--;
362 }
363  
364 /* back up over the lbearing of the first character on the line */
365 99 x -= 2 * font->chars[(int) *string].lbearing;
366  
367 3267 ▶ while (*string)
Branch: 1 branch
BranchTrueFalseTotal
while (*string) 3168 99 3267
368 {
369 3168 ▶ if (*string == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (*string == '\n') 0 3168 3168
370 {
371 0 y += font->ascent + font->descent;
372 0 string++;
373 0 goto LINE;
374 }
375 else
376 {
377 3168 int w = draw_char (font, *string, into, x, y, fg, bg, alpha);
378 3168 x += w;
379 }
380 3168 string++;
381 }
382 99 }
383  
384  
385 void
386 1 scale_font (struct font *font, double scale)
387 {
388 int i;
389  
390 # define SCALE(x) ((x) = (scale * ((x) > 0 ? ((x) + 0.5) : ((x) - 0.5))))
391  
392 1 ▶ SCALE (font->ascent);
Branch: 1 branch
BranchTrueFalseTotal
SCALE (font->ascent); 1 0 1
393 1 ▶ SCALE (font->descent);
Branch: 1 branch
BranchTrueFalseTotal
SCALE (font->descent); 1 0 1
394  
395 257 ▶ for (i = 0; i < countof(font->chars); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < countof(font->chars); i++) 256 1 257
396 {
397 256 ▶ SCALE (font->chars[i].lbearing);
Branch: 1 branch
BranchTrueFalseTotal
SCALE (font->chars[i].lbearing); 147 109 256
398 256 ▶ SCALE (font->chars[i].width);
Branch: 1 branch
BranchTrueFalseTotal
SCALE (font->chars[i].width); 191 65 256
399 256 ▶ SCALE (font->chars[i].descent);
Branch: 1 branch
BranchTrueFalseTotal
SCALE (font->chars[i].descent); 0 256 256
400  
401 256 ▶ if (font->chars[i].ppm)
Branch: 1 branch
BranchTrueFalseTotal
if (font->chars[i].ppm) 191 65 256
402 {
403 191 struct ppm *ppm2 = scale_ppm (font->chars[i].ppm, scale);
404 191 free_ppm (font->chars[i].ppm);
405 191 font->chars[i].ppm = ppm2;
406 }
407 }
408 # undef SCALE
409 1 }
410  
411  
412 void
413 0 halo_font (struct font *font, int radius)
414 {
415 int i;
416 0 ▶ for (i = 0; i < countof(font->chars); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < countof(font->chars); i++) 0 0 0
417 0 ▶ if (font->chars[i].ppm)
Branch: 1 branch
BranchTrueFalseTotal
if (font->chars[i].ppm) 0 0 0
418 {
419 0 struct ppm *ppm = font->chars[i].ppm;
420 0 struct ppm *halo = blur_ppm (ppm, radius);
421 0 unsigned char *p = halo->rgba;
422 0 unsigned char *end = p + (halo->width * halo->height * 4);
423  
424 /* Set the halo pixels to be the background color (white). */
425 0 ▶ while (p < end)
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) 0 0 0
426 {
427 0 *p++ = 255;
428 0 *p++ = 255;
429 0 *p++ = 255;
430 0 p++;
431 }
432  
433 /* Overlay the character bits (black) on the halo bits (white). */
434 0 paste_ppm (halo, radius, radius,
435 ppm, 0, 0, ppm->width, ppm->height,
436 -1, -1, 255);
437  
438 /* Now install the result in the font. */
439 0 font->chars[i].ppm = halo;
440 0 free_ppm (ppm);
441  
442 /* The size of the PPM changed, so adjust the offsets into it. */
443 0 font->chars[i].lbearing += radius;
444 0 font->chars[i].descent -= radius;
445 }
446  
447 0 font->monochrome_p = 0;
448 0 }
449  
450 void
451 0 dump_font (struct font *font, int which)
452 {
453 int i;
454 0 int alpha = 128;
455  
456 0 ▶ for (i = 0; i < countof(font->chars); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < countof(font->chars); i++) 0 0 0
457 {
458 int x, y;
459 int x1, x2, y1, y2;
460  
461 0 struct ppm *ppm = font->chars[i].ppm;
462 0 ▶ if (!ppm)
Branch: 1 branch
BranchTrueFalseTotal
if (!ppm) 0 0 0
463 0 continue;
464  
465 0 ▶ if (which > 0 && which != i)
Branch: 2 branches
BranchTrueFalseTotal
if (which > 0 && which != i) 0 0 0
if (which > 0 && which != i) 0 0 0
466 0 continue;
467  
468 0 x1 = font->chars[i].lbearing;
469 0 x2 = font->chars[i].width + font->chars[i].lbearing;
470 0 y1 = ppm->height + font->chars[i].descent - font->ascent;
471 0 y2 = ppm->height + font->chars[i].descent;
472  
473 0 fprintf (stderr, "%3d: %c ", i, i);
474 0 ▶ for (x = 0; x < ppm->width; x++)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < ppm->width; x++) 0 0 0
475 0 ▶ fprintf (stderr, "%c", (x >= 10 ? (x / 10) + '0' : ' '));
Branch: 1 branch
BranchTrueFalseTotal
fprintf (stderr, "%c", (x >= 10 ? (x / 10) + '0' : ' ')); 0 0 0
476 0 fputs ("\n", stderr);
477  
478 0 fprintf (stderr, " ");
479 0 ▶ for (x = 0; x < ppm->width; x++)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < ppm->width; x++) 0 0 0
480 0 fprintf (stderr, "%d", (x % 10));
481 0 fputs ("\n", stderr);
482  
483 0 fputs (" +", stderr);
484  
485 0 ▶ for (x = 0; x < ppm->width; x++)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < ppm->width; x++) 0 0 0
486 0 ▶ fputs ((x == x1 ? "/" : x == x2 ? "\\" : "-"), stderr);
Branch: 2 branches
BranchTrueFalseTotal
fputs ((x == x1 ? "/" : x == x2 ? "\\" : "-"), stderr); 0 0 0
fputs ((x == x1 ? "/" : x == x2 ? "\\" : "-"), stderr); 0 0 0
487 0 fprintf (stderr, "+ %d,%d - %d,%d\n", x1, y1, x2, y2);
488  
489 0 ▶ for (y = 0; y < ppm->height; y++)
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < ppm->height; y++) 0 0 0
490 {
491 0 ▶ fprintf (stderr, " %2d: %c",
Branch: 1 branch
BranchTrueFalseTotal
fprintf (stderr, " %2d: %c", 0 0 0
492 0 ▶ y, (y == y1 ? '/' : y == y2 ? '\\' : '|'));
Branch: 1 branch
BranchTrueFalseTotal
y, (y == y1 ? '/' : y == y2 ? '\\' : '|')); 0 0 0
493 0 ▶ for (x = 0; x < ppm->width; x++)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < ppm->width; x++) 0 0 0
494 {
495 unsigned char r, g, b, a;
496 0 get_pixel (ppm, x, y, &r, &g, &b, &a);
497 0 ▶ if (a >= alpha)
Branch: 1 branch
BranchTrueFalseTotal
if (a >= alpha) 0 0 0
498 0 fputs ("#", stderr);
499 0 ▶ else if (x == x1 || x == x2 ||
Branch: 3 branches
BranchTrueFalseTotal
else if (x == x1 || x == x2 || 0 0 0
else if (x == x1 || x == x2 || 0 0 0
else if (x == x1 || x == x2 || 0 0 0
500 0 ▶ y == y1 || y == y2)
Branch: 1 branch
BranchTrueFalseTotal
y == y1 || y == y2) 0 0 0
501 0 fputs (":", stderr);
502 0 ▶ else if (a > 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (a > 0) 0 0 0
503 0 fputs ("-", stderr);
504 else
505 0 fputs (" ", stderr);
506 }
507 0 ▶ fprintf (stderr, "%c\n", (y == y1 ? '\\' : y == y2 ? '/' : '|'));
Branch: 2 branches
BranchTrueFalseTotal
fprintf (stderr, "%c\n", (y == y1 ? '\\' : y == y2 ? '/' : '|')); 0 0 0
fprintf (stderr, "%c\n", (y == y1 ? '\\' : y == y2 ? '/' : '|')); 0 0 0
508 }
509  
510 0 fputs (" +", stderr);
511 0 ▶ for (x = 0; x < ppm->width; x++)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < ppm->width; x++) 0 0 0
512 0 ▶ fputs ((x == x1 ? "\\" : x == x2 ? "/" : "-"), stderr);
Branch: 2 branches
BranchTrueFalseTotal
fputs ((x == x1 ? "\\" : x == x2 ? "/" : "-"), stderr); 0 0 0
fputs ((x == x1 ? "\\" : x == x2 ? "/" : "-"), stderr); 0 0 0
513 0 fputs ("+\n", stderr);
514  
515 }
516 0 }
« blib/lib/Image/Caption.pm ↑ Summary ppm-lite.c »