1/* 2 * Copyright (C) 2006, 2007, 2008 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 "Font.h" 28 29#include "FontFallbackList.h" 30#include "GlyphBuffer.h" 31#include "GraphicsContext.h" 32#include "IntRect.h" 33#include "Logging.h" 34#include "SimpleFontData.h" 35#include "TextRun.h" 36#include "UniscribeController.h" 37#include <wtf/MathExtras.h> 38 39using namespace std; 40 41namespace WebCore { 42 43bool Font::canReturnFallbackFontsForComplexText() 44{ 45 return true; 46} 47 48bool Font::canExpandAroundIdeographsInComplexText() 49{ 50 return false; 51} 52 53FloatRect Font::selectionRectForComplexText(const TextRun& run, const FloatPoint& point, int h, 54 int from, int to) const 55{ 56 UniscribeController it(this, run); 57 it.advance(from); 58 float beforeWidth = it.runWidthSoFar(); 59 it.advance(to); 60 float afterWidth = it.runWidthSoFar(); 61 62 // Using roundf() rather than ceilf() for the right edge as a compromise to ensure correct caret positioning 63 if (run.rtl()) { 64 it.advance(run.length()); 65 float totalWidth = it.runWidthSoFar(); 66 return FloatRect(point.x() + floorf(totalWidth - afterWidth), point.y(), roundf(totalWidth - beforeWidth) - floorf(totalWidth - afterWidth), h); 67 } 68 69 return FloatRect(point.x() + floorf(beforeWidth), point.y(), roundf(afterWidth) - floorf(beforeWidth), h); 70} 71 72float Font::getGlyphsAndAdvancesForComplexText(const TextRun& run, int from, int to, GlyphBuffer& glyphBuffer, ForTextEmphasisOrNot forTextEmphasis) const 73{ 74 if (forTextEmphasis) { 75 // FIXME: Add forTextEmphasis paremeter to UniscribeController and use it. 76 LOG_ERROR("Not implemented for text emphasis."); 77 return 0; 78 } 79 80 UniscribeController controller(this, run); 81 controller.advance(from); 82 float beforeWidth = controller.runWidthSoFar(); 83 controller.advance(to, &glyphBuffer); 84 85 if (glyphBuffer.isEmpty()) 86 return 0; 87 88 float afterWidth = controller.runWidthSoFar(); 89 90 if (run.rtl()) { 91 controller.advance(run.length()); 92 return controller.runWidthSoFar() - afterWidth; 93 } 94 return beforeWidth; 95} 96 97void Font::drawComplexText(GraphicsContext* context, const TextRun& run, const FloatPoint& point, 98 int from, int to) const 99{ 100 // This glyph buffer holds our glyphs + advances + font data for each glyph. 101 GlyphBuffer glyphBuffer; 102 103 float startX = point.x() + getGlyphsAndAdvancesForComplexText(run, from, to, glyphBuffer); 104 105 // We couldn't generate any glyphs for the run. Give up. 106 if (glyphBuffer.isEmpty()) 107 return; 108 109 // Draw the glyph buffer now at the starting point returned in startX. 110 FloatPoint startPoint(startX, point.y()); 111 drawGlyphBuffer(context, glyphBuffer, startPoint); 112} 113 114void Font::drawEmphasisMarksForComplexText(GraphicsContext* context, const TextRun& run, const AtomicString& mark, const FloatPoint& point, int from, int to) const 115{ 116 GlyphBuffer glyphBuffer; 117 float initialAdvance = getGlyphsAndAdvancesForComplexText(run, from, to, glyphBuffer, ForTextEmphasis); 118 119 if (glyphBuffer.isEmpty()) 120 return; 121 122 drawEmphasisMarks(context, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y())); 123} 124 125float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const 126{ 127 UniscribeController controller(this, run, fallbackFonts); 128 controller.advance(run.length()); 129 if (glyphOverflow) { 130 glyphOverflow->top = max<int>(glyphOverflow->top, ceilf(-controller.minGlyphBoundingBoxY()) - (glyphOverflow->computeBounds ? 0 : fontMetrics().ascent())); 131 glyphOverflow->bottom = max<int>(glyphOverflow->bottom, ceilf(controller.maxGlyphBoundingBoxY()) - (glyphOverflow->computeBounds ? 0 : fontMetrics().descent())); 132 glyphOverflow->left = max<int>(0, ceilf(-controller.minGlyphBoundingBoxX())); 133 glyphOverflow->right = max<int>(0, ceilf(controller.maxGlyphBoundingBoxX() - controller.runWidthSoFar())); 134 } 135 return controller.runWidthSoFar(); 136} 137 138int Font::offsetForPositionForComplexText(const TextRun& run, float xFloat, bool includePartialGlyphs) const 139{ 140 // FIXME: This truncation is not a problem for HTML, but only affects SVG, which passes floating-point numbers 141 // to Font::offsetForPosition(). Bug http://webkit.org/b/40673 tracks fixing this problem. 142 int x = static_cast<int>(xFloat); 143 144 UniscribeController controller(this, run); 145 return controller.offsetForPosition(x, includePartialGlyphs); 146} 147 148} 149