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_line_builder.h" | 5 #include "core/layout/ng/ng_line_builder.h" |
6 | 6 |
7 #include "core/layout/BidiRun.h" | 7 #include "core/layout/BidiRun.h" |
8 #include "core/layout/LayoutBlockFlow.h" | 8 #include "core/layout/LayoutBlockFlow.h" |
9 #include "core/layout/line/LineInfo.h" | 9 #include "core/layout/line/LineInfo.h" |
10 #include "core/layout/line/RootInlineBox.h" | 10 #include "core/layout/line/RootInlineBox.h" |
11 #include "core/layout/ng/ng_bidi_paragraph.h" | 11 #include "core/layout/ng/ng_bidi_paragraph.h" |
12 #include "core/layout/ng/ng_constraint_space.h" | 12 #include "core/layout/ng/ng_constraint_space.h" |
13 #include "core/layout/ng/ng_fragment_builder.h" | 13 #include "core/layout/ng/ng_fragment_builder.h" |
14 #include "core/layout/ng/ng_inline_node.h" | 14 #include "core/layout/ng/ng_inline_node.h" |
15 #include "core/layout/ng/ng_length_utils.h" | 15 #include "core/layout/ng/ng_length_utils.h" |
16 #include "core/layout/ng/ng_text_fragment.h" | 16 #include "core/layout/ng/ng_text_fragment.h" |
17 #include "core/style/ComputedStyle.h" | 17 #include "core/style/ComputedStyle.h" |
18 #include "platform/text/BidiRunList.h" | 18 #include "platform/text/BidiRunList.h" |
19 | 19 |
20 namespace blink { | 20 namespace blink { |
21 | 21 |
22 NGLineBuilder::NGLineBuilder(NGInlineNode* inline_box, | 22 NGLineBuilder::NGLineBuilder(NGInlineNode* inline_box, |
23 const NGConstraintSpace* constraint_space) | 23 NGConstraintSpace* constraint_space) |
24 : inline_box_(inline_box), | 24 : inline_box_(inline_box), |
25 constraint_space_(constraint_space), | 25 constraint_space_(constraint_space), |
26 baseline_type_(constraint_space->WritingMode() == | 26 baseline_type_(constraint_space->WritingMode() == |
27 NGWritingMode::kHorizontalTopBottom | 27 NGWritingMode::kHorizontalTopBottom |
28 ? FontBaseline::AlphabeticBaseline | 28 ? FontBaseline::AlphabeticBaseline |
29 : FontBaseline::IdeographicBaseline) | 29 : FontBaseline::IdeographicBaseline) |
30 #if DCHECK_IS_ON() | 30 #if DCHECK_IS_ON() |
31 , | 31 , |
32 is_bidi_reordered_(false) | 32 is_bidi_reordered_(false) |
33 #endif | 33 #endif |
34 { | 34 { |
35 } | 35 } |
36 | 36 |
37 bool NGLineBuilder::CanFitOnLine() const { | 37 bool NGLineBuilder::CanFitOnLine() const { |
38 LayoutUnit available_size = constraint_space_->AvailableSize().inline_size; | 38 LayoutUnit available_size = current_opportunity_.InlineSize(); |
39 if (available_size == NGSizeIndefinite) | 39 if (available_size == NGSizeIndefinite) |
40 return true; | 40 return true; |
41 return end_position_ <= available_size; | 41 return end_position_ <= available_size; |
42 } | 42 } |
43 | 43 |
44 bool NGLineBuilder::HasItems() const { | 44 bool NGLineBuilder::HasItems() const { |
45 return start_offset_ != end_offset_; | 45 return start_offset_ != end_offset_; |
46 } | 46 } |
47 | 47 |
48 bool NGLineBuilder::HasBreakOpportunity() const { | 48 bool NGLineBuilder::HasBreakOpportunity() const { |
49 return start_offset_ != last_break_opportunity_offset_; | 49 return start_offset_ != last_break_opportunity_offset_; |
50 } | 50 } |
51 | 51 |
52 bool NGLineBuilder::HasItemsAfterLastBreakOpportunity() const { | 52 bool NGLineBuilder::HasItemsAfterLastBreakOpportunity() const { |
53 return last_break_opportunity_offset_ != end_offset_; | 53 return last_break_opportunity_offset_ != end_offset_; |
54 } | 54 } |
55 | 55 |
56 void NGLineBuilder::SetStart(unsigned index, unsigned offset) { | 56 void NGLineBuilder::SetStart(unsigned index, unsigned offset) { |
57 inline_box_->AssertOffset(index, offset); | 57 inline_box_->AssertOffset(index, offset); |
58 | 58 |
59 start_index_ = last_index_ = last_break_opportunity_index_ = index; | 59 start_index_ = last_index_ = last_break_opportunity_index_ = index; |
60 start_offset_ = end_offset_ = last_break_opportunity_offset_ = offset; | 60 start_offset_ = end_offset_ = last_break_opportunity_offset_ = offset; |
61 end_position_ = last_break_opportunity_position_ = LayoutUnit(); | 61 end_position_ = last_break_opportunity_position_ = LayoutUnit(); |
| 62 |
| 63 FindNextLayoutOpportunity(); |
62 } | 64 } |
63 | 65 |
64 void NGLineBuilder::SetEnd(unsigned end_offset) { | 66 void NGLineBuilder::SetEnd(unsigned end_offset) { |
65 const Vector<NGLayoutInlineItem>& items = inline_box_->Items(); | 67 const Vector<NGLayoutInlineItem>& items = inline_box_->Items(); |
66 DCHECK(end_offset > end_offset_ && end_offset <= items.back().EndOffset()); | 68 DCHECK(end_offset > end_offset_ && end_offset <= items.back().EndOffset()); |
67 | 69 |
68 // Find the item index for |end_offset|, while accumulating inline-size. | 70 // Find the item index for |end_offset|, while accumulating inline-size. |
69 unsigned last_index = last_index_; | 71 unsigned last_index = last_index_; |
70 const NGLayoutInlineItem* item = &items[last_index]; | 72 const NGLayoutInlineItem* item = &items[last_index]; |
71 LayoutUnit inline_size_since_current_end; | 73 LayoutUnit inline_size_since_current_end; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 // Move |start| to |last_break_opportunity|, keeping items after | 141 // Move |start| to |last_break_opportunity|, keeping items after |
140 // |last_break_opportunity|. | 142 // |last_break_opportunity|. |
141 start_index_ = last_break_opportunity_index_; | 143 start_index_ = last_break_opportunity_index_; |
142 start_offset_ = last_break_opportunity_offset_; | 144 start_offset_ = last_break_opportunity_offset_; |
143 DCHECK_GE(end_position_, last_break_opportunity_position_); | 145 DCHECK_GE(end_position_, last_break_opportunity_position_); |
144 end_position_ -= last_break_opportunity_position_; | 146 end_position_ -= last_break_opportunity_position_; |
145 last_break_opportunity_position_ = LayoutUnit(); | 147 last_break_opportunity_position_ = LayoutUnit(); |
146 #if DCHECK_IS_ON() | 148 #if DCHECK_IS_ON() |
147 is_bidi_reordered_ = false; | 149 is_bidi_reordered_ = false; |
148 #endif | 150 #endif |
| 151 |
| 152 FindNextLayoutOpportunity(); |
149 } | 153 } |
150 | 154 |
151 void NGLineBuilder::BidiReorder(Vector<LineItemChunk, 32>* line_item_chunks) { | 155 void NGLineBuilder::BidiReorder(Vector<LineItemChunk, 32>* line_item_chunks) { |
152 #if DCHECK_IS_ON() | 156 #if DCHECK_IS_ON() |
153 DCHECK(!is_bidi_reordered_); | 157 DCHECK(!is_bidi_reordered_); |
154 is_bidi_reordered_ = true; | 158 is_bidi_reordered_ = true; |
155 #endif | 159 #endif |
156 | 160 |
157 // TODO(kojii): UAX#9 L1 is not supported yet. Supporting L1 may change | 161 // TODO(kojii): UAX#9 L1 is not supported yet. Supporting L1 may change |
158 // embedding levels of parts of runs, which requires to split items. | 162 // embedding levels of parts of runs, which requires to split items. |
(...skipping 23 matching lines...) Expand all Loading... |
182 line_item_chunks->swap(line_item_chunks_in_visual_order); | 186 line_item_chunks->swap(line_item_chunks_in_visual_order); |
183 } | 187 } |
184 | 188 |
185 void NGLineBuilder::PlaceItems( | 189 void NGLineBuilder::PlaceItems( |
186 const Vector<LineItemChunk, 32>& line_item_chunks) { | 190 const Vector<LineItemChunk, 32>& line_item_chunks) { |
187 const Vector<NGLayoutInlineItem>& items = inline_box_->Items(); | 191 const Vector<NGLayoutInlineItem>& items = inline_box_->Items(); |
188 const unsigned fragment_start_index = fragments_.size(); | 192 const unsigned fragment_start_index = fragments_.size(); |
189 | 193 |
190 NGFragmentBuilder text_builder(NGPhysicalFragment::kFragmentText, | 194 NGFragmentBuilder text_builder(NGPhysicalFragment::kFragmentText, |
191 inline_box_); | 195 inline_box_); |
192 text_builder.SetWritingMode(constraint_space_->WritingMode()); | 196 text_builder.SetWritingMode(ConstraintSpace().WritingMode()); |
193 line_box_data_list_.grow(line_box_data_list_.size() + 1); | 197 line_box_data_list_.grow(line_box_data_list_.size() + 1); |
194 LineBoxData& line_box_data = line_box_data_list_.back(); | 198 LineBoxData& line_box_data = line_box_data_list_.back(); |
195 | 199 |
196 // Use the block style to compute the estimated baseline position because the | 200 // Use the block style to compute the estimated baseline position because the |
197 // baseline position is not known until we know the maximum ascent and leading | 201 // baseline position is not known until we know the maximum ascent and leading |
198 // of the line. Items are placed on this baseline, then adjusted later if the | 202 // of the line. Items are placed on this baseline, then adjusted later if the |
199 // estimation turned out to be different. | 203 // estimation turned out to be different. |
200 const ComputedStyle* block_style = inline_box_->BlockStyle(); | 204 const ComputedStyle* block_style = inline_box_->BlockStyle(); |
201 InlineItemMetrics estimated_metrics(*block_style, baseline_type_); | 205 InlineItemMetrics estimated_metrics(*block_style, baseline_type_); |
202 LayoutUnit estimated_baseline = | 206 LayoutUnit estimated_baseline = |
(...skipping 28 matching lines...) Expand all Loading... |
231 // properties, not the resolved bidi direction. | 235 // properties, not the resolved bidi direction. |
232 text_builder.SetDirection(style->direction()) | 236 text_builder.SetDirection(style->direction()) |
233 .SetInlineSize(line_item_chunk.inline_size) | 237 .SetInlineSize(line_item_chunk.inline_size) |
234 .SetInlineOverflow(line_item_chunk.inline_size) | 238 .SetInlineOverflow(line_item_chunk.inline_size) |
235 .SetBlockSize(height) | 239 .SetBlockSize(height) |
236 .SetBlockOverflow(height); | 240 .SetBlockOverflow(height); |
237 RefPtr<NGPhysicalTextFragment> text_fragment = text_builder.ToTextFragment( | 241 RefPtr<NGPhysicalTextFragment> text_fragment = text_builder.ToTextFragment( |
238 line_item_chunk.index, line_item_chunk.start_offset, | 242 line_item_chunk.index, line_item_chunk.start_offset, |
239 line_item_chunk.end_offset); | 243 line_item_chunk.end_offset); |
240 fragments_.push_back(std::move(text_fragment)); | 244 fragments_.push_back(std::move(text_fragment)); |
241 offsets_.push_back(NGLogicalOffset(line_box_data.inline_size, top)); | 245 |
| 246 NGLogicalOffset logical_offset( |
| 247 line_box_data.inline_size + current_opportunity_.InlineStartOffset() - |
| 248 ConstraintSpace().BfcOffset().inline_offset, |
| 249 top); |
| 250 offsets_.push_back(logical_offset); |
242 line_box_data.inline_size += line_item_chunk.inline_size; | 251 line_box_data.inline_size += line_item_chunk.inline_size; |
243 } | 252 } |
244 DCHECK_EQ(fragments_.size(), offsets_.size()); | 253 DCHECK_EQ(fragments_.size(), offsets_.size()); |
245 | 254 |
246 if (fragment_start_index == fragments_.size()) { | 255 if (fragment_start_index == fragments_.size()) { |
247 // The line was empty. Remove the LineBoxData. | 256 // The line was empty. Remove the LineBoxData. |
248 line_box_data_list_.shrink(line_box_data_list_.size() - 1); | 257 line_box_data_list_.shrink(line_box_data_list_.size() - 1); |
249 return; | 258 return; |
250 } | 259 } |
251 | 260 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 HashSet<const SimpleFontData*> fallback_fonts; | 321 HashSet<const SimpleFontData*> fallback_fonts; |
313 item.GetFallbackFonts(&fallback_fonts, line_item_chunk.start_offset, | 322 item.GetFallbackFonts(&fallback_fonts, line_item_chunk.start_offset, |
314 line_item_chunk.end_offset); | 323 line_item_chunk.end_offset); |
315 for (const auto& fallback_font : fallback_fonts) { | 324 for (const auto& fallback_font : fallback_fonts) { |
316 InlineItemMetrics fallback_font_metrics(fallback_font->getFontMetrics(), | 325 InlineItemMetrics fallback_font_metrics(fallback_font->getFontMetrics(), |
317 baseline_type_); | 326 baseline_type_); |
318 line_box_data->UpdateMaxAscentAndDescent(fallback_font_metrics); | 327 line_box_data->UpdateMaxAscentAndDescent(fallback_font_metrics); |
319 } | 328 } |
320 } | 329 } |
321 | 330 |
| 331 void NGLineBuilder::FindNextLayoutOpportunity() { |
| 332 NGLogicalOffset iter_offset = constraint_space_->BfcOffset(); |
| 333 iter_offset.block_offset += content_size_; |
| 334 auto* iter = constraint_space_->LayoutOpportunityIterator(iter_offset); |
| 335 NGLayoutOpportunity opportunity = iter->Next(); |
| 336 if (!opportunity.IsEmpty()) |
| 337 current_opportunity_ = opportunity; |
| 338 } |
| 339 |
322 void NGLineBuilder::CreateFragments(NGFragmentBuilder* container_builder) { | 340 void NGLineBuilder::CreateFragments(NGFragmentBuilder* container_builder) { |
323 DCHECK(!HasItems()) << "Must call CreateLine()"; | 341 DCHECK(!HasItems()) << "Must call CreateLine()"; |
324 DCHECK_EQ(fragments_.size(), offsets_.size()); | 342 DCHECK_EQ(fragments_.size(), offsets_.size()); |
325 | 343 |
326 for (unsigned i = 0; i < fragments_.size(); i++) { | 344 for (unsigned i = 0; i < fragments_.size(); i++) { |
327 // TODO(layout-dev): This should really be a std::move but | 345 // TODO(layout-dev): This should really be a std::move but |
328 // CopyFragmentDataToLayoutBlockFlow also uses the fragments. | 346 // CopyFragmentDataToLayoutBlockFlow also uses the fragments. |
329 container_builder->AddChild(fragments_[i].get(), offsets_[i]); | 347 container_builder->AddChild(fragments_[i].get(), offsets_[i]); |
330 } | 348 } |
331 | 349 |
332 // TODO(kojii): Check if the line box width should be content or available. | 350 // TODO(kojii): Check if the line box width should be content or available. |
333 // TODO(kojii): Need to take constraint_space into account. | |
334 container_builder->SetInlineSize(max_inline_size_) | 351 container_builder->SetInlineSize(max_inline_size_) |
335 .SetInlineOverflow(max_inline_size_) | 352 .SetInlineOverflow(max_inline_size_) |
336 .SetBlockSize(content_size_) | 353 .SetBlockSize(content_size_) |
337 .SetBlockOverflow(content_size_); | 354 .SetBlockOverflow(content_size_); |
338 } | 355 } |
339 | 356 |
340 void NGLineBuilder::CopyFragmentDataToLayoutBlockFlow() { | 357 void NGLineBuilder::CopyFragmentDataToLayoutBlockFlow() { |
341 LayoutBlockFlow* block = inline_box_->GetLayoutBlockFlow(); | 358 LayoutBlockFlow* block = inline_box_->GetLayoutBlockFlow(); |
342 block->deleteLineBoxTree(); | 359 block->deleteLineBoxTree(); |
343 | 360 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 // RootInlineBox are set to Bidirun::m_box. | 397 // RootInlineBox are set to Bidirun::m_box. |
381 line_info.setEmpty(false); | 398 line_info.setEmpty(false); |
382 // TODO(kojii): Implement setFirstLine, LastLine, etc. | 399 // TODO(kojii): Implement setFirstLine, LastLine, etc. |
383 RootInlineBox* line_box = block->constructLine(bidi_runs, line_info); | 400 RootInlineBox* line_box = block->constructLine(bidi_runs, line_info); |
384 | 401 |
385 // Copy fragments data to InlineBoxes. | 402 // Copy fragments data to InlineBoxes. |
386 DCHECK_EQ(fragments_for_bidi_runs.size(), bidi_runs.runCount()); | 403 DCHECK_EQ(fragments_for_bidi_runs.size(), bidi_runs.runCount()); |
387 BidiRun* run = bidi_runs.firstRun(); | 404 BidiRun* run = bidi_runs.firstRun(); |
388 for (auto* physical_fragment : fragments_for_bidi_runs) { | 405 for (auto* physical_fragment : fragments_for_bidi_runs) { |
389 DCHECK(run); | 406 DCHECK(run); |
390 NGTextFragment fragment(constraint_space_->WritingMode(), | 407 NGTextFragment fragment(ConstraintSpace().WritingMode(), |
391 toNGPhysicalTextFragment(physical_fragment)); | 408 toNGPhysicalTextFragment(physical_fragment)); |
392 InlineBox* inline_box = run->m_box; | 409 InlineBox* inline_box = run->m_box; |
393 inline_box->setLogicalWidth(fragment.InlineSize()); | 410 inline_box->setLogicalWidth(fragment.InlineSize()); |
394 inline_box->setLogicalLeft(fragment.InlineOffset()); | 411 inline_box->setLogicalLeft(fragment.InlineOffset()); |
395 inline_box->setLogicalTop(fragment.BlockOffset()); | 412 inline_box->setLogicalTop(fragment.BlockOffset()); |
396 run = run->next(); | 413 run = run->next(); |
397 } | 414 } |
398 DCHECK(!run); | 415 DCHECK(!run); |
399 | 416 |
400 // Copy LineBoxData to RootInlineBox. | 417 // Copy LineBoxData to RootInlineBox. |
401 line_box->setLogicalWidth(line_box_data.inline_size); | 418 line_box->setLogicalWidth(line_box_data.inline_size); |
402 LayoutUnit baseline_position = | 419 LayoutUnit baseline_position = |
403 line_box_data.top_with_leading + | 420 line_box_data.top_with_leading + |
404 LayoutUnit(line_box_data.max_ascent_and_leading); | 421 LayoutUnit(line_box_data.max_ascent_and_leading); |
405 line_box->setLineTopBottomPositions( | 422 line_box->setLineTopBottomPositions( |
406 baseline_position - LayoutUnit(line_box_data.max_ascent), | 423 baseline_position - LayoutUnit(line_box_data.max_ascent), |
407 baseline_position + LayoutUnit(line_box_data.max_descent), | 424 baseline_position + LayoutUnit(line_box_data.max_descent), |
408 line_box_data.top_with_leading, | 425 line_box_data.top_with_leading, |
409 baseline_position + LayoutUnit(line_box_data.max_descent_and_leading)); | 426 baseline_position + LayoutUnit(line_box_data.max_descent_and_leading)); |
410 | 427 |
411 bidi_runs.deleteRuns(); | 428 bidi_runs.deleteRuns(); |
412 fragments_for_bidi_runs.clear(); | 429 fragments_for_bidi_runs.clear(); |
413 } | 430 } |
414 } | 431 } |
415 | |
416 } // namespace blink | 432 } // namespace blink |
OLD | NEW |