initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / chromium / FontChromiumWin.cpp
1 /*
2  * Copyright (C) 2006, 2007 Apple Computer, Inc.
3  * Copyright (c) 2006, 2007, 2008, 2009, Google Inc. All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  * 
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  * 
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "Font.h"
34
35 #include "FontFallbackList.h"
36 #include "GlyphBuffer.h"
37 #include "NotImplemented.h"
38 #include "PlatformSupport.h"
39 #include "PlatformContextSkia.h"
40 #include "SimpleFontData.h"
41 #include "SkiaFontWin.h"
42 #include "UniscribeHelperTextRun.h"
43
44 #include <windows.h>
45
46 namespace WebCore {
47
48 bool Font::canReturnFallbackFontsForComplexText()
49 {
50     return false;
51 }
52
53 bool Font::canExpandAroundIdeographsInComplexText()
54 {
55     return false;
56 }
57
58 void Font::drawGlyphs(GraphicsContext* graphicsContext,
59                       const SimpleFontData* font,
60                       const GlyphBuffer& glyphBuffer,
61                       int from,
62                       int numGlyphs,
63                       const FloatPoint& point) const
64 {
65     SkColor color = graphicsContext->platformContext()->effectiveFillColor();
66     unsigned char alpha = SkColorGetA(color);
67     // Skip 100% transparent text; no need to draw anything.
68     if (!alpha && graphicsContext->platformContext()->getStrokeStyle() == NoStroke && !graphicsContext->hasShadow())
69         return;
70
71     HFONT hfont = font->platformData().hfont();
72
73     // We draw the glyphs in chunks to avoid having to do a heap allocation for
74     // the arrays of characters and advances.
75     const int kMaxBufferLength = 256;
76     Vector<WORD, kMaxBufferLength> glyphs;
77     Vector<int, kMaxBufferLength> advances;
78     int glyphIndex = 0;  // The starting glyph of the current chunk.
79
80     // In order to round all offsets to the correct pixel boundary, this code keeps track of the absolute position
81     // of each glyph in floating point units and rounds to integer advances at the last possible moment.
82
83     float horizontalOffset = point.x(); // The floating point offset of the left side of the current glyph.
84     int lastHorizontalOffsetRounded = lroundf(horizontalOffset); // The rounded offset of the left side of the last glyph rendered.
85     while (glyphIndex < numGlyphs) {
86         // How many chars will be in this chunk?
87         int curLen = std::min(kMaxBufferLength, numGlyphs - glyphIndex);
88         glyphs.resize(curLen);
89         advances.resize(curLen);
90
91         float currentWidth = 0;
92         for (int i = 0; i < curLen; ++i, ++glyphIndex) {
93             glyphs[i] = glyphBuffer.glyphAt(from + glyphIndex);
94             horizontalOffset += glyphBuffer.advanceAt(from + glyphIndex);
95             advances[i] = lroundf(horizontalOffset) - lastHorizontalOffsetRounded;
96             lastHorizontalOffsetRounded += advances[i];
97             currentWidth += glyphBuffer.advanceAt(from + glyphIndex);
98             
99             // Bug 26088 - very large positive or negative runs can fail to
100             // render so we clamp the size here. In the specs, negative
101             // letter-spacing is implementation-defined, so this should be
102             // fine, and it matches Safari's implementation. The call actually
103             // seems to crash if kMaxNegativeRun is set to somewhere around
104             // -32830, so we give ourselves a little breathing room.
105             const int maxNegativeRun = -32768;
106             const int maxPositiveRun =  32768;
107             if ((currentWidth + advances[i] < maxNegativeRun) || (currentWidth + advances[i] > maxPositiveRun)) 
108                 advances[i] = 0;
109         }
110
111         SkPoint origin = point;
112         origin.fX += SkFloatToScalar(horizontalOffset - point.x() - currentWidth);
113         paintSkiaText(graphicsContext, hfont, curLen, &glyphs[0], &advances[0], 0, &origin);
114     }
115 }
116
117 FloatRect Font::selectionRectForComplexText(const TextRun& run,
118                                             const FloatPoint& point,
119                                             int h,
120                                             int from,
121                                             int to) const
122 {
123     UniscribeHelperTextRun state(run, *this);
124     float left = static_cast<float>(point.x() + state.characterToX(from));
125     float right = static_cast<float>(point.x() + state.characterToX(to));
126
127     // If the text is RTL, left will actually be after right.
128     if (left < right)
129         return FloatRect(left, point.y(),
130                        right - left, static_cast<float>(h));
131
132     return FloatRect(right, point.y(),
133                      left - right, static_cast<float>(h));
134 }
135
136 void Font::drawComplexText(GraphicsContext* graphicsContext,
137                            const TextRun& run,
138                            const FloatPoint& point,
139                            int from,
140                            int to) const
141 {
142     PlatformGraphicsContext* context = graphicsContext->platformContext();
143     UniscribeHelperTextRun state(run, *this);
144
145     SkColor color = graphicsContext->platformContext()->effectiveFillColor();
146     unsigned char alpha = SkColorGetA(color);
147     // Skip 100% transparent text; no need to draw anything.
148     if (!alpha && graphicsContext->platformContext()->getStrokeStyle() == NoStroke)
149         return;
150
151     // Uniscribe counts the coordinates from the upper left, while WebKit uses
152     // the baseline, so we have to subtract off the ascent.
153     HDC hdc = 0;
154     state.draw(graphicsContext, hdc, lroundf(point.x()), lroundf(point.y() - fontMetrics().ascent()), from, to);
155 }
156
157 void Font::drawEmphasisMarksForComplexText(GraphicsContext* /* context */, const TextRun& /* run */, const AtomicString& /* mark */, const FloatPoint& /* point */, int /* from */, int /* to */) const
158 {
159     notImplemented();
160 }
161
162 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* /* fallbackFonts */, GlyphOverflow* /* glyphOverflow */) const
163 {
164     UniscribeHelperTextRun state(run, *this);
165     return static_cast<float>(state.width());
166 }
167
168 int Font::offsetForPositionForComplexText(const TextRun& run, float xFloat,
169                                           bool includePartialGlyphs) const
170 {
171     // FIXME: This truncation is not a problem for HTML, but only affects SVG, which passes floating-point numbers
172     // to Font::offsetForPosition(). Bug http://webkit.org/b/40673 tracks fixing this problem.
173     int x = static_cast<int>(xFloat);
174
175     // Mac code ignores includePartialGlyphs, and they don't know what it's
176     // supposed to do, so we just ignore it as well.
177     UniscribeHelperTextRun state(run, *this);
178     int charIndex = state.xToCharacter(x);
179
180     // XToCharacter will return -1 if the position is before the first
181     // character (we get called like this sometimes).
182     if (charIndex < 0)
183         charIndex = 0;
184     return charIndex;
185 }
186
187 } // namespace WebCore