1 #define _WIN32_WINNT 0x500
9 win32.c - implements some win32 specific code, specifically Win32 font support.
14 if (i_wf_bbox(facename, size, text, text_len, bbox)) {
17 i_wf_text(face, im, tx, ty, cl, size, text, len, align, aa);
18 i_wf_cp(face, im, tx, ty, channel, size, text, len, align, aa)
22 An Imager interface to font output using the Win32 GDI.
29 #define fixed(x) ((x).value + ((x).fract) / 65536.0)
31 static void set_logfont(char *face, int size, LOGFONT *lf);
33 static LPVOID render_text(char *face, int size, char *text, int length, int aa,
34 HBITMAP *pbm, SIZE *psz, TEXTMETRIC *tm);
37 =item i_wf_bbox(face, size, text, length, bbox)
39 Calculate a bounding box for the text.
44 int i_wf_bbox(char *face, int size, char *text, int length, int *bbox) {
54 int ascent, descent, max_ascent = -size, min_descent = size;
56 mm_log((1, "i_wf_bbox(face %s, size %d, text %p, length %d, bbox %p)\n", face, size, text, length, bbox));
58 set_logfont(face, size, &lf);
59 font = CreateFontIndirect(&lf);
63 oldFont = (HFONT)SelectObject(dc, font);
67 if (GetTextFace(dc, sizeof(facename), facename)) {
68 mm_log((1, " face: %s\n", facename));
72 for (i = 0; i < length; ++i) {
73 unsigned char c = text[i];
74 unsigned char cp = c > '~' ? '.' : c < ' ' ? '.' : c;
76 memset(&mat, 0, sizeof(mat));
79 if (GetGlyphOutline(dc, c, GGO_METRICS, &gm, 0, NULL, &mat) != GDI_ERROR) {
80 mm_log((2, " glyph '%c' (%02x): bbx (%u,%u) org (%d,%d) inc(%d,%d)\n",
81 cp, c, gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmptGlyphOrigin.x,
82 gm.gmptGlyphOrigin.y, gm.gmCellIncX, gm.gmCellIncY));
84 ascent = gm.gmptGlyphOrigin.y;
85 descent = ascent - gm.gmBlackBoxY;
86 if (ascent > max_ascent) max_ascent = ascent;
87 if (descent < min_descent) min_descent = descent;
90 mm_log((1, " glyph '%c' (%02x): error %d\n", cp, c, GetLastError()));
94 if (!GetTextExtentPoint32(dc, text, length, &sz)
95 || !GetTextMetrics(dc, &tm)) {
96 SelectObject(dc, oldFont);
101 bbox[BBOX_GLOBAL_DESCENT] = tm.tmDescent;
102 bbox[BBOX_DESCENT] = min_descent == size ? tm.tmDescent : min_descent;
103 bbox[BBOX_POS_WIDTH] = sz.cx;
104 bbox[BBOX_ADVANCE_WIDTH] = sz.cx;
105 bbox[BBOX_GLOBAL_ASCENT] = tm.tmAscent;
106 bbox[BBOX_ASCENT] = max_ascent == -size ? tm.tmAscent : max_ascent;
109 && GetCharABCWidths(dc, text[0], text[0], &first)
110 && GetCharABCWidths(dc, text[length-1], text[length-1], &last)) {
111 mm_log((1, "first: %d A: %d B: %d C: %d\n", text[0],
112 first.abcA, first.abcB, first.abcC));
113 mm_log((1, "last: %d A: %d B: %d C: %d\n", text[length-1],
114 last.abcA, last.abcB, last.abcC));
115 bbox[BBOX_NEG_WIDTH] = first.abcA;
116 bbox[BBOX_RIGHT_BEARING] = last.abcC;
118 bbox[BBOX_POS_WIDTH] -= last.abcC;
121 bbox[BBOX_NEG_WIDTH] = 0;
122 bbox[BBOX_RIGHT_BEARING] = 0;
125 SelectObject(dc, oldFont);
129 mm_log((1, " bbox=> negw=%d glob_desc=%d pos_wid=%d glob_asc=%d desc=%d asc=%d adv_width=%d rightb=%d\n", bbox[0], bbox[1], bbox[2], bbox[3], bbox[4], bbox[5], bbox[6], bbox[7]));
131 return BBOX_RIGHT_BEARING + 1;
135 =item i_wf_text(face, im, tx, ty, cl, size, text, len, align, aa)
137 Draws the text in the given color.
143 i_wf_text(char *face, i_img *im, int tx, int ty, i_color *cl, int size,
144 char *text, int len, int align, int aa) {
154 bits = render_text(face, size, text, len, aa, &bm, &sz, &tm);
158 line_width = sz.cx * 3;
159 line_width = (line_width + 3) / 4 * 4;
165 int bbox[BOUNDING_BOX_COUNT];
167 i_wf_bbox(face, size, text, len, bbox);
168 top -= tm.tmAscent - bbox[BBOX_ASCENT];
171 for (y = 0; y < sz.cy; ++y) {
172 for (x = 0; x < sz.cx; ++x) {
174 int scale = bits[3 * x];
175 i_gpix(im, tx+x, top+sz.cy-y-1, &pel);
176 for (ch = 0; ch < im->channels; ++ch) {
178 ((255-scale) * pel.channel[ch] + scale*cl->channel[ch]) / 255;
180 i_ppix(im, tx+x, top+sz.cy-y-1, &pel);
190 =item i_wf_cp(face, im, tx, ty, channel, size, text, len, align, aa)
192 Draws the text in the given channel.
198 i_wf_cp(char *face, i_img *im, int tx, int ty, int channel, int size,
199 char *text, int len, int align, int aa) {
208 bits = render_text(face, size, text, len, aa, &bm, &sz, &tm);
212 line_width = sz.cx * 3;
213 line_width = (line_width + 3) / 4 * 4;
219 int bbox[BOUNDING_BOX_COUNT];
221 i_wf_bbox(face, size, text, len, bbox);
222 top -= tm.tmAscent - bbox[BBOX_ASCENT];
225 for (y = 0; y < sz.cy; ++y) {
226 for (x = 0; x < sz.cx; ++x) {
228 int scale = bits[3 * x];
229 i_gpix(im, tx+x, top+sz.cy-y-1, &pel);
230 pel.channel[channel] = scale;
231 i_ppix(im, tx+x, top+sz.cy-y-1, &pel);
241 =item i_wf_addfont(char const *filename, char const *resource_file)
243 Adds a TTF font file as usable by the application.
245 The font is always added as private to the application.
250 i_wf_addfont(char const *filename) {
253 if (AddFontResourceEx(filename, FR_PRIVATE, 0)) {
257 i_push_errorf(0, "Could not add resource: %ld", GetLastError());
265 =head1 INTERNAL FUNCTIONS
269 =item set_logfont(face, size, lf)
271 Fills in a LOGFONT structure with reasonable defaults.
276 static void set_logfont(char *face, int size, LOGFONT *lf) {
277 memset(lf, 0, sizeof(LOGFONT));
279 lf->lfHeight = -size; /* character height rather than cell height */
280 lf->lfCharSet = DEFAULT_CHARSET;
281 lf->lfOutPrecision = OUT_TT_PRECIS;
282 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
283 lf->lfQuality = PROOF_QUALITY;
284 strncpy(lf->lfFaceName, face, sizeof(lf->lfFaceName)-1);
285 /* NUL terminated by the memset at the top */
289 =item render_text(face, size, text, length, aa, pbm, psz, tm)
291 renders the text to an in-memory RGB bitmap
293 It would be nice to render to greyscale, but Windows doesn't have
294 native greyscale bitmaps.
298 static LPVOID render_text(char *face, int size, char *text, int length, int aa,
299 HBITMAP *pbm, SIZE *psz, TEXTMETRIC *tm) {
301 BITMAPINFOHEADER *bmih = &bmi.bmiHeader;
310 set_logfont(face, size, &lf);
312 #ifdef ANTIALIASED_QUALITY
313 /* See KB article Q197076
314 "INFO: Controlling Anti-aliased Text via the LOGFONT Structure"
316 lf.lfQuality = aa ? ANTIALIASED_QUALITY : NONANTIALIASED_QUALITY;
319 bmpDc = CreateCompatibleDC(dc);
321 font = CreateFontIndirect(&lf);
323 oldFont = SelectObject(bmpDc, font);
324 GetTextExtentPoint32(bmpDc, text, length, &sz);
325 GetTextMetrics(bmpDc, tm);
327 memset(&bmi, 0, sizeof(bmi));
328 bmih->biSize = sizeof(*bmih);
329 bmih->biWidth = sz.cx;
330 bmih->biHeight = sz.cy;
332 bmih->biBitCount = 24;
333 bmih->biCompression = BI_RGB;
334 bmih->biSizeImage = 0;
335 bmih->biXPelsPerMeter = (LONG)(72 / 2.54 * 100);
336 bmih->biYPelsPerMeter = bmih->biXPelsPerMeter;
338 bmih->biClrImportant = 0;
340 bm = CreateDIBSection(dc, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
343 oldBm = SelectObject(bmpDc, bm);
344 SetTextColor(bmpDc, RGB(255, 255, 255));
345 SetBkColor(bmpDc, RGB(0, 0, 0));
346 TextOut(bmpDc, 0, 0, text, length);
347 SelectObject(bmpDc, oldBm);
350 i_push_errorf(0, "Could not create DIB section for render: %ld",
352 SelectObject(bmpDc, oldFont);
358 SelectObject(bmpDc, oldFont);
362 i_push_errorf(0, "Could not create logical font: %ld",
371 i_push_errorf(0, "Could not create rendering DC: %ld", GetLastError());
387 Should really use a structure so we can set more attributes.
393 Tony Cook <tony@develop-help.com>