| 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 | #include "SVGTextMetrics.h" |
| 22 | |
| 23 | #include "RenderSVGInlineText.h" |
| 24 | #include "WidthIterator.h" |
| 25 | |
| 26 | namespace WebCore { |
| 27 | |
| 28 | SVGTextMetrics::SVGTextMetrics() |
| 29 | : m_width(0) |
| 30 | , m_height(0) |
| 31 | , m_length(0) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | SVGTextMetrics::SVGTextMetrics(SVGTextMetrics::MetricsType) |
| 36 | : m_width(0) |
| 37 | , m_height(0) |
| 38 | , m_length(1) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText& textRenderer, const TextRun& run) |
| 43 | { |
| 44 | float scalingFactor = textRenderer.scalingFactor(); |
| 45 | ASSERT(scalingFactor); |
| 46 | |
| 47 | const FontCascade& scaledFont = textRenderer.scaledFont(); |
| 48 | int length = 0; |
| 49 | |
| 50 | // Calculate width/height using the scaled font, divide this result by the scalingFactor afterwards. |
| 51 | m_width = scaledFont.width(run) / scalingFactor; |
| 52 | length = run.length(); |
| 53 | m_glyph.name = emptyString(); |
| 54 | m_height = scaledFont.fontMetrics().floatHeight() / scalingFactor; |
| 55 | |
| 56 | m_glyph.unicodeString = run.is8Bit() ? String(run.characters8(), length) : String(run.characters16(), length); |
| 57 | m_glyph.isValid = true; |
| 58 | |
| 59 | ASSERT(length >= 0); |
| 60 | m_length = static_cast<unsigned>(length); |
| 61 | } |
| 62 | |
| 63 | TextRun SVGTextMetrics::constructTextRun(RenderSVGInlineText& text, unsigned position, unsigned length) |
| 64 | { |
| 65 | const RenderStyle& style = text.style(); |
| 66 | |
| 67 | TextRun run(StringView(text.text()).substring(position, length) |
| 68 | , 0 /* xPos, only relevant with allowTabs=true */ |
| 69 | , 0 /* padding, only relevant for justified text, not relevant for SVG */ |
| 70 | , AllowTrailingExpansion |
| 71 | , style.direction() |
| 72 | , isOverride(style.unicodeBidi()) /* directionalOverride */); |
| 73 | |
| 74 | // We handle letter & word spacing ourselves. |
| 75 | run.disableSpacing(); |
| 76 | return run; |
| 77 | } |
| 78 | |
| 79 | SVGTextMetrics SVGTextMetrics::measureCharacterRange(RenderSVGInlineText& text, unsigned position, unsigned length) |
| 80 | { |
| 81 | return SVGTextMetrics(text, constructTextRun(text, position, length)); |
| 82 | } |
| 83 | |
| 84 | SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText& text, unsigned length, float width) |
| 85 | { |
| 86 | float scalingFactor = text.scalingFactor(); |
| 87 | ASSERT(scalingFactor); |
| 88 | |
| 89 | m_width = width / scalingFactor; |
| 90 | m_height = text.scaledFont().fontMetrics().floatHeight() / scalingFactor; |
| 91 | |
| 92 | m_length = length; |
| 93 | } |
| 94 | |
| 95 | } |
| 96 | |