OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2007 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 #include "config.h" | |
26 #include "WebCoreTextRenderer.h" | |
27 | |
28 #include "Font.h" | |
29 #include "FontCache.h" | |
30 #include "FontDescription.h" | |
31 #include "GraphicsContext.h" | |
32 #include "StringTruncator.h" | |
33 #include "TextRun.h" | |
34 #include <wtf/unicode/Unicode.h> | |
35 | |
36 namespace WebCore { | |
37 | |
38 static bool shouldUseFontSmoothing = true; | |
39 | |
40 static bool isOneLeftToRightRun(const TextRun& run) | |
41 { | |
42 for (int i = 0; i < run.length(); i++) { | |
43 WTF::Unicode::Direction direction = WTF::Unicode::direction(run[i]); | |
44 if (direction == WTF::Unicode::RightToLeft || direction > WTF::Unicode::
OtherNeutral) | |
45 return false; | |
46 } | |
47 return true; | |
48 } | |
49 | |
50 static void doDrawTextAtPoint(GraphicsContext& context, const String& text, cons
t IntPoint& point, const Font& font, const Color& color, int underlinedIndex) | |
51 { | |
52 FontCachePurgePreventer fontCachePurgePreventer; | |
53 | |
54 TextRun run(text.characters(), text.length()); | |
55 | |
56 context.setFillColor(color, ColorSpaceDeviceRGB); | |
57 if (isOneLeftToRightRun(run)) | |
58 font.drawText(&context, run, point); | |
59 else | |
60 context.drawBidiText(font, run, point); | |
61 | |
62 if (underlinedIndex >= 0) { | |
63 ASSERT_WITH_SECURITY_IMPLICATION(underlinedIndex < static_cast<int>(text
.length())); | |
64 | |
65 int beforeWidth; | |
66 if (underlinedIndex > 0) { | |
67 TextRun beforeRun(text.characters(), underlinedIndex); | |
68 beforeWidth = font.width(beforeRun); | |
69 } else | |
70 beforeWidth = 0; | |
71 | |
72 TextRun underlinedRun(text.characters() + underlinedIndex, 1); | |
73 int underlinedWidth = font.width(underlinedRun); | |
74 | |
75 IntPoint underlinePoint(point); | |
76 underlinePoint.move(beforeWidth, 1); | |
77 | |
78 context.setStrokeColor(color, ColorSpaceDeviceRGB); | |
79 context.drawLineForText(underlinePoint, underlinedWidth, false); | |
80 } | |
81 } | |
82 | |
83 void WebCoreDrawTextAtPoint(GraphicsContext& context, const String& text, const
IntPoint& point, const Font& font, const Color& color, int underlinedIndex) | |
84 { | |
85 context.save(); | |
86 | |
87 doDrawTextAtPoint(context, text, point, font, color, underlinedIndex); | |
88 | |
89 context.restore(); | |
90 } | |
91 | |
92 void WebCoreDrawDoubledTextAtPoint(GraphicsContext& context, const String& text,
const IntPoint& point, const Font& font, const Color& topColor, const Color& bo
ttomColor, int underlinedIndex) | |
93 { | |
94 context.save(); | |
95 | |
96 IntPoint textPos = point; | |
97 | |
98 doDrawTextAtPoint(context, text, textPos, font, bottomColor, underlinedIndex
); | |
99 textPos.move(0, -1); | |
100 doDrawTextAtPoint(context, text, textPos, font, topColor, underlinedIndex); | |
101 | |
102 context.restore(); | |
103 } | |
104 | |
105 float WebCoreTextFloatWidth(const String& text, const Font& font) | |
106 { | |
107 FontCachePurgePreventer fontCachePurgePreventer; | |
108 | |
109 return StringTruncator::width(text, font, StringTruncator::EnableRoundingHac
ks); | |
110 } | |
111 | |
112 void WebCoreSetShouldUseFontSmoothing(bool smooth) | |
113 { | |
114 shouldUseFontSmoothing = smooth; | |
115 } | |
116 | |
117 bool WebCoreShouldUseFontSmoothing() | |
118 { | |
119 return shouldUseFontSmoothing; | |
120 } | |
121 | |
122 void WebCoreSetAlwaysUsesComplexTextCodePath(bool complex) | |
123 { | |
124 Font::setCodePath(complex ? Font::Complex : Font::Auto); | |
125 } | |
126 | |
127 bool WebCoreAlwaysUsesComplexTextCodePath() | |
128 { | |
129 return Font::codePath() == Font::Complex; | |
130 } | |
131 | |
132 } // namespace WebCore | |
OLD | NEW |