Home | History | Annotate | Download | only in win
      1 /*
      2  * Copyright (C) 2006, 2007, 2008 Apple Inc.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 #include "SimpleFontData.h"
     31 
     32 #include "Font.h"
     33 #include "FontCache.h"
     34 #include "FloatRect.h"
     35 #include "FontDescription.h"
     36 #include "PlatformString.h"
     37 #include <ApplicationServices/ApplicationServices.h>
     38 #include <WebKitSystemInterface/WebKitSystemInterface.h>
     39 #include <mlang.h>
     40 #include <unicode/uchar.h>
     41 #include <unicode/unorm.h>
     42 #include <winsock2.h>
     43 #include <wtf/MathExtras.h>
     44 #include <wtf/RetainPtr.h>
     45 
     46 namespace WebCore {
     47 
     48 using std::max;
     49 
     50 static inline float scaleEmToUnits(float x, unsigned unitsPerEm) { return unitsPerEm ? x / static_cast<float>(unitsPerEm) : x; }
     51 
     52 void SimpleFontData::platformInit()
     53 {
     54     m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f;
     55     m_scriptCache = 0;
     56     m_scriptFontProperties = 0;
     57     m_isSystemFont = false;
     58 
     59     if (m_platformData.useGDI())
     60        return initGDIFont();
     61 
     62     CGFontRef font = m_platformData.cgFont();
     63     int iAscent = CGFontGetAscent(font);
     64     int iDescent = CGFontGetDescent(font);
     65     int iLineGap = CGFontGetLeading(font);
     66     unsigned unitsPerEm = CGFontGetUnitsPerEm(font);
     67     float pointSize = m_platformData.size();
     68     float fAscent = scaleEmToUnits(iAscent, unitsPerEm) * pointSize;
     69     float fDescent = -scaleEmToUnits(iDescent, unitsPerEm) * pointSize;
     70     float fLineGap = scaleEmToUnits(iLineGap, unitsPerEm) * pointSize;
     71 
     72     if (!isCustomFont()) {
     73         HDC dc = GetDC(0);
     74         HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
     75         int faceLength = GetTextFace(dc, 0, 0);
     76         Vector<WCHAR> faceName(faceLength);
     77         GetTextFace(dc, faceLength, faceName.data());
     78         m_isSystemFont = !wcscmp(faceName.data(), L"Lucida Grande");
     79         SelectObject(dc, oldFont);
     80         ReleaseDC(0, dc);
     81 
     82         if (shouldApplyMacAscentHack()) {
     83             // This code comes from FontDataMac.mm. We only ever do this when running regression tests so that our metrics will match Mac.
     84 
     85             // We need to adjust Times, Helvetica, and Courier to closely match the
     86             // vertical metrics of their Microsoft counterparts that are the de facto
     87             // web standard. The AppKit adjustment of 20% is too big and is
     88             // incorrectly added to line spacing, so we use a 15% adjustment instead
     89             // and add it to the ascent.
     90             if (!wcscmp(faceName.data(), L"Times") || !wcscmp(faceName.data(), L"Helvetica") || !wcscmp(faceName.data(), L"Courier"))
     91                 fAscent += floorf(((fAscent + fDescent) * 0.15f) + 0.5f);
     92         }
     93     }
     94 
     95     m_fontMetrics.setAscent(fAscent);
     96     m_fontMetrics.setDescent(fDescent);
     97     m_fontMetrics.setLineGap(fLineGap);
     98     m_fontMetrics.setLineSpacing(lroundf(fAscent) + lroundf(fDescent) + lroundf(fLineGap));
     99 
    100     GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
    101     Glyph xGlyph = glyphPageZero ? glyphPageZero->glyphDataForCharacter('x').glyph : 0;
    102     if (xGlyph) {
    103         // Measure the actual character "x", since it's possible for it to extend below the baseline, and we need the
    104         // reported x-height to only include the portion of the glyph that is above the baseline.
    105         CGRect xBox;
    106         CGFontGetGlyphBBoxes(font, &xGlyph, 1, &xBox);
    107         m_fontMetrics.setXHeight(scaleEmToUnits(CGRectGetMaxY(xBox), unitsPerEm) * pointSize);
    108     } else {
    109         int iXHeight = CGFontGetXHeight(font);
    110         m_fontMetrics.setXHeight(scaleEmToUnits(iXHeight, unitsPerEm) * pointSize);
    111     }
    112 
    113     m_fontMetrics.setUnitsPerEm(unitsPerEm);
    114 }
    115 
    116 void SimpleFontData::platformCharWidthInit()
    117 {
    118     // GDI Fonts init charwidths in initGDIFont.
    119     if (!m_platformData.useGDI()) {
    120         m_avgCharWidth = 0.f;
    121         m_maxCharWidth = 0.f;
    122         initCharWidths();
    123     }
    124 }
    125 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph glyph) const
    126 {
    127     if (m_platformData.useGDI())
    128         return boundsForGDIGlyph(glyph);
    129 
    130     CGRect box;
    131     CGFontGetGlyphBBoxes(m_platformData.cgFont(), &glyph, 1, &box);
    132     float pointSize = m_platformData.size();
    133     CGFloat scale = pointSize / fontMetrics().unitsPerEm();
    134     FloatRect boundingBox = CGRectApplyAffineTransform(box, CGAffineTransformMakeScale(scale, -scale));
    135     if (m_syntheticBoldOffset)
    136         boundingBox.setWidth(boundingBox.width() + m_syntheticBoldOffset);
    137 
    138     return boundingBox;
    139 }
    140 
    141 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
    142 {
    143     if (m_platformData.useGDI())
    144         return widthForGDIGlyph(glyph);
    145 
    146     CGFontRef font = m_platformData.cgFont();
    147     float pointSize = m_platformData.size();
    148     CGSize advance;
    149     CGAffineTransform m = CGAffineTransformMakeScale(pointSize, pointSize);
    150 
    151     // FIXME: Need to add real support for printer fonts.
    152     bool isPrinterFont = false;
    153     wkGetGlyphAdvances(font, m, m_isSystemFont, isPrinterFont, glyph, advance);
    154 
    155     return advance.width + m_syntheticBoldOffset;
    156 }
    157 
    158 }
    159