1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_GRID_TRACK_LIST_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_GRID_TRACK_LIST_H_
7 
8 #include "third_party/blink/renderer/core/core_export.h"
9 #include "third_party/blink/renderer/core/style/grid_track_size.h"
10 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
11 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
12 #include "third_party/blink/renderer/platform/wtf/vector.h"
13 
14 namespace blink {
15 
16 // Stores tracks related data by compressing repeated tracks into a single node.
17 struct NGGridTrackRepeater {
18   enum RepeatType {
19     kNoAutoRepeat,
20     kAutoFill,
21     kAutoFit,
22   };
23   NGGridTrackRepeater(wtf_size_t repeat_index,
24                       wtf_size_t repeat_size,
25                       wtf_size_t repeat_count,
26                       RepeatType repeat_type);
27   String ToString() const;
28   bool operator==(const NGGridTrackRepeater& o) const;
29 
30   // |NGGridTrackList| will store the sizes for each track in this repeater
31   // consecutively in a single vector for all repeaters; this index specifies
32   // the position of the first track size that belongs to this repeater.
33   wtf_size_t repeat_index;
34   // Amount of tracks to be repeated.
35   wtf_size_t repeat_size;
36   // Amount of times the group of tracks are repeated.
37   wtf_size_t repeat_count;
38   // Type of repetition.
39   RepeatType repeat_type;
40 };
41 
42 class CORE_EXPORT NGGridTrackList {
43  public:
44   NGGridTrackList() = default;
45   NGGridTrackList(const NGGridTrackList& other) = default;
46   // Returns the repeat count of the repeater at |index|, or |auto_value|
47   // if the repeater is auto.
48   wtf_size_t RepeatCount(const wtf_size_t index,
49                          const wtf_size_t auto_value) const;
50   // Returns the number of tracks in the repeater at |index|.
51   wtf_size_t RepeatSize(const wtf_size_t index) const;
52   // Returns the repeat type of the repeater at |index|.
53   NGGridTrackRepeater::RepeatType RepeatType(const wtf_size_t index) const;
54   // Returns the size of the |n|-th specified track of the repeater at |index|.
55   const GridTrackSize& RepeatTrackSize(const wtf_size_t index,
56                                        const wtf_size_t n) const;
57   // Returns the count of repeaters.
58   wtf_size_t RepeaterCount() const;
59   // Returns the total count of all the tracks in this list.
60   wtf_size_t TotalTrackCount() const;
61 
62   // Adds a non-auto repeater.
63   bool AddRepeater(const Vector<GridTrackSize>& repeater_track_sizes,
64                    wtf_size_t repeat_count);
65   // Adds an auto repeater.
66   bool AddAutoRepeater(const Vector<GridTrackSize>& repeater_track_sizes,
67                        NGGridTrackRepeater::RepeatType repeat_type);
68   // Returns true if this list contains an auto repeater.
69   bool HasAutoRepeater() const;
70 
71   // Clears all data.
72   void Clear();
73 
74   String ToString() const;
75 
76   void operator=(const NGGridTrackList& o);
77   bool operator==(const NGGridTrackList& o) const;
78 
79  private:
80   bool AddRepeater(const Vector<GridTrackSize>& repeater_track_sizes,
81                    NGGridTrackRepeater::RepeatType repeat_type,
82                    wtf_size_t repeat_count);
83   // Returns the amount of tracks available before overflow.
84   wtf_size_t AvailableTrackCount() const;
85 
86   Vector<NGGridTrackRepeater> repeaters_;
87 
88   // Stores the track sizes of every repeater added to this list; tracks from
89   // the same repeater group are stored consecutively.
90   Vector<GridTrackSize> repeater_track_sizes_;
91 
92   // The index of the automatic repeater, if there is one; |kInvalidRangeIndex|
93   // otherwise.
94   wtf_size_t auto_repeater_index_ = kNotFound;
95   // Total count of tracks.
96   wtf_size_t total_track_count_ = 0;
97 };
98 
99 // This class wraps both legacy grid track list type, and the GridNG version:
100 // Vector<GridTrackSize>, and NGGridTrackList respectively. The NGGridTrackList
101 // is stored in a pointer to keep size down when not running GridNG.
102 class GridTrackList {
103   DISALLOW_NEW();
104 
105  public:
106   GridTrackList();
107 
108   GridTrackList(const GridTrackList& other);
109   explicit GridTrackList(const GridTrackSize& default_track_size);
110   explicit GridTrackList(Vector<GridTrackSize>& legacy_tracks);
111 
112   Vector<GridTrackSize>& LegacyTrackList();
113   const Vector<GridTrackSize>& LegacyTrackList() const;
114 
115   NGGridTrackList& NGTrackList();
116   const NGGridTrackList& NGTrackList() const;
117 
118   void operator=(const GridTrackList& other);
119   bool operator==(const GridTrackList& other) const;
120   bool operator!=(const GridTrackList& other) const;
121 
122  private:
123   void AssignFrom(const GridTrackList& other);
124   Vector<GridTrackSize> legacy_track_list_;
125   std::unique_ptr<NGGridTrackList> ng_track_list_;
126 };
127 
128 }  // namespace blink
129 
130 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_GRID_TRACK_LIST_H_
131