2 * Copyright (C) 2006, 2007 Apple Computer, Inc.
3 * Copyright (c) 2006, 2007, 2008, 2009, Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
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.
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"
48 bool Font::canReturnFallbackFontsForComplexText()
53 bool Font::canExpandAroundIdeographsInComplexText()
58 void Font::drawGlyphs(GraphicsContext* graphicsContext,
59 const SimpleFontData* font,
60 const GlyphBuffer& glyphBuffer,
63 const FloatPoint& point) const
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())
71 HFONT hfont = font->platformData().hfont();
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.
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.
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);
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);
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))
111 SkPoint origin = point;
112 origin.fX += SkFloatToScalar(horizontalOffset - point.x() - currentWidth);
113 paintSkiaText(graphicsContext, hfont, curLen, &glyphs[0], &advances[0], 0, &origin);
117 FloatRect Font::selectionRectForComplexText(const TextRun& run,
118 const FloatPoint& point,
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));
127 // If the text is RTL, left will actually be after right.
129 return FloatRect(left, point.y(),
130 right - left, static_cast<float>(h));
132 return FloatRect(right, point.y(),
133 left - right, static_cast<float>(h));
136 void Font::drawComplexText(GraphicsContext* graphicsContext,
138 const FloatPoint& point,
142 PlatformGraphicsContext* context = graphicsContext->platformContext();
143 UniscribeHelperTextRun state(run, *this);
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)
151 // Uniscribe counts the coordinates from the upper left, while WebKit uses
152 // the baseline, so we have to subtract off the ascent.
154 state.draw(graphicsContext, hdc, lroundf(point.x()), lroundf(point.y() - fontMetrics().ascent()), from, to);
157 void Font::drawEmphasisMarksForComplexText(GraphicsContext* /* context */, const TextRun& /* run */, const AtomicString& /* mark */, const FloatPoint& /* point */, int /* from */, int /* to */) const
162 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* /* fallbackFonts */, GlyphOverflow* /* glyphOverflow */) const
164 UniscribeHelperTextRun state(run, *this);
165 return static_cast<float>(state.width());
168 int Font::offsetForPositionForComplexText(const TextRun& run, float xFloat,
169 bool includePartialGlyphs) const
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);
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);
180 // XToCharacter will return -1 if the position is before the first
181 // character (we get called like this sometimes).
187 } // namespace WebCore