This source file includes following definitions.
- m_fontElement
- create
- cssPropertyIdForFontFaceAttributeName
- parseAttribute
- unitsPerEm
- xHeight
- horizontalOriginX
- horizontalOriginY
- horizontalAdvanceX
- verticalOriginX
- verticalOriginY
- verticalAdvanceY
- ascent
- descent
- fontFamily
- associatedFontElement
- rebuildFontFace
- insertedInto
- removedFrom
- childrenChanged
#include "config.h"
#if ENABLE(SVG_FONTS)
#include "core/svg/SVGFontFaceElement.h"
#include <math.h>
#include "CSSPropertyNames.h"
#include "CSSValueKeywords.h"
#include "core/css/CSSFontFaceSrcValue.h"
#include "core/css/CSSFontSelector.h"
#include "core/css/CSSStyleSheet.h"
#include "core/css/CSSValueList.h"
#include "core/css/StylePropertySet.h"
#include "core/css/StyleRule.h"
#include "core/dom/Attribute.h"
#include "core/dom/Document.h"
#include "core/dom/StyleEngine.h"
#include "core/svg/SVGDocumentExtensions.h"
#include "core/svg/SVGFontElement.h"
#include "core/svg/SVGFontFaceSrcElement.h"
#include "core/svg/SVGGlyphElement.h"
#include "platform/fonts/Font.h"
namespace WebCore {
using namespace SVGNames;
inline SVGFontFaceElement::SVGFontFaceElement(Document& document)
: SVGElement(font_faceTag, document)
, m_fontFaceRule(StyleRuleFontFace::create())
, m_fontElement(0)
{
ScriptWrappable::init(this);
RefPtrWillBeRawPtr<MutableStylePropertySet> styleDeclaration = MutableStylePropertySet::create(HTMLStandardMode);
m_fontFaceRule->setProperties(styleDeclaration.release());
}
PassRefPtr<SVGFontFaceElement> SVGFontFaceElement::create(Document& document)
{
return adoptRef(new SVGFontFaceElement(document));
}
static CSSPropertyID cssPropertyIdForFontFaceAttributeName(const QualifiedName& attrName)
{
if (!attrName.namespaceURI().isNull())
return CSSPropertyInvalid;
static HashMap<StringImpl*, CSSPropertyID>* propertyNameToIdMap = 0;
if (!propertyNameToIdMap) {
propertyNameToIdMap = new HashMap<StringImpl*, CSSPropertyID>;
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::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
CSSPropertyID propId = cssPropertyIdForFontFaceAttributeName(name);
if (propId > 0) {
m_fontFaceRule->mutableProperties().setProperty(propId, value, false);
rebuildFontFace();
return;
}
SVGElement::parseAttribute(name, value);
}
unsigned SVGFontFaceElement::unitsPerEm() const
{
const AtomicString& value = fastGetAttribute(units_per_emAttr);
if (value.isEmpty())
return gDefaultUnitsPerEm;
return static_cast<unsigned>(ceilf(value.toFloat()));
}
int SVGFontFaceElement::xHeight() const
{
return static_cast<int>(ceilf(fastGetAttribute(x_heightAttr).toFloat()));
}
float SVGFontFaceElement::horizontalOriginX() const
{
if (!m_fontElement)
return 0.0f;
return m_fontElement->fastGetAttribute(horiz_origin_xAttr).toFloat();
}
float SVGFontFaceElement::horizontalOriginY() const
{
if (!m_fontElement)
return 0.0f;
return m_fontElement->fastGetAttribute(horiz_origin_yAttr).toFloat();
}
float SVGFontFaceElement::horizontalAdvanceX() const
{
if (!m_fontElement)
return 0.0f;
return m_fontElement->fastGetAttribute(horiz_adv_xAttr).toFloat();
}
float SVGFontFaceElement::verticalOriginX() const
{
if (!m_fontElement)
return 0.0f;
const AtomicString& value = m_fontElement->fastGetAttribute(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->fastGetAttribute(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->fastGetAttribute(vert_adv_yAttr);
if (value.isEmpty())
return 1.0f;
return value.toFloat();
}
int SVGFontFaceElement::ascent() const
{
const AtomicString& ascentValue = fastGetAttribute(ascentAttr);
if (!ascentValue.isEmpty())
return static_cast<int>(ceilf(ascentValue.toFloat()));
if (m_fontElement) {
const AtomicString& vertOriginY = m_fontElement->fastGetAttribute(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 = fastGetAttribute(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->fastGetAttribute(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_fontFaceRule->properties().getPropertyValue(CSSPropertyFontFamily);
}
SVGFontElement* SVGFontFaceElement::associatedFontElement() const
{
ASSERT(parentNode() == m_fontElement);
ASSERT(!parentNode() || isSVGFontElement(*parentNode()));
return m_fontElement;
}
void SVGFontFaceElement::rebuildFontFace()
{
if (!inDocument()) {
ASSERT(!m_fontElement);
return;
}
bool describesParentFont = isSVGFontElement(*parentNode());
RefPtrWillBeRawPtr<CSSValueList> list = nullptr;
if (describesParentFont) {
m_fontElement = toSVGFontElement(parentNode());
list = CSSValueList::createCommaSeparated();
list->append(CSSFontFaceSrcValue::createLocal(fontFamily()));
} else {
m_fontElement = 0;
if (SVGFontFaceSrcElement* element = Traversal<SVGFontFaceSrcElement>::lastChild(*this))
list = element->srcValue();
}
if (!list || !list->length())
return;
m_fontFaceRule->mutableProperties().addParsedProperty(CSSProperty(CSSPropertySrc, list));
if (describesParentFont) {
RefPtrWillBeRawPtr<CSSValue> src = m_fontFaceRule->properties().getPropertyCSSValue(CSSPropertySrc);
CSSValueList* srcList = toCSSValueList(src.get());
unsigned srcLength = srcList ? srcList->length() : 0;
for (unsigned i = 0; i < srcLength; i++) {
if (CSSFontFaceSrcValue* item = toCSSFontFaceSrcValue(srcList->itemWithoutBoundsCheck(i)))
item->setSVGFontFaceElement(this);
}
}
document().styleResolverChanged(RecalcStyleDeferred);
}
Node::InsertionNotificationRequest SVGFontFaceElement::insertedInto(ContainerNode* rootParent)
{
SVGElement::insertedInto(rootParent);
if (!rootParent->inDocument()) {
ASSERT(!m_fontElement);
return InsertionDone;
}
document().accessSVGExtensions().registerSVGFontFaceElement(this);
rebuildFontFace();
return InsertionDone;
}
void SVGFontFaceElement::removedFrom(ContainerNode* rootParent)
{
SVGElement::removedFrom(rootParent);
if (rootParent->inDocument()) {
m_fontElement = 0;
document().accessSVGExtensions().unregisterSVGFontFaceElement(this);
if (document().isActive() && document().styleEngine()->fontSelector()) {
document().styleEngine()->fontSelector()->fontFaceCache()->remove(m_fontFaceRule.get());
document().accessSVGExtensions().registerPendingSVGFontFaceElementsForRemoval(this);
}
m_fontFaceRule->mutableProperties().clear();
document().styleResolverChanged(RecalcStyleDeferred);
} else
ASSERT(!m_fontElement);
}
void SVGFontFaceElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
rebuildFontFace();
}
}
#endif