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