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 COMPILE_ASSERT(sizeof(GlyphBufferGlyph) == sizeof(uint16_t), GlyphBufferGlyphSize_equals_uint16_t);
88 bool shouldSmoothFonts = true;
89 bool shouldAntialias = true;
91 switch (fontDescription().fontSmoothing()) {
93 shouldSmoothFonts = false;
95 case SubpixelAntialiased:
98 shouldAntialias = false;
99 shouldSmoothFonts = false;
102 // For the AutoSmooth case, don't do anything! Keep the default settings.
106 if (isRunningLayoutTest()) {
107 shouldSmoothFonts = false;
108 shouldAntialias = shouldAntialias && isFontAntialiasingEnabledForTest();
111 const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from);
112 SkScalar x = SkFloatToScalar(point.x());
113 SkScalar y = SkFloatToScalar(point.y());
115 if (font->platformData().orientation() == Vertical)
116 y += SkFloatToScalar(font->fontMetrics().floatAscent(IdeographicBaseline) - font->fontMetrics().floatAscent());
117 // FIXME: text rendering speed:
118 // Android has code in their WebCore fork to special case when the
119 // GlyphBuffer has no advances other than the defaults. In that case the
120 // text drawing can proceed faster. However, it's unclear when those
121 // patches may be upstreamed to WebKit so we always use the slower path
123 const GlyphBufferAdvance* adv = glyphBuffer.advances(from);
124 SkAutoSTMalloc<32, SkPoint> storage(numGlyphs);
125 SkPoint* pos = storage.get();
127 for (unsigned i = 0; i < numGlyphs; i++) {
129 x += SkFloatToScalar(adv[i].width());
130 y += SkFloatToScalar(adv[i].height());
133 if (font->platformData().orientation() == Vertical) {
135 gc->rotate(-0.5 * SK_ScalarPI);
138 rotator.setRotate(90);
139 rotator.mapPoints(pos, numGlyphs);
141 TextDrawingModeFlags textMode = gc->textDrawingMode();
143 // We draw text up to two times (once for fill, once for stroke).
144 if (textMode & TextModeFill) {
146 gc->setupPaintForFilling(&paint);
147 setupPaint(&paint, font, this, shouldAntialias, shouldSmoothFonts);
148 gc->adjustTextRenderMode(&paint);
149 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
151 gc->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, textRect, paint);
154 if ((textMode & TextModeStroke)
155 && gc->strokeStyle() != NoStroke
156 && gc->strokeThickness() > 0) {
159 gc->setupPaintForStroking(&paint);
160 setupPaint(&paint, font, this, shouldAntialias, shouldSmoothFonts);
161 gc->adjustTextRenderMode(&paint);
162 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
164 if (textMode & TextModeFill) {
165 // If we also filled, we don't want to draw shadows twice.
166 // See comment in FontHarfBuzz.cpp::paintGlyphs() for more details.
170 gc->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, textRect, paint);
172 if (font->platformData().orientation() == Vertical)
176 } // namespace WebCore