2 * Copyright (c) 2011 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.
34 #include "GlyphBuffer.h"
35 #include "GraphicsContext.h"
36 #include "PlatformContextSkia.h"
37 #include "SimpleFontData.h"
41 #include "SkTypeface.h"
42 #include "SkTypeface_mac.h"
46 bool Font::canReturnFallbackFontsForComplexText()
51 // FIXME: Determine if the Mac port of Chromium using Skia can expand around
52 // ideographs in complex text. (The Windows and Linux ports for Chromium can't.)
53 // This issue is tracked in https://bugs.webkit.org/show_bug.cgi?id=62987
54 bool Font::canExpandAroundIdeographsInComplexText()
59 static bool isCanvasMultiLayered(SkCanvas* canvas)
61 SkCanvas::LayerIter layerIterator(canvas, false);
63 return !layerIterator.done();
66 static void adjustTextRenderMode(SkPaint* paint, PlatformContextSkia* skiaContext)
68 // Our layers only have a single alpha channel. This means that subpixel
69 // rendered text cannot be compositied correctly when the layer is
70 // collapsed. Therefore, subpixel text is disabled when we are drawing
71 // onto a layer or when the compositor is being used.
72 if (isCanvasMultiLayered(skiaContext->canvas()) || skiaContext->isDrawingToImageBuffer())
73 paint->setLCDRenderText(false);
76 static void setupPaint(SkPaint* paint, const SimpleFontData* fontData, const Font* font)
78 const FontPlatformData& platformData = fontData->platformData();
79 const float textSize = platformData.m_size >= 0 ? platformData.m_size : 12;
81 paint->setAntiAlias(true);
82 paint->setEmbeddedBitmapText(false);
83 paint->setTextSize(SkFloatToScalar(textSize));
84 SkTypeface* typeface = SkCreateTypefaceFromCTFont(platformData.ctFont());
85 SkAutoUnref autoUnref(typeface);
86 paint->setTypeface(typeface);
87 paint->setFakeBoldText(platformData.m_syntheticBold);
88 paint->setTextSkewX(platformData.m_syntheticOblique ? -SK_Scalar1 / 4 : 0);
89 paint->setAutohinted(false); // freetype specific
90 paint->setLCDRenderText(font->fontDescription().fontSmoothing() == SubpixelAntialiased);
93 // TODO: This needs to be split into helper functions to better scope the
94 // inputs/outputs, and reduce duplicate code.
95 // This issue is tracked in https://bugs.webkit.org/show_bug.cgi?id=62989
96 void Font::drawGlyphs(GraphicsContext* gc, const SimpleFontData* font,
97 const GlyphBuffer& glyphBuffer, int from, int numGlyphs,
98 const FloatPoint& point) const {
99 COMPILE_ASSERT(sizeof(GlyphBufferGlyph) == sizeof(uint16_t), GlyphBufferGlyphSize_equals_uint16_t);
101 const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from);
102 SkScalar x = SkFloatToScalar(point.x());
103 SkScalar y = SkFloatToScalar(point.y());
105 // FIXME: text rendering speed:
106 // Android has code in their WebCore fork to special case when the
107 // GlyphBuffer has no advances other than the defaults. In that case the
108 // text drawing can proceed faster. However, it's unclear when those
109 // patches may be upstreamed to WebKit so we always use the slower path
111 const GlyphBufferAdvance* adv = glyphBuffer.advances(from);
112 SkAutoSTMalloc<32, SkPoint> storage(numGlyphs), storage2(numGlyphs), storage3(numGlyphs);
113 SkPoint* pos = storage.get();
114 SkPoint* vPosBegin = storage2.get();
115 SkPoint* vPosEnd = storage3.get();
117 bool isVertical = font->platformData().orientation() == Vertical;
118 for (int i = 0; i < numGlyphs; i++) {
119 SkScalar myWidth = SkFloatToScalar(adv[i].width);
122 vPosBegin[i].set(x + myWidth, y);
123 vPosEnd[i].set(x + myWidth, y - myWidth);
126 y += SkFloatToScalar(adv[i].height);
129 SkCanvas* canvas = gc->platformContext()->canvas();
130 TextDrawingModeFlags textMode = gc->platformContext()->getTextDrawingMode();
132 // We draw text up to two times (once for fill, once for stroke).
133 if (textMode & TextModeFill) {
135 gc->platformContext()->setupPaintForFilling(&paint);
136 setupPaint(&paint, font, this);
137 adjustTextRenderMode(&paint, gc->platformContext());
138 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
139 paint.setColor(gc->fillColor().rgb());
143 for (int i = 0; i < numGlyphs; ++i) {
145 path.moveTo(vPosBegin[i]);
146 path.lineTo(vPosEnd[i]);
147 canvas->drawTextOnPath(glyphs + i, sizeof(uint16_t), path, 0, paint);
150 canvas->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, paint);
153 if ((textMode & TextModeStroke)
154 && gc->platformContext()->getStrokeStyle() != NoStroke
155 && gc->platformContext()->getStrokeThickness() > 0) {
158 gc->platformContext()->setupPaintForStroking(&paint, 0, 0);
159 setupPaint(&paint, font, this);
160 adjustTextRenderMode(&paint, gc->platformContext());
161 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
162 paint.setColor(gc->strokeColor().rgb());
164 if (textMode & TextModeFill) {
165 // If we also filled, we don't want to draw shadows twice.
166 // See comment in FontChromiumWin.cpp::paintSkiaText() for more details.
172 for (int i = 0; i < numGlyphs; ++i) {
174 path.moveTo(vPosBegin[i]);
175 path.lineTo(vPosEnd[i]);
176 canvas->drawTextOnPath(glyphs + i, sizeof(uint16_t), path, 0, paint);
179 canvas->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, paint);
183 } // namespace WebCore