OLD | NEW |
| (Empty) |
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 "platform/fonts/SimpleFontData.h" | |
34 | |
35 #include <mlang.h> | |
36 #include <objidl.h> | |
37 #include <unicode/uchar.h> | |
38 #include <unicode/unorm.h> | |
39 #include "platform/fonts/FontCache.h" | |
40 #include "platform/fonts/FontDescription.h" | |
41 #include "platform/fonts/win/FontPlatformDataWin.h" | |
42 #include "platform/geometry/FloatRect.h" | |
43 #include "platform/win/HWndDC.h" | |
44 #include "wtf/MathExtras.h" | |
45 | |
46 namespace WebCore { | |
47 | |
48 void SimpleFontData::platformInit() | |
49 { | |
50 if (!m_platformData.size()) { | |
51 m_fontMetrics.reset(); | |
52 m_avgCharWidth = 0; | |
53 m_maxCharWidth = 0; | |
54 return; | |
55 } | |
56 | |
57 HWndDC dc(0); | |
58 HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont()); | |
59 | |
60 TEXTMETRIC textMetric = {0}; | |
61 if (!GetTextMetrics(dc, &textMetric)) { | |
62 if (FontPlatformData::ensureFontLoaded(m_platformData.hfont())) { | |
63 // Retry GetTextMetrics. | |
64 // FIXME: Handle gracefully the error if this call also fails. | |
65 // See http://crbug.com/6401. | |
66 if (!GetTextMetrics(dc, &textMetric)) | |
67 WTF_LOG_ERROR("Unable to get the text metrics after second attem
pt"); | |
68 } | |
69 } | |
70 | |
71 m_avgCharWidth = textMetric.tmAveCharWidth; | |
72 m_maxCharWidth = textMetric.tmMaxCharWidth; | |
73 | |
74 // FIXME: Access ascent/descent/lineGap with floating point precision. | |
75 float ascent = textMetric.tmAscent; | |
76 float descent = textMetric.tmDescent; | |
77 float lineGap = textMetric.tmExternalLeading; | |
78 float xHeight = ascent * 0.56f; // Best guess for xHeight for non-Truetype f
onts. | |
79 | |
80 OUTLINETEXTMETRIC outlineTextMetric; | |
81 if (GetOutlineTextMetrics(dc, sizeof(outlineTextMetric), &outlineTextMetric)
> 0) { | |
82 m_fontMetrics.setUnitsPerEm(outlineTextMetric.otmEMSquare); | |
83 | |
84 // This is a TrueType font. We might be able to get an accurate xHeight
. | |
85 GLYPHMETRICS glyphMetrics = {0}; | |
86 MAT2 identityMatrix = {{0, 1}, {0, 0}, {0, 0}, {0, 1}}; | |
87 DWORD len = GetGlyphOutlineW(dc, 'x', GGO_METRICS, &glyphMetrics, 0, 0,
&identityMatrix); | |
88 if (len != GDI_ERROR && glyphMetrics.gmBlackBoxY > 0) | |
89 xHeight = static_cast<float>(glyphMetrics.gmBlackBoxY); | |
90 } | |
91 | |
92 m_fontMetrics.setAscent(ascent); | |
93 m_fontMetrics.setDescent(descent); | |
94 m_fontMetrics.setLineGap(lineGap); | |
95 m_fontMetrics.setXHeight(xHeight); | |
96 m_fontMetrics.setLineSpacing(ascent + descent + lineGap); | |
97 | |
98 SelectObject(dc, oldFont); | |
99 } | |
100 | |
101 void SimpleFontData::platformCharWidthInit() | |
102 { | |
103 // charwidths are set in platformInit. | |
104 } | |
105 | |
106 void SimpleFontData::platformDestroy() | |
107 { | |
108 } | |
109 | |
110 PassRefPtr<SimpleFontData> SimpleFontData::platformCreateScaledFontData(const Fo
ntDescription& fontDescription, float scaleFactor) const | |
111 { | |
112 LOGFONT winFont; | |
113 GetObject(m_platformData.hfont(), sizeof(LOGFONT), &winFont); | |
114 float scaledSize = scaleFactor * fontDescription.computedSize(); | |
115 winFont.lfHeight = -lroundf(scaledSize); | |
116 HFONT hfont = CreateFontIndirect(&winFont); | |
117 return SimpleFontData::create(FontPlatformData(hfont, scaledSize, m_platform
Data.orientation()), isCustomFont() ? CustomFontData::create(false) : 0); | |
118 } | |
119 | |
120 void SimpleFontData::determinePitch() | |
121 { | |
122 m_treatAsFixedPitch = platformData().isFixedPitch(); | |
123 } | |
124 | |
125 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph glyph) const | |
126 { | |
127 HWndDC hdc(0); | |
128 SetGraphicsMode(hdc, GM_ADVANCED); | |
129 HGDIOBJ oldFont = SelectObject(hdc, m_platformData.hfont()); | |
130 | |
131 GLYPHMETRICS gdiMetrics; | |
132 static const MAT2 identity = { 0, 1, 0, 0, 0, 0, 0, 1 }; | |
133 if (GetGlyphOutline(hdc, glyph, GGO_METRICS | GGO_GLYPH_INDEX, &gdiMetrics,
0, 0, &identity) == -1) { | |
134 if (FontPlatformData::ensureFontLoaded(m_platformData.hfont())) { | |
135 // Retry GetTextMetrics. | |
136 // FIXME: Handle gracefully the error if this call also fails. | |
137 // See http://crbug.com/6401. | |
138 if (GetGlyphOutline(hdc, glyph, GGO_METRICS | GGO_GLYPH_INDEX, &gdiM
etrics, 0, 0, &identity) == -1) | |
139 WTF_LOG_ERROR("Unable to get the glyph metrics after second atte
mpt"); | |
140 } | |
141 } | |
142 | |
143 SelectObject(hdc, oldFont); | |
144 | |
145 return FloatRect(gdiMetrics.gmptGlyphOrigin.x, -gdiMetrics.gmptGlyphOrigin.y
, | |
146 gdiMetrics.gmBlackBoxX, gdiMetrics.gmBlackBoxY); | |
147 } | |
148 | |
149 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const | |
150 { | |
151 if (!m_platformData.size()) | |
152 return 0; | |
153 | |
154 HWndDC dc(0); | |
155 HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont()); | |
156 | |
157 int width = 0; | |
158 if (!GetCharWidthI(dc, glyph, 1, 0, &width)) { | |
159 // Ask the browser to preload the font and retry. | |
160 if (FontPlatformData::ensureFontLoaded(m_platformData.hfont())) { | |
161 // FIXME: Handle gracefully the error if this call also fails. | |
162 // See http://crbug.com/6401. | |
163 if (!GetCharWidthI(dc, glyph, 1, 0, &width)) | |
164 WTF_LOG_ERROR("Unable to get the char width after second attempt
"); | |
165 } | |
166 } | |
167 | |
168 SelectObject(dc, oldFont); | |
169 | |
170 return static_cast<float>(width); | |
171 } | |
172 | |
173 } // namespace WebCore | |
OLD | NEW |