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 "platform/fonts/harfbuzz/FontPlatformDataHarfBuzz.h"
34 #include "RuntimeEnabledFeatures.h"
35 #include "SkTypeface.h"
36 #include "platform/LayoutTestSupport.h"
37 #include "platform/NotImplemented.h"
38 #include "platform/fonts/FontCache.h"
39 #include "platform/fonts/harfbuzz/HarfBuzzFace.h"
41 #include "public/platform/linux/WebFontInfo.h"
42 #include "public/platform/linux/WebFontRenderStyle.h"
43 #include "public/platform/linux/WebSandboxSupport.h"
44 #include "public/platform/Platform.h"
45 #include "wtf/text/WTFString.h"
49 static SkPaint::Hinting skiaHinting = SkPaint::kNormal_Hinting;
50 static bool useSkiaAutoHint = true;
51 static bool useSkiaBitmaps = true;
52 static bool useSkiaAntiAlias = true;
53 static bool useSkiaSubpixelRendering = false;
55 void FontPlatformData::setHinting(SkPaint::Hinting hinting)
57 skiaHinting = hinting;
60 void FontPlatformData::setAutoHint(bool useAutoHint)
62 useSkiaAutoHint = useAutoHint;
65 void FontPlatformData::setUseBitmaps(bool useBitmaps)
67 useSkiaBitmaps = useBitmaps;
70 void FontPlatformData::setAntiAlias(bool useAntiAlias)
72 useSkiaAntiAlias = useAntiAlias;
75 void FontPlatformData::setSubpixelRendering(bool useSubpixelRendering)
77 useSkiaSubpixelRendering = useSubpixelRendering;
80 FontPlatformData::FontPlatformData(WTF::HashTableDeletedValueType)
82 , m_syntheticBold(false)
83 , m_syntheticItalic(false)
84 , m_orientation(Horizontal)
85 , m_isHashTableDeletedValue(true)
89 FontPlatformData::FontPlatformData()
91 , m_syntheticBold(false)
92 , m_syntheticItalic(false)
93 , m_orientation(Horizontal)
94 , m_isHashTableDeletedValue(false)
98 FontPlatformData::FontPlatformData(float textSize, bool syntheticBold, bool syntheticItalic)
99 : m_textSize(textSize)
100 , m_syntheticBold(syntheticBold)
101 , m_syntheticItalic(syntheticItalic)
102 , m_orientation(Horizontal)
103 , m_isHashTableDeletedValue(false)
107 FontPlatformData::FontPlatformData(const FontPlatformData& src)
108 : m_typeface(src.m_typeface)
109 , m_family(src.m_family)
110 , m_textSize(src.m_textSize)
111 , m_syntheticBold(src.m_syntheticBold)
112 , m_syntheticItalic(src.m_syntheticItalic)
113 , m_orientation(src.m_orientation)
114 , m_style(src.m_style)
115 , m_harfBuzzFace(nullptr)
116 , m_isHashTableDeletedValue(false)
120 FontPlatformData::FontPlatformData(PassRefPtr<SkTypeface> tf, const char* family, float textSize, bool syntheticBold, bool syntheticItalic, FontOrientation orientation, bool subpixelTextPosition)
123 , m_textSize(textSize)
124 , m_syntheticBold(syntheticBold)
125 , m_syntheticItalic(syntheticItalic)
126 , m_orientation(orientation)
127 , m_isHashTableDeletedValue(false)
129 querySystemForRenderStyle(subpixelTextPosition);
132 FontPlatformData::FontPlatformData(const FontPlatformData& src, float textSize)
133 : m_typeface(src.m_typeface)
134 , m_family(src.m_family)
135 , m_textSize(textSize)
136 , m_syntheticBold(src.m_syntheticBold)
137 , m_syntheticItalic(src.m_syntheticItalic)
138 , m_orientation(src.m_orientation)
139 , m_harfBuzzFace(nullptr)
140 , m_isHashTableDeletedValue(false)
142 querySystemForRenderStyle(FontDescription::subpixelPositioning());
145 FontPlatformData::~FontPlatformData()
149 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& src)
151 m_typeface = src.m_typeface;
152 m_family = src.m_family;
153 m_textSize = src.m_textSize;
154 m_syntheticBold = src.m_syntheticBold;
155 m_syntheticItalic = src.m_syntheticItalic;
156 m_harfBuzzFace = nullptr;
157 m_orientation = src.m_orientation;
158 m_style = src.m_style;
164 String FontPlatformData::description() const
170 void FontPlatformData::setupPaint(SkPaint* paint, GraphicsContext*) const
172 paint->setAntiAlias(m_style.useAntiAlias);
173 paint->setHinting(static_cast<SkPaint::Hinting>(m_style.hintStyle));
174 paint->setEmbeddedBitmapText(m_style.useBitmaps);
175 paint->setAutohinted(m_style.useAutoHint);
176 if (m_style.useAntiAlias)
177 paint->setLCDRenderText(m_style.useSubpixelRendering);
179 // TestRunner specifically toggles the subpixel positioning flag.
180 if (RuntimeEnabledFeatures::subpixelFontScalingEnabled() && !isRunningLayoutTest())
181 paint->setSubpixelText(true);
183 paint->setSubpixelText(m_style.useSubpixelPositioning);
185 const float ts = m_textSize >= 0 ? m_textSize : 12;
186 paint->setTextSize(SkFloatToScalar(ts));
187 paint->setTypeface(m_typeface.get());
188 paint->setFakeBoldText(m_syntheticBold);
189 paint->setTextSkewX(m_syntheticItalic ? -SK_Scalar1 / 4 : 0);
192 SkFontID FontPlatformData::uniqueID() const
194 return m_typeface->uniqueID();
197 String FontPlatformData::fontFamilyName() const
199 // FIXME(crbug.com/326582): come up with a proper way of handling SVG.
200 if (!this->typeface())
202 SkTypeface::LocalizedStrings* fontFamilyIterator = this->typeface()->createFamilyNameIterator();
203 SkTypeface::LocalizedString localizedString;
204 while (fontFamilyIterator->next(&localizedString) && !localizedString.fString.size()) { }
205 fontFamilyIterator->unref();
206 return String(localizedString.fString.c_str());
209 bool FontPlatformData::operator==(const FontPlatformData& a) const
211 // If either of the typeface pointers are null then we test for pointer
212 // equality. Otherwise, we call SkTypeface::Equal on the valid pointers.
214 if (!m_typeface || !a.m_typeface)
215 typefacesEqual = m_typeface == a.m_typeface;
217 typefacesEqual = SkTypeface::Equal(m_typeface.get(), a.m_typeface.get());
219 return typefacesEqual
220 && m_textSize == a.m_textSize
221 && m_syntheticBold == a.m_syntheticBold
222 && m_syntheticItalic == a.m_syntheticItalic
223 && m_orientation == a.m_orientation
224 && m_style == a.m_style
225 && m_isHashTableDeletedValue == a.m_isHashTableDeletedValue;
228 bool FontPlatformData::isFixedPitch() const
234 HarfBuzzFace* FontPlatformData::harfBuzzFace() const
237 m_harfBuzzFace = HarfBuzzFace::create(const_cast<FontPlatformData*>(this), uniqueID());
239 return m_harfBuzzFace.get();
242 void FontPlatformData::getRenderStyleForStrike(const char* font, int sizeAndStyle)
244 blink::WebFontRenderStyle style;
250 style.setDefaults(); // It's probably a webfont. Take the system defaults.
251 else if (blink::Platform::current()->sandboxSupport())
252 blink::Platform::current()->sandboxSupport()->getRenderStyleForStrike(font, sizeAndStyle, &style);
254 blink::WebFontInfo::renderStyleForStrike(font, sizeAndStyle, &style);
257 style.toFontRenderStyle(&m_style);
260 void FontPlatformData::querySystemForRenderStyle(bool useSkiaSubpixelPositioning)
262 getRenderStyleForStrike(m_family.data(), (((int)m_textSize) << 2) | (m_typeface->style() & 3));
264 // Fix FontRenderStyle::NoPreference to actual styles.
265 if (m_style.useAntiAlias == FontRenderStyle::NoPreference)
266 m_style.useAntiAlias = useSkiaAntiAlias;
268 if (!m_style.useHinting)
269 m_style.hintStyle = SkPaint::kNo_Hinting;
270 else if (m_style.useHinting == FontRenderStyle::NoPreference)
271 m_style.hintStyle = skiaHinting;
273 if (m_style.useBitmaps == FontRenderStyle::NoPreference)
274 m_style.useBitmaps = useSkiaBitmaps;
275 if (m_style.useAutoHint == FontRenderStyle::NoPreference)
276 m_style.useAutoHint = useSkiaAutoHint;
277 if (m_style.useAntiAlias == FontRenderStyle::NoPreference)
278 m_style.useAntiAlias = useSkiaAntiAlias;
279 if (m_style.useSubpixelRendering == FontRenderStyle::NoPreference)
280 m_style.useSubpixelRendering = useSkiaSubpixelRendering;
282 // TestRunner specifically toggles the subpixel positioning flag.
283 if (m_style.useSubpixelPositioning == FontRenderStyle::NoPreference
284 || isRunningLayoutTest())
285 m_style.useSubpixelPositioning = useSkiaSubpixelPositioning;
288 bool FontPlatformData::defaultUseSubpixelPositioning()
290 return FontDescription::subpixelPositioning();
293 } // namespace WebCore