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