2 * Copyright (c) 1999 Greg Haerr <greg@censoft.com>
4 * MS Windows Font Grabber for Micro-Windows
6 * Usage: convfnt32 [1|2|3|4|<fontname>]
8 * Note: a Microsoft License is required to use MS Fonts
10 #define WIN32_LEAN_AND_MEAN
16 #define MAX_CHAR_HEIGHT 16 /* max character height*/
17 typedef unsigned short IMAGEBITS; /* bitmap image unit size*/
20 #define IMAGE_SIZE(width, height) ((height) * (((width) + sizeof(IMAGEBITS) * 8 - 1) / (sizeof(IMAGEBITS) * 8)))
21 #define IMAGE_WORDS(x) (((x)+15)/16)
22 #define IMAGE_BITSPERIMAGE (sizeof(IMAGEBITS) * 8)
23 #define IMAGE_FIRSTBIT ((IMAGEBITS) 0x8000)
24 #define IMAGE_NEXTBIT(m) ((IMAGEBITS) ((m) >> 1))
25 #define IMAGE_TESTBIT(m) ((m) & IMAGE_FIRSTBIT) /* use with shiftbit*/
26 #define IMAGE_SHIFTBIT(m) ((IMAGEBITS) ((m) << 1)) /* for testbit*/
30 char APPWINCLASS[] = "convfnt";
45 LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wp,LPARAM lp);
48 int InitClasses(void);
50 void convfnt(HDC hdc);
51 void print_char(int ch,IMAGEBITS *b, int w, int h);
52 void print_bits(IMAGEBITS *bits, int width, int height);
53 HFONT WINAPI GetFont(HDC hDC, LPSTR fontName,int fontSize,int fontStyle);
54 HFONT WINAPI GetFontEx(HDC hDC, LPSTR fontName,int fontSize,int fontStyle,
58 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
67 ghInstance = hInstance;
76 lpCmdLine = "MS Sans Serif";
78 for(q=arg; *lpCmdLine; ++lpCmdLine) {
79 if(*lpCmdLine == '"' || *lpCmdLine == '\'')
84 hfont = GetFont(hdc, arg, 8, 0);
87 hfont = GetStockObject(DEFAULT_GUI_FONT); /* winMSSansSerif11x13 */
90 hfont = GetStockObject(SYSTEM_FONT); /* winSystem14x16 */
93 hfont = GetStockObject(OEM_FIXED_FONT); /* winTerminal8x12 */
96 hfont = GetStockObject(ANSI_VAR_FONT); /* winMSSansSerif11x13 */
101 while(GetMessage(&msg, NULL, 0, 0)) {
102 TranslateMessage(&msg);
103 DispatchMessage(&msg);
113 wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
114 wc.lpfnWndProc = (WNDPROC)WndProc;
117 wc.hInstance = ghInstance;
118 wc.hIcon = LoadIcon(ghInstance, MAKEINTRESOURCE( 1));
119 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
120 wc.hbrBackground = GetStockObject(LTGRAY_BRUSH);
121 wc.lpszMenuName = NULL;
122 wc.lpszClassName = APPWINCLASS;
123 return RegisterClass( &wc);
131 hwnd = CreateWindowEx( 0L, APPWINCLASS,
134 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
143 ShowWindow( hwnd, SW_SHOW);
149 WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
167 hdc = BeginPaint(hwnd, &ps);
168 SelectObject(hdc, hfont);
170 GetObject(hfont, sizeof(lf), &lf);
171 GetTextMetrics(hdc, &tm);
172 CHAR_WIDTH = tm.tmMaxCharWidth;
173 CHAR_HEIGHT = tm.tmHeight;
174 CHAR_ASCENT = tm.tmAscent;
175 FIRST_CHAR = tm.tmFirstChar;
176 LAST_CHAR = tm.tmLastChar + 1;
177 strcpy(fontname, lf.lfFaceName);
186 wsprintf(outfile, "win%s%dx%d.c", fontname, CHAR_WIDTH, CHAR_HEIGHT);
187 fp = fopen(outfile, "wt");
198 return DefWindowProc( hwnd, msg, wp, lp);
212 IMAGEBITS image[MAX_CHAR_HEIGHT];
213 static USHORT mask[] = {
214 0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0200, 0x0100,
215 0x0080, 0x0040, 0x0020, 0x0010, 0x0008, 0x0004, 0x0002, 0x0001
218 for(i=FIRST_CHAR; i<LAST_CHAR; ++i) {
220 TextOut(hdc, 0, 0, &ch, 1);
221 GetTextExtentPoint32(hdc, &ch, 1, &size);
222 for(y=0; y<size.cy; ++y) {
224 for(x=0; x<size.cx; ++x) {
225 c = GetPixel(hdc, x, y)? 0: 1;
226 image[y] = (image[y] & ~mask[x&15]) | (c << (15 - (x & 15)));
229 offsets[ch] = curoff;
230 widths[ch] = size.cx;
231 print_char(ch, image, size.cx, size.cy);
232 print_bits(image, size.cx, size.cy);
244 fprintf(fp, "/* Generated by convfnt.exe*/\n");
245 fprintf(fp, "#include \"device.h\"\n\n");
246 fprintf(fp, "/* Windows %s %dx%d Font */\n\n",
247 fontname, CHAR_WIDTH, CHAR_HEIGHT);
248 fprintf(fp, "static MWIMAGEBITS win%s%dx%d_bits[] = {\n\n",
249 fontname, CHAR_WIDTH, CHAR_HEIGHT);
253 fprintf(fp, "};\n\n");
255 fprintf(fp, "/* Character->glyph data. */\n");
256 fprintf(fp, "static unsigned short win%s%dx%d_offset[] = {\n",
257 fontname, CHAR_WIDTH, CHAR_HEIGHT);
258 for(i=FIRST_CHAR; i<LAST_CHAR; ++i)
259 fprintf(fp, " %d,\t /* %c (0x%02x) */\n", offsets[i], i<' '? ' ':i , i);
260 fprintf(fp, "};\n\n");
262 fprintf(fp, "/* Character width data. */\n");
263 fprintf(fp, "static unsigned char win%s%dx%d_width[] = {\n",
264 fontname, CHAR_WIDTH, CHAR_HEIGHT);
265 for(i=FIRST_CHAR; i<LAST_CHAR; ++i)
266 fprintf(fp, " %d,\t /* %c (0x%02x) */\n", widths[i], i<' '? ' ':i , i);
267 fprintf(fp, "};\n\n");
270 fprintf(fp, "/* Exported structure definition. */\n"
271 "MWCFONT font_win%s%dx%d = {\n",
272 fontname, CHAR_WIDTH, CHAR_HEIGHT);
273 fprintf(fp, "\t\"win%s%dx%d\",\n", fontname, CHAR_WIDTH, CHAR_HEIGHT);
274 fprintf(fp, "\t%d,\n", CHAR_WIDTH);
275 fprintf(fp, "\t%d,\n", CHAR_HEIGHT);
276 fprintf(fp, "\t%d,\n", CHAR_ASCENT);
277 fprintf(fp, "\t%d,\n\t%d,\n", FIRST_CHAR, LAST_CHAR-FIRST_CHAR);
278 fprintf(fp, "\twin%s%dx%d_bits,\n", fontname, CHAR_WIDTH, CHAR_HEIGHT);
279 fprintf(fp, "\twin%s%dx%d_offset,\n", fontname, CHAR_WIDTH, CHAR_HEIGHT);
280 fprintf(fp, "\twin%s%dx%d_width,\n", fontname, CHAR_WIDTH, CHAR_HEIGHT);
284 /* Character ! (0x21):
299 +----------------+ */
302 print_char(int ch,IMAGEBITS *bits, int width, int height)
305 int bitcount; /* number of bits left in bitmap word */
306 IMAGEBITS bitvalue; /* bitmap word value */
308 fprintf(fp, "/* Character %c (0x%02x):\n", (ch < ' '? ' ': ch), ch);
309 fprintf(fp, " ht=%d, width=%d\n", height, width);
311 for(x=0; x<width; ++x)
319 bitcount = IMAGE_BITSPERIMAGE;
322 if (IMAGE_TESTBIT(bitvalue))
324 else fprintf(fp, " ");
325 bitvalue = IMAGE_SHIFTBIT(bitvalue);
327 if (x++ == width-1) {
335 for(x=0; x<width; ++x)
337 fprintf(fp, "+ */\n");
340 #define IMAGE_GETBIT4(m) (((m) & 0xf000) >> 12)
341 #define IMAGE_SHIFTBIT4(m) ((IMAGEBITS) ((m) << 4))
344 print_bits(IMAGEBITS *bits, int width, int height)
347 int bitcount; /* number of bits left in bitmap word */
348 IMAGEBITS bitvalue; /* bitmap word value */
355 bitcount = IMAGE_BITSPERIMAGE;
358 fprintf(fp, "%x", IMAGE_GETBIT4(bitvalue));
359 bitvalue = IMAGE_SHIFTBIT4(bitvalue);
363 if(IMAGE_BITSPERIMAGE > width)
364 for(x=IMAGE_BITSPERIMAGE-width; x>3; ) {
379 * GetFont style bits:
382 * fontSize > 0 points (must pass hDC for non-screen font)
383 * fontSize < 0 pixels (no HDC needed)
387 GetFont(HDC hDC, LPSTR fontName,int fontSize,int fontStyle)
389 return GetFontEx(hDC, fontName, fontSize, fontStyle, ANSI_CHARSET);
393 GetFontEx(HDC hDC, LPSTR fontName,int fontSize,int fontStyle,int charset)
398 memset( &lf, 0, sizeof(LOGFONT));
400 if( fontSize < 0 || hDC)
402 else hdc = GetDC( GetDesktopWindow());
404 /* calculate font size from passed point size*/
406 lf.lfHeight = -fontSize;
407 else lf.lfHeight = -MulDiv( fontSize,
408 GetDeviceCaps( hdc, LOGPIXELSY), 72);
410 strncpy( lf.lfFaceName, fontName, LF_FACESIZE);
411 else lf.lfFaceName[ 0] = '\0';
412 lf.lfWeight = (fontStyle & 01)? FW_BOLD: FW_NORMAL;
415 lf.lfCharSet = charset;
417 if( fontSize > 0 && !hDC)
418 ReleaseDC( GetDesktopWindow(), hdc);
419 return CreateFontIndirect( &lf);