Debian Codegraph @

examples/pangowin32tobmp.c

raw
1 /* Pango
2 * Copyright (C) 1999 Red Hat Software
3 *
4 * testfonts.c:
5 * Copyright (C) 2001 Hans Breuer
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include <stdlib.h>
27 #include "pango.h"
28 #include "pango-impl-utils.h"
29 #include "pangowin32.h"
30
31
32 #include <errno.h>
33 #include <string.h>
34 #ifndef G_OS_WIN32
35 #include <unistd.h>
36 #endif
37 #include <stdio.h>
38 #include <locale.h>
39
40 #include <windows.h>
41
42 static HDC pre_render (int width, int height);
43 static void post_render (HDC hdc, const char* sFile);
44
45 static float
46 calc_duration (GTimeVal *tv1, GTimeVal *tv0)
47 {
48 return ( ((float)tv1->tv_sec - tv0->tv_sec)
49 + (tv1->tv_usec - tv0->tv_usec) / 1000000.0);
50 }
51
52 static int
53 compare_font_family (gconstpointer a,
54 gconstpointer b)
55 {
56 PangoFontFamily **aa = (PangoFontFamily **)a;
57 PangoFontFamily **bb = (PangoFontFamily **)b;
58 return strcmp (pango_font_family_get_name (*aa),
59 pango_font_family_get_name (*bb));
60 }
61
62 int main (int argc, char **argv)
63 {
64 PangoFontMap *fontmap = pango_win32_font_map_for_display();
65 PangoContext *context;
66 PangoCoverage * coverage = NULL;
67 PangoFont* font = NULL;
68 PangoFontFamily** families = NULL;
69 PangoFontFace** faces = NULL;
70 int nb, i;
71 gchar* family_name = NULL;
72 PangoLanguage *lang = pango_language_from_string (g_win32_getlocale ());
73 HDC hdc = NULL;
74 int line = 0;
75 GTimeVal tv0, tv1;
76 int my_font_size = 12;
77
78 printf ("# Pango Font Test\n"
79 "# Language: %s\n"
80 "#\n", pango_language_to_string (lang));
81
82 /* this wasn't necessary with previous version
83 *
84 * force initialization of built-in engines, otherwise
85 * the rendering get's really fast - too fast to work :-(
86 */
87 context = pango_font_map_create_context (pango_win32_font_map_for_display ());
88
89 if (argc == 1) /* No arguments given */
90 {
91 char *std_fonts[] = {"Sans 12", "Serif 12", "Monospace 12"};
92
93 /* try to load some fonts often hardcoded */
94 for (i = 0; i < G_N_ELEMENTS (std_fonts); i++)
95 {
96 PangoFontDescription *desc = pango_font_description_from_string(std_fonts[i]);
97
98 /* spits warnings if font cannot be loaded */
99 font = pango_font_map_load_font (fontmap, context, desc);
100
101 g_object_unref (font);
102 }
103 }
104 else
105 {
106 PangoFontDescription *desc = NULL;
107 GString *s;
108
109 s = g_string_new (argv[1]);
110 for (i = 2; i < argc; i++)
111 {
112 s = g_string_append_c (s, ' ');
113 s = g_string_append (s, argv[i]);
114
115 if (0 != atoi (argv[i]))
116 my_font_size = atoi (argv[i]);
117 }
118
119 desc = pango_font_description_from_string(s->str);
120 family_name = g_strdup (pango_font_description_get_family (desc));
121
122 font = pango_font_map_load_font (fontmap, context, desc);
123
124 coverage = pango_font_get_coverage (font, lang);
125
126 /* ... */
127
128 g_object_unref (coverage);
129 pango_font_description_free (desc);
130 g_object_unref (font);
131 }
132
133 pango_font_map_list_families (fontmap, &families, &nb);
134
135 if (!family_name)
136 {
137 qsort (families, nb, sizeof (PangoFontFamily*), compare_font_family);
138 }
139 else
140 {
141 /* Get on the family faces. No simple way ? */
142 for (i = 0; i < nb; i++)
143 {
144 if (0 == g_ascii_strcasecmp (pango_font_family_get_name (families[i]), family_name))
145 {
146 pango_font_family_list_faces (families[i], &faces, &nb);
147 /* now nb is the number of faces */
148 break;
149 }
150 }
151 g_free (families);
152 families = NULL;
153 g_free (family_name);
154 family_name = NULL;
155 }
156
157 hdc = pre_render(my_font_size * 64, 3 * my_font_size * nb / 2);
158
159 for (i = 0; i < nb; i++)
160 {
161 PangoFontDescription *desc;
162 const char *f_name;
163 PangoWeight weight;
164 PangoStyle style;
165
166 if (families)
167 {
168 desc = pango_font_description_new ();
169
170 f_name = pango_font_family_get_name (families[i]);
171 pango_font_description_set_family (desc, f_name);
172 }
173 else
174 {
175 desc = pango_font_face_describe (faces[i]);
176 /* this is _not_ the family name from above */
177 f_name = pango_font_description_get_family (desc);
178 }
179 weight = pango_font_description_get_weight (desc);
180 style = pango_font_description_get_style (desc);
181
182 g_print ("%s; Style: %d; Weight: %d\n",
183 f_name, style, weight);
184
185 /* give it an arbitray size to load it */
186 pango_font_description_set_size (desc, my_font_size * PANGO_SCALE);
187
188 g_get_current_time (&tv0);
189 font = pango_font_map_load_font (fontmap, context, desc);
190 g_get_current_time (&tv1);
191 g_print ("\tpango_font_map_load_font took %.3f sec\n", calc_duration (&tv1, &tv0));
192
193 if (font)
194 {
195 PangoItem *item;
196 PangoGlyphString * glyphs;
197 char s[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
198 "abcdefghijklmnopqrstuvwxyz"
199 "1234567890 -+*/!\xc2\xa7$%&()[]{}<>|#=?@";
200
201 g_get_current_time (&tv0);
202 coverage = pango_font_get_coverage (font, lang);
203 g_get_current_time (&tv1);
204 g_print ("\tpango_font_get_coverage took %.3f sec\n", calc_duration (&tv1, &tv0));
205
206 /* ... */
207 pango_context_set_language (context, lang);
208 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
209 pango_context_set_font_description (context, desc);
210
211 glyphs = pango_glyph_string_new ();
212 item = pango_item_new ();
213
214 item->analysis.font = g_object_ref (font);
215 pango_shape ( s, sizeof(s), &(item->analysis), glyphs);
216
217 if (hdc)
218 {
219 /* the positioning isn't correct */
220 char* name = g_strdup_printf ("%s (%s%s)",
221 f_name,
222 weight == PANGO_WEIGHT_NORMAL ? "n" :
223 (weight == PANGO_WEIGHT_HEAVY ? "h" :
224 (weight > PANGO_WEIGHT_NORMAL ? "b" : "l")),
225 style == PANGO_STYLE_OBLIQUE ? "o" :
226 (style == PANGO_STYLE_ITALIC ? "i" : "n"));
227
228 TextOut (hdc, 0, line, name, strlen(name));
229 g_get_current_time (&tv0);
230 pango_win32_render (hdc, font, glyphs, 200, line);
231 g_get_current_time (&tv1);
232 g_print ("\tpango_win32_render took %.3f sec\n",
233 calc_duration (&tv1, &tv0));
234 line += (3 * my_font_size / 2);
235 g_free(name);
236 }
237
238 /* free glyphs, ... */
239 pango_glyph_string_free (glyphs);
240 pango_item_free (item);
241
242 g_object_unref (coverage);
243 g_object_unref (font);
244 }
245 pango_font_description_free (desc);
246 }
247
248 if (hdc)
249 post_render (hdc, "pango-fonts.bmp");
250
251 g_free (families);
252 g_free (faces);
253
254 return 0;
255 }
256
257 /*
258 * Real Win32 specific render support
259 */
260 static HBITMAP hbmpold = NULL;
261 static HWND hwndRender = NULL;
262
263 static BOOL
264 SaveBitmap (HBITMAP hBmp, const char* pszFile);
265
266 static HDC
267 pre_render (int width, int height)
268 {
269 HDC hmemdc;
270 HDC hdc;
271 HBITMAP hbmp;
272 RECT r;
273 r.top = 0; r.left = 0;
274 r.right = width;
275 r.bottom = height;
276
277 hwndRender = CreateWindow ("EDIT",
278 "pango-render-window",
279 WS_DISABLED,
280 0, 0, width, height,
281 GetDesktopWindow(),
282 NULL,
283 GetModuleHandle(NULL),
284 NULL);
285
286 if (hwndRender == NULL)
287 fprintf (stderr, "Couldn't create window\n"), exit (1);
288
289 hdc = GetDC(hwndRender);
290 hmemdc = CreateCompatibleDC (hdc);
291 if (hdc == NULL)
292 fprintf (stderr, "CreateCompatibleDC failed\n"), exit (1);
293
294 hbmp = CreateCompatibleBitmap (hdc, width, height);
295 if (hbmp == NULL)
296 fprintf (stderr, "CreateCompatibleBitmap failed\n"), exit (1);
297
298 hbmpold = SelectObject(hmemdc, hbmp);
299
300 FillRect (hmemdc, &r, GetStockObject(WHITE_BRUSH));
301 SetTextColor (hmemdc, RGB (0,0,0));
302 SetBkMode (hmemdc, TRANSPARENT);
303 return hmemdc;
304 }
305
306 static void
307 post_render (HDC hdc, const char* sFile)
308 {
309 HBITMAP hbmp = SelectObject(hdc, hbmpold);
310 if (sFile)
311 SaveBitmap (hbmp, sFile);
312 DeleteObject (hbmp);
313 DeleteDC (hdc);
314 ReleaseDC(hwndRender, GetDC(hwndRender));
315 DestroyWindow (hwndRender);
316 }
317
318 static BOOL
319 SaveBitmap (HBITMAP hBmp, const char* pszFile)
320 {
321 BITMAP bmp;
322 PBITMAPINFO pbmi;
323 WORD cClrBits;
324 /* Retrieve the bitmap's color format, width, and height. */
325 if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
326 return FALSE;
327 /* Convert the color format to a count of bits. */
328 cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
329 if (cClrBits == 1)
330 cClrBits = 1;
331 else if (cClrBits <= 4)
332 cClrBits = 4;
333 else if (cClrBits <= 8)
334 cClrBits = 8;
335 else if (cClrBits <= 16)
336 cClrBits = 16;
337 else if (cClrBits <= 24)
338 cClrBits = 24;
339 else
340 cClrBits = 32;
341
342 /*
343 * Allocate memory for the BITMAPINFO structure. (This structure
344 * contains a BITMAPINFOHEADER structure and an array of RGBQUAD data
345 * structures.) */
346 if (cClrBits < 24)
347 pbmi = (PBITMAPINFO) GlobalAlloc(LPTR,
348 sizeof(BITMAPINFOHEADER) +
349 sizeof(RGBQUAD) * (1 << cClrBits));
350 /*
351 * There is no RGBQUAD array for the 24-bit-per-pixel format. */
352 else
353 pbmi = (PBITMAPINFO) GlobalAlloc(LPTR,
354 sizeof(BITMAPINFOHEADER));
355 /* Initialize the fields in the BITMAPINFO structure. */
356 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
357 pbmi->bmiHeader.biWidth = bmp.bmWidth;
358 pbmi->bmiHeader.biHeight = bmp.bmHeight;
359 pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
360 pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
361 if (cClrBits < 24)
362 pbmi->bmiHeader.biClrUsed = (1 << cClrBits);
363 else
364 pbmi->bmiHeader.biClrUsed = 0;
365 /* If the bitmap is not compressed, set the BI_RGB flag. */
366 pbmi->bmiHeader.biCompression = BI_RGB;
367 /*
368 * Compute the number of bytes in the array of color
369 * indices and store the result in biSizeImage.
370 */
371 pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8
372 * pbmi->bmiHeader.biHeight
373 * cClrBits;
374 /*
375 * Set biClrImportant to 0, indicating that all of the
376 * device colors are important.
377 */
378 pbmi->bmiHeader.biClrImportant = 0;
379
380 { /* C sucks */
381 HANDLE hf; /* file handle */
382 BITMAPFILEHEADER hdr; /* bitmap file-header */
383 PBITMAPINFOHEADER pbih; /* bitmap info-header */
384 LPBYTE lpBits; /* memory pointer */
385 DWORD dwTotal; /* total count of bytes */
386 DWORD cb; /* incremental count of bytes */
387 DWORD dwTmp;
388 HDC hDC;
389
390 pbih = (PBITMAPINFOHEADER) pbmi;
391 lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
392 if (!lpBits)
393 return FALSE;
394 /*
395 * Retrieve the color table (RGBQUAD array) and the bits
396 * (array of palette indices) from the DIB.
397 */
398 hDC = CreateCompatibleDC(NULL);
399 if (!GetDIBits(hDC, hBmp, 0, (WORD) pbih->biHeight,
400 lpBits, pbmi, DIB_RGB_COLORS))
401 return FALSE;
402 /* Create the .BMP file. */
403 hf = CreateFile (pszFile,
404 GENERIC_READ | GENERIC_WRITE,
405 (DWORD) 0,
406 (LPSECURITY_ATTRIBUTES) NULL,
407 CREATE_ALWAYS,
408 FILE_ATTRIBUTE_NORMAL,
409 (HANDLE) NULL);
410
411 if (hf == INVALID_HANDLE_VALUE)
412 return FALSE;
413 hdr.bfType = 0x4d42; /* 0x42 = "B" 0x4d = "M" */
414 /* Compute the size of the entire file. */
415 hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER)
416 + pbih->biSize + pbih->biClrUsed
417 * sizeof(RGBQUAD) + pbih->biSizeImage);
418 hdr.bfReserved1 = 0;
419 hdr.bfReserved2 = 0;
420 /* Compute the offset to the array of color indices. */
421 hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER)
422 + pbih->biSize + pbih->biClrUsed
423 * sizeof (RGBQUAD);
424 /* Copy the BITMAPFILEHEADER into the .BMP file. */
425 if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),
426 (LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
427 return FALSE;
428 /* Copy the BITMAPINFOHEADER and RGBQUAD array into the file. */
429 if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
430 + pbih->biClrUsed * sizeof (RGBQUAD),
431 (LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
432 return FALSE;
433 /* Copy the array of color indices into the .BMP file. */
434 dwTotal = cb = pbih->biSizeImage;
435
436 if (!WriteFile(hf, (LPSTR) lpBits, (int) cb,
437 (LPDWORD) &dwTotal, (LPOVERLAPPED) NULL))
438 return FALSE;
439
440 /* Close the .BMP file. */
441 if (!CloseHandle(hf))
442 return FALSE;
443
444 /* Free memory. */
445 GlobalFree((HGLOBAL)lpBits);
446 GlobalFree((HGLOBAL)pbmi);
447
448 DeleteDC(hDC);
449 } /* C sucks */
450 return TRUE;
451 }