2 * Copyright (c) 2006, 2007, 2008, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "FontPlatformData.h"
34 #include "HarfbuzzSkia.h"
35 #include "NotImplemented.h"
36 #include "PlatformBridge.h"
37 #include "PlatformString.h"
39 #include "SkAdvancedTypefaceMetrics.h"
41 #include "SkTypeface.h"
43 #include <wtf/text/StringImpl.h>
47 static SkPaint::Hinting skiaHinting = SkPaint::kNormal_Hinting;
48 static bool isSkiaAntiAlias = true;
49 static bool isSkiaSubpixelGlyphs = false;
51 void FontPlatformData::setHinting(SkPaint::Hinting hinting)
53 skiaHinting = hinting;
56 void FontPlatformData::setAntiAlias(bool isAntiAlias)
58 isSkiaAntiAlias = isAntiAlias;
61 void FontPlatformData::setSubpixelGlyphs(bool isSubpixelGlyphs)
63 isSkiaSubpixelGlyphs = isSubpixelGlyphs;
66 FontPlatformData::RefCountedHarfbuzzFace::~RefCountedHarfbuzzFace()
68 HB_FreeFace(m_harfbuzzFace);
71 FontPlatformData::FontPlatformData(const FontPlatformData& src)
72 : m_typeface(src.m_typeface)
73 , m_family(src.m_family)
74 , m_textSize(src.m_textSize)
75 , m_emSizeInFontUnits(src.m_emSizeInFontUnits)
76 , m_fakeBold(src.m_fakeBold)
77 , m_fakeItalic(src.m_fakeItalic)
78 , m_orientation(src.m_orientation)
79 , m_textOrientation(src.m_textOrientation)
80 , m_style(src.m_style)
81 , m_harfbuzzFace(src.m_harfbuzzFace)
83 SkSafeRef(m_typeface);
86 FontPlatformData::FontPlatformData(SkTypeface* tf, const char* family, float textSize, bool fakeBold, bool fakeItalic, FontOrientation orientation, TextOrientation textOrientation)
89 , m_textSize(textSize)
90 , m_emSizeInFontUnits(0)
91 , m_fakeBold(fakeBold)
92 , m_fakeItalic(fakeItalic)
93 , m_orientation(orientation)
94 , m_textOrientation(textOrientation)
96 SkSafeRef(m_typeface);
97 querySystemForRenderStyle();
100 FontPlatformData::FontPlatformData(const FontPlatformData& src, float textSize)
101 : m_typeface(src.m_typeface)
102 , m_family(src.m_family)
103 , m_textSize(textSize)
104 , m_emSizeInFontUnits(src.m_emSizeInFontUnits)
105 , m_fakeBold(src.m_fakeBold)
106 , m_fakeItalic(src.m_fakeItalic)
107 , m_orientation(src.m_orientation)
108 , m_textOrientation(src.m_textOrientation)
109 , m_harfbuzzFace(src.m_harfbuzzFace)
111 SkSafeRef(m_typeface);
112 querySystemForRenderStyle();
115 FontPlatformData::~FontPlatformData()
117 SkSafeUnref(m_typeface);
120 int FontPlatformData::emSizeInFontUnits() const
122 if (m_emSizeInFontUnits)
123 return m_emSizeInFontUnits;
125 SkAdvancedTypefaceMetrics* metrics = m_typeface->getAdvancedTypefaceMetrics(SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo);
126 m_emSizeInFontUnits = metrics->fEmSize;
128 return m_emSizeInFontUnits;
131 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& src)
133 SkRefCnt_SafeAssign(m_typeface, src.m_typeface);
135 m_family = src.m_family;
136 m_textSize = src.m_textSize;
137 m_fakeBold = src.m_fakeBold;
138 m_fakeItalic = src.m_fakeItalic;
139 m_harfbuzzFace = src.m_harfbuzzFace;
140 m_orientation = src.m_orientation;
141 m_textOrientation = src.m_textOrientation;
142 m_style = src.m_style;
143 m_emSizeInFontUnits = src.m_emSizeInFontUnits;
149 String FontPlatformData::description() const
155 void FontPlatformData::setupPaint(SkPaint* paint) const
157 const float ts = m_textSize >= 0 ? m_textSize : 12;
159 paint->setAntiAlias(m_style.useAntiAlias == FontRenderStyle::NoPreference ? isSkiaAntiAlias : m_style.useAntiAlias);
160 switch (m_style.useHinting) {
161 case FontRenderStyle::NoPreference:
162 paint->setHinting(skiaHinting);
165 paint->setHinting(SkPaint::kNo_Hinting);
168 paint->setHinting(static_cast<SkPaint::Hinting>(m_style.hintStyle));
172 paint->setEmbeddedBitmapText(m_style.useBitmaps);
173 paint->setTextSize(SkFloatToScalar(ts));
174 paint->setTypeface(m_typeface);
175 paint->setFakeBoldText(m_fakeBold);
176 paint->setTextSkewX(m_fakeItalic ? -SK_Scalar1 / 4 : 0);
177 paint->setAutohinted(m_style.useAutoHint);
179 if (m_style.useAntiAlias == 1 || (m_style.useAntiAlias == FontRenderStyle::NoPreference && isSkiaAntiAlias))
180 paint->setLCDRenderText(m_style.useSubpixel == FontRenderStyle::NoPreference ? isSkiaSubpixelGlyphs : m_style.useSubpixel);
183 SkFontID FontPlatformData::uniqueID() const
185 return m_typeface->uniqueID();
188 bool FontPlatformData::operator==(const FontPlatformData& a) const
190 // If either of the typeface pointers are invalid (either NULL or the
191 // special deleted value) then we test for pointer equality. Otherwise, we
192 // call SkTypeface::Equal on the valid pointers.
194 if (m_typeface == hashTableDeletedFontValue()
195 || a.m_typeface == hashTableDeletedFontValue()
198 typefacesEqual = m_typeface == a.m_typeface;
200 typefacesEqual = SkTypeface::Equal(m_typeface, a.m_typeface);
202 return typefacesEqual
203 && m_textSize == a.m_textSize
204 && m_fakeBold == a.m_fakeBold
205 && m_fakeItalic == a.m_fakeItalic
206 && m_orientation == a.m_orientation
207 && m_textOrientation == a.m_textOrientation
208 && m_style == a.m_style;
211 unsigned FontPlatformData::hash() const
213 unsigned h = SkTypeface::UniqueID(m_typeface);
214 h ^= 0x01010101 * ((static_cast<int>(m_textOrientation) << 3) | (static_cast<int>(m_orientation) << 2) | (static_cast<int>(m_fakeBold) << 1) | static_cast<int>(m_fakeItalic));
216 // This memcpy is to avoid a reinterpret_cast that breaks strict-aliasing
217 // rules. Memcpy is generally optimized enough so that performance doesn't
219 uint32_t textSizeBytes;
220 memcpy(&textSizeBytes, &m_textSize, sizeof(uint32_t));
226 bool FontPlatformData::isFixedPitch() const
232 HB_FaceRec_* FontPlatformData::harfbuzzFace() const
235 m_harfbuzzFace = RefCountedHarfbuzzFace::create(HB_NewFace(const_cast<FontPlatformData*>(this), harfbuzzSkiaGetTable));
237 return m_harfbuzzFace->face();
240 void FontPlatformData::querySystemForRenderStyle()
242 if (!m_family.length()) {
243 // We don't have a family for this. Probably because it's a webfont. We
244 // set all the values to 'no preference' and take the defaults passed
245 // in from XSETTINGS.
246 m_style.useBitmaps = FontRenderStyle::NoPreference;
247 m_style.useAutoHint = FontRenderStyle::NoPreference;
248 m_style.useHinting = FontRenderStyle::NoPreference;
249 m_style.useAntiAlias = FontRenderStyle::NoPreference;
250 m_style.useSubpixel = FontRenderStyle::NoPreference;
254 PlatformBridge::getRenderStyleForStrike(m_family.data(), (((int)m_textSize) << 2) | (m_typeface->style() & 3), &m_style);
257 } // namespace WebCore