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