OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2007, 2008, 2010 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 "core/platform/graphics/Font.h" | |
33 | |
34 #include "platform/NotImplemented.h" | |
35 #include "core/platform/graphics/GraphicsContext.h" | |
36 #include "core/platform/graphics/SimpleFontData.h" | |
37 #include "core/platform/graphics/harfbuzz/HarfBuzzShaper.h" | |
38 #include "platform/fonts/GlyphBuffer.h" | |
39 #include "platform/geometry/FloatRect.h" | |
40 | |
41 #include "SkPaint.h" | |
42 #include "SkTemplates.h" | |
43 | |
44 #include "wtf/unicode/Unicode.h" | |
45 | |
46 namespace WebCore { | |
47 | |
48 bool Font::canReturnFallbackFontsForComplexText() | |
49 { | |
50 return false; | |
51 } | |
52 | |
53 bool Font::canExpandAroundIdeographsInComplexText() | |
54 { | |
55 return false; | |
56 } | |
57 | |
58 | |
59 static void paintGlyphs(GraphicsContext* gc, const SimpleFontData* font, | |
60 const GlyphBufferGlyph* glyphs, unsigned numGlyphs, | |
61 SkPoint* pos, const FloatRect& textRect) | |
62 { | |
63 TextDrawingModeFlags textMode = gc->textDrawingMode(); | |
64 | |
65 // We draw text up to two times (once for fill, once for stroke). | |
66 if (textMode & TextModeFill) { | |
67 SkPaint paint; | |
68 gc->setupPaintForFilling(&paint); | |
69 font->platformData().setupPaint(&paint, gc); | |
70 gc->adjustTextRenderMode(&paint); | |
71 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
72 | |
73 gc->drawPosText(glyphs, numGlyphs << 1, pos, textRect, paint); | |
74 } | |
75 | |
76 if ((textMode & TextModeStroke) | |
77 && gc->strokeStyle() != NoStroke | |
78 && gc->strokeThickness() > 0) { | |
79 | |
80 SkPaint paint; | |
81 gc->setupPaintForStroking(&paint); | |
82 font->platformData().setupPaint(&paint, gc); | |
83 gc->adjustTextRenderMode(&paint); | |
84 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
85 | |
86 if (textMode & TextModeFill) { | |
87 // If we also filled, we don't want to draw shadows twice. | |
88 // See comment in FontChromiumWin.cpp::paintSkiaText() for more deta
ils. | |
89 // Since we use the looper for shadows, we remove it (if any) now. | |
90 paint.setLooper(0); | |
91 } | |
92 | |
93 gc->drawPosText(glyphs, numGlyphs << 1, pos, textRect, paint); | |
94 } | |
95 } | |
96 | |
97 void Font::drawGlyphs(GraphicsContext* gc, const SimpleFontData* font, | |
98 const GlyphBuffer& glyphBuffer, unsigned from, unsigned numGlyphs, | |
99 const FloatPoint& point, const FloatRect& textRect) const | |
100 { | |
101 SkASSERT(sizeof(GlyphBufferGlyph) == sizeof(uint16_t)); // compile-time asse
rt | |
102 | |
103 SkScalar x = SkFloatToScalar(point.x()); | |
104 SkScalar y = SkFloatToScalar(point.y()); | |
105 | |
106 SkAutoSTMalloc<32, SkPoint> storage(numGlyphs); | |
107 SkPoint* pos = storage.get(); | |
108 | |
109 const OpenTypeVerticalData* verticalData = font->verticalData(); | |
110 if (font->platformData().orientation() == Vertical && verticalData) { | |
111 AffineTransform savedMatrix = gc->getCTM(); | |
112 gc->concatCTM(AffineTransform(0, -1, 1, 0, point.x(), point.y())); | |
113 gc->concatCTM(AffineTransform(1, 0, 0, 1, -point.x(), -point.y())); | |
114 | |
115 const unsigned kMaxBufferLength = 256; | |
116 Vector<FloatPoint, kMaxBufferLength> translations; | |
117 | |
118 const FontMetrics& metrics = font->fontMetrics(); | |
119 SkScalar verticalOriginX = SkFloatToScalar(point.x() + metrics.floatAsce
nt() - metrics.floatAscent(IdeographicBaseline)); | |
120 float horizontalOffset = point.x(); | |
121 | |
122 unsigned glyphIndex = 0; | |
123 while (glyphIndex < numGlyphs) { | |
124 unsigned chunkLength = std::min(kMaxBufferLength, numGlyphs - glyphI
ndex); | |
125 | |
126 const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from + glyphInde
x); | |
127 translations.resize(chunkLength); | |
128 verticalData->getVerticalTranslationsForGlyphs(font, &glyphs[0], chu
nkLength, reinterpret_cast<float*>(&translations[0])); | |
129 | |
130 x = verticalOriginX; | |
131 y = SkFloatToScalar(point.y() + horizontalOffset - point.x()); | |
132 | |
133 float currentWidth = 0; | |
134 for (unsigned i = 0; i < chunkLength; ++i, ++glyphIndex) { | |
135 pos[i].set( | |
136 x + SkIntToScalar(lroundf(translations[i].x())), | |
137 y + -SkIntToScalar(-lroundf(currentWidth - translations[i].y
()))); | |
138 currentWidth += glyphBuffer.advanceAt(from + glyphIndex); | |
139 } | |
140 horizontalOffset += currentWidth; | |
141 paintGlyphs(gc, font, glyphs, chunkLength, pos, textRect); | |
142 } | |
143 | |
144 gc->setCTM(savedMatrix); | |
145 return; | |
146 } | |
147 | |
148 // FIXME: text rendering speed: | |
149 // Android has code in their WebCore fork to special case when the | |
150 // GlyphBuffer has no advances other than the defaults. In that case the | |
151 // text drawing can proceed faster. However, it's unclear when those | |
152 // patches may be upstreamed to WebKit so we always use the slower path | |
153 // here. | |
154 const GlyphBufferAdvance* adv = glyphBuffer.advances(from); | |
155 for (unsigned i = 0; i < numGlyphs; i++) { | |
156 pos[i].set(x, y); | |
157 x += SkFloatToScalar(adv[i].width()); | |
158 y += SkFloatToScalar(adv[i].height()); | |
159 } | |
160 | |
161 const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from); | |
162 paintGlyphs(gc, font, glyphs, numGlyphs, pos, textRect); | |
163 } | |
164 | |
165 void Font::drawComplexText(GraphicsContext* gc, const TextRunPaintInfo& runInfo,
const FloatPoint& point) const | |
166 { | |
167 if (!runInfo.run.length()) | |
168 return; | |
169 | |
170 TextDrawingModeFlags textMode = gc->textDrawingMode(); | |
171 bool fill = textMode & TextModeFill; | |
172 bool stroke = (textMode & TextModeStroke) | |
173 && gc->strokeStyle() != NoStroke | |
174 && gc->strokeThickness() > 0; | |
175 | |
176 if (!fill && !stroke) | |
177 return; | |
178 | |
179 GlyphBuffer glyphBuffer; | |
180 HarfBuzzShaper shaper(this, runInfo.run); | |
181 shaper.setDrawRange(runInfo.from, runInfo.to); | |
182 if (!shaper.shape(&glyphBuffer)) | |
183 return; | |
184 FloatPoint adjustedPoint = shaper.adjustStartPoint(point); | |
185 drawGlyphBuffer(gc, runInfo, glyphBuffer, adjustedPoint); | |
186 } | |
187 | |
188 void Font::drawEmphasisMarksForComplexText(GraphicsContext* /* context */, const
TextRunPaintInfo& /* runInfo */, const AtomicString& /* mark */, const FloatPoi
nt& /* point */) const | |
189 { | |
190 notImplemented(); | |
191 } | |
192 | |
193 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFon
tData*>* /* fallbackFonts */, GlyphOverflow* /* glyphOverflow */) const | |
194 { | |
195 HarfBuzzShaper shaper(this, run); | |
196 if (!shaper.shape()) | |
197 return 0; | |
198 return shaper.totalWidth(); | |
199 } | |
200 | |
201 // Return the code point index for the given |x| offset into the text run. | |
202 int Font::offsetForPositionForComplexText(const TextRun& run, float xFloat, | |
203 bool includePartialGlyphs) const | |
204 { | |
205 HarfBuzzShaper shaper(this, run); | |
206 if (!shaper.shape()) | |
207 return 0; | |
208 return shaper.offsetForPosition(xFloat); | |
209 } | |
210 | |
211 // Return the rectangle for selecting the given range of code-points in the Text
Run. | |
212 FloatRect Font::selectionRectForComplexText(const TextRun& run, | |
213 const FloatPoint& point, int height, | |
214 int from, int to) const | |
215 { | |
216 HarfBuzzShaper shaper(this, run); | |
217 if (!shaper.shape()) | |
218 return FloatRect(); | |
219 return shaper.selectionRect(point, height, from, to); | |
220 } | |
221 | |
222 } // namespace WebCore | |
OLD | NEW |