1 // Copyright 2017 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_LAYOUT_NG_INLINE_NG_INLINE_BREAK_TOKEN_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_NG_INLINE_NG_INLINE_BREAK_TOKEN_H_
7 
8 #include "third_party/blink/renderer/core/core_export.h"
9 #include "third_party/blink/renderer/core/layout/ng/inline/ng_inline_node.h"
10 #include "third_party/blink/renderer/core/layout/ng/ng_break_token.h"
11 #include "third_party/blink/renderer/platform/wtf/casting.h"
12 
13 namespace blink {
14 
15 // Represents a break token for an inline node.
16 class CORE_EXPORT NGInlineBreakToken final : public NGBreakToken {
17  public:
18   enum NGInlineBreakTokenFlags {
19     kDefault = 0,
20     kIsForcedBreak = 1 << 0,
21     kUseFirstLineStyle = 1 << 1
22     // When adding values, ensure |flags_| has enough storage.
23   };
24 
25   // Creates a break token for a node which did fragment, and can potentially
26   // produce more fragments.
27   // Takes ownership of the state_stack.
Create(NGInlineNode node,const ComputedStyle * style,unsigned item_index,unsigned text_offset,unsigned flags)28   static scoped_refptr<NGInlineBreakToken> Create(
29       NGInlineNode node,
30       const ComputedStyle* style,
31       unsigned item_index,
32       unsigned text_offset,
33       unsigned flags /* NGInlineBreakTokenFlags */) {
34     return base::AdoptRef(new NGInlineBreakToken(
35         PassKey(), node, style, item_index, text_offset, flags));
36   }
37 
38   // Creates a break token for a node which cannot produce any more fragments.
Create(NGLayoutInputNode node)39   static scoped_refptr<NGInlineBreakToken> Create(NGLayoutInputNode node) {
40     return base::AdoptRef(new NGInlineBreakToken(PassKey(), node));
41   }
42 
43   ~NGInlineBreakToken() override;
44 
45   // The style at the end of this break token. The next line should start with
46   // this style.
Style()47   const ComputedStyle* Style() const {
48     DCHECK(!IsFinished());
49     return style_.get();
50   }
51 
ItemIndex()52   unsigned ItemIndex() const {
53     DCHECK(!IsFinished());
54     return item_index_;
55   }
56 
TextOffset()57   unsigned TextOffset() const {
58     DCHECK(!IsFinished());
59     return text_offset_;
60   }
61 
UseFirstLineStyle()62   bool UseFirstLineStyle() const {
63     DCHECK(!IsFinished());
64     return flags_ & kUseFirstLineStyle;
65   }
66 
IsForcedBreak()67   bool IsForcedBreak() const {
68     DCHECK(!IsFinished());
69     return flags_ & kIsForcedBreak;
70   }
71 
72   using PassKey = util::PassKey<NGInlineBreakToken>;
73   NGInlineBreakToken(PassKey,
74                      NGInlineNode node,
75                      const ComputedStyle*,
76                      unsigned item_index,
77                      unsigned text_offset,
78                      unsigned flags /* NGInlineBreakTokenFlags */);
79 
80   explicit NGInlineBreakToken(PassKey, NGLayoutInputNode node);
81 
82 #if DCHECK_IS_ON()
83   String ToString() const override;
84 #endif
85 
86  private:
87   scoped_refptr<const ComputedStyle> style_;
88   unsigned item_index_;
89   unsigned text_offset_;
90 };
91 
92 template <>
93 struct DowncastTraits<NGInlineBreakToken> {
94   static bool AllowFrom(const NGBreakToken& token) {
95     return token.IsInlineType();
96   }
97 };
98 
99 }  // namespace blink
100 
101 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_NG_INLINE_NG_INLINE_BREAK_TOKEN_H_
102