• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007 Apple Inc. All Rights Reserved.
3  * Copyright (c) 2008, 2009, Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "config.h"
33 #include "SimpleFontData.h"
34 
35 #include "FloatRect.h"
36 #include "Font.h"
37 #include "FontCache.h"
38 #include "FontDescription.h"
39 #include "PlatformBridge.h"
40 #include <wtf/MathExtras.h>
41 
42 #include <unicode/uchar.h>
43 #include <unicode/unorm.h>
44 #include <objidl.h>
45 #include <mlang.h>
46 
47 namespace WebCore {
48 
scaleEmToUnits(float x,int unitsPerEm)49 static inline float scaleEmToUnits(float x, int unitsPerEm)
50 {
51     return unitsPerEm ? x / static_cast<float>(unitsPerEm) : x;
52 }
53 
platformInit()54 void SimpleFontData::platformInit()
55 {
56     if (!m_platformData.size()) {
57         m_fontMetrics.reset();
58         m_avgCharWidth = 0;
59         m_maxCharWidth = 0;
60         return;
61     }
62 
63     HDC dc = GetDC(0);
64     HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
65 
66     TEXTMETRIC textMetric = {0};
67     if (!GetTextMetrics(dc, &textMetric)) {
68         if (PlatformBridge::ensureFontLoaded(m_platformData.hfont())) {
69             // Retry GetTextMetrics.
70             // FIXME: Handle gracefully the error if this call also fails.
71             // See http://crbug.com/6401.
72             if (!GetTextMetrics(dc, &textMetric))
73                 LOG_ERROR("Unable to get the text metrics after second attempt");
74         }
75     }
76 
77     m_avgCharWidth = textMetric.tmAveCharWidth;
78     m_maxCharWidth = textMetric.tmMaxCharWidth;
79 
80     // FIXME: Access ascent/descent/lineGap with floating point precision.
81     float ascent = textMetric.tmAscent;
82     float descent = textMetric.tmDescent;
83     float lineGap = textMetric.tmExternalLeading;
84     float xHeight = ascent * 0.56f; // Best guess for xHeight for non-Truetype fonts.
85 
86     OUTLINETEXTMETRIC outlineTextMetric;
87     if (GetOutlineTextMetrics(dc, sizeof(outlineTextMetric), &outlineTextMetric) > 0) {
88         // This is a TrueType font.  We might be able to get an accurate xHeight.
89         GLYPHMETRICS glyphMetrics = {0};
90         MAT2 identityMatrix = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
91         DWORD len = GetGlyphOutlineW(dc, 'x', GGO_METRICS, &glyphMetrics, 0, 0, &identityMatrix);
92         if (len != GDI_ERROR && glyphMetrics.gmBlackBoxY > 0)
93             xHeight = static_cast<float>(glyphMetrics.gmBlackBoxY);
94     }
95 
96     m_fontMetrics.setAscent(ascent);
97     m_fontMetrics.setDescent(descent);
98     m_fontMetrics.setLineGap(lineGap);
99     m_fontMetrics.setXHeight(xHeight);
100     m_fontMetrics.setLineSpacing(ascent + descent + lineGap);
101 
102     SelectObject(dc, oldFont);
103     ReleaseDC(0, dc);
104 }
105 
platformCharWidthInit()106 void SimpleFontData::platformCharWidthInit()
107 {
108     // charwidths are set in platformInit.
109 }
110 
platformDestroy()111 void SimpleFontData::platformDestroy()
112 {
113 }
114 
scaledFontData(const FontDescription & fontDescription,float scaleFactor) const115 SimpleFontData* SimpleFontData::scaledFontData(const FontDescription& fontDescription, float scaleFactor) const
116 {
117     LOGFONT winFont;
118     GetObject(m_platformData.hfont(), sizeof(LOGFONT), &winFont);
119     float scaledSize = scaleFactor * fontDescription.computedSize();
120     winFont.lfHeight = -lroundf(scaledSize);
121     HFONT hfont = CreateFontIndirect(&winFont);
122     return new SimpleFontData(FontPlatformData(hfont, scaledSize), isCustomFont(), false);
123 }
124 
smallCapsFontData(const FontDescription & fontDescription) const125 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
126 {
127     if (!m_derivedFontData)
128         m_derivedFontData = DerivedFontData::create(isCustomFont());
129     if (!m_derivedFontData->smallCaps)
130         m_derivedFontData->smallCaps = scaledFontData(fontDescription, .7);
131 
132     return m_derivedFontData->smallCaps.get();
133 }
134 
emphasisMarkFontData(const FontDescription & fontDescription) const135 SimpleFontData* SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const
136 {
137     if (!m_derivedFontData)
138         m_derivedFontData = DerivedFontData::create(isCustomFont());
139     if (!m_derivedFontData->emphasisMark)
140         m_derivedFontData->emphasisMark = scaledFontData(fontDescription, .5);
141 
142     return m_derivedFontData->emphasisMark.get();
143 }
144 
containsCharacters(const UChar * characters,int length) const145 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
146 {
147   // This used to be implemented with IMLangFontLink2, but since that code has
148   // been disabled, this would always return false anyway.
149   return false;
150 }
151 
determinePitch()152 void SimpleFontData::determinePitch()
153 {
154     // TEXTMETRICS have this.  Set m_treatAsFixedPitch based off that.
155     HDC dc = GetDC(0);
156     HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
157 
158     // Yes, this looks backwards, but the fixed pitch bit is actually set if the font
159     // is *not* fixed pitch.  Unbelievable but true.
160     TEXTMETRIC textMetric = {0};
161     if (!GetTextMetrics(dc, &textMetric)) {
162         if (PlatformBridge::ensureFontLoaded(m_platformData.hfont())) {
163             // Retry GetTextMetrics.
164             // FIXME: Handle gracefully the error if this call also fails.
165             // See http://crbug.com/6401.
166             if (!GetTextMetrics(dc, &textMetric))
167                 LOG_ERROR("Unable to get the text metrics after second attempt");
168         }
169     }
170 
171     m_treatAsFixedPitch = ((textMetric.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0);
172 
173     SelectObject(dc, oldFont);
174     ReleaseDC(0, dc);
175 }
176 
platformBoundsForGlyph(Glyph) const177 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph) const
178 {
179     return FloatRect();
180 }
181 
platformWidthForGlyph(Glyph glyph) const182 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
183 {
184     if (!m_platformData.size())
185         return 0;
186 
187     HDC dc = GetDC(0);
188     HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
189 
190     int width = 0;
191     if (!GetCharWidthI(dc, glyph, 1, 0, &width)) {
192         // Ask the browser to preload the font and retry.
193         if (PlatformBridge::ensureFontLoaded(m_platformData.hfont())) {
194             // FIXME: Handle gracefully the error if this call also fails.
195             // See http://crbug.com/6401.
196             if (!GetCharWidthI(dc, glyph, 1, 0, &width))
197                 LOG_ERROR("Unable to get the char width after second attempt");
198         }
199     }
200 
201     SelectObject(dc, oldFont);
202     ReleaseDC(0, dc);
203 
204     return static_cast<float>(width);
205 }
206 
207 }  // namespace WebCore
208