2 * Copyright (C) 2007 Kevin Ollivier. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
29 #include "FontFallbackList.h"
30 #include "GlyphBuffer.h"
31 #include "GraphicsContext.h"
33 #include "NotImplemented.h"
34 #include "SimpleFontData.h"
38 #include "UniscribeController.h"
39 typedef WebCore::UniscribeController ComplexTextController;
43 #include "mac/ComplexTextController.h"
46 #include <wx/dcclient.h>
47 #include "fontprops.h"
48 #include "non-kerned-drawing.h"
52 bool Font::canReturnFallbackFontsForComplexText()
54 #if OS(WINDOWS) || OS(DARWIN)
61 bool Font::canExpandAroundIdeographsInComplexText()
70 void Font::drawGlyphs(GraphicsContext* graphicsContext, const SimpleFontData* font, const GlyphBuffer& glyphBuffer,
71 int from, int numGlyphs, const FloatPoint& point) const
74 Color color = graphicsContext->fillColor();
76 // We can't use wx drawing methods on Win/Linux because they automatically kern text
77 // so we've created a function with platform dependent drawing implementations that
78 // will hopefully be folded into wx once the API has solidified.
79 // see platform/wx/wxcode/<platform> for the implementations.
80 graphicsContext->save();
81 drawTextWithSpacing(graphicsContext, font, color, glyphBuffer, from, numGlyphs, point);
82 graphicsContext->restore();
85 FloatRect Font::selectionRectForComplexText(const TextRun& run, const FloatPoint& point, int h, int from, int to) const
87 #if OS(WINDOWS) || OS(DARWIN)
88 ComplexTextController it(this, run);
90 float beforeWidth = it.runWidthSoFar();
92 float afterWidth = it.runWidthSoFar();
94 // Using roundf() rather than ceilf() for the right edge as a compromise to ensure correct caret positioning
97 it.advance(run.length());
98 float totalWidth = it.runWidthSoFar();
99 return FloatRect(point.x() + floorf(totalWidth - afterWidth), point.y(), roundf(totalWidth - beforeWidth) - floorf(totalWidth - afterWidth), h);
101 float totalWidth = it.totalWidth();
102 return FloatRect(point.x() + floorf(totalWidth - afterWidth), point.y(), roundf(totalWidth - beforeWidth) - floorf(totalWidth - afterWidth), h);
106 return FloatRect(point.x() + floorf(beforeWidth), point.y(), roundf(afterWidth) - floorf(beforeWidth), h);
113 float Font::getGlyphsAndAdvancesForComplexText(const TextRun& /* run */, int /* from */, int /* to */, GlyphBuffer& /* glyphBuffer */, ForTextEmphasisOrNot /* forTextEmphasis */) const
115 // FIXME: Implement this by moving most of the drawComplexText() implementation in here. Set up the
116 // ComplexTextController according to forTextEmphasis.
121 void Font::drawComplexText(GraphicsContext* context, const TextRun& run, const FloatPoint& point, int from, int to) const
123 #if OS(WINDOWS) || OS(DARWIN)
124 // This glyph buffer holds our glyphs + advances + font data for each glyph.
125 GlyphBuffer glyphBuffer;
127 float startX = point.x();
128 ComplexTextController controller(this, run);
129 controller.advance(from);
130 float beforeWidth = controller.runWidthSoFar();
131 controller.advance(to, &glyphBuffer);
133 // We couldn't generate any glyphs for the run. Give up.
134 if (glyphBuffer.isEmpty())
137 float afterWidth = controller.runWidthSoFar();
141 controller.advance(run.length());
142 startX += controller.runWidthSoFar() - afterWidth;
144 startX += controller.totalWidth() - afterWidth;
145 for (int i = 0, end = glyphBuffer.size() - 1; i < glyphBuffer.size() / 2; ++i, --end)
146 glyphBuffer.swap(i, end);
149 startX += beforeWidth;
151 // Draw the glyph buffer now at the starting point returned in startX.
152 FloatPoint startPoint(startX, point.y());
153 drawGlyphBuffer(context, run, glyphBuffer, startPoint);
159 void Font::drawEmphasisMarksForComplexText(GraphicsContext* context, const TextRun& run, const AtomicString& mark, const FloatPoint& point, int from, int to) const
161 GlyphBuffer glyphBuffer;
162 float initialAdvance = getGlyphsAndAdvancesForComplexText(run, from, to, glyphBuffer, ForTextEmphasis);
164 if (glyphBuffer.isEmpty())
167 drawEmphasisMarks(context, run, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y()));
170 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow*) const
172 #if OS(WINDOWS) || OS(DARWIN)
173 ComplexTextController controller(this, run, fallbackFonts);
175 controller.advance(run.length());
176 return controller.runWidthSoFar();
178 return controller.totalWidth();
186 int Font::offsetForPositionForComplexText(const TextRun& run, float x, bool includePartialGlyphs) const
188 #if OS(WINDOWS) || OS(DARWIN)
189 ComplexTextController controller(this, run);
190 return controller.offsetForPosition(x, includePartialGlyphs);