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 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 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#include "config.h" 26#include "WebCoreTextRenderer.h" 27 28#include "Font.h" 29#include "FontCache.h" 30#include "FontDescription.h" 31#include "GraphicsContext.h" 32#include "StringTruncator.h" 33#include "TextRun.h" 34 35namespace WebCore { 36 37static bool shouldUseFontSmoothing = true; 38 39static bool isOneLeftToRightRun(const TextRun& run) 40{ 41 for (int i = 0; i < run.length(); i++) { 42 UCharDirection direction = u_charDirection(run[i]); 43 if (direction == U_RIGHT_TO_LEFT || direction > U_OTHER_NEUTRAL) 44 return false; 45 } 46 return true; 47} 48 49static void doDrawTextAtPoint(GraphicsContext& context, const String& text, const IntPoint& point, const Font& font, const Color& color, int underlinedIndex) 50{ 51 FontCachePurgePreventer fontCachePurgePreventer; 52 53 TextRun run(text); 54 55 context.setFillColor(color, ColorSpaceDeviceRGB); 56 if (isOneLeftToRightRun(run)) 57 font.drawText(&context, run, point); 58 else 59 context.drawBidiText(font, run, point); 60 61 if (underlinedIndex >= 0) { 62 ASSERT_WITH_SECURITY_IMPLICATION(underlinedIndex < static_cast<int>(text.length())); 63 64 int beforeWidth; 65 if (underlinedIndex > 0) { 66 TextRun beforeRun(StringView(text).substring(0, underlinedIndex)); 67 beforeWidth = font.width(beforeRun); 68 } else 69 beforeWidth = 0; 70 71 TextRun underlinedRun(StringView(text).substring(underlinedIndex, 1)); 72 int underlinedWidth = font.width(underlinedRun); 73 74 IntPoint underlinePoint(point); 75 underlinePoint.move(beforeWidth, 1); 76 77 context.setStrokeColor(color, ColorSpaceDeviceRGB); 78 context.drawLineForText(underlinePoint, underlinedWidth, false); 79 } 80} 81 82void WebCoreDrawDoubledTextAtPoint(GraphicsContext& context, const String& text, const IntPoint& point, const Font& font, const Color& topColor, const Color& bottomColor, int underlinedIndex) 83{ 84 context.save(); 85 86 IntPoint textPos = point; 87 88 doDrawTextAtPoint(context, text, textPos, font, bottomColor, underlinedIndex); 89 textPos.move(0, -1); 90 doDrawTextAtPoint(context, text, textPos, font, topColor, underlinedIndex); 91 92 context.restore(); 93} 94 95float WebCoreTextFloatWidth(const String& text, const Font& font) 96{ 97 FontCachePurgePreventer fontCachePurgePreventer; 98 99 return StringTruncator::width(text, font, StringTruncator::EnableRoundingHacks); 100} 101 102void WebCoreSetShouldUseFontSmoothing(bool smooth) 103{ 104 shouldUseFontSmoothing = smooth; 105} 106 107bool WebCoreShouldUseFontSmoothing() 108{ 109 return shouldUseFontSmoothing; 110} 111 112void WebCoreSetAlwaysUsesComplexTextCodePath(bool complex) 113{ 114 Font::setCodePath(complex ? Font::Complex : Font::Auto); 115} 116 117bool WebCoreAlwaysUsesComplexTextCodePath() 118{ 119 return Font::codePath() == Font::Complex; 120} 121 122} // namespace WebCore 123