1/*
2 * This file is part of the internal font implementation.  It should not be included by anyone other than
3 * FontMac.cpp, FontWin.cpp and Font.cpp.
4 *
5 * Copyright (C) 2006, 2007, 2008 Apple Inc.
6 * Copyright (C) 2007 Alp Toker
7 * Copyright (C) 2008, 2010, 2011 Brent Fulgham
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB.  If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "config.h"
27#include "FontPlatformData.h"
28
29#include <wtf/HashMap.h>
30#include <wtf/RetainPtr.h>
31#include <wtf/Vector.h>
32#include <wtf/text/StringHash.h>
33#include <wtf/text/WTFString.h>
34
35#include <cairo-win32.h>
36
37using namespace std;
38
39namespace WebCore {
40
41void FontPlatformData::platformDataInit(HFONT font, float size, HDC hdc, WCHAR* faceName)
42{
43    cairo_font_face_t* fontFace = cairo_win32_font_face_create_for_hfont(font);
44
45    cairo_matrix_t sizeMatrix, ctm;
46    cairo_matrix_init_identity(&ctm);
47    cairo_matrix_init_scale(&sizeMatrix, size, size);
48
49    static cairo_font_options_t* fontOptions = 0;
50    if (!fontOptions) {
51       fontOptions = cairo_font_options_create();
52       cairo_font_options_set_antialias(fontOptions, CAIRO_ANTIALIAS_SUBPIXEL);
53    }
54
55    m_scaledFont = cairo_scaled_font_create(fontFace, &sizeMatrix, &ctm, fontOptions);
56    cairo_font_face_destroy(fontFace);
57}
58
59FontPlatformData::FontPlatformData(GDIObject<HFONT> font, cairo_font_face_t* fontFace, float size, bool bold, bool oblique)
60    : m_font(SharedGDIObject<HFONT>::create(WTF::move(font)))
61    , m_size(size)
62    , m_orientation(Horizontal)
63    , m_widthVariant(RegularWidth)
64    , m_scaledFont(0)
65    , m_isCompositeFontReference(false)
66    , m_isColorBitmapFont(false)
67    , m_syntheticBold(bold)
68    , m_syntheticOblique(oblique)
69    , m_useGDI(false)
70{
71   cairo_matrix_t fontMatrix;
72   cairo_matrix_init_scale(&fontMatrix, size, size);
73   cairo_matrix_t ctm;
74   cairo_matrix_init_identity(&ctm);
75   cairo_font_options_t* options = cairo_font_options_create();
76
77   // We force antialiasing and disable hinting to provide consistent
78   // typographic qualities for custom fonts on all platforms.
79   cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_NONE);
80   cairo_font_options_set_antialias(options, CAIRO_ANTIALIAS_GRAY);
81
82    if (syntheticOblique()) {
83        static const float syntheticObliqueSkew = -tanf(14 * acosf(0) / 90);
84        cairo_matrix_t skew = {1, 0, syntheticObliqueSkew, 1, 0, 0};
85        cairo_matrix_multiply(&fontMatrix, &skew, &fontMatrix);
86    }
87
88   m_scaledFont = cairo_scaled_font_create(fontFace, &fontMatrix, &ctm, options);
89   cairo_font_options_destroy(options);
90}
91
92FontPlatformData::~FontPlatformData()
93{
94    if (m_scaledFont && m_scaledFont != hashTableDeletedFontValue())
95        cairo_scaled_font_destroy(m_scaledFont);
96}
97
98void FontPlatformData::platformDataInit(const FontPlatformData& source)
99{
100    m_font = source.m_font;
101    m_useGDI = source.m_useGDI;
102    m_scaledFont = 0;
103
104    if (source.m_scaledFont)
105        m_scaledFont = cairo_scaled_font_reference(source.m_scaledFont);
106}
107
108const FontPlatformData& FontPlatformData::platformDataAssign(const FontPlatformData& other)
109{
110    m_font = other.m_font;
111    m_useGDI = other.m_useGDI;
112
113    if (m_scaledFont && m_scaledFont != hashTableDeletedFontValue())
114        cairo_scaled_font_destroy(m_scaledFont);
115
116    m_scaledFont = cairo_scaled_font_reference(other.m_scaledFont);
117
118    return *this;
119}
120
121bool FontPlatformData::platformIsEqual(const FontPlatformData& other) const
122{
123    return m_font == other.m_font
124        && m_scaledFont == other.m_scaledFont
125        && m_useGDI == other.m_useGDI;
126}
127
128}
129