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.
32 #include "platform/fonts/Font.h"
34 #include "platform/LayoutTestSupport.h"
35 #include "platform/fonts/FontPlatformFeatures.h"
36 #include "platform/fonts/FontSmoothingMode.h"
37 #include "platform/fonts/GlyphBuffer.h"
38 #include "platform/fonts/SimpleFontData.h"
39 #include "platform/graphics/GraphicsContext.h"
41 #include "third_party/skia/include/core/SkCanvas.h"
42 #include "third_party/skia/include/core/SkPaint.h"
43 #include "third_party/skia/include/core/SkTypeface.h"
47 bool FontPlatformFeatures::canReturnFallbackFontsForComplexText()
52 bool FontPlatformFeatures::canExpandAroundIdeographsInComplexText()
57 static void setupPaint(SkPaint* paint, const SimpleFontData* fontData, const Font* font, bool shouldAntialias, bool shouldSmoothFonts)
59 const FontPlatformData& platformData = fontData->platformData();
60 const float textSize = platformData.m_size >= 0 ? platformData.m_size : 12;
62 paint->setAntiAlias(shouldAntialias);
63 paint->setEmbeddedBitmapText(false);
64 paint->setTextSize(SkFloatToScalar(textSize));
65 paint->setVerticalText(platformData.orientation() == Vertical);
66 paint->setTypeface(platformData.typeface());
67 paint->setFakeBoldText(platformData.m_syntheticBold);
68 paint->setTextSkewX(platformData.m_syntheticOblique ? -SK_Scalar1 / 4 : 0);
69 paint->setAutohinted(false); // freetype specific
70 paint->setLCDRenderText(shouldSmoothFonts);
71 paint->setSubpixelText(true);
73 // When using CoreGraphics, disable hinting when webkit-font-smoothing:antialiased is used.
74 // See crbug.com/152304
75 if (font->fontDescription().fontSmoothing() == Antialiased || font->fontDescription().textRendering() == GeometricPrecision)
76 paint->setHinting(SkPaint::kNo_Hinting);
79 // TODO: This needs to be split into helper functions to better scope the
80 // inputs/outputs, and reduce duplicate code.
81 // This issue is tracked in https://bugs.webkit.org/show_bug.cgi?id=62989
82 void Font::drawGlyphs(GraphicsContext* gc, const SimpleFontData* font,
83 const GlyphBuffer& glyphBuffer, unsigned from, unsigned numGlyphs,
84 const FloatPoint& point, const FloatRect& textRect) const
86 bool shouldSmoothFonts = true;
87 bool shouldAntialias = true;
89 switch (fontDescription().fontSmoothing()) {
91 shouldSmoothFonts = false;
93 case SubpixelAntialiased:
96 shouldAntialias = false;
97 shouldSmoothFonts = false;
100 // For the AutoSmooth case, don't do anything! Keep the default settings.
104 if (LayoutTestSupport::isRunningLayoutTest()) {
105 shouldSmoothFonts = false;
106 shouldAntialias = shouldAntialias && LayoutTestSupport::isFontAntialiasingEnabledForTest();
109 const Glyph* glyphs = glyphBuffer.glyphs(from);
110 SkScalar x = SkFloatToScalar(point.x());
111 SkScalar y = SkFloatToScalar(point.y());
113 if (font->platformData().orientation() == Vertical)
114 y += SkFloatToScalar(font->fontMetrics().floatAscent(IdeographicBaseline) - font->fontMetrics().floatAscent());
116 // FIXME: implement a no-vertical-advances fast path like FontHarfBuzz has.
118 // FIXME: text rendering speed:
119 // Android has code in their WebCore fork to special case when the
120 // GlyphBuffer has no advances other than the defaults. In that case the
121 // text drawing can proceed faster. However, it's unclear when those
122 // patches may be upstreamed to WebKit so we always use the slower path
124 const FloatSize* adv = glyphBuffer.advances(from);
125 SkAutoSTMalloc<32, SkPoint> storage(numGlyphs);
126 SkPoint* pos = storage.get();
128 for (unsigned i = 0; i < numGlyphs; i++) {
130 x += SkFloatToScalar(adv[i].width());
131 y += SkFloatToScalar(adv[i].height());
134 if (font->platformData().orientation() == Vertical) {
136 gc->rotate(-0.5 * SK_ScalarPI);
139 rotator.setRotate(90);
140 rotator.mapPoints(pos, numGlyphs);
142 TextDrawingModeFlags textMode = gc->textDrawingMode();
144 // We draw text up to two times (once for fill, once for stroke).
145 if (textMode & TextModeFill) {
146 SkPaint paint = gc->fillPaint();
147 setupPaint(&paint, font, this, shouldAntialias, shouldSmoothFonts);
148 gc->adjustTextRenderMode(&paint);
149 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
151 gc->drawPosText(glyphs, numGlyphs * sizeof(Glyph), pos, textRect, paint);
154 if ((textMode & TextModeStroke)
155 && gc->strokeStyle() != NoStroke
156 && gc->strokeThickness() > 0) {
158 SkPaint paint = gc->strokePaint();
159 setupPaint(&paint, font, this, shouldAntialias, shouldSmoothFonts);
160 gc->adjustTextRenderMode(&paint);
161 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
163 if (textMode & TextModeFill) {
164 // If we also filled, we don't want to draw shadows twice.
165 // See comment in FontHarfBuzz.cpp::paintGlyphs() for more details.
169 gc->drawPosText(glyphs, numGlyphs * sizeof(Glyph), pos, textRect, paint);
171 if (font->platformData().orientation() == Vertical)