1/*
2 * Copyright (C) 2008 Apple Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
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.
16 *
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.
27 */
28
29#include "config.h"
30#include "SimpleFontData.h"
31
32#include <windows.h>
33
34#include "Font.h"
35#include "FontCache.h"
36#include "FontDescription.h"
37#include <cairo.h>
38#include <cairo-win32.h>
39#include <mlang.h>
40#include <wtf/MathExtras.h>
41
42namespace WebCore {
43
44void SimpleFontData::platformInit()
45{
46    m_scriptCache = 0;
47    m_scriptFontProperties = 0;
48    m_isSystemFont = false;
49
50    m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f;
51
52    if (m_platformData.useGDI())
53       return initGDIFont();
54
55    HDC hdc = GetDC(0);
56    SaveDC(hdc);
57
58    cairo_scaled_font_t* scaledFont = m_platformData.scaledFont();
59    const double metricsMultiplier = cairo_win32_scaled_font_get_metrics_factor(scaledFont) * m_platformData.size();
60
61    cairo_win32_scaled_font_select_font(scaledFont, hdc);
62
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;
75
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
79        GLYPHMETRICS gm;
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;
84    }
85
86    m_fontMetrics.setXHeight(xHeight);
87    cairo_win32_scaled_font_done_font(scaledFont);
88
89    m_isSystemFont = false;
90    m_scriptCache = 0;
91    m_scriptFontProperties = 0;
92
93    RestoreDC(hdc, -1);
94    ReleaseDC(0, hdc);
95}
96
97void SimpleFontData::platformCharWidthInit()
98{
99    // charwidths are set in platformInit.
100}
101
102FloatRect SimpleFontData::platformBoundsForGlyph(Glyph glyph) const
103{
104    if (m_platformData.useGDI())
105        return boundsForGDIGlyph(glyph);
106    //FIXME: Implement this
107    return FloatRect();
108}
109
110float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
111{
112    if (m_platformData.useGDI())
113       return widthForGDIGlyph(glyph);
114
115    HDC hdc = GetDC(0);
116    SaveDC(hdc);
117
118    cairo_scaled_font_t* scaledFont = m_platformData.scaledFont();
119    cairo_win32_scaled_font_select_font(scaledFont, hdc);
120
121    int width;
122    GetCharWidthI(hdc, glyph, 1, 0, &width);
123
124    cairo_win32_scaled_font_done_font(scaledFont);
125
126    RestoreDC(hdc, -1);
127    ReleaseDC(0, hdc);
128
129    const double metricsMultiplier = cairo_win32_scaled_font_get_metrics_factor(scaledFont) * m_platformData.size();
130    return width * metricsMultiplier;
131}
132
133}
134