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"
33 #include "wtf/MathExtras.h"
39 const float smallCapsFontSizeMultiplier = 0.7f;
40 const float emphasisMarkFontSizeMultiplier = 0.5f;
42 SimpleFontData::SimpleFontData(const FontPlatformData& platformData, PassRefPtr<CustomFontData> customData, bool isTextOrientationFallback)
45 , m_platformData(platformData)
46 , m_treatAsFixedPitch(false)
47 , m_isTextOrientationFallback(isTextOrientationFallback)
48 , m_isBrokenIdeographFallback(false)
49 #if ENABLE(OPENTYPE_VERTICAL)
50 , m_verticalData(nullptr)
52 , m_hasVerticalGlyphs(false)
53 , m_customFontData(customData)
57 platformCharWidthInit();
58 #if ENABLE(OPENTYPE_VERTICAL)
59 if (platformData.orientation() == Vertical && !isTextOrientationFallback) {
60 m_verticalData = platformData.verticalData();
61 m_hasVerticalGlyphs = m_verticalData.get() && m_verticalData->hasVerticalMetrics();
66 SimpleFontData::SimpleFontData(PassRefPtr<CustomFontData> customData, float fontSize, bool syntheticBold, bool syntheticItalic)
67 : m_platformData(FontPlatformData(fontSize, syntheticBold, syntheticItalic))
68 , m_treatAsFixedPitch(false)
69 , m_isTextOrientationFallback(false)
70 , m_isBrokenIdeographFallback(false)
71 #if ENABLE(OPENTYPE_VERTICAL)
72 , m_verticalData(nullptr)
74 , m_hasVerticalGlyphs(false)
75 , m_customFontData(customData)
78 m_customFontData->initializeFontData(this, fontSize);
81 // Estimates of avgCharWidth and maxCharWidth for platforms that don't support accessing these values from the font.
82 void SimpleFontData::initCharWidths()
84 GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
86 // Treat the width of a '0' as the avgCharWidth.
87 if (m_avgCharWidth <= 0.f && glyphPageZero) {
88 static const UChar32 digitZeroChar = '0';
89 Glyph digitZeroGlyph = glyphPageZero->glyphForCharacter(digitZeroChar);
91 m_avgCharWidth = widthForGlyph(digitZeroGlyph);
94 // If we can't retrieve the width of a '0', fall back to the x height.
95 if (m_avgCharWidth <= 0.f)
96 m_avgCharWidth = m_fontMetrics.xHeight();
98 if (m_maxCharWidth <= 0.f)
99 m_maxCharWidth = max(m_avgCharWidth, m_fontMetrics.floatAscent());
102 void SimpleFontData::platformGlyphInit()
104 GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
105 if (!glyphPageZero) {
106 WTF_LOG_ERROR("Failed to get glyph page zero.");
110 m_adjustedSpaceWidth = 0;
112 m_zeroWidthSpaceGlyph = 0;
113 m_missingGlyphData.fontData = this;
114 m_missingGlyphData.glyph = 0;
118 m_zeroWidthSpaceGlyph = glyphPageZero->glyphForCharacter(0);
120 // Nasty hack to determine if we should round or ceil space widths.
121 // If the font is monospace or fake monospace we ceil to ensure that
122 // every character and the space are the same width. Otherwise we round.
123 m_spaceGlyph = glyphPageZero->glyphForCharacter(' ');
124 float width = widthForGlyph(m_spaceGlyph);
125 m_spaceWidth = width;
126 m_zeroGlyph = glyphPageZero->glyphForCharacter('0');
127 m_fontMetrics.setZeroWidth(widthForGlyph(m_zeroGlyph));
129 m_adjustedSpaceWidth = m_treatAsFixedPitch ? ceilf(width) : roundf(width);
131 // Force the glyph for ZERO WIDTH SPACE to have zero width, unless it is shared with SPACE.
132 // Helvetica is an example of a non-zero width ZERO WIDTH SPACE glyph.
133 // See <http://bugs.webkit.org/show_bug.cgi?id=13178>
134 // Ask for the glyph for 0 to avoid paging in ZERO WIDTH SPACE. Control characters, including 0,
135 // are mapped to the ZERO WIDTH SPACE glyph.
136 if (m_zeroWidthSpaceGlyph == m_spaceGlyph) {
137 m_zeroWidthSpaceGlyph = 0;
138 WTF_LOG_ERROR("Font maps SPACE and ZERO WIDTH SPACE to the same glyph. Glyph width will not be overridden.");
141 m_missingGlyphData.fontData = this;
142 m_missingGlyphData.glyph = 0;
145 SimpleFontData::~SimpleFontData()
151 GlyphPageTreeNode::pruneTreeCustomFontData(this);
153 GlyphPageTreeNode::pruneTreeFontData(this);
156 const SimpleFontData* SimpleFontData::fontDataForCharacter(UChar32) const
161 Glyph SimpleFontData::glyphForCharacter(UChar32 character) const
163 GlyphPageTreeNode* node = GlyphPageTreeNode::getRootChild(this, character / GlyphPage::size);
164 return node->page() ? node->page()->glyphAt(character % GlyphPage::size) : 0;
167 bool SimpleFontData::isSegmented() const
172 PassRefPtr<SimpleFontData> SimpleFontData::verticalRightOrientationFontData() const
174 if (!m_derivedFontData)
175 m_derivedFontData = DerivedFontData::create(isCustomFont());
176 if (!m_derivedFontData->verticalRightOrientation) {
177 FontPlatformData verticalRightPlatformData(m_platformData);
178 verticalRightPlatformData.setOrientation(Horizontal);
179 m_derivedFontData->verticalRightOrientation = create(verticalRightPlatformData, isCustomFont() ? CustomFontData::create(): nullptr, true);
181 return m_derivedFontData->verticalRightOrientation;
184 PassRefPtr<SimpleFontData> SimpleFontData::uprightOrientationFontData() const
186 if (!m_derivedFontData)
187 m_derivedFontData = DerivedFontData::create(isCustomFont());
188 if (!m_derivedFontData->uprightOrientation)
189 m_derivedFontData->uprightOrientation = create(m_platformData, isCustomFont() ? CustomFontData::create(): nullptr, true);
190 return m_derivedFontData->uprightOrientation;
193 PassRefPtr<SimpleFontData> SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
195 if (!m_derivedFontData)
196 m_derivedFontData = DerivedFontData::create(isCustomFont());
197 if (!m_derivedFontData->smallCaps)
198 m_derivedFontData->smallCaps = createScaledFontData(fontDescription, smallCapsFontSizeMultiplier);
200 return m_derivedFontData->smallCaps;
203 PassRefPtr<SimpleFontData> SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const
205 if (!m_derivedFontData)
206 m_derivedFontData = DerivedFontData::create(isCustomFont());
207 if (!m_derivedFontData->emphasisMark)
208 m_derivedFontData->emphasisMark = createScaledFontData(fontDescription, emphasisMarkFontSizeMultiplier);
210 return m_derivedFontData->emphasisMark;
213 PassRefPtr<SimpleFontData> SimpleFontData::brokenIdeographFontData() const
215 if (!m_derivedFontData)
216 m_derivedFontData = DerivedFontData::create(isCustomFont());
217 if (!m_derivedFontData->brokenIdeograph) {
218 m_derivedFontData->brokenIdeograph = create(m_platformData, isCustomFont() ? CustomFontData::create(): nullptr);
219 m_derivedFontData->brokenIdeograph->m_isBrokenIdeographFallback = true;
221 return m_derivedFontData->brokenIdeograph;
225 String SimpleFontData::description() const
230 return "[custom font]";
232 return platformData().description();
236 PassOwnPtr<SimpleFontData::DerivedFontData> SimpleFontData::DerivedFontData::create(bool forCustomFont)
238 return adoptPtr(new DerivedFontData(forCustomFont));
241 SimpleFontData::DerivedFontData::~DerivedFontData()
247 GlyphPageTreeNode::pruneTreeCustomFontData(smallCaps.get());
249 GlyphPageTreeNode::pruneTreeCustomFontData(emphasisMark.get());
251 GlyphPageTreeNode::pruneTreeCustomFontData(brokenIdeograph.get());
252 if (verticalRightOrientation)
253 GlyphPageTreeNode::pruneTreeCustomFontData(verticalRightOrientation.get());
254 if (uprightOrientation)
255 GlyphPageTreeNode::pruneTreeCustomFontData(uprightOrientation.get());
258 PassRefPtr<SimpleFontData> SimpleFontData::createScaledFontData(const FontDescription& fontDescription, float scaleFactor) const
260 // FIXME: Support scaled SVG fonts. Given that SVG is scalable in general this should be achievable.
264 return platformCreateScaledFontData(fontDescription, scaleFactor);
267 } // namespace WebCore