OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/layout/ng/ng_text_layout_algorithm.h" | 5 #include "core/layout/ng/ng_line_breaker.h" |
6 | 6 |
7 #include "core/layout/ng/ng_box_fragment.h" | 7 #include "core/layout/ng/ng_box_fragment.h" |
8 #include "core/layout/ng/ng_break_token.h" | 8 #include "core/layout/ng/ng_break_token.h" |
9 #include "core/layout/ng/ng_constraint_space.h" | 9 #include "core/layout/ng/ng_constraint_space.h" |
10 #include "core/layout/ng/ng_fragment_builder.h" | 10 #include "core/layout/ng/ng_fragment_builder.h" |
| 11 #include "core/layout/ng/ng_inline_layout_algorithm.h" |
11 #include "core/layout/ng/ng_inline_node.h" | 12 #include "core/layout/ng/ng_inline_node.h" |
12 #include "core/layout/ng/ng_layout_opportunity_iterator.h" | 13 #include "core/layout/ng/ng_layout_opportunity_iterator.h" |
13 #include "core/layout/ng/ng_line_builder.h" | |
14 #include "core/layout/ng/ng_text_fragment.h" | 14 #include "core/layout/ng/ng_text_fragment.h" |
15 #include "core/style/ComputedStyle.h" | 15 #include "core/style/ComputedStyle.h" |
16 #include "platform/text/TextBreakIterator.h" | 16 #include "platform/text/TextBreakIterator.h" |
17 | 17 |
18 namespace blink { | 18 namespace blink { |
19 | 19 |
20 NGTextLayoutAlgorithm::NGTextLayoutAlgorithm( | |
21 NGInlineNode* inline_box, | |
22 NGBreakToken* break_token) | |
23 : inline_box_(inline_box), | |
24 break_token_(break_token) { | |
25 DCHECK(inline_box_); | |
26 } | |
27 | |
28 RefPtr<NGLayoutResult> NGTextLayoutAlgorithm::Layout() { | |
29 NOTREACHED(); | |
30 return nullptr; | |
31 } | |
32 | |
33 static bool IsHangable(UChar ch) { | 20 static bool IsHangable(UChar ch) { |
34 return ch == ' '; | 21 return ch == ' '; |
35 } | 22 } |
36 | 23 |
37 void NGTextLayoutAlgorithm::LayoutInline(NGLineBuilder* line_builder) { | 24 void NGLineBreaker::BreakLines(NGInlineLayoutAlgorithm* algorithm, |
38 // TODO(kojii): Make this tickable. Each line is easy. Needs more thoughts | 25 const String& text_content, |
39 // for each fragment in a line. Bidi reordering is probably atomic. | 26 unsigned current_offset) { |
40 // TODO(kojii): oof is not well-thought yet. The bottom static position may be | |
41 // in the next line, https://github.com/w3c/csswg-drafts/issues/609 | |
42 const String& text_content = inline_box_->Text(); | |
43 DCHECK(!text_content.isEmpty()); | 27 DCHECK(!text_content.isEmpty()); |
44 // TODO(kojii): Give the locale to LazyLineBreakIterator. | 28 // TODO(kojii): Give the locale to LazyLineBreakIterator. |
45 LazyLineBreakIterator line_break_iterator(text_content); | 29 LazyLineBreakIterator line_break_iterator(text_content); |
46 unsigned current_offset = 0; | |
47 line_builder->SetStart(0, current_offset); | |
48 const unsigned end_offset = text_content.length(); | 30 const unsigned end_offset = text_content.length(); |
49 while (current_offset < end_offset) { | 31 while (current_offset < end_offset) { |
50 // Find the next break opportunity. | 32 // Find the next break opportunity. |
51 int tmp_next_breakable_offset = -1; | 33 int tmp_next_breakable_offset = -1; |
52 line_break_iterator.isBreakable(current_offset + 1, | 34 line_break_iterator.isBreakable(current_offset + 1, |
53 tmp_next_breakable_offset); | 35 tmp_next_breakable_offset); |
54 current_offset = | 36 current_offset = |
55 tmp_next_breakable_offset >= 0 ? tmp_next_breakable_offset : end_offset; | 37 tmp_next_breakable_offset >= 0 ? tmp_next_breakable_offset : end_offset; |
56 DCHECK_LE(current_offset, end_offset); | 38 DCHECK_LE(current_offset, end_offset); |
57 | 39 |
58 // Advance the break opportunity to the end of hangable characters; e.g., | 40 // Advance the break opportunity to the end of hangable characters; e.g., |
59 // spaces. | 41 // spaces. |
60 // Unlike the ICU line breaker, LazyLineBreakIterator breaks before | 42 // Unlike the ICU line breaker, LazyLineBreakIterator breaks before |
61 // breakable spaces, and expect the line breaker to handle spaces | 43 // breakable spaces, and expect the line breaker to handle spaces |
62 // differently. This logic computes in the ICU way; break after spaces, and | 44 // differently. This logic computes in the ICU way; break after spaces, and |
63 // handle spaces as hangable characters. | 45 // handle spaces as hangable characters. |
64 unsigned start_of_hangables = current_offset; | 46 unsigned start_of_hangables = current_offset; |
65 while (current_offset < end_offset && | 47 while (current_offset < end_offset && |
66 IsHangable(text_content[current_offset])) | 48 IsHangable(text_content[current_offset])) |
67 current_offset++; | 49 current_offset++; |
68 | 50 |
69 // Set the end to the next break opportunity. | 51 // Set the end to the next break opportunity. |
70 line_builder->SetEnd(current_offset); | 52 algorithm->SetEnd(current_offset); |
71 | 53 |
72 // If there are more available spaces, mark the break opportunity and fetch | 54 // If there are more available spaces, mark the break opportunity and fetch |
73 // more text. | 55 // more text. |
74 // TODO(layout-ng): check if the height of the linebox can fit within | 56 // TODO(layout-ng): check if the height of the linebox can fit within |
75 // the current opportunity. | 57 // the current opportunity. |
76 if (line_builder->CanFitOnLine()) { | 58 if (algorithm->CanFitOnLine()) { |
77 line_builder->SetBreakOpportunity(); | 59 algorithm->SetBreakOpportunity(); |
78 continue; | 60 continue; |
79 } | 61 } |
80 | 62 |
81 // Compute hangable characters if exists. | 63 // Compute hangable characters if exists. |
82 if (current_offset != start_of_hangables) { | 64 if (current_offset != start_of_hangables) { |
83 line_builder->SetStartOfHangables(start_of_hangables); | 65 algorithm->SetStartOfHangables(start_of_hangables); |
84 // If text before hangables can fit, include it in the current line. | 66 // If text before hangables can fit, include it in the current line. |
85 if (line_builder->CanFitOnLine()) | 67 if (algorithm->CanFitOnLine()) |
86 line_builder->SetBreakOpportunity(); | 68 algorithm->SetBreakOpportunity(); |
87 } | 69 } |
88 | 70 |
89 if (!line_builder->HasBreakOpportunity()) { | 71 if (!algorithm->HasBreakOpportunity()) { |
90 // The first word (break opportunity) did not fit on the line. | 72 // The first word (break opportunity) did not fit on the line. |
91 // Create a line including items that don't fit, allowing them to | 73 // Create a line including items that don't fit, allowing them to |
92 // overflow. | 74 // overflow. |
93 line_builder->CreateLine(); | 75 if (!algorithm->CreateLine()) |
| 76 return; |
94 } else { | 77 } else { |
95 line_builder->CreateLineUpToLastBreakOpportunity(); | 78 if (!algorithm->CreateLineUpToLastBreakOpportunity()) |
| 79 return; |
96 | 80 |
97 // Items after the last break opportunity were sent to the next line. | 81 // Items after the last break opportunity were sent to the next line. |
98 // Set the break opportunity, or create a line if the word doesn't fit. | 82 // Set the break opportunity, or create a line if the word doesn't fit. |
99 if (line_builder->HasItems()) { | 83 if (algorithm->HasItems()) { |
100 if (!line_builder->CanFitOnLine()) | 84 if (algorithm->CanFitOnLine()) |
101 line_builder->CreateLine(); | 85 algorithm->SetBreakOpportunity(); |
102 else | 86 else if (!algorithm->CreateLine()) |
103 line_builder->SetBreakOpportunity(); | 87 return; |
104 } | 88 } |
105 } | 89 } |
106 } | 90 } |
107 | 91 |
108 // If inline children ended with items left in the line builder, create a line | 92 // If inline children ended with items left in the line builder, create a line |
109 // for them. | 93 // for them. |
110 if (line_builder->HasItems()) | 94 if (algorithm->HasItems()) |
111 line_builder->CreateLine(); | 95 algorithm->CreateLine(); |
112 } | 96 } |
113 | 97 |
114 } // namespace blink | 98 } // namespace blink |
OLD | NEW |