]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/layout/GDIFontInstance.cpp
ICU-6.2.13.tar.gz
[apple/icu.git] / icuSources / samples / layout / GDIFontInstance.cpp
CommitLineData
b75a7d8f
A
1/*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: GDIFontInstance.cpp
9 *
10 * created on: 08/09/2000
11 * created by: Eric R. Mader
12 */
13
14#include <windows.h>
15
16#include "layout/LETypes.h"
17#include "layout/LESwaps.h"
18#include "layout/LEFontInstance.h"
19
20#include "GDIFontInstance.h"
21#include "sfnt.h"
22#include "cmaps.h"
23
24GDISurface::GDISurface(HDC theHDC)
25 : fHdc(theHDC), fCurrentFont(NULL)
26{
27 // nothing else to do
28}
29
30GDISurface::~GDISurface()
31{
32 // nothing to do
33}
34
35void GDISurface::setHDC(HDC theHDC)
36{
37 fHdc = theHDC;
38 fCurrentFont = NULL;
39}
40
41void GDISurface::setFont(const GDIFontInstance *font)
42{
43#if 0
44 if (fCurrentFont != font) {
45 fCurrentFont = font;
46 SelectObject(fHdc, font->getFont());
47 }
48#else
49 SelectObject(fHdc, font->getFont());
50#endif
51}
52
53void GDISurface::drawGlyphs(const LEFontInstance *font, const LEGlyphID *glyphs, le_int32 count, const float *positions,
54 le_int32 x, le_int32 y, le_int32 width, le_int32 height)
55{
56 TTGlyphID *ttGlyphs = LE_NEW_ARRAY(TTGlyphID, count);
57 le_int32 *dx = LE_NEW_ARRAY(le_int32, count);
58 RECT clip;
59
60 clip.top = 0;
61 clip.left = 0;
62 clip.bottom = height;
63 clip.right = width;
64
65 for (le_int32 g = 0; g < count; g += 1) {
66 ttGlyphs[g] = (TTGlyphID) LE_GET_GLYPH(glyphs[g]);
67
68 if (ttGlyphs[g] == 0xFFFF || ttGlyphs[g] == 0xFFFE) {
69 ttGlyphs[g] = 0x0002;
70 }
71
72 dx[g] = (le_int32) (positions[g * 2 + 2] - positions[g * 2]);
73 }
74
75 le_int32 dyStart, dyEnd;
76
77 setFont((GDIFontInstance *) font);
78
79 dyStart = dyEnd = 0;
80
81 while (dyEnd < count) {
82 float yOffset = positions[dyStart * 2 + 1];
83 float xOffset = positions[dyStart * 2];
84
85 while (dyEnd < count && yOffset == positions[dyEnd * 2 + 1]) {
86 dyEnd += 1;
87 }
88
89 ExtTextOut(fHdc, x + (le_int32) xOffset, y + (le_int32) yOffset - font->getAscent(), ETO_CLIPPED | ETO_GLYPH_INDEX, &clip,
90 &ttGlyphs[dyStart], dyEnd - dyStart, (INT *) &dx[dyStart]);
91
92 dyStart = dyEnd;
93 }
94
95 LE_DELETE_ARRAY(dx);
96 LE_DELETE_ARRAY(ttGlyphs);
97}
98
99GDIFontInstance::GDIFontInstance(GDISurface *surface, TCHAR *faceName, le_int16 pointSize, LEErrorCode &status)
100 : FontTableCache(), fSurface(surface), fFont(NULL),
101 fPointSize(pointSize), fUnitsPerEM(0), fAscent(0), fDescent(0), fLeading(0),
102 fDeviceScaleX(1), fDeviceScaleY(1), fMapper(NULL)
103{
104 LOGFONT lf;
105 FLOAT dpiX, dpiY;
106 POINT pt;
107 OUTLINETEXTMETRIC otm;
108 HDC hdc = surface->getHDC();
109
110 if (LE_FAILURE(status)) {
111 return;
112 }
113
114 SaveDC(hdc);
115
116 SetGraphicsMode(hdc, GM_ADVANCED);
117 ModifyWorldTransform(hdc, NULL, MWT_IDENTITY);
118 SetViewportOrgEx(hdc, 0, 0, NULL);
119 SetWindowOrgEx(hdc, 0, 0, NULL);
120
121 dpiX = (FLOAT) GetDeviceCaps(hdc, LOGPIXELSX);
122 dpiY = (FLOAT) GetDeviceCaps(hdc, LOGPIXELSY);
123
124#if 1
125 pt.x = (int) (pointSize * dpiX / 72);
126 pt.y = (int) (pointSize * dpiY / 72);
127
128 DPtoLP(hdc, &pt, 1);
129#else
130 pt.x = pt.y = pointSize;
131#endif
132
133 lf.lfHeight = - pt.y;
134 lf.lfWidth = 0;
135 lf.lfEscapement = 0;
136 lf.lfOrientation = 0;
137 lf.lfWeight = 0;
138 lf.lfItalic = 0;
139 lf.lfUnderline = 0;
140 lf.lfStrikeOut = 0;
141 lf.lfCharSet = DEFAULT_CHARSET;
142 lf.lfOutPrecision = 0;
143 lf.lfClipPrecision = 0;
144 lf.lfQuality = 0;
145 lf.lfPitchAndFamily = 0;
146
147 lstrcpy(lf.lfFaceName, faceName);
148
149 fFont = CreateFontIndirect(&lf);
150
151 if (fFont == NULL) {
152 status = LE_FONT_FILE_NOT_FOUND_ERROR;
153 return;
154 }
155
156 SelectObject(hdc, fFont);
157
158 UINT ret = GetOutlineTextMetrics(hdc, sizeof otm, &otm);
159
160 if (ret == 0) {
161 status = LE_MISSING_FONT_TABLE_ERROR;
162 goto restore;
163 }
164
165 fUnitsPerEM = otm.otmEMSquare;
166 fAscent = otm.otmTextMetrics.tmAscent;
167 fDescent = otm.otmTextMetrics.tmDescent;
168 fLeading = otm.otmTextMetrics.tmExternalLeading;
169
170 status = initMapper();
171
172 if (LE_FAILURE(status)) {
173 goto restore;
174 }
175
176#if 0
177 status = initFontTableCache();
178#endif
179
180restore:
181 RestoreDC(hdc, -1);
182}
183
184GDIFontInstance::GDIFontInstance(GDISurface *surface, const char *faceName, le_int16 pointSize, LEErrorCode &status)
185 : FontTableCache(), fSurface(surface), fFont(NULL),
186 fPointSize(pointSize), fUnitsPerEM(0), fAscent(0), fDescent(0), fLeading(0),
187 fDeviceScaleX(1), fDeviceScaleY(1), fMapper(NULL)
188{
189 LOGFONTA lf;
190 FLOAT dpiX, dpiY;
191 POINT pt;
192 OUTLINETEXTMETRIC otm;
193 HDC hdc = surface->getHDC();
194
195 if (LE_FAILURE(status)) {
196 return;
197 }
198
199 SaveDC(hdc);
200
201 SetGraphicsMode(hdc, GM_ADVANCED);
202 ModifyWorldTransform(hdc, NULL, MWT_IDENTITY);
203 SetViewportOrgEx(hdc, 0, 0, NULL);
204 SetWindowOrgEx(hdc, 0, 0, NULL);
205
206 dpiX = (FLOAT) GetDeviceCaps(hdc, LOGPIXELSX);
207 dpiY = (FLOAT) GetDeviceCaps(hdc, LOGPIXELSY);
208
209 fDeviceScaleX = dpiX / 72;
210 fDeviceScaleY = dpiY / 72;
211
212#if 1
213 pt.x = (int) (pointSize * fDeviceScaleX);
214 pt.y = (int) (pointSize * fDeviceScaleY);
215
216 DPtoLP(hdc, &pt, 1);
217#else
218 pt.x = pt.y = pointSize;
219#endif
220
221 lf.lfHeight = - pt.y;
222 lf.lfWidth = 0;
223 lf.lfEscapement = 0;
224 lf.lfOrientation = 0;
225 lf.lfWeight = 0;
226 lf.lfItalic = 0;
227 lf.lfUnderline = 0;
228 lf.lfStrikeOut = 0;
229 lf.lfCharSet = DEFAULT_CHARSET;
230 lf.lfOutPrecision = 0;
231 lf.lfClipPrecision = 0;
232 lf.lfQuality = 0;
233 lf.lfPitchAndFamily = 0;
234
235 strcpy(lf.lfFaceName, faceName);
236
237 fFont = CreateFontIndirectA(&lf);
238
239 if (fFont == NULL) {
240 status = LE_FONT_FILE_NOT_FOUND_ERROR;
241 return;
242 }
243
244 SelectObject(hdc, fFont);
245
246 UINT ret = GetOutlineTextMetrics(hdc, sizeof otm, &otm);
247
248 if (ret == 0) {
249 status = LE_MISSING_FONT_TABLE_ERROR;
250 goto restore;
251 }
252
253 fUnitsPerEM = otm.otmEMSquare;
254 fAscent = otm.otmTextMetrics.tmAscent;
255 fDescent = otm.otmTextMetrics.tmDescent;
256 fLeading = otm.otmTextMetrics.tmExternalLeading;
257
258 status = initMapper();
259
260 if (LE_FAILURE(status)) {
261 goto restore;
262 }
263
264#if 0
265 status = initFontTableCache();
266#endif
267
268restore:
269 RestoreDC(hdc, -1);
270}
271
272GDIFontInstance::~GDIFontInstance()
273{
274#if 0
275 flushFontTableCache();
276 delete[] fTableCache;
277#endif
278
279 if (fFont != NULL) {
280 // FIXME: call RemoveObject first?
281 DeleteObject(fFont);
282 }
283
284 delete fMapper;
285 fMapper = NULL;
286}
287
288LEErrorCode GDIFontInstance::initMapper()
289{
290 LETag cmapTag = LE_CMAP_TABLE_TAG;
291 const CMAPTable *cmap = (const CMAPTable *) readFontTable(cmapTag);
292
293 if (cmap == NULL) {
294 return LE_MISSING_FONT_TABLE_ERROR;
295 }
296
297 fMapper = CMAPMapper::createUnicodeMapper(cmap);
298
299 if (fMapper == NULL) {
300 return LE_MISSING_FONT_TABLE_ERROR;
301 }
302
303 return LE_NO_ERROR;
304}
305
306const void *GDIFontInstance::getFontTable(LETag tableTag) const
307{
308 return FontTableCache::find(tableTag);
309}
310
311const void *GDIFontInstance::readFontTable(LETag tableTag) const
312{
313 fSurface->setFont(this);
314
315 HDC hdc = fSurface->getHDC();
316 DWORD stag = SWAPL(tableTag);
317 DWORD len = GetFontData(hdc, stag, 0, NULL, 0);
318 void *result = NULL;
319
320 if (len != GDI_ERROR) {
321 result = LE_NEW_ARRAY(char, len);
322 GetFontData(hdc, stag, 0, result, len);
323 }
324
325 return result;
326}
327
328void GDIFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
329{
330 advance.fX = 0;
331 advance.fY = 0;
332
333 if (glyph == 0xFFFE || glyph == 0xFFFF) {
334 return;
335 }
336
337
338 GLYPHMETRICS metrics;
339 DWORD result;
340 MAT2 identity = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
341 HDC hdc = fSurface->getHDC();
342
343 fSurface->setFont(this);
344
345 result = GetGlyphOutline(hdc, glyph, GGO_GLYPH_INDEX | GGO_METRICS, &metrics, 0, NULL, &identity);
346
347 if (result == GDI_ERROR) {
348 return;
349 }
350
351 advance.fX = metrics.gmCellIncX;
352 return;
353}
354
355le_bool GDIFontInstance::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
356{
357#if 0
358 hsFixedPoint2 pt;
359 le_bool result;
360
361 result = fFontInstance->getGlyphPoint(glyph, pointNumber, pt);
362
363 if (result) {
364 point.fX = xUnitsToPoints(pt.fX);
365 point.fY = yUnitsToPoints(pt.fY);
366 }
367
368 return result;
369#else
374ca955 370 return FALSE;
b75a7d8f
A
371#endif
372}
373