• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 Apple Computer, Inc.
3  * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
4  * Copyright (C) 2007, 2008 Alp Toker <alp@atoker.com>
5  * Copyright (C) 2007 Holger Hans Peter Freyther
6  * Copyright (C) 2007 Pioneer Research Center USA, Inc.
7  * Copyright (C) 2009 Igalia S.L.
8  * All rights reserved.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public License
21  * along with this library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25 #include "config.h"
26 #include "FontPlatformData.h"
27 
28 #include "PlatformString.h"
29 #include "FontDescription.h"
30 #include <wtf/text/CString.h>
31 #include <cairo.h>
32 #include <assert.h>
33 
34 #include <pango/pango.h>
35 #include <pango/pangocairo.h>
36 
37 // Use cairo-ft i a recent enough Pango version isn't available
38 #if !PANGO_VERSION_CHECK(1,18,0)
39 #include <cairo-ft.h>
40 #include <pango/pangofc-fontmap.h>
41 #endif
42 
43 namespace WebCore {
44 
45 PangoFontMap* FontPlatformData::m_fontMap = 0;
46 GHashTable* FontPlatformData::m_hashTable = 0;
47 
FontPlatformData(const FontDescription & fontDescription,const AtomicString & familyName)48 FontPlatformData::FontPlatformData(const FontDescription& fontDescription, const AtomicString& familyName)
49     : m_context(0)
50     , m_font(0)
51     , m_size(fontDescription.computedSize())
52     , m_syntheticBold(false)
53     , m_syntheticOblique(false)
54     , m_scaledFont(0)
55 {
56     FontPlatformData::init();
57 
58     CString stored_family = familyName.string().utf8();
59     char const* families[] = {
60       stored_family.data(),
61       NULL
62     };
63 
64     switch (fontDescription.genericFamily()) {
65     case FontDescription::SerifFamily:
66         families[1] = "serif";
67         break;
68     case FontDescription::SansSerifFamily:
69         families[1] = "sans";
70         break;
71     case FontDescription::MonospaceFamily:
72         families[1] = "monospace";
73         break;
74     case FontDescription::NoFamily:
75     case FontDescription::StandardFamily:
76     default:
77         families[1] = "sans";
78         break;
79     }
80 
81     PangoFontDescription* description = pango_font_description_new();
82     pango_font_description_set_absolute_size(description, fontDescription.computedSize() * PANGO_SCALE);
83 
84     // FIXME: Map all FontWeight values to Pango font weights.
85     if (fontDescription.weight() >= FontWeight600)
86         pango_font_description_set_weight(description, PANGO_WEIGHT_BOLD);
87     if (fontDescription.italic())
88         pango_font_description_set_style(description, PANGO_STYLE_ITALIC);
89 
90 #if PANGO_VERSION_CHECK(1,21,5)   // deprecated in 1.21
91     m_context = pango_font_map_create_context(m_fontMap);
92 #else
93     m_context = pango_cairo_font_map_create_context(PANGO_CAIRO_FONT_MAP(m_fontMap));
94 #endif
95     for (unsigned int i = 0; !m_font && i < G_N_ELEMENTS(families); i++) {
96         pango_font_description_set_family(description, families[i]);
97         pango_context_set_font_description(m_context, description);
98         m_font = pango_font_map_load_font(m_fontMap, m_context, description);
99     }
100 
101 #if PANGO_VERSION_CHECK(1,18,0)
102     if (m_font)
103         m_scaledFont = cairo_scaled_font_reference(pango_cairo_font_get_scaled_font(PANGO_CAIRO_FONT(m_font)));
104 #else
105     // This compatibility code for older versions of Pango is not well-tested.
106     if (m_font) {
107         PangoFcFont* fcfont = PANGO_FC_FONT(m_font);
108         cairo_font_face_t* face = cairo_ft_font_face_create_for_pattern(fcfont->font_pattern);
109         double size;
110         if (FcPatternGetDouble(fcfont->font_pattern, FC_PIXEL_SIZE, 0, &size) != FcResultMatch)
111             size = 12.0;
112         cairo_matrix_t fontMatrix;
113         cairo_matrix_init_scale(&fontMatrix, size, size);
114         cairo_font_options_t* fontOptions;
115         if (pango_cairo_context_get_font_options(m_context))
116             fontOptions = cairo_font_options_copy(pango_cairo_context_get_font_options(m_context));
117         else
118             fontOptions = cairo_font_options_create();
119         cairo_matrix_t ctm;
120         cairo_matrix_init_identity(&ctm);
121         m_scaledFont = cairo_scaled_font_create(face, &fontMatrix, &ctm, fontOptions);
122         cairo_font_options_destroy(fontOptions);
123         cairo_font_face_destroy(face);
124     }
125 #endif
126     pango_font_description_free(description);
127 }
128 
FontPlatformData(float size,bool bold,bool italic)129 FontPlatformData::FontPlatformData(float size, bool bold, bool italic)
130     : m_context(0)
131     , m_font(0)
132     , m_size(size)
133     , m_syntheticBold(bold)
134     , m_syntheticOblique(italic)
135     , m_scaledFont(0)
136 {
137 }
138 
FontPlatformData(cairo_font_face_t * fontFace,float size,bool bold,bool italic)139 FontPlatformData::FontPlatformData(cairo_font_face_t* fontFace, float size, bool bold, bool italic)
140     : m_context(0)
141     , m_font(0)
142     , m_size(size)
143     , m_syntheticBold(bold)
144     , m_syntheticOblique(italic)
145     , m_scaledFont(0)
146 {
147     cairo_matrix_t fontMatrix;
148     cairo_matrix_init_scale(&fontMatrix, size, size);
149     cairo_matrix_t ctm;
150     cairo_matrix_init_identity(&ctm);
151     cairo_font_options_t* options = cairo_font_options_create();
152 
153     // We force antialiasing and disable hinting to provide consistent
154     // typographic qualities for custom fonts on all platforms.
155     cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_NONE);
156     cairo_font_options_set_antialias(options, CAIRO_ANTIALIAS_GRAY);
157 
158     m_scaledFont = cairo_scaled_font_create(fontFace, &fontMatrix, &ctm, options);
159     cairo_font_options_destroy(options);
160 }
161 
init()162 bool FontPlatformData::init()
163 {
164     static bool initialized = false;
165     if (initialized)
166         return true;
167     initialized = true;
168 
169     if (!m_fontMap)
170         m_fontMap = pango_cairo_font_map_get_default();
171     if (!m_hashTable) {
172         PangoFontFamily** families = 0;
173         int n_families = 0;
174 
175         m_hashTable = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
176 
177         pango_font_map_list_families(m_fontMap, &families, &n_families);
178 
179         for (int family = 0; family < n_families; family++)
180                 g_hash_table_insert(m_hashTable,
181                                     g_strdup(pango_font_family_get_name(families[family])),
182                                     g_object_ref(families[family]));
183 
184         g_free(families);
185     }
186 
187     return true;
188 }
189 
~FontPlatformData()190 FontPlatformData::~FontPlatformData()
191 {
192     if (m_font && m_font != reinterpret_cast<PangoFont*>(-1)) {
193         g_object_unref(m_font);
194         m_font = 0;
195     }
196 
197     if (m_context) {
198         g_object_unref(m_context);
199         m_context = 0;
200     }
201 
202     if (m_scaledFont) {
203         cairo_scaled_font_destroy(m_scaledFont);
204         m_scaledFont = 0;
205     }
206 }
207 
isFixedPitch()208 bool FontPlatformData::isFixedPitch()
209 {
210     PangoFontDescription* description = pango_font_describe_with_absolute_size(m_font);
211     PangoFontFamily* family = reinterpret_cast<PangoFontFamily*>(g_hash_table_lookup(m_hashTable, pango_font_description_get_family(description)));
212     pango_font_description_free(description);
213     return pango_font_family_is_monospace(family);
214 }
215 
operator =(const FontPlatformData & other)216 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& other)
217 {
218     // Check for self-assignment.
219     if (this == &other)
220         return *this;
221 
222     m_size = other.m_size;
223     m_syntheticBold = other.m_syntheticBold;
224     m_syntheticOblique = other.m_syntheticOblique;
225 
226     if (other.m_scaledFont)
227         cairo_scaled_font_reference(other.m_scaledFont);
228     if (m_scaledFont)
229         cairo_scaled_font_destroy(m_scaledFont);
230     m_scaledFont = other.m_scaledFont;
231 
232     if (other.m_font)
233         g_object_ref(other.m_font);
234     if (m_font)
235         g_object_unref(m_font);
236     m_font = other.m_font;
237 
238     if (other.m_context)
239         g_object_ref(other.m_context);
240     if (m_context)
241         g_object_unref(m_context);
242     m_context = other.m_context;
243 
244     return *this;
245 }
246 
FontPlatformData(const FontPlatformData & other)247 FontPlatformData::FontPlatformData(const FontPlatformData& other)
248     : m_context(0)
249     , m_font(0)
250     , m_scaledFont(0)
251 {
252     *this = other;
253 }
254 
operator ==(const FontPlatformData & other) const255 bool FontPlatformData::operator==(const FontPlatformData& other) const
256 {
257     if (m_font == other.m_font)
258         return true;
259     if (m_font == 0 || m_font == reinterpret_cast<PangoFont*>(-1)
260         || other.m_font == 0 || other.m_font == reinterpret_cast<PangoFont*>(-1))
261         return false;
262     PangoFontDescription* thisDesc = pango_font_describe(m_font);
263     PangoFontDescription* otherDesc = pango_font_describe(other.m_font);
264     bool result = pango_font_description_equal(thisDesc, otherDesc);
265     pango_font_description_free(otherDesc);
266     pango_font_description_free(thisDesc);
267     return result;
268 }
269 
270 #ifndef NDEBUG
description() const271 String FontPlatformData::description() const
272 {
273     return String();
274 }
275 #endif
276 
277 }
278