OSDN Git Service

am 359de72a: am 237faca5: Cherry-pick security fix in WebKit change 63773
[android-x86/external-webkit.git] / WebCore / platform / graphics / win / FontPlatformDataCairoWin.cpp
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 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 "PlatformString.h"
30 #include <wtf/HashMap.h>
31 #include <wtf/RetainPtr.h>
32 #include <wtf/Vector.h>
33 #include <wtf/text/StringHash.h>
34
35 #include <cairo-win32.h>
36
37 using namespace std;
38
39 namespace WebCore {
40
41 void FontPlatformData::platformDataInit(HFONT font, float size, HDC hdc, WCHAR* faceName)
42 {
43     m_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(m_fontFace, &sizeMatrix, &ctm, fontOptions);
56 }
57
58 FontPlatformData::FontPlatformData(cairo_font_face_t* fontFace, float size, bool bold, bool oblique)
59     : m_font(0)
60     , m_size(size)
61     , m_fontFace(fontFace)
62     , m_scaledFont(0)
63     , m_syntheticBold(bold)
64     , m_syntheticOblique(oblique)
65     , m_useGDI(false)
66 {
67    cairo_matrix_t fontMatrix;
68    cairo_matrix_init_scale(&fontMatrix, size, size);
69    cairo_matrix_t ctm;
70    cairo_matrix_init_identity(&ctm);
71    cairo_font_options_t* options = cairo_font_options_create();
72
73    // We force antialiasing and disable hinting to provide consistent
74    // typographic qualities for custom fonts on all platforms.
75    cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_NONE);
76    cairo_font_options_set_antialias(options, CAIRO_ANTIALIAS_GRAY);
77
78    m_scaledFont = cairo_scaled_font_create(fontFace, &fontMatrix, &ctm, options);
79    cairo_font_options_destroy(options);
80 }
81
82 FontPlatformData::FontPlatformData(const FontPlatformData& source)
83     : m_font(source.m_font)
84     , m_size(source.m_size)
85     , m_fontFace(0)
86     , m_scaledFont(0)
87     , m_syntheticBold(source.m_syntheticBold)
88     , m_syntheticOblique(source.m_syntheticOblique)
89     , m_useGDI(source.m_useGDI)
90 {
91     if (source.m_fontFace)
92         m_fontFace = cairo_font_face_reference(source.m_fontFace);
93
94     if (source.m_scaledFont)
95         m_scaledFont = cairo_scaled_font_reference(source.m_scaledFont);
96 }
97
98
99 FontPlatformData::~FontPlatformData()
100 {
101     cairo_scaled_font_destroy(m_scaledFont);
102     cairo_font_face_destroy(m_fontFace);
103 }
104
105 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& other)
106 {
107     // Check for self-assignment.
108     if (this == &other)
109         return *this;
110
111     m_font = other.m_font;
112     m_size = other.m_size;
113     m_syntheticBold = other.m_syntheticBold;
114     m_syntheticOblique = other.m_syntheticOblique;
115     m_useGDI = other.m_useGDI;
116
117     if (other.m_fontFace)
118         cairo_font_face_reference(other.m_fontFace);
119     if (m_fontFace)
120         cairo_font_face_destroy(m_fontFace);
121     m_fontFace = other.m_fontFace;
122
123     if (other.m_scaledFont)
124         cairo_scaled_font_reference(other.m_scaledFont);
125     if (m_scaledFont)
126         cairo_scaled_font_destroy(m_scaledFont);
127     m_scaledFont = other.m_scaledFont;
128
129     return *this;
130 }
131
132 bool FontPlatformData::operator==(const FontPlatformData& other) const
133
134     return m_font == other.m_font
135         && m_fontFace == other.m_fontFace
136         && m_scaledFont == other.m_scaledFont
137         && m_size == other.m_size
138         && m_syntheticBold == other.m_syntheticBold
139         && m_syntheticOblique == other.m_syntheticOblique
140         && m_useGDI == other.m_useGDI;
141 }
142
143 }