2 * Copyright (C) 2005, 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "platform/fonts/SimpleFontData.h"
35 #include "SkTypeface.h"
38 #include "platform/fonts/FontDescription.h"
39 #include "platform/fonts/GlyphPage.h"
40 #include "platform/fonts/VDMXParser.h"
41 #include "platform/geometry/FloatRect.h"
42 #include "wtf/MathExtras.h"
43 #include "wtf/text/CharacterNames.h"
44 #include "wtf/text/Unicode.h"
45 #include <unicode/normlzr.h>
49 const float smallCapsFontSizeMultiplier
= 0.7f
;
50 const float emphasisMarkFontSizeMultiplier
= 0.5f
;
52 #if OS(LINUX) || OS(ANDROID)
53 // This is the largest VDMX table which we'll try to load and parse.
54 static const size_t maxVDMXTableSize
= 1024 * 1024; // 1 MB
57 SimpleFontData::SimpleFontData(const FontPlatformData
& platformData
, PassRefPtr
<CustomFontData
> customData
, bool isTextOrientationFallback
)
60 , m_platformData(platformData
)
61 , m_isTextOrientationFallback(isTextOrientationFallback
)
62 , m_verticalData(nullptr)
63 , m_hasVerticalGlyphs(false)
64 , m_customFontData(customData
)
68 if (platformData
.isVerticalAnyUpright() && !isTextOrientationFallback
) {
69 m_verticalData
= platformData
.verticalData();
70 m_hasVerticalGlyphs
= m_verticalData
.get() && m_verticalData
->hasVerticalMetrics();
74 SimpleFontData::SimpleFontData(PassRefPtr
<CustomFontData
> customData
, float fontSize
, bool syntheticBold
, bool syntheticItalic
)
75 : m_platformData(FontPlatformData(fontSize
, syntheticBold
, syntheticItalic
))
76 , m_isTextOrientationFallback(false)
77 , m_verticalData(nullptr)
78 , m_hasVerticalGlyphs(false)
79 , m_customFontData(customData
)
82 m_customFontData
->initializeFontData(this, fontSize
);
85 void SimpleFontData::platformInit()
87 if (!m_platformData
.size()) {
88 m_fontMetrics
.reset();
95 SkPaint::FontMetrics metrics
;
97 m_platformData
.setupPaint(&paint
);
98 paint
.getFontMetrics(&metrics
);
99 SkTypeface
* face
= paint
.getTypeface();
102 int vdmxAscent
= 0, vdmxDescent
= 0;
103 bool isVDMXValid
= false;
105 #if OS(LINUX) || OS(ANDROID)
106 // Manually digging up VDMX metrics is only applicable when bytecode hinting using FreeType.
107 // With GDI, the metrics will already have taken this into account (as needed).
108 // With DirectWrite or CoreText, no bytecode hinting is ever done.
109 // This code should be pushed into FreeType (hinted font metrics).
110 static const uint32_t vdmxTag
= SkSetFourByteTag('V', 'D', 'M', 'X');
111 int pixelSize
= m_platformData
.size() + 0.5;
112 if (!paint
.isAutohinted()
113 && (paint
.getHinting() == SkPaint::kFull_Hinting
114 || paint
.getHinting() == SkPaint::kNormal_Hinting
))
116 size_t vdmxSize
= face
->getTableSize(vdmxTag
);
117 if (vdmxSize
&& vdmxSize
< maxVDMXTableSize
) {
118 uint8_t* vdmxTable
= (uint8_t*) fastMalloc(vdmxSize
);
120 && face
->getTableData(vdmxTag
, 0, vdmxSize
, vdmxTable
) == vdmxSize
121 && parseVDMX(&vdmxAscent
, &vdmxDescent
, vdmxTable
, vdmxSize
, pixelSize
))
131 // Beware those who step here: This code is designed to match Win32 font
132 // metrics *exactly* (except the adjustment of ascent/descent on Linux/Android).
135 descent
= -vdmxDescent
;
137 ascent
= SkScalarRoundToInt(-metrics
.fAscent
);
138 descent
= SkScalarRoundToInt(metrics
.fDescent
);
139 #if OS(LINUX) || OS(ANDROID)
140 // When subpixel positioning is enabled, if the descent is rounded down, the descent part
141 // of the glyph may be truncated when displayed in a 'overflow: hidden' container.
142 // To avoid that, borrow 1 unit from the ascent when possible.
143 // FIXME: This can be removed if sub-pixel ascent/descent is supported.
144 if (platformData().fontRenderStyle().useSubpixelPositioning
&& descent
< SkScalarToFloat(metrics
.fDescent
) && ascent
>= 1) {
152 // We are preserving this ascent hack to match Safari's ascent adjustment
153 // in their SimpleFontDataMac.mm, for details see crbug.com/445830.
154 // We need to adjust Times, Helvetica, and Courier to closely match the
155 // vertical metrics of their Microsoft counterparts that are the de facto
156 // web standard. The AppKit adjustment of 20% is too big and is
157 // incorrectly added to line spacing, so we use a 15% adjustment instead
158 // and add it to the ascent.
159 DEFINE_STATIC_LOCAL(AtomicString
, timesName
, ("Times", AtomicString::ConstructFromLiteral
));
160 DEFINE_STATIC_LOCAL(AtomicString
, helveticaName
, ("Helvetica", AtomicString::ConstructFromLiteral
));
161 DEFINE_STATIC_LOCAL(AtomicString
, courierName
, ("Courier", AtomicString::ConstructFromLiteral
));
162 String familyName
= m_platformData
.fontFamilyName();
163 if (familyName
== timesName
|| familyName
== helveticaName
|| familyName
== courierName
)
164 ascent
+= floorf(((ascent
+ descent
) * 0.15f
) + 0.5f
);
167 m_fontMetrics
.setAscent(ascent
);
168 m_fontMetrics
.setDescent(descent
);
171 if (metrics
.fXHeight
) {
172 xHeight
= metrics
.fXHeight
;
174 // Mac OS CTFontGetXHeight reports the bounding box height of x,
175 // including parts extending below the baseline and apparently no x-height
176 // value from the OS/2 table. However, the CSS ex unit
177 // expects only parts above the baseline, hence measuring the glyph:
178 // http://www.w3.org/TR/css3-values/#ex-unit
179 GlyphPage
* glyphPageZero
= GlyphPageTreeNode::getRootChild(this, 0)->page();
181 static const UChar32 xChar
= 'x';
182 const Glyph xGlyph
= glyphPageZero
->glyphForCharacter(xChar
);
184 FloatRect
glyphBounds(boundsForGlyph(xGlyph
));
185 // SkGlyph bounds, y down, based on rendering at (0,0).
186 xHeight
= - glyphBounds
.y();
190 m_fontMetrics
.setXHeight(xHeight
);
192 xHeight
= ascent
* 0.56; // Best guess from Windows font metrics.
193 m_fontMetrics
.setXHeight(xHeight
);
194 m_fontMetrics
.setHasXHeight(false);
197 float lineGap
= SkScalarToFloat(metrics
.fLeading
);
198 m_fontMetrics
.setLineGap(lineGap
);
199 m_fontMetrics
.setLineSpacing(lroundf(ascent
) + lroundf(descent
) + lroundf(lineGap
));
201 if (platformData().isVerticalAnyUpright() && !isTextOrientationFallback()) {
202 static const uint32_t vheaTag
= SkSetFourByteTag('v', 'h', 'e', 'a');
203 static const uint32_t vorgTag
= SkSetFourByteTag('V', 'O', 'R', 'G');
204 size_t vheaSize
= face
->getTableSize(vheaTag
);
205 size_t vorgSize
= face
->getTableSize(vorgTag
);
206 if ((vheaSize
> 0) || (vorgSize
> 0))
207 m_hasVerticalGlyphs
= true;
210 // In WebKit/WebCore/platform/graphics/SimpleFontData.cpp, m_spaceWidth is
211 // calculated for us, but we need to calculate m_maxCharWidth and
212 // m_avgCharWidth in order for text entry widgets to be sized correctly.
214 m_maxCharWidth
= SkScalarRoundToInt(metrics
.fMaxCharWidth
);
216 // Older version of the DirectWrite API doesn't implement support for max
217 // char width. Fall back on a multiple of the ascent. This is entirely
218 // arbitrary but comes pretty close to the expected value in most cases.
219 if (m_maxCharWidth
< 1)
220 m_maxCharWidth
= ascent
* 2;
222 // FIXME: The current avg/max character width calculation is not ideal,
223 // it should check either the OS2 table or, better yet, query FontMetrics.
224 // Sadly FontMetrics provides incorrect data on Mac at the moment.
225 // https://crbug.com/420901
226 m_maxCharWidth
= std::max(m_avgCharWidth
, m_fontMetrics
.floatAscent());
228 // Better would be to rely on either fMaxCharWidth or fAveCharWidth.
230 m_maxCharWidth
= SkScalarRoundToInt(metrics
.fXMax
- metrics
.fXMin
);
235 if (metrics
.fAvgCharWidth
) {
236 m_avgCharWidth
= SkScalarRoundToInt(metrics
.fAvgCharWidth
);
239 m_avgCharWidth
= xHeight
;
241 GlyphPage
* glyphPageZero
= GlyphPageTreeNode::getRootChild(this, 0)->page();
244 static const UChar32 xChar
= 'x';
245 const Glyph xGlyph
= glyphPageZero
->glyphForCharacter(xChar
);
248 // In widthForGlyph(), xGlyph will be compared with
249 // m_zeroWidthSpaceGlyph, which isn't initialized yet here.
250 // Initialize it with zero to make sure widthForGlyph() returns
252 m_zeroWidthSpaceGlyph
= 0;
253 m_avgCharWidth
= widthForGlyph(xGlyph
);
260 if (int unitsPerEm
= face
->getUnitsPerEm())
261 m_fontMetrics
.setUnitsPerEm(unitsPerEm
);
264 void SimpleFontData::platformGlyphInit()
266 GlyphPage
* glyphPageZero
= GlyphPageTreeNode::getRootChild(this, 0)->page();
267 if (!glyphPageZero
) {
271 m_zeroWidthSpaceGlyph
= 0;
272 m_missingGlyphData
.fontData
= this;
273 m_missingGlyphData
.glyph
= 0;
277 // Ask for the glyph for 0 to avoid paging in ZERO WIDTH SPACE. Control characters, including 0,
278 // are mapped to the ZERO WIDTH SPACE glyph.
279 m_zeroWidthSpaceGlyph
= glyphPageZero
->glyphForCharacter(0);
281 // Nasty hack to determine if we should round or ceil space widths.
282 // If the font is monospace or fake monospace we ceil to ensure that
283 // every character and the space are the same width. Otherwise we round.
284 m_spaceGlyph
= glyphPageZero
->glyphForCharacter(spaceCharacter
);
285 float width
= widthForGlyph(m_spaceGlyph
);
286 m_spaceWidth
= width
;
287 m_zeroGlyph
= glyphPageZero
->glyphForCharacter('0');
288 m_fontMetrics
.setZeroWidth(widthForGlyph(m_zeroGlyph
));
290 // Force the glyph for ZERO WIDTH SPACE to have zero width, unless it is shared with SPACE.
291 // Helvetica is an example of a non-zero width ZERO WIDTH SPACE glyph.
292 // See <http://bugs.webkit.org/show_bug.cgi?id=13178>
293 if (m_zeroWidthSpaceGlyph
== m_spaceGlyph
) {
294 m_zeroWidthSpaceGlyph
= 0;
295 WTF_LOG_ERROR("Font maps SPACE and ZERO WIDTH SPACE to the same glyph. Glyph width will not be overridden.");
298 m_missingGlyphData
.fontData
= this;
299 m_missingGlyphData
.glyph
= 0;
302 SimpleFontData::~SimpleFontData()
305 GlyphPageTreeNode::pruneTreeCustomFontData(this);
307 GlyphPageTreeNode::pruneTreeFontData(this);
310 const SimpleFontData
* SimpleFontData::fontDataForCharacter(UChar32
) const
315 Glyph
SimpleFontData::glyphForCharacter(UChar32 character
) const
317 // As GlyphPage::size is power of 2 so shifting is valid
318 GlyphPageTreeNode
* node
= GlyphPageTreeNode::getNormalRootChild(this, character
>> GlyphPage::sizeBits
);
319 return node
->page() ? node
->page()->glyphAt(character
& 0xFF) : 0;
322 bool SimpleFontData::isSegmented() const
327 PassRefPtr
<SimpleFontData
> SimpleFontData::verticalRightOrientationFontData() const
329 if (!m_derivedFontData
)
330 m_derivedFontData
= DerivedFontData::create(isCustomFont());
331 if (!m_derivedFontData
->verticalRightOrientation
) {
332 FontPlatformData
verticalRightPlatformData(m_platformData
);
333 verticalRightPlatformData
.setOrientation(FontOrientation::Horizontal
);
334 m_derivedFontData
->verticalRightOrientation
= create(verticalRightPlatformData
, isCustomFont() ? CustomFontData::create(): nullptr, true);
336 return m_derivedFontData
->verticalRightOrientation
;
339 PassRefPtr
<SimpleFontData
> SimpleFontData::uprightOrientationFontData() const
341 if (!m_derivedFontData
)
342 m_derivedFontData
= DerivedFontData::create(isCustomFont());
343 if (!m_derivedFontData
->uprightOrientation
)
344 m_derivedFontData
->uprightOrientation
= create(m_platformData
, isCustomFont() ? CustomFontData::create(): nullptr, true);
345 return m_derivedFontData
->uprightOrientation
;
348 PassRefPtr
<SimpleFontData
> SimpleFontData::smallCapsFontData(const FontDescription
& fontDescription
) const
350 if (!m_derivedFontData
)
351 m_derivedFontData
= DerivedFontData::create(isCustomFont());
352 if (!m_derivedFontData
->smallCaps
)
353 m_derivedFontData
->smallCaps
= createScaledFontData(fontDescription
, smallCapsFontSizeMultiplier
);
355 return m_derivedFontData
->smallCaps
;
358 PassRefPtr
<SimpleFontData
> SimpleFontData::emphasisMarkFontData(const FontDescription
& fontDescription
) const
360 if (!m_derivedFontData
)
361 m_derivedFontData
= DerivedFontData::create(isCustomFont());
362 if (!m_derivedFontData
->emphasisMark
)
363 m_derivedFontData
->emphasisMark
= createScaledFontData(fontDescription
, emphasisMarkFontSizeMultiplier
);
365 return m_derivedFontData
->emphasisMark
;
368 PassOwnPtr
<SimpleFontData::DerivedFontData
> SimpleFontData::DerivedFontData::create(bool forCustomFont
)
370 return adoptPtr(new DerivedFontData(forCustomFont
));
373 SimpleFontData::DerivedFontData::~DerivedFontData()
379 GlyphPageTreeNode::pruneTreeCustomFontData(smallCaps
.get());
381 GlyphPageTreeNode::pruneTreeCustomFontData(emphasisMark
.get());
382 if (verticalRightOrientation
)
383 GlyphPageTreeNode::pruneTreeCustomFontData(verticalRightOrientation
.get());
384 if (uprightOrientation
)
385 GlyphPageTreeNode::pruneTreeCustomFontData(uprightOrientation
.get());
388 PassRefPtr
<SimpleFontData
> SimpleFontData::createScaledFontData(const FontDescription
& fontDescription
, float scaleFactor
) const
390 return platformCreateScaledFontData(fontDescription
, scaleFactor
);
393 PassRefPtr
<SimpleFontData
> SimpleFontData::platformCreateScaledFontData(const FontDescription
& fontDescription
, float scaleFactor
) const
395 const float scaledSize
= lroundf(fontDescription
.computedSize() * scaleFactor
);
396 return SimpleFontData::create(FontPlatformData(m_platformData
, scaledSize
), isCustomFont() ? CustomFontData::create() : nullptr);
399 static inline void getSkiaBoundsForGlyph(SkPaint
& paint
, Glyph glyph
, SkRect
& bounds
)
401 paint
.setTextEncoding(SkPaint::kGlyphID_TextEncoding
);
404 paint
.getTextPath(&glyph
, sizeof(glyph
), 0, 0, &path
);
405 bounds
= path
.getBounds();
407 if (!paint
.isSubpixelText()) {
414 FloatRect
SimpleFontData::platformBoundsForGlyph(Glyph glyph
) const
416 if (!m_platformData
.size())
419 SkASSERT(sizeof(glyph
) == 2); // compile-time assert
422 m_platformData
.setupPaint(&paint
);
425 getSkiaBoundsForGlyph(paint
, glyph
, bounds
);
426 return FloatRect(bounds
);
429 float SimpleFontData::platformWidthForGlyph(Glyph glyph
) const
431 if (!m_platformData
.size())
434 SkASSERT(sizeof(glyph
) == 2); // compile-time assert
438 m_platformData
.setupPaint(&paint
);
440 paint
.setTextEncoding(SkPaint::kGlyphID_TextEncoding
);
441 SkScalar width
= paint
.measureText(&glyph
, 2);
442 if (!paint
.isSubpixelText())
443 width
= SkScalarRoundToInt(width
);
444 return SkScalarToFloat(width
);
447 bool SimpleFontData::canRenderCombiningCharacterSequence(const UChar
* characters
, size_t length
) const
449 if (!m_combiningCharacterSequenceSupport
)
450 m_combiningCharacterSequenceSupport
= adoptPtr(new HashMap
<String
, bool>);
452 WTF::HashMap
<String
, bool>::AddResult addResult
= m_combiningCharacterSequenceSupport
->add(String(characters
, length
), false);
453 if (!addResult
.isNewEntry
)
454 return addResult
.storedValue
->value
;
456 UErrorCode error
= U_ZERO_ERROR
;
457 Vector
<UChar
, 4> normalizedCharacters(length
);
458 size_t normalizedLength
= unorm_normalize(characters
, length
, UNORM_NFC
, UNORM_UNICODE_3_2
, &normalizedCharacters
[0], length
, &error
);
459 // Can't render if we have an error or no composition occurred.
460 if (U_FAILURE(error
) || normalizedLength
== length
)
463 for (size_t offset
= 0; offset
< normalizedLength
;) {
465 U16_NEXT(normalizedCharacters
, offset
, normalizedLength
, character
);
466 if (!glyphForCharacter(character
))
470 addResult
.storedValue
->value
= true;
474 bool SimpleFontData::fillGlyphPage(GlyphPage
* pageToFill
, unsigned offset
, unsigned length
, UChar
* buffer
, unsigned bufferLength
) const
476 if (SkUTF16_IsHighSurrogate(buffer
[bufferLength
-1])) {
477 SkDebugf("%s last char is high-surrogate", __FUNCTION__
);
481 SkTypeface
* typeface
= platformData().typeface();
483 WTF_LOG_ERROR("fillGlyphPage called on an empty Skia typeface.");
487 SkAutoSTMalloc
<GlyphPage::size
, uint16_t> glyphStorage(length
);
488 uint16_t* glyphs
= glyphStorage
.get();
489 typeface
->charsToGlyphs(buffer
, SkTypeface::kUTF16_Encoding
, glyphs
, length
);
491 bool haveGlyphs
= false;
492 for (unsigned i
= 0; i
< length
; i
++) {
494 pageToFill
->setGlyphDataForIndex(offset
+ i
, glyphs
[i
], this);