Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / harfbuzz / FontPlatformDataHarfBuzz.cpp
1 /*
2  * Copyright (c) 2006, 2007, 2008, Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "platform/fonts/harfbuzz/FontPlatformDataHarfBuzz.h"
33
34 #include "SkTypeface.h"
35 #include "platform/fonts/harfbuzz/HarfBuzzFace.h"
36 #include "wtf/text/WTFString.h"
37
38 namespace blink {
39
40 FontPlatformData::FontPlatformData(WTF::HashTableDeletedValueType)
41     : m_textSize(0)
42     , m_syntheticBold(false)
43     , m_syntheticItalic(false)
44     , m_orientation(Horizontal)
45     , m_isHashTableDeletedValue(true)
46 #if OS(WIN)
47     , m_paintTextFlags(0)
48     , m_minSizeForAntiAlias(0)
49     , m_minSizeForSubpixel(0)
50     , m_useSubpixelPositioning(false)
51 #endif
52 {
53 }
54
55 FontPlatformData::FontPlatformData()
56     : m_textSize(0)
57     , m_syntheticBold(false)
58     , m_syntheticItalic(false)
59     , m_orientation(Horizontal)
60     , m_isHashTableDeletedValue(false)
61 #if OS(WIN)
62     , m_paintTextFlags(0)
63     , m_minSizeForAntiAlias(0)
64     , m_minSizeForSubpixel(0)
65     , m_useSubpixelPositioning(false)
66 #endif
67 {
68 }
69
70 FontPlatformData::FontPlatformData(float textSize, bool syntheticBold, bool syntheticItalic)
71     : m_textSize(textSize)
72     , m_syntheticBold(syntheticBold)
73     , m_syntheticItalic(syntheticItalic)
74     , m_orientation(Horizontal)
75     , m_isHashTableDeletedValue(false)
76 #if OS(WIN)
77     , m_paintTextFlags(0)
78     , m_minSizeForAntiAlias(0)
79     , m_minSizeForSubpixel(0)
80     , m_useSubpixelPositioning(false)
81 #endif
82 {
83 }
84
85 FontPlatformData::FontPlatformData(const FontPlatformData& src)
86     : m_typeface(src.m_typeface)
87 #if !OS(WIN)
88     , m_family(src.m_family)
89 #endif
90     , m_textSize(src.m_textSize)
91     , m_syntheticBold(src.m_syntheticBold)
92     , m_syntheticItalic(src.m_syntheticItalic)
93     , m_orientation(src.m_orientation)
94     , m_style(src.m_style)
95     , m_harfBuzzFace(nullptr)
96     , m_isHashTableDeletedValue(false)
97 #if OS(WIN)
98     , m_paintTextFlags(src.m_paintTextFlags)
99     , m_minSizeForAntiAlias(src.m_minSizeForAntiAlias)
100     , m_minSizeForSubpixel(src.m_minSizeForSubpixel)
101     , m_useSubpixelPositioning(src.m_useSubpixelPositioning)
102 #endif
103 {
104 }
105
106 FontPlatformData::FontPlatformData(PassRefPtr<SkTypeface> tf, const char* family, float textSize, bool syntheticBold, bool syntheticItalic, FontOrientation orientation, bool subpixelTextPosition)
107     : m_typeface(tf)
108 #if !OS(WIN)
109     , m_family(family)
110 #endif
111     , m_textSize(textSize)
112     , m_syntheticBold(syntheticBold)
113     , m_syntheticItalic(syntheticItalic)
114     , m_orientation(orientation)
115     , m_isHashTableDeletedValue(false)
116 #if OS(WIN)
117     , m_paintTextFlags(0)
118     , m_minSizeForAntiAlias(0)
119     , m_minSizeForSubpixel(0)
120     , m_useSubpixelPositioning(subpixelTextPosition)
121 #endif
122 {
123     querySystemForRenderStyle(subpixelTextPosition);
124 }
125
126 FontPlatformData::FontPlatformData(const FontPlatformData& src, float textSize)
127     : m_typeface(src.m_typeface)
128 #if !OS(WIN)
129     , m_family(src.m_family)
130 #endif
131     , m_textSize(textSize)
132     , m_syntheticBold(src.m_syntheticBold)
133     , m_syntheticItalic(src.m_syntheticItalic)
134     , m_orientation(src.m_orientation)
135     , m_harfBuzzFace(nullptr)
136     , m_isHashTableDeletedValue(false)
137 #if OS(WIN)
138     , m_paintTextFlags(src.m_paintTextFlags)
139     , m_minSizeForAntiAlias(src.m_minSizeForAntiAlias)
140     , m_minSizeForSubpixel(src.m_minSizeForSubpixel)
141     , m_useSubpixelPositioning(src.m_useSubpixelPositioning)
142 #endif
143 {
144     querySystemForRenderStyle(FontDescription::subpixelPositioning());
145 }
146
147 FontPlatformData::~FontPlatformData()
148 {
149 }
150
151 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& src)
152 {
153     m_typeface = src.m_typeface;
154 #if !OS(WIN)
155     m_family = src.m_family;
156 #endif
157     m_textSize = src.m_textSize;
158     m_syntheticBold = src.m_syntheticBold;
159     m_syntheticItalic = src.m_syntheticItalic;
160     m_harfBuzzFace = nullptr;
161     m_orientation = src.m_orientation;
162     m_style = src.m_style;
163 #if OS(WIN)
164     m_paintTextFlags = 0;
165     m_minSizeForAntiAlias = src.m_minSizeForAntiAlias;
166     m_minSizeForSubpixel = src.m_minSizeForSubpixel;
167     m_useSubpixelPositioning = src.m_useSubpixelPositioning;
168 #endif
169
170     return *this;
171 }
172
173 #ifndef NDEBUG
174 String FontPlatformData::description() const
175 {
176     return String();
177 }
178 #endif
179
180 SkFontID FontPlatformData::uniqueID() const
181 {
182     return m_typeface->uniqueID();
183 }
184
185 String FontPlatformData::fontFamilyName() const
186 {
187     // FIXME(crbug.com/326582): come up with a proper way of handling SVG.
188     if (!this->typeface())
189         return "";
190     SkTypeface::LocalizedStrings* fontFamilyIterator = this->typeface()->createFamilyNameIterator();
191     SkTypeface::LocalizedString localizedString;
192     while (fontFamilyIterator->next(&localizedString) && !localizedString.fString.size()) { }
193     fontFamilyIterator->unref();
194     return String(localizedString.fString.c_str());
195 }
196
197 bool FontPlatformData::operator==(const FontPlatformData& a) const
198 {
199     // If either of the typeface pointers are null then we test for pointer
200     // equality. Otherwise, we call SkTypeface::Equal on the valid pointers.
201     bool typefacesEqual;
202     if (!m_typeface || !a.m_typeface)
203         typefacesEqual = m_typeface == a.m_typeface;
204     else
205         typefacesEqual = SkTypeface::Equal(m_typeface.get(), a.m_typeface.get());
206
207     return typefacesEqual
208         && m_textSize == a.m_textSize
209         && m_syntheticBold == a.m_syntheticBold
210         && m_syntheticItalic == a.m_syntheticItalic
211         && m_orientation == a.m_orientation
212         && m_style == a.m_style
213         && m_isHashTableDeletedValue == a.m_isHashTableDeletedValue;
214 }
215
216 bool FontPlatformData::isFixedPitch() const
217 {
218     return typeface() && typeface()->isFixedPitch();
219 }
220
221 HarfBuzzFace* FontPlatformData::harfBuzzFace() const
222 {
223     if (!m_harfBuzzFace)
224         m_harfBuzzFace = HarfBuzzFace::create(const_cast<FontPlatformData*>(this), uniqueID());
225
226     return m_harfBuzzFace.get();
227 }
228
229 } // namespace blink