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