SVGFontFaceElement.cpp [plain text]
#include "config.h"
#if ENABLE(SVG_FONTS)
#include "SVGFontFaceElement.h"
#include "CSSFontFaceSrcValue.h"
#include "CSSParser.h"
#include "CSSPropertyNames.h"
#include "CSSStyleSheet.h"
#include "CSSValueKeywords.h"
#include "CSSValueList.h"
#include "Document.h"
#include "ElementIterator.h"
#include "FontCascade.h"
#include "Logging.h"
#include "SVGDocumentExtensions.h"
#include "SVGFontElement.h"
#include "SVGFontFaceSrcElement.h"
#include "SVGGlyphElement.h"
#include "SVGNames.h"
#include "StyleProperties.h"
#include "StyleResolver.h"
#include "StyleRule.h"
#include "StyleScope.h"
#include <math.h>
#include <wtf/IsoMallocInlines.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(SVGFontFaceElement);
using namespace SVGNames;
inline SVGFontFaceElement::SVGFontFaceElement(const QualifiedName& tagName, Document& document)
: SVGElement(tagName, document)
, m_fontFaceRule(StyleRuleFontFace::create(MutableStyleProperties::create(HTMLStandardMode)))
, m_fontElement(nullptr)
{
LOG(Fonts, "SVGFontFaceElement %p ctor", this);
ASSERT(hasTagName(font_faceTag));
}
SVGFontFaceElement::~SVGFontFaceElement()
{
LOG(Fonts, "SVGFontFaceElement %p dtor", this);
}
Ref<SVGFontFaceElement> SVGFontFaceElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new SVGFontFaceElement(tagName, document));
}
void SVGFontFaceElement::parseAttribute(const QualifiedName& name, const AtomString& value)
{
CSSPropertyID propertyId = cssPropertyIdForSVGAttributeName(name);
if (propertyId > 0) {
auto& properties = m_fontFaceRule->mutableProperties();
bool valueChanged = properties.setProperty(propertyId, value);
if (valueChanged) {
if (auto parsedValue = properties.getPropertyCSSValue(propertyId)) {
if (parsedValue->isGlobalKeyword())
properties.removeProperty(propertyId);
}
}
rebuildFontFace();
return;
}
SVGElement::parseAttribute(name, value);
}
unsigned SVGFontFaceElement::unitsPerEm() const
{
const AtomString& valueString = attributeWithoutSynchronization(units_per_emAttr);
if (valueString.isEmpty())
return FontMetrics::defaultUnitsPerEm;
auto value = static_cast<unsigned>(ceilf(valueString.toFloat()));
if (!value)
return FontMetrics::defaultUnitsPerEm;
return value;
}
int SVGFontFaceElement::xHeight() const
{
return static_cast<int>(ceilf(attributeWithoutSynchronization(x_heightAttr).toFloat()));
}
int SVGFontFaceElement::capHeight() const
{
return static_cast<int>(ceilf(attributeWithoutSynchronization(cap_heightAttr).toFloat()));
}
float SVGFontFaceElement::horizontalOriginX() const
{
if (!m_fontElement)
return 0.0f;
return m_fontElement->attributeWithoutSynchronization(horiz_origin_xAttr).toFloat();
}
float SVGFontFaceElement::horizontalOriginY() const
{
if (!m_fontElement)
return 0.0f;
return m_fontElement->attributeWithoutSynchronization(horiz_origin_yAttr).toFloat();
}
float SVGFontFaceElement::horizontalAdvanceX() const
{
if (!m_fontElement)
return 0.0f;
return m_fontElement->attributeWithoutSynchronization(horiz_adv_xAttr).toFloat();
}
float SVGFontFaceElement::verticalOriginX() const
{
if (!m_fontElement)
return 0.0f;
const AtomString& value = m_fontElement->attributeWithoutSynchronization(vert_origin_xAttr);
if (value.isEmpty())
return horizontalAdvanceX() / 2.0f;
return value.toFloat();
}
float SVGFontFaceElement::verticalOriginY() const
{
if (!m_fontElement)
return 0.0f;
const AtomString& value = m_fontElement->attributeWithoutSynchronization(vert_origin_yAttr);
if (value.isEmpty())
return ascent();
return value.toFloat();
}
float SVGFontFaceElement::verticalAdvanceY() const
{
if (!m_fontElement)
return 0.0f;
const AtomString& value = m_fontElement->attributeWithoutSynchronization(vert_adv_yAttr);
if (value.isEmpty())
return 1.0f;
return value.toFloat();
}
int SVGFontFaceElement::ascent() const
{
const AtomString& ascentValue = attributeWithoutSynchronization(ascentAttr);
if (!ascentValue.isEmpty())
return static_cast<int>(ceilf(ascentValue.toFloat()));
if (m_fontElement) {
const AtomString& vertOriginY = m_fontElement->attributeWithoutSynchronization(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 AtomString& descentValue = attributeWithoutSynchronization(descentAttr);
if (!descentValue.isEmpty()) {
int descent = static_cast<int>(ceilf(descentValue.toFloat()));
return descent < 0 ? -descent : descent;
}
if (m_fontElement) {
const AtomString& vertOriginY = m_fontElement->attributeWithoutSynchronization(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() || is<SVGFontElement>(*parentNode()));
return m_fontElement;
}
void SVGFontFaceElement::rebuildFontFace()
{
if (!isConnected()) {
ASSERT(!m_fontElement);
return;
}
auto srcElement = childrenOfType<SVGFontFaceSrcElement>(*this).first();
bool describesParentFont = is<SVGFontElement>(*parentNode());
RefPtr<CSSValueList> list;
if (describesParentFont) {
m_fontElement = downcast<SVGFontElement>(parentNode());
list = CSSValueList::createCommaSeparated();
list->append(CSSFontFaceSrcValue::createLocal(fontFamily()));
} else {
m_fontElement = nullptr;
if (srcElement)
list = srcElement->srcValue();
}
if (!list || !list->length())
return;
m_fontFaceRule->mutableProperties().addParsedProperty(CSSProperty(CSSPropertySrc, list));
if (describesParentFont) {
RefPtr<CSSValue> src = m_fontFaceRule->properties().getPropertyCSSValue(CSSPropertySrc);
CSSValueList* srcList = downcast<CSSValueList>(src.get());
unsigned srcLength = srcList ? srcList->length() : 0;
for (unsigned i = 0; i < srcLength; ++i) {
if (auto item = makeRefPtr(downcast<CSSFontFaceSrcValue>(srcList->itemWithoutBoundsCheck(i))))
item->setSVGFontFaceElement(this);
}
}
document().styleScope().didChangeStyleSheetEnvironment();
}
Node::InsertedIntoAncestorResult SVGFontFaceElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree)
{
SVGElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);
if (!insertionType.connectedToDocument) {
ASSERT(!m_fontElement);
return InsertedIntoAncestorResult::Done;
}
document().accessSVGExtensions().registerSVGFontFaceElement(*this);
rebuildFontFace();
return InsertedIntoAncestorResult::Done;
}
void SVGFontFaceElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree)
{
SVGElement::removedFromAncestor(removalType, oldParentOfRemovedTree);
if (removalType.disconnectedFromDocument) {
m_fontElement = nullptr;
document().accessSVGExtensions().unregisterSVGFontFaceElement(*this);
m_fontFaceRule->mutableProperties().clear();
document().styleScope().didChangeStyleSheetEnvironment();
} else
ASSERT(!m_fontElement);
}
void SVGFontFaceElement::childrenChanged(const ChildChange& change)
{
SVGElement::childrenChanged(change);
rebuildFontFace();
}
}
#endif // ENABLE(SVG_FONTS)