Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / mac / FontMac.cpp
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/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"
40
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"
44
45 namespace WebCore {
46
47 bool FontPlatformFeatures::canReturnFallbackFontsForComplexText()
48 {
49     return true;
50 }
51
52 bool FontPlatformFeatures::canExpandAroundIdeographsInComplexText()
53 {
54     return true;
55 }
56
57 static 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     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);
72
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().textRenderingMode() == GeometricPrecision)
76         paint->setHinting(SkPaint::kNo_Hinting);
77 }
78
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
85 {
86     COMPILE_ASSERT(sizeof(GlyphBufferGlyph) == sizeof(uint16_t), GlyphBufferGlyphSize_equals_uint16_t);
87
88     bool shouldSmoothFonts = true;
89     bool shouldAntialias = true;
90
91     switch (fontDescription().fontSmoothing()) {
92     case Antialiased:
93         shouldSmoothFonts = false;
94         break;
95     case SubpixelAntialiased:
96         break;
97     case NoSmoothing:
98         shouldAntialias = false;
99         shouldSmoothFonts = false;
100         break;
101     case AutoSmoothing:
102         // For the AutoSmooth case, don't do anything! Keep the default settings.
103         break;
104     }
105
106     if (isRunningLayoutTest()) {
107         shouldSmoothFonts = false;
108         shouldAntialias = false;
109     }
110
111     const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from);
112     SkScalar x = SkFloatToScalar(point.x());
113     SkScalar y = SkFloatToScalar(point.y());
114
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
122     // here.
123     const GlyphBufferAdvance* adv = glyphBuffer.advances(from);
124     SkAutoSTMalloc<32, SkPoint> storage(numGlyphs);
125     SkPoint* pos = storage.get();
126
127     for (unsigned i = 0; i < numGlyphs; i++) {
128         pos[i].set(x, y);
129         x += SkFloatToScalar(adv[i].width());
130         y += SkFloatToScalar(adv[i].height());
131     }
132
133     if (font->platformData().orientation() == Vertical) {
134         gc->save();
135         gc->rotate(-0.5 * SK_ScalarPI);
136         SkMatrix rotator;
137         rotator.reset();
138         rotator.setRotate(90);
139         rotator.mapPoints(pos, numGlyphs);
140     }
141     TextDrawingModeFlags textMode = gc->textDrawingMode();
142
143     // We draw text up to two times (once for fill, once for stroke).
144     if (textMode & TextModeFill) {
145         SkPaint paint;
146         gc->setupPaintForFilling(&paint);
147         setupPaint(&paint, font, this, shouldAntialias, shouldSmoothFonts);
148         gc->adjustTextRenderMode(&paint);
149         paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
150
151         gc->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, textRect, paint);
152     }
153
154     if ((textMode & TextModeStroke)
155         && gc->strokeStyle() != NoStroke
156         && gc->strokeThickness() > 0) {
157
158         SkPaint paint;
159         gc->setupPaintForStroking(&paint);
160         setupPaint(&paint, font, this, shouldAntialias, shouldSmoothFonts);
161         gc->adjustTextRenderMode(&paint);
162         paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
163
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.
167             paint.setLooper(0);
168         }
169
170         gc->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, textRect, paint);
171     }
172     if (font->platformData().orientation() == Vertical)
173         gc->restore();
174 }
175
176 } // namespace WebCore