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 <winsock2.h> 33 #include "Font.h" 34 #include "FontCache.h" 35 #include "FloatRect.h" 36 #include "FontDescription.h" 37 #include "PlatformString.h" 38 #include <wtf/MathExtras.h> 39 #include <wtf/RetainPtr.h> 40 #include <unicode/uchar.h> 41 #include <unicode/unorm.h> 42 #include <ApplicationServices/ApplicationServices.h> 43 #include <WebKitSystemInterface/WebKitSystemInterface.h> 44 #include <mlang.h> 45 #include <tchar.h> 46 47 namespace WebCore { 48 49 using std::max; 50 51 static inline float scaleEmToUnits(float x, unsigned unitsPerEm) { return unitsPerEm ? x / static_cast<float>(unitsPerEm) : x; } 52 53 void SimpleFontData::platformInit() 54 { 55 m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f; 56 m_scriptCache = 0; 57 m_scriptFontProperties = 0; 58 m_isSystemFont = false; 59 60 if (m_platformData.useGDI()) 61 return initGDIFont(); 62 63 CGFontRef font = m_platformData.cgFont(); 64 int iAscent = CGFontGetAscent(font); 65 int iDescent = CGFontGetDescent(font); 66 int iLineGap = CGFontGetLeading(font); 67 m_unitsPerEm = CGFontGetUnitsPerEm(font); 68 float pointSize = m_platformData.size(); 69 float fAscent = scaleEmToUnits(iAscent, m_unitsPerEm) * pointSize; 70 float fDescent = -scaleEmToUnits(iDescent, m_unitsPerEm) * pointSize; 71 float fLineGap = scaleEmToUnits(iLineGap, m_unitsPerEm) * pointSize; 72 73 if (!isCustomFont()) { 74 HDC dc = GetDC(0); 75 HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont()); 76 int faceLength = GetTextFace(dc, 0, 0); 77 Vector<TCHAR> faceName(faceLength); 78 GetTextFace(dc, faceLength, faceName.data()); 79 m_isSystemFont = !_tcscmp(faceName.data(), _T("Lucida Grande")); 80 SelectObject(dc, oldFont); 81 ReleaseDC(0, dc); 82 83 if (shouldApplyMacAscentHack()) { 84 // This code comes from FontDataMac.mm. We only ever do this when running regression tests so that our metrics will match Mac. 85 86 // We need to adjust Times, Helvetica, and Courier to closely match the 87 // vertical metrics of their Microsoft counterparts that are the de facto 88 // web standard. The AppKit adjustment of 20% is too big and is 89 // incorrectly added to line spacing, so we use a 15% adjustment instead 90 // and add it to the ascent. 91 if (!_tcscmp(faceName.data(), _T("Times")) || !_tcscmp(faceName.data(), _T("Helvetica")) || !_tcscmp(faceName.data(), _T("Courier"))) 92 fAscent += floorf(((fAscent + fDescent) * 0.15f) + 0.5f); 93 } 94 } 95 96 m_ascent = lroundf(fAscent); 97 m_descent = lroundf(fDescent); 98 m_lineGap = lroundf(fLineGap); 99 m_lineSpacing = m_ascent + m_descent + m_lineGap; 100 101 // Measure the actual character "x", because AppKit synthesizes X height rather than getting it from the font. 102 // Unfortunately, NSFont will round this for us so we don't quite get the right value. 103 GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page(); 104 Glyph xGlyph = glyphPageZero ? glyphPageZero->glyphDataForCharacter('x').glyph : 0; 105 if (xGlyph) { 106 CGRect xBox; 107 CGFontGetGlyphBBoxes(font, &xGlyph, 1, &xBox); 108 // Use the maximum of either width or height because "x" is nearly square 109 // and web pages that foolishly use this metric for width will be laid out 110 // poorly if we return an accurate height. Classic case is Times 13 point, 111 // which has an "x" that is 7x6 pixels. 112 m_xHeight = scaleEmToUnits(max(CGRectGetMaxX(xBox), CGRectGetMaxY(xBox)), m_unitsPerEm) * pointSize; 113 } else { 114 int iXHeight = CGFontGetXHeight(font); 115 m_xHeight = scaleEmToUnits(iXHeight, m_unitsPerEm) * pointSize; 116 } 117 } 118 119 void SimpleFontData::platformCharWidthInit() 120 { 121 // GDI Fonts init charwidths in initGDIFont. 122 if (!m_platformData.useGDI()) { 123 m_avgCharWidth = 0.f; 124 m_maxCharWidth = 0.f; 125 initCharWidths(); 126 } 127 } 128 129 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const 130 { 131 if (m_platformData.useGDI()) 132 return widthForGDIGlyph(glyph); 133 134 CGFontRef font = m_platformData.cgFont(); 135 float pointSize = m_platformData.size(); 136 CGSize advance; 137 CGAffineTransform m = CGAffineTransformMakeScale(pointSize, pointSize); 138 139 // FIXME: Need to add real support for printer fonts. 140 bool isPrinterFont = false; 141 wkGetGlyphAdvances(font, m, m_isSystemFont, isPrinterFont, glyph, advance); 142 143 return advance.width + m_syntheticBoldOffset; 144 } 145 146 } 147