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 String drawString(text, length);
79 drawString = drawString.impl()->secure(WebCore::bullet);
80 WebCoreDrawTextAtPoint(context, drawString, point, makeFont(description), color, underlinedIndex);
83 void WebDrawText(WebTextRenderInfo* info)
85 if (!info || info->structSize != sizeof(WebTextRenderInfo) || !info->cgContext || !info->description)
88 int oldFontSmoothingLevel = -1;
89 if (info->overrideSmoothingLevel >= 0) {
90 oldFontSmoothingLevel = wkGetFontSmoothingLevel();
91 wkSetFontSmoothingLevel(info->overrideSmoothingLevel);
94 DrawTextAtPoint(info->cgContext, info->text, info->length, info->pt, *(info->description), info->color, info->underlinedIndex, info->drawAsPassword);
96 if (info->overrideSmoothingLevel >= 0)
97 wkSetFontSmoothingLevel(oldFontSmoothingLevel);
100 float TextFloatWidth(LPCTSTR text, int length, const WebFontDescription& description)
102 return WebCoreTextFloatWidth(String(text, length), makeFont(description));
105 void FontMetrics(const WebFontDescription& description, int* ascent, int* descent, int* lineSpacing)
107 if (!ascent && !descent && !lineSpacing)
110 Font font(makeFont(description));
113 *ascent = font.ascent();
116 *descent = font.descent();
119 *lineSpacing = font.lineSpacing();
122 void CenterTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
126 String result = StringTruncator::centerTruncate(String(text, length), width, makeFont(description), false);
127 memcpy(buffer, result.characters(), result.length() * sizeof(UChar));
128 buffer[result.length()] = '\0';
131 void RightTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
135 String result = StringTruncator::rightTruncate(String(text, length), width, makeFont(description), false);
136 memcpy(buffer, result.characters(), result.length() * sizeof(UChar));
137 buffer[result.length()] = '\0';