initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / skia / FontSkia.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 "Font.h"
33
34 #include "GlyphBuffer.h"
35 #include "GraphicsContext.h"
36 #include "PlatformContextSkia.h"
37 #include "SimpleFontData.h"
38
39 #include "SkCanvas.h"
40 #include "SkPaint.h"
41 #include "SkTypeface.h"
42 #include "SkTypeface_mac.h"
43
44 namespace WebCore {
45
46 bool Font::canReturnFallbackFontsForComplexText()
47 {
48     return true;
49 }
50
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()
55 {
56     return false;
57 }
58
59 static bool isCanvasMultiLayered(SkCanvas* canvas)
60 {
61     SkCanvas::LayerIter layerIterator(canvas, false);
62     layerIterator.next();
63     return !layerIterator.done();
64 }
65
66 static void adjustTextRenderMode(SkPaint* paint, PlatformContextSkia* skiaContext)
67 {
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);
74 }
75
76 static void setupPaint(SkPaint* paint, const SimpleFontData* fontData, const Font* font)
77 {
78     const FontPlatformData& platformData = fontData->platformData();
79     const float textSize = platformData.m_size >= 0 ? platformData.m_size : 12;
80
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);
91 }
92
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);
100
101     const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from);
102     SkScalar x = SkFloatToScalar(point.x());
103     SkScalar y = SkFloatToScalar(point.y());
104
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
110     // here.
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();
116
117     bool isVertical = font->platformData().orientation() == Vertical;
118     for (int i = 0; i < numGlyphs; i++) {
119         SkScalar myWidth = SkFloatToScalar(adv[i].width);
120         pos[i].set(x, y);
121         if (isVertical) {
122             vPosBegin[i].set(x + myWidth, y);
123             vPosEnd[i].set(x + myWidth, y - myWidth);
124         }
125         x += myWidth;
126         y += SkFloatToScalar(adv[i].height);
127     }
128
129     SkCanvas* canvas = gc->platformContext()->canvas();
130     TextDrawingModeFlags textMode = gc->platformContext()->getTextDrawingMode();
131
132     // We draw text up to two times (once for fill, once for stroke).
133     if (textMode & TextModeFill) {
134         SkPaint paint;
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());
140
141         if (isVertical) {
142             SkPath path;
143             for (int i = 0; i < numGlyphs; ++i) {
144                 path.reset();
145                 path.moveTo(vPosBegin[i]);
146                 path.lineTo(vPosEnd[i]);
147                 canvas->drawTextOnPath(glyphs + i, sizeof(uint16_t), path, 0, paint);
148             }
149         } else
150             canvas->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, paint);
151     }
152
153     if ((textMode & TextModeStroke)
154         && gc->platformContext()->getStrokeStyle() != NoStroke
155         && gc->platformContext()->getStrokeThickness() > 0) {
156
157         SkPaint paint;
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());
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         if (isVertical) {
171             SkPath path;
172             for (int i = 0; i < numGlyphs; ++i) {
173                 path.reset();
174                 path.moveTo(vPosBegin[i]);
175                 path.lineTo(vPosEnd[i]);
176                 canvas->drawTextOnPath(glyphs + i, sizeof(uint16_t), path, 0, paint);
177             }
178         } else
179             canvas->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, paint);
180     }
181 }
182
183 } // namespace WebCore