OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [MW_0_8_9PRE7/] [mw/] [src/] [fonts/] [convfnt.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Copyright (c) 1999 Greg Haerr <greg@censoft.com>
3
 *
4
 * MS Windows Font Grabber for Micro-Windows
5
 *
6
 * Usage: convfnt32 [1|2|3|4|<fontname>]
7
 *
8
 * Note: a Microsoft License is required to use MS Fonts
9
 */
10
#define WIN32_LEAN_AND_MEAN
11
#include <windows.h>
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <util.h>
15
 
16
#define MAX_CHAR_HEIGHT 16      /* max character height*/
17
typedef unsigned short  IMAGEBITS;      /* bitmap image unit size*/
18
 
19
/* IMAGEBITS macros*/
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*/
27
 
28
/* global data*/
29
HINSTANCE       ghInstance;
30
char            APPWINCLASS[] = "convfnt";
31
int             CHAR_WIDTH;
32
int             CHAR_HEIGHT;
33
int             CHAR_ASCENT;
34
char            fontname[64];
35
FILE *          fp;
36
HFONT           hfont;
37
int             FIRST_CHAR = ' ';
38
int             LAST_CHAR = 256;
39
int             curoff = 0;
40
int             offsets[256];
41
int             widths[256];
42
 
43
 
44
/* forward decls*/
45
LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wp,LPARAM lp);
46
 
47
HWND InitApp(void);
48
int  InitClasses(void);
49
void doit(HDC hdc);
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,
55
                int charset);
56
 
57
int WINAPI
58
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
59
        int nShowCmd)
60
{
61
        MSG             msg;
62
        HDC             hdc;
63
        int             i;
64
        char *  q;
65
        char    arg[80];
66
 
67
        ghInstance = hInstance;
68
        InitClasses();
69
        InitApp();
70
 
71
        i = atoi(lpCmdLine);
72
        hdc = GetDC(NULL);
73
        switch(i) {
74
        case 0:
75
                if(*lpCmdLine == 0)
76
                        lpCmdLine = "MS Sans Serif";
77
                q = arg;
78
                for(q=arg; *lpCmdLine; ++lpCmdLine) {
79
                        if(*lpCmdLine == '"' || *lpCmdLine == '\'')
80
                                continue;
81
                        *q++ = *lpCmdLine;
82
                }
83
                *q = 0;
84
                hfont = GetFont(hdc, arg, 8, 0);
85
                break;
86
        case 1:
87
                hfont = GetStockObject(DEFAULT_GUI_FONT);       /* winMSSansSerif11x13 */
88
                break;
89
        case 2:
90
                hfont = GetStockObject(SYSTEM_FONT);            /* winSystem14x16 */
91
                break;
92
        case 3:
93
                hfont = GetStockObject(OEM_FIXED_FONT);         /* winTerminal8x12 */
94
                break;
95
        case 4:
96
                hfont = GetStockObject(ANSI_VAR_FONT);          /* winMSSansSerif11x13 */
97
                break;
98
        }
99
        ReleaseDC(NULL, hdc);
100
 
101
        while(GetMessage(&msg, NULL, 0, 0)) {
102
                TranslateMessage(&msg);
103
                DispatchMessage(&msg);
104
        }
105
        return 0;
106
}
107
 
108
int
109
InitClasses(void)
110
{
111
        WNDCLASS        wc;
112
 
113
        wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
114
        wc.lpfnWndProc = (WNDPROC)WndProc;
115
        wc.cbClsExtra = 0;
116
        wc.cbWndExtra = 0;
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);
124
}
125
 
126
HWND
127
InitApp(void)
128
{
129
        HWND    hwnd;
130
 
131
        hwnd = CreateWindowEx( 0L, APPWINCLASS,
132
                "Font Grabber",
133
                WS_OVERLAPPEDWINDOW,
134
                CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
135
                NULL,
136
                NULL,
137
                ghInstance,
138
                NULL);
139
 
140
        if( hwnd == NULL)
141
                return( 0);
142
 
143
        ShowWindow( hwnd, SW_SHOW);
144
        return hwnd;
145
}
146
 
147
 
148
LRESULT CALLBACK
149
WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
150
{
151
        PAINTSTRUCT             ps;
152
        HDC                             hdc;
153
        LOGFONT                 lf;
154
        TEXTMETRIC              tm;
155
        char                    outfile[64];
156
        char                    *p, *q;
157
 
158
        switch( msg) {
159
        case WM_CREATE:
160
                break;
161
 
162
        case WM_DESTROY:
163
                PostQuitMessage(0);
164
                break;
165
 
166
        case WM_PAINT:
167
                hdc = BeginPaint(hwnd, &ps);
168
                SelectObject(hdc, hfont);
169
 
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);
178
                q = p = fontname;
179
                while(*p) {
180
                        if(*p != ' ')
181
                                *q++ = *p;
182
                        ++p;
183
                }
184
                *q = 0;
185
 
186
                wsprintf(outfile, "win%s%dx%d.c", fontname, CHAR_WIDTH, CHAR_HEIGHT);
187
                fp = fopen(outfile, "wt");
188
                doit(hdc);
189
                fclose(fp);
190
                exit(1);
191
                EndPaint(hwnd, &ps);
192
                break;
193
 
194
        case WM_LBUTTONDOWN:
195
                break;
196
 
197
        default:
198
                return DefWindowProc( hwnd, msg, wp, lp);
199
        }
200
        return( 0);
201
}
202
 
203
 
204
void
205
convfnt(HDC hdc)
206
{
207
        SIZE    size;
208
        unsigned char   ch;
209
        int             i;
210
        int             x, y;
211
        USHORT  c;
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
216
        };
217
 
218
        for(i=FIRST_CHAR; i<LAST_CHAR; ++i) {
219
                ch = i;
220
                TextOut(hdc, 0, 0, &ch, 1);
221
                GetTextExtentPoint32(hdc, &ch, 1, &size);
222
                for(y=0; y<size.cy; ++y) {
223
                        image[y] = 0;
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)));
227
                        }
228
                }
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);
233
                curoff += size.cy;
234
                fprintf(fp, "\n");
235
        }
236
}
237
 
238
 
239
void
240
doit(HDC hdc)
241
{
242
        int             i;
243
 
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);
250
 
251
        convfnt(hdc);
252
 
253
        fprintf(fp, "};\n\n");
254
 
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");
261
 
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");
268
 
269
 
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);
281
        fprintf(fp, "};\n");
282
}
283
 
284
/* Character ! (0x21):
285
   ht=16, width=8
286
   +----------------+
287
   |                |
288
   |                |
289
   | *              |
290
   | *              |
291
   | *              |
292
   | *              |
293
   | *              |
294
   | *              |
295
   |                |
296
   | *              |
297
   |                |
298
   |                |
299
   +----------------+ */
300
 
301
void
302
print_char(int ch,IMAGEBITS *bits, int width, int height)
303
{
304
        int             x;
305
        int             bitcount;       /* number of bits left in bitmap word */
306
        IMAGEBITS       bitvalue;       /* bitmap word value */
307
 
308
        fprintf(fp, "/* Character %c (0x%02x):\n", (ch < ' '? ' ': ch), ch);
309
        fprintf(fp, "   ht=%d, width=%d\n", height, width);
310
        fprintf(fp, "   +");
311
        for(x=0; x<width; ++x)
312
                fprintf(fp, "-");
313
        fprintf(fp, "+\n");
314
        x = 0;
315
        bitcount = 0;
316
        while (height > 0) {
317
            if (bitcount <= 0) {
318
                    fprintf(fp, "   |");
319
                    bitcount = IMAGE_BITSPERIMAGE;
320
                    bitvalue = *bits++;
321
            }
322
                if (IMAGE_TESTBIT(bitvalue))
323
                            fprintf(fp, "*");
324
                else fprintf(fp, " ");
325
            bitvalue = IMAGE_SHIFTBIT(bitvalue);
326
            --bitcount;
327
            if (x++ == width-1) {
328
                    x = 0;
329
                    --height;
330
                    bitcount = 0;
331
                    fprintf(fp, "|\n");
332
            }
333
        }
334
        fprintf(fp, "   +");
335
        for(x=0; x<width; ++x)
336
                fprintf(fp, "-");
337
        fprintf(fp, "+ */\n");
338
}
339
 
340
#define IMAGE_GETBIT4(m)        (((m) & 0xf000) >> 12)
341
#define IMAGE_SHIFTBIT4(m)      ((IMAGEBITS) ((m) << 4))
342
 
343
void
344
print_bits(IMAGEBITS *bits, int width, int height)
345
{
346
        int             x;
347
        int             bitcount;       /* number of bits left in bitmap word */
348
        IMAGEBITS       bitvalue;       /* bitmap word value */
349
 
350
        x = 0;
351
        bitcount = 0;
352
        while (height > 0) {
353
            if (bitcount <= 0) {
354
                    fprintf(fp, "0x");
355
                    bitcount = IMAGE_BITSPERIMAGE;
356
                    bitvalue = *bits++;
357
            }
358
                fprintf(fp, "%x", IMAGE_GETBIT4(bitvalue));
359
            bitvalue = IMAGE_SHIFTBIT4(bitvalue);
360
            bitcount -= 4;
361
                x += 4;
362
            if (x >= width) {
363
                        if(IMAGE_BITSPERIMAGE > width)
364
                                for(x=IMAGE_BITSPERIMAGE-width; x>3; ) {
365
                                        fprintf(fp, "0");
366
                                        x -= 4;
367
                                }
368
                    x = 0;
369
                    --height;
370
                    bitcount = 0;
371
                    fprintf(fp, ",\n");
372
            }
373
        }
374
}
375
 
376
/*
377
 * WIN Draw Library
378
 *
379
 * GetFont style bits:
380
 *                      01 bold
381
 *                      02 italic
382
 * fontSize > 0         points (must pass hDC for non-screen font)
383
 * fontSize < 0         pixels (no HDC needed)
384
 */
385
 
386
HFONT WINAPI
387
GetFont(HDC hDC, LPSTR fontName,int fontSize,int fontStyle)
388
{
389
        return GetFontEx(hDC, fontName, fontSize, fontStyle, ANSI_CHARSET);
390
}
391
 
392
HFONT WINAPI
393
GetFontEx(HDC hDC, LPSTR fontName,int fontSize,int fontStyle,int charset)
394
{
395
        LOGFONT lf;
396
        HDC             hdc;
397
 
398
        memset( &lf, 0, sizeof(LOGFONT));
399
 
400
        if( fontSize < 0 || hDC)
401
                hdc = hDC;
402
        else hdc = GetDC( GetDesktopWindow());
403
 
404
        /* calculate font size from passed point size*/
405
        if( fontSize < 0)
406
                lf.lfHeight = -fontSize;
407
        else lf.lfHeight = -MulDiv( fontSize,
408
                                GetDeviceCaps( hdc, LOGPIXELSY), 72);
409
        if( fontName)
410
                strncpy( lf.lfFaceName, fontName, LF_FACESIZE);
411
        else lf.lfFaceName[ 0] = '\0';
412
        lf.lfWeight = (fontStyle & 01)? FW_BOLD: FW_NORMAL;
413
        if( fontStyle & 02)
414
                lf.lfItalic = 1;
415
        lf.lfCharSet = charset;
416
 
417
        if( fontSize > 0 && !hDC)
418
                ReleaseDC( GetDesktopWindow(), hdc);
419
        return CreateFontIndirect( &lf);
420
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.