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|. |
| 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. |
| 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)|. |
| 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]; |
| 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_, |
| 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 |
| 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 |
| 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), |
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 return string_size_; |
226 } | 446 } |
227 | 447 |
| 448 Size RenderTextWin::GetMultilineTextSize() { |
| 449 if (!multiline()) |
| 450 return GetStringSize(); |
| 451 EnsureLayout(); |
| 452 return multiline_string_size_; |
| 453 } |
| 454 |
228 int RenderTextWin::GetBaseline() { | 455 int RenderTextWin::GetBaseline() { |
229 EnsureLayout(); | 456 EnsureLayout(); |
230 return common_baseline_; | 457 return common_baseline_; |
231 } | 458 } |
232 | 459 |
233 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { | 460 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { |
234 if (text().empty()) | 461 if (text().empty()) |
235 return SelectionModel(); | 462 return SelectionModel(); |
236 | 463 |
237 EnsureLayout(); | 464 EnsureLayout(); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 GetGlyphXBoundary(run, layout_index, true)); | 603 GetGlyphXBoundary(run, layout_index, true)); |
377 } | 604 } |
378 | 605 |
379 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) { | 606 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) { |
380 DCHECK(!needs_layout_); | 607 DCHECK(!needs_layout_); |
381 DCHECK(ui::Range(0, text().length()).Contains(range)); | 608 DCHECK(ui::Range(0, text().length()).Contains(range)); |
382 ui::Range layout_range(TextIndexToLayoutIndex(range.start()), | 609 ui::Range layout_range(TextIndexToLayoutIndex(range.start()), |
383 TextIndexToLayoutIndex(range.end())); | 610 TextIndexToLayoutIndex(range.end())); |
384 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range)); | 611 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range)); |
385 | 612 |
386 std::vector<Rect> bounds; | 613 std::vector<Rect> rects; |
387 if (layout_range.is_empty()) | 614 if (layout_range.is_empty()) |
388 return bounds; | 615 return rects; |
| 616 std::vector<ui::Range> bounds; |
389 | 617 |
390 // Add a Rect for each run/selection intersection. | 618 // Add a Range for each run/selection intersection. |
391 // TODO(msw): The bounds should probably not always be leading the range ends. | 619 // TODO(msw): The bounds should probably not always be leading the range ends. |
392 for (size_t i = 0; i < runs_.size(); ++i) { | 620 for (size_t i = 0; i < runs_.size(); ++i) { |
393 const internal::TextRun* run = runs_[visual_to_logical_[i]]; | 621 const internal::TextRun* run = runs_[visual_to_logical_[i]]; |
394 ui::Range intersection = run->range.Intersect(layout_range); | 622 ui::Range intersection = run->range.Intersect(layout_range); |
395 if (intersection.IsValid()) { | 623 if (intersection.IsValid()) { |
396 DCHECK(!intersection.is_reversed()); | 624 DCHECK(!intersection.is_reversed()); |
397 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false), | 625 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false), |
398 GetGlyphXBoundary(run, intersection.end(), false)); | 626 GetGlyphXBoundary(run, intersection.end(), false)); |
399 Rect rect(range_x.GetMin(), 0, range_x.length(), run->font.GetHeight()); | 627 if (range_x.is_empty()) |
400 rect.set_origin(ToViewPoint(rect.origin())); | 628 continue; |
401 // Union this with the last rect if they're adjacent. | 629 range_x = ui::Range(range_x.GetMin(), range_x.GetMax()); |
402 if (!bounds.empty() && rect.SharesEdgeWith(bounds.back())) { | 630 // Union this with the last range if they're adjacent. |
403 rect.Union(bounds.back()); | 631 DCHECK(bounds.empty() || bounds.back().GetMin() != range_x.GetMax()); |
| 632 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) { |
| 633 range_x = ui::Range(bounds.back().GetMin(), range_x.GetMax()); |
404 bounds.pop_back(); | 634 bounds.pop_back(); |
405 } | 635 } |
406 bounds.push_back(rect); | 636 bounds.push_back(range_x); |
407 } | 637 } |
408 } | 638 } |
409 return bounds; | 639 for (size_t i = 0; i < bounds.size(); ++i) { |
| 640 std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]); |
| 641 rects.insert(rects.end(), current_rects.begin(), current_rects.end()); |
| 642 } |
| 643 return rects; |
410 } | 644 } |
411 | 645 |
412 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const { | 646 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const { |
413 DCHECK_LE(index, text().length()); | 647 DCHECK_LE(index, text().length()); |
414 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index; | 648 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index; |
415 CHECK_GE(i, 0); | 649 CHECK_GE(i, 0); |
416 // Clamp layout indices to the length of the text actually used for layout. | 650 // Clamp layout indices to the length of the text actually used for layout. |
417 return std::min<size_t>(GetLayoutText().length(), i); | 651 return std::min<size_t>(GetLayoutText().length(), i); |
418 } | 652 } |
419 | 653 |
(...skipping 21 matching lines...) Expand all Loading... |
441 position < LayoutIndexToTextIndex(GetLayoutText().length()) && | 675 position < LayoutIndexToTextIndex(GetLayoutText().length()) && |
442 GetGlyphBounds(position) != GetGlyphBounds(position - 1); | 676 GetGlyphBounds(position) != GetGlyphBounds(position - 1); |
443 } | 677 } |
444 | 678 |
445 void RenderTextWin::ResetLayout() { | 679 void RenderTextWin::ResetLayout() { |
446 // Layout is performed lazily as needed for drawing/metrics. | 680 // Layout is performed lazily as needed for drawing/metrics. |
447 needs_layout_ = true; | 681 needs_layout_ = true; |
448 } | 682 } |
449 | 683 |
450 void RenderTextWin::EnsureLayout() { | 684 void RenderTextWin::EnsureLayout() { |
451 if (!needs_layout_) | 685 if (needs_layout_) { |
452 return; | 686 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. |
453 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. | 687 ItemizeLogicalText(); |
454 ItemizeLogicalText(); | 688 if (!runs_.empty()) |
455 if (!runs_.empty()) | 689 LayoutVisualText(); |
456 LayoutVisualText(); | 690 needs_layout_ = false; |
457 needs_layout_ = false; | 691 std::vector<internal::Line> lines; |
| 692 set_lines(&lines); |
| 693 } |
| 694 |
| 695 // Compute lines if they're not valid. This is separate from the layout steps |
| 696 // above to avoid text layout and shaping when we resize |display_rect_|. |
| 697 if (lines().empty()) { |
| 698 DCHECK(!needs_layout_); |
| 699 std::vector<internal::Line> lines; |
| 700 internal::LineBreaker line_breaker(display_rect().width() - 1, multiline(), |
| 701 multiline() ? &GetLineBreaks() : NULL, |
| 702 runs_); |
| 703 for (size_t i = 0; i < runs_.size(); ++i) |
| 704 line_breaker.AddRun(visual_to_logical_[i]); |
| 705 line_breaker.Finalize(&lines, &multiline_string_size_); |
| 706 DCHECK(!lines.empty()); |
| 707 set_lines(&lines); |
| 708 } |
458 } | 709 } |
459 | 710 |
460 void RenderTextWin::DrawVisualText(Canvas* canvas) { | 711 void RenderTextWin::DrawVisualText(Canvas* canvas) { |
461 DCHECK(!needs_layout_); | 712 DCHECK(!needs_layout_); |
462 | 713 DCHECK(!multiline() || !lines().empty()); |
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 | 714 |
469 std::vector<SkPoint> pos; | 715 std::vector<SkPoint> pos; |
470 | 716 |
471 internal::SkiaTextRenderer renderer(canvas); | 717 internal::SkiaTextRenderer renderer(canvas); |
472 ApplyFadeEffects(&renderer); | 718 ApplyFadeEffects(&renderer); |
473 ApplyTextShadows(&renderer); | 719 ApplyTextShadows(&renderer); |
474 | 720 |
475 bool smoothing_enabled; | 721 bool smoothing_enabled; |
476 bool cleartype_enabled; | 722 bool cleartype_enabled; |
477 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); | 723 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); |
478 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. | 724 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. |
479 renderer.SetFontSmoothingSettings( | 725 renderer.SetFontSmoothingSettings( |
480 smoothing_enabled, cleartype_enabled && !background_is_transparent()); | 726 smoothing_enabled, cleartype_enabled && !background_is_transparent()); |
481 | 727 |
482 ApplyCompositionAndSelectionStyles(); | 728 ApplyCompositionAndSelectionStyles(); |
483 | 729 |
484 for (size_t i = 0; i < runs_.size(); ++i) { | 730 for (size_t i = 0; i < lines().size(); ++i) { |
485 // Get the run specified by the visual-to-logical map. | 731 const internal::Line& line = lines()[i]; |
486 internal::TextRun* run = runs_[visual_to_logical_[i]]; | 732 Vector2d line_offset = GetLineOffset(i); |
| 733 Vector2d text_offset = line_offset + Vector2d(0, line.baseline); |
| 734 int preceding_segment_widths = 0; |
487 | 735 |
488 // Skip painting empty runs and runs outside the display rect area. | 736 // Skip painting empty lines or lines outside the display rect area. |
489 if ((run->glyph_count == 0) || (x >= display_rect().right()) || | 737 if (!display_rect().Intersects(Rect(PointAtOffsetFromOrigin(line_offset), |
490 (x + run->width <= display_rect().x())) { | 738 line.size))) |
491 x += run->width; | |
492 continue; | 739 continue; |
| 740 |
| 741 for (size_t j = 0; j < line.segments.size(); ++j) { |
| 742 const internal::LineSegment* segment = &line.segments[j]; |
| 743 const int segment_width = segment->x_range.length(); |
| 744 const internal::TextRun* run = runs_[segment->run]; |
| 745 DCHECK(!segment->char_range.is_empty()); |
| 746 DCHECK(run->range.Contains(segment->char_range)); |
| 747 ui::Range glyph_range = CharRangeToGlyphRange(*run, segment->char_range); |
| 748 if (glyph_range.is_empty()) { |
| 749 DCHECK_EQ(segment_width, 0); |
| 750 continue; |
| 751 } |
| 752 // Skip painting segments outside the display rect area. |
| 753 if (!multiline()) { |
| 754 const Rect segment_bounds(PointAtOffsetFromOrigin(line_offset) + |
| 755 Vector2d(preceding_segment_widths, 0), |
| 756 Size(segment_width, line.size.height())); |
| 757 if (!display_rect().Intersects(segment_bounds)) { |
| 758 preceding_segment_widths += segment_width; |
| 759 continue; |
| 760 } |
| 761 } |
| 762 |
| 763 // |pos| contains the positions of glyphs. An extra terminal |pos| entry |
| 764 // is added to simplify width calculations. |
| 765 int segment_x = preceding_segment_widths; |
| 766 pos.resize(glyph_range.length() + 1); |
| 767 for (size_t k = glyph_range.start(); k < glyph_range.end(); ++k) { |
| 768 pos[k - glyph_range.start()].set( |
| 769 SkIntToScalar(text_offset.x() + segment_x + run->offsets[k].du), |
| 770 SkIntToScalar(text_offset.y() + run->offsets[k].dv)); |
| 771 segment_x += run->advance_widths[k]; |
| 772 } |
| 773 pos.back().set(SkIntToScalar(text_offset.x() + segment_x), |
| 774 SkIntToScalar(text_offset.y())); |
| 775 |
| 776 renderer.SetTextSize(run->font.GetFontSize()); |
| 777 renderer.SetFontFamilyWithStyle(run->font.GetFontName(), run->font_style); |
| 778 |
| 779 for (BreakList<SkColor>::const_iterator it = |
| 780 colors().GetBreak(run->range.start()); |
| 781 it != colors().breaks().end() && it->first < run->range.end(); |
| 782 ++it) { |
| 783 const ui::Range intersection = |
| 784 colors().GetRange(it).Intersect(segment->char_range); |
| 785 const ui::Range colored_glyphs = |
| 786 CharRangeToGlyphRange(*run, intersection); |
| 787 DCHECK(glyph_range.Contains(colored_glyphs)); |
| 788 if (colored_glyphs.is_empty()) |
| 789 continue; |
| 790 const SkPoint& start_pos = |
| 791 pos[colored_glyphs.start() - glyph_range.start()]; |
| 792 const SkPoint& end_pos = |
| 793 pos[colored_glyphs.end() - glyph_range.start()]; |
| 794 |
| 795 renderer.SetForegroundColor(it->second); |
| 796 renderer.DrawPosText(&start_pos, &run->glyphs[colored_glyphs.start()], |
| 797 colored_glyphs.length()); |
| 798 renderer.DrawDecorations(start_pos.x(), text_offset.y(), |
| 799 SkScalarCeilToInt(end_pos.x() - start_pos.x()), |
| 800 run->underline, run->strike, |
| 801 run->diagonal_strike); |
| 802 } |
| 803 |
| 804 preceding_segment_widths += segment_width; |
493 } | 805 } |
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 } | 806 } |
531 | 807 |
532 UndoCompositionAndSelectionStyles(); | 808 UndoCompositionAndSelectionStyles(); |
533 } | 809 } |
534 | 810 |
535 void RenderTextWin::ItemizeLogicalText() { | 811 void RenderTextWin::ItemizeLogicalText() { |
536 runs_.clear(); | 812 runs_.clear(); |
537 // Make |string_size_|'s height and |common_baseline_| tall enough to draw | 813 // 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. | 814 // often-used characters which are rendered with fonts in the font list. |
539 string_size_ = Size(0, font_list().GetHeight()); | 815 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()); | 1173 size_t position = LayoutIndexToTextIndex(run->range.end()); |
898 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); | 1174 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); |
899 return SelectionModel(position, CURSOR_FORWARD); | 1175 return SelectionModel(position, CURSOR_FORWARD); |
900 } | 1176 } |
901 | 1177 |
902 RenderText* RenderText::CreateInstance() { | 1178 RenderText* RenderText::CreateInstance() { |
903 return new RenderTextWin; | 1179 return new RenderTextWin; |
904 } | 1180 } |
905 | 1181 |
906 } // namespace gfx | 1182 } // namespace gfx |
OLD | NEW |