File: SVGLengthValue.cpp

package info (click to toggle)
webkit2gtk 2.51.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 477,912 kB
  • sloc: cpp: 3,898,343; javascript: 198,215; ansic: 165,229; python: 50,371; asm: 21,819; ruby: 18,095; perl: 16,953; xml: 4,623; sh: 2,398; yacc: 2,356; java: 2,019; lex: 1,358; pascal: 372; makefile: 197
file content (408 lines) | stat: -rw-r--r-- 14,367 bytes parent folder | download
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
 * Copyright (C) 2007-2025 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"
#include "SVGLengthValue.h"

#include "AnimationUtilities.h"
#include "CSSParserContext.h"
#include "CSSParserTokenRange.h"
#include "CSSPrimitiveNumericTypes+Serialization.h"
#include "CSSPropertyParserConsumer+LengthPercentageDefinitions.h"
#include "CSSPropertyParserConsumer+MetaConsumer.h"
#include "CSSPropertyParserConsumer+NumberDefinitions.h"
#include "CSSToLengthConversionData.h"
#include "CSSTokenizer.h"
#include "ExceptionOr.h"
#include "SVGElement.h"
#include "SVGLengthContext.h"
#include "SVGParsingError.h"
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/MakeString.h>
#include <wtf/text/TextStream.h>

namespace WebCore {

WTF_MAKE_TZONE_ALLOCATED_IMPL(SVGLengthValue);

static inline SVGLengthType cssLengthUnitToSVGLengthType(CSS::LengthPercentageUnit unit)
{
    switch (unit) {
    case CSS::LengthPercentageUnit::Px:                 return SVGLengthType::Pixels;
    case CSS::LengthPercentageUnit::Percentage:         return SVGLengthType::Percentage;
    case CSS::LengthPercentageUnit::Em:                 return SVGLengthType::Ems;
    case CSS::LengthPercentageUnit::Ex:                 return SVGLengthType::Exs;
    case CSS::LengthPercentageUnit::Cm:                 return SVGLengthType::Centimeters;
    case CSS::LengthPercentageUnit::Mm:                 return SVGLengthType::Millimeters;
    case CSS::LengthPercentageUnit::In:                 return SVGLengthType::Inches;
    case CSS::LengthPercentageUnit::Pt:                 return SVGLengthType::Points;
    case CSS::LengthPercentageUnit::Pc:                 return SVGLengthType::Picas;
    case CSS::LengthPercentageUnit::Lh:                 return SVGLengthType::Lh;
    case CSS::LengthPercentageUnit::Ch:                 return SVGLengthType::Ch;
    default:                                            return SVGLengthType::Unknown;
    }
}

static inline CSS::LengthPercentageUnit svgLengthTypeToCSSLengthUnit(SVGLengthType type)
{
    switch (type) {
    case SVGLengthType::Number:       return CSS::LengthPercentageUnit::Px;
    case SVGLengthType::Pixels:       return CSS::LengthPercentageUnit::Px;
    case SVGLengthType::Percentage:   return CSS::LengthPercentageUnit::Percentage;
    case SVGLengthType::Ems:          return CSS::LengthPercentageUnit::Em;
    case SVGLengthType::Exs:          return CSS::LengthPercentageUnit::Ex;
    case SVGLengthType::Centimeters:  return CSS::LengthPercentageUnit::Cm;
    case SVGLengthType::Millimeters:  return CSS::LengthPercentageUnit::Mm;
    case SVGLengthType::Inches:       return CSS::LengthPercentageUnit::In;
    case SVGLengthType::Points:       return CSS::LengthPercentageUnit::Pt;
    case SVGLengthType::Picas:        return CSS::LengthPercentageUnit::Pc;
    case SVGLengthType::Lh:           return CSS::LengthPercentageUnit::Lh;
    case SVGLengthType::Ch:           return CSS::LengthPercentageUnit::Ch;
    default:                          return CSS::LengthPercentageUnit::Px;
    }
}

static Variant<CSS::Number<>, CSS::LengthPercentage<>> createVariantForLengthType(float value, SVGLengthType lengthType)
{
    if (lengthType == SVGLengthType::Number)
        return CSS::Number<>(value);

    if (lengthType == SVGLengthType::Unknown) {
        // For unknown types (like container units), fall back to Number
        // FIXME: Add support for container units
        return CSS::Number<>(value);
    }

    return CSS::LengthPercentage<>(svgLengthTypeToCSSLengthUnit(lengthType), value);
}


SVGLengthValue::SVGLengthValue(SVGLengthMode lengthMode, const String& valueAsString)
    : m_value(CSS::Number<>(0))
    , m_lengthMode(lengthMode)
{
    setValueAsString(valueAsString);
}

SVGLengthValue::SVGLengthValue(float valueInSpecifiedUnits, SVGLengthType lengthType, SVGLengthMode lengthMode)
    : m_value(createVariantForLengthType(valueInSpecifiedUnits, lengthType))
    , m_lengthMode(lengthMode)
{
}

SVGLengthValue::SVGLengthValue(const SVGLengthContext& context, float value, SVGLengthType lengthType, SVGLengthMode lengthMode)
    : m_value(createVariantForLengthType(0, lengthType))
    , m_lengthMode(lengthMode)
{
    setValue(context, value);
}

SVGLengthValue SVGLengthValue::construct(SVGLengthMode lengthMode, StringView valueAsString, SVGParsingError& parseError, SVGLengthNegativeValuesMode negativeValuesMode, ASCIILiteral fallbackValue)
{
    SVGLengthValue length(lengthMode);

    parseError = SVGParsingError::None;
    if (length.setValueAsString(valueAsString).hasException())
        parseError = SVGParsingError::ParsingFailed;
    else if (negativeValuesMode == SVGLengthNegativeValuesMode::Forbid && length.valueInSpecifiedUnits() < 0)
        parseError = SVGParsingError::ForbiddenNegativeValue;

    // If parsing failed or value is null, and we have a fallback, use it
    if (!fallbackValue.isNull() && (parseError != SVGParsingError::None || valueAsString.isNull()))
        return SVGLengthValue(lengthMode, fallbackValue);

    return length;
}

ExceptionOr<void> SVGLengthValue::setValueAsString(StringView valueAsString, SVGLengthMode lengthMode)
{
    m_lengthMode = lengthMode;
    return setValueAsString(valueAsString);
}

SVGLengthType SVGLengthValue::lengthType() const
{
    return WTF::switchOn(m_value,
        [](const CSS::Number<>&) -> SVGLengthType {
            return SVGLengthType::Number;
        },
        [](const CSS::LengthPercentage<>& length) -> SVGLengthType {
            if (auto raw = length.raw())
                return cssLengthUnitToSVGLengthType(raw->unit);

            return SVGLengthType::Unknown;
        }
    );
}

bool SVGLengthValue::isZero() const
{
    return WTF::switchOn(m_value,
        [](const auto& value) {
            return value.isKnownZero();
        }
    );
}

bool SVGLengthValue::isRelative() const
{
    return WTF::switchOn(m_value,
        [](const CSS::Number<>&) {
            return false;
        },
        [](const CSS::LengthPercentage<>& length) {
            if (auto raw = length.raw()) {
                using Unit = CSS::LengthPercentageUnit;
                switch (raw->unit) {
                case Unit::Percentage:
                case Unit::Em:
                case Unit::Ex:
                case Unit::Ch:
                case Unit::Lh:
                case Unit::Rem:
                case Unit::Rex:
                case Unit::Rlh:
                case Unit::Rch:
                    return true;
                default:
                    return false;
                }
            }

            return false;
        }
    );
}

float SVGLengthValue::value(const SVGLengthContext& context) const
{
    auto result = valueForBindings(context);
    if (result.hasException())
        return 0;
    return result.releaseReturnValue();
}

float SVGLengthValue::valueAsPercentage() const
{
    return WTF::switchOn(m_value,
        [](const CSS::Number<>& number) -> float {
            if (auto raw = number.raw())
                return raw->value;

            return 0.0f;
        },
        [](const CSS::LengthPercentage<>& length) -> float {
            if (auto raw = length.raw()) {
                if (raw->unit == CSS::LengthPercentageUnit::Percentage)
                    return raw->value / 100.0f;

                return raw->value;
            }

            return 0.0f;
        }
    );
}

float SVGLengthValue::valueInSpecifiedUnits() const
{
    // Per SVG spec: return 0 for non-scalar values like calc()
    // https://svgwg.org/svg2-draft/types.html#__svg__SVGLength__valueInSpecifiedUnits

    return WTF::switchOn(m_value,
        [](const auto& value) {
            if (auto raw = value.raw())
                return clampTo<float>(raw->value);

            return 0.f;
        }
    );
}

String SVGLengthValue::valueAsString() const
{
    return WTF::switchOn(m_value,
        [](const auto& value) {
            if (auto raw = value.raw()) {
                // FIXME: Handle calc() expressions and consider exponential notation for very large/small values
                float numericValue = clampTo<float>(raw->value);
                return formatCSSNumberValue(CSS::SerializableNumber { numericValue, CSS::unitString(raw->unit) });
            }

            return String();
        }
    );
}

AtomString SVGLengthValue::valueAsAtomString() const
{
    return makeAtomString(valueAsString());
}

ExceptionOr<float> SVGLengthValue::valueForBindings(const SVGLengthContext& context) const
{
    return WTF::switchOn(m_value,
        [&](const CSS::Number<>& number) -> ExceptionOr<float> {
            if (auto raw = number.raw())
                return raw->value;

            return Exception { ExceptionCode::NotFoundError };
        },
        [&](const CSS::LengthPercentage<>& length) -> ExceptionOr<float> {
            if (auto raw = length.raw())
                return context.resolveValueToUserUnits(raw->value, raw->unit, m_lengthMode);

            return Exception { ExceptionCode::NotFoundError };
        }
    );
}

void SVGLengthValue::setValueInSpecifiedUnits(float value)
{
    m_value = WTF::switchOn(m_value,
        [&](const CSS::Number<>&) -> decltype(m_value) {
            return CSS::Number<>(value);
        },
        [&](const CSS::LengthPercentage<>& current) -> decltype(m_value) {
            if (auto raw = current.raw())
                return CSS::LengthPercentage<>(raw->unit, value);

            return CSS::Number<>(value);
        }
    );
}

ExceptionOr<void> SVGLengthValue::setValue(const SVGLengthContext& context, float value)
{
    return WTF::switchOn(m_value,
        [&](const CSS::Number<>&) -> ExceptionOr<void> {
            m_value = CSS::Number<>(value);
            return { };
        },
        [&](const CSS::LengthPercentage<>& current) -> ExceptionOr<void> {
            if (auto raw = current.raw()) {
                auto resolvedValue = context.resolveValueFromUserUnits(value, raw->unit, m_lengthMode);
                if (resolvedValue.hasException())
                    return resolvedValue.releaseException();

                m_value = resolvedValue.releaseReturnValue();
                return { };
            }

            m_value = CSS::Number<>(value);
            return { };
        }
    );
}

ExceptionOr<void> SVGLengthValue::setValue(const SVGLengthContext& context, float value, SVGLengthType lengthType, SVGLengthMode lengthMode)
{
    // FIXME: Seems like a bug that we change the value of m_unit even if setValue throws an exception.
    m_lengthMode = lengthMode;
    m_value = createVariantForLengthType(value, lengthType);

    return setValue(context, value);
}

ExceptionOr<void> SVGLengthValue::setValueAsString(StringView string)
{
    if (string.isEmpty())
        return Exception { ExceptionCode::SyntaxError };

    // Trim leading and trailing whitespace to match SVG parsing expectations.
    auto trimmedString = string.toString().trim(isASCIIWhitespace);
    if (trimmedString.isEmpty())
        return Exception { ExceptionCode::SyntaxError };

    // CSS::Range only clamps to boundaries, but we historically handled
    // overflow values like "-45e58" to 0 instead of FLT_MAX.
    // FIXME: Consider setting to a proper value
    auto isFloatOverflow = [](const auto& parsedValue) {
        if (auto raw = parsedValue.raw()) {
            double value = raw->value;
            return value > FLT_MAX || value < -FLT_MAX;
        }
        return true;
    };

    auto parserContext = CSSParserContext { SVGAttributeMode };
    auto parserState = CSS::PropertyParserState {
        .context = parserContext
    };

    CSSTokenizer tokenizer(trimmedString);
    auto tokenRange = tokenizer.tokenRange();

    if (auto number = CSSPropertyParserHelpers::MetaConsumer<CSS::Number<>>::consume(tokenRange, parserState, { })) {
        if (!tokenRange.atEnd())
            return Exception { ExceptionCode::SyntaxError };

        m_value = isFloatOverflow(*number) ? CSS::Number<>(0) : WTFMove(*number);

        return { };
    }

    tokenRange = tokenizer.tokenRange();
    if (auto length = CSSPropertyParserHelpers::MetaConsumer<CSS::LengthPercentage<>>::consume(tokenRange, parserState, { })) {
        if (!tokenRange.atEnd())
            return Exception { ExceptionCode::SyntaxError };

        // FIXME: Add support for calculated lengths.
        if (length->isCalc())
            return Exception { ExceptionCode::SyntaxError };

        m_value = WTFMove(*length);

        return { };
    }

    return Exception { ExceptionCode::SyntaxError };
}

ExceptionOr<void> SVGLengthValue::convertToSpecifiedUnits(const SVGLengthContext& context, SVGLengthType targetType)
{
    auto valueInUserUnits = valueForBindings(context);
    if (valueInUserUnits.hasException())
        return valueInUserUnits.releaseException();

    float userUnits = valueInUserUnits.releaseReturnValue();

    if (targetType == SVGLengthType::Number) {
        m_value = CSS::Number<>(userUnits);
        return { };
    }

    auto convertedValue = context.resolveValueFromUserUnits(userUnits, svgLengthTypeToCSSLengthUnit(targetType), m_lengthMode);

    if (convertedValue.hasException())
        return convertedValue.releaseException();

    m_value = convertedValue.releaseReturnValue();
    return { };
}

TextStream& operator<<(TextStream& ts, const SVGLengthValue& length)
{
    ts << length.valueAsString();
    return ts;
}

}