Debian Codegraph @

W32/win32.c

raw
1 #define _WIN32_WINNT 0x500
2 #include "imw32.h"
3 #define STRICT
4 #include <windows.h>
5 #include "imext.h"
6
7 /*
8 =head1 NAME
9
10 win32.c - implements some win32 specific code, specifically Win32 font support.
11
12 =head1 SYNOPSIS
13
14 i_img_dim bbox[6];
15 if (i_wf_bbox(facename, size, text, text_len, bbox)) {
16 // we have the bbox
17 }
18 i_wf_text(face, im, tx, ty, cl, size, text, len, align, aa, utf8);
19 i_wf_cp(face, im, tx, ty, channel, size, text, len, align, aa, utf8)
20
21 =head1 DESCRIPTION
22
23 An Imager interface to font output using the Win32 GDI.
24
25 =over
26
27 =cut
28 */
29
30 #define fixed(x) ((x).value + ((x).fract) / 65536.0)
31
32 static void set_logfont(const char *face, int size, LOGFONT *lf);
33
34 static unsigned char *render_text(const char *face, int size, const char *text, size_t length, int aa,
35 SIZE *psz, TEXTMETRIC *tm, size_t *bytes_per_line, i_img_dim *bbox, int utf8);
36 static LPWSTR utf8_to_wide_string(char const *text, int text_len, int *wide_chars);
37 static LPWSTR latin1_to_wide_string(char const *text, int text_len, int *wide_chars);
38
39 /*
40 =item i_wf_bbox(face, size, text, length, bbox, utf8)
41
42 Calculate a bounding box for the text.
43
44 =cut
45 */
46
47 int i_wf_bbox(const char *face, i_img_dim size, const char *text, size_t length,
48 i_img_dim *bbox, int utf8) {
49 LOGFONT lf;
50 HFONT font, oldFont;
51 HDC dc;
52 SIZE sz;
53 TEXTMETRIC tm;
54 ABC first, last;
55 GLYPHMETRICS gm;
56 MAT2 mat;
57 int ascent, descent, max_ascent = -size, min_descent = size;
58 const char *workp;
59 size_t work_len;
60 int got_first_ch = 0;
61 unsigned long first_ch, last_ch;
62
63 i_clear_error();
64
65 mm_log((1, "i_wf_bbox(face %s, size %d, text %p, length %d, bbox %p, utf8 %d)\n", face, size, text, length, bbox, utf8));
66
67 set_logfont(face, size, &lf);
68 font = CreateFontIndirect(&lf);
69 if (!font)
70 return 0;
71 dc = GetDC(NULL);
72 oldFont = (HFONT)SelectObject(dc, font);
73
74 {
75 char facename[100];
76 if (GetTextFace(dc, sizeof(facename), facename)) {
77 mm_log((1, " face: %s\n", facename));
78 }
79 }
80
81 workp = text;
82 work_len = length;
83 while (work_len > 0) {
84 unsigned long c;
85 unsigned char cp;
86
87 if (utf8) {
88 c = i_utf8_advance(&workp, &work_len);
89 if (c == ~0UL) {
90 i_push_error(0, "invalid UTF8 character");
91 return 0;
92 }
93 }
94 else {
95 c = (unsigned char)*workp++;
96 --work_len;
97 }
98 if (!got_first_ch) {
99 first_ch = c;
100 ++got_first_ch;
101 }
102 last_ch = c;
103
104 cp = c > '~' ? '.' : c < ' ' ? '.' : c;
105
106 memset(&mat, 0, sizeof(mat));
107 mat.eM11.value = 1;
108 mat.eM22.value = 1;
109 if (GetGlyphOutline(dc, (UINT)c, GGO_METRICS, &gm, 0, NULL, &mat) != GDI_ERROR) {
110 mm_log((2, " glyph '%c' (%02x): bbx (%u,%u) org (%d,%d) inc(%d,%d)\n",
111 cp, c, gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmptGlyphOrigin.x,
112 gm.gmptGlyphOrigin.y, gm.gmCellIncX, gm.gmCellIncY));
113
114 ascent = gm.gmptGlyphOrigin.y;
115 descent = ascent - gm.gmBlackBoxY;
116 if (ascent > max_ascent) max_ascent = ascent;
117 if (descent < min_descent) min_descent = descent;
118 }
119 else {
120 mm_log((1, " glyph '%c' (%02x): error %d\n", cp, c, GetLastError()));
121 }
122 }
123
124 if (utf8) {
125 int wide_chars;
126 LPWSTR wide_text = utf8_to_wide_string(text, length, &wide_chars);
127
128 if (!wide_text)
129 return 0;
130
131 if (!GetTextExtentPoint32W(dc, wide_text, wide_chars, &sz)
132 || !GetTextMetrics(dc, &tm)) {
133 SelectObject(dc, oldFont);
134 ReleaseDC(NULL, dc);
135 DeleteObject(font);
136 return 0;
137 }
138
139 myfree(wide_text);
140 }
141 else {
142 if (!GetTextExtentPoint32(dc, text, length, &sz)
143 || !GetTextMetrics(dc, &tm)) {
144 SelectObject(dc, oldFont);
145 ReleaseDC(NULL, dc);
146 DeleteObject(font);
147 return 0;
148 }
149 }
150 bbox[BBOX_GLOBAL_DESCENT] = -tm.tmDescent;
151 bbox[BBOX_DESCENT] = min_descent == size ? -tm.tmDescent : min_descent;
152 bbox[BBOX_POS_WIDTH] = sz.cx;
153 bbox[BBOX_ADVANCE_WIDTH] = sz.cx;
154 bbox[BBOX_GLOBAL_ASCENT] = tm.tmAscent;
155 bbox[BBOX_ASCENT] = max_ascent == -size ? tm.tmAscent : max_ascent;
156
157 if (length
158 && GetCharABCWidths(dc, first_ch, first_ch, &first)
159 && GetCharABCWidths(dc, last_ch, last_ch, &last)) {
160 mm_log((1, "first: %d A: %d B: %d C: %d\n", first_ch,
161 first.abcA, first.abcB, first.abcC));
162 mm_log((1, "last: %d A: %d B: %d C: %d\n", last_ch,
163 last.abcA, last.abcB, last.abcC));
164 bbox[BBOX_NEG_WIDTH] = first.abcA;
165 bbox[BBOX_RIGHT_BEARING] = last.abcC;
166 if (last.abcC < 0)
167 bbox[BBOX_POS_WIDTH] -= last.abcC;
168 }
169 else {
170 bbox[BBOX_NEG_WIDTH] = 0;
171 bbox[BBOX_RIGHT_BEARING] = 0;
172 }
173
174 SelectObject(dc, oldFont);
175 ReleaseDC(NULL, dc);
176 DeleteObject(font);
177
178 mm_log((1, " bbox=> negw=%" i_DF " glob_desc=%" i_DF " pos_wid=%" i_DF
179 " glob_asc=%" i_DF " desc=%" i_DF " asc=%" i_DF " adv_width=%" i_DF
180 " rightb=%" i_DF "\n", i_DFc(bbox[0]), i_DFc(bbox[1]), i_DFc(bbox[2]),
181 i_DFc(bbox[3]), i_DFc(bbox[4]), i_DFc(bbox[5]), i_DFc(bbox[6]),
182 i_DFc(bbox[7])));
183
184 return BBOX_RIGHT_BEARING + 1;
185 }
186
187 /*
188 =item i_wf_text(face, im, tx, ty, cl, size, text, len, align, aa)
189
190 Draws the text in the given color.
191
192 =cut
193 */
194
195 int
196 i_wf_text(const char *face, i_img *im, i_img_dim tx, i_img_dim ty, const i_color *cl, i_img_dim size,
197 const char *text, size_t len, int align, int aa, int utf8) {
198 unsigned char *bits;
199 SIZE sz;
200 size_t line_width;
201 i_img_dim y;
202 TEXTMETRIC tm;
203 int top;
204 i_img_dim bbox[BOUNDING_BOX_COUNT];
205 i_render *r;
206 unsigned char *outp;
207
208 i_clear_error();
209
210 mm_log((1, "i_wf_text(face %s, im %p, tx %" i_DF ", ty %" i_DF ", cl %p, size %" i_DF ", text %p, length %lu, align %d, aa %d, utf8 %d)\n", face, im, i_DFcp(tx, ty), cl, i_DFc(size), text, (unsigned long)len, align, aa, aa, utf8));
211
212 if (!i_wf_bbox(face, size, text, len, bbox, utf8))
213 return 0;
214
215 bits = render_text(face, size, text, len, aa, &sz, &tm, &line_width, bbox, utf8);
216 if (!bits)
217 return 0;
218
219 tx += bbox[BBOX_NEG_WIDTH];
220 top = ty;
221 if (align) {
222 top -= tm.tmAscent;
223 }
224 else {
225 top -= tm.tmAscent - bbox[BBOX_ASCENT];
226 }
227
228 r = i_render_new(im, sz.cx);
229 outp = bits;
230 for (y = 0; y < sz.cy; ++y) {
231 i_render_color(r, tx, top + sz.cy - y - 1, sz.cx, outp, cl);
232 outp += line_width;
233 }
234 i_render_delete(r);
235 myfree(bits);
236
237 return 1;
238 }
239
240 /*
241 =item i_wf_cp(face, im, tx, ty, channel, size, text, len, align, aa)
242
243 Draws the text in the given channel.
244
245 =cut
246 */
247
248 int
249 i_wf_cp(const char *face, i_img *im, i_img_dim tx, i_img_dim ty, int channel, i_img_dim size,
250 const char *text, size_t len, int align, int aa, int utf8) {
251 unsigned char *bits;
252 SIZE sz;
253 size_t line_width;
254 i_img_dim x, y;
255 TEXTMETRIC tm;
256 i_img_dim top;
257 i_img_dim bbox[BOUNDING_BOX_COUNT];
258 unsigned char *outp;
259
260 i_clear_error();
261
262 mm_log((1, "i_wf_cp(face %s, im %p, tx %" i_DF ", ty %" i_DF ", channel %d, size %" i_DF ", text %p, length %lu, align %d, aa %d, utf8 %d)\n", face, im, i_DFcp(tx, ty), channel, i_DFc(size), text, (unsigned long)len, align, aa, aa, utf8));
263
264 if (!i_wf_bbox(face, size, text, len, bbox, utf8))
265 return 0;
266
267 bits = render_text(face, size, text, len, aa, &sz, &tm, &line_width, bbox, utf8);
268 if (!bits)
269 return 0;
270
271 top = ty;
272 if (align) {
273 top -= tm.tmAscent;
274 }
275 else {
276 top -= tm.tmAscent - bbox[BBOX_ASCENT];
277 }
278
279 outp = bits;
280 for (y = 0; y < sz.cy; ++y) {
281 for (x = 0; x < sz.cx; ++x) {
282 i_color pel;
283 int scale = outp[x];
284 i_gpix(im, tx+x, top+sz.cy-y-1, &pel);
285 pel.channel[channel] = scale;
286 i_ppix(im, tx+x, top+sz.cy-y-1, &pel);
287 }
288 outp += line_width;
289 }
290 myfree(bits);
291
292 return 1;
293 }
294
295 /* a function pointer type that prevents cast-function-type warnings */
296 typedef void (*NeutralFunction_t)(void);
297 static HMODULE gdi_dll;
298 typedef BOOL (CALLBACK *AddFontResourceExA_t)(LPCSTR, DWORD, PVOID);
299 static AddFontResourceExA_t AddFontResourceExAp;
300 typedef BOOL (CALLBACK *RemoveFontResourceExA_t)(LPCSTR, DWORD, PVOID);
301 static RemoveFontResourceExA_t RemoveFontResourceExAp;
302
303 /*
304 =item i_wf_addfont(char const *filename, char const *resource_file)
305
306 Adds a TTF font file as usable by the application.
307
308 Where possible the font is added as private to the application.
309
310 Under Windows 95/98/ME the font is added globally, since we don't have
311 any choice. In either case call i_wf_delfont() to remove it.
312
313 =cut
314 */
315 int
316 i_wf_addfont(char const *filename) {
317 i_clear_error();
318
319 mm_log((1, "i_wf_addfont(%s)\n", filename));
320 if (!gdi_dll) {
321 gdi_dll = GetModuleHandle("GDI32");
322 if (gdi_dll) {
323 AddFontResourceExAp = (AddFontResourceExA_t)(NeutralFunction_t)
324 GetProcAddress(gdi_dll, "AddFontResourceExA");
325 RemoveFontResourceExAp = (RemoveFontResourceExA_t)(NeutralFunction_t)
326 GetProcAddress(gdi_dll, "RemoveFontResourceExA");
327 mm_log((1, "i_wf_addfont: AddFontResourceExA %p RemoveFontResourceExA %p\n",
328 AddFontResourceExAp, RemoveFontResourceExAp));
329 }
330 }
331
332 if (AddFontResourceExAp && RemoveFontResourceExAp) {
333 mm_log((1, "i_wf_addfont: adding via AddFontResourceEx()\n"));
334 if (AddFontResourceExAp(filename, FR_PRIVATE, 0)) {
335 return 1;
336 }
337 }
338 else {
339 mm_log((1, "i_wf_addfont: adding via AddFontResource()\n"));
340 if (AddFontResource(filename)) {
341 return 1;
342 }
343 }
344
345 mm_log((1, "i_wf_addfont failed: %ld\n", GetLastError()));
346 i_push_errorf(0, "Could not add resource: %ld", GetLastError());
347 return 0;
348 }
349
350 /*
351 =item i_wf_delfont(char const *filename, char const *resource_file)
352
353 Deletes a TTF font file as usable by the application.
354
355 =cut
356 */
357 int
358 i_wf_delfont(char const *filename) {
359 i_clear_error();
360
361 mm_log((1, "i_wf_delfont(%s)\n", filename));
362
363 if (AddFontResourceExAp && RemoveFontResourceExAp) {
364 mm_log((1, "i_wf_delfont: removing via RemoveFontResourceEx()\n"));
365 if (RemoveFontResourceExAp(filename, FR_PRIVATE, 0))
366 return 1;
367 }
368 else {
369 mm_log((1, "i_wf_delfont: adding via RemoveFontResourceEx()\n"));
370 if (RemoveFontResource(filename))
371 return 1;
372 }
373
374 mm_log((1, "i_wf_delfont failed: %ld\n", GetLastError()));
375 i_push_errorf(0, "Could not remove resource: %ld", GetLastError());
376 return 0;
377 }
378
379 /*
380 =back
381
382 =head1 INTERNAL FUNCTIONS
383
384 =over
385
386 =item set_logfont(face, size, lf)
387
388 Fills in a LOGFONT structure with reasonable defaults.
389
390 =cut
391 */
392
393 static void set_logfont(const char *face, int size, LOGFONT *lf) {
394 memset(lf, 0, sizeof(LOGFONT));
395
396 lf->lfHeight = -size; /* character height rather than cell height */
397 lf->lfCharSet = DEFAULT_CHARSET;
398 lf->lfOutPrecision = OUT_TT_PRECIS;
399 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
400 lf->lfQuality = PROOF_QUALITY;
401 strncpy(lf->lfFaceName, face, sizeof(lf->lfFaceName)-1);
402 /* NUL terminated by the memset at the top */
403 }
404
405 /*
406 =item render_text(face, size, text, length, aa, pbm, psz, tm)
407
408 renders the text to an in-memory RGB bitmap
409
410 It would be nice to render to greyscale, but Windows doesn't have
411 native greyscale bitmaps.
412
413 =cut
414 */
415 static unsigned char *
416 render_text(const char *face, int size, const char *text, size_t length, int aa,
417 SIZE *psz, TEXTMETRIC *tm, size_t *bytes_per_line, i_img_dim *bbox, int utf8) {
418 BITMAPINFO bmi;
419 BITMAPINFOHEADER *bmih = &bmi.bmiHeader;
420 HDC dc, bmpDc;
421 LOGFONT lf;
422 HFONT font, oldFont;
423 SIZE sz;
424 HBITMAP bm, oldBm;
425 LPVOID bits;
426 int wide_count;
427 LPWSTR wide_text;
428 unsigned char *result;
429
430 dc = GetDC(NULL);
431 set_logfont(face, size, &lf);
432
433 #ifdef ANTIALIASED_QUALITY
434 /* See KB article Q197076
435 "INFO: Controlling Anti-aliased Text via the LOGFONT Structure"
436 */
437 lf.lfQuality = aa ? ANTIALIASED_QUALITY : NONANTIALIASED_QUALITY;
438 #endif
439
440 if (utf8) {
441 wide_text = utf8_to_wide_string(text, length, &wide_count);
442 }
443 else {
444 wide_text = latin1_to_wide_string(text, length, &wide_count);
445 }
446
447 bmpDc = CreateCompatibleDC(dc);
448 if (bmpDc) {
449 font = CreateFontIndirect(&lf);
450 if (font) {
451 oldFont = SelectObject(bmpDc, font);
452
453 GetTextMetrics(bmpDc, tm);
454 sz.cx = bbox[BBOX_ADVANCE_WIDTH] - bbox[BBOX_NEG_WIDTH] + bbox[BBOX_POS_WIDTH];
455 sz.cy = bbox[BBOX_GLOBAL_ASCENT] - bbox[BBOX_GLOBAL_DESCENT];
456
457 memset(&bmi, 0, sizeof(bmi));
458 bmih->biSize = sizeof(*bmih);
459 bmih->biWidth = sz.cx;
460 bmih->biHeight = sz.cy;
461 bmih->biPlanes = 1;
462 bmih->biBitCount = 24;
463 bmih->biCompression = BI_RGB;
464 bmih->biSizeImage = 0;
465 bmih->biXPelsPerMeter = (LONG)(72 / 2.54 * 100);
466 bmih->biYPelsPerMeter = bmih->biXPelsPerMeter;
467 bmih->biClrUsed = 0;
468 bmih->biClrImportant = 0;
469
470 bm = CreateDIBSection(dc, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
471
472 if (bm) {
473 oldBm = SelectObject(bmpDc, bm);
474 SetTextColor(bmpDc, RGB(255, 255, 255));
475 SetBkColor(bmpDc, RGB(0, 0, 0));
476 TextOutW(bmpDc, -bbox[BBOX_NEG_WIDTH], 0, wide_text, wide_count);
477 SelectObject(bmpDc, oldBm);
478 }
479 else {
480 i_push_errorf(0, "Could not create DIB section for render: %ld",
481 GetLastError());
482 SelectObject(bmpDc, oldFont);
483 DeleteObject(font);
484 DeleteDC(bmpDc);
485 ReleaseDC(NULL, dc);
486 myfree(wide_text);
487 return NULL;
488 }
489 SelectObject(bmpDc, oldFont);
490 DeleteObject(font);
491 }
492 else {
493 myfree(wide_text);
494 i_push_errorf(0, "Could not create logical font: %ld",
495 GetLastError());
496 DeleteDC(bmpDc);
497 ReleaseDC(NULL, dc);
498 return NULL;
499 }
500 DeleteDC(bmpDc);
501 }
502 else {
503 myfree(wide_text);
504 i_push_errorf(0, "Could not create rendering DC: %ld", GetLastError());
505 ReleaseDC(NULL, dc);
506 return NULL;
507 }
508
509 myfree(wide_text);
510
511 ReleaseDC(NULL, dc);
512
513 *psz = sz;
514
515 /* convert into a map we can just pass to i_render_color() */
516 {
517 i_img_dim x, y;
518 unsigned char *outp, *ucbits;
519 size_t bits_line_width;
520
521 *bytes_per_line = sz.cx;
522 result = mymalloc(sz.cx * sz.cy);
523 outp = result;
524 ucbits = bits;
525 bits_line_width = sz.cx * 3;
526 bits_line_width = (bits_line_width + 3) / 4 * 4;
527
528 for (y = 0; y < sz.cy; ++y) {
529 for (x = 0; x < sz.cx; ++x) {
530 *outp++ = ucbits[3 * x];
531 }
532 ucbits += bits_line_width;
533 }
534 }
535 DeleteObject(bm);
536
537 return result;
538 }
539
540 /*
541 =item utf8_to_wide_string(text, text_len, wide_chars)
542
543 =cut
544 */
545
546 static
547 LPWSTR
548 utf8_to_wide_string(char const *text, int text_len, int *wide_chars) {
549 int wide_count = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, text, text_len, NULL, 0);
550 LPWSTR result;
551
552 if (wide_count < 0) {
553 i_push_errorf(0, "Could not convert utf8: %ld", GetLastError());
554 return NULL;
555 }
556 ++wide_count;
557 result = mymalloc(sizeof(WCHAR) * wide_count);
558 if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, text, text_len, result, wide_count) < 0) {
559 i_push_errorf(0, "Could not convert utf8: %ld", GetLastError());
560 return NULL;
561 }
562
563 result[wide_count-1] = (WCHAR)'\0';
564 *wide_chars = wide_count - 1;
565
566 return result;
567 }
568
569 static LPWSTR
570 latin1_to_wide_string(char const *text, int text_len, int *wide_chars) {
571 LPWSTR result = mymalloc(sizeof(WCHAR) * (text_len + 1));
572 int i;
573
574 for (i = 0; i < text_len; ++i) {
575 result[i] = (unsigned char)text[i];
576 }
577 result[i] = 0;
578 *wide_chars = text_len;
579
580 return result;
581 }
582
583 /*
584 =back
585
586 =head1 BUGS
587
588 Should really use a structure so we can set more attributes.
589
590 Should support UTF8
591
592 =head1 AUTHOR
593
594 Tony Cook <[email protected]>
595
596 =head1 SEE ALSO
597
598 Imager(3)
599
600 =cut
601 */