2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebKitGraphics.h"
30 #include "WebKitDLL.h"
32 #include "WebPreferences.h"
34 #include <WebCore/Font.h>
35 #include <WebCore/FontCache.h>
36 #include <WebCore/FontDescription.h>
37 #include <WebCore/FontSelector.h>
38 #include <WebCore/GraphicsContext.h>
39 #include <WebCore/PlatformString.h>
40 #include <WebCore/StringTruncator.h>
41 #include <WebCore/WebCoreTextRenderer.h>
42 #include <wtf/unicode/CharacterNames.h>
44 #include <CoreGraphics/CoreGraphics.h>
46 #include <WebKitSystemInterface/WebKitSystemInterface.h>
48 using namespace WebCore;
50 static Font makeFont(const WebFontDescription& description)
54 String fontFamilyString(description.family, description.familyLength);
58 family.setFamily(fontFamilyString);
60 f.setSpecifiedSize(description.size);
61 f.setComputedSize(description.size);
62 f.setItalic(description.italic);
63 f.setWeight(description.bold ? FontWeightBold : FontWeightNormal);
64 f.setIsAbsoluteSize(true);
66 FontSmoothingType smoothingType;
67 if (SUCCEEDED(WebPreferences::sharedStandardPreferences()->fontSmoothing(&smoothingType)))
68 f.setRenderingMode(smoothingType == FontSmoothingTypeWindows ? AlternateRenderingMode : NormalRenderingMode);
76 // Text shadow is added post 3.1.1. In order for nightlies to not break Safari 3.1.1, we should still allow
77 // the old WebTextRenderInfo that has a smaller structSize than the current one with the new text shadow data members.
78 struct WebTextRenderInfoWithoutShadow
81 CGContextRef cgContext;
85 const WebFontDescription* description;
89 int overrideSmoothingLevel; // pass in -1 if caller does not want to override smoothing level
92 void WebDrawText(WebTextRenderInfo* info)
94 if (!info || info->structSize < sizeof(WebTextRenderInfoWithoutShadow) || !info->cgContext || !info->description)
97 int oldFontSmoothingLevel = -1;
98 if (info->overrideSmoothingLevel >= 0) {
99 oldFontSmoothingLevel = wkGetFontSmoothingLevel();
100 wkSetFontSmoothingLevel(info->overrideSmoothingLevel);
104 GraphicsContext context(info->cgContext);
105 String drawString(info->text, info->length);
106 if (info->drawAsPassword)
107 drawString.fill(WTF::Unicode::bullet);
111 // Set shadow setting
112 if (info->structSize == sizeof(WebTextRenderInfo) &&
113 (info->shadowOffset.cx || info->shadowOffset.cy || info->shadowBlur || info->shadowColor))
114 context.setShadow(FloatSize(info->shadowOffset.cx, info->shadowOffset.cy), info->shadowBlur, info->shadowColor, ColorSpaceDeviceRGB);
116 WebCoreDrawTextAtPoint(context, drawString, info->pt, makeFont(*(info->description)), info->color, info->underlinedIndex);
120 if (info->overrideSmoothingLevel >= 0)
121 wkSetFontSmoothingLevel(oldFontSmoothingLevel);
124 float TextFloatWidth(LPCTSTR text, int length, const WebFontDescription& description)
126 return WebCoreTextFloatWidth(String(text, length), makeFont(description));
129 void FontMetrics(const WebFontDescription& description, int* ascent, int* descent, int* lineSpacing)
131 if (!ascent && !descent && !lineSpacing)
134 Font font(makeFont(description));
135 const WebCore::FontMetrics& fontMetrics(font.fontMetrics());
138 *ascent = fontMetrics.ascent();
141 *descent = fontMetrics.descent();
144 *lineSpacing = fontMetrics.lineSpacing();
147 unsigned CenterTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
151 FontCachePurgePreventer fontCachePurgePreventer;
153 String result = StringTruncator::centerTruncate(String(text, length), width, makeFont(description), StringTruncator::EnableRoundingHacks);
154 memcpy(buffer, result.characters(), result.length() * sizeof(UChar));
155 buffer[result.length()] = '\0';
156 return result.length();
159 unsigned RightTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
163 FontCachePurgePreventer fontCachePurgePreventer;
165 String result = StringTruncator::rightTruncate(String(text, length), width, makeFont(description), StringTruncator::EnableRoundingHacks);
166 memcpy(buffer, result.characters(), result.length() * sizeof(UChar));
167 buffer[result.length()] = '\0';
168 return result.length();
171 void WebKitSetShouldUseFontSmoothing(bool smooth)
173 WebCoreSetShouldUseFontSmoothing(smooth);
176 bool WebKitShouldUseFontSmoothing()
178 return WebCoreShouldUseFontSmoothing();