2 * Copyright (c) 2007, 2008, 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "platform/fonts/Font.h"
34 #include "platform/RuntimeEnabledFeatures.h"
35 #include "platform/fonts/FontPlatformFeatures.h"
36 #include "platform/fonts/SimpleFontData.h"
37 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h"
38 #include "platform/fonts/GlyphBuffer.h"
39 #include "platform/geometry/FloatRect.h"
40 #include "platform/graphics/GraphicsContext.h"
43 #include "SkTemplates.h"
45 #include "wtf/unicode/Unicode.h"
51 bool FontPlatformFeatures::canExpandAroundIdeographsInComplexText()
56 static SkPaint textFillPaint(GraphicsContext* gc, const SimpleFontData* font)
58 SkPaint paint = gc->fillPaint();
59 font->platformData().setupPaint(&paint, gc);
60 gc->adjustTextRenderMode(&paint);
61 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
65 static SkPaint textStrokePaint(GraphicsContext* gc, const SimpleFontData* font, bool isFilling)
67 SkPaint paint = gc->strokePaint();
68 font->platformData().setupPaint(&paint, gc);
69 gc->adjustTextRenderMode(&paint);
70 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
72 // If there is a shadow and we filled above, there will already be
73 // a shadow. We don't want to draw it again or it will be too dark
74 // and it will go on top of the fill.
76 // Note that this isn't strictly correct, since the stroke could be
77 // very thick and the shadow wouldn't account for this. The "right"
78 // thing would be to draw to a new layer and then draw that layer
79 // with a shadow. But this is a lot of extra work for something
80 // that isn't normally an issue.
86 static void paintGlyphs(GraphicsContext* gc, const SimpleFontData* font,
87 const Glyph glyphs[], unsigned numGlyphs,
88 const SkPoint pos[], const FloatRect& textRect)
90 TextDrawingModeFlags textMode = gc->textDrawingMode();
92 // We draw text up to two times (once for fill, once for stroke).
93 if (textMode & TextModeFill) {
94 SkPaint paint = textFillPaint(gc, font);
95 gc->drawPosText(glyphs, numGlyphs * sizeof(Glyph), pos, textRect, paint);
98 if ((textMode & TextModeStroke) && gc->hasStroke()) {
99 SkPaint paint = textStrokePaint(gc, font, textMode & TextModeFill);
100 gc->drawPosText(glyphs, numGlyphs * sizeof(Glyph), pos, textRect, paint);
104 static void paintGlyphsHorizontal(GraphicsContext* gc, const SimpleFontData* font,
105 const Glyph glyphs[], unsigned numGlyphs,
106 const SkScalar xpos[], SkScalar constY, const FloatRect& textRect)
108 TextDrawingModeFlags textMode = gc->textDrawingMode();
110 if (textMode & TextModeFill) {
111 SkPaint paint = textFillPaint(gc, font);
112 gc->drawPosTextH(glyphs, numGlyphs * sizeof(Glyph), xpos, constY, textRect, paint);
115 if ((textMode & TextModeStroke) && gc->hasStroke()) {
116 SkPaint paint = textStrokePaint(gc, font, textMode & TextModeFill);
117 gc->drawPosTextH(glyphs, numGlyphs * sizeof(Glyph), xpos, constY, textRect, paint);
121 void Font::drawGlyphs(GraphicsContext* gc, const SimpleFontData* font,
122 const GlyphBuffer& glyphBuffer, unsigned from, unsigned numGlyphs,
123 const FloatPoint& point, const FloatRect& textRect) const
125 SkScalar x = SkFloatToScalar(point.x());
126 SkScalar y = SkFloatToScalar(point.y());
128 const OpenTypeVerticalData* verticalData = font->verticalData();
129 if (font->platformData().orientation() == Vertical && verticalData) {
130 SkAutoSTMalloc<32, SkPoint> storage(numGlyphs);
131 SkPoint* pos = storage.get();
133 AffineTransform savedMatrix = gc->getCTM();
134 gc->concatCTM(AffineTransform(0, -1, 1, 0, point.x(), point.y()));
135 gc->concatCTM(AffineTransform(1, 0, 0, 1, -point.x(), -point.y()));
137 const unsigned kMaxBufferLength = 256;
138 Vector<FloatPoint, kMaxBufferLength> translations;
140 const FontMetrics& metrics = font->fontMetrics();
141 SkScalar verticalOriginX = SkFloatToScalar(point.x() + metrics.floatAscent() - metrics.floatAscent(IdeographicBaseline));
142 float horizontalOffset = point.x();
144 unsigned glyphIndex = 0;
145 while (glyphIndex < numGlyphs) {
146 unsigned chunkLength = std::min(kMaxBufferLength, numGlyphs - glyphIndex);
148 const Glyph* glyphs = glyphBuffer.glyphs(from + glyphIndex);
149 translations.resize(chunkLength);
150 verticalData->getVerticalTranslationsForGlyphs(font, &glyphs[0], chunkLength, reinterpret_cast<float*>(&translations[0]));
153 y = SkFloatToScalar(point.y() + horizontalOffset - point.x());
155 float currentWidth = 0;
156 for (unsigned i = 0; i < chunkLength; ++i, ++glyphIndex) {
158 x + SkIntToScalar(lroundf(translations[i].x())),
159 y + -SkIntToScalar(-lroundf(currentWidth - translations[i].y())));
160 currentWidth += glyphBuffer.advanceAt(from + glyphIndex).width();
162 horizontalOffset += currentWidth;
163 paintGlyphs(gc, font, glyphs, chunkLength, pos, textRect);
166 gc->setCTM(savedMatrix);
170 if (!glyphBuffer.hasVerticalAdvances()) {
171 SkAutoSTMalloc<64, SkScalar> storage(numGlyphs);
172 SkScalar* xpos = storage.get();
173 const FloatSize* adv = glyphBuffer.advances(from);
174 for (unsigned i = 0; i < numGlyphs; i++) {
176 x += SkFloatToScalar(adv[i].width());
178 const Glyph* glyphs = glyphBuffer.glyphs(from);
179 paintGlyphsHorizontal(gc, font, glyphs, numGlyphs, xpos, SkFloatToScalar(y), textRect);
183 // FIXME: text rendering speed:
184 // Android has code in their WebCore fork to special case when the
185 // GlyphBuffer has no advances other than the defaults. In that case the
186 // text drawing can proceed faster. However, it's unclear when those
187 // patches may be upstreamed to WebKit so we always use the slower path
189 SkAutoSTMalloc<32, SkPoint> storage(numGlyphs);
190 SkPoint* pos = storage.get();
191 const FloatSize* adv = glyphBuffer.advances(from);
192 for (unsigned i = 0; i < numGlyphs; i++) {
194 x += SkFloatToScalar(adv[i].width());
195 y += SkFloatToScalar(adv[i].height());
198 const Glyph* glyphs = glyphBuffer.glyphs(from);
199 paintGlyphs(gc, font, glyphs, numGlyphs, pos, textRect);
202 void Font::drawTextBlob(GraphicsContext* gc, const SkTextBlob* blob, const SkPoint& origin) const
204 ASSERT(RuntimeEnabledFeatures::textBlobEnabled());
206 // FIXME: It would be good to move this to Font.cpp, if we're sure that none
207 // of the things in FontMac's setupPaint need to apply here.
208 // See also paintGlyphs.
209 TextDrawingModeFlags textMode = gc->textDrawingMode();
211 if (textMode & TextModeFill)
212 gc->drawTextBlob(blob, origin, gc->fillPaint());
214 if ((textMode & TextModeStroke) && gc->hasStroke()) {
215 SkPaint paint = gc->strokePaint();
216 if (textMode & TextModeFill)
218 gc->drawTextBlob(blob, origin, paint);
222 float Font::drawComplexText(GraphicsContext* gc, const TextRunPaintInfo& runInfo, const FloatPoint& point) const
224 if (!runInfo.run.length())
227 TextDrawingModeFlags textMode = gc->textDrawingMode();
228 bool fill = textMode & TextModeFill;
229 bool stroke = (textMode & TextModeStroke) && gc->hasStroke();
231 if (!fill && !stroke)
234 GlyphBuffer glyphBuffer;
235 HarfBuzzShaper shaper(this, runInfo.run);
236 shaper.setDrawRange(runInfo.from, runInfo.to);
237 if (!shaper.shape(&glyphBuffer) || glyphBuffer.isEmpty())
239 FloatPoint adjustedPoint = shaper.adjustStartPoint(point);
240 return drawGlyphBuffer(gc, runInfo, glyphBuffer, adjustedPoint);
243 void Font::drawEmphasisMarksForComplexText(GraphicsContext* context, const TextRunPaintInfo& runInfo, const AtomicString& mark, const FloatPoint& point) const
245 GlyphBuffer glyphBuffer;
247 float initialAdvance = getGlyphsAndAdvancesForComplexText(runInfo, glyphBuffer, ForTextEmphasis);
249 if (glyphBuffer.isEmpty())
252 drawEmphasisMarks(context, runInfo, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y()));
255 float Font::getGlyphsAndAdvancesForComplexText(const TextRunPaintInfo& runInfo, GlyphBuffer& glyphBuffer, ForTextEmphasisOrNot forTextEmphasis) const
257 HarfBuzzShaper shaper(this, runInfo.run, HarfBuzzShaper::ForTextEmphasis);
258 shaper.setDrawRange(runInfo.from, runInfo.to);
259 shaper.shape(&glyphBuffer);
263 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts, IntRectExtent* glyphBounds) const
265 HarfBuzzShaper shaper(this, run, HarfBuzzShaper::NotForTextEmphasis, fallbackFonts);
269 glyphBounds->setTop(floorf(-shaper.glyphBoundingBox().top()));
270 glyphBounds->setBottom(ceilf(shaper.glyphBoundingBox().bottom()));
271 glyphBounds->setLeft(std::max<int>(0, floorf(-shaper.glyphBoundingBox().left())));
272 glyphBounds->setRight(std::max<int>(0, ceilf(shaper.glyphBoundingBox().right() - shaper.totalWidth())));
274 return shaper.totalWidth();
277 // Return the code point index for the given |x| offset into the text run.
278 int Font::offsetForPositionForComplexText(const TextRun& run, float xFloat,
279 bool includePartialGlyphs) const
281 HarfBuzzShaper shaper(this, run);
284 return shaper.offsetForPosition(xFloat);
287 // Return the rectangle for selecting the given range of code-points in the TextRun.
288 FloatRect Font::selectionRectForComplexText(const TextRun& run,
289 const FloatPoint& point, int height,
290 int from, int to) const
292 HarfBuzzShaper shaper(this, run);
295 return shaper.selectionRect(point, height, from, to);
298 PassTextBlobPtr Font::buildTextBlob(const GlyphBuffer& glyphBuffer, float initialAdvance,
299 const FloatRect& bounds, float& advance, bool couldUseLCD) const
301 ASSERT(RuntimeEnabledFeatures::textBlobEnabled());
303 // FIXME: Except for setupPaint, this is not specific to FontHarfBuzz.
304 // FIXME: Also implement the more general full-positioning path.
305 ASSERT(!glyphBuffer.hasVerticalAdvances());
307 SkTextBlobBuilder builder;
308 SkScalar x = SkFloatToScalar(initialAdvance);
309 SkRect skBounds = bounds;
312 while (i < glyphBuffer.size()) {
313 const SimpleFontData* fontData = glyphBuffer.fontDataAt(i);
315 // FIXME: Handle vertical text.
316 if (fontData->platformData().orientation() == Vertical)
319 // FIXME: Handle SVG fonts.
320 if (fontData->isSVGFont())
323 // FIXME: FontPlatformData makes some decisions on the device scale
324 // factor, which is found via the GraphicsContext. This should be fixed
325 // to avoid correctness problems here.
327 fontData->platformData().setupPaint(&paint);
328 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
330 // FIXME: this should go away after the big LCD cleanup.
331 paint.setLCDRenderText(paint.isLCDRenderText() && couldUseLCD);
333 unsigned start = i++;
334 while (i < glyphBuffer.size() && glyphBuffer.fontDataAt(i) == fontData)
336 unsigned count = i - start;
338 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPosH(paint, count, 0, &skBounds);
340 const uint16_t* glyphs = glyphBuffer.glyphs(start);
341 std::copy(glyphs, glyphs + count, buffer.glyphs);
343 const FloatSize* advances = glyphBuffer.advances(start);
344 for (unsigned j = 0; j < count; j++) {
346 x += SkFloatToScalar(advances[j].width());
351 return adoptRef(builder.build());