OSDN Git Service

no bone
[nethackexpress/trunk.git] / sys / wince / mhfont.c
1 /* Copyright (C) 2001 by Alex Kompel <shurikk@pacbell.net> */
2 /* NetHack may be freely redistributed.  See license for details. */
3
4 /* font management and such */
5
6 #include "mhfont.h"
7
8 #define MAXFONTS        64
9
10 /* font table - 64 fonts ought to be enough */
11 static struct font_table_entry {
12         int             code;
13         HFONT   hFont;
14 } font_table[MAXFONTS] ;
15 static int font_table_size = 0;
16 HFONT version_splash_font;
17 HFONT extrainfo_splash_font;
18
19 #define NHFONT_CODE(win, attr) (((attr&0xFF)<<8)|(win_type&0xFF))
20
21 static void __cdecl font_table_cleanup(void);
22
23 /* create font based on window type, charater attributes and
24    window device context */
25 HGDIOBJ mswin_get_font(int win_type, int attr, HDC hdc, BOOL replace)
26 {
27         HFONT fnt = NULL;
28         LOGFONT lgfnt;
29         int font_size;
30         int font_index;
31         static BOOL once = FALSE;
32
33         if( !once ) {
34                 once = TRUE;
35                 atexit(font_table_cleanup);
36         }
37
38         ZeroMemory( &lgfnt, sizeof(lgfnt) );
39
40         /* try find font in the table */
41         for(font_index=0; font_index<font_table_size; font_index++)
42                 if(NHFONT_CODE(win_type, attr)==font_table[font_index].code)
43                         break;
44
45         if( !replace && font_index<font_table_size )
46                 return font_table[font_index].hFont;
47
48         switch(win_type) {
49         case NHW_STATUS:
50                 lgfnt.lfHeight                  =       -iflags.wc_fontsiz_status*GetDeviceCaps(hdc, LOGPIXELSY)/72;     // height of font
51                 lgfnt.lfWidth                   =       0;                                   // average character width
52                 lgfnt.lfEscapement              =       0;                                       // angle of escapement
53                 lgfnt.lfOrientation             =       0;                                       // base-line orientation angle
54                 lgfnt.lfWeight                  =       FW_NORMAL;           // font weight
55                 lgfnt.lfItalic                  =       FALSE;                   // italic attribute option
56                 lgfnt.lfUnderline               =       FALSE;                       // underline attribute option
57                 lgfnt.lfStrikeOut               =       FALSE;                       // strikeout attribute option
58                 lgfnt.lfCharSet                 =       mswin_charset();     // character set identifier
59                 lgfnt.lfOutPrecision    =       OUT_DEFAULT_PRECIS;  // output precision
60                 lgfnt.lfClipPrecision   =       CLIP_DEFAULT_PRECIS; // clipping precision
61                 lgfnt.lfQuality                 =       DEFAULT_QUALITY;     // output quality
62                 if( iflags.wc_font_status &&
63                         *iflags.wc_font_status ) {
64                         lgfnt.lfPitchAndFamily = DEFAULT_PITCH;          // pitch and family
65                         NH_A2W( iflags.wc_font_status, lgfnt.lfFaceName, LF_FACESIZE);
66                 } else {
67                         lgfnt.lfPitchAndFamily = FIXED_PITCH;            // pitch and family
68                 }
69                 break;
70
71         case NHW_MENU:
72                 lgfnt.lfHeight                  =       -iflags.wc_fontsiz_menu*GetDeviceCaps(hdc, LOGPIXELSY)/72;       // height of font
73                 lgfnt.lfWidth                   =       0;                                   // average character width
74                 lgfnt.lfEscapement              =       0;                                       // angle of escapement
75                 lgfnt.lfOrientation             =       0;                                       // base-line orientation angle
76                 lgfnt.lfWeight                  =       (attr==ATR_BOLD || attr==ATR_INVERSE)? FW_BOLD : FW_NORMAL;   // font weight
77                 lgfnt.lfItalic                  =       (attr==ATR_BLINK)? TRUE: FALSE;              // italic attribute option
78                 lgfnt.lfUnderline               =       (attr==ATR_ULINE)? TRUE : FALSE;                 // underline attribute option
79                 lgfnt.lfStrikeOut               =       FALSE;                          // strikeout attribute option
80                 lgfnt.lfCharSet                 =       mswin_charset();     // character set identifier
81                 lgfnt.lfOutPrecision    =       OUT_DEFAULT_PRECIS;  // output precision
82                 lgfnt.lfClipPrecision   =       CLIP_DEFAULT_PRECIS; // clipping precision
83                 lgfnt.lfQuality                 =       DEFAULT_QUALITY;     // output quality
84                 if( iflags.wc_font_menu &&
85                         *iflags.wc_font_menu ) {
86                         lgfnt.lfPitchAndFamily  = DEFAULT_PITCH;                 // pitch and family
87                         NH_A2W( iflags.wc_font_menu, lgfnt.lfFaceName, LF_FACESIZE);
88                 } else {
89                         lgfnt.lfPitchAndFamily = FIXED_PITCH;            // pitch and family
90                 }
91                 break;
92
93         case NHW_MESSAGE:
94                 font_size = (attr==ATR_INVERSE)? iflags.wc_fontsiz_message+1 : iflags.wc_fontsiz_message;
95                 lgfnt.lfHeight                  =       -font_size*GetDeviceCaps(hdc, LOGPIXELSY)/72;    // height of font
96                 lgfnt.lfWidth                   =       0;                                   // average character width
97                 lgfnt.lfEscapement              =       0;                                       // angle of escapement
98                 lgfnt.lfOrientation             =       0;                                       // base-line orientation angle
99                 lgfnt.lfWeight                  =       (attr==ATR_BOLD || attr==ATR_INVERSE)? FW_BOLD : FW_NORMAL;   // font weight
100                 lgfnt.lfItalic                  =       (attr==ATR_BLINK)? TRUE: FALSE;              // italic attribute option
101                 lgfnt.lfUnderline               =       (attr==ATR_ULINE)? TRUE : FALSE;                 // underline attribute option
102                 lgfnt.lfStrikeOut               =       FALSE;                       // strikeout attribute option
103                 lgfnt.lfCharSet                 =       mswin_charset();     // character set identifier
104                 lgfnt.lfOutPrecision    =       OUT_DEFAULT_PRECIS;  // output precision
105                 lgfnt.lfClipPrecision   =       CLIP_DEFAULT_PRECIS; // clipping precision
106                 lgfnt.lfQuality                 =       DEFAULT_QUALITY;     // output quality
107                 if( iflags.wc_font_message &&
108                         *iflags.wc_font_message ) {
109                         lgfnt.lfPitchAndFamily  = DEFAULT_PITCH;                 // pitch and family
110                         NH_A2W( iflags.wc_font_message, lgfnt.lfFaceName, LF_FACESIZE);
111                 } else {
112                         lgfnt.lfPitchAndFamily  = VARIABLE_PITCH;                // pitch and family
113                 }
114                 break;
115
116         case NHW_TEXT:
117                 lgfnt.lfHeight                  =       -iflags.wc_fontsiz_text*GetDeviceCaps(hdc, LOGPIXELSY)/72;       // height of font
118                 lgfnt.lfWidth                   =       0;                                   // average character width
119                 lgfnt.lfEscapement              =       0;                                       // angle of escapement
120                 lgfnt.lfOrientation             =       0;                                       // base-line orientation angle
121                 lgfnt.lfWeight                  =       (attr==ATR_BOLD || attr==ATR_INVERSE)? FW_BOLD : FW_NORMAL;   // font weight
122                 lgfnt.lfItalic                  =       (attr==ATR_BLINK)? TRUE: FALSE;              // italic attribute option
123                 lgfnt.lfUnderline               =       (attr==ATR_ULINE)? TRUE : FALSE;                 // underline attribute option
124                 lgfnt.lfStrikeOut               =       FALSE;                       // strikeout attribute option
125                 lgfnt.lfCharSet                 =       mswin_charset();     // character set identifier
126                 lgfnt.lfOutPrecision    =       OUT_DEFAULT_PRECIS;  // output precision
127                 lgfnt.lfClipPrecision   =       CLIP_DEFAULT_PRECIS; // clipping precision
128                 lgfnt.lfQuality                 =       DEFAULT_QUALITY;     // output quality
129                 if( iflags.wc_font_text &&
130                         *iflags.wc_font_text ) {
131                         lgfnt.lfPitchAndFamily  = DEFAULT_PITCH;                 // pitch and family
132                         NH_A2W( iflags.wc_font_text, lgfnt.lfFaceName, LF_FACESIZE);
133                 } else {
134                         lgfnt.lfPitchAndFamily  = FIXED_PITCH;           // pitch and family
135                 }
136                 break;
137         }
138
139         fnt = CreateFontIndirect(&lgfnt);
140
141         /* add font to the table */
142         if( font_index==font_table_size ) {
143                 if( font_table_size>=MAXFONTS ) panic( "font table overflow!" );
144                 font_table_size++;
145         } else {
146                 DeleteObject(font_table[font_index].hFont);
147         }
148
149         font_table[font_index].code = NHFONT_CODE(win_type, attr);
150         font_table[font_index].hFont = fnt;
151         return fnt;
152 }
153
154 UINT mswin_charset()
155 {
156         CHARSETINFO cis;
157         if( iflags.IBMgraphics )
158                 if( TranslateCharsetInfo((DWORD*)GetOEMCP(), &cis, TCI_SRCCODEPAGE) ) 
159                         return cis.ciCharset;
160                 else
161                         return OEM_CHARSET;
162         else 
163                 if( TranslateCharsetInfo((DWORD*)GetACP(), &cis, TCI_SRCCODEPAGE) ) 
164                         return cis.ciCharset;
165                 else
166                         return ANSI_CHARSET;
167 }
168
169 void __cdecl font_table_cleanup(void)
170 {
171         int i;
172         for(i=0; i<font_table_size; i++) {
173                 DeleteObject(font_table[i].hFont);
174         }
175         font_table_size = 0;
176 }
177