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_style(src.m_style)
80 , m_harfbuzzFace(src.m_harfbuzzFace)
82 SkSafeRef(m_typeface);
85 FontPlatformData::FontPlatformData(SkTypeface* tf, const char* family, float textSize, bool fakeBold, bool fakeItalic, FontOrientation orientation)
88 , m_textSize(textSize)
89 , m_emSizeInFontUnits(0)
90 , m_fakeBold(fakeBold)
91 , m_fakeItalic(fakeItalic)
92 , m_orientation(orientation)
94 SkSafeRef(m_typeface);
95 querySystemForRenderStyle();
98 FontPlatformData::FontPlatformData(const FontPlatformData& src, float textSize)
99 : m_typeface(src.m_typeface)
100 , m_family(src.m_family)
101 , m_textSize(textSize)
102 , m_emSizeInFontUnits(src.m_emSizeInFontUnits)
103 , m_fakeBold(src.m_fakeBold)
104 , m_fakeItalic(src.m_fakeItalic)
105 , m_harfbuzzFace(src.m_harfbuzzFace)
107 SkSafeRef(m_typeface);
108 querySystemForRenderStyle();
111 FontPlatformData::~FontPlatformData()
113 SkSafeUnref(m_typeface);
116 int FontPlatformData::emSizeInFontUnits() const
118 if (m_emSizeInFontUnits)
119 return m_emSizeInFontUnits;
121 SkAdvancedTypefaceMetrics* metrics = m_typeface->getAdvancedTypefaceMetrics(false);
122 m_emSizeInFontUnits = metrics->fEmSize;
124 return m_emSizeInFontUnits;
127 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& src)
129 SkRefCnt_SafeAssign(m_typeface, src.m_typeface);
131 m_family = src.m_family;
132 m_textSize = src.m_textSize;
133 m_fakeBold = src.m_fakeBold;
134 m_fakeItalic = src.m_fakeItalic;
135 m_harfbuzzFace = src.m_harfbuzzFace;
136 m_orientation = src.m_orientation;
137 m_style = src.m_style;
138 m_emSizeInFontUnits = src.m_emSizeInFontUnits;
144 String FontPlatformData::description() const
150 void FontPlatformData::setupPaint(SkPaint* paint) const
152 const float ts = m_textSize >= 0 ? m_textSize : 12;
154 paint->setAntiAlias(m_style.useAntiAlias == FontRenderStyle::NoPreference ? isSkiaAntiAlias : m_style.useAntiAlias);
155 switch (m_style.useHinting) {
156 case FontRenderStyle::NoPreference:
157 paint->setHinting(skiaHinting);
160 paint->setHinting(SkPaint::kNo_Hinting);
163 paint->setHinting(static_cast<SkPaint::Hinting>(m_style.hintStyle));
167 paint->setEmbeddedBitmapText(m_style.useBitmaps);
168 paint->setTextSize(SkFloatToScalar(ts));
169 paint->setTypeface(m_typeface);
170 paint->setFakeBoldText(m_fakeBold);
171 paint->setTextSkewX(m_fakeItalic ? -SK_Scalar1 / 4 : 0);
172 paint->setAutohinted(m_style.useAutoHint);
174 if (m_style.useAntiAlias == 1 || (m_style.useAntiAlias == FontRenderStyle::NoPreference && isSkiaAntiAlias))
175 paint->setLCDRenderText(m_style.useSubpixel == FontRenderStyle::NoPreference ? isSkiaSubpixelGlyphs : m_style.useSubpixel);
178 SkFontID FontPlatformData::uniqueID() const
180 return m_typeface->uniqueID();
183 bool FontPlatformData::operator==(const FontPlatformData& a) const
185 // If either of the typeface pointers are invalid (either NULL or the
186 // special deleted value) then we test for pointer equality. Otherwise, we
187 // call SkTypeface::Equal on the valid pointers.
189 if (m_typeface == hashTableDeletedFontValue()
190 || a.m_typeface == hashTableDeletedFontValue()
193 typefacesEqual = m_typeface == a.m_typeface;
195 typefacesEqual = SkTypeface::Equal(m_typeface, a.m_typeface);
197 return typefacesEqual
198 && m_textSize == a.m_textSize
199 && m_fakeBold == a.m_fakeBold
200 && m_fakeItalic == a.m_fakeItalic
201 && m_orientation == a.m_orientation
202 && m_style == a.m_style;
205 unsigned FontPlatformData::hash() const
207 unsigned h = SkTypeface::UniqueID(m_typeface);
208 h ^= 0x01010101 * ((static_cast<int>(m_orientation) << 2) | (static_cast<int>(m_fakeBold) << 1) | static_cast<int>(m_fakeItalic));
210 // This memcpy is to avoid a reinterpret_cast that breaks strict-aliasing
211 // rules. Memcpy is generally optimized enough so that performance doesn't
213 uint32_t textSizeBytes;
214 memcpy(&textSizeBytes, &m_textSize, sizeof(uint32_t));
220 bool FontPlatformData::isFixedPitch() const
226 HB_FaceRec_* FontPlatformData::harfbuzzFace() const
229 m_harfbuzzFace = RefCountedHarfbuzzFace::create(HB_NewFace(const_cast<FontPlatformData*>(this), harfbuzzSkiaGetTable));
231 return m_harfbuzzFace->face();
234 void FontPlatformData::querySystemForRenderStyle()
236 if (!m_family.length()) {
237 // We don't have a family for this. Probably because it's a webfont. We
238 // set all the values to 'no preference' and take the defaults passed
239 // in from XSETTINGS.
240 m_style.useBitmaps = FontRenderStyle::NoPreference;
241 m_style.useAutoHint = FontRenderStyle::NoPreference;
242 m_style.useHinting = FontRenderStyle::NoPreference;
243 m_style.useAntiAlias = FontRenderStyle::NoPreference;
244 m_style.useSubpixel = FontRenderStyle::NoPreference;
248 PlatformBridge::getRenderStyleForStrike(m_family.data(), (((int)m_textSize) << 2) | (m_typeface->style() & 3), &m_style);
251 } // namespace WebCore