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
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller ( mueller@kde.org )
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights
* reserved.
* Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net)
*
* 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 "third_party/blink/renderer/platform/geometry/length.h"
#include <array>
#include "third_party/blink/renderer/platform/geometry/blend.h"
#include "third_party/blink/renderer/platform/geometry/calculation_value.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/size_assertions.h"
#include "third_party/blink/renderer/platform/wtf/static_constructors.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
namespace blink {
DEFINE_GLOBAL(PLATFORM_EXPORT, Length, g_auto_length);
DEFINE_GLOBAL(PLATFORM_EXPORT, Length, g_fill_available_length);
DEFINE_GLOBAL(PLATFORM_EXPORT, Length, g_stretch_length);
DEFINE_GLOBAL(PLATFORM_EXPORT, Length, g_fit_content_length);
DEFINE_GLOBAL(PLATFORM_EXPORT, Length, g_max_content_length);
DEFINE_GLOBAL(PLATFORM_EXPORT, Length, g_min_content_length);
DEFINE_GLOBAL(PLATFORM_EXPORT, Length, g_min_intrinsic_length);
// static
void Length::Initialize() {
new (WTF::NotNullTag::kNotNull, (void*)&g_auto_length) Length(kAuto);
new (WTF::NotNullTag::kNotNull, (void*)&g_fill_available_length)
Length(kFillAvailable);
new (WTF::NotNullTag::kNotNull, (void*)&g_stretch_length) Length(kStretch);
new (WTF::NotNullTag::kNotNull, (void*)&g_fit_content_length)
Length(kFitContent);
new (WTF::NotNullTag::kNotNull, (void*)&g_max_content_length)
Length(kMaxContent);
new (WTF::NotNullTag::kNotNull, (void*)&g_min_content_length)
Length(kMinContent);
new (WTF::NotNullTag::kNotNull, (void*)&g_min_intrinsic_length)
Length(kMinIntrinsic);
}
class CalculationValueHandleMap {
USING_FAST_MALLOC(CalculationValueHandleMap);
public:
CalculationValueHandleMap() = default;
CalculationValueHandleMap(const CalculationValueHandleMap&) = delete;
CalculationValueHandleMap& operator=(const CalculationValueHandleMap&) =
delete;
int insert(scoped_refptr<const CalculationValue> calc_value) {
DCHECK(index_);
// FIXME calc(): https://bugs.webkit.org/show_bug.cgi?id=80489
// This monotonically increasing handle generation scheme is potentially
// wasteful of the handle space. Consider reusing empty handles.
while (map_.Contains(index_))
index_++;
map_.Set(index_, std::move(calc_value));
return index_;
}
void Remove(int index) {
DCHECK(map_.Contains(index));
map_.erase(index);
}
const CalculationValue& Get(int index) {
DCHECK(map_.Contains(index));
return *map_.at(index);
}
void DecrementRef(int index) {
DCHECK(map_.Contains(index));
auto iter = map_.find(index);
if (iter->value->HasOneRef()) {
// Force the CalculationValue destructor early to avoid a potential
// recursive call inside HashMap remove().
iter->value = nullptr;
// |iter| may be invalidated during the CalculationValue destructor.
map_.erase(index);
} else {
iter->value->Release();
}
}
private:
int index_ = 1;
HashMap<int, scoped_refptr<const CalculationValue>> map_;
};
static CalculationValueHandleMap& CalcHandles() {
DEFINE_STATIC_LOCAL(CalculationValueHandleMap, handle_map, ());
return handle_map;
}
Length::Length(scoped_refptr<const CalculationValue> calc)
: quirk_(false), type_(kCalculated) {
calculation_handle_ = CalcHandles().insert(std::move(calc));
}
Length Length::BlendMixedTypes(const Length& from,
double progress,
ValueRange range) const {
DCHECK(from.CanConvertToCalculation());
DCHECK(CanConvertToCalculation());
return Length(
AsCalculationValue()->Blend(*from.AsCalculationValue(), progress, range));
}
Length Length::BlendSameTypes(const Length& from,
double progress,
ValueRange range) const {
Length::Type result_type = GetType();
if (IsZero())
result_type = from.GetType();
float blended_value =
blink::Blend(from.GetFloatValue(), GetFloatValue(), progress);
if (range == ValueRange::kNonNegative)
blended_value = ClampTo<float>(blended_value, 0);
return Length(blended_value, result_type);
}
PixelsAndPercent Length::GetPixelsAndPercent() const {
switch (GetType()) {
case kFixed:
return PixelsAndPercent(Pixels());
case kPercent:
return PixelsAndPercent(0.0f, Percent(), /*has_explicit_pixels=*/false,
/*has_explicit_percent=*/true);
case kCalculated:
return GetCalculationValue().GetPixelsAndPercent();
default:
NOTREACHED();
}
}
scoped_refptr<const CalculationValue> Length::AsCalculationValue() const {
if (IsCalculated())
return &GetCalculationValue();
return CalculationValue::Create(GetPixelsAndPercent(), ValueRange::kAll);
}
Length Length::SubtractFromOneHundredPercent() const {
if (IsPercent())
return Length::Percent(100 - Percent());
DCHECK(CanConvertToCalculation());
return Length(AsCalculationValue()->SubtractFromOneHundredPercent());
}
Length Length::Add(const Length& other) const {
CHECK(CanConvertToCalculation());
if (IsFixed() && other.IsFixed()) {
return Length::Fixed(Pixels() + other.Pixels());
}
if (IsPercent() && other.IsPercent()) {
return Length::Percent(Percent() + other.Percent());
}
return Length(AsCalculationValue()->Add(*other.AsCalculationValue()));
}
Length Length::Zoom(double factor) const {
switch (GetType()) {
case kFixed:
return Length::Fixed(GetFloatValue() * factor);
case kCalculated:
return Length(GetCalculationValue().Zoom(factor));
default:
return *this;
}
}
const CalculationValue& Length::GetCalculationValue() const {
DCHECK(IsCalculated());
return CalcHandles().Get(CalculationHandle());
}
void Length::IncrementCalculatedRef() const {
DCHECK(IsCalculated());
GetCalculationValue().AddRef();
}
void Length::DecrementCalculatedRef() const {
DCHECK(IsCalculated());
CalcHandles().DecrementRef(CalculationHandle());
}
float Length::NonNanCalculatedValue(float max_value,
const EvaluationInput& input) const {
DCHECK(IsCalculated());
float result = GetCalculationValue().Evaluate(max_value, input);
if (std::isnan(result))
return 0;
return result;
}
bool Length::HasOnlyFixedAndPercent() const {
if (GetType() == kFixed || GetType() == kPercent) {
return true;
}
if (GetType() == kCalculated) {
return GetCalculationValue().HasOnlyFixedAndPercent();
}
return false;
}
bool Length::HasAuto() const {
if (GetType() == kCalculated) {
return GetCalculationValue().HasAuto();
}
return GetType() == kAuto;
}
bool Length::HasContentOrIntrinsic() const {
if (GetType() == kCalculated) {
return GetCalculationValue().HasContentOrIntrinsicSize();
}
return GetType() == kMinContent || GetType() == kMaxContent ||
GetType() == kFitContent || GetType() == kMinIntrinsic ||
GetType() == kContent;
}
bool Length::HasAutoOrContentOrIntrinsic() const {
if (GetType() == kCalculated) {
return GetCalculationValue().HasAutoOrContentOrIntrinsicSize();
}
return GetType() == kAuto || HasContentOrIntrinsic();
}
bool Length::HasPercent() const {
if (GetType() == kCalculated) {
return GetCalculationValue().HasPercent();
}
return GetType() == kPercent;
}
bool Length::HasPercentOrStretch() const {
if (GetType() == kCalculated) {
return GetCalculationValue().HasPercentOrStretch();
}
return GetType() == kPercent || GetType() == kStretch ||
GetType() == kFillAvailable;
}
bool Length::HasStretch() const {
if (GetType() == kCalculated) {
return GetCalculationValue().HasStretch();
}
return GetType() == kStretch || GetType() == kFillAvailable;
}
bool Length::HasMinContent() const {
if (GetType() == kCalculated) {
return GetCalculationValue().HasMinContent();
}
return GetType() == kMinContent;
}
bool Length::HasMaxContent() const {
if (GetType() == kCalculated) {
return GetCalculationValue().HasMaxContent();
}
return GetType() == kMaxContent;
}
bool Length::HasFitContent() const {
if (GetType() == kCalculated) {
return GetCalculationValue().HasFitContent();
}
return GetType() == kFitContent;
}
bool Length::IsCalculatedEqual(const Length& o) const {
return IsCalculated() &&
(&GetCalculationValue() == &o.GetCalculationValue() ||
GetCalculationValue() == o.GetCalculationValue());
}
String Length::ToString() const {
StringBuilder builder;
builder.Append("Length(");
static const auto kTypeNames = std::to_array<const char* const>(
{"Auto", "Percent", "Fixed", "MinContent", "MaxContent", "MinIntrinsic",
"FillAvailable", "Stretch", "FitContent", "Calculated", "Flex",
"ExtendToZoom", "DeviceWidth", "DeviceHeight", "None", "Content"});
if (type_ < std::size(kTypeNames))
builder.Append(kTypeNames[type_]);
else
builder.Append("?");
builder.Append(", ");
if (IsCalculated()) {
builder.AppendNumber(calculation_handle_);
} else {
builder.AppendNumber(value_);
}
if (quirk_)
builder.Append(", Quirk");
builder.Append(")");
return builder.ToString();
}
std::ostream& operator<<(std::ostream& ostream, const Length& value) {
return ostream << value.ToString();
}
struct SameSizeAsLength {
int32_t value;
int32_t meta_data;
};
ASSERT_SIZE(Length, SameSizeAsLength);
} // namespace blink
|