Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/gfx/render_text_win.h" | 5 #include "ui/gfx/render_text_win.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 } else { | 147 } else { |
| 148 result = ui::Range(run.logical_clusters[run_range.start()], | 148 result = ui::Range(run.logical_clusters[run_range.start()], |
| 149 run_range.end() < run.range.length() ? | 149 run_range.end() < run.range.length() ? |
| 150 run.logical_clusters[run_range.end()] : run.glyph_count); | 150 run.logical_clusters[run_range.end()] : run.glyph_count); |
| 151 } | 151 } |
| 152 DCHECK(!result.is_reversed()); | 152 DCHECK(!result.is_reversed()); |
| 153 DCHECK(ui::Range(0, run.glyph_count).Contains(result)); | 153 DCHECK(ui::Range(0, run.glyph_count).Contains(result)); |
| 154 return result; | 154 return result; |
| 155 } | 155 } |
| 156 | 156 |
| 157 // Starting from |start_char|, finds a suitable line break position at or before | |
| 158 // |available_width| using word break info from |breaks|. If |empty_line| is | |
| 159 // true, this function will not roll back to |start_char| and |*next_char| will | |
| 160 // be greater than |start_char|. | |
|
msw
2013/09/06 23:47:45
nit: add "(to avoid constructing empty lines)."
ckocagil
2013/09/11 14:59:49
Done.
| |
| 161 // TODO(ckocagil): Do not break ligatures and diacritics. | |
| 162 // TextRun::logical_clusters might help. | |
| 163 // TODO(ckocagil): We might have to reshape after breaking at ligatures. | |
| 164 // See whether resolving the TODO above resolves this too. | |
| 165 // TODO(ckocagil): Do not reserve space for whitespace at the end of lines. | |
|
msw
2013/09/06 23:47:45
nit: s/space/width/
ckocagil
2013/09/11 14:59:49
Done.
| |
| 166 void BreakRunAtWidth(const internal::TextRun& run, | |
| 167 const BreakList<size_t>& breaks, | |
| 168 size_t start_char, | |
| 169 int available_width, | |
| 170 bool empty_line, | |
| 171 int* width, | |
| 172 size_t* next_char) { | |
| 173 DCHECK(run.range.Contains(ui::Range(start_char, start_char + 1))); | |
| 174 BreakList<size_t>::const_iterator word = breaks.GetBreak(start_char); | |
| 175 BreakList<size_t>::const_iterator next_word = word + 1; | |
| 176 // Width from |std::max(word->first, start_char)|. | |
|
msw
2013/09/06 23:47:45
nit: ... to the current character.
ckocagil
2013/09/11 14:59:49
Done.
| |
| 177 int word_width = 0; | |
| 178 *width = 0; | |
| 179 | |
| 180 for (size_t i = start_char; i < run.range.end(); ++i) { | |
| 181 // |word| holds the word boundary at or before |i|, and |next_word| holds | |
| 182 // the word boundary right after |i|. Advance both |word| and |next_word| | |
| 183 // when |i| reaches |next_word|. | |
| 184 if (next_word != breaks.breaks().end() && i >= next_word->first) { | |
| 185 word = next_word++; | |
| 186 word_width = 0; | |
| 187 } | |
| 188 | |
| 189 ui::Range glyph_range = CharRangeToGlyphRange(run, ui::Range(i, i + 1)); | |
| 190 int char_width = 0; | |
| 191 for (size_t j = glyph_range.start(); j < glyph_range.end(); ++j) | |
| 192 char_width += run.advance_widths[j]; | |
| 193 | |
| 194 *width += char_width; | |
| 195 word_width += char_width; | |
| 196 | |
| 197 if (*width > available_width) { | |
| 198 if (!empty_line || word_width < *width) { | |
| 199 *width -= word_width; | |
| 200 *next_char = std::max(word->first, start_char); | |
| 201 } else if (char_width < *width) { | |
| 202 *width -= char_width; | |
| 203 *next_char = i; | |
| 204 } else { | |
| 205 *next_char = i + 1; | |
| 206 } | |
| 207 | |
| 208 return; | |
| 209 } | |
| 210 } | |
| 211 | |
| 212 *next_char = run.range.end(); | |
| 213 } | |
| 214 | |
| 157 } // namespace | 215 } // namespace |
| 158 | 216 |
| 159 namespace internal { | 217 namespace internal { |
| 160 | 218 |
| 161 TextRun::TextRun() | 219 TextRun::TextRun() |
| 162 : font_style(0), | 220 : font_style(0), |
| 163 strike(false), | 221 strike(false), |
| 164 diagonal_strike(false), | 222 diagonal_strike(false), |
| 165 underline(false), | 223 underline(false), |
| 166 width(0), | 224 width(0), |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 190 run->glyph_count, | 248 run->glyph_count, |
| 191 run->logical_clusters.get(), | 249 run->logical_clusters.get(), |
| 192 run->visible_attributes.get(), | 250 run->visible_attributes.get(), |
| 193 run->advance_widths.get(), | 251 run->advance_widths.get(), |
| 194 &run->script_analysis, | 252 &run->script_analysis, |
| 195 &x); | 253 &x); |
| 196 DCHECK(SUCCEEDED(hr)); | 254 DCHECK(SUCCEEDED(hr)); |
| 197 return run->preceding_run_widths + x; | 255 return run->preceding_run_widths + x; |
| 198 } | 256 } |
| 199 | 257 |
| 258 // Internal class to generate Line structures. If |multiline| is true, the text | |
| 259 // is broken into lines at |words| boundaries such that each line is no longer | |
| 260 // than |max_width|. If |multiline| is false, only outputs a single Line from | |
| 261 // the given runs. | |
| 262 // TODO(ckocagil): Expose the interface of this class in the header and test | |
| 263 // this class directly. | |
| 264 class LineBreaker { | |
| 265 public: | |
| 266 LineBreaker(int max_width, | |
| 267 bool multiline, | |
| 268 const BreakList<size_t>* words, | |
| 269 const ScopedVector<TextRun>& runs) | |
| 270 : max_width_(max_width), | |
| 271 multiline_(multiline), | |
| 272 words_(words), | |
| 273 runs_(runs), | |
| 274 text_x_(0), | |
| 275 line_x_(0), | |
| 276 line_ascent_(0), | |
| 277 line_descent_(0) { | |
| 278 AdvanceLine(); | |
| 279 } | |
| 280 | |
| 281 // Breaks the run at given |run_index| into Line structs. | |
| 282 void AddRun(int run_index) { | |
| 283 const TextRun* run = runs_[run_index]; | |
| 284 if (multiline_ && line_x_ + run->width > max_width_) | |
| 285 BreakRun(run_index); | |
| 286 else | |
| 287 AddSegment(run_index, run->range, run->width); | |
| 288 } | |
| 289 | |
| 290 // Finishes line breaking and outputs the results. Can be called at most once. | |
| 291 void Finalize(std::vector<Line>* lines, Size* size) { | |
| 292 DCHECK(!lines_.empty()); | |
| 293 // Add an empty line to finish the line size calculation and remove it. | |
| 294 AdvanceLine(); | |
| 295 lines_.pop_back(); | |
| 296 *size = total_size_; | |
| 297 lines->swap(lines_); | |
| 298 } | |
| 299 | |
| 300 private: | |
| 301 // A (line index, segment index) pair that specifies a segment in |lines_|. | |
| 302 typedef std::pair<size_t, size_t> SegmentHandle; | |
| 303 | |
| 304 LineSegment* SegmentFromHandle(const SegmentHandle& handle) { | |
| 305 return &lines_[handle.first].segments[handle.second]; | |
| 306 } | |
| 307 | |
| 308 // Breaks a run into segments that fit in the last line in |lines_| and adds | |
| 309 // them. Adds a new Line to the back of |lines_| whenever a new segment can't | |
| 310 // be added without the Line's width exceeding |max_width_|. | |
| 311 void BreakRun(int run_index) { | |
| 312 DCHECK(words_); | |
| 313 TextRun* const run = runs_[run_index]; | |
|
msw
2013/09/06 23:47:45
nit: can this be const TextRun* const? Sorry if yo
ckocagil
2013/09/11 14:59:49
It can be; done.
| |
| 314 int width = 0; | |
| 315 size_t next_char = run->range.start(); | |
| 316 | |
| 317 // Break the run until it fits the current line. | |
| 318 while (next_char < run->range.end()) { | |
| 319 const size_t current_char = next_char; | |
| 320 BreakRunAtWidth(*run, *words_, next_char, max_width_ - line_x_, | |
|
msw
2013/09/06 23:47:45
nit: send |current_char| instead of |next_char|, j
ckocagil
2013/09/11 14:59:49
Done.
| |
| 321 line_x_ == 0, &width, &next_char); | |
| 322 AddSegment(run_index, ui::Range(current_char, next_char), width); | |
| 323 if (next_char < run->range.end()) | |
| 324 AdvanceLine(); | |
| 325 } | |
| 326 } | |
| 327 | |
| 328 // RTL runs are broken in logical order but displayed in visual order. To find | |
| 329 // the text-space coordinate (where it would fall in a single-line text) | |
| 330 // |x_range| of RTL segments, segment widths are applied in reverse order. | |
| 331 // e.g. {[5, 10], [10, 40]} will become {[35, 40], [5, 35]}. | |
| 332 void UpdateRTLSegmentRanges() { | |
| 333 if (rtl_segments_.empty()) | |
| 334 return; | |
| 335 int x = SegmentFromHandle(rtl_segments_[0])->x_range.start(); | |
| 336 for (size_t i = rtl_segments_.size(); i > 0; --i) { | |
| 337 LineSegment* segment = SegmentFromHandle(rtl_segments_[i - 1]); | |
| 338 const size_t segment_width = segment->x_range.length(); | |
| 339 segment->x_range = ui::Range(x, x + segment_width); | |
| 340 x += segment_width; | |
| 341 } | |
| 342 rtl_segments_.clear(); | |
| 343 } | |
| 344 | |
| 345 // Finishes the size calculations of the last Line in |lines_|. Adds a new | |
| 346 // Line to the back of |lines_|. | |
| 347 void AdvanceLine() { | |
| 348 if (!lines_.empty()) { | |
| 349 Line* line = &lines_.back(); | |
| 350 line->baseline = line_ascent_; | |
| 351 line->size.set_height(line_ascent_ + line_descent_); | |
| 352 line->preceding_heights = total_size_.height(); | |
| 353 total_size_.set_height(total_size_.height() + line->size.height()); | |
| 354 total_size_.set_width(std::max(total_size_.width(), line->size.width())); | |
| 355 } | |
| 356 line_x_ = 0; | |
| 357 line_ascent_ = 0; | |
| 358 line_descent_ = 0; | |
| 359 lines_.push_back(Line()); | |
| 360 } | |
| 361 | |
| 362 // Adds a new segment with the given properties to |lines_.back()|. | |
| 363 void AddSegment(int run_index, ui::Range char_range, int width) { | |
| 364 if (char_range.is_empty()) { | |
| 365 DCHECK_EQ(width, 0); | |
| 366 return; | |
| 367 } | |
| 368 const TextRun* run = runs_[run_index]; | |
| 369 line_ascent_ = std::max(line_ascent_, run->font.GetBaseline()); | |
| 370 line_descent_ = std::max(line_descent_, | |
| 371 run->font.GetHeight() - run->font.GetBaseline()); | |
| 372 | |
| 373 LineSegment segment; | |
| 374 segment.run = run_index; | |
| 375 segment.char_range = char_range; | |
| 376 segment.x_range = ui::Range(text_x_, text_x_ + width); | |
| 377 | |
| 378 Line* line = &lines_.back(); | |
| 379 line->segments.push_back(segment); | |
| 380 line->size.set_width(line->size.width() + segment.x_range.length()); | |
| 381 if (run->script_analysis.fRTL) { | |
| 382 rtl_segments_.push_back(SegmentHandle(lines_.size() - 1, | |
| 383 line->segments.size() - 1)); | |
| 384 // If this is the last segment of an RTL run, reprocess the text-space x | |
| 385 // ranges of all segments from the run. | |
| 386 if (char_range.end() == run->range.end()) | |
| 387 UpdateRTLSegmentRanges(); | |
| 388 } | |
| 389 text_x_ += width; | |
| 390 line_x_ += width; | |
| 391 } | |
| 392 | |
| 393 const int max_width_; | |
| 394 const bool multiline_; | |
| 395 const BreakList<size_t>* const words_; | |
| 396 const ScopedVector<TextRun>& runs_; | |
| 397 | |
| 398 // Stores the resulting lines. | |
| 399 std::vector<Line> lines_; | |
| 400 | |
| 401 // |text_x_| and |line_x_| are text-space and line-space x coordinates of the | |
|
msw
2013/09/06 23:47:45
nit: remove "|text_x_| and |line_x_| are ".
ckocagil
2013/09/11 14:59:49
Done.
| |
| 402 // next segment to be added. | |
| 403 int text_x_; | |
| 404 int line_x_; | |
| 405 | |
| 406 // Size of the multi-line text, not including the currently processed line. | |
| 407 Size total_size_; | |
| 408 | |
| 409 // Ascent and descent values of the current line, |lines_.back()|. | |
| 410 int line_ascent_; | |
| 411 int line_descent_; | |
| 412 | |
| 413 // Segments to be applied by |UpdateRTLSegmentRanges()|. Contains the segments | |
|
msw
2013/09/06 23:47:45
nit: consider "// The current RTL run segments, to
ckocagil
2013/09/11 14:59:49
Done.
| |
| 414 // of the current run, if the run is RTL. | |
| 415 std::vector<SegmentHandle> rtl_segments_; | |
| 416 | |
| 417 DISALLOW_COPY_AND_ASSIGN(LineBreaker); | |
| 418 }; | |
| 419 | |
| 200 } // namespace internal | 420 } // namespace internal |
| 201 | 421 |
| 202 // static | 422 // static |
| 203 HDC RenderTextWin::cached_hdc_ = NULL; | 423 HDC RenderTextWin::cached_hdc_ = NULL; |
| 204 | 424 |
| 205 // static | 425 // static |
| 206 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_; | 426 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_; |
| 207 | 427 |
| 208 RenderTextWin::RenderTextWin() | 428 RenderTextWin::RenderTextWin() |
| 209 : RenderText(), | 429 : RenderText(), |
| 210 common_baseline_(0), | 430 common_baseline_(0), |
|
msw
2013/09/06 23:47:45
Remove this and just return the first line's basel
ckocagil
2013/09/11 14:59:49
Done.
| |
| 211 needs_layout_(false) { | 431 needs_layout_(false) { |
| 212 set_truncate_length(kMaxUniscribeTextLength); | 432 set_truncate_length(kMaxUniscribeTextLength); |
| 213 | 433 |
| 214 memset(&script_control_, 0, sizeof(script_control_)); | 434 memset(&script_control_, 0, sizeof(script_control_)); |
| 215 memset(&script_state_, 0, sizeof(script_state_)); | 435 memset(&script_state_, 0, sizeof(script_state_)); |
| 216 | 436 |
| 217 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT)); | 437 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT)); |
| 218 } | 438 } |
| 219 | 439 |
| 220 RenderTextWin::~RenderTextWin() { | 440 RenderTextWin::~RenderTextWin() { |
| 221 } | 441 } |
| 222 | 442 |
| 223 Size RenderTextWin::GetStringSize() { | 443 Size RenderTextWin::GetStringSize() { |
| 224 EnsureLayout(); | 444 EnsureLayout(); |
| 225 return string_size_; | 445 // TODO(ckocagil): Always return |multiline_string_size| and remove |
| 446 // |string_size_| if possible. | |
|
msw
2013/09/06 23:47:45
nit: add some detail about why this should/can't b
ckocagil
2013/09/11 14:59:49
I resolved this TODO by updating LineBreaker and t
msw
2013/09/11 17:32:37
Awesome, but I have some comments about the next b
| |
| 447 return multiline() ? multiline_string_size_ : string_size_; | |
| 226 } | 448 } |
| 227 | 449 |
| 228 int RenderTextWin::GetBaseline() { | 450 int RenderTextWin::GetBaseline() { |
| 229 EnsureLayout(); | 451 EnsureLayout(); |
| 230 return common_baseline_; | 452 return common_baseline_; |
| 231 } | 453 } |
| 232 | 454 |
| 233 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { | 455 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { |
| 234 if (text().empty()) | 456 if (text().empty()) |
| 235 return SelectionModel(); | 457 return SelectionModel(); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 376 GetGlyphXBoundary(run, layout_index, true)); | 598 GetGlyphXBoundary(run, layout_index, true)); |
| 377 } | 599 } |
| 378 | 600 |
| 379 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) { | 601 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) { |
| 380 DCHECK(!needs_layout_); | 602 DCHECK(!needs_layout_); |
| 381 DCHECK(ui::Range(0, text().length()).Contains(range)); | 603 DCHECK(ui::Range(0, text().length()).Contains(range)); |
| 382 ui::Range layout_range(TextIndexToLayoutIndex(range.start()), | 604 ui::Range layout_range(TextIndexToLayoutIndex(range.start()), |
| 383 TextIndexToLayoutIndex(range.end())); | 605 TextIndexToLayoutIndex(range.end())); |
| 384 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range)); | 606 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range)); |
| 385 | 607 |
| 386 std::vector<Rect> bounds; | 608 std::vector<Rect> rects; |
| 387 if (layout_range.is_empty()) | 609 if (layout_range.is_empty()) |
| 388 return bounds; | 610 return rects; |
| 611 std::vector<ui::Range> bounds; | |
| 389 | 612 |
| 390 // Add a Rect for each run/selection intersection. | 613 // Add a Range for each run/selection intersection. |
| 391 // TODO(msw): The bounds should probably not always be leading the range ends. | 614 // TODO(msw): The bounds should probably not always be leading the range ends. |
| 392 for (size_t i = 0; i < runs_.size(); ++i) { | 615 for (size_t i = 0; i < runs_.size(); ++i) { |
| 393 const internal::TextRun* run = runs_[visual_to_logical_[i]]; | 616 const internal::TextRun* run = runs_[visual_to_logical_[i]]; |
| 394 ui::Range intersection = run->range.Intersect(layout_range); | 617 ui::Range intersection = run->range.Intersect(layout_range); |
| 395 if (intersection.IsValid()) { | 618 if (intersection.IsValid()) { |
| 396 DCHECK(!intersection.is_reversed()); | 619 DCHECK(!intersection.is_reversed()); |
| 397 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false), | 620 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false), |
| 398 GetGlyphXBoundary(run, intersection.end(), false)); | 621 GetGlyphXBoundary(run, intersection.end(), false)); |
| 399 Rect rect(range_x.GetMin(), 0, range_x.length(), run->font.GetHeight()); | 622 if (range_x.is_empty()) |
| 400 rect.set_origin(ToViewPoint(rect.origin())); | 623 continue; |
| 401 // Union this with the last rect if they're adjacent. | 624 range_x = ui::Range(range_x.GetMin(), range_x.GetMax()); |
| 402 if (!bounds.empty() && rect.SharesEdgeWith(bounds.back())) { | 625 // Union this with the last range if they're adjacent. |
| 403 rect.Union(bounds.back()); | 626 DCHECK(bounds.empty() || bounds.back().GetMin() != range_x.GetMax()); |
|
msw
2013/09/06 23:47:45
nit: Use DCHECK(bounds.empty() || bounds.back().Ge
ckocagil
2013/09/11 14:59:49
Done.
| |
| 627 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) { | |
| 628 range_x = ui::Range(bounds.back().GetMin(), range_x.GetMax()); | |
| 404 bounds.pop_back(); | 629 bounds.pop_back(); |
| 405 } | 630 } |
| 406 bounds.push_back(rect); | 631 bounds.push_back(range_x); |
| 407 } | 632 } |
| 408 } | 633 } |
| 409 return bounds; | 634 for (size_t i = 0; i < bounds.size(); ++i) { |
| 635 std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]); | |
| 636 rects.insert(rects.end(), current_rects.begin(), current_rects.end()); | |
| 637 } | |
| 638 return rects; | |
| 410 } | 639 } |
| 411 | 640 |
| 412 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const { | 641 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const { |
| 413 DCHECK_LE(index, text().length()); | 642 DCHECK_LE(index, text().length()); |
| 414 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index; | 643 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index; |
| 415 CHECK_GE(i, 0); | 644 CHECK_GE(i, 0); |
| 416 // Clamp layout indices to the length of the text actually used for layout. | 645 // Clamp layout indices to the length of the text actually used for layout. |
| 417 return std::min<size_t>(GetLayoutText().length(), i); | 646 return std::min<size_t>(GetLayoutText().length(), i); |
| 418 } | 647 } |
| 419 | 648 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 441 position < LayoutIndexToTextIndex(GetLayoutText().length()) && | 670 position < LayoutIndexToTextIndex(GetLayoutText().length()) && |
| 442 GetGlyphBounds(position) != GetGlyphBounds(position - 1); | 671 GetGlyphBounds(position) != GetGlyphBounds(position - 1); |
| 443 } | 672 } |
| 444 | 673 |
| 445 void RenderTextWin::ResetLayout() { | 674 void RenderTextWin::ResetLayout() { |
| 446 // Layout is performed lazily as needed for drawing/metrics. | 675 // Layout is performed lazily as needed for drawing/metrics. |
| 447 needs_layout_ = true; | 676 needs_layout_ = true; |
| 448 } | 677 } |
| 449 | 678 |
| 450 void RenderTextWin::EnsureLayout() { | 679 void RenderTextWin::EnsureLayout() { |
| 451 if (!needs_layout_) | 680 if (needs_layout_) { |
| 452 return; | 681 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. |
| 453 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. | 682 ItemizeLogicalText(); |
| 454 ItemizeLogicalText(); | 683 if (!runs_.empty()) |
| 455 if (!runs_.empty()) | 684 LayoutVisualText(); |
| 456 LayoutVisualText(); | 685 needs_layout_ = false; |
| 457 needs_layout_ = false; | 686 std::vector<internal::Line> lines; |
| 687 set_lines(&lines); | |
| 688 } | |
| 689 | |
| 690 // Compute lines if they're not valid. This is separate from the layout steps | |
| 691 // above to avoid text layout and shaping when we resize |display_rect_|. | |
| 692 if (lines().empty()) { | |
| 693 DCHECK(!needs_layout_); | |
| 694 std::vector<internal::Line> lines; | |
| 695 internal::LineBreaker line_breaker(display_rect().width() - 1, multiline(), | |
| 696 multiline() ? &GetLineBreaks() : NULL, | |
| 697 runs_); | |
| 698 for (size_t i = 0; i < runs_.size(); ++i) | |
| 699 line_breaker.AddRun(visual_to_logical_[i]); | |
| 700 line_breaker.Finalize(&lines, &multiline_string_size_); | |
| 701 DCHECK(!lines.empty()); | |
| 702 set_lines(&lines); | |
| 703 } | |
| 458 } | 704 } |
| 459 | 705 |
| 460 void RenderTextWin::DrawVisualText(Canvas* canvas) { | 706 void RenderTextWin::DrawVisualText(Canvas* canvas) { |
| 461 DCHECK(!needs_layout_); | 707 DCHECK(!needs_layout_); |
| 462 | 708 DCHECK(!multiline() || !lines().empty()); |
|
msw
2013/09/06 23:47:45
Shouldn't lines() be non-empty here, even in the s
ckocagil
2013/09/11 14:59:49
Done; the "!multiline()" was a leftover from a pre
| |
| 463 // Skia will draw glyphs with respect to the baseline. | |
| 464 Vector2d offset(GetTextOffset() + Vector2d(0, common_baseline_)); | |
| 465 | |
| 466 SkScalar x = SkIntToScalar(offset.x()); | |
| 467 SkScalar y = SkIntToScalar(offset.y()); | |
| 468 | 709 |
| 469 std::vector<SkPoint> pos; | 710 std::vector<SkPoint> pos; |
| 470 | 711 |
| 471 internal::SkiaTextRenderer renderer(canvas); | 712 internal::SkiaTextRenderer renderer(canvas); |
| 472 ApplyFadeEffects(&renderer); | 713 ApplyFadeEffects(&renderer); |
| 473 ApplyTextShadows(&renderer); | 714 ApplyTextShadows(&renderer); |
| 474 | 715 |
| 475 bool smoothing_enabled; | 716 bool smoothing_enabled; |
| 476 bool cleartype_enabled; | 717 bool cleartype_enabled; |
| 477 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); | 718 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); |
| 478 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. | 719 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. |
| 479 renderer.SetFontSmoothingSettings( | 720 renderer.SetFontSmoothingSettings( |
| 480 smoothing_enabled, cleartype_enabled && !background_is_transparent()); | 721 smoothing_enabled, cleartype_enabled && !background_is_transparent()); |
| 481 | 722 |
| 482 ApplyCompositionAndSelectionStyles(); | 723 ApplyCompositionAndSelectionStyles(); |
| 483 | 724 |
| 484 for (size_t i = 0; i < runs_.size(); ++i) { | 725 for (size_t i = 0; i < lines().size(); ++i) { |
| 485 // Get the run specified by the visual-to-logical map. | 726 const internal::Line& line = lines()[i]; |
| 486 internal::TextRun* run = runs_[visual_to_logical_[i]]; | 727 Vector2d line_offset = GetLineOffset(i); |
| 728 Vector2d text_offset = line_offset + Vector2d(0, line.baseline); | |
|
msw
2013/09/06 23:47:45
nit: declare |text_offset| and |preceding_segment_
ckocagil
2013/09/11 14:59:49
Done. (assuming you meant "before the loop")
| |
| 729 int preceding_segment_widths = 0; | |
| 487 | 730 |
| 488 // Skip painting empty runs and runs outside the display rect area. | 731 // Skip painting empty lines or lines outside the display rect area. |
| 489 if ((run->glyph_count == 0) || (x >= display_rect().right()) || | 732 if (!display_rect().Intersects(Rect(PointAtOffsetFromOrigin(line_offset), |
| 490 (x + run->width <= display_rect().x())) { | 733 line.size))) |
| 491 x += run->width; | |
| 492 continue; | 734 continue; |
| 735 | |
| 736 for (size_t j = 0; j < line.segments.size(); ++j) { | |
| 737 const internal::LineSegment* segment = &line.segments[j]; | |
| 738 const int segment_width = segment->x_range.length(); | |
| 739 const internal::TextRun* run = runs_[segment->run]; | |
| 740 DCHECK(!segment->char_range.is_empty()); | |
| 741 DCHECK(run->range.Contains(segment->char_range)); | |
| 742 ui::Range glyph_range = CharRangeToGlyphRange(*run, segment->char_range); | |
| 743 if (glyph_range.is_empty()) { | |
|
msw
2013/09/06 23:47:45
Can you instead DCHECK(!glyph_range.is_empty())? S
ckocagil
2013/09/11 14:59:49
Done. We don't seem to hit that DCHECK with the st
msw
2013/09/11 17:32:37
Leave it as a DCHECK for now; we safely handle it
| |
| 744 DCHECK_EQ(segment_width, 0); | |
| 745 continue; | |
| 746 } | |
| 747 // Skip painting segments outside the display rect area. | |
| 748 if (!multiline()) { | |
| 749 const Rect segment_bounds(PointAtOffsetFromOrigin(line_offset) + | |
| 750 Vector2d(preceding_segment_widths, 0), | |
| 751 Size(segment_width, line.size.height())); | |
| 752 if (!display_rect().Intersects(segment_bounds)) { | |
| 753 preceding_segment_widths += segment_width; | |
| 754 continue; | |
| 755 } | |
| 756 } | |
| 757 | |
| 758 // |pos| contains the positions of glyphs. An extra terminal |pos| entry | |
| 759 // is added to simplify width calculations. | |
| 760 int segment_x = preceding_segment_widths; | |
| 761 pos.resize(glyph_range.length() + 1); | |
| 762 for (size_t k = glyph_range.start(); k < glyph_range.end(); ++k) { | |
| 763 pos[k - glyph_range.start()].set( | |
| 764 SkIntToScalar(text_offset.x() + segment_x + run->offsets[k].du), | |
|
msw
2013/09/06 23:47:45
nit: swap the order of |segment_x| and |run->offse
ckocagil
2013/09/11 14:59:49
Done.
| |
| 765 SkIntToScalar(text_offset.y() + run->offsets[k].dv)); | |
| 766 segment_x += run->advance_widths[k]; | |
| 767 } | |
| 768 pos.back().set(SkIntToScalar(text_offset.x() + segment_x), | |
| 769 SkIntToScalar(text_offset.y())); | |
| 770 | |
| 771 renderer.SetTextSize(run->font.GetFontSize()); | |
| 772 renderer.SetFontFamilyWithStyle(run->font.GetFontName(), run->font_style); | |
| 773 | |
| 774 for (BreakList<SkColor>::const_iterator it = | |
| 775 colors().GetBreak(run->range.start()); | |
|
msw
2013/09/06 23:47:45
Should run->range.start() be changed to segment->c
ckocagil
2013/09/11 14:59:49
Yes. Done.
| |
| 776 it != colors().breaks().end() && it->first < run->range.end(); | |
|
msw
2013/09/06 23:47:45
Should run->range.end() be changed to segment->cha
ckocagil
2013/09/11 14:59:49
Yes. Done.
| |
| 777 ++it) { | |
| 778 const ui::Range intersection = | |
| 779 colors().GetRange(it).Intersect(segment->char_range); | |
| 780 const ui::Range colored_glyphs = | |
| 781 CharRangeToGlyphRange(*run, intersection); | |
| 782 DCHECK(glyph_range.Contains(colored_glyphs)); | |
| 783 if (colored_glyphs.is_empty()) | |
|
msw
2013/09/06 23:47:45
With the above changes to use segment->char_range,
ckocagil
2013/09/11 14:59:49
Done.
| |
| 784 continue; | |
| 785 const SkPoint& start_pos = | |
| 786 pos[colored_glyphs.start() - glyph_range.start()]; | |
| 787 const SkPoint& end_pos = | |
| 788 pos[colored_glyphs.end() - glyph_range.start()]; | |
| 789 | |
| 790 renderer.SetForegroundColor(it->second); | |
| 791 renderer.DrawPosText(&start_pos, &run->glyphs[colored_glyphs.start()], | |
| 792 colored_glyphs.length()); | |
| 793 renderer.DrawDecorations(start_pos.x(), text_offset.y(), | |
| 794 SkScalarCeilToInt(end_pos.x() - start_pos.x()), | |
| 795 run->underline, run->strike, | |
| 796 run->diagonal_strike); | |
| 797 } | |
| 798 | |
| 799 preceding_segment_widths += segment_width; | |
| 493 } | 800 } |
| 494 | |
| 495 // Based on WebCore::skiaDrawText. |pos| contains the positions of glyphs. | |
| 496 // An extra terminal |pos| entry is added to simplify width calculations. | |
| 497 pos.resize(run->glyph_count + 1); | |
| 498 SkScalar glyph_x = x; | |
| 499 for (int glyph = 0; glyph < run->glyph_count; glyph++) { | |
| 500 pos[glyph].set(glyph_x + run->offsets[glyph].du, | |
| 501 y + run->offsets[glyph].dv); | |
| 502 glyph_x += SkIntToScalar(run->advance_widths[glyph]); | |
| 503 } | |
| 504 pos.back().set(glyph_x, y); | |
| 505 | |
| 506 renderer.SetTextSize(run->font.GetFontSize()); | |
| 507 renderer.SetFontFamilyWithStyle(run->font.GetFontName(), run->font_style); | |
| 508 | |
| 509 for (BreakList<SkColor>::const_iterator it = | |
| 510 colors().GetBreak(run->range.start()); | |
| 511 it != colors().breaks().end() && it->first < run->range.end(); | |
| 512 ++it) { | |
| 513 const ui::Range glyph_range = CharRangeToGlyphRange(*run, | |
| 514 colors().GetRange(it).Intersect(run->range)); | |
| 515 if (glyph_range.is_empty()) | |
| 516 continue; | |
| 517 renderer.SetForegroundColor(it->second); | |
| 518 renderer.DrawPosText(&pos[glyph_range.start()], | |
| 519 &run->glyphs[glyph_range.start()], | |
| 520 glyph_range.length()); | |
| 521 const SkScalar width = pos[glyph_range.end()].x() - | |
| 522 pos[glyph_range.start()].x(); | |
| 523 renderer.DrawDecorations(pos[glyph_range.start()].x(), y, | |
| 524 SkScalarCeilToInt(width), run->underline, | |
| 525 run->strike, run->diagonal_strike); | |
| 526 } | |
| 527 | |
| 528 DCHECK_EQ(glyph_x - x, run->width); | |
| 529 x = glyph_x; | |
| 530 } | 801 } |
| 531 | 802 |
| 532 UndoCompositionAndSelectionStyles(); | 803 UndoCompositionAndSelectionStyles(); |
| 533 } | 804 } |
| 534 | 805 |
| 535 void RenderTextWin::ItemizeLogicalText() { | 806 void RenderTextWin::ItemizeLogicalText() { |
| 536 runs_.clear(); | 807 runs_.clear(); |
| 537 // Make |string_size_|'s height and |common_baseline_| tall enough to draw | 808 // Make |string_size_|'s height and |common_baseline_| tall enough to draw |
| 538 // often-used characters which are rendered with fonts in the font list. | 809 // often-used characters which are rendered with fonts in the font list. |
| 539 string_size_ = Size(0, font_list().GetHeight()); | 810 string_size_ = Size(0, font_list().GetHeight()); |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 897 size_t position = LayoutIndexToTextIndex(run->range.end()); | 1168 size_t position = LayoutIndexToTextIndex(run->range.end()); |
| 898 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); | 1169 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); |
| 899 return SelectionModel(position, CURSOR_FORWARD); | 1170 return SelectionModel(position, CURSOR_FORWARD); |
| 900 } | 1171 } |
| 901 | 1172 |
| 902 RenderText* RenderText::CreateInstance() { | 1173 RenderText* RenderText::CreateInstance() { |
| 903 return new RenderTextWin; | 1174 return new RenderTextWin; |
| 904 } | 1175 } |
| 905 | 1176 |
| 906 } // namespace gfx | 1177 } // namespace gfx |
| OLD | NEW |