| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 Google Inc. All rights reserved. | 2 * Copyright (c) 2012 Google Inc. All rights reserved. |
| 3 * Copyright (C) 2013 BlackBerry Limited. All rights reserved. | 3 * Copyright (C) 2013 BlackBerry Limited. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 --glyph_index) { | 93 --glyph_index) { |
| 94 position -= glyph_data_[glyph_index].advance; | 94 position -= glyph_data_[glyph_index].advance; |
| 95 if (!glyph_index) | 95 if (!glyph_index) |
| 96 break; | 96 break; |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 return position; | 100 return position; |
| 101 } | 101 } |
| 102 | 102 |
| 103 static bool TargetPastEdge(bool rtl, float target_x, float next_x) { |
| 104 // In LTR, the edge belongs to the character on right. |
| 105 if (!rtl) |
| 106 return target_x < next_x; |
| 107 |
| 108 // In RTL, the edge belongs to the character on left. |
| 109 return target_x <= next_x; |
| 110 } |
| 111 |
| 103 int ShapeResult::RunInfo::CharacterIndexForXPosition( | 112 int ShapeResult::RunInfo::CharacterIndexForXPosition( |
| 104 float target_x, | 113 float target_x, |
| 105 bool include_partial_glyphs) const { | 114 bool include_partial_glyphs) const { |
| 106 DCHECK(target_x >= 0 && target_x <= width_); | 115 DCHECK(target_x >= 0 && target_x <= width_); |
| 116 if (target_x <= 0) |
| 117 return !Rtl() ? 0 : num_characters_; |
| 107 const unsigned num_glyphs = glyph_data_.size(); | 118 const unsigned num_glyphs = glyph_data_.size(); |
| 108 float current_x = 0; | 119 float current_x = 0; |
| 109 float current_advance = 0; | 120 float current_advance = 0; |
| 110 unsigned glyph_index = 0; | 121 unsigned glyph_index = 0; |
| 111 unsigned prev_character_index = num_characters_; // used only when rtl() | 122 unsigned prev_character_index = num_characters_; // used only when rtl() |
| 112 | 123 |
| 113 while (glyph_index < num_glyphs) { | 124 while (glyph_index < num_glyphs) { |
| 114 float prev_advance = current_advance; | 125 float prev_advance = current_advance; |
| 115 unsigned current_character_index = glyph_data_[glyph_index].character_index; | 126 unsigned current_character_index = glyph_data_[glyph_index].character_index; |
| 116 current_advance = glyph_data_[glyph_index].advance; | 127 current_advance = glyph_data_[glyph_index].advance; |
| 117 while (glyph_index < num_glyphs - 1 && | 128 while (glyph_index < num_glyphs - 1 && |
| 118 current_character_index == | 129 current_character_index == |
| 119 glyph_data_[glyph_index + 1].character_index) | 130 glyph_data_[glyph_index + 1].character_index) |
| 120 current_advance += glyph_data_[++glyph_index].advance; | 131 current_advance += glyph_data_[++glyph_index].advance; |
| 121 float next_x; | 132 float next_x; |
| 122 if (include_partial_glyphs) { | 133 if (include_partial_glyphs) { |
| 123 // For hit testing, find the closest caret point by incuding | 134 // For hit testing, find the closest caret point by incuding |
| 124 // end-half of the previous character and start-half of the current | 135 // end-half of the previous character and start-half of the current |
| 125 // character. | 136 // character. |
| 126 current_advance = current_advance / 2.0; | 137 current_advance = current_advance / 2.0; |
| 127 next_x = current_x + prev_advance + current_advance; | 138 next_x = current_x + prev_advance + current_advance; |
| 139 // When include_partial_glyphs, "<=" or "<" is not a big deal because |
| 140 // |next_x| is not at the character boundary. |
| 141 if (target_x <= next_x) |
| 142 return Rtl() ? prev_character_index : current_character_index; |
| 128 } else { | 143 } else { |
| 129 next_x = current_x + current_advance; | 144 next_x = current_x + current_advance; |
| 145 if (TargetPastEdge(Rtl(), target_x, next_x)) |
| 146 return current_character_index; |
| 130 } | 147 } |
| 131 if (current_x <= target_x && target_x <= next_x) | |
| 132 return include_partial_glyphs && Rtl() ? prev_character_index | |
| 133 : current_character_index; | |
| 134 current_x = next_x; | 148 current_x = next_x; |
| 135 prev_character_index = current_character_index; | 149 prev_character_index = current_character_index; |
| 136 ++glyph_index; | 150 ++glyph_index; |
| 137 } | 151 } |
| 138 | 152 |
| 139 return Rtl() ? 0 : num_characters_; | 153 return Rtl() ? 0 : num_characters_; |
| 140 } | 154 } |
| 141 | 155 |
| 142 void ShapeResult::RunInfo::SetGlyphAndPositions(unsigned index, | 156 void ShapeResult::RunInfo::SetGlyphAndPositions(unsigned index, |
| 143 uint16_t glyph_id, | 157 uint16_t glyph_id, |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 result->width_ = run->width_; | 499 result->width_ = run->width_; |
| 486 result->num_glyphs_ = count; | 500 result->num_glyphs_ = count; |
| 487 DCHECK_EQ(result->num_glyphs_, count); // no overflow | 501 DCHECK_EQ(result->num_glyphs_, count); // no overflow |
| 488 result->has_vertical_offsets_ = | 502 result->has_vertical_offsets_ = |
| 489 font_data->PlatformData().IsVerticalAnyUpright(); | 503 font_data->PlatformData().IsVerticalAnyUpright(); |
| 490 result->runs_.push_back(std::move(run)); | 504 result->runs_.push_back(std::move(run)); |
| 491 return result.Release(); | 505 return result.Release(); |
| 492 } | 506 } |
| 493 | 507 |
| 494 } // namespace blink | 508 } // namespace blink |
| OLD | NEW |