1/*
2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#pragma once
22
23#include "Decimal.h"
24#include <wtf/Forward.h>
25
26namespace WebCore {
27
28enum AnyStepHandling { RejectAny, AnyIsDefaultStep };
29
30enum class RangeLimitations {
31 Valid,
32 Invalid
33};
34
35class StepRange {
36public:
37 enum StepValueShouldBe {
38 StepValueShouldBeReal,
39 ParsedStepValueShouldBeInteger,
40 ScaledStepValueShouldBeInteger,
41 };
42
43 struct StepDescription {
44 WTF_MAKE_FAST_ALLOCATED;
45 public:
46 int defaultStep { 1 };
47 int defaultStepBase { 0 };
48 int stepScaleFactor { 1 };
49 StepValueShouldBe stepValueShouldBe { StepValueShouldBeReal };
50
51 constexpr StepDescription(int defaultStep, int defaultStepBase, int stepScaleFactor, StepValueShouldBe stepValueShouldBe = StepValueShouldBeReal)
52 : defaultStep(defaultStep)
53 , defaultStepBase(defaultStepBase)
54 , stepScaleFactor(stepScaleFactor)
55 , stepValueShouldBe(stepValueShouldBe)
56 {
57 }
58
59 StepDescription() = default;
60
61 Decimal defaultValue() const
62 {
63 return defaultStep * stepScaleFactor;
64 }
65 };
66
67 StepRange();
68 StepRange(const StepRange&);
69 StepRange(const Decimal& stepBase, RangeLimitations, const Decimal& minimum, const Decimal& maximum, const Decimal& step, const StepDescription&);
70 Decimal acceptableError() const;
71 Decimal alignValueForStep(const Decimal& currentValue, const Decimal& newValue) const;
72 Decimal clampValue(const Decimal& value) const;
73 bool hasStep() const { return m_hasStep; }
74 bool hasRangeLimitations() const { return m_hasRangeLimitations; }
75 Decimal maximum() const { return m_maximum; }
76 Decimal minimum() const { return m_minimum; }
77 static Decimal parseStep(AnyStepHandling, const StepDescription&, const String&);
78 Decimal step() const { return m_step; }
79 Decimal stepBase() const { return m_stepBase; }
80 int stepScaleFactor() const { return m_stepDescription.stepScaleFactor; }
81 bool stepMismatch(const Decimal&) const;
82
83 // Clamp the middle value according to the step
84 Decimal defaultValue() const
85 {
86 return clampValue((m_minimum + m_maximum) / 2);
87 }
88
89 // Map value into 0-1 range
90 Decimal proportionFromValue(const Decimal& value) const
91 {
92 if (m_minimum == m_maximum)
93 return 0;
94
95 return (value - m_minimum) / (m_maximum - m_minimum);
96 }
97
98 // Map from 0-1 range to value
99 Decimal valueFromProportion(const Decimal& proportion) const
100 {
101 return m_minimum + proportion * (m_maximum - m_minimum);
102 }
103
104private:
105 StepRange& operator =(const StepRange&);
106 Decimal roundByStep(const Decimal& value, const Decimal& base) const;
107
108 const Decimal m_maximum; // maximum must be >= minimum.
109 const Decimal m_minimum;
110 const Decimal m_step;
111 const Decimal m_stepBase;
112 const StepDescription m_stepDescription;
113 const bool m_hasRangeLimitations { false };
114 const bool m_hasStep { false };
115};
116
117} // namespace WebCore
118