2 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. 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 INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "ComplexTextController.h"
29 #include "FontFallbackList.h"
30 #include "GlyphBuffer.h"
31 #include "GraphicsContext.h"
33 #include "SimpleFontData.h"
35 #include <wtf/MathExtras.h>
41 FloatRect Font::selectionRectForComplexText(const TextRun& run, const FloatPoint& point, int h,
42 int from, int to) const
44 ComplexTextController controller(this, run);
45 controller.advance(from);
46 float beforeWidth = controller.runWidthSoFar();
47 controller.advance(to);
48 float afterWidth = controller.runWidthSoFar();
50 // Using roundf() rather than ceilf() for the right edge as a compromise to ensure correct caret positioning
52 float totalWidth = controller.totalWidth();
53 return FloatRect(point.x() + floorf(totalWidth - afterWidth), point.y(), roundf(totalWidth - beforeWidth) - floorf(totalWidth - afterWidth), h);
56 return FloatRect(point.x() + floorf(beforeWidth), point.y(), roundf(afterWidth) - floorf(beforeWidth), h);
59 float Font::getGlyphsAndAdvancesForComplexText(const TextRun& run, int from, int to, GlyphBuffer& glyphBuffer, ForTextEmphasisOrNot forTextEmphasis) const
63 ComplexTextController controller(this, run, false, 0, forTextEmphasis);
64 controller.advance(from);
65 float beforeWidth = controller.runWidthSoFar();
66 controller.advance(to, &glyphBuffer);
68 if (glyphBuffer.isEmpty())
71 float afterWidth = controller.runWidthSoFar();
74 initialAdvance = controller.totalWidth() + controller.finalRoundingWidth() - afterWidth;
75 for (int i = 0, end = glyphBuffer.size() - 1; i < glyphBuffer.size() / 2; ++i, --end)
76 glyphBuffer.swap(i, end);
78 initialAdvance = beforeWidth;
80 return initialAdvance;
83 void Font::drawComplexText(GraphicsContext* context, const TextRun& run, const FloatPoint& point, int from, int to) const
85 // This glyph buffer holds our glyphs + advances + font data for each glyph.
86 GlyphBuffer glyphBuffer;
88 float startX = point.x() + getGlyphsAndAdvancesForComplexText(run, from, to, glyphBuffer);
90 // We couldn't generate any glyphs for the run. Give up.
91 if (glyphBuffer.isEmpty())
94 // Draw the glyph buffer now at the starting point returned in startX.
95 FloatPoint startPoint(startX, point.y());
96 drawGlyphBuffer(context, glyphBuffer, startPoint);
99 void Font::drawEmphasisMarksForComplexText(GraphicsContext* context, const TextRun& run, const AtomicString& mark, const FloatPoint& point, int from, int to) const
101 GlyphBuffer glyphBuffer;
102 float initialAdvance = getGlyphsAndAdvancesForComplexText(run, from, to, glyphBuffer, ForTextEmphasis);
104 if (glyphBuffer.isEmpty())
107 drawEmphasisMarks(context, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y()));
110 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
112 ComplexTextController controller(this, run, true, fallbackFonts);
114 glyphOverflow->top = max<int>(glyphOverflow->top, ceilf(-controller.minGlyphBoundingBoxY()) - fontMetrics().ascent());
115 glyphOverflow->bottom = max<int>(glyphOverflow->bottom, ceilf(controller.maxGlyphBoundingBoxY()) - fontMetrics().descent());
116 glyphOverflow->left = max<int>(0, ceilf(-controller.minGlyphBoundingBoxX()));
117 glyphOverflow->right = max<int>(0, ceilf(controller.maxGlyphBoundingBoxX() - controller.totalWidth()));
119 return controller.totalWidth();
122 int Font::offsetForPositionForComplexText(const TextRun& run, float x, bool includePartialGlyphs) const
124 ComplexTextController controller(this, run);
125 return controller.offsetForPosition(x, includePartialGlyphs);
128 } // namespace WebCore