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 "SimpleFontData.h"
35 #include "FloatRect.h"
37 #include "FontCache.h"
38 #include "FontDescription.h"
39 #include "PlatformBridge.h"
40 #include <wtf/MathExtras.h>
42 #include <unicode/uchar.h>
43 #include <unicode/unorm.h>
49 static inline float scaleEmToUnits(float x, int unitsPerEm)
51 return unitsPerEm ? x / static_cast<float>(unitsPerEm) : x;
54 void SimpleFontData::platformInit()
56 if (!m_platformData.size()) {
69 HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
71 TEXTMETRIC textMetric = {0};
72 if (!GetTextMetrics(dc, &textMetric)) {
73 if (PlatformBridge::ensureFontLoaded(m_platformData.hfont())) {
74 // Retry GetTextMetrics.
75 // FIXME: Handle gracefully the error if this call also fails.
76 // See http://crbug.com/6401.
77 if (!GetTextMetrics(dc, &textMetric))
78 LOG_ERROR("Unable to get the text metrics after second attempt");
82 m_avgCharWidth = textMetric.tmAveCharWidth;
83 m_maxCharWidth = textMetric.tmMaxCharWidth;
85 m_ascent = textMetric.tmAscent;
86 m_descent = textMetric.tmDescent;
87 m_lineGap = textMetric.tmExternalLeading;
88 m_xHeight = m_ascent * 0.56f; // Best guess for xHeight for non-Truetype fonts.
90 OUTLINETEXTMETRIC outlineTextMetric;
91 if (GetOutlineTextMetrics(dc, sizeof(outlineTextMetric), &outlineTextMetric) > 0) {
92 // This is a TrueType font. We might be able to get an accurate xHeight.
93 GLYPHMETRICS glyphMetrics = {0};
94 MAT2 identityMatrix = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
95 DWORD len = GetGlyphOutlineW(dc, 'x', GGO_METRICS, &glyphMetrics, 0, 0, &identityMatrix);
96 if (len != GDI_ERROR && glyphMetrics.gmBlackBoxY > 0)
97 m_xHeight = static_cast<float>(glyphMetrics.gmBlackBoxY);
100 m_lineSpacing = m_ascent + m_descent + m_lineGap;
102 SelectObject(dc, oldFont);
106 void SimpleFontData::platformCharWidthInit()
108 // charwidths are set in platformInit.
111 void SimpleFontData::platformDestroy()
115 SimpleFontData* SimpleFontData::scaledFontData(const FontDescription& fontDescription, float scaleFactor) const
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);
125 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
127 if (!m_derivedFontData)
128 m_derivedFontData = DerivedFontData::create(isCustomFont());
129 if (!m_derivedFontData->smallCaps)
130 m_derivedFontData->smallCaps = scaledFontData(fontDescription, .7);
132 return m_derivedFontData->smallCaps.get();
135 SimpleFontData* SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const
137 if (!m_derivedFontData)
138 m_derivedFontData = DerivedFontData::create(isCustomFont());
139 if (!m_derivedFontData->emphasisMark)
140 m_derivedFontData->emphasisMark = scaledFontData(fontDescription, .5);
142 return m_derivedFontData->emphasisMark.get();
145 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
147 // This used to be implemented with IMLangFontLink2, but since that code has
148 // been disabled, this would always return false anyway.
152 void SimpleFontData::determinePitch()
154 // TEXTMETRICS have this. Set m_treatAsFixedPitch based off that.
156 HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
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");
171 m_treatAsFixedPitch = ((textMetric.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0);
173 SelectObject(dc, oldFont);
177 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph) const
182 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
184 if (!m_platformData.size())
188 HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
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");
201 SelectObject(dc, oldFont);
204 return static_cast<float>(width);
207 } // namespace WebCore