SVGFontFaceElement.cpp [plain text]
#include "config.h"
#if ENABLE(SVG_FONTS)
#include "SVGFontFaceElement.h"
#include "CString.h"
#include "CSSFontFaceRule.h"
#include "CSSFontFaceSrcValue.h"
#include "CSSProperty.h"
#include "CSSPropertyNames.h"
#include "CSSStyleSelector.h"
#include "CSSStyleSheet.h"
#include "CSSValueKeywords.h"
#include "CSSValueList.h"
#include "FontCache.h"
#include "FontPlatformData.h"
#include "SimpleFontData.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(new CSSFontFaceRule(0))
, m_styleDeclaration(new CSSMutableStyleDeclaration)
{
m_styleDeclaration->setParent(document()->mappedElementSheet());
m_styleDeclaration->setStrictParsing(true);
m_fontFaceRule->setDeclaration(m_styleDeclaration.get());
document()->mappedElementSheet()->append(m_fontFaceRule);
}
SVGFontFaceElement::~SVGFontFaceElement()
{
}
static inline void mapAttributeToCSSProperty(HashMap<AtomicStringImpl*, int>* propertyNameToIdMap, const QualifiedName& attrName, const char* cssPropertyName = 0)
{
int propertyId = 0;
if (cssPropertyName)
propertyId = getPropertyID(cssPropertyName, strlen(cssPropertyName));
else {
CString propertyName = attrName.localName().domString().utf8();
propertyId = getPropertyID(propertyName.data(), propertyName.length());
}
if (propertyId < 1)
fprintf(stderr, "Failed to find property: %s\n", attrName.localName().deprecatedString().ascii());
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);
rebuildFontFace();
return;
}
SVGElement::parseMappedAttribute(attr);
}
unsigned SVGFontFaceElement::unitsPerEm() const
{
AtomicString value(getAttribute(units_per_emAttr));
if (value.isEmpty())
return 1000;
return static_cast<unsigned>(ceilf(value.toFloat()));
}
int SVGFontFaceElement::xHeight() const
{
AtomicString value(getAttribute(x_heightAttr));
if (value.isEmpty())
return 0;
return static_cast<int>(ceilf(value.toFloat()));
}
float SVGFontFaceElement::horizontalOriginX() const
{
if (!m_fontElement)
return 0.0f;
AtomicString value(m_fontElement->getAttribute(horiz_origin_xAttr));
if (value.isEmpty())
return 0.0f;
return value.toFloat();
}
float SVGFontFaceElement::horizontalOriginY() const
{
if (!m_fontElement)
return 0.0f;
AtomicString value(m_fontElement->getAttribute(horiz_origin_yAttr));
if (value.isEmpty())
return 0.0f;
return value.toFloat();
}
float SVGFontFaceElement::horizontalAdvanceX() const
{
if (!m_fontElement)
return 0.0f;
AtomicString value(m_fontElement->getAttribute(horiz_adv_xAttr));
if (value.isEmpty())
return 0.0f;
return value.toFloat();
}
float SVGFontFaceElement::verticalOriginX() const
{
if (!m_fontElement)
return 0.0f;
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;
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;
AtomicString value(m_fontElement->getAttribute(vert_adv_yAttr));
if (value.isEmpty())
return 1.0f;
return value.toFloat();
}
int SVGFontFaceElement::ascent() const
{
if (!m_fontElement)
return 0.0f;
AtomicString value(m_fontElement->getAttribute(ascentAttr));
if (!value.isEmpty())
return static_cast<int>(ceilf(value.toFloat()));
value = m_fontElement->getAttribute(vert_origin_yAttr);
if (!value.isEmpty())
return static_cast<int>(unitsPerEm()) - static_cast<int>(ceilf(value.toFloat()));
return static_cast<int>(ceilf(unitsPerEm() * 0.8f));
}
int SVGFontFaceElement::descent() const
{
if (!m_fontElement)
return 0.0f;
AtomicString value(m_fontElement->getAttribute(descentAttr));
if (!value.isEmpty()) {
int descent = static_cast<int>(ceilf(value.toFloat()));
return descent < 0 ? -descent : descent;
}
value = m_fontElement->getAttribute(vert_origin_yAttr);
if (!value.isEmpty())
return static_cast<int>(ceilf(value.toFloat()));
return static_cast<int>(ceilf(unitsPerEm() * 0.2f));
}
String SVGFontFaceElement::fontFamily() const
{
return m_styleDeclaration->getPropertyValue(CSS_PROP_FONT_FAMILY);
}
SVGFontElement* SVGFontFaceElement::associatedFontElement() const
{
return m_fontElement.get();
}
void SVGFontFaceElement::rebuildFontFace()
{
if (!parentNode())
return;
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(CSS_PROP_DEFINITION_SRC, definitionSrc->getAttribute(XLinkNames::hrefAttr), false);
#endif
bool describesParentFont = parentNode()->hasTagName(fontTag);
RefPtr<CSSValueList> list;
if (describesParentFont) {
m_fontElement = static_cast<SVGFontElement*>(parentNode());
list = new CSSValueList;
list->append(new CSSFontFaceSrcValue(fontFamily(), true));
} else if (srcElement)
list = srcElement->srcValue();
if (!list)
return;
CSSProperty srcProperty(CSS_PROP_SRC, list);
const CSSProperty* srcPropertyRef = &srcProperty;
m_styleDeclaration->addParsedProperties(&srcPropertyRef, 1);
if (describesParentFont) {
RefPtr<CSSValue> src = m_styleDeclaration->getPropertyCSSValue(CSS_PROP_SRC);
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->item(i)))
item->setSVGFontFaceElement(this);
}
}
document()->updateStyleSelector();
}
void SVGFontFaceElement::insertedIntoDocument()
{
rebuildFontFace();
}
void SVGFontFaceElement::childrenChanged(bool changedByParser)
{
SVGElement::childrenChanged(changedByParser);
rebuildFontFace();
}
}
#endif // ENABLE(SVG_FONTS)