1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "FontMetrics.h"
29#include "Length.h"
30#include <wtf/text/TextStream.h>
31
32namespace WebCore {
33
34class TextDecorationThickness {
35public:
36 static TextDecorationThickness createWithAuto()
37 {
38 return TextDecorationThickness(Type::Auto);
39 }
40 static TextDecorationThickness createFromFont()
41 {
42 return TextDecorationThickness(Type::FromFont);
43 }
44 static TextDecorationThickness createWithLength(float length)
45 {
46 TextDecorationThickness result(Type::Length);
47 result.setLengthValue(length);
48 return result;
49 }
50
51 bool isAuto() const
52 {
53 return m_type == Type::Auto;
54 }
55
56 bool isFromFont() const
57 {
58 return m_type == Type::FromFont;
59 }
60
61 bool isLength() const
62 {
63 return m_type == Type::Length;
64 }
65
66 void setLengthValue(float length)
67 {
68 ASSERT(isLength());
69 m_length = length;
70 }
71
72 float lengthValue() const
73 {
74 ASSERT(isLength());
75 return m_length;
76 }
77
78 float resolve(float fontSize, const FontMetrics& metrics) const
79 {
80 if (isAuto()) {
81 const float textDecorationBaseFontSize = 16;
82 return fontSize / textDecorationBaseFontSize;
83 }
84 if (isFromFont())
85 return metrics.underlineThickness();
86 ASSERT(isLength());
87 return m_length;
88 }
89
90 bool operator==(const TextDecorationThickness& other) const
91 {
92 switch (m_type) {
93 case Type::Auto:
94 case Type::FromFont:
95 return m_type == other.m_type;
96 case Type::Length:
97 return m_type == other.m_type && m_length == other.m_length;
98 default:
99 ASSERT_NOT_REACHED();
100 return true;
101 }
102 }
103
104 bool operator!=(const TextDecorationThickness& other) const
105 {
106 return !(*this == other);
107 }
108
109private:
110 enum class Type : uint8_t {
111 Auto,
112 FromFont,
113 Length
114 };
115
116 TextDecorationThickness(Type type)
117 : m_type { type }
118 {
119 }
120
121 Type m_type;
122 float m_length;
123};
124
125inline TextStream& operator<<(TextStream& ts, const TextDecorationThickness& thickness)
126{
127 if (thickness.isAuto())
128 ts << "auto";
129 else if (thickness.isFromFont())
130 ts << "from-font";
131 else
132 ts << TextStream::FormatNumberRespectingIntegers(thickness.lengthValue());
133 return ts;
134}
135
136} // namespace WebCore
137