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