[Release] Webkit2-efl-123997_0.11.86
[framework/web/webkit-efl.git] / Source / WebKit / win / WebKitGraphics.cpp
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/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>
43
44 #include <CoreGraphics/CoreGraphics.h>
45
46 #include <WebKitSystemInterface/WebKitSystemInterface.h>
47
48 using namespace WebCore;
49
50 static Font makeFont(const WebFontDescription& description)
51 {
52     AtomicString::init();
53
54     String fontFamilyString(description.family, description.familyLength);
55
56     FontDescription f;
57     FontFamily family;
58     family.setFamily(fontFamilyString);
59     f.setFamily(family);
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);
65
66     FontSmoothingType smoothingType;
67     if (SUCCEEDED(WebPreferences::sharedStandardPreferences()->fontSmoothing(&smoothingType)))
68         f.setRenderingMode(smoothingType == FontSmoothingTypeWindows ? AlternateRenderingMode : NormalRenderingMode);
69
70     Font font(f, 0, 0);
71     font.update(0);
72
73     return font;
74 }
75
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
79 {
80     DWORD structSize;
81     CGContextRef cgContext;
82     LPCTSTR text;
83     int length;
84     POINT pt;
85     const WebFontDescription* description;
86     CGColorRef color;
87     int underlinedIndex;
88     bool drawAsPassword;
89     int overrideSmoothingLevel; // pass in -1 if caller does not want to override smoothing level
90 };
91
92 void WebDrawText(WebTextRenderInfo* info)
93 {
94     if (!info || info->structSize < sizeof(WebTextRenderInfoWithoutShadow) || !info->cgContext || !info->description)
95         return;
96
97     int oldFontSmoothingLevel = -1;
98     if (info->overrideSmoothingLevel >= 0) {
99         oldFontSmoothingLevel = wkGetFontSmoothingLevel();
100         wkSetFontSmoothingLevel(info->overrideSmoothingLevel);
101     }
102
103     {
104         GraphicsContext context(info->cgContext);
105         String drawString(info->text, info->length);
106         if (info->drawAsPassword)
107             drawString.fill(WTF::Unicode::bullet);
108
109         context.save();
110
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);
115
116         WebCoreDrawTextAtPoint(context, drawString, info->pt, makeFont(*(info->description)), info->color, info->underlinedIndex);
117         context.restore();
118     }
119
120     if (info->overrideSmoothingLevel >= 0)
121         wkSetFontSmoothingLevel(oldFontSmoothingLevel);
122 }
123
124 float TextFloatWidth(LPCTSTR text, int length, const WebFontDescription& description)
125 {
126     return WebCoreTextFloatWidth(String(text, length), makeFont(description));
127 }
128
129 void FontMetrics(const WebFontDescription& description, int* ascent, int* descent, int* lineSpacing)
130 {
131     if (!ascent && !descent && !lineSpacing)
132         return;
133
134     Font font(makeFont(description));
135     const WebCore::FontMetrics& fontMetrics(font.fontMetrics());
136
137     if (ascent)
138         *ascent = fontMetrics.ascent();
139
140     if (descent)
141         *descent = fontMetrics.descent();
142
143     if (lineSpacing)
144         *lineSpacing = fontMetrics.lineSpacing();
145 }
146
147 unsigned CenterTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
148 {
149     ASSERT(buffer);
150
151     FontCachePurgePreventer fontCachePurgePreventer;
152
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();
157 }
158
159 unsigned RightTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
160 {
161     ASSERT(buffer);
162
163     FontCachePurgePreventer fontCachePurgePreventer;
164
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();
169 }
170
171 void WebKitSetShouldUseFontSmoothing(bool smooth)
172 {
173     WebCoreSetShouldUseFontSmoothing(smooth);
174 }
175
176 bool WebKitShouldUseFontSmoothing()
177 {
178     return WebCoreShouldUseFontSmoothing();
179 }