OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2008, 2009, Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
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 | |
13 * distribution. | |
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. | |
17 * | |
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. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 #include "platform/fonts/SimpleFontData.h" | |
33 | |
34 #include <unicode/normlzr.h> | |
35 #include "SkPaint.h" | |
36 #include "SkPath.h" | |
37 #include "SkTypeface.h" | |
38 #include "SkTypes.h" | |
39 #include "SkUtils.h" | |
40 #include "platform/fonts/FontDescription.h" | |
41 #include "platform/fonts/GlyphPage.h" | |
42 #include "platform/fonts/VDMXParser.h" | |
43 #include "platform/geometry/FloatRect.h" | |
44 #include "wtf/unicode/Unicode.h" | |
45 | |
46 namespace blink { | |
47 | |
48 #if OS(LINUX) || OS(ANDROID) | |
49 // This is the largest VDMX table which we'll try to load and parse. | |
50 static const size_t maxVDMXTableSize = 1024 * 1024; // 1 MB | |
51 #endif | |
52 | |
53 void SimpleFontData::platformInit() | |
54 { | |
55 if (!m_platformData.size()) { | |
56 m_fontMetrics.reset(); | |
57 m_avgCharWidth = 0; | |
58 m_maxCharWidth = 0; | |
59 return; | |
60 } | |
61 | |
62 SkPaint paint; | |
63 SkPaint::FontMetrics metrics; | |
64 | |
65 m_platformData.setupPaint(&paint); | |
66 paint.getFontMetrics(&metrics); | |
67 SkTypeface* face = paint.getTypeface(); | |
68 ASSERT(face); | |
69 | |
70 int vdmxAscent = 0, vdmxDescent = 0; | |
71 bool isVDMXValid = false; | |
72 | |
73 #if OS(LINUX) || OS(ANDROID) | |
74 // Manually digging up VDMX metrics is only applicable when bytecode hinting
using FreeType. | |
75 // With GDI, the metrics will already have taken this into account (as neede
d). | |
76 // With DirectWrite or CoreText, no bytecode hinting is ever done. | |
77 // This code should be pushed into FreeType (hinted font metrics). | |
78 static const uint32_t vdmxTag = SkSetFourByteTag('V', 'D', 'M', 'X'); | |
79 int pixelSize = m_platformData.size() + 0.5; | |
80 if (!paint.isAutohinted() | |
81 && (paint.getHinting() == SkPaint::kFull_Hinting | |
82 || paint.getHinting() == SkPaint::kNormal_Hinting)) | |
83 { | |
84 size_t vdmxSize = face->getTableSize(vdmxTag); | |
85 if (vdmxSize && vdmxSize < maxVDMXTableSize) { | |
86 uint8_t* vdmxTable = (uint8_t*) fastMalloc(vdmxSize); | |
87 if (vdmxTable | |
88 && face->getTableData(vdmxTag, 0, vdmxSize, vdmxTable) == vdmxSi
ze | |
89 && parseVDMX(&vdmxAscent, &vdmxDescent, vdmxTable, vdmxSize, pix
elSize)) | |
90 isVDMXValid = true; | |
91 fastFree(vdmxTable); | |
92 } | |
93 } | |
94 #endif | |
95 | |
96 float ascent; | |
97 float descent; | |
98 | |
99 // Beware those who step here: This code is designed to match Win32 font | |
100 // metrics *exactly* (except the adjustment of ascent/descent on Linux/Andro
id). | |
101 if (isVDMXValid) { | |
102 ascent = vdmxAscent; | |
103 descent = -vdmxDescent; | |
104 } else { | |
105 ascent = SkScalarRoundToInt(-metrics.fAscent); | |
106 descent = SkScalarRoundToInt(metrics.fDescent); | |
107 #if OS(LINUX) || OS(ANDROID) | |
108 // When subpixel positioning is enabled, if the descent is rounded down,
the descent part | |
109 // of the glyph may be truncated when displayed in a 'overflow: hidden'
container. | |
110 // To avoid that, borrow 1 unit from the ascent when possible. | |
111 // FIXME: This can be removed if sub-pixel ascent/descent is supported. | |
112 if (platformData().fontRenderStyle().useSubpixelPositioning && descent <
SkScalarToFloat(metrics.fDescent) && ascent >= 1) { | |
113 ++descent; | |
114 --ascent; | |
115 } | |
116 #endif | |
117 } | |
118 | |
119 m_fontMetrics.setAscent(ascent); | |
120 m_fontMetrics.setDescent(descent); | |
121 | |
122 float xHeight; | |
123 if (metrics.fXHeight) { | |
124 xHeight = metrics.fXHeight; | |
125 m_fontMetrics.setXHeight(xHeight); | |
126 } else { | |
127 xHeight = ascent * 0.56; // Best guess from Windows font metrics. | |
128 m_fontMetrics.setXHeight(xHeight); | |
129 m_fontMetrics.setHasXHeight(false); | |
130 } | |
131 | |
132 float lineGap = SkScalarToFloat(metrics.fLeading); | |
133 m_fontMetrics.setLineGap(lineGap); | |
134 m_fontMetrics.setLineSpacing(lroundf(ascent) + lroundf(descent) + lroundf(li
neGap)); | |
135 | |
136 if (platformData().orientation() == Vertical && !isTextOrientationFallback()
) { | |
137 static const uint32_t vheaTag = SkSetFourByteTag('v', 'h', 'e', 'a'); | |
138 static const uint32_t vorgTag = SkSetFourByteTag('V', 'O', 'R', 'G'); | |
139 size_t vheaSize = face->getTableSize(vheaTag); | |
140 size_t vorgSize = face->getTableSize(vorgTag); | |
141 if ((vheaSize > 0) || (vorgSize > 0)) | |
142 m_hasVerticalGlyphs = true; | |
143 } | |
144 | |
145 // In WebKit/WebCore/platform/graphics/SimpleFontData.cpp, m_spaceWidth is | |
146 // calculated for us, but we need to calculate m_maxCharWidth and | |
147 // m_avgCharWidth in order for text entry widgets to be sized correctly. | |
148 #if OS(WIN) | |
149 m_maxCharWidth = SkScalarRoundToInt(metrics.fMaxCharWidth); | |
150 | |
151 // Older version of the DirectWrite API doesn't implement support for max | |
152 // char width. Fall back on a multiple of the ascent. This is entirely | |
153 // arbitrary but comes pretty close to the expected value in most cases. | |
154 if (m_maxCharWidth < 1) | |
155 m_maxCharWidth = ascent * 2; | |
156 #else | |
157 // FIXME: This seems incorrect and should probably use fMaxCharWidth as | |
158 // the code path above. | |
159 SkScalar xRange = metrics.fXMax - metrics.fXMin; | |
160 m_maxCharWidth = SkScalarRoundToInt(xRange * SkScalarRoundToInt(m_platformDa
ta.size())); | |
161 #endif | |
162 | |
163 if (metrics.fAvgCharWidth) | |
164 m_avgCharWidth = SkScalarRoundToInt(metrics.fAvgCharWidth); | |
165 else { | |
166 m_avgCharWidth = xHeight; | |
167 | |
168 GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->pag
e(); | |
169 | |
170 if (glyphPageZero) { | |
171 static const UChar32 xChar = 'x'; | |
172 const Glyph xGlyph = glyphPageZero->glyphForCharacter(xChar); | |
173 | |
174 if (xGlyph) { | |
175 // In widthForGlyph(), xGlyph will be compared with | |
176 // m_zeroWidthSpaceGlyph, which isn't initialized yet here. | |
177 // Initialize it with zero to make sure widthForGlyph() returns | |
178 // the right width. | |
179 m_zeroWidthSpaceGlyph = 0; | |
180 m_avgCharWidth = widthForGlyph(xGlyph); | |
181 } | |
182 } | |
183 } | |
184 | |
185 if (int unitsPerEm = face->getUnitsPerEm()) | |
186 m_fontMetrics.setUnitsPerEm(unitsPerEm); | |
187 } | |
188 | |
189 void SimpleFontData::platformCharWidthInit() | |
190 { | |
191 // charwidths are set in platformInit. | |
192 } | |
193 | |
194 void SimpleFontData::platformDestroy() | |
195 { | |
196 } | |
197 | |
198 PassRefPtr<SimpleFontData> SimpleFontData::platformCreateScaledFontData(const Fo
ntDescription& fontDescription, float scaleFactor) const | |
199 { | |
200 const float scaledSize = lroundf(fontDescription.computedSize() * scaleFacto
r); | |
201 return SimpleFontData::create(FontPlatformData(m_platformData, scaledSize),
isCustomFont() ? CustomFontData::create() : nullptr); | |
202 } | |
203 | |
204 void SimpleFontData::determinePitch() | |
205 { | |
206 m_treatAsFixedPitch = platformData().isFixedPitch(); | |
207 } | |
208 | |
209 static inline void getSkiaBoundsForGlyph(SkPaint& paint, Glyph glyph, SkRect& bo
unds) | |
210 { | |
211 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
212 | |
213 SkPath path; | |
214 paint.getTextPath(&glyph, sizeof(glyph), 0, 0, &path); | |
215 bounds = path.getBounds(); | |
216 | |
217 if (!paint.isSubpixelText()) { | |
218 SkIRect ir; | |
219 bounds.round(&ir); | |
220 bounds.set(ir); | |
221 } | |
222 } | |
223 | |
224 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph glyph) const | |
225 { | |
226 if (!m_platformData.size()) | |
227 return FloatRect(); | |
228 | |
229 SkASSERT(sizeof(glyph) == 2); // compile-time assert | |
230 | |
231 SkPaint paint; | |
232 m_platformData.setupPaint(&paint); | |
233 | |
234 SkRect bounds; | |
235 getSkiaBoundsForGlyph(paint, glyph, bounds); | |
236 return FloatRect(bounds); | |
237 } | |
238 | |
239 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const | |
240 { | |
241 if (!m_platformData.size()) | |
242 return 0; | |
243 | |
244 SkASSERT(sizeof(glyph) == 2); // compile-time assert | |
245 | |
246 SkPaint paint; | |
247 | |
248 m_platformData.setupPaint(&paint); | |
249 | |
250 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
251 SkScalar width = paint.measureText(&glyph, 2); | |
252 if (!paint.isSubpixelText()) | |
253 width = SkScalarRoundToInt(width); | |
254 return SkScalarToFloat(width); | |
255 } | |
256 | |
257 bool SimpleFontData::canRenderCombiningCharacterSequence(const UChar* characters
, size_t length) const | |
258 { | |
259 if (!m_combiningCharacterSequenceSupport) | |
260 m_combiningCharacterSequenceSupport = adoptPtr(new HashMap<String, bool>
); | |
261 | |
262 WTF::HashMap<String, bool>::AddResult addResult = m_combiningCharacterSequen
ceSupport->add(String(characters, length), false); | |
263 if (!addResult.isNewEntry) | |
264 return addResult.storedValue->value; | |
265 | |
266 UErrorCode error = U_ZERO_ERROR; | |
267 Vector<UChar, 4> normalizedCharacters(length); | |
268 int32_t normalizedLength = unorm_normalize(characters, length, UNORM_NFC, UN
ORM_UNICODE_3_2, &normalizedCharacters[0], length, &error); | |
269 // Can't render if we have an error or no composition occurred. | |
270 if (U_FAILURE(error) || (static_cast<size_t>(normalizedLength) == length)) | |
271 return false; | |
272 | |
273 SkPaint paint; | |
274 m_platformData.setupPaint(&paint); | |
275 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); | |
276 if (paint.textToGlyphs(&normalizedCharacters[0], normalizedLength * 2, 0)) { | |
277 addResult.storedValue->value = true; | |
278 return true; | |
279 } | |
280 return false; | |
281 } | |
282 | |
283 bool SimpleFontData::fillGlyphPage(GlyphPage* pageToFill, unsigned offset, unsig
ned length, UChar* buffer, unsigned bufferLength) const | |
284 { | |
285 if (SkUTF16_IsHighSurrogate(buffer[bufferLength-1])) { | |
286 SkDebugf("%s last char is high-surrogate", __FUNCTION__); | |
287 return false; | |
288 } | |
289 | |
290 SkAutoSTMalloc<GlyphPage::size, uint16_t> glyphStorage(length); | |
291 | |
292 uint16_t* glyphs = glyphStorage.get(); | |
293 SkTypeface* typeface = platformData().typeface(); | |
294 typeface->charsToGlyphs(buffer, SkTypeface::kUTF16_Encoding, glyphs, length)
; | |
295 | |
296 bool haveGlyphs = false; | |
297 for (unsigned i = 0; i < length; i++) { | |
298 if (glyphs[i]) { | |
299 pageToFill->setGlyphDataForIndex(offset + i, glyphs[i], this); | |
300 haveGlyphs = true; | |
301 } | |
302 } | |
303 | |
304 return haveGlyphs; | |
305 } | |
306 | |
307 } // namespace blink | |
OLD | NEW |