OLD | NEW |
| (Empty) |
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 "core/platform/graphics/Font.h" | |
34 | |
35 #include "core/platform/graphics/FontFallbackList.h" | |
36 #include "core/platform/graphics/GraphicsContext.h" | |
37 #include "core/platform/graphics/SimpleFontData.h" | |
38 #include "core/platform/graphics/skia/SkiaFontWin.h" | |
39 #include "core/platform/graphics/win/FontPlatformDataWin.h" | |
40 #include "core/platform/graphics/win/UniscribeHelperTextRun.h" | |
41 #include "platform/fonts/GlyphBuffer.h" | |
42 #include "platform/NotImplemented.h" | |
43 | |
44 #include <windows.h> | |
45 | |
46 using namespace std; | |
47 | |
48 namespace WebCore { | |
49 | |
50 bool Font::canReturnFallbackFontsForComplexText() | |
51 { | |
52 return false; | |
53 } | |
54 | |
55 bool Font::canExpandAroundIdeographsInComplexText() | |
56 { | |
57 return false; | |
58 } | |
59 | |
60 void Font::drawGlyphs(GraphicsContext* graphicsContext, | |
61 const SimpleFontData* font, const GlyphBuffer& glyphBuffer, | |
62 unsigned from, unsigned numGlyphs, const FloatPoint& point, | |
63 const FloatRect& textRect) const | |
64 { | |
65 SkColor color = graphicsContext->effectiveFillColor(); | |
66 unsigned char alpha = SkColorGetA(color); | |
67 // Skip 100% transparent text; no need to draw anything. | |
68 if (!alpha && graphicsContext->strokeStyle() == NoStroke && !graphicsContext
->hasShadow()) | |
69 return; | |
70 | |
71 // We draw the glyphs in chunks to avoid having to do a heap allocation for | |
72 // the arrays of characters and advances. | |
73 const unsigned kMaxBufferLength = 256; | |
74 Vector<int, kMaxBufferLength> advances; | |
75 unsigned glyphIndex = 0; // The starting glyph of the current chunk. | |
76 | |
77 float horizontalOffset = point.x(); // The floating point offset of the left
side of the current glyph. | |
78 | |
79 #if ENABLE(OPENTYPE_VERTICAL) | |
80 const OpenTypeVerticalData* verticalData = font->verticalData(); | |
81 if (verticalData) { | |
82 Vector<FloatPoint, kMaxBufferLength> translations; | |
83 Vector<GOFFSET, kMaxBufferLength> offsets; | |
84 | |
85 // Skia doesn't have matrix for glyph coordinate space, so we rotate bac
k the CTM. | |
86 AffineTransform savedMatrix = graphicsContext->getCTM(); | |
87 graphicsContext->concatCTM(AffineTransform(0, -1, 1, 0, point.x(), point
.y())); | |
88 graphicsContext->concatCTM(AffineTransform(1, 0, 0, 1, -point.x(), -poin
t.y())); | |
89 | |
90 const FontMetrics& metrics = font->fontMetrics(); | |
91 SkScalar verticalOriginX = SkFloatToScalar(point.x() + metrics.floatAsce
nt() - metrics.floatAscent(IdeographicBaseline)); | |
92 while (glyphIndex < numGlyphs) { | |
93 // How many chars will be in this chunk? | |
94 unsigned curLen = std::min(kMaxBufferLength, numGlyphs - glyphIndex)
; | |
95 | |
96 const Glyph* glyphs = glyphBuffer.glyphs(from + glyphIndex); | |
97 translations.resize(curLen); | |
98 verticalData->getVerticalTranslationsForGlyphs(font, &glyphs[0], cur
Len, reinterpret_cast<float*>(&translations[0])); | |
99 // To position glyphs vertically, we use offsets instead of advances
. | |
100 advances.resize(curLen); | |
101 advances.fill(0); | |
102 offsets.resize(curLen); | |
103 float currentWidth = 0; | |
104 for (unsigned i = 0; i < curLen; ++i, ++glyphIndex) { | |
105 offsets[i].du = lroundf(translations[i].x()); | |
106 offsets[i].dv = -lroundf(currentWidth - translations[i].y()); | |
107 currentWidth += glyphBuffer.advanceAt(from + glyphIndex); | |
108 } | |
109 SkPoint origin; | |
110 origin.set(verticalOriginX, SkFloatToScalar(point.y() + horizontalOf
fset - point.x())); | |
111 horizontalOffset += currentWidth; | |
112 paintSkiaText(graphicsContext, font->platformData(), curLen, &glyphs
[0], &advances[0], &offsets[0], origin, SkRect(textRect)); | |
113 } | |
114 | |
115 graphicsContext->setCTM(savedMatrix); | |
116 return; | |
117 } | |
118 #endif | |
119 | |
120 // In order to round all offsets to the correct pixel boundary, this code ke
eps track of the absolute position | |
121 // of each glyph in floating point units and rounds to integer advances at t
he last possible moment. | |
122 | |
123 int lastHorizontalOffsetRounded = lroundf(horizontalOffset); // The rounded
offset of the left side of the last glyph rendered. | |
124 Vector<WORD, kMaxBufferLength> glyphs; | |
125 while (glyphIndex < numGlyphs) { | |
126 // How many chars will be in this chunk? | |
127 unsigned curLen = std::min(kMaxBufferLength, numGlyphs - glyphIndex); | |
128 glyphs.resize(curLen); | |
129 advances.resize(curLen); | |
130 | |
131 float currentWidth = 0; | |
132 for (unsigned i = 0; i < curLen; ++i, ++glyphIndex) { | |
133 glyphs[i] = glyphBuffer.glyphAt(from + glyphIndex); | |
134 horizontalOffset += glyphBuffer.advanceAt(from + glyphIndex); | |
135 advances[i] = lroundf(horizontalOffset) - lastHorizontalOffsetRounde
d; | |
136 lastHorizontalOffsetRounded += advances[i]; | |
137 currentWidth += glyphBuffer.advanceAt(from + glyphIndex); | |
138 | |
139 // Bug 26088 - very large positive or negative runs can fail to | |
140 // render so we clamp the size here. In the specs, negative | |
141 // letter-spacing is implementation-defined, so this should be | |
142 // fine, and it matches Safari's implementation. The call actually | |
143 // seems to crash if kMaxNegativeRun is set to somewhere around | |
144 // -32830, so we give ourselves a little breathing room. | |
145 const int maxNegativeRun = -32768; | |
146 const int maxPositiveRun = 32768; | |
147 if ((currentWidth + advances[i] < maxNegativeRun) || (currentWidth +
advances[i] > maxPositiveRun)) | |
148 advances[i] = 0; | |
149 } | |
150 | |
151 SkPoint origin = point; | |
152 origin.fX += SkFloatToScalar(horizontalOffset - point.x() - currentWidth
); | |
153 paintSkiaText(graphicsContext, font->platformData(), curLen, &glyphs[0],
&advances[0], 0, origin, SkRect(textRect)); | |
154 } | |
155 } | |
156 | |
157 FloatRect Font::selectionRectForComplexText(const TextRun& run, | |
158 const FloatPoint& point, | |
159 int h, | |
160 int from, | |
161 int to) const | |
162 { | |
163 UniscribeHelperTextRun state(run, *this); | |
164 float left = static_cast<float>(point.x() + state.characterToX(from)); | |
165 float right = static_cast<float>(point.x() + state.characterToX(to)); | |
166 | |
167 // If the text is RTL, left will actually be after right. | |
168 if (left < right) | |
169 return FloatRect(left, point.y(), | |
170 right - left, static_cast<float>(h)); | |
171 | |
172 return FloatRect(right, point.y(), | |
173 left - right, static_cast<float>(h)); | |
174 } | |
175 | |
176 void Font::drawComplexText(GraphicsContext* graphicsContext, | |
177 const TextRunPaintInfo& runInfo, | |
178 const FloatPoint& point) const | |
179 { | |
180 UniscribeHelperTextRun state(runInfo.run, *this); | |
181 | |
182 SkColor color = graphicsContext->effectiveFillColor(); | |
183 unsigned char alpha = SkColorGetA(color); | |
184 // Skip 100% transparent text; no need to draw anything. | |
185 if (!alpha && graphicsContext->strokeStyle() == NoStroke) | |
186 return; | |
187 | |
188 HDC hdc = 0; | |
189 // Uniscribe counts the coordinates from the upper left, while WebKit uses | |
190 // the baseline, so we have to subtract off the ascent. | |
191 state.draw(graphicsContext, primaryFont()->platformData(), hdc, lroundf(poin
t.x()), lroundf(point.y() - fontMetrics().ascent()), runInfo.bounds, runInfo.fro
m, runInfo.to); | |
192 } | |
193 | |
194 void Font::drawEmphasisMarksForComplexText(GraphicsContext* /* context */, const
TextRunPaintInfo& /* runInfo */, const AtomicString& /* mark */, const FloatPoi
nt& /* point */) const | |
195 { | |
196 notImplemented(); | |
197 } | |
198 | |
199 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFon
tData*>* /* fallbackFonts */, GlyphOverflow* /* glyphOverflow */) const | |
200 { | |
201 UniscribeHelperTextRun state(run, *this); | |
202 return static_cast<float>(state.width()); | |
203 } | |
204 | |
205 int Font::offsetForPositionForComplexText(const TextRun& run, float xFloat, | |
206 bool includePartialGlyphs) const | |
207 { | |
208 // FIXME: This truncation is not a problem for HTML, but only affects SVG, w
hich passes floating-point numbers | |
209 // to Font::offsetForPosition(). Bug http://webkit.org/b/40673 tracks fixing
this problem. | |
210 int x = static_cast<int>(xFloat); | |
211 | |
212 // Mac code ignores includePartialGlyphs, and they don't know what it's | |
213 // supposed to do, so we just ignore it as well. | |
214 UniscribeHelperTextRun state(run, *this); | |
215 int charIndex = state.xToCharacter(x); | |
216 | |
217 // XToCharacter will return -1 if the position is before the first | |
218 // character (we get called like this sometimes). | |
219 if (charIndex < 0) | |
220 charIndex = 0; | |
221 return charIndex; | |
222 } | |
223 | |
224 } // namespace WebCore | |
OLD | NEW |