Home | History | Annotate | Download | only in layout
      1 /*
      2  *******************************************************************************
      3  *
      4  *   Copyright (C) 1999-2008, 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 
     24 GDISurface::GDISurface(HDC theHDC)
     25     : fHdc(theHDC), fCurrentFont(NULL)
     26 {
     27     // nothing else to do
     28 }
     29 
     30 GDISurface::~GDISurface()
     31 {
     32     // nothing to do
     33 }
     34 
     35 void GDISurface::setHDC(HDC theHDC)
     36 {
     37     fHdc         = theHDC;
     38     fCurrentFont = NULL;
     39 }
     40 
     41 void 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 
     53 void 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 
    104 GDIFontInstance::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 
    185 restore:
    186     RestoreDC(hdc, -1);
    187 }
    188 
    189 GDIFontInstance::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         fUnitsPerEM = otm.otmEMSquare;
    255         fAscent  = otm.otmTextMetrics.tmAscent;
    256         fDescent = otm.otmTextMetrics.tmDescent;
    257         fLeading = otm.otmTextMetrics.tmExternalLeading;
    258     } else {
    259         const HEADTable *headTable = NULL;
    260         const HHEATable *hheaTable = NULL;
    261 
    262         // read unitsPerEm from 'head' table
    263         headTable = (const HEADTable *) readFontTable(LE_HEAD_TABLE_TAG);
    264 
    265         if (headTable == NULL) {
    266             status = LE_MISSING_FONT_TABLE_ERROR;
    267             goto restore;
    268         }
    269 
    270         fUnitsPerEM   = SWAPW(headTable->unitsPerEm);
    271         freeFontTable((const void *)headTable);
    272 
    273         hheaTable = (HHEATable *) readFontTable(LE_HHEA_TABLE_TAG);
    274 
    275         if (hheaTable == NULL) {
    276             status = LE_MISSING_FONT_TABLE_ERROR;
    277             goto restore;
    278         }
    279 
    280         fAscent  = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->ascent));
    281         fDescent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->descent));
    282         fLeading = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->lineGap));
    283 
    284         freeFontTable((const void *) hheaTable);
    285     }
    286 
    287     status = initMapper();
    288 
    289     if (LE_FAILURE(status)) {
    290         goto restore;
    291     }
    292 
    293 #if 0
    294     status = initFontTableCache();
    295 #endif
    296 
    297 restore:
    298     RestoreDC(hdc, -1);
    299 }
    300 
    301 GDIFontInstance::~GDIFontInstance()
    302 {
    303 #if 0
    304     flushFontTableCache();
    305     delete[] fTableCache;
    306 #endif
    307 
    308     if (fFont != NULL) {
    309         // FIXME: call RemoveObject first?
    310         DeleteObject(fFont);
    311     }
    312 
    313     delete fMapper;
    314     fMapper = NULL;
    315 }
    316 
    317 LEErrorCode GDIFontInstance::initMapper()
    318 {
    319     LETag cmapTag = LE_CMAP_TABLE_TAG;
    320     const CMAPTable *cmap = (const CMAPTable *) readFontTable(cmapTag);
    321 
    322     if (cmap == NULL) {
    323         return LE_MISSING_FONT_TABLE_ERROR;
    324     }
    325 
    326     fMapper = CMAPMapper::createUnicodeMapper(cmap);
    327 
    328     if (fMapper == NULL) {
    329         return LE_MISSING_FONT_TABLE_ERROR;
    330     }
    331 
    332     return LE_NO_ERROR;
    333 }
    334 
    335 const void *GDIFontInstance::getFontTable(LETag tableTag) const
    336 {
    337     return FontTableCache::find(tableTag);
    338 }
    339 
    340 const void *GDIFontInstance::readFontTable(LETag tableTag) const
    341 {
    342     fSurface->setFont(this);
    343 
    344     HDC   hdc    = fSurface->getHDC();
    345     DWORD stag   = SWAPL(tableTag);
    346     DWORD len    = GetFontData(hdc, stag, 0, NULL, 0);
    347     void *result = NULL;
    348 
    349     if (len != GDI_ERROR) {
    350         result = LE_NEW_ARRAY(char, len);
    351         GetFontData(hdc, stag, 0, result, len);
    352     }
    353 
    354     return result;
    355 }
    356 
    357 void GDIFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
    358 {
    359     advance.fX = 0;
    360     advance.fY = 0;
    361 
    362     if (glyph == 0xFFFE || glyph == 0xFFFF) {
    363         return;
    364     }
    365 
    366 
    367     GLYPHMETRICS metrics;
    368     DWORD result;
    369     MAT2 identity = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
    370     HDC hdc = fSurface->getHDC();
    371 
    372     fSurface->setFont(this);
    373 
    374     result = GetGlyphOutline(hdc, glyph, GGO_GLYPH_INDEX | GGO_METRICS, &metrics, 0, NULL, &identity);
    375 
    376     if (result == GDI_ERROR) {
    377         return;
    378     }
    379 
    380     advance.fX = metrics.gmCellIncX;
    381     return;
    382 }
    383 
    384 le_bool GDIFontInstance::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
    385 {
    386 #if 0
    387     hsFixedPoint2 pt;
    388     le_bool result;
    389 
    390     result = fFontInstance->getGlyphPoint(glyph, pointNumber, pt);
    391 
    392     if (result) {
    393         point.fX = xUnitsToPoints(pt.fX);
    394         point.fY = yUnitsToPoints(pt.fY);
    395     }
    396 
    397     return result;
    398 #else
    399     return FALSE;
    400 #endif
    401 }
    402 
    403