| 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 "platform/fonts/shaping/ShapeResultSpacing.h" | 5 #include "platform/fonts/shaping/ShapeResultSpacing.h" |
| 6 | 6 |
| 7 #include "platform/fonts/FontDescription.h" | 7 #include "platform/fonts/FontDescription.h" |
| 8 #include "platform/text/TextRun.h" | 8 #include "platform/text/TextRun.h" |
| 9 | 9 |
| 10 namespace blink { | 10 namespace blink { |
| 11 | 11 |
| 12 ShapeResultSpacing::ShapeResultSpacing(const TextRun& run, | 12 template <typename TextContainerType> |
| 13 const FontDescription& font_description) | 13 ShapeResultSpacing<TextContainerType>::ShapeResultSpacing( |
| 14 : text_run_(run), | 14 const TextContainerType& text) |
| 15 letter_spacing_(font_description.LetterSpacing()), | 15 : text_(text), |
| 16 word_spacing_(font_description.WordSpacing()), | 16 letter_spacing_(0), |
| 17 expansion_(run.Expansion()), | 17 word_spacing_(0), |
| 18 expansion_(0), |
| 18 expansion_per_opportunity_(0), | 19 expansion_per_opportunity_(0), |
| 19 expansion_opportunity_count_(0), | 20 expansion_opportunity_count_(0), |
| 20 text_justify_(TextJustify::kTextJustifyAuto), | 21 text_justify_(TextJustify::kTextJustifyAuto), |
| 21 has_spacing_(false), | 22 has_spacing_(false), |
| 22 normalize_space_(run.NormalizeSpace()), | 23 normalize_space_(false), |
| 23 allow_tabs_(run.AllowTabs()), | 24 allow_tabs_(false), |
| 24 is_after_expansion_(false), | 25 is_after_expansion_(false), |
| 25 is_vertical_offset_(font_description.IsVerticalAnyUpright()) { | 26 is_vertical_offset_(false) {} |
| 26 if (text_run_.SpacingDisabled()) | 27 |
| 28 template <typename TextContainerType> |
| 29 bool ShapeResultSpacing<TextContainerType>::SetSpacing( |
| 30 const FontDescription& font_description) { |
| 31 if (!font_description.LetterSpacing() && !font_description.WordSpacing()) { |
| 32 has_spacing_ = false; |
| 33 return false; |
| 34 } |
| 35 |
| 36 letter_spacing_ = font_description.LetterSpacing(); |
| 37 word_spacing_ = font_description.WordSpacing(); |
| 38 is_vertical_offset_ = font_description.IsVerticalAnyUpright(); |
| 39 DCHECK(!normalize_space_); |
| 40 allow_tabs_ = true; |
| 41 has_spacing_ = true; |
| 42 return true; |
| 43 } |
| 44 |
| 45 template <typename TextContainerType> |
| 46 void ShapeResultSpacing<TextContainerType>::SetSpacingAndExpansion( |
| 47 const FontDescription& font_description) { |
| 48 // Available only for TextRun since it has expansion data. |
| 49 NOTREACHED(); |
| 50 } |
| 51 |
| 52 template <> |
| 53 void ShapeResultSpacing<TextRun>::SetSpacingAndExpansion( |
| 54 const FontDescription& font_description) { |
| 55 letter_spacing_ = font_description.LetterSpacing(); |
| 56 word_spacing_ = font_description.WordSpacing(); |
| 57 expansion_ = text_.Expansion(); |
| 58 has_spacing_ = letter_spacing_ || word_spacing_ || expansion_; |
| 59 if (!has_spacing_) |
| 27 return; | 60 return; |
| 28 | 61 |
| 29 if (!letter_spacing_ && !word_spacing_ && !expansion_) | 62 is_vertical_offset_ = font_description.IsVerticalAnyUpright(); |
| 30 return; | 63 normalize_space_ = text_.NormalizeSpace(); |
| 64 allow_tabs_ = text_.AllowTabs(); |
| 31 | 65 |
| 32 has_spacing_ = true; | 66 if (expansion_) { |
| 67 ComputeExpansion(text_.AllowsLeadingExpansion(), |
| 68 text_.AllowsTrailingExpansion(), text_.GetTextJustify()); |
| 69 } |
| 70 } |
| 33 | 71 |
| 34 if (!expansion_) | 72 template <typename TextContainerType> |
| 35 return; | 73 void ShapeResultSpacing<TextContainerType>::ComputeExpansion( |
| 74 bool allows_leading_expansion, |
| 75 bool allows_trailing_expansion, |
| 76 TextJustify text_justify) { |
| 77 DCHECK_GT(expansion_, 0); |
| 36 | 78 |
| 37 // Setup for justifications (expansions.) | 79 text_justify_ = text_justify; |
| 38 text_justify_ = run.GetTextJustify(); | 80 is_after_expansion_ = !allows_leading_expansion; |
| 39 is_after_expansion_ = !run.AllowsLeadingExpansion(); | |
| 40 | 81 |
| 41 bool is_after_expansion = is_after_expansion_; | 82 bool is_after_expansion = is_after_expansion_; |
| 42 expansion_opportunity_count_ = | 83 expansion_opportunity_count_ = |
| 43 Character::ExpansionOpportunityCount(run, is_after_expansion); | 84 Character::ExpansionOpportunityCount(text_, is_after_expansion); |
| 44 if (is_after_expansion && !run.AllowsTrailingExpansion()) { | 85 if (is_after_expansion && !allows_trailing_expansion) { |
| 45 DCHECK_GT(expansion_opportunity_count_, 0u); | 86 DCHECK_GT(expansion_opportunity_count_, 0u); |
| 46 --expansion_opportunity_count_; | 87 --expansion_opportunity_count_; |
| 47 } | 88 } |
| 48 | 89 |
| 49 if (expansion_opportunity_count_) | 90 if (expansion_opportunity_count_) |
| 50 expansion_per_opportunity_ = expansion_ / expansion_opportunity_count_; | 91 expansion_per_opportunity_ = expansion_ / expansion_opportunity_count_; |
| 51 } | 92 } |
| 52 | 93 |
| 53 float ShapeResultSpacing::NextExpansion() { | 94 template <typename TextContainerType> |
| 95 float ShapeResultSpacing<TextContainerType>::NextExpansion() { |
| 54 if (!expansion_opportunity_count_) { | 96 if (!expansion_opportunity_count_) { |
| 55 NOTREACHED(); | 97 NOTREACHED(); |
| 56 return 0; | 98 return 0; |
| 57 } | 99 } |
| 58 | 100 |
| 59 is_after_expansion_ = true; | 101 is_after_expansion_ = true; |
| 60 | 102 |
| 61 if (!--expansion_opportunity_count_) { | 103 if (!--expansion_opportunity_count_) { |
| 62 float remaining = expansion_; | 104 float remaining = expansion_; |
| 63 expansion_ = 0; | 105 expansion_ = 0; |
| 64 return remaining; | 106 return remaining; |
| 65 } | 107 } |
| 66 | 108 |
| 67 expansion_ -= expansion_per_opportunity_; | 109 expansion_ -= expansion_per_opportunity_; |
| 68 return expansion_per_opportunity_; | 110 return expansion_per_opportunity_; |
| 69 } | 111 } |
| 70 | 112 |
| 71 bool ShapeResultSpacing::IsFirstRun(const TextRun& run) const { | 113 template <typename TextContainerType> |
| 72 if (&run == &text_run_) | 114 bool ShapeResultSpacing<TextContainerType>::IsFirstRun( |
| 73 return true; | 115 const TextContainerType& run) const { |
| 74 return run.Is8Bit() ? run.Characters8() == text_run_.Characters8() | 116 return &run == &text_ || run.Bytes() == text_.Bytes(); |
| 75 : run.Characters16() == text_run_.Characters16(); | |
| 76 } | 117 } |
| 77 | 118 |
| 78 float ShapeResultSpacing::ComputeSpacing(const TextRun& run, | 119 template <typename TextContainerType> |
| 79 size_t index, | 120 float ShapeResultSpacing<TextContainerType>::ComputeSpacing( |
| 80 float& offset) { | 121 const TextContainerType& run, |
| 122 size_t index, |
| 123 float& offset) { |
| 124 DCHECK(has_spacing_); |
| 81 UChar32 character = run[index]; | 125 UChar32 character = run[index]; |
| 82 bool treat_as_space = | 126 bool treat_as_space = |
| 83 (Character::TreatAsSpace(character) || | 127 (Character::TreatAsSpace(character) || |
| 84 (normalize_space_ && | 128 (normalize_space_ && |
| 85 Character::IsNormalizedCanvasSpaceCharacter(character))) && | 129 Character::IsNormalizedCanvasSpaceCharacter(character))) && |
| 86 (character != '\t' || !allow_tabs_); | 130 (character != '\t' || !allow_tabs_); |
| 87 if (treat_as_space && character != kNoBreakSpaceCharacter) | 131 if (treat_as_space && character != kNoBreakSpaceCharacter) |
| 88 character = kSpaceCharacter; | 132 character = kSpaceCharacter; |
| 89 | 133 |
| 90 float spacing = 0; | 134 float spacing = 0; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 offset += expand_before; | 166 offset += expand_before; |
| 123 spacing += expand_before; | 167 spacing += expand_before; |
| 124 } | 168 } |
| 125 if (!HasExpansion()) | 169 if (!HasExpansion()) |
| 126 return spacing; | 170 return spacing; |
| 127 } | 171 } |
| 128 | 172 |
| 129 return spacing + NextExpansion(); | 173 return spacing + NextExpansion(); |
| 130 } | 174 } |
| 131 | 175 |
| 176 // Instantiate the template class. |
| 177 template class ShapeResultSpacing<TextRun>; |
| 178 template class ShapeResultSpacing<StringView>; |
| 179 |
| 132 } // namespace blink | 180 } // namespace blink |
| OLD | NEW |