1 #define _WIN32_WINNT 0x500
10 win32.c - implements some win32 specific code, specifically Win32 font support.
15 if (i_wf_bbox(facename, size, text, text_len, bbox)) {
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)
23 An Imager interface to font output using the Win32 GDI.
30 #define fixed(x) ((x).value + ((x).fract) / 65536.0)
32 static void set_logfont(const char *face, int size, LOGFONT *lf);
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);
40 =item i_wf_bbox(face, size, text, length, bbox, utf8)
42 Calculate a bounding box for the text.
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) {
57 int ascent, descent, max_ascent = -size, min_descent = size;
61 unsigned long first_ch, last_ch;
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));
67 set_logfont(face, size, &lf);
68 font = CreateFontIndirect(&lf);
72 oldFont = (HFONT)SelectObject(dc, font);
76 if (GetTextFace(dc, sizeof(facename), facename)) {
77 mm_log((1, " face: %s\n", facename));
83 while (work_len > 0) {
88 c = i_utf8_advance(&workp, &work_len);
90 i_push_error(0, "invalid UTF8 character");
95 c = (unsigned char)*workp++;
104 cp = c > '~' ? '.' : c < ' ' ? '.' : c;
106 memset(&mat, 0, sizeof(mat));
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));
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;
120 mm_log((1, " glyph '%c' (%02x): error %d\n", cp, c, GetLastError()));
126 LPWSTR wide_text = utf8_to_wide_string(text, length, &wide_chars);
131 if (!GetTextExtentPoint32W(dc, wide_text, wide_chars, &sz)
132 || !GetTextMetrics(dc, &tm)) {
133 SelectObject(dc, oldFont);
142 if (!GetTextExtentPoint32(dc, text, length, &sz)
143 || !GetTextMetrics(dc, &tm)) {
144 SelectObject(dc, oldFont);
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;
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;
167 bbox[BBOX_POS_WIDTH] -= last.abcC;
170 bbox[BBOX_NEG_WIDTH] = 0;
171 bbox[BBOX_RIGHT_BEARING] = 0;
174 SelectObject(dc, oldFont);
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]),
184 return BBOX_RIGHT_BEARING + 1;
188 =item i_wf_text(face, im, tx, ty, cl, size, text, len, align, aa)
190 Draws the text in the given color.
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) {
205 i_img_dim bbox[BOUNDING_BOX_COUNT];
211 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));
213 if (!i_wf_bbox(face, size, text, len, bbox, utf8))
216 bits = render_text(face, size, text, len, aa, &sz, &tm, &line_width, bbox, utf8);
220 tx += bbox[BBOX_NEG_WIDTH];
226 top -= tm.tmAscent - bbox[BBOX_ASCENT];
229 r = i_render_new(im, sz.cx);
231 for (y = 0; y < sz.cy; ++y) {
232 i_render_color(r, tx, top + sz.cy - y - 1, sz.cx, outp, cl);
242 =item i_wf_cp(face, im, tx, ty, channel, size, text, len, align, aa)
244 Draws the text in the given channel.
250 i_wf_cp(const char *face, i_img *im, i_img_dim tx, i_img_dim ty, int channel, i_img_dim size,
251 const char *text, size_t len, int align, int aa, int utf8) {
258 i_img_dim bbox[BOUNDING_BOX_COUNT];
263 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));
265 if (!i_wf_bbox(face, size, text, len, bbox, utf8))
268 bits = render_text(face, size, text, len, aa, &sz, &tm, &line_width, bbox, utf8);
277 top -= tm.tmAscent - bbox[BBOX_ASCENT];
281 for (y = 0; y < sz.cy; ++y) {
282 for (x = 0; x < sz.cx; ++x) {
285 i_gpix(im, tx+x, top+sz.cy-y-1, &pel);
286 pel.channel[channel] = scale;
287 i_ppix(im, tx+x, top+sz.cy-y-1, &pel);
296 static HMODULE gdi_dll;
297 typedef BOOL (CALLBACK *AddFontResourceExA_t)(LPCSTR, DWORD, PVOID);
298 static AddFontResourceExA_t AddFontResourceExAp;
299 typedef BOOL (CALLBACK *RemoveFontResourceExA_t)(LPCSTR, DWORD, PVOID);
300 static RemoveFontResourceExA_t RemoveFontResourceExAp;
303 =item i_wf_addfont(char const *filename, char const *resource_file)
305 Adds a TTF font file as usable by the application.
307 Where possible the font is added as private to the application.
309 Under Windows 95/98/ME the font is added globally, since we don't have
310 any choice. In either case call i_wf_delfont() to remove it.
315 i_wf_addfont(char const *filename) {
318 mm_log((1, "i_wf_addfont(%s)\n", filename));
320 gdi_dll = GetModuleHandle("GDI32");
322 AddFontResourceExAp = (AddFontResourceExA_t)GetProcAddress(gdi_dll, "AddFontResourceExA");
323 RemoveFontResourceExAp = (RemoveFontResourceExA_t)GetProcAddress(gdi_dll, "RemoveFontResourceExA");
324 mm_log((1, "i_wf_addfont: AddFontResourceExA %p RemoveFontResourceExA %p\n",
325 AddFontResourceExAp, RemoveFontResourceExAp));
329 if (AddFontResourceExAp && RemoveFontResourceExAp) {
330 mm_log((1, "i_wf_addfont: adding via AddFontResourceEx()\n"));
331 if (AddFontResourceExAp(filename, FR_PRIVATE, 0)) {
336 mm_log((1, "i_wf_addfont: adding via AddFontResource()\n"));
337 if (AddFontResource(filename)) {
342 mm_log((1, "i_wf_addfont failed: %ld\n", GetLastError()));
343 i_push_errorf(0, "Could not add resource: %ld", GetLastError());
348 =item i_wf_delfont(char const *filename, char const *resource_file)
350 Deletes a TTF font file as usable by the application.
355 i_wf_delfont(char const *filename) {
358 mm_log((1, "i_wf_delfont(%s)\n", filename));
360 if (AddFontResourceExAp && RemoveFontResourceExAp) {
361 mm_log((1, "i_wf_delfont: removing via RemoveFontResourceEx()\n"));
362 if (RemoveFontResourceExAp(filename, FR_PRIVATE, 0))
366 mm_log((1, "i_wf_delfont: adding via RemoveFontResourceEx()\n"));
367 if (RemoveFontResource(filename))
371 mm_log((1, "i_wf_delfont failed: %ld\n", GetLastError()));
372 i_push_errorf(0, "Could not remove resource: %ld", GetLastError());
379 =head1 INTERNAL FUNCTIONS
383 =item set_logfont(face, size, lf)
385 Fills in a LOGFONT structure with reasonable defaults.
390 static void set_logfont(const char *face, int size, LOGFONT *lf) {
391 memset(lf, 0, sizeof(LOGFONT));
393 lf->lfHeight = -size; /* character height rather than cell height */
394 lf->lfCharSet = DEFAULT_CHARSET;
395 lf->lfOutPrecision = OUT_TT_PRECIS;
396 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
397 lf->lfQuality = PROOF_QUALITY;
398 strncpy(lf->lfFaceName, face, sizeof(lf->lfFaceName)-1);
399 /* NUL terminated by the memset at the top */
403 =item render_text(face, size, text, length, aa, pbm, psz, tm)
405 renders the text to an in-memory RGB bitmap
407 It would be nice to render to greyscale, but Windows doesn't have
408 native greyscale bitmaps.
412 static unsigned char *
413 render_text(const char *face, int size, const char *text, size_t length, int aa,
414 SIZE *psz, TEXTMETRIC *tm, size_t *bytes_per_line, i_img_dim *bbox, int utf8) {
416 BITMAPINFOHEADER *bmih = &bmi.bmiHeader;
425 unsigned char *result;
428 set_logfont(face, size, &lf);
430 #ifdef ANTIALIASED_QUALITY
431 /* See KB article Q197076
432 "INFO: Controlling Anti-aliased Text via the LOGFONT Structure"
434 lf.lfQuality = aa ? ANTIALIASED_QUALITY : NONANTIALIASED_QUALITY;
438 wide_text = utf8_to_wide_string(text, length, &wide_count);
441 wide_text = latin1_to_wide_string(text, length, &wide_count);
444 bmpDc = CreateCompatibleDC(dc);
446 font = CreateFontIndirect(&lf);
448 oldFont = SelectObject(bmpDc, font);
450 GetTextMetrics(bmpDc, tm);
451 sz.cx = bbox[BBOX_ADVANCE_WIDTH] - bbox[BBOX_NEG_WIDTH] + bbox[BBOX_POS_WIDTH];
452 sz.cy = bbox[BBOX_GLOBAL_ASCENT] - bbox[BBOX_GLOBAL_DESCENT];
454 memset(&bmi, 0, sizeof(bmi));
455 bmih->biSize = sizeof(*bmih);
456 bmih->biWidth = sz.cx;
457 bmih->biHeight = sz.cy;
459 bmih->biBitCount = 24;
460 bmih->biCompression = BI_RGB;
461 bmih->biSizeImage = 0;
462 bmih->biXPelsPerMeter = (LONG)(72 / 2.54 * 100);
463 bmih->biYPelsPerMeter = bmih->biXPelsPerMeter;
465 bmih->biClrImportant = 0;
467 bm = CreateDIBSection(dc, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
470 oldBm = SelectObject(bmpDc, bm);
471 SetTextColor(bmpDc, RGB(255, 255, 255));
472 SetBkColor(bmpDc, RGB(0, 0, 0));
473 TextOutW(bmpDc, -bbox[BBOX_NEG_WIDTH], 0, wide_text, wide_count);
474 SelectObject(bmpDc, oldBm);
477 i_push_errorf(0, "Could not create DIB section for render: %ld",
479 SelectObject(bmpDc, oldFont);
486 SelectObject(bmpDc, oldFont);
491 i_push_errorf(0, "Could not create logical font: %ld",
501 i_push_errorf(0, "Could not create rendering DC: %ld", GetLastError());
512 /* convert into a map we can just pass to i_render_color() */
515 unsigned char *outp, *ucbits;
516 size_t bits_line_width;
518 *bytes_per_line = sz.cx;
519 result = mymalloc(sz.cx * sz.cy);
522 bits_line_width = sz.cx * 3;
523 bits_line_width = (bits_line_width + 3) / 4 * 4;
525 for (y = 0; y < sz.cy; ++y) {
526 for (x = 0; x < sz.cx; ++x) {
527 *outp++ = ucbits[3 * x];
529 ucbits += bits_line_width;
538 =item utf8_to_wide_string(text, text_len, wide_chars)
545 utf8_to_wide_string(char const *text, int text_len, int *wide_chars) {
546 int wide_count = MultiByteToWideChar(CP_UTF8, 0, text, text_len, NULL, 0);
549 if (wide_count < 0) {
550 i_push_errorf(0, "Could not convert utf8: %ld", GetLastError());
554 result = mymalloc(sizeof(WCHAR) * wide_count);
555 if (MultiByteToWideChar(CP_UTF8, 0, text, text_len, result, wide_count) < 0) {
556 i_push_errorf(0, "Could not convert utf8: %ld", GetLastError());
560 result[wide_count-1] = (WCHAR)'\0';
561 *wide_chars = wide_count - 1;
567 latin1_to_wide_string(char const *text, int text_len, int *wide_chars) {
568 LPWSTR result = mymalloc(sizeof(WCHAR) * (text_len + 1));
571 for (i = 0; i < text_len; ++i) {
572 result[i] = (unsigned char)text[i];
575 *wide_chars = text_len;
585 Should really use a structure so we can set more attributes.
591 Tony Cook <tony@develop-help.com>