KHTML
SVGFontFaceElement.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022
00023 #if ENABLE(SVG_FONTS)
00024 #include "SVGFontFaceElement.h"
00025 #include "CString.h"
00026 #include "CSSFontFaceRule.h"
00027 #include "CSSFontFaceSrcValue.h"
00028 #include "CSSParser.h"
00029 #include "CSSProperty.h"
00030 #include "CSSPropertyNames.h"
00031 #include "CSSStyleSelector.h"
00032 #include "CSSStyleSheet.h"
00033 #include "CSSValueKeywords.h"
00034 #include "CSSValueList.h"
00035 #include "FontCache.h"
00036 #include "SimpleFontData.h"
00037 #include "SVGDefinitionSrcElement.h"
00038 #include "SVGFontElement.h"
00039 #include "SVGFontFaceSrcElement.h"
00040 #include "SVGGlyphElement.h"
00041 #include "SVGNames.h"
00042
00043 #include <math.h>
00044
00045 namespace WebCore {
00046
00047 using namespace SVGNames;
00048
00049 SVGFontFaceElement::SVGFontFaceElement(const QualifiedName& tagName, Document* doc)
00050 : SVGElement(tagName, doc)
00051 , m_fontFaceRule(new CSSFontFaceRule(0))
00052 , m_styleDeclaration(new CSSMutableStyleDeclaration)
00053 {
00054 m_styleDeclaration->setParent(document()->mappedElementSheet());
00055 m_styleDeclaration->setStrictParsing(true);
00056 m_fontFaceRule->setDeclaration(m_styleDeclaration.get());
00057 document()->mappedElementSheet()->append(m_fontFaceRule);
00058 }
00059
00060 SVGFontFaceElement::~SVGFontFaceElement()
00061 {
00062 }
00063
00064 static void mapAttributeToCSSProperty(HashMap<AtomicStringImpl*, int>* propertyNameToIdMap, const QualifiedName& attrName)
00065 {
00066 int propertyId = cssPropertyID(attrName.localName());
00067 ASSERT(propertyId > 0);
00068 propertyNameToIdMap->set(attrName.localName().impl(), propertyId);
00069 }
00070
00071 static int cssPropertyIdForSVGAttributeName(const QualifiedName& attrName)
00072 {
00073 if (!attrName.namespaceURI().isNull())
00074 return 0;
00075
00076 static HashMap<AtomicStringImpl*, int>* propertyNameToIdMap = 0;
00077 if (!propertyNameToIdMap) {
00078 propertyNameToIdMap = new HashMap<AtomicStringImpl*, int>;
00079
00080
00081
00082
00083
00084
00085
00086
00087 mapAttributeToCSSProperty(propertyNameToIdMap, font_familyAttr);
00088 mapAttributeToCSSProperty(propertyNameToIdMap, font_sizeAttr);
00089 mapAttributeToCSSProperty(propertyNameToIdMap, font_stretchAttr);
00090 mapAttributeToCSSProperty(propertyNameToIdMap, font_styleAttr);
00091 mapAttributeToCSSProperty(propertyNameToIdMap, font_variantAttr);
00092 mapAttributeToCSSProperty(propertyNameToIdMap, font_weightAttr);
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 }
00115
00116 return propertyNameToIdMap->get(attrName.localName().impl());
00117 }
00118
00119 void SVGFontFaceElement::parseMappedAttribute(MappedAttribute* attr)
00120 {
00121 int propId = cssPropertyIdForSVGAttributeName(attr->name());
00122 if (propId > 0) {
00123 m_styleDeclaration->setProperty(propId, attr->value(), false);
00124 rebuildFontFace();
00125 return;
00126 }
00127
00128 SVGElement::parseMappedAttribute(attr);
00129 }
00130
00131 unsigned SVGFontFaceElement::unitsPerEm() const
00132 {
00133 AtomicString value(getAttribute(units_per_emAttr));
00134 if (value.isEmpty())
00135 return 1000;
00136
00137 return static_cast<unsigned>(ceilf(value.toFloat()));
00138 }
00139
00140 int SVGFontFaceElement::xHeight() const
00141 {
00142 AtomicString value(getAttribute(x_heightAttr));
00143 if (value.isEmpty())
00144 return 0;
00145
00146 return static_cast<int>(ceilf(value.toFloat()));
00147 }
00148
00149 float SVGFontFaceElement::horizontalOriginX() const
00150 {
00151 if (!m_fontElement)
00152 return 0.0f;
00153
00154
00155
00156
00157 AtomicString value(m_fontElement->getAttribute(horiz_origin_xAttr));
00158 if (value.isEmpty())
00159 return 0.0f;
00160
00161 return value.toFloat();
00162 }
00163
00164 float SVGFontFaceElement::horizontalOriginY() const
00165 {
00166 if (!m_fontElement)
00167 return 0.0f;
00168
00169
00170
00171
00172 AtomicString value(m_fontElement->getAttribute(horiz_origin_yAttr));
00173 if (value.isEmpty())
00174 return 0.0f;
00175
00176 return value.toFloat();
00177 }
00178
00179 float SVGFontFaceElement::horizontalAdvanceX() const
00180 {
00181 if (!m_fontElement)
00182 return 0.0f;
00183
00184
00185
00186
00187 AtomicString value(m_fontElement->getAttribute(horiz_adv_xAttr));
00188 if (value.isEmpty())
00189 return 0.0f;
00190
00191 return value.toFloat();
00192 }
00193
00194 float SVGFontFaceElement::verticalOriginX() const
00195 {
00196 if (!m_fontElement)
00197 return 0.0f;
00198
00199
00200
00201
00202 AtomicString value(m_fontElement->getAttribute(vert_origin_xAttr));
00203 if (value.isEmpty())
00204 return horizontalAdvanceX() / 2.0f;
00205
00206 return value.toFloat();
00207 }
00208
00209 float SVGFontFaceElement::verticalOriginY() const
00210 {
00211 if (!m_fontElement)
00212 return 0.0f;
00213
00214
00215
00216
00217 AtomicString value(m_fontElement->getAttribute(vert_origin_yAttr));
00218 if (value.isEmpty())
00219 return ascent();
00220
00221 return value.toFloat();
00222 }
00223
00224 float SVGFontFaceElement::verticalAdvanceY() const
00225 {
00226 if (!m_fontElement)
00227 return 0.0f;
00228
00229
00230
00231 AtomicString value(m_fontElement->getAttribute(vert_adv_yAttr));
00232 if (value.isEmpty())
00233 return 1.0f;
00234
00235 return value.toFloat();
00236 }
00237
00238 int SVGFontFaceElement::ascent() const
00239 {
00240 if (!m_fontElement)
00241 return 0.0f;
00242
00243
00244
00245
00246
00247 AtomicString value(m_fontElement->getAttribute(ascentAttr));
00248 if (!value.isEmpty())
00249 return static_cast<int>(ceilf(value.toFloat()));
00250
00251 value = m_fontElement->getAttribute(vert_origin_yAttr);
00252 if (!value.isEmpty())
00253 return static_cast<int>(unitsPerEm()) - static_cast<int>(ceilf(value.toFloat()));
00254
00255
00256 return static_cast<int>(ceilf(unitsPerEm() * 0.8f));
00257 }
00258
00259 int SVGFontFaceElement::descent() const
00260 {
00261 if (!m_fontElement)
00262 return 0.0f;
00263
00264
00265
00266
00267 AtomicString value(m_fontElement->getAttribute(descentAttr));
00268 if (!value.isEmpty()) {
00269
00270 int descent = static_cast<int>(ceilf(value.toFloat()));
00271 return descent < 0 ? -descent : descent;
00272 }
00273
00274 value = m_fontElement->getAttribute(vert_origin_yAttr);
00275 if (!value.isEmpty())
00276 return static_cast<int>(ceilf(value.toFloat()));
00277
00278
00279 return static_cast<int>(ceilf(unitsPerEm() * 0.2f));
00280 }
00281
00282 String SVGFontFaceElement::fontFamily() const
00283 {
00284 return m_styleDeclaration->getPropertyValue(CSSPropertyFontFamily);
00285 }
00286
00287 SVGFontElement* SVGFontFaceElement::associatedFontElement() const
00288 {
00289 return m_fontElement.get();
00290 }
00291
00292 void SVGFontFaceElement::rebuildFontFace()
00293 {
00294
00295 if (!parentNode())
00296 return;
00297
00298
00299 SVGFontFaceSrcElement* srcElement = 0;
00300 SVGDefinitionSrcElement* definitionSrc = 0;
00301
00302 for (Node* child = firstChild(); child; child = child->nextSibling()) {
00303 if (child->hasTagName(font_face_srcTag) && !srcElement)
00304 srcElement = static_cast<SVGFontFaceSrcElement*>(child);
00305 else if (child->hasTagName(definition_srcTag) && !definitionSrc)
00306 definitionSrc = static_cast<SVGDefinitionSrcElement*>(child);
00307 }
00308
00309 #if 0
00310
00311 if (definitionSrc)
00312 m_styleDeclaration->setProperty(CSSPropertyDefinitionSrc, definitionSrc->getAttribute(XLinkNames::hrefAttr), false);
00313 #endif
00314
00315 bool describesParentFont = parentNode()->hasTagName(fontTag);
00316 RefPtr<CSSValueList> list;
00317
00318 if (describesParentFont) {
00319 m_fontElement = static_cast<SVGFontElement*>(parentNode());
00320
00321 list = new CSSValueList;
00322 list->append(new CSSFontFaceSrcValue(fontFamily(), true));
00323 } else if (srcElement)
00324 list = srcElement->srcValue();
00325
00326 if (!list)
00327 return;
00328
00329
00330 CSSProperty srcProperty(CSSPropertySrc, list);
00331 const CSSProperty* srcPropertyRef = &srcProperty;
00332 m_styleDeclaration->addParsedProperties(&srcPropertyRef, 1);
00333
00334 if (describesParentFont) {
00335
00336 RefPtr<CSSValue> src = m_styleDeclaration->getPropertyCSSValue(CSSPropertySrc);
00337 CSSValueList* srcList = static_cast<CSSValueList*>(src.get());
00338
00339 unsigned srcLength = srcList ? srcList->length() : 0;
00340 for (unsigned i = 0; i < srcLength; i++) {
00341 if (CSSFontFaceSrcValue* item = static_cast<CSSFontFaceSrcValue*>(srcList->itemWithoutBoundsCheck(i)))
00342 item->setSVGFontFaceElement(this);
00343 }
00344 }
00345
00346 document()->updateStyleSelector();
00347 }
00348
00349 void SVGFontFaceElement::insertedIntoDocument()
00350 {
00351 rebuildFontFace();
00352 SVGElement::insertedIntoDocument();
00353 }
00354
00355 void SVGFontFaceElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
00356 {
00357 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
00358 rebuildFontFace();
00359 }
00360
00361 }
00362
00363 #endif // ENABLE(SVG_FONTS)