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 "SimpleFontData.h"
34 #include "FontCache.h"
37 #include "SVGFontData.h"
38 #include "SVGFontElement.h"
39 #include "SVGFontFaceElement.h"
40 #include "SVGGlyphElement.h"
43 #include <wtf/MathExtras.h>
44 #include <wtf/UnusedParam.h>
50 SimpleFontData::SimpleFontData(const FontPlatformData& platformData, bool isCustomFont, bool isLoading)
53 , m_orientation(platformData.orientation())
54 , m_platformData(platformData)
55 , m_treatAsFixedPitch(false)
56 , m_isCustomFont(isCustomFont)
57 , m_isLoading(isLoading)
58 , m_isBrokenIdeographFont(false)
62 platformCharWidthInit();
66 SimpleFontData::SimpleFontData(PassOwnPtr<SVGFontData> svgFontData, int size, bool syntheticBold, bool syntheticItalic)
67 : m_orientation(Horizontal)
68 , m_platformData(FontPlatformData(size, syntheticBold, syntheticItalic))
69 , m_treatAsFixedPitch(false)
70 , m_svgFontData(svgFontData)
71 , m_isCustomFont(true)
73 , m_isBrokenIdeographFont(false)
75 SVGFontFaceElement* svgFontFaceElement = m_svgFontData->svgFontFaceElement();
76 unsigned unitsPerEm = svgFontFaceElement->unitsPerEm();
82 float xHeight = svgFontFaceElement->xHeight() * scale;
83 float ascent = svgFontFaceElement->ascent() * scale;
84 float descent = svgFontFaceElement->descent() * scale;
85 float lineGap = 0.1f * size;
87 SVGFontElement* associatedFontElement = svgFontFaceElement->associatedFontElement();
89 // Fallback if x_heightAttr is not specified for the font element.
90 Vector<SVGGlyphIdentifier> letterXGlyphs;
91 associatedFontElement->getGlyphIdentifiersForString(String("x", 1), letterXGlyphs);
92 xHeight = letterXGlyphs.isEmpty() ? 2 * ascent / 3 : letterXGlyphs.first().horizontalAdvanceX * scale;
95 m_fontMetrics.setUnitsPerEm(unitsPerEm);
96 m_fontMetrics.setAscent(ascent);
97 m_fontMetrics.setDescent(descent);
98 m_fontMetrics.setLineGap(lineGap);
99 m_fontMetrics.setLineSpacing(roundf(ascent) + roundf(descent) + roundf(lineGap));
100 m_fontMetrics.setXHeight(xHeight);
102 Vector<SVGGlyphIdentifier> spaceGlyphs;
103 associatedFontElement->getGlyphIdentifiersForString(String(" ", 1), spaceGlyphs);
104 m_spaceWidth = spaceGlyphs.isEmpty() ? xHeight : spaceGlyphs.first().horizontalAdvanceX * scale;
106 Vector<SVGGlyphIdentifier> numeralZeroGlyphs;
107 associatedFontElement->getGlyphIdentifiersForString(String("0", 1), numeralZeroGlyphs);
108 m_avgCharWidth = numeralZeroGlyphs.isEmpty() ? m_spaceWidth : numeralZeroGlyphs.first().horizontalAdvanceX * scale;
110 Vector<SVGGlyphIdentifier> letterWGlyphs;
111 associatedFontElement->getGlyphIdentifiersForString(String("W", 1), letterWGlyphs);
112 m_maxCharWidth = letterWGlyphs.isEmpty() ? ascent : letterWGlyphs.first().horizontalAdvanceX * scale;
114 // FIXME: is there a way we can get the space glyph from the SVGGlyphIdentifier above?
116 m_zeroWidthSpaceGlyph = 0;
118 m_missingGlyphData.fontData = this;
119 m_missingGlyphData.glyph = 0;
124 // Estimates of avgCharWidth and maxCharWidth for platforms that don't support accessing these values from the font.
125 void SimpleFontData::initCharWidths()
127 GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
129 // Treat the width of a '0' as the avgCharWidth.
130 if (m_avgCharWidth <= 0.f && glyphPageZero) {
131 static const UChar32 digitZeroChar = '0';
132 Glyph digitZeroGlyph = glyphPageZero->glyphDataForCharacter(digitZeroChar).glyph;
134 m_avgCharWidth = widthForGlyph(digitZeroGlyph);
137 // If we can't retrieve the width of a '0', fall back to the x height.
138 if (m_avgCharWidth <= 0.f)
139 m_avgCharWidth = m_fontMetrics.xHeight();
141 if (m_maxCharWidth <= 0.f)
142 m_maxCharWidth = max(m_avgCharWidth, m_fontMetrics.floatAscent());
145 void SimpleFontData::platformGlyphInit()
147 GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
148 if (!glyphPageZero) {
149 LOG_ERROR("Failed to get glyph page zero.");
153 m_zeroWidthSpaceGlyph = 0;
154 m_missingGlyphData.fontData = this;
155 m_missingGlyphData.glyph = 0;
159 m_zeroWidthSpaceGlyph = glyphPageZero->glyphDataForCharacter(0).glyph;
161 // Nasty hack to determine if we should round or ceil space widths.
162 // If the font is monospace or fake monospace we ceil to ensure that
163 // every character and the space are the same width. Otherwise we round.
164 m_spaceGlyph = glyphPageZero->glyphDataForCharacter(' ').glyph;
165 float width = widthForGlyph(m_spaceGlyph);
166 m_spaceWidth = width;
169 // Force the glyph for ZERO WIDTH SPACE to have zero width, unless it is shared with SPACE.
170 // Helvetica is an example of a non-zero width ZERO WIDTH SPACE glyph.
171 // See <http://bugs.webkit.org/show_bug.cgi?id=13178>
172 // Ask for the glyph for 0 to avoid paging in ZERO WIDTH SPACE. Control characters, including 0,
173 // are mapped to the ZERO WIDTH SPACE glyph.
174 if (m_zeroWidthSpaceGlyph == m_spaceGlyph) {
175 m_zeroWidthSpaceGlyph = 0;
176 LOG_ERROR("Font maps SPACE and ZERO WIDTH SPACE to the same glyph. Glyph width will not be overridden.");
179 m_missingGlyphData.fontData = this;
180 m_missingGlyphData.glyph = 0;
184 SimpleFontData::~SimpleFontData()
186 #if ENABLE(SVG_FONTS)
187 if (!m_svgFontData || !m_svgFontData->svgFontFaceElement())
192 GlyphPageTreeNode::pruneTreeFontData(this);
195 const SimpleFontData* SimpleFontData::fontDataForCharacter(UChar32) const
200 bool SimpleFontData::isSegmented() const
205 SimpleFontData* SimpleFontData::brokenIdeographFontData() const
207 if (!m_derivedFontData)
208 m_derivedFontData = DerivedFontData::create(isCustomFont());
209 if (!m_derivedFontData->brokenIdeograph) {
210 m_derivedFontData->brokenIdeograph = new SimpleFontData(m_platformData, isCustomFont(), false);
211 m_derivedFontData->brokenIdeograph->m_orientation = Vertical;
212 m_derivedFontData->brokenIdeograph->m_isBrokenIdeographFont = true;
214 return m_derivedFontData->brokenIdeograph.get();
218 String SimpleFontData::description() const
223 return "[custom font]";
225 return platformData().description();
229 PassOwnPtr<SimpleFontData::DerivedFontData> SimpleFontData::DerivedFontData::create(bool forCustomFont)
231 return adoptPtr(new DerivedFontData(forCustomFont));
234 SimpleFontData::DerivedFontData::~DerivedFontData()
240 GlyphPageTreeNode::pruneTreeCustomFontData(smallCaps.get());
242 GlyphPageTreeNode::pruneTreeCustomFontData(emphasisMark.get());
244 GlyphPageTreeNode::pruneTreeCustomFontData(brokenIdeograph.get());
247 } // namespace WebCore