1 /*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #include "config.h"
27 #include "Font.h"
28
29 #if USE(CORE_TEXT)
30
31 #include "CoreTextController.h"
32 #include "FontFallbackList.h"
33 #include "GlyphBuffer.h"
34 #include "GraphicsContext.h"
35 #include "IntRect.h"
36 #include "SimpleFontData.h"
37 #include <wtf/MathExtras.h>
38
39 namespace WebCore {
40
selectionRectForComplexText(const TextRun & run,const IntPoint & point,int h,int from,int to) const41 FloatRect Font::selectionRectForComplexText(const TextRun& run, const IntPoint& point, int h,
42 int from, int to) const
43 {
44 CoreTextController controller(this, run);
45 controller.advance(from);
46 float beforeWidth = controller.runWidthSoFar();
47 controller.advance(to);
48 float afterWidth = controller.runWidthSoFar();
49
50 // Using roundf() rather than ceilf() for the right edge as a compromise to ensure correct caret positioning
51 if (run.rtl()) {
52 float totalWidth = controller.totalWidth();
53 return FloatRect(point.x() + floorf(totalWidth - afterWidth), point.y(), roundf(totalWidth - beforeWidth) - floorf(totalWidth - afterWidth), h);
54 }
55
56 return FloatRect(point.x() + floorf(beforeWidth), point.y(), roundf(afterWidth) - floorf(beforeWidth), h);
57 }
58
drawComplexText(GraphicsContext * context,const TextRun & run,const FloatPoint & point,int from,int to) const59 void Font::drawComplexText(GraphicsContext* context, const TextRun& run, const FloatPoint& point,
60 int from, int to) const
61 {
62 // This glyph buffer holds our glyphs + advances + font data for each glyph.
63 GlyphBuffer glyphBuffer;
64
65 float startX = point.x();
66 CoreTextController controller(this, run);
67 controller.advance(from);
68 float beforeWidth = controller.runWidthSoFar();
69 controller.advance(to, &glyphBuffer);
70
71 // We couldn't generate any glyphs for the run. Give up.
72 if (glyphBuffer.isEmpty())
73 return;
74
75 float afterWidth = controller.runWidthSoFar();
76
77 if (run.rtl()) {
78 startX += controller.totalWidth() + controller.finalRoundingWidth() - afterWidth;
79 for (int i = 0, end = glyphBuffer.size() - 1; i < glyphBuffer.size() / 2; ++i, --end)
80 glyphBuffer.swap(i, end);
81 } else
82 startX += beforeWidth;
83
84 // Draw the glyph buffer now at the starting point returned in startX.
85 FloatPoint startPoint(startX, point.y());
86 drawGlyphBuffer(context, glyphBuffer, run, startPoint);
87 }
88
floatWidthForComplexText(const TextRun & run,HashSet<const SimpleFontData * > * fallbackFonts) const89 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts) const
90 {
91 CoreTextController controller(this, run, true, fallbackFonts);
92 return controller.totalWidth();
93 }
94
offsetForPositionForComplexText(const TextRun & run,int x,bool includePartialGlyphs) const95 int Font::offsetForPositionForComplexText(const TextRun& run, int x, bool includePartialGlyphs) const
96 {
97 CoreTextController controller(this, run);
98 return controller.offsetForPosition(x, includePartialGlyphs);
99 }
100
101 }
102 #endif // USE(CORE_TEXT)
103