comparison packages/services/gfx/mw/current/src/mwin/winfont.c @ 208:e0c0827131d1 ecos

Merge from eCos master repository on 2002-05-20-20:11:54-BST
author jlarmour
date Mon, 20 May 2002 22:19:26 +0000
parents
children
comparison
equal deleted inserted replaced
207:74c807ddde34 208:e0c0827131d1
1 /*
2 * Copyright (c) 2000 Greg Haerr <greg@censoft.com>
3 * GetTextExtent*Point by Roman Guseynov
4 * Original contributions by Shane Nay
5 *
6 * Win32 API upper level font selection routines
7 */
8 #include "windows.h"
9 #include "wintern.h"
10 #include "device.h"
11 #include <stdlib.h>
12 #include <string.h>
13
14 HFONT WINAPI
15 CreateFont(int nHeight, int nWidth, int nEscapement, int nOrientation,
16 int fnWeight, DWORD fdwItalic, DWORD fdwUnderline, DWORD fdwStrikeOut,
17 DWORD fdwCharSet,DWORD fdwOutputPrecision,DWORD fdwClipPrecision,
18 DWORD fdwQuality, DWORD fdwPitchAndFamily, LPCSTR lpszFace)
19 {
20 LOGFONT lf;
21
22 lf.lfHeight = nHeight;
23 lf.lfWidth = nWidth;
24 lf.lfEscapement = nEscapement;
25 lf.lfOrientation = nOrientation;
26 lf.lfWeight = fnWeight;
27 lf.lfItalic = fdwItalic;
28 lf.lfUnderline = fdwUnderline;
29 lf.lfStrikeOut = fdwStrikeOut;
30 lf.lfCharSet = fdwCharSet;
31 lf.lfOutPrecision = fdwOutputPrecision;
32 lf.lfClipPrecision = fdwClipPrecision;
33 lf.lfQuality = fdwQuality;
34 lf.lfPitchAndFamily = fdwPitchAndFamily;
35 strncpy(lf.lfFaceName, lpszFace, sizeof(lf.lfFaceName));
36
37 return CreateFontIndirect(&lf);
38 }
39
40 HFONT WINAPI
41 CreateFontIndirect(CONST LOGFONT *lplf)
42 {
43 MWFONTOBJ * hfont;
44 int family, pitch;
45 MWLOGFONT mwlf;
46 char szFacename[32];
47
48 /* create a gdi font object*/
49 hfont = GdItemNew(MWFONTOBJ);
50 if(!hfont)
51 return NULL;
52 hfont->hdr.type = OBJ_FONT;
53 hfont->hdr.stockobj = FALSE;
54
55 /* convert LOGFONT to MWLOGFONT*/
56 memset(&mwlf, 0, sizeof(mwlf));
57 mwlf.lfHeight = lplf->lfHeight;
58 mwlf.lfWidth = lplf->lfWidth;
59 mwlf.lfEscapement = lplf->lfEscapement;
60 mwlf.lfOrientation = lplf->lfOrientation;
61 mwlf.lfWeight = lplf->lfWeight;
62 mwlf.lfItalic = lplf->lfItalic;
63 mwlf.lfUnderline = lplf->lfUnderline;
64 mwlf.lfStrikeOut = lplf->lfStrikeOut;
65 mwlf.lfCharSet = lplf->lfCharSet;
66 mwlf.lfOutPrecision = lplf->lfOutPrecision;
67 mwlf.lfClipPrecision = lplf->lfClipPrecision;
68 mwlf.lfQuality = lplf->lfQuality;
69 strncpy(mwlf.lfFaceName, lplf->lfFaceName, sizeof(mwlf.lfFaceName));
70
71 family = lplf->lfPitchAndFamily & 0xf0;
72 switch(family) {
73 case FF_DONTCARE:
74 break;
75 case FF_ROMAN:
76 mwlf.lfRoman = 1;
77 mwlf.lfSerif = 1;
78 break;
79 case FF_SWISS:
80 mwlf.lfSansSerif = 1;
81 break;
82 case FF_MODERN:
83 mwlf.lfModern = 1;
84 break;
85 }
86
87 pitch = lplf->lfPitchAndFamily & 0x0f;
88 switch(pitch) {
89 case DEFAULT_PITCH:
90 break;
91 case FIXED_PITCH:
92 case MONO_FONT:
93 mwlf.lfMonospace = 1;
94 break;
95 case VARIABLE_PITCH:
96 mwlf.lfProportional = 1;
97 break;
98 }
99 /*mwlf.lfOblique = 0;*/
100 /*mwlf.lfSmallCaps = 0;*/
101 /*mwlf.lfPitch = 0;*/
102
103 /* select a font based on facename, bold/italic and height*/
104 strncpy(szFacename, lplf->lfFaceName, sizeof(szFacename));
105 if (lplf->lfWeight==FW_BOLD)
106 strcat(szFacename, "B");
107 if (lplf->lfItalic)
108 strcat(szFacename, "I");
109 hfont->pfont = GdCreateFont(&scrdev, szFacename, lplf->lfHeight, &mwlf);
110
111 return (HFONT)hfont;
112 }
113
114 BOOL WINAPI
115 GetTextMetrics(HDC hdc, LPTEXTMETRIC lptm)
116 {
117 MWFONTINFO fi;
118
119 if(!hdc)
120 return FALSE;
121
122 GdGetFontInfo(hdc->font->pfont, &fi);
123
124 /* FIXME many items are guessed for the time being*/
125 lptm->tmHeight = fi.height;
126
127 /* reversed for kaffe port
128 lptm->tmAscent = fi.height - fi.baseline;
129 lptm->tmDescent= fi.baseline;
130 */
131
132 lptm->tmDescent = fi.height - fi.baseline;
133 lptm->tmAscent= fi.baseline;
134 lptm->tmInternalLeading = 0;
135 lptm->tmExternalLeading = 0;
136 lptm->tmAveCharWidth = fi.widths['x'];
137 lptm->tmMaxCharWidth = fi.maxwidth;
138 lptm->tmWeight = FW_NORMAL;
139 lptm->tmOverhang = 0;
140 lptm->tmDigitizedAspectX = fi.maxwidth;
141 lptm->tmDigitizedAspectY = fi.height;
142 lptm->tmFirstChar = 32;
143 lptm->tmLastChar = 255;
144 lptm->tmDefaultChar = '?';
145 lptm->tmBreakChar = 0;
146 lptm->tmItalic = 0;
147 lptm->tmUnderlined = 0;
148 lptm->tmStruckOut = 0;
149 /* note that win32 has the TMPF_FIXED_PITCH flags REVERSED...*/
150 lptm->tmPitchAndFamily = fi.fixed?
151 FF_DONTCARE: (FF_DONTCARE | TMPF_FIXED_PITCH);
152 lptm->tmCharSet = OEM_CHARSET;
153 return TRUE;
154 }
155
156 BOOL WINAPI
157 GetCharWidth(HDC hdc, UINT iFirstChar, UINT iLastChar, LPINT lpBuffer)
158 {
159 int i;
160 int j = 0;
161 MWFONTINFO fi;
162
163 if(!hdc || iLastChar < iFirstChar)
164 return FALSE;
165
166 GdGetFontInfo(hdc->font->pfont, &fi);
167 for(i=iFirstChar; i <= iLastChar; ++i)
168 if(i < fi.firstchar || i > fi.lastchar || i > 255)
169 lpBuffer[j++] = 0;
170 else lpBuffer[j++] = fi.widths[i];
171 lpBuffer[j++] = fi.widths[i];
172
173 return TRUE;
174 }
175
176 BOOL WINAPI
177 GetTextExtentPoint(
178 HDC hdc, /* handle to DC*/
179 LPCTSTR lpszStr, /* character string*/
180 int cchString, /* number of characters*/
181 LPSIZE lpSize) /* string dimensions*/
182 {
183 int width = 1, height = 1, baseline = 0;
184
185 if (lpSize) {
186 lpSize->cx = 0;
187 lpSize->cy = 0;
188 }
189 if (!hdc || !lpszStr || !cchString || !lpSize)
190 return FALSE;
191 GdGetTextSize(hdc->font->pfont, lpszStr, cchString, &width, &height,
192 &baseline, MWTF_UTF8);
193 lpSize->cx = width;
194 lpSize->cy = height;
195
196 /*printf("<MWIN>: lpszStr=\"%s\", cchString=%d, lpsize->cx=%d, lpSize->cy=%d\n", lpszStr, cchString, lpSize->cx, lpSize->cy);*/
197 return TRUE;
198 }
199
200 BOOL WINAPI
201 GetTextExtentExPoint(HDC hdc, /* handle to DC*/
202 LPCTSTR lpszStr, /* character string*/
203 int cchString, /* number of characters*/
204 int nMaxExtent, /* maximum width of formatted string*/
205 LPINT lpnFit, /* maximum number of characters*/
206 LPINT alpDx, /* array of partial string widths*/
207 LPSIZE lpSize) /* string dimensions*/
208
209 {
210 int attr,width=0,height=0;
211
212 if(!hdc || !lpszStr)
213 return FALSE;
214 if (cchString<0)
215 cchString = strlen((char *)lpszStr);
216 attr=hdc->font->pfont->fontattr;
217 if (attr&FS_FREETYPE)
218 {
219 if (GdGetTextSizeEx(hdc->font->pfont,lpszStr,cchString,
220 nMaxExtent,lpnFit,alpDx,&width,&height,NULL,MWTF_UTF8))
221 {
222 lpSize->cx=width;
223 lpSize->cy=height;
224 return TRUE;
225 }
226 return FALSE;
227 }
228 else
229 {
230 SIZE sz;
231 int i;
232
233 if (!GetTextExtentPoint(hdc, lpszStr, cchString, lpSize))
234 return FALSE;
235 if ((!nMaxExtent)||(!lpnFit)||(!alpDx))
236 return TRUE;
237 for (i=0; i<cchString; i++) {
238 if (!GetTextExtentPoint(hdc, lpszStr, i+1, &sz))
239 return FALSE;
240 if (sz.cx <= nMaxExtent)
241 alpDx[i] = sz.cx;
242 else {
243 (*lpnFit) = i;
244 return TRUE;
245 }
246 }
247 (*lpnFit) = cchString;
248 return TRUE;
249 }
250 }