SVGFontFaceElement.cpp [plain text]
#include "config.h"
#if ENABLE(SVG_FONTS)
#include "SVGFontFaceElement.h"
#include "CString.h"
#include "CSSFontFaceRule.h"
#include "CSSFontFaceSrcValue.h"
#include "CSSParser.h"
#include "CSSProperty.h"
#include "CSSPropertyNames.h"
#include "CSSStyleSelector.h"
#include "CSSStyleSheet.h"
#include "CSSValueKeywords.h"
#include "CSSValueList.h"
#include "Document.h"
#include "Font.h"
#include "MappedAttribute.h"
#include "SVGDefinitionSrcElement.h"
#include "SVGFontElement.h"
#include "SVGFontFaceSrcElement.h"
#include "SVGGlyphElement.h"
#include "SVGNames.h"
#include <math.h>
namespace WebCore {
using namespace SVGNames;
SVGFontFaceElement::SVGFontFaceElement(const QualifiedName& tagName, Document* doc)
: SVGElement(tagName, doc)
, m_fontFaceRule(CSSFontFaceRule::create())
, m_styleDeclaration(CSSMutableStyleDeclaration::create())
{
m_styleDeclaration->setParent(document()->mappedElementSheet());
m_styleDeclaration->setStrictParsing(true);
m_fontFaceRule->setDeclaration(m_styleDeclaration.get());
}
SVGFontFaceElement::~SVGFontFaceElement()
{
removeFromMappedElementSheet();
}
static void mapAttributeToCSSProperty(HashMap<AtomicStringImpl*, int>* propertyNameToIdMap, const QualifiedName& attrName)
{
int propertyId = cssPropertyID(attrName.localName());
ASSERT(propertyId > 0);
propertyNameToIdMap->set(attrName.localName().impl(), propertyId);
}
static int cssPropertyIdForSVGAttributeName(const QualifiedName& attrName)
{
if (!attrName.namespaceURI().isNull())
return 0;
static HashMap<AtomicStringImpl*, int>* propertyNameToIdMap = 0;
if (!propertyNameToIdMap) {
propertyNameToIdMap = new HashMap<AtomicStringImpl*, int>;
mapAttributeToCSSProperty(propertyNameToIdMap, font_familyAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_sizeAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_stretchAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_styleAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_variantAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_weightAttr);
}
return propertyNameToIdMap->get(attrName.localName().impl());
}
void SVGFontFaceElement::parseMappedAttribute(MappedAttribute* attr)
{
int propId = cssPropertyIdForSVGAttributeName(attr->name());
if (propId > 0) {
m_styleDeclaration->setProperty(propId, attr->value(), false);
if (inDocument())
rebuildFontFace();
return;
}
SVGElement::parseMappedAttribute(attr);
}
unsigned SVGFontFaceElement::unitsPerEm() const
{
const AtomicString& value = getAttribute(units_per_emAttr);
if (value.isEmpty())
return defaultUnitsPerEm;
return static_cast<unsigned>(ceilf(value.toFloat()));
}
int SVGFontFaceElement::xHeight() const
{
return static_cast<int>(ceilf(getAttribute(x_heightAttr).toFloat()));
}
float SVGFontFaceElement::horizontalOriginX() const
{
if (!m_fontElement)
return 0.0f;
return m_fontElement->getAttribute(horiz_origin_xAttr).toFloat();
}
float SVGFontFaceElement::horizontalOriginY() const
{
if (!m_fontElement)
return 0.0f;
return m_fontElement->getAttribute(horiz_origin_yAttr).toFloat();
}
float SVGFontFaceElement::horizontalAdvanceX() const
{
if (!m_fontElement)
return 0.0f;
return m_fontElement->getAttribute(horiz_adv_xAttr).toFloat();
}
float SVGFontFaceElement::verticalOriginX() const
{
if (!m_fontElement)
return 0.0f;
const AtomicString& value = m_fontElement->getAttribute(vert_origin_xAttr);
if (value.isEmpty())
return horizontalAdvanceX() / 2.0f;
return value.toFloat();
}
float SVGFontFaceElement::verticalOriginY() const
{
if (!m_fontElement)
return 0.0f;
const AtomicString& value = m_fontElement->getAttribute(vert_origin_yAttr);
if (value.isEmpty())
return ascent();
return value.toFloat();
}
float SVGFontFaceElement::verticalAdvanceY() const
{
if (!m_fontElement)
return 0.0f;
const AtomicString& value = m_fontElement->getAttribute(vert_adv_yAttr);
if (value.isEmpty())
return 1.0f;
return value.toFloat();
}
int SVGFontFaceElement::ascent() const
{
const AtomicString& ascentValue = getAttribute(ascentAttr);
if (!ascentValue.isEmpty())
return static_cast<int>(ceilf(ascentValue.toFloat()));
if (m_fontElement) {
const AtomicString& vertOriginY = m_fontElement->getAttribute(vert_origin_yAttr);
if (!vertOriginY.isEmpty())
return static_cast<int>(unitsPerEm()) - static_cast<int>(ceilf(vertOriginY.toFloat()));
}
return static_cast<int>(ceilf(unitsPerEm() * 0.8f));
}
int SVGFontFaceElement::descent() const
{
const AtomicString& descentValue = getAttribute(descentAttr);
if (!descentValue.isEmpty()) {
int descent = static_cast<int>(ceilf(descentValue.toFloat()));
return descent < 0 ? -descent : descent;
}
if (m_fontElement) {
const AtomicString& vertOriginY = m_fontElement->getAttribute(vert_origin_yAttr);
if (!vertOriginY.isEmpty())
return static_cast<int>(ceilf(vertOriginY.toFloat()));
}
return static_cast<int>(ceilf(unitsPerEm() * 0.2f));
}
String SVGFontFaceElement::fontFamily() const
{
return m_styleDeclaration->getPropertyValue(CSSPropertyFontFamily);
}
void SVGFontFaceElement::rebuildFontFace()
{
ASSERT(inDocument());
SVGFontFaceSrcElement* srcElement = 0;
SVGDefinitionSrcElement* definitionSrc = 0;
for (Node* child = firstChild(); child; child = child->nextSibling()) {
if (child->hasTagName(font_face_srcTag) && !srcElement)
srcElement = static_cast<SVGFontFaceSrcElement*>(child);
else if (child->hasTagName(definition_srcTag) && !definitionSrc)
definitionSrc = static_cast<SVGDefinitionSrcElement*>(child);
}
#if 0
if (definitionSrc)
m_styleDeclaration->setProperty(CSSPropertyDefinitionSrc, definitionSrc->getAttribute(XLinkNames::hrefAttr), false);
#endif
bool describesParentFont = parentNode()->hasTagName(fontTag);
RefPtr<CSSValueList> list;
if (describesParentFont) {
m_fontElement = static_cast<SVGFontElement*>(parentNode());
list = CSSValueList::createCommaSeparated();
list->append(CSSFontFaceSrcValue::createLocal(fontFamily()));
} else {
m_fontElement = 0;
if (srcElement)
list = srcElement->srcValue();
}
if (!list)
return;
CSSProperty srcProperty(CSSPropertySrc, list);
const CSSProperty* srcPropertyRef = &srcProperty;
m_styleDeclaration->addParsedProperties(&srcPropertyRef, 1);
if (describesParentFont) {
RefPtr<CSSValue> src = m_styleDeclaration->getPropertyCSSValue(CSSPropertySrc);
CSSValueList* srcList = static_cast<CSSValueList*>(src.get());
unsigned srcLength = srcList ? srcList->length() : 0;
for (unsigned i = 0; i < srcLength; i++) {
if (CSSFontFaceSrcValue* item = static_cast<CSSFontFaceSrcValue*>(srcList->itemWithoutBoundsCheck(i)))
item->setSVGFontFaceElement(this);
}
}
document()->updateStyleSelector();
}
void SVGFontFaceElement::insertedIntoDocument()
{
SVGElement::insertedIntoDocument();
document()->mappedElementSheet()->append(m_fontFaceRule);
rebuildFontFace();
}
void SVGFontFaceElement::removedFromDocument()
{
removeFromMappedElementSheet();
SVGElement::removedFromDocument();
}
void SVGFontFaceElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
if (inDocument())
rebuildFontFace();
}
void SVGFontFaceElement::removeFromMappedElementSheet()
{
CSSStyleSheet* mappedElementSheet = document()->mappedElementSheet();
if (!mappedElementSheet)
return;
for (unsigned i = 0; i < mappedElementSheet->length(); ++i) {
if (mappedElementSheet->item(i) == m_fontFaceRule) {
mappedElementSheet->remove(i);
break;
}
}
document()->updateStyleSelector();
}
}
#endif // ENABLE(SVG_FONTS)