1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/*
* Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#if ENABLE(SVG_FONTS)
#include "SVGFontData.h"
#include "RenderObject.h"
#include "SVGAltGlyphElement.h"
#include "SVGFontElement.h"
#include "SVGFontFaceElement.h"
#include "SVGGlyph.h"
#include "SVGGlyphElement.h"
#include "SVGNames.h"
#include "SVGTextRunRenderingContext.h"
#include "TextRun.h"
#include "WidthIterator.h"
#include "XMLNames.h"
#include <wtf/text/StringBuilder.h>
#include <wtf/unicode/CharacterNames.h>
#include <wtf/unicode/Unicode.h>
using namespace WTF;
using namespace Unicode;
namespace WebCore {
SVGFontData::SVGFontData(SVGFontFaceElement* fontFaceElement)
: m_svgFontFaceElement(fontFaceElement)
, m_horizontalOriginX(fontFaceElement->horizontalOriginX())
, m_horizontalOriginY(fontFaceElement->horizontalOriginY())
, m_horizontalAdvanceX(fontFaceElement->horizontalAdvanceX())
, m_verticalOriginX(fontFaceElement->verticalOriginX())
, m_verticalOriginY(fontFaceElement->verticalOriginY())
, m_verticalAdvanceY(fontFaceElement->verticalAdvanceY())
{
ASSERT_ARG(fontFaceElement, fontFaceElement);
}
void SVGFontData::initializeFontData(SimpleFontData* fontData, float fontSize)
{
ASSERT(fontData);
SVGFontFaceElement* svgFontFaceElement = this->svgFontFaceElement();
ASSERT(svgFontFaceElement);
SVGFontElement* svgFontElement = svgFontFaceElement->associatedFontElement();
ASSERT(svgFontElement);
GlyphData missingGlyphData;
missingGlyphData.fontData = fontData;
missingGlyphData.glyph = svgFontElement->missingGlyph();
fontData->setMissingGlyphData(missingGlyphData);
fontData->setZeroWidthSpaceGlyph(0);
fontData->determinePitch();
unsigned unitsPerEm = svgFontFaceElement->unitsPerEm();
float scale = scaleEmToUnits(fontSize, unitsPerEm);
float xHeight = svgFontFaceElement->xHeight() * scale;
float ascent = svgFontFaceElement->ascent() * scale;
float descent = svgFontFaceElement->descent() * scale;
float lineGap = 0.1f * fontSize;
GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(fontData, 0)->page();
if (!xHeight && glyphPageZero) {
// Fallback if x_heightAttr is not specified for the font element.
Glyph letterXGlyph = glyphPageZero->glyphDataForCharacter('x').glyph;
xHeight = letterXGlyph ? fontData->widthForGlyph(letterXGlyph) : 2 * ascent / 3;
}
FontMetrics& fontMetrics = fontData->fontMetrics();
fontMetrics.setUnitsPerEm(unitsPerEm);
fontMetrics.setAscent(ascent);
fontMetrics.setDescent(descent);
fontMetrics.setLineGap(lineGap);
fontMetrics.setLineSpacing(roundf(ascent) + roundf(descent) + roundf(lineGap));
fontMetrics.setXHeight(xHeight);
if (!glyphPageZero) {
fontData->setSpaceGlyph(0);
fontData->setSpaceWidth(0);
fontData->setAvgCharWidth(0);
fontData->setMaxCharWidth(ascent);
return;
}
// Calculate space width.
Glyph spaceGlyph = glyphPageZero->glyphDataForCharacter(' ').glyph;
fontData->setSpaceGlyph(spaceGlyph);
fontData->setSpaceWidth(fontData->widthForGlyph(spaceGlyph));
// Estimate average character width.
Glyph numeralZeroGlyph = glyphPageZero->glyphDataForCharacter('0').glyph;
fontData->setAvgCharWidth(numeralZeroGlyph ? fontData->widthForGlyph(numeralZeroGlyph) : fontData->spaceWidth());
// Estimate maximum character width.
Glyph letterWGlyph = glyphPageZero->glyphDataForCharacter('W').glyph;
fontData->setMaxCharWidth(letterWGlyph ? fontData->widthForGlyph(letterWGlyph) : ascent);
}
float SVGFontData::widthForSVGGlyph(Glyph glyph, float fontSize) const
{
SVGFontFaceElement* svgFontFaceElement = this->svgFontFaceElement();
ASSERT(svgFontFaceElement);
SVGFontElement* associatedFontElement = svgFontFaceElement->associatedFontElement();
ASSERT(associatedFontElement);
SVGGlyph svgGlyph = associatedFontElement->svgGlyphForGlyph(glyph);
SVGGlyphElement::inheritUnspecifiedAttributes(svgGlyph, this);
return svgGlyph.horizontalAdvanceX * scaleEmToUnits(fontSize, svgFontFaceElement->unitsPerEm());
}
bool SVGFontData::applySVGGlyphSelection(WidthIterator& iterator, GlyphData& glyphData, bool mirror, int currentCharacter, unsigned& advanceLength) const
{
const TextRun& run = iterator.run();
Vector<SVGGlyph::ArabicForm>& arabicForms = iterator.arabicForms();
ASSERT(int(run.charactersLength()) >= currentCharacter);
// Associate text with arabic forms, if needed.
String remainingTextInRun;
if (run.is8Bit()) {
remainingTextInRun = String(run.data8(currentCharacter), run.charactersLength() - currentCharacter);
remainingTextInRun = Font::normalizeSpaces(remainingTextInRun.characters8(), remainingTextInRun.length());
} else {
remainingTextInRun = String(run.data16(currentCharacter), run.charactersLength() - currentCharacter);
remainingTextInRun = Font::normalizeSpaces(remainingTextInRun.characters16(), remainingTextInRun.length());
}
if (mirror)
remainingTextInRun = createStringWithMirroredCharacters(remainingTextInRun.characters(), remainingTextInRun.length());
if (!currentCharacter && arabicForms.isEmpty())
arabicForms = charactersWithArabicForm(remainingTextInRun, mirror);
SVGFontFaceElement* svgFontFaceElement = this->svgFontFaceElement();
ASSERT(svgFontFaceElement);
SVGFontElement* associatedFontElement = svgFontFaceElement->associatedFontElement();
ASSERT(associatedFontElement);
RenderObject* renderObject = 0;
if (TextRun::RenderingContext* renderingContext = run.renderingContext())
renderObject = static_cast<SVGTextRunRenderingContext*>(renderingContext)->renderer();
String language;
bool isVerticalText = false;
Vector<String> altGlyphNames;
if (renderObject) {
RenderObject* parentRenderObject = renderObject->isText() ? renderObject->parent() : renderObject;
ASSERT(parentRenderObject);
isVerticalText = parentRenderObject->style()->svgStyle()->isVerticalWritingMode();
if (Element* parentRenderObjectElement = toElement(parentRenderObject->node())) {
language = parentRenderObjectElement->getAttribute(XMLNames::langAttr);
if (parentRenderObjectElement->hasTagName(SVGNames::altGlyphTag)) {
SVGAltGlyphElement* altGlyph = static_cast<SVGAltGlyphElement*>(parentRenderObjectElement);
if (!altGlyph->hasValidGlyphElements(altGlyphNames))
altGlyphNames.clear();
}
}
}
Vector<SVGGlyph> glyphs;
size_t altGlyphNamesSize = altGlyphNames.size();
if (altGlyphNamesSize) {
for (size_t index = 0; index < altGlyphNamesSize; ++index)
associatedFontElement->collectGlyphsForGlyphName(altGlyphNames[index], glyphs);
// Assign the unicodeStringLength now that its known.
size_t glyphsSize = glyphs.size();
for (size_t i = 0; i < glyphsSize; ++i)
glyphs[i].unicodeStringLength = run.length();
// Do not check alt glyphs for compatibility. Just return the first one.
// Later code will fail if we do not do this and the glyph is incompatible.
if (glyphsSize) {
SVGGlyph& svgGlyph = glyphs[0];
iterator.setLastGlyphName(svgGlyph.glyphName);
glyphData.glyph = svgGlyph.tableEntry;
advanceLength = svgGlyph.unicodeStringLength;
return true;
}
} else
associatedFontElement->collectGlyphsForString(remainingTextInRun, glyphs);
size_t glyphsSize = glyphs.size();
for (size_t i = 0; i < glyphsSize; ++i) {
SVGGlyph& svgGlyph = glyphs[i];
if (svgGlyph.isPartOfLigature)
continue;
if (!isCompatibleGlyph(svgGlyph, isVerticalText, language, arabicForms, currentCharacter, currentCharacter + svgGlyph.unicodeStringLength))
continue;
iterator.setLastGlyphName(svgGlyph.glyphName);
glyphData.glyph = svgGlyph.tableEntry;
advanceLength = svgGlyph.unicodeStringLength;
return true;
}
iterator.setLastGlyphName(String());
return false;
}
bool SVGFontData::fillSVGGlyphPage(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData) const
{
ASSERT(fontData->isCustomFont());
ASSERT(fontData->isSVGFont());
SVGFontFaceElement* fontFaceElement = this->svgFontFaceElement();
ASSERT(fontFaceElement);
SVGFontElement* fontElement = fontFaceElement->associatedFontElement();
ASSERT(fontElement);
if (bufferLength == length)
return fillBMPGlyphs(fontElement, pageToFill, offset, length, buffer, fontData);
ASSERT(bufferLength == 2 * length);
return fillNonBMPGlyphs(fontElement, pageToFill, offset, length, buffer, fontData);
}
bool SVGFontData::fillBMPGlyphs(SVGFontElement* fontElement, GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, const SimpleFontData* fontData) const
{
bool haveGlyphs = false;
Vector<SVGGlyph> glyphs;
for (unsigned i = 0; i < length; ++i) {
String lookupString(buffer + i, 1);
fontElement->collectGlyphsForString(lookupString, glyphs);
if (glyphs.isEmpty()) {
pageToFill->setGlyphDataForIndex(offset + i, 0, 0);
continue;
}
// Associate entry in glyph page with first valid SVGGlyph.
// If there are multiple valid ones, just take the first one. WidthIterator will take
// care of matching to the correct glyph, if multiple ones are available, as that's
// only possible within the context of a string (eg. arabic form matching).
haveGlyphs = true;
pageToFill->setGlyphDataForIndex(offset + i, glyphs.first().tableEntry, fontData);
glyphs.clear();
}
return haveGlyphs;
}
bool SVGFontData::fillNonBMPGlyphs(SVGFontElement* fontElement, GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, const SimpleFontData* fontData) const
{
bool haveGlyphs = false;
Vector<SVGGlyph> glyphs;
for (unsigned i = 0; i < length; ++i) {
// Each character here consists of a surrogate pair
String lookupString(buffer + i * 2, 2);
fontElement->collectGlyphsForString(lookupString, glyphs);
if (glyphs.isEmpty()) {
pageToFill->setGlyphDataForIndex(offset + i, 0, 0);
continue;
}
// Associate entry in glyph page with first valid SVGGlyph.
// If there are multiple valid ones, just take the first one. WidthIterator will take
// care of matching to the correct glyph, if multiple ones are available, as that's
// only possible within the context of a string (eg. arabic form matching).
haveGlyphs = true;
pageToFill->setGlyphDataForIndex(offset + i, glyphs.first().tableEntry, fontData);
glyphs.clear();
}
return haveGlyphs;
}
String SVGFontData::createStringWithMirroredCharacters(const UChar* characters, unsigned length) const
{
StringBuilder mirroredCharacters;
mirroredCharacters.reserveCapacity(length);
unsigned i = 0;
while (i < length) {
UChar32 character;
U16_NEXT(characters, i, length, character);
mirroredCharacters.append(mirroredChar(character));
}
return mirroredCharacters.toString();
}
} // namespace WebCore
#endif
|