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