1/* 2 * Copyright (c) 2011 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 "platform/fonts/Font.h" 33 34#include "platform/LayoutTestSupport.h" 35#include "platform/fonts/FontSmoothingMode.h" 36#include "platform/fonts/GlyphBuffer.h" 37#include "platform/fonts/SimpleFontData.h" 38#include "platform/graphics/GraphicsContext.h" 39 40#include "third_party/skia/include/core/SkCanvas.h" 41#include "third_party/skia/include/core/SkPaint.h" 42#include "third_party/skia/include/core/SkTypeface.h" 43#include "third_party/skia/include/ports/SkTypeface_mac.h" 44 45namespace WebCore { 46 47bool Font::canReturnFallbackFontsForComplexText() 48{ 49 return true; 50} 51 52bool Font::canExpandAroundIdeographsInComplexText() 53{ 54 return true; 55} 56 57static void setupPaint(SkPaint* paint, const SimpleFontData* fontData, const Font* font, bool shouldAntialias, bool shouldSmoothFonts) 58{ 59 const FontPlatformData& platformData = fontData->platformData(); 60 const float textSize = platformData.m_size >= 0 ? platformData.m_size : 12; 61 62 paint->setAntiAlias(shouldAntialias); 63 paint->setEmbeddedBitmapText(false); 64 paint->setTextSize(SkFloatToScalar(textSize)); 65 paint->setVerticalText(platformData.orientation() == Vertical); 66 SkTypeface* typeface = SkCreateTypefaceFromCTFont(platformData.ctFont()); 67 SkAutoUnref autoUnref(typeface); 68 paint->setTypeface(typeface); 69 paint->setFakeBoldText(platformData.m_syntheticBold); 70 paint->setTextSkewX(platformData.m_syntheticOblique ? -SK_Scalar1 / 4 : 0); 71 paint->setAutohinted(false); // freetype specific 72 paint->setLCDRenderText(shouldSmoothFonts); 73 paint->setSubpixelText(true); 74 75#if OS(MACOSX) 76 // When using CoreGraphics, disable hinting when webkit-font-smoothing:antialiased is used. 77 // See crbug.com/152304 78 if (font->fontDescription().fontSmoothing() == Antialiased) 79 paint->setHinting(SkPaint::kNo_Hinting); 80#endif 81 82 if (font->fontDescription().textRenderingMode() == GeometricPrecision) 83 paint->setHinting(SkPaint::kNo_Hinting); 84} 85 86// TODO: This needs to be split into helper functions to better scope the 87// inputs/outputs, and reduce duplicate code. 88// This issue is tracked in https://bugs.webkit.org/show_bug.cgi?id=62989 89void Font::drawGlyphs(GraphicsContext* gc, const SimpleFontData* font, 90 const GlyphBuffer& glyphBuffer, unsigned from, unsigned numGlyphs, 91 const FloatPoint& point, const FloatRect& textRect) const 92{ 93 COMPILE_ASSERT(sizeof(GlyphBufferGlyph) == sizeof(uint16_t), GlyphBufferGlyphSize_equals_uint16_t); 94 95 bool shouldSmoothFonts = true; 96 bool shouldAntialias = true; 97 98 switch (fontDescription().fontSmoothing()) { 99 case Antialiased: 100 shouldSmoothFonts = false; 101 break; 102 case SubpixelAntialiased: 103 break; 104 case NoSmoothing: 105 shouldAntialias = false; 106 shouldSmoothFonts = false; 107 break; 108 case AutoSmoothing: 109 // For the AutoSmooth case, don't do anything! Keep the default settings. 110 break; 111 } 112 113 if (!shouldUseSmoothing() || isRunningLayoutTest()) { 114 shouldSmoothFonts = false; 115 shouldAntialias = false; 116 } 117 118 const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from); 119 SkScalar x = SkFloatToScalar(point.x()); 120 SkScalar y = SkFloatToScalar(point.y()); 121 122 if (font->platformData().orientation() == Vertical) 123 y += SkFloatToScalar(font->fontMetrics().floatAscent(IdeographicBaseline) - font->fontMetrics().floatAscent()); 124 // FIXME: text rendering speed: 125 // Android has code in their WebCore fork to special case when the 126 // GlyphBuffer has no advances other than the defaults. In that case the 127 // text drawing can proceed faster. However, it's unclear when those 128 // patches may be upstreamed to WebKit so we always use the slower path 129 // here. 130 const GlyphBufferAdvance* adv = glyphBuffer.advances(from); 131 SkAutoSTMalloc<32, SkPoint> storage(numGlyphs); 132 SkPoint* pos = storage.get(); 133 134 for (unsigned i = 0; i < numGlyphs; i++) { 135 pos[i].set(x, y); 136 x += SkFloatToScalar(adv[i].width()); 137 y += SkFloatToScalar(adv[i].height()); 138 } 139 140 if (font->platformData().orientation() == Vertical) { 141 gc->save(); 142 gc->rotate(-0.5 * SK_ScalarPI); 143 SkMatrix rotator; 144 rotator.reset(); 145 rotator.setRotate(90); 146 rotator.mapPoints(pos, numGlyphs); 147 } 148 TextDrawingModeFlags textMode = gc->textDrawingMode(); 149 150 // We draw text up to two times (once for fill, once for stroke). 151 if (textMode & TextModeFill) { 152 SkPaint paint; 153 gc->setupPaintForFilling(&paint); 154 setupPaint(&paint, font, this, shouldAntialias, shouldSmoothFonts); 155 gc->adjustTextRenderMode(&paint); 156 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); 157 158 gc->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, textRect, paint); 159 } 160 161 if ((textMode & TextModeStroke) 162 && gc->strokeStyle() != NoStroke 163 && gc->strokeThickness() > 0) { 164 165 SkPaint paint; 166 gc->setupPaintForStroking(&paint); 167 setupPaint(&paint, font, this, shouldAntialias, shouldSmoothFonts); 168 gc->adjustTextRenderMode(&paint); 169 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); 170 171 if (textMode & TextModeFill) { 172 // If we also filled, we don't want to draw shadows twice. 173 // See comment in FontChromiumWin.cpp::paintSkiaText() for more details. 174 paint.setLooper(0); 175 } 176 177 gc->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, textRect, paint); 178 } 179 if (font->platformData().orientation() == Vertical) 180 gc->restore(); 181} 182 183} // namespace WebCore 184