File: SVGLength.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (277 lines) | stat: -rw-r--r-- 10,718 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
/*
 * Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
 * Copyright (C) 2007 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 "core/svg/SVGLength.h"

#include "core/SVGNames.h"
#include "core/css/CSSPrimitiveValue.h"
#include "core/css/CSSValue.h"
#include "core/css/parser/CSSParser.h"
#include "core/svg/SVGAnimationElement.h"
#include "wtf/MathExtras.h"
#include "wtf/text/WTFString.h"

namespace blink {

SVGLength::SVGLength(SVGLengthMode mode)
    : m_value(
          CSSPrimitiveValue::create(0, CSSPrimitiveValue::UnitType::UserUnits)),
      m_unitMode(static_cast<unsigned>(mode)) {
  ASSERT(unitMode() == mode);
}

SVGLength::SVGLength(const SVGLength& o)
    : m_value(o.m_value), m_unitMode(o.m_unitMode) {}

DEFINE_TRACE(SVGLength) {
  visitor->trace(m_value);
  SVGPropertyBase::trace(visitor);
}

SVGLength* SVGLength::clone() const {
  return new SVGLength(*this);
}

SVGPropertyBase* SVGLength::cloneForAnimation(const String& value) const {
  SVGLength* length = create();
  length->m_unitMode = m_unitMode;

  if (length->setValueAsString(value) != SVGParseStatus::NoError)
    length->m_value =
        CSSPrimitiveValue::create(0, CSSPrimitiveValue::UnitType::UserUnits);

  return length;
}

bool SVGLength::operator==(const SVGLength& other) const {
  return m_unitMode == other.m_unitMode && m_value == other.m_value;
}

float SVGLength::value(const SVGLengthContext& context) const {
  if (isCalculated())
    return context.resolveValue(asCSSPrimitiveValue(), unitMode());

  return context.convertValueToUserUnits(m_value->getFloatValue(), unitMode(),
                                         m_value->typeWithCalcResolved());
}

void SVGLength::setValueAsNumber(float value) {
  m_value =
      CSSPrimitiveValue::create(value, CSSPrimitiveValue::UnitType::UserUnits);
}

void SVGLength::setValue(float value, const SVGLengthContext& context) {
  m_value = CSSPrimitiveValue::create(
      context.convertValueFromUserUnits(value, unitMode(),
                                        m_value->typeWithCalcResolved()),
      m_value->typeWithCalcResolved());
}

static bool isCalcCSSUnitType(CSSPrimitiveValue::UnitType type) {
  return type >= CSSPrimitiveValue::UnitType::Calc &&
         type <= CSSPrimitiveValue::UnitType::CalcPercentageWithLengthAndNumber;
}

static bool isSupportedCSSUnitType(CSSPrimitiveValue::UnitType type) {
  return (CSSPrimitiveValue::isLength(type) ||
          type == CSSPrimitiveValue::UnitType::Number ||
          type == CSSPrimitiveValue::UnitType::Percentage ||
          isCalcCSSUnitType(type)) &&
         type != CSSPrimitiveValue::UnitType::QuirkyEms;
}

void SVGLength::setUnitType(CSSPrimitiveValue::UnitType type) {
  ASSERT(isSupportedCSSUnitType(type));
  m_value = CSSPrimitiveValue::create(m_value->getFloatValue(), type);
}

float SVGLength::valueAsPercentage() const {
  // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy
  // but could eventually be changed.
  if (m_value->isPercentage()) {
    // Note: This division is a source of floating point inaccuracy.
    return m_value->getFloatValue() / 100;
  }

  return m_value->getFloatValue();
}

float SVGLength::valueAsPercentage100() const {
  // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy
  // but could eventually be changed.
  if (m_value->isPercentage())
    return m_value->getFloatValue();

  return m_value->getFloatValue() * 100;
}

float SVGLength::scaleByPercentage(float input) const {
  float result = input * m_value->getFloatValue();
  if (m_value->isPercentage()) {
    // Delaying division by 100 as long as possible since it introduces floating
    // point errors.
    result = result / 100;
  }
  return result;
}

SVGParsingError SVGLength::setValueAsString(const String& string) {
  if (string.isEmpty()) {
    m_value =
        CSSPrimitiveValue::create(0, CSSPrimitiveValue::UnitType::UserUnits);
    return SVGParseStatus::NoError;
  }

  CSSParserContext* svgParserContext =
      CSSParserContext::create(SVGAttributeMode);
  const CSSValue* parsed =
      CSSParser::parseSingleValue(CSSPropertyX, string, svgParserContext);
  if (!parsed || !parsed->isPrimitiveValue())
    return SVGParseStatus::ExpectedLength;

  const CSSPrimitiveValue* newValue = toCSSPrimitiveValue(parsed);
  if (!isSupportedCSSUnitType(newValue->typeWithCalcResolved()))
    return SVGParseStatus::ExpectedLength;

  m_value = newValue;
  return SVGParseStatus::NoError;
}

String SVGLength::valueAsString() const {
  return m_value->customCSSText();
}

void SVGLength::newValueSpecifiedUnits(CSSPrimitiveValue::UnitType type,
                                       float value) {
  m_value = CSSPrimitiveValue::create(value, type);
}

void SVGLength::convertToSpecifiedUnits(CSSPrimitiveValue::UnitType type,
                                        const SVGLengthContext& context) {
  ASSERT(isSupportedCSSUnitType(type));

  float valueInUserUnits = value(context);
  m_value = CSSPrimitiveValue::create(
      context.convertValueFromUserUnits(valueInUserUnits, unitMode(), type),
      type);
}

SVGLengthMode SVGLength::lengthModeForAnimatedLengthAttribute(
    const QualifiedName& attrName) {
  typedef HashMap<QualifiedName, SVGLengthMode> LengthModeForLengthAttributeMap;
  DEFINE_STATIC_LOCAL(LengthModeForLengthAttributeMap, s_lengthModeMap, ());

  if (s_lengthModeMap.isEmpty()) {
    s_lengthModeMap.set(SVGNames::xAttr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::yAttr, SVGLengthMode::Height);
    s_lengthModeMap.set(SVGNames::cxAttr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::cyAttr, SVGLengthMode::Height);
    s_lengthModeMap.set(SVGNames::dxAttr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::dyAttr, SVGLengthMode::Height);
    s_lengthModeMap.set(SVGNames::frAttr, SVGLengthMode::Other);
    s_lengthModeMap.set(SVGNames::fxAttr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::fyAttr, SVGLengthMode::Height);
    s_lengthModeMap.set(SVGNames::rAttr, SVGLengthMode::Other);
    s_lengthModeMap.set(SVGNames::rxAttr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::ryAttr, SVGLengthMode::Height);
    s_lengthModeMap.set(SVGNames::widthAttr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::heightAttr, SVGLengthMode::Height);
    s_lengthModeMap.set(SVGNames::x1Attr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::x2Attr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::y1Attr, SVGLengthMode::Height);
    s_lengthModeMap.set(SVGNames::y2Attr, SVGLengthMode::Height);
    s_lengthModeMap.set(SVGNames::refXAttr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::refYAttr, SVGLengthMode::Height);
    s_lengthModeMap.set(SVGNames::markerWidthAttr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::markerHeightAttr, SVGLengthMode::Height);
    s_lengthModeMap.set(SVGNames::textLengthAttr, SVGLengthMode::Width);
    s_lengthModeMap.set(SVGNames::startOffsetAttr, SVGLengthMode::Width);
  }

  if (s_lengthModeMap.contains(attrName))
    return s_lengthModeMap.get(attrName);

  return SVGLengthMode::Other;
}

bool SVGLength::negativeValuesForbiddenForAnimatedLengthAttribute(
    const QualifiedName& attrName) {
  DEFINE_STATIC_LOCAL(
      HashSet<QualifiedName>, noNegativeValuesSet,
      ({
          SVGNames::frAttr, SVGNames::rAttr, SVGNames::rxAttr, SVGNames::ryAttr,
          SVGNames::widthAttr, SVGNames::heightAttr, SVGNames::markerWidthAttr,
          SVGNames::markerHeightAttr, SVGNames::textLengthAttr,
      }));
  return noNegativeValuesSet.contains(attrName);
}

void SVGLength::add(SVGPropertyBase* other, SVGElement* contextElement) {
  SVGLengthContext lengthContext(contextElement);
  setValue(value(lengthContext) + toSVGLength(other)->value(lengthContext),
           lengthContext);
}

void SVGLength::calculateAnimatedValue(SVGAnimationElement* animationElement,
                                       float percentage,
                                       unsigned repeatCount,
                                       SVGPropertyBase* fromValue,
                                       SVGPropertyBase* toValue,
                                       SVGPropertyBase* toAtEndOfDurationValue,
                                       SVGElement* contextElement) {
  SVGLength* fromLength = toSVGLength(fromValue);
  SVGLength* toLength = toSVGLength(toValue);
  SVGLength* toAtEndOfDurationLength = toSVGLength(toAtEndOfDurationValue);

  SVGLengthContext lengthContext(contextElement);
  float animatedNumber = value(lengthContext);
  animationElement->animateAdditiveNumber(
      percentage, repeatCount, fromLength->value(lengthContext),
      toLength->value(lengthContext),
      toAtEndOfDurationLength->value(lengthContext), animatedNumber);

  ASSERT(unitMode() == lengthModeForAnimatedLengthAttribute(
                           animationElement->attributeName()));

  // TODO(shanmuga.m): Construct a calc() expression if the units fall in
  // different categories.
  CSSPrimitiveValue::UnitType newUnit = CSSPrimitiveValue::UnitType::UserUnits;
  if (percentage < 0.5) {
    if (!fromLength->isCalculated())
      newUnit = fromLength->typeWithCalcResolved();
  } else {
    if (!toLength->isCalculated())
      newUnit = toLength->typeWithCalcResolved();
  }
  animatedNumber = lengthContext.convertValueFromUserUnits(animatedNumber,
                                                           unitMode(), newUnit);
  m_value = CSSPrimitiveValue::create(animatedNumber, newUnit);
}

float SVGLength::calculateDistance(SVGPropertyBase* toValue,
                                   SVGElement* contextElement) {
  SVGLengthContext lengthContext(contextElement);
  SVGLength* toLength = toSVGLength(toValue);

  return fabsf(toLength->value(lengthContext) - value(lengthContext));
}

}  // namespace blink