This source file includes following definitions.
- m_verticalAdvanceY
- initializeFontData
- widthForSVGGlyph
- applySVGGlyphSelection
- fillSVGGlyphPage
- fillBMPGlyphs
- fillNonBMPGlyphs
- createStringWithMirroredCharacters
#include "config.h"
#if ENABLE(SVG_FONTS)
#include "core/svg/SVGFontData.h"
#include "SVGNames.h"
#include "XMLNames.h"
#include "core/rendering/RenderObject.h"
#include "core/rendering/svg/SVGTextRunRenderingContext.h"
#include "core/svg/SVGAltGlyphElement.h"
#include "core/svg/SVGFontElement.h"
#include "core/svg/SVGFontFaceElement.h"
#include "core/svg/SVGGlyphElement.h"
#include "platform/fonts/Character.h"
#include "platform/fonts/SVGGlyph.h"
#include "platform/fonts/SimpleFontData.h"
#include "platform/fonts/WidthIterator.h"
#include "platform/text/TextRun.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)
: CustomFontData()
, 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 && svgFontFaceElement->inDocument());
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) {
Glyph letterXGlyph = glyphPageZero->glyphForCharacter('x');
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;
}
Glyph spaceGlyph = glyphPageZero->glyphForCharacter(' ');
fontData->setSpaceGlyph(spaceGlyph);
fontData->setSpaceWidth(fontData->widthForGlyph(spaceGlyph));
Glyph numeralZeroGlyph = glyphPageZero->glyphForCharacter('0');
fontData->setAvgCharWidth(numeralZeroGlyph ? fontData->widthForGlyph(numeralZeroGlyph) : fontData->spaceWidth());
Glyph letterWGlyph = glyphPageZero->glyphForCharacter('W');
fontData->setMaxCharWidth(letterWGlyph ? fontData->widthForGlyph(letterWGlyph) : ascent);
}
float SVGFontData::widthForSVGGlyph(Glyph glyph, float fontSize) const
{
SVGFontFaceElement* svgFontFaceElement = this->svgFontFaceElement();
ASSERT(svgFontFaceElement);
if (!svgFontFaceElement->inDocument())
return m_horizontalAdvanceX * scaleEmToUnits(fontSize, svgFontFaceElement->unitsPerEm());
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);
String remainingTextInRun;
if (run.is8Bit()) {
remainingTextInRun = String(run.data8(currentCharacter), run.charactersLength() - currentCharacter);
remainingTextInRun = Character::normalizeSpaces(remainingTextInRun.characters8(), remainingTextInRun.length());
} else {
remainingTextInRun = String(run.data16(currentCharacter), run.charactersLength() - currentCharacter);
remainingTextInRun = Character::normalizeSpaces(remainingTextInRun.characters16(), remainingTextInRun.length());
}
if (mirror)
remainingTextInRun = createStringWithMirroredCharacters(remainingTextInRun);
if (!currentCharacter && arabicForms.isEmpty())
arabicForms = charactersWithArabicForm(remainingTextInRun, mirror);
SVGFontFaceElement* svgFontFaceElement = this->svgFontFaceElement();
ASSERT(svgFontFaceElement && svgFontFaceElement->inDocument());
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<AtomicString> 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 (isSVGAltGlyphElement(*parentRenderObjectElement)) {
if (!toSVGAltGlyphElement(*parentRenderObjectElement).hasValidGlyphElements(altGlyphNames))
altGlyphNames.clear();
}
}
}
Vector<SVGGlyph> glyphs;
size_t altGlyphNamesSize = altGlyphNames.size();
if (altGlyphNamesSize) {
for (size_t index = 0; index < altGlyphNamesSize; ++index)
associatedFontElement->collectGlyphsForAltGlyphReference(altGlyphNames[index], glyphs);
size_t glyphsSize = glyphs.size();
for (size_t i = 0; i < glyphsSize; ++i)
glyphs[i].unicodeStringLength = run.length();
if (glyphsSize) {
SVGGlyph& svgGlyph = glyphs[0];
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;
glyphData.glyph = svgGlyph.tableEntry;
advanceLength = svgGlyph.unicodeStringLength;
return true;
}
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 && fontFaceElement->inDocument());
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;
}
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) {
String lookupString(buffer + i * 2, 2);
fontElement->collectGlyphsForString(lookupString, glyphs);
if (glyphs.isEmpty()) {
pageToFill->setGlyphDataForIndex(offset + i, 0, 0);
continue;
}
haveGlyphs = true;
pageToFill->setGlyphDataForIndex(offset + i, glyphs.first().tableEntry, fontData);
glyphs.clear();
}
return haveGlyphs;
}
String SVGFontData::createStringWithMirroredCharacters(const String& string) const
{
if (string.isEmpty())
return emptyString();
unsigned length = string.length();
StringBuilder mirroredCharacters;
mirroredCharacters.reserveCapacity(length);
if (string.is8Bit()) {
const LChar* characters = string.characters8();
for (unsigned i = 0; i < length; ++i)
mirroredCharacters.append(mirroredChar(characters[i]));
} else {
const UChar* characters = string.characters16();
unsigned i = 0;
while (i < length) {
UChar32 character;
U16_NEXT(characters, i, length, character);
mirroredCharacters.append(mirroredChar(character));
}
}
return mirroredCharacters.toString();
}
}
#endif