2 * Copyright (C) 2006, 2007 Apple Inc. All Rights Reserved.
3 * Copyright (c) 2008, 2009, Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #include "platform/fonts/SimpleFontData.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"
48 void SimpleFontData::platformInit()
50 if (!m_platformData.size()) {
51 m_fontMetrics.reset();
58 HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
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 attempt");
71 m_avgCharWidth = textMetric.tmAveCharWidth;
72 m_maxCharWidth = textMetric.tmMaxCharWidth;
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 fonts.
80 OUTLINETEXTMETRIC outlineTextMetric;
81 if (GetOutlineTextMetrics(dc, sizeof(outlineTextMetric), &outlineTextMetric) > 0) {
82 m_fontMetrics.setUnitsPerEm(outlineTextMetric.otmEMSquare);
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);
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);
98 SelectObject(dc, oldFont);
101 void SimpleFontData::platformCharWidthInit()
103 // charwidths are set in platformInit.
106 void SimpleFontData::platformDestroy()
110 PassRefPtr<SimpleFontData> SimpleFontData::platformCreateScaledFontData(const FontDescription& fontDescription, float scaleFactor) const
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_platformData.orientation()), isCustomFont() ? CustomFontData::create(false) : 0);
120 void SimpleFontData::determinePitch()
122 m_treatAsFixedPitch = platformData().isFixedPitch();
125 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph glyph) const
128 SetGraphicsMode(hdc, GM_ADVANCED);
129 HGDIOBJ oldFont = SelectObject(hdc, m_platformData.hfont());
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, &gdiMetrics, 0, 0, &identity) == -1)
139 WTF_LOG_ERROR("Unable to get the glyph metrics after second attempt");
143 SelectObject(hdc, oldFont);
145 return FloatRect(gdiMetrics.gmptGlyphOrigin.x, -gdiMetrics.gmptGlyphOrigin.y,
146 gdiMetrics.gmBlackBoxX, gdiMetrics.gmBlackBoxY);
149 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
151 if (!m_platformData.size())
155 HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
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");
168 SelectObject(dc, oldFont);
170 return static_cast<float>(width);
173 } // namespace WebCore