1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/*
* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
* Copyright (C) 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) 2008 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#if ENABLE(SVG_FONTS)
#include "SVGFontFaceElement.h"
#include "Attribute.h"
#include "CSSFontFaceSrcValue.h"
#include "CSSParser.h"
#include "CSSPropertyNames.h"
#include "CSSStyleSheet.h"
#include "CSSValueKeywords.h"
#include "CSSValueList.h"
#include "Document.h"
#include "Font.h"
#include "SVGDocumentExtensions.h"
#include "SVGFontElement.h"
#include "SVGFontFaceSrcElement.h"
#include "SVGGlyphElement.h"
#include "SVGNames.h"
#include "StyleResolver.h"
#include "StyleRule.h"
#include <math.h>
namespace WebCore {
using namespace SVGNames;
inline SVGFontFaceElement::SVGFontFaceElement(const QualifiedName& tagName, Document* document)
: SVGElement(tagName, document)
, m_fontFaceRule(StyleRuleFontFace::create())
{
ASSERT(hasTagName(font_faceTag));
RefPtr<StylePropertySet> styleDeclaration = StylePropertySet::create(CSSStrictMode);
m_fontFaceRule->setProperties(styleDeclaration.release());
}
PassRefPtr<SVGFontFaceElement> SVGFontFaceElement::create(const QualifiedName& tagName, Document* document)
{
return adoptRef(new SVGFontFaceElement(tagName, document));
}
static CSSPropertyID cssPropertyIdForSVGAttributeName(const QualifiedName& attrName)
{
if (!attrName.namespaceURI().isNull())
return CSSPropertyInvalid;
static HashMap<AtomicStringImpl*, CSSPropertyID>* propertyNameToIdMap = 0;
if (!propertyNameToIdMap) {
propertyNameToIdMap = new HashMap<AtomicStringImpl*, CSSPropertyID>;
// This is a list of all @font-face CSS properties which are exposed as SVG XML attributes
// Those commented out are not yet supported by WebCore's style system
// mapAttributeToCSSProperty(propertyNameToIdMap, accent_heightAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, alphabeticAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, ascentAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, bboxAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, cap_heightAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, descentAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_familyAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_sizeAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_stretchAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_styleAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_variantAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_weightAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, hangingAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, ideographicAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, mathematicalAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, overline_positionAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, overline_thicknessAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, panose_1Attr);
// mapAttributeToCSSProperty(propertyNameToIdMap, slopeAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, stemhAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, stemvAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, strikethrough_positionAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, strikethrough_thicknessAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, underline_positionAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, underline_thicknessAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, unicode_rangeAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, units_per_emAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, v_alphabeticAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, v_hangingAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, v_ideographicAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, v_mathematicalAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, widthsAttr);
// mapAttributeToCSSProperty(propertyNameToIdMap, x_heightAttr);
}
return propertyNameToIdMap->get(attrName.localName().impl());
}
void SVGFontFaceElement::parseAttribute(const Attribute& attribute)
{
CSSPropertyID propId = cssPropertyIdForSVGAttributeName(attribute.name());
if (propId > 0) {
m_fontFaceRule->mutableProperties()->setProperty(propId, attribute.value(), false);
rebuildFontFace();
return;
}
SVGElement::parseAttribute(attribute);
}
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;
// Spec: The X-coordinate in the font coordinate system of the origin of a glyph to be used when
// drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
// If the attribute is not specified, the effect is as if a value of "0" were specified.
return m_fontElement->fastGetAttribute(horiz_origin_xAttr).toFloat();
}
float SVGFontFaceElement::horizontalOriginY() const
{
if (!m_fontElement)
return 0.0f;
// Spec: The Y-coordinate in the font coordinate system of the origin of a glyph to be used when
// drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
// If the attribute is not specified, the effect is as if a value of "0" were specified.
return m_fontElement->fastGetAttribute(horiz_origin_yAttr).toFloat();
}
float SVGFontFaceElement::horizontalAdvanceX() const
{
if (!m_fontElement)
return 0.0f;
// Spec: The default horizontal advance after rendering a glyph in horizontal orientation. Glyph
// widths are required to be non-negative, even if the glyph is typically rendered right-to-left,
// as in Hebrew and Arabic scripts.
return m_fontElement->fastGetAttribute(horiz_adv_xAttr).toFloat();
}
float SVGFontFaceElement::verticalOriginX() const
{
if (!m_fontElement)
return 0.0f;
// Spec: The default X-coordinate in the font coordinate system of the origin of a glyph to be used when
// drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
// were set to half of the effective value of attribute horiz-adv-x.
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;
// Spec: The default Y-coordinate in the font coordinate system of the origin of a glyph to be used when
// drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
// were set to the position specified by the font's ascent attribute.
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;
// Spec: The default vertical advance after rendering a glyph in vertical orientation. If the attribute is
// not specified, the effect is as if a value equivalent of one em were specified (see units-per-em).
const AtomicString& value = m_fontElement->fastGetAttribute(vert_adv_yAttr);
if (value.isEmpty())
return 1.0f;
return value.toFloat();
}
int SVGFontFaceElement::ascent() const
{
// Spec: Same syntax and semantics as the 'ascent' descriptor within an @font-face rule. The maximum
// unaccented height of the font within the font coordinate system. If the attribute is not specified,
// the effect is as if the attribute were set to the difference between the units-per-em value and the
// vert-origin-y value for the corresponding font.
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()));
}
// Match Batiks default value
return static_cast<int>(ceilf(unitsPerEm() * 0.8f));
}
int SVGFontFaceElement::descent() const
{
// Spec: Same syntax and semantics as the 'descent' descriptor within an @font-face rule. The maximum
// unaccented depth of the font within the font coordinate system. If the attribute is not specified,
// the effect is as if the attribute were set to the vert-origin-y value for the corresponding font.
const AtomicString& descentValue = fastGetAttribute(descentAttr);
if (!descentValue.isEmpty()) {
// 14 different W3C SVG 1.1 testcases use a negative descent value,
// where a positive was meant to be used Including:
// animate-elem-24-t.svg, fonts-elem-01-t.svg, fonts-elem-02-t.svg (and 11 others)
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()));
}
// Match Batiks default value
return static_cast<int>(ceilf(unitsPerEm() * 0.2f));
}
String SVGFontFaceElement::fontFamily() const
{
return m_fontFaceRule->properties()->getPropertyValue(CSSPropertyFontFamily);
}
SVGFontElement* SVGFontFaceElement::associatedFontElement() const
{
return m_fontElement.get();
}
void SVGFontFaceElement::rebuildFontFace()
{
if (!inDocument())
return;
// we currently ignore all but the first src element, alternatively we could concat them
SVGFontFaceSrcElement* srcElement = 0;
for (Node* child = firstChild(); child && !srcElement; child = child->nextSibling()) {
if (child->hasTagName(font_face_srcTag))
srcElement = static_cast<SVGFontFaceSrcElement*>(child);
}
bool describesParentFont = parentNode()->hasTagName(SVGNames::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 || !list->length())
return;
// Parse in-memory CSS rules
m_fontFaceRule->mutableProperties()->addParsedProperty(CSSProperty(CSSPropertySrc, list));
if (describesParentFont) {
// Traverse parsed CSS values and associate CSSFontFaceSrcValue elements with ourselves.
RefPtr<CSSValue> src = m_fontFaceRule->properties()->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()->styleResolverChanged(DeferRecalcStyle);
}
Node::InsertionNotificationRequest SVGFontFaceElement::insertedInto(ContainerNode* rootParent)
{
SVGElement::insertedInto(rootParent);
if (!rootParent->inDocument())
return InsertionDone;
document()->accessSVGExtensions()->registerSVGFontFaceElement(this);
rebuildFontFace();
return InsertionDone;
}
void SVGFontFaceElement::removedFrom(ContainerNode* rootParent)
{
SVGElement::removedFrom(rootParent);
if (rootParent->inDocument()) {
document()->accessSVGExtensions()->unregisterSVGFontFaceElement(this);
m_fontFaceRule->mutableProperties()->parseDeclaration(emptyString(), 0);
document()->styleResolverChanged(DeferRecalcStyle);
}
}
void SVGFontFaceElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
rebuildFontFace();
}
} // namespace WebCore
#endif // ENABLE(SVG_FONTS)
|