File: length.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (422 lines) | stat: -rw-r--r-- 14,560 bytes parent folder | download | duplicates (2)
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
    Copyright (C) 1999 Lars Knoll (knoll@kde.org)
    Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
    Copyright (C) 2011 Rik Cabanier (cabanier@adobe.com)
    Copyright (C) 2011 Adobe Systems Incorporated. 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.
*/

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_LENGTH_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_LENGTH_H_

#include <cmath>
#include <cstring>
#include <optional>

#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/memory/stack_allocated.h"
#include "base/notreached.h"
#include "third_party/blink/renderer/platform/geometry/evaluation_input.h"
#include "third_party/blink/renderer/platform/geometry/layout_unit.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"

namespace WTF {
class String;
}  // namespace WTF

namespace blink {

struct PixelsAndPercent {
  DISALLOW_NEW();
  explicit PixelsAndPercent(float pixels)
      : pixels(pixels),
        percent(0.0f),
        has_explicit_pixels(true),
        has_explicit_percent(false) {}
  PixelsAndPercent(float pixels,
                   float percent,
                   bool has_explicit_pixels,
                   bool has_explicit_percent)
      : pixels(pixels),
        percent(percent),
        has_explicit_pixels(has_explicit_pixels),
        has_explicit_percent(has_explicit_percent) {}

  PixelsAndPercent& operator+=(const PixelsAndPercent& rhs) {
    pixels += rhs.pixels;
    percent += rhs.percent;
    has_explicit_pixels |= rhs.has_explicit_pixels;
    has_explicit_percent |= rhs.has_explicit_percent;
    return *this;
  }
  friend PixelsAndPercent operator+(PixelsAndPercent lhs,
                                    const PixelsAndPercent& rhs) {
    lhs += rhs;
    return lhs;
  }
  PixelsAndPercent& operator-=(const PixelsAndPercent& rhs) {
    pixels -= rhs.pixels;
    percent -= rhs.percent;
    has_explicit_pixels |= rhs.has_explicit_pixels;
    has_explicit_percent |= rhs.has_explicit_percent;
    return *this;
  }
  PixelsAndPercent& operator*=(float number) {
    pixels *= number;
    percent *= number;
    return *this;
  }

  float pixels;
  float percent;
  bool has_explicit_pixels;
  bool has_explicit_percent;
};

class CalculationValue;
class Length;

PLATFORM_EXPORT extern const Length& g_auto_length;
PLATFORM_EXPORT extern const Length& g_fill_available_length;
PLATFORM_EXPORT extern const Length& g_stretch_length;
PLATFORM_EXPORT extern const Length& g_fit_content_length;
PLATFORM_EXPORT extern const Length& g_max_content_length;
PLATFORM_EXPORT extern const Length& g_min_content_length;
PLATFORM_EXPORT extern const Length& g_min_intrinsic_length;

class PLATFORM_EXPORT Length {
  DISALLOW_NEW();

 public:
  // Initializes global instances.
  static void Initialize();

  enum class ValueRange { kAll, kNonNegative };

  // FIXME: This enum makes it hard to tell in general what values may be
  // appropriate for any given Length.
  enum Type : unsigned char {
    kAuto,
    kPercent,
    kFixed,
    kMinContent,
    kMaxContent,
    kMinIntrinsic,
    kFillAvailable,
    kStretch,
    kFitContent,
    kCalculated,
    kFlex,
    kExtendToZoom,
    kDeviceWidth,
    kDeviceHeight,
    kNone,    // only valid for max-width, max-height, or contain-intrinsic-size
    kContent  // only valid for flex-basis
  };

  Length() : value_(0), type_(kAuto) {}

  explicit Length(Length::Type t) : value_(0), type_(t) {
    DCHECK_NE(t, kCalculated);
  }

  Length(int v, Length::Type t) : value_(v), type_(t) {
    DCHECK_NE(t, kCalculated);
  }

  Length(LayoutUnit v, Length::Type t) : value_(v.ToFloat()), type_(t) {
    DCHECK(std::isfinite(v.ToFloat()));
    DCHECK_NE(t, kCalculated);
  }

  Length(float v, Length::Type t) : value_(v), type_(t) {
    DCHECK(std::isfinite(v));
    DCHECK_NE(t, kCalculated);
  }

  Length(double v, Length::Type t) : type_(t) {
    DCHECK(std::isfinite(v));
    value_ = ClampTo<float>(v);
  }

  explicit Length(scoped_refptr<const CalculationValue>);

  Length(const Length& length) {
    UNSAFE_TODO(memcpy(this, &length, sizeof(Length)));
    if (IsCalculated())
      IncrementCalculatedRef();
  }

  Length& operator=(const Length& length) {
    if (length.IsCalculated())
      length.IncrementCalculatedRef();
    if (IsCalculated())
      DecrementCalculatedRef();
    UNSAFE_TODO(memcpy(this, &length, sizeof(Length)));
    return *this;
  }

  ~Length() {
    if (IsCalculated())
      DecrementCalculatedRef();
  }

  bool operator==(const Length& o) const {
    if (type_ != o.type_ || quirk_ != o.quirk_) {
      return false;
    }
    if (type_ == kCalculated) {
      return IsCalculatedEqual(o);
    } else {
      // For everything that doesn't use value_, it is defined to be zero,
      // so we can compare here unconditionally.
      return value_ == o.value_;
    }
  }
  bool operator!=(const Length& o) const { return !(*this == o); }

  static const Length& Auto() { return g_auto_length; }
  static const Length& FillAvailable() { return g_fill_available_length; }
  static const Length& Stretch() { return g_stretch_length; }
  static const Length& FitContent() { return g_fit_content_length; }
  static const Length& MaxContent() { return g_max_content_length; }
  static const Length& MinContent() { return g_min_content_length; }
  static const Length& MinIntrinsic() { return g_min_intrinsic_length; }

  static Length Content() { return Length(kContent); }
  static Length Fixed() { return Length(kFixed); }
  static Length None() { return Length(kNone); }

  static Length ExtendToZoom() { return Length(kExtendToZoom); }
  static Length DeviceWidth() { return Length(kDeviceWidth); }
  static Length DeviceHeight() { return Length(kDeviceHeight); }

  template <typename NUMBER_TYPE>
  static Length Fixed(NUMBER_TYPE number) {
    return Length(number, kFixed);
  }
  template <typename NUMBER_TYPE>
  static Length Percent(NUMBER_TYPE number) {
    return Length(number, kPercent);
  }
  static Length Flex(float value) { return Length(value, kFlex); }

  int IntValue() const {
    if (IsCalculated()) {
      NOTREACHED();
    }
    DCHECK(!IsNone());
    return static_cast<int>(value_);
  }

  float Pixels() const {
    DCHECK_EQ(GetType(), kFixed);
    return GetFloatValue();
  }
  float Percent() const {
    DCHECK_EQ(GetType(), kPercent);
    return GetFloatValue();
  }
  float Flex() const {
    DCHECK_EQ(GetType(), kFlex);
    return GetFloatValue();
  }

  PixelsAndPercent GetPixelsAndPercent() const;

  const CalculationValue& GetCalculationValue() const;

  // If |this| is calculated, returns the underlying |CalculationValue|. If not,
  // returns a |CalculationValue| constructed from |GetPixelsAndPercent()|. Hits
  // a DCHECK if |this| is not a specified value (e.g., 'auto').
  scoped_refptr<const CalculationValue> AsCalculationValue() const;

  Length::Type GetType() const { return static_cast<Length::Type>(type_); }
  bool Quirk() const { return quirk_; }

  void SetQuirk(bool quirk) { quirk_ = quirk; }

  bool IsNone() const { return GetType() == kNone; }

  // FIXME calc: https://bugs.webkit.org/show_bug.cgi?id=80357. A calculated
  // Length always contains a percentage, and without a maxValue passed to these
  // functions it's impossible to determine the sign or zero-ness. We assume all
  // calc values are positive and non-zero for now.
  bool IsZero() const {
    DCHECK(!IsNone());
    if (IsCalculated())
      return false;

    return !value_;
  }

  // If this is a length in a property that accepts calc-size(), use
  // |HasAuto()|.  If this |Length| is a block-axis size
  // |HasAutoOrContentOrIntrinsic()| is usually a better choice.
  bool IsAuto() const { return GetType() == kAuto; }
  bool IsFixed() const { return GetType() == kFixed; }

  // For the block axis, intrinsic sizes such as `min-content` behave the same
  // as `auto`. https://www.w3.org/TR/css-sizing-3/#valdef-width-min-content
  // This includes content-based sizes in calc-size().
  bool HasAuto() const;
  bool HasContentOrIntrinsic() const;
  bool HasAutoOrContentOrIntrinsic() const;
  // HasPercent and HasPercentOrStretch refer to whether the toplevel value
  // should be treated as a percentage type for web-exposed behavior
  // decisions.  However, a value can still depend on a percentage when
  // HasPercent() is false:  for example, calc-size(any, 20%).
  bool HasPercent() const;
  bool HasPercentOrStretch() const;
  bool HasStretch() const;

  bool HasMinContent() const;
  bool HasMaxContent() const;
  bool HasMinIntrinsic() const { return IsMinIntrinsic(); }
  bool HasFitContent() const;

  // CanConvertToCalculation() is true for any Lengths that are a fixed length,
  // a percent, or a calc() expression.  Note that this *includes* calc-size()
  // expressions that contain sizing keywords, which may not be what you want.
  //
  // Compare to HasOnlyFixedAndPercent.  (The difference is relevant only when
  // sizing keywords may be present.)
  //
  // Note that in some contexts sizing keywords can be converted to
  // calculation expressions, but this function does *not* return true for
  // those cases; the caller is required to convert appropriately.
  bool CanConvertToCalculation() const {
    return GetType() == kFixed || GetType() == kPercent ||
           GetType() == kCalculated;
  }

  // HasOnlyFixedAndPercent() is true for any Lengths that are a fixed length,
  // a percent, or calc() expressions that consist only of those.  (This
  // excludes calc() expressions with calc-size() that depend on sizing
  // keywords.)
  // Compare to CanConvertToCalculation.  (The difference is relevant only
  // when sizing keywords may be present.)
  bool HasOnlyFixedAndPercent() const;

  bool IsCalculated() const { return GetType() == kCalculated; }
  bool IsCalculatedEqual(const Length&) const;

  // These type checking methods should be used with extreme caution;
  // many uses probably want the Has* methods above to work correctly
  // with calc-size().
  bool IsMinContent() const { return GetType() == kMinContent; }
  bool IsMaxContent() const { return GetType() == kMaxContent; }
  bool IsMinIntrinsic() const { return GetType() == kMinIntrinsic; }
  bool IsFillAvailable() const { return GetType() == kFillAvailable; }
  bool IsStretch() const { return GetType() == kStretch; }
  bool IsFitContent() const { return GetType() == kFitContent; }
  bool IsPercent() const { return GetType() == kPercent; }
  // MayHavePercentDependence should be used to decide whether to optimize
  // away computing the value on which percentages depend or optimize away
  // recomputation that results from changes to that value.  It is intended to
  // be used *only* in cases where the implementation could be changed to one
  // that returns true only if there are percentage values somewhere in the
  // expression (that is, one that still returns true for calc-size(any, 30%)
  // for which HasPercent() is false, but is false for calc-size(any, 30px)).
  //
  // We could (if we want) make this exact and remove "May" from the name.
  // But this would require looking into the calculation value like HasPercent
  // does.  However, it needs to be different from HasPercent because of cases
  // where calc-size() erases percentage-ness from the type, like
  // calc-size(any, 20%).
  //
  // For properties that cannot have calc-size in them, we currently use
  // HasPercent() rather than MayHavePercentDependence() since it's a
  // shorter/simpler function name, and the two functions are equivalent in
  // that case.
  bool MayHavePercentDependence() const {
    return GetType() == kPercent || GetType() == kCalculated;
  }
  bool IsFlex() const { return GetType() == kFlex; }
  bool IsExtendToZoom() const { return GetType() == kExtendToZoom; }
  bool IsDeviceWidth() const { return GetType() == kDeviceWidth; }
  bool IsDeviceHeight() const { return GetType() == kDeviceHeight; }

  Length Blend(const Length& from, double progress, ValueRange range) const {
    DCHECK(CanConvertToCalculation());
    DCHECK(from.CanConvertToCalculation());

    if (progress == 0.0)
      return from;

    if (progress == 1.0)
      return *this;

    if (from.GetType() == kCalculated || GetType() == kCalculated)
      return BlendMixedTypes(from, progress, range);

    if (!from.IsZero() && !IsZero() && from.GetType() != GetType())
      return BlendMixedTypes(from, progress, range);

    if (from.IsZero() && IsZero())
      return *this;

    return BlendSameTypes(from, progress, range);
  }

  float NonNanCalculatedValue(float max_value, const EvaluationInput&) const;

  Length SubtractFromOneHundredPercent() const;

  Length Add(const Length& other) const;

  Length Zoom(double factor) const;

  WTF::String ToString() const;

 private:
  float GetFloatValue() const {
    DCHECK(!IsNone());
    DCHECK(!IsCalculated());
    return value_;
  }

  Length BlendMixedTypes(const Length& from, double progress, ValueRange) const;

  Length BlendSameTypes(const Length& from, double progress, ValueRange) const;

  int CalculationHandle() const {
    DCHECK(IsCalculated());
    return calculation_handle_;
  }
  void IncrementCalculatedRef() const;
  void DecrementCalculatedRef() const;

  union {
    // If kType == kCalculated.
    int calculation_handle_;

    // Otherwise. Must be zero if not in use (e.g., for kAuto or kNone).
    float value_;
  };
  bool quirk_ = false;
  unsigned char type_;
};

PLATFORM_EXPORT std::ostream& operator<<(std::ostream&, const Length&);

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_LENGTH_H_