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
26#include "config.h"
27#include "WebKitGraphics.h"
28
29#include "WebKit.h"
30#include "WebKitDLL.h"
31
32#include "WebPreferences.h"
33
34#include <WebCore/Font.h>
35#include <WebCore/FontDescription.h>
36#include <WebCore/FontSelector.h>
37#include <WebCore/GraphicsContext.h>
38#include <WebCore/PlatformString.h>
39#include <WebCore/StringTruncator.h>
40#include <WebCore/WebCoreTextRenderer.h>
41#include <wtf/unicode/CharacterNames.h>
42
43#include <CoreGraphics/CoreGraphics.h>
44
45#include <WebKitSystemInterface/WebKitSystemInterface.h>
46
47using namespace WebCore;
48
49static Font makeFont(const WebFontDescription& description)
50{
51    AtomicString::init();
52
53    String fontFamilyString(description.family, description.familyLength);
54
55    FontDescription f;
56    FontFamily family;
57    family.setFamily(fontFamilyString);
58    f.setFamily(family);
59    f.setSpecifiedSize(description.size);
60    f.setComputedSize(description.size);
61    f.setItalic(description.italic);
62    f.setWeight(description.bold ? FontWeightBold : FontWeightNormal);
63    f.setIsAbsoluteSize(true);
64
65    FontSmoothingType smoothingType;
66    if (SUCCEEDED(WebPreferences::sharedStandardPreferences()->fontSmoothing(&smoothingType)))
67        f.setRenderingMode(smoothingType == FontSmoothingTypeWindows ? AlternateRenderingMode : NormalRenderingMode);
68
69    Font font(f, 0, 0);
70    font.update(0);
71
72    return font;
73}
74
75// Text shadow is added post 3.1.1.  In order for nightlies to not break Safari 3.1.1, we should still allow
76// the old WebTextRenderInfo that has a smaller structSize than the current one with the new text shadow data members.
77struct WebTextRenderInfoWithoutShadow
78{
79    DWORD structSize;
80    CGContextRef cgContext;
81    LPCTSTR text;
82    int length;
83    POINT pt;
84    const WebFontDescription* description;
85    CGColorRef color;
86    int underlinedIndex;
87    bool drawAsPassword;
88    int overrideSmoothingLevel; // pass in -1 if caller does not want to override smoothing level
89};
90
91void WebDrawText(WebTextRenderInfo* info)
92{
93    if (!info || info->structSize < sizeof(WebTextRenderInfoWithoutShadow) || !info->cgContext || !info->description)
94        return;
95
96    int oldFontSmoothingLevel = -1;
97    if (info->overrideSmoothingLevel >= 0) {
98        oldFontSmoothingLevel = wkGetFontSmoothingLevel();
99        wkSetFontSmoothingLevel(info->overrideSmoothingLevel);
100    }
101
102    {
103        GraphicsContext context(info->cgContext);
104        String drawString(info->text, info->length);
105        if (info->drawAsPassword)
106            drawString = drawString.impl()->secure(WTF::Unicode::bullet);
107
108        context.save();
109
110        // Set shadow setting
111        if (info->structSize == sizeof(WebTextRenderInfo) &&
112            (info->shadowOffset.cx || info->shadowOffset.cy || info->shadowBlur || info->shadowColor))
113            context.setShadow(FloatSize(info->shadowOffset.cx, info->shadowOffset.cy), info->shadowBlur, info->shadowColor, ColorSpaceDeviceRGB);
114
115        WebCoreDrawTextAtPoint(context, drawString, info->pt, makeFont(*(info->description)), info->color, info->underlinedIndex);
116        context.restore();
117    }
118
119    if (info->overrideSmoothingLevel >= 0)
120        wkSetFontSmoothingLevel(oldFontSmoothingLevel);
121}
122
123float TextFloatWidth(LPCTSTR text, int length, const WebFontDescription& description)
124{
125    return WebCoreTextFloatWidth(String(text, length), makeFont(description));
126}
127
128void FontMetrics(const WebFontDescription& description, int* ascent, int* descent, int* lineSpacing)
129{
130    if (!ascent && !descent && !lineSpacing)
131        return;
132
133    Font font(makeFont(description));
134    const WebCore::FontMetrics& fontMetrics(font.fontMetrics());
135
136    if (ascent)
137        *ascent = fontMetrics.ascent();
138
139    if (descent)
140        *descent = fontMetrics.descent();
141
142    if (lineSpacing)
143        *lineSpacing = fontMetrics.lineSpacing();
144}
145
146unsigned CenterTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
147{
148    ASSERT(buffer);
149
150    String result = StringTruncator::centerTruncate(String(text, length), width, makeFont(description));
151    memcpy(buffer, result.characters(), result.length() * sizeof(UChar));
152    buffer[result.length()] = '\0';
153    return result.length();
154}
155
156unsigned RightTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
157{
158    ASSERT(buffer);
159
160    String result = StringTruncator::rightTruncate(String(text, length), width, makeFont(description));
161    memcpy(buffer, result.characters(), result.length() * sizeof(UChar));
162    buffer[result.length()] = '\0';
163    return result.length();
164}
165
166void WebKitSetShouldUseFontSmoothing(bool smooth)
167{
168    WebCoreSetShouldUseFontSmoothing(smooth);
169}
170
171bool WebKitShouldUseFontSmoothing()
172{
173    return WebCoreShouldUseFontSmoothing();
174}
175