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 LPVOID render_text(const char *face, int size, const char *text, int length, int aa,
35 HBITMAP *pbm, SIZE *psz, TEXTMETRIC *tm, i_img_dim *bbox, int utf8);
36 static LPWSTR utf8_to_wide_string(char const *text, int text_len, int *wide_chars);
39 =item i_wf_bbox(face, size, text, length, bbox, utf8)
41 Calculate a bounding box for the text.
46 int i_wf_bbox(const char *face, i_img_dim size, const char *text, size_t length,
47 i_img_dim *bbox, int utf8) {
56 int ascent, descent, max_ascent = -size, min_descent = size;
60 unsigned long first_ch, last_ch;
64 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 set_logfont(face, size, &lf);
67 font = CreateFontIndirect(&lf);
71 oldFont = (HFONT)SelectObject(dc, font);
75 if (GetTextFace(dc, sizeof(facename), facename)) {
76 mm_log((1, " face: %s\n", facename));
82 while (work_len > 0) {
87 c = i_utf8_advance(&workp, &work_len);
89 i_push_error(0, "invalid UTF8 character");
94 c = (unsigned char)*workp++;
103 cp = c > '~' ? '.' : c < ' ' ? '.' : c;
105 memset(&mat, 0, sizeof(mat));
108 if (GetGlyphOutline(dc, (UINT)c, GGO_METRICS, &gm, 0, NULL, &mat) != GDI_ERROR) {
109 mm_log((2, " glyph '%c' (%02x): bbx (%u,%u) org (%d,%d) inc(%d,%d)\n",
110 cp, c, gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmptGlyphOrigin.x,
111 gm.gmptGlyphOrigin.y, gm.gmCellIncX, gm.gmCellIncY));
113 ascent = gm.gmptGlyphOrigin.y;
114 descent = ascent - gm.gmBlackBoxY;
115 if (ascent > max_ascent) max_ascent = ascent;
116 if (descent < min_descent) min_descent = descent;
119 mm_log((1, " glyph '%c' (%02x): error %d\n", cp, c, GetLastError()));
125 LPWSTR wide_text = utf8_to_wide_string(text, length, &wide_chars);
130 if (!GetTextExtentPoint32W(dc, wide_text, wide_chars, &sz)
131 || !GetTextMetrics(dc, &tm)) {
132 SelectObject(dc, oldFont);
141 if (!GetTextExtentPoint32(dc, text, length, &sz)
142 || !GetTextMetrics(dc, &tm)) {
143 SelectObject(dc, oldFont);
149 bbox[BBOX_GLOBAL_DESCENT] = -tm.tmDescent;
150 bbox[BBOX_DESCENT] = min_descent == size ? -tm.tmDescent : min_descent;
151 bbox[BBOX_POS_WIDTH] = sz.cx;
152 bbox[BBOX_ADVANCE_WIDTH] = sz.cx;
153 bbox[BBOX_GLOBAL_ASCENT] = tm.tmAscent;
154 bbox[BBOX_ASCENT] = max_ascent == -size ? tm.tmAscent : max_ascent;
157 && GetCharABCWidths(dc, first_ch, first_ch, &first)
158 && GetCharABCWidths(dc, last_ch, last_ch, &last)) {
159 mm_log((1, "first: %d A: %d B: %d C: %d\n", first_ch,
160 first.abcA, first.abcB, first.abcC));
161 mm_log((1, "last: %d A: %d B: %d C: %d\n", last_ch,
162 last.abcA, last.abcB, last.abcC));
163 bbox[BBOX_NEG_WIDTH] = first.abcA;
164 bbox[BBOX_RIGHT_BEARING] = last.abcC;
166 bbox[BBOX_POS_WIDTH] -= last.abcC;
169 bbox[BBOX_NEG_WIDTH] = 0;
170 bbox[BBOX_RIGHT_BEARING] = 0;
173 SelectObject(dc, oldFont);
177 mm_log((1, " bbox=> negw=%" i_DF " glob_desc=%" i_DF " pos_wid=%" i_DF
178 " glob_asc=%" i_DF " desc=%" i_DF " asc=%" i_DF " adv_width=%" i_DF
179 " rightb=%" i_DF "\n", i_DFc(bbox[0]), i_DFc(bbox[1]), i_DFc(bbox[2]),
180 i_DFc(bbox[3]), i_DFc(bbox[4]), i_DFc(bbox[5]), i_DFc(bbox[6]),
183 return BBOX_RIGHT_BEARING + 1;
187 =item i_wf_text(face, im, tx, ty, cl, size, text, len, align, aa)
189 Draws the text in the given color.
195 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,
196 const char *text, size_t len, int align, int aa, int utf8) {
200 i_img_dim line_width;
205 i_img_dim bbox[BOUNDING_BOX_COUNT];
209 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 if (!i_wf_bbox(face, size, text, len, bbox, utf8))
214 bits = render_text(face, size, text, len, aa, &bm, &sz, &tm, bbox, utf8);
218 tx += bbox[BBOX_NEG_WIDTH];
219 line_width = sz.cx * 3;
220 line_width = (line_width + 3) / 4 * 4;
226 top -= tm.tmAscent - bbox[BBOX_ASCENT];
229 for (y = 0; y < sz.cy; ++y) {
230 for (x = 0; x < sz.cx; ++x) {
232 int scale = bits[3 * x];
233 i_gpix(im, tx+x, top+sz.cy-y-1, &pel);
234 for (ch = 0; ch < im->channels; ++ch) {
236 ((255-scale) * pel.channel[ch] + scale*cl->channel[ch]) / 255;
238 i_ppix(im, tx+x, top+sz.cy-y-1, &pel);
248 =item i_wf_cp(face, im, tx, ty, channel, size, text, len, align, aa)
250 Draws the text in the given channel.
256 i_wf_cp(const char *face, i_img *im, i_img_dim tx, i_img_dim ty, int channel, i_img_dim size,
257 const char *text, size_t len, int align, int aa, int utf8) {
265 i_img_dim bbox[BOUNDING_BOX_COUNT];
269 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));
271 if (!i_wf_bbox(face, size, text, len, bbox, utf8))
274 bits = render_text(face, size, text, len, aa, &bm, &sz, &tm, bbox, utf8);
278 line_width = sz.cx * 3;
279 line_width = (line_width + 3) / 4 * 4;
285 top -= tm.tmAscent - bbox[BBOX_ASCENT];
288 for (y = 0; y < sz.cy; ++y) {
289 for (x = 0; x < sz.cx; ++x) {
291 int scale = bits[3 * x];
292 i_gpix(im, tx+x, top+sz.cy-y-1, &pel);
293 pel.channel[channel] = scale;
294 i_ppix(im, tx+x, top+sz.cy-y-1, &pel);
303 static HMODULE gdi_dll;
304 typedef BOOL (CALLBACK *AddFontResourceExA_t)(LPCSTR, DWORD, PVOID);
305 static AddFontResourceExA_t AddFontResourceExAp;
306 typedef BOOL (CALLBACK *RemoveFontResourceExA_t)(LPCSTR, DWORD, PVOID);
307 static RemoveFontResourceExA_t RemoveFontResourceExAp;
310 =item i_wf_addfont(char const *filename, char const *resource_file)
312 Adds a TTF font file as usable by the application.
314 Where possible the font is added as private to the application.
316 Under Windows 95/98/ME the font is added globally, since we don't have
317 any choice. In either case call i_wf_delfont() to remove it.
322 i_wf_addfont(char const *filename) {
326 gdi_dll = GetModuleHandle("GDI32");
328 AddFontResourceExAp = (AddFontResourceExA_t)GetProcAddress(gdi_dll, "AddFontResourceExA");
329 RemoveFontResourceExAp = (RemoveFontResourceExA_t)GetProcAddress(gdi_dll, "RemoveFontResourceExA");
333 if (AddFontResourceExAp && RemoveFontResourceExAp
334 && AddFontResourceExAp(filename, FR_PRIVATE, 0)) {
337 else if (AddFontResource(filename)) {
341 i_push_errorf(0, "Could not add resource: %ld", GetLastError());
347 =item i_wf_delfont(char const *filename, char const *resource_file)
349 Deletes a TTF font file as usable by the application.
354 i_wf_delfont(char const *filename) {
357 if (AddFontResourceExAp && RemoveFontResourceExAp
358 && RemoveFontResourceExAp(filename, FR_PRIVATE, 0)) {
361 else if (RemoveFontResource(filename)) {
365 i_push_errorf(0, "Could not remove resource: %ld", GetLastError());
373 =head1 INTERNAL FUNCTIONS
377 =item set_logfont(face, size, lf)
379 Fills in a LOGFONT structure with reasonable defaults.
384 static void set_logfont(const char *face, int size, LOGFONT *lf) {
385 memset(lf, 0, sizeof(LOGFONT));
387 lf->lfHeight = -size; /* character height rather than cell height */
388 lf->lfCharSet = DEFAULT_CHARSET;
389 lf->lfOutPrecision = OUT_TT_PRECIS;
390 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
391 lf->lfQuality = PROOF_QUALITY;
392 strncpy(lf->lfFaceName, face, sizeof(lf->lfFaceName)-1);
393 /* NUL terminated by the memset at the top */
397 =item render_text(face, size, text, length, aa, pbm, psz, tm)
399 renders the text to an in-memory RGB bitmap
401 It would be nice to render to greyscale, but Windows doesn't have
402 native greyscale bitmaps.
406 static LPVOID render_text(const char *face, int size, const char *text, int length, int aa,
407 HBITMAP *pbm, SIZE *psz, TEXTMETRIC *tm, i_img_dim *bbox, int utf8) {
409 BITMAPINFOHEADER *bmih = &bmi.bmiHeader;
420 set_logfont(face, size, &lf);
422 #ifdef ANTIALIASED_QUALITY
423 /* See KB article Q197076
424 "INFO: Controlling Anti-aliased Text via the LOGFONT Structure"
426 lf.lfQuality = aa ? ANTIALIASED_QUALITY : NONANTIALIASED_QUALITY;
430 wide_text = utf8_to_wide_string(text, length, &wide_count);
436 bmpDc = CreateCompatibleDC(dc);
438 font = CreateFontIndirect(&lf);
440 oldFont = SelectObject(bmpDc, font);
442 GetTextMetrics(bmpDc, tm);
443 sz.cx = bbox[BBOX_ADVANCE_WIDTH] - bbox[BBOX_NEG_WIDTH] + bbox[BBOX_POS_WIDTH];
444 sz.cy = bbox[BBOX_GLOBAL_ASCENT] - bbox[BBOX_GLOBAL_DESCENT];
446 memset(&bmi, 0, sizeof(bmi));
447 bmih->biSize = sizeof(*bmih);
448 bmih->biWidth = sz.cx;
449 bmih->biHeight = sz.cy;
451 bmih->biBitCount = 24;
452 bmih->biCompression = BI_RGB;
453 bmih->biSizeImage = 0;
454 bmih->biXPelsPerMeter = (LONG)(72 / 2.54 * 100);
455 bmih->biYPelsPerMeter = bmih->biXPelsPerMeter;
457 bmih->biClrImportant = 0;
459 bm = CreateDIBSection(dc, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
462 oldBm = SelectObject(bmpDc, bm);
463 SetTextColor(bmpDc, RGB(255, 255, 255));
464 SetBkColor(bmpDc, RGB(0, 0, 0));
466 TextOutW(bmpDc, -bbox[BBOX_NEG_WIDTH], 0, wide_text, wide_count);
469 TextOut(bmpDc, -bbox[BBOX_NEG_WIDTH], 0, text, length);
471 SelectObject(bmpDc, oldBm);
474 i_push_errorf(0, "Could not create DIB section for render: %ld",
476 SelectObject(bmpDc, oldFont);
484 SelectObject(bmpDc, oldFont);
490 i_push_errorf(0, "Could not create logical font: %ld",
501 i_push_errorf(0, "Could not create rendering DC: %ld", GetLastError());
518 =item utf8_to_wide_string(text, text_len, wide_chars)
525 utf8_to_wide_string(char const *text, int text_len, int *wide_chars) {
526 int wide_count = MultiByteToWideChar(CP_UTF8, 0, text, text_len, NULL, 0);
529 if (wide_count < 0) {
530 i_push_errorf(0, "Could not convert utf8: %ld", GetLastError());
534 result = mymalloc(sizeof(WCHAR) * wide_count);
535 if (MultiByteToWideChar(CP_UTF8, 0, text, text_len, result, wide_count) < 0) {
536 i_push_errorf(0, "Could not convert utf8: %ld", GetLastError());
540 result[wide_count-1] = (WCHAR)'\0';
541 *wide_chars = wide_count - 1;
552 Should really use a structure so we can set more attributes.
558 Tony Cook <tony@develop-help.com>