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 #pragma warning(push, 0)
33 #include <WebCore/CharacterNames.h>
34 #include <WebCore/Font.h>
35 #include <WebCore/FontDatabase.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>
43 #include <CoreGraphics/CoreGraphics.h>
46 #include <WebKitSystemInterface/WebKitSystemInterface.h>
48 using namespace WebCore;
50 static Font makeFont(const WebFontDescription& description)
53 populateFontDatabase();
55 String fontFamilyString(description.family, description.familyLength);
59 family.setFamily(fontFamilyString);
61 f.setSpecifiedSize(description.size);
62 f.setComputedSize(description.size);
63 f.setItalic(description.italic);
64 f.setBold(description.bold);
65 f.setIsAbsoluteSize(true);
73 void DrawTextAtPoint(CGContextRef cgContext, LPCTSTR text, int length, POINT point, const WebFontDescription& description, CGColorRef color, int underlinedIndex, bool drawAsPassword)
75 GraphicsContext context(cgContext);
77 ASSERT(CGColorGetNumberOfComponents(color) == 4); // this code assumes the CGColorRef has 4 components
78 const CGFloat* components = CGColorGetComponents(color);
79 Color textColor((int)(components[0] * 255), (int)(components[1] * 255), (int)(components[2] * 255), (int)(components[3] * 255));
81 String drawString(text, length);
83 drawString = drawString.impl()->secure(WebCore::bullet);
84 WebCoreDrawTextAtPoint(context, drawString, point, makeFont(description), textColor, underlinedIndex);
87 void WebDrawText(WebTextRenderInfo* info)
89 if (!info || info->structSize != sizeof(WebTextRenderInfo) || !info->cgContext || !info->description)
92 int oldFontSmoothingLevel = -1;
93 if (info->overrideSmoothingLevel >= 0) {
94 oldFontSmoothingLevel = wkGetFontSmoothingLevel();
95 wkSetFontSmoothingLevel(info->overrideSmoothingLevel);
98 DrawTextAtPoint(info->cgContext, info->text, info->length, info->pt, *(info->description), info->color, info->underlinedIndex, info->drawAsPassword);
100 if (info->overrideSmoothingLevel >= 0)
101 wkSetFontSmoothingLevel(oldFontSmoothingLevel);
104 float TextFloatWidth(LPCTSTR text, int length, const WebFontDescription& description)
106 return WebCoreTextFloatWidth(String(text, length), makeFont(description));
109 void FontMetrics(const WebFontDescription& description, int* ascent, int* descent, int* lineSpacing)
111 if (!ascent && !descent && !lineSpacing)
114 Font font(makeFont(description));
117 *ascent = font.ascent();
120 *descent = font.descent();
123 *lineSpacing = font.lineSpacing();
126 void CenterTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
130 String result = StringTruncator::centerTruncate(String(text, length), width, makeFont(description), false);
131 memcpy(buffer, result.characters(), result.length() * sizeof(UChar));
132 buffer[result.length()] = '\0';
135 void RightTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
139 String result = StringTruncator::rightTruncate(String(text, length), width, makeFont(description), false);
140 memcpy(buffer, result.characters(), result.length() * sizeof(UChar));
141 buffer[result.length()] = '\0';