1/* 2 * Copyright (C) Research In Motion Limited 2010-2012. All rights reserved. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20#include "config.h" 21 22#if ENABLE(SVG) 23#include "SVGTextMetrics.h" 24 25#include "RenderSVGInlineText.h" 26#include "SVGTextRunRenderingContext.h" 27#include "WidthIterator.h" 28 29namespace WebCore { 30 31SVGTextMetrics::SVGTextMetrics() 32 : m_width(0) 33 , m_height(0) 34 , m_length(0) 35{ 36} 37 38SVGTextMetrics::SVGTextMetrics(SVGTextMetrics::MetricsType) 39 : m_width(0) 40 , m_height(0) 41 , m_length(1) 42{ 43} 44 45SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText* textRenderer, const TextRun& run) 46{ 47 ASSERT(textRenderer); 48 49 float scalingFactor = textRenderer->scalingFactor(); 50 ASSERT(scalingFactor); 51 52 const Font& scaledFont = textRenderer->scaledFont(); 53 int length = 0; 54 55 // Calculate width/height using the scaled font, divide this result by the scalingFactor afterwards. 56 m_width = scaledFont.width(run, length, m_glyph.name) / scalingFactor; 57 m_height = scaledFont.fontMetrics().floatHeight() / scalingFactor; 58 59 m_glyph.unicodeString = run.is8Bit() ? String(run.characters8(), length) : String(run.characters16(), length); 60 m_glyph.isValid = true; 61 62 ASSERT(length >= 0); 63 m_length = static_cast<unsigned>(length); 64} 65 66TextRun SVGTextMetrics::constructTextRun(RenderSVGInlineText* text, const UChar* characters, unsigned position, unsigned length) 67{ 68 RenderStyle* style = text->style(); 69 ASSERT(style); 70 71 TextRun run(characters + position 72 , length 73 , 0 /* xPos, only relevant with allowTabs=true */ 74 , 0 /* padding, only relevant for justified text, not relevant for SVG */ 75 , TextRun::AllowTrailingExpansion 76 , style->direction() 77 , isOverride(style->unicodeBidi()) /* directionalOverride */); 78 79 if (textRunNeedsRenderingContext(style->font())) 80 run.setRenderingContext(SVGTextRunRenderingContext::create(text)); 81 82 run.disableRoundingHacks(); 83 84 // We handle letter & word spacing ourselves. 85 run.disableSpacing(); 86 87 // Propagate the maximum length of the characters buffer to the TextRun, even when we're only processing a substring. 88 run.setCharactersLength(text->textLength() - position); 89 ASSERT(run.charactersLength() >= run.length()); 90 return run; 91} 92 93SVGTextMetrics SVGTextMetrics::measureCharacterRange(RenderSVGInlineText* text, unsigned position, unsigned length) 94{ 95 ASSERT(text); 96 return SVGTextMetrics(text, constructTextRun(text, text->characters(), position, length)); 97} 98 99SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText* text, unsigned position, unsigned length, float width, const String& glyphName) 100{ 101 ASSERT(text); 102 103 bool needsContext = textRunNeedsRenderingContext(text->style()->font()); 104 float scalingFactor = text->scalingFactor(); 105 ASSERT(scalingFactor); 106 107 m_width = width / scalingFactor; 108 m_height = text->scaledFont().fontMetrics().floatHeight() / scalingFactor; 109 if (needsContext) { 110 m_glyph.isValid = true; 111 m_glyph.unicodeString = String(text->characters() + position, length); 112 m_glyph.name = glyphName; 113 } 114 115 m_length = length; 116} 117 118} 119 120#endif // ENABLE(SVG) 121