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