Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Side by Side Diff: webkit/port/platform/graphics/chromium/FontChromiumWin.cpp

Issue 8615: Add support for transformed, stoked, shadowed, filled text. We do this by... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 #include <windows.h> 27 #include <windows.h>
28 28
29 #include "AffineTransform.h"
29 #include "ChromiumBridge.h" 30 #include "ChromiumBridge.h"
30 #include "Font.h" 31 #include "Font.h"
31 #include "FontFallbackList.h" 32 #include "FontFallbackList.h"
32 #include "GlyphBuffer.h" 33 #include "GlyphBuffer.h"
33 #include "PlatformContextSkia.h" 34 #include "PlatformContextSkia.h"
34 #include "SimpleFontData.h" 35 #include "SimpleFontData.h"
36 #include "SkiaFontWin.h"
35 #include "SkiaUtils.h" 37 #include "SkiaUtils.h"
36 #include "UniscribeHelperTextRun.h" 38 #include "UniscribeHelperTextRun.h"
37 39
38 #include "skia/ext/platform_canvas_win.h" 40 #include "skia/ext/platform_canvas_win.h"
39 #include "skia/ext/skia_utils_win.h" // TODO(brettw) remove this dependency. 41 #include "skia/ext/skia_utils_win.h" // TODO(brettw) remove this dependency.
40 42
41 namespace WebCore { 43 namespace WebCore {
42 44
45 static bool windowsCanHandleTextDrawing(GraphicsContext* context)
46 {
47 // Check for non-translation transforms.
48 const AffineTransform& xform = context->getCTM();
49 if (xform.b() != 0 || // Y skew
50 xform.c() != 0 || // X skew
51 xform.a() != 1.0 || // X scale
52 xform.d() != 1.0)
53 return false;
54
55 // Check for stroke effects.
56 if (context->platformContext()->getTextDrawingMode() != cTextFill)
57 return false;
58
59 // Check for shadow effects.
60 if (context->platformContext()->getDrawLooper())
M-A Ruel 2008/12/15 19:54:51 shadow should be ignored when printing on Windows.
61 return false;
62
63 return true;
64 }
65
66 static bool PaintSkiaText(PlatformContextSkia* platformContext,
67 HFONT hfont,
68 int numGlyphs,
69 const WORD* glyphs,
70 const int* advances,
71 const SkPoint& origin)
72 {
73 int textMode = platformContext->getTextDrawingMode();
74
75 // Filling (if necessary). This is the common case.
76 SkPaint paint;
77 platformContext->setupPaintForFilling(&paint);
78 paint.setFlags(SkPaint::kAntiAlias_Flag);
79 bool didFill = false;
80 if ((textMode & cTextFill) && SkColorGetA(paint.getColor())) {
81 if (!SkiaDrawText(hfont, platformContext->canvas(), origin, &paint,
82 &glyphs[0], &advances[0], numGlyphs))
83 return false;
84 didFill = true;
85 }
86
87 // Stroking on top (if necessary).
88 if ((textMode & WebCore::cTextStroke) &&
89 platformContext->getStrokeStyle() != NoStroke &&
90 platformContext->getStrokeThickness() > 0) {
91
92 paint.reset();
93 platformContext->setupPaintForStroking(&paint, NULL, 0);
94 paint.setFlags(SkPaint::kAntiAlias_Flag);
95 if (didFill) {
96 // If there is a shadow and we filled above, there will already be
97 // a shadow. We don't want to draw it again or it will be too dark
98 // and it will go on top of the fill.
99 //
100 // Note that this isn't strictly correct, since the stroke could be
101 // very thick and the shadow wouldn't account for this. The "right"
102 // thing would be to draw to a new layer and then draw that layer
103 // with a shadow. But this is a lot of extra work for something
104 // that isn't normally an issue.
105 paint.setLooper(NULL)->safeUnref();
106 }
107
108 if (!SkiaDrawText(hfont, platformContext->canvas(), origin,
109 &paint, &glyphs[0], &advances[0], numGlyphs))
110 return false;
111 }
112 return true;
113 }
114
43 void Font::drawGlyphs(GraphicsContext* graphicsContext, 115 void Font::drawGlyphs(GraphicsContext* graphicsContext,
44 const SimpleFontData* font, 116 const SimpleFontData* font,
45 const GlyphBuffer& glyphBuffer, 117 const GlyphBuffer& glyphBuffer,
46 int from, 118 int from,
47 int numGlyphs, 119 int numGlyphs,
48 const FloatPoint& point) const 120 const FloatPoint& point) const
49 { 121 {
50 PlatformGraphicsContext* context = graphicsContext->platformContext(); 122 PlatformGraphicsContext* context = graphicsContext->platformContext();
51 123
52 // Max buffer length passed to the underlying windows API. 124 // Max buffer length passed to the underlying windows API.
(...skipping 22 matching lines...) Expand all
75 // Windows needs the characters and the advances in nice contiguous 147 // Windows needs the characters and the advances in nice contiguous
76 // buffers, which we build here. 148 // buffers, which we build here.
77 Vector<WORD, kDefaultBufferLength> glyphs; 149 Vector<WORD, kDefaultBufferLength> glyphs;
78 Vector<int, kDefaultBufferLength> advances; 150 Vector<int, kDefaultBufferLength> advances;
79 151
80 // Compute the coordinate. The 'origin' represents the baseline, so we need 152 // Compute the coordinate. The 'origin' represents the baseline, so we need
81 // to move it up to the top of the bounding square. 153 // to move it up to the top of the bounding square.
82 int x = static_cast<int>(point.x()); 154 int x = static_cast<int>(point.x());
83 int lineTop = static_cast<int>(point.y()) - font->ascent(); 155 int lineTop = static_cast<int>(point.y()) - font->ascent();
84 156
157 bool canUseGDI = windowsCanHandleTextDrawing(graphicsContext);
M-A Ruel 2008/12/15 19:54:51 I wonder if canUseGDI should always be true when p
158
85 // We draw the glyphs in chunks to avoid having to do a heap allocation for 159 // We draw the glyphs in chunks to avoid having to do a heap allocation for
86 // the arrays of characters and advances. Since ExtTextOut is the 160 // the arrays of characters and advances. Since ExtTextOut is the
87 // lowest-level text output function on Windows, there should be little 161 // lowest-level text output function on Windows, there should be little
88 // penalty for splitting up the text. On the other hand, the buffer cannot 162 // penalty for splitting up the text. On the other hand, the buffer cannot
89 // be bigger than 4094 or the function will fail. 163 // be bigger than 4094 or the function will fail.
90 int glyphIndex = 0; 164 int glyphIndex = 0;
91 while (glyphIndex < numGlyphs) { 165 while (glyphIndex < numGlyphs) {
92 // how many chars will be in this chunk? 166 // how many chars will be in this chunk?
93 int curLen = std::min(kMaxBufferLength, numGlyphs - glyphIndex); 167 int curLen = std::min(kMaxBufferLength, numGlyphs - glyphIndex);
94 168
95 glyphs.resize(curLen); 169 glyphs.resize(curLen);
96 advances.resize(curLen); 170 advances.resize(curLen);
97 171
98 int curWidth = 0; 172 int curWidth = 0;
99 for (int i = 0; i < curLen; ++i, ++glyphIndex) { 173 for (int i = 0; i < curLen; ++i, ++glyphIndex) {
100 glyphs[i] = glyphBuffer.glyphAt(from + glyphIndex); 174 glyphs[i] = glyphBuffer.glyphAt(from + glyphIndex);
101 advances[i] = 175 advances[i] =
102 static_cast<int>(glyphBuffer.advanceAt(from + glyphIndex)); 176 static_cast<int>(glyphBuffer.advanceAt(from + glyphIndex));
103 curWidth += advances[i]; 177 curWidth += advances[i];
104 } 178 }
105 179
106 bool success = false; 180 bool success = false;
107 for (int executions = 0; executions < 2; ++executions) { 181 for (int executions = 0; executions < 2; ++executions) {
108 success = !!ExtTextOut(hdc, x, lineTop, ETO_GLYPH_INDEX, NULL, 182 if (canUseGDI) {
109 reinterpret_cast<const wchar_t*>(&glyphs[0]), 183 success = !!ExtTextOut(hdc, x, lineTop, ETO_GLYPH_INDEX, NULL,
110 curLen, 184 reinterpret_cast<const wchar_t*>(&glyphs[ 0]),
111 &advances[0]); 185 curLen, &advances[0]);
186 } else {
187 // Skia's text draing origin is the baseline, like WebKit, not
M-A Ruel 2008/12/15 19:54:51 drawing
188 // the top, like Windows.
189 SkPoint origin = { x, point.y() };
190 success = PaintSkiaText(context,
191 font->platformData().hfont(), numGlyphs,
192 reinterpret_cast<const WORD*>(&glyphs[0] ),
M-A Ruel 2008/12/15 19:54:51 80 cols, if you care.
193 &advances[0], origin);
194 }
112 195
113 if (!success && executions == 0) { 196 if (!success && executions == 0) {
114 // Ask the browser to load the font for us and retry. 197 // Ask the browser to load the font for us and retry.
115 ChromiumBridge::ensureFontLoaded(font->platformData().hfont()); 198 ChromiumBridge::ensureFontLoaded(font->platformData().hfont());
116 continue; 199 continue;
117 } 200 }
118 break; 201 break;
119 } 202 }
120 203
121 ASSERT(success); 204 ASSERT(success);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 int char_index = state.XToCharacter(x); 279 int char_index = state.XToCharacter(x);
197 280
198 // XToCharacter will return -1 if the position is before the first 281 // XToCharacter will return -1 if the position is before the first
199 // character (we get called like this sometimes). 282 // character (we get called like this sometimes).
200 if (char_index < 0) 283 if (char_index < 0)
201 char_index = 0; 284 char_index = 0;
202 return char_index; 285 return char_index;
203 } 286 }
204 287
205 } // namespace WebCore 288 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698