2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "SimpleFontData.h"
35 #include "FontCache.h"
36 #include "FontDescription.h"
38 #include <cairo-win32.h>
40 #include <wtf/MathExtras.h>
44 void SimpleFontData::platformInit()
47 m_scriptFontProperties = 0;
48 m_isSystemFont = false;
50 m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f;
52 if (m_platformData.useGDI())
58 cairo_scaled_font_t* scaledFont = m_platformData.scaledFont();
59 const double metricsMultiplier = cairo_win32_scaled_font_get_metrics_factor(scaledFont) * m_platformData.size();
61 cairo_win32_scaled_font_select_font(scaledFont, hdc);
63 TEXTMETRIC textMetrics;
64 GetTextMetrics(hdc, &textMetrics);
65 float ascent = textMetrics.tmAscent * metricsMultiplier;
66 float descent = textMetrics.tmDescent * metricsMultiplier;
67 float xHeight = ascent * 0.56f; // Best guess for xHeight for non-Truetype fonts.
68 float lineGap = textMetrics.tmExternalLeading * metricsMultiplier;
69 m_fontMetrics.setAscent(ascent);
70 m_fontMetrics.setDescent(descent);
71 m_fontMetrics.setLineGap(lineGap);
72 m_fontMetrics.setLineSpacing(lroundf(ascent) + lroundf(descent) + lroundf(lineGap));
73 m_avgCharWidth = textMetrics.tmAveCharWidth * metricsMultiplier;
74 m_maxCharWidth = textMetrics.tmMaxCharWidth * metricsMultiplier;
76 OUTLINETEXTMETRIC metrics;
77 if (GetOutlineTextMetrics(hdc, sizeof(metrics), &metrics) > 0) {
78 // This is a TrueType font. We might be able to get an accurate xHeight
80 MAT2 mat = { 1, 0, 0, 1 };
81 DWORD len = GetGlyphOutline(hdc, 'x', GGO_METRICS, &gm, 0, 0, &mat);
82 if (len != GDI_ERROR && gm.gmptGlyphOrigin.y > 0)
83 xHeight = gm.gmptGlyphOrigin.y * metricsMultiplier;
86 m_fontMetrics.setXHeight(xHeight);
87 cairo_win32_scaled_font_done_font(scaledFont);
89 m_isSystemFont = false;
91 m_scriptFontProperties = 0;
97 void SimpleFontData::platformCharWidthInit()
99 // charwidths are set in platformInit.
102 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph glyph) const
104 if (m_platformData.useGDI())
105 return boundsForGDIGlyph(glyph);
106 //FIXME: Implement this
110 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
112 if (m_platformData.useGDI())
113 return widthForGDIGlyph(glyph);
118 cairo_scaled_font_t* scaledFont = m_platformData.scaledFont();
119 cairo_win32_scaled_font_select_font(scaledFont, hdc);
122 GetCharWidthI(hdc, glyph, 1, 0, &width);
124 cairo_win32_scaled_font_done_font(scaledFont);
129 const double metricsMultiplier = cairo_win32_scaled_font_get_metrics_factor(scaledFont) * m_platformData.size();
130 return width * metricsMultiplier;