Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(422)

Side by Side Diff: ui/gfx/render_text_win.cc

Issue 16867016: Windows implementation of multiline RenderText (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Alexei's comments Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/render_text_win.h ('k') | ui/views/examples/multiline_example.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 struct BreakRunResults {
158 // If |overflowed|, holds the width of [start_char, overflow_pos].
159 // Otherwise, holds the width of [start_char, run.range.end()).
160 int width;
161
162 // Index of the first character that doesn't fit the given width.
163 size_t overflow_pos;
164
165 // Width of the character at |overflow_pos|.
166 int char_rollback_width;
167
168 // Index of the last word break before |overflow_pos|.
169 size_t word_rollback_pos;
170
171 // Width of [word_rollback_pos, overflow_pos].
172 int word_rollback_width;
173 };
174
175 // Starting from |start_char|, finds the first character that doesn't fit the
176 // given |available_width|. If |available_width| is reached, returns true and
177 // fills all the fields in |results|. Otherwise returns false and only fills
178 // |results->width|.
179 bool BreakRunAtWidth(const internal::TextRun& run,
180 const BreakList<size_t>& breaks,
181 size_t start_char,
182 int available_width,
183 BreakRunResults* results) {
184 DCHECK(run.range.Contains(ui::Range(start_char)));
185 BreakList<size_t>::const_iterator current_word = breaks.GetBreak(start_char);
186 BreakList<size_t>::const_iterator next_word = current_word + 1;
187 // x distance from |current_word|.
188 int current_word_x = 0;
189 // x distance from |start_char|.
190 int x = 0;
191
192 for (size_t i = start_char; i < run.range.end(); ++i) {
193 if (next_word != breaks.breaks().end() && i >= next_word->first) {
194 current_word = next_word++;
195 current_word_x = 0;
196 }
197
198 ui::Range glyphs = CharRangeToGlyphRange(run, ui::Range(i, i + 1));
199 int char_width = 0;
200 for (size_t j = glyphs.start(); j < glyphs.end(); ++j)
201 char_width += run.advance_widths[j];
202
203 x += char_width;
204 current_word_x += char_width;
205
206 if (x > available_width) {
207 results->overflow_pos = i;
208 results->width = x;
209 results->char_rollback_width = char_width;
210 results->word_rollback_pos = std::max(current_word->first, start_char);
211 results->word_rollback_width = current_word_x;
212 return true;
213 }
214 }
215
216 results->width = x;
217 return false;
218 }
219
157 } // namespace 220 } // namespace
158 221
159 namespace internal { 222 namespace internal {
160 223
161 TextRun::TextRun() 224 TextRun::TextRun()
162 : font_style(0), 225 : font_style(0),
163 strike(false), 226 strike(false),
164 diagonal_strike(false), 227 diagonal_strike(false),
165 underline(false), 228 underline(false),
166 width(0), 229 width(0),
(...skipping 23 matching lines...) Expand all
190 run->glyph_count, 253 run->glyph_count,
191 run->logical_clusters.get(), 254 run->logical_clusters.get(),
192 run->visible_attributes.get(), 255 run->visible_attributes.get(),
193 run->advance_widths.get(), 256 run->advance_widths.get(),
194 &run->script_analysis, 257 &run->script_analysis,
195 &x); 258 &x);
196 DCHECK(SUCCEEDED(hr)); 259 DCHECK(SUCCEEDED(hr));
197 return run->preceding_run_widths + x; 260 return run->preceding_run_widths + x;
198 } 261 }
199 262
263 struct LineSegmentWin : LineSegment {
264 const internal::TextRun* run;
265 };
266
267 // Internal class to help break a text into lines.
268 class LineBreaker {
269 public:
270 LineBreaker(int max_width, bool multiline, const BreakList<size_t>* words)
271 : max_width_(max_width),
272 multiline_(multiline),
273 words_(words),
274 preceding_line_heights_(0),
275 pos_(0),
276 text_x_(0),
277 line_x_(0),
278 current_pos_(0),
279 current_width_(0) {
280 SkipLine();
281 }
282
283 // Breaks the given |runs| into |lines|. Should be called at most once for
284 // each instance. If |multiline| is false, doesn't do any breaking and fills
285 // |lines| with a single Line.
286 void AddRun(const internal::TextRun& run) {
287 ResetPos(run.range.start());
288 int width = run.width;
289 if (multiline_ && line_x_ + width > max_width_)
290 width = BreakRun(run);
291 // Remaining part of the run fits the line, add it as well.
292 Advance(run.range.end(), width);
293 CommitSegment(run);
294 }
295
296 // Finishes line breaking and outputs the results. Can be called at most once.
297 void Finalize(std::vector<internal::Line>* lines) {
298 DCHECK(!lines_.empty());
299 lines->swap(lines_);
300 }
301
302 private:
303 // Breaks a run into segments of at most |max_width| width, adds the segments,
304 // returns the width of the final segment that fits the current line.
305 int BreakRun(const internal::TextRun& run) {
306 BreakRunResults results;
307 memset(&results, 0, sizeof(results));
308
309 // Break the run until it fits the current line.
310 while (BreakRunAtWidth(run, *words_, pos_, max_width_ - line_x_,
311 &results)) {
312 DCHECK(pos_ < run.range.end());
313 Advance(results.overflow_pos + 1, results.width);
314 // First, try rolling back one word to move it to the next line. If
315 // it's not possible, roll back one character. If neither of these
316 // could be done, don't do any rollback since each line must have at
317 // least one character.
318 if (line_x_ > 0 || results.word_rollback_width < results.width)
319 Rollback(results.word_rollback_pos, results.word_rollback_width);
320 else if (line_x_ > 0 || results.char_rollback_width < results.width)
321 Rollback(results.overflow_pos, results.char_rollback_width);
322 CommitSegment(run);
323 SkipLine();
324 }
325
326 return results.width;
327 }
328
329 // RTL runs are broken in logical order but displayed in visual order. To find
330 // the text-space coordinate (where it would fall in a single-line text)
331 // |x_pos| of RTL segments, segment widths are applied in reverse order.
332 // e.g. {[5, 10], [10, 40]} will become {[35, 40], [5, 35]}.
333 void PopRtl() {
334 if (rtl_segments_.empty())
335 return;
336 int x = rtl_segments_[0]->x_pos.start();
337 for (size_t i = rtl_segments_.size(); i > 0; --i) {
338 LineSegment* segment = rtl_segments_[i - 1];
339 segment->x_pos = ui::Range(x, x + segment->x_pos.length());
340 x += segment->x_pos.length();
341 }
342 rtl_segments_.clear();
343 }
344
345 void ResetPos(size_t new_pos) {
346 pos_ = new_pos;
347 current_pos_ = new_pos;
348 current_width_ = 0;
349 }
350
351 void Advance(size_t new_pos, int amount) {
352 current_pos_ = new_pos;
353 current_width_ += amount;
354 }
355
356 void Rollback(size_t new_pos, int amount) {
357 Advance(new_pos, -amount);
358 }
359
360 void SkipLine() {
361 if (!lines_.empty())
362 preceding_line_heights_ += lines_.back().height;
363 line_x_ = 0;
364 lines_.push_back(internal::Line());
365 }
366
367 void CommitSegment(const internal::TextRun& run) {
368 if (pos_ == current_pos_) {
369 DCHECK(current_width_ == 0);
370 return;
371 }
372 internal::LineSegmentWin* segment = new internal::LineSegmentWin;
373 segment->run = &run;
374 segment->char_pos = ui::Range(pos_, current_pos_);
375 segment->x_pos = ui::Range(text_x_, text_x_ + current_width_);
376 lines_.back().segments.push_back(segment);
377 lines_.back().width += segment->x_pos.length();
378 lines_.back().height = std::max(lines_.back().height,
379 segment->run->font.GetHeight());
380 lines_.back().baseline = std::max(lines_.back().baseline,
381 segment->run->font.GetBaseline());
382 lines_.back().preceding_heights = preceding_line_heights_;
383 if (run.script_analysis.fRTL) {
384 rtl_segments_.push_back(segment);
385 if (current_pos_ == run.range.end())
386 PopRtl();
387 }
388 pos_ = current_pos_;
389 text_x_ += current_width_;
390 line_x_ += current_width_;
391 current_width_ = 0;
392 }
393
394 int max_width_;
395 bool multiline_;
396 const BreakList<size_t>* words_;
397
398 // Stores the resulting lines.
399 std::vector<internal::Line> lines_;
400
401 // Position information of the last committed segment. |pos_| is the segment's
402 // end character position. |text_x_| is the text-space and |line_x| is the
Alexei Svitkine (slow) 2013/08/13 20:56:32 Nit: |line_x| -> |line_x_|
ckocagil 2013/08/14 11:40:25 Done.
403 // line-space x coordinate of |pos_|.
404 size_t pos_;
405 int text_x_;
406 int line_x_;
407 int preceding_line_heights_;
408
409 // The end character position and width of the current segment to be
410 // committed. The segment will have the character range [pos_, current_pos_)
411 // and width |current_width_|.
412 size_t current_pos_;
413 int current_width_;
414
415 // Segments to be applied by |PopRtl()|.
416 std::vector<LineSegment*> rtl_segments_;
417
418 DISALLOW_COPY_AND_ASSIGN(LineBreaker);
419 };
420
200 } // namespace internal 421 } // namespace internal
201 422
202 // static 423 // static
203 HDC RenderTextWin::cached_hdc_ = NULL; 424 HDC RenderTextWin::cached_hdc_ = NULL;
204 425
205 // static 426 // static
206 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_; 427 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_;
207 428
208 RenderTextWin::RenderTextWin() 429 RenderTextWin::RenderTextWin()
209 : RenderText(), 430 : RenderText(),
210 common_baseline_(0), 431 common_baseline_(0),
211 needs_layout_(false) { 432 needs_layout_(false) {
212 set_truncate_length(kMaxUniscribeTextLength); 433 set_truncate_length(kMaxUniscribeTextLength);
213 434
214 memset(&script_control_, 0, sizeof(script_control_)); 435 memset(&script_control_, 0, sizeof(script_control_));
215 memset(&script_state_, 0, sizeof(script_state_)); 436 memset(&script_state_, 0, sizeof(script_state_));
216 437
217 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT)); 438 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT));
218 } 439 }
219 440
220 RenderTextWin::~RenderTextWin() { 441 RenderTextWin::~RenderTextWin() {
221 } 442 }
222 443
223 Size RenderTextWin::GetStringSize() { 444 Size RenderTextWin::GetStringSize() {
224 EnsureLayout(); 445 EnsureLayout();
225 return string_size_; 446 return string_size_;
226 } 447 }
227 448
449 Size RenderTextWin::GetMultilineTextSize() {
450 EnsureLayout();
451 if (!multiline())
452 return Size(display_rect().width(), string_size_.height());
453 return Size(display_rect().width(),
454 lines().back().preceding_heights + lines().back().height);
455 }
456
228 int RenderTextWin::GetBaseline() { 457 int RenderTextWin::GetBaseline() {
229 EnsureLayout(); 458 EnsureLayout();
230 return common_baseline_; 459 return common_baseline_;
231 } 460 }
232 461
233 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { 462 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) {
234 if (text().empty()) 463 if (text().empty())
235 return SelectionModel(); 464 return SelectionModel();
236 465
237 EnsureLayout(); 466 EnsureLayout();
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 GetGlyphXBoundary(run, layout_index, true)); 605 GetGlyphXBoundary(run, layout_index, true));
377 } 606 }
378 607
379 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) { 608 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) {
380 DCHECK(!needs_layout_); 609 DCHECK(!needs_layout_);
381 DCHECK(ui::Range(0, text().length()).Contains(range)); 610 DCHECK(ui::Range(0, text().length()).Contains(range));
382 ui::Range layout_range(TextIndexToLayoutIndex(range.start()), 611 ui::Range layout_range(TextIndexToLayoutIndex(range.start()),
383 TextIndexToLayoutIndex(range.end())); 612 TextIndexToLayoutIndex(range.end()));
384 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range)); 613 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range));
385 614
386 std::vector<Rect> bounds; 615 std::vector<ui::Range> bounds;
616 std::vector<Rect> rects;
387 if (layout_range.is_empty()) 617 if (layout_range.is_empty())
388 return bounds; 618 return rects;
389 619
390 // Add a Rect for each run/selection intersection. 620 // Add a Range for each run/selection intersection.
391 // TODO(msw): The bounds should probably not always be leading the range ends. 621 // TODO(msw): The bounds should probably not always be leading the range ends.
392 for (size_t i = 0; i < runs_.size(); ++i) { 622 for (size_t i = 0; i < runs_.size(); ++i) {
393 const internal::TextRun* run = runs_[visual_to_logical_[i]]; 623 const internal::TextRun* run = runs_[visual_to_logical_[i]];
394 ui::Range intersection = run->range.Intersect(layout_range); 624 ui::Range intersection = run->range.Intersect(layout_range);
395 if (intersection.IsValid()) { 625 if (intersection.IsValid()) {
396 DCHECK(!intersection.is_reversed()); 626 DCHECK(!intersection.is_reversed());
397 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false), 627 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false),
398 GetGlyphXBoundary(run, intersection.end(), false)); 628 GetGlyphXBoundary(run, intersection.end(), false));
399 Rect rect(range_x.GetMin(), 0, range_x.length(), run->font.GetHeight()); 629 if (range_x.is_empty())
400 rect.set_origin(ToViewPoint(rect.origin())); 630 continue;
401 // Union this with the last rect if they're adjacent. 631 range_x = ui::Range(range_x.GetMin(), range_x.GetMax());
402 if (!bounds.empty() && rect.SharesEdgeWith(bounds.back())) { 632 // Union this with the last range if they're adjacent.
403 rect.Union(bounds.back()); 633 DCHECK(bounds.empty() || bounds.back().GetMin() != range_x.GetMax());
634 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) {
635 range_x = ui::Range(bounds.back().GetMin(), range_x.GetMax());
404 bounds.pop_back(); 636 bounds.pop_back();
405 } 637 }
406 bounds.push_back(rect); 638 bounds.push_back(range_x);
407 } 639 }
408 } 640 }
409 return bounds; 641 for (size_t i = 0; i < bounds.size(); ++i) {
642 std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]);
643 rects.insert(rects.end(), current_rects.begin(), current_rects.end());
644 }
645 return rects;
410 } 646 }
411 647
412 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const { 648 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const {
413 DCHECK_LE(index, text().length()); 649 DCHECK_LE(index, text().length());
414 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index; 650 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index;
415 CHECK_GE(i, 0); 651 CHECK_GE(i, 0);
416 // Clamp layout indices to the length of the text actually used for layout. 652 // Clamp layout indices to the length of the text actually used for layout.
417 return std::min<size_t>(GetLayoutText().length(), i); 653 return std::min<size_t>(GetLayoutText().length(), i);
418 } 654 }
419 655
(...skipping 21 matching lines...) Expand all
441 position < LayoutIndexToTextIndex(GetLayoutText().length()) && 677 position < LayoutIndexToTextIndex(GetLayoutText().length()) &&
442 GetGlyphBounds(position) != GetGlyphBounds(position - 1); 678 GetGlyphBounds(position) != GetGlyphBounds(position - 1);
443 } 679 }
444 680
445 void RenderTextWin::ResetLayout() { 681 void RenderTextWin::ResetLayout() {
446 // Layout is performed lazily as needed for drawing/metrics. 682 // Layout is performed lazily as needed for drawing/metrics.
447 needs_layout_ = true; 683 needs_layout_ = true;
448 } 684 }
449 685
450 void RenderTextWin::EnsureLayout() { 686 void RenderTextWin::EnsureLayout() {
451 if (!needs_layout_) 687 if (needs_layout_) {
452 return; 688 // TODO(msw): Skip complex processing if ScriptIsComplex returns false.
453 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. 689 ItemizeLogicalText();
454 ItemizeLogicalText(); 690 if (!runs_.empty())
455 if (!runs_.empty()) 691 LayoutVisualText();
456 LayoutVisualText(); 692 needs_layout_ = false;
457 needs_layout_ = false; 693 std::vector<internal::Line> lines;
694 set_lines(&lines);
695 }
696 // Compute lines if they're not valid. This is separate from the layout steps
697 // above to avoid text layout and shaping when we resize |display_rect_|.
698 if (lines().empty())
699 ComputeLines();
700 }
701
702 void RenderTextWin::ComputeLines() {
703 DCHECK(!needs_layout_);
704 std::vector<internal::Line> lines;
705 internal::LineBreaker line_breaker(display_rect().width(), multiline(),
706 multiline() ? &GetLineBreaks() : 0);
707 for (size_t i = 0; i < runs_.size(); ++i)
708 line_breaker.AddRun(*runs_[visual_to_logical_[i]]);
709 line_breaker.Finalize(&lines);
710 DCHECK(!lines.empty());
711 set_lines(&lines);
458 } 712 }
459 713
460 void RenderTextWin::DrawVisualText(Canvas* canvas) { 714 void RenderTextWin::DrawVisualText(Canvas* canvas) {
461 DCHECK(!needs_layout_); 715 DCHECK(!needs_layout_);
462 716 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 717
469 std::vector<SkPoint> pos; 718 std::vector<SkPoint> pos;
470 719
471 internal::SkiaTextRenderer renderer(canvas); 720 internal::SkiaTextRenderer renderer(canvas);
472 ApplyFadeEffects(&renderer); 721 ApplyFadeEffects(&renderer);
473 ApplyTextShadows(&renderer); 722 ApplyTextShadows(&renderer);
474 723
475 bool smoothing_enabled; 724 bool smoothing_enabled;
476 bool cleartype_enabled; 725 bool cleartype_enabled;
477 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); 726 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled);
478 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. 727 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|.
479 renderer.SetFontSmoothingSettings( 728 renderer.SetFontSmoothingSettings(
480 smoothing_enabled, cleartype_enabled && !background_is_transparent()); 729 smoothing_enabled, cleartype_enabled && !background_is_transparent());
481 730
482 ApplyCompositionAndSelectionStyles(); 731 ApplyCompositionAndSelectionStyles();
483 732
484 for (size_t i = 0; i < runs_.size(); ++i) { 733 for (size_t i = 0; i < lines().size(); ++i) {
485 // Get the run specified by the visual-to-logical map. 734 const internal::Line& line = lines()[i];
486 internal::TextRun* run = runs_[visual_to_logical_[i]]; 735 Vector2d line_offset = GetLineOffset(i);
736 Vector2d text_offset = line_offset + Vector2d(0, line.baseline);
737 int preceding_segment_widths = 0;
487 738
488 // Skip painting empty runs and runs outside the display rect area. 739 // Skip painting empty lines or lines outside the display rect area.
489 if ((run->glyph_count == 0) || (x >= display_rect().right()) || 740 if (!display_rect().Intersects(Rect(PointAtOffsetFromOrigin(line_offset),
490 (x + run->width <= display_rect().x())) { 741 Size(line.width, line.height))))
491 x += run->width;
492 continue; 742 continue;
743
744 for (size_t j = 0; j < line.segments.size(); ++j) {
745 const internal::LineSegmentWin* segment =
746 static_cast<internal::LineSegmentWin*>(line.segments[j]);
747 const int segment_width = segment->x_pos.length();
748 const internal::TextRun* run = segment->run;
749 DCHECK(!segment->char_pos.is_empty());
750 DCHECK(run->range.Contains(segment->char_pos));
751 ui::Range glyphs = CharRangeToGlyphRange(*run, segment->char_pos);
752 if (glyphs.is_empty()) {
753 DCHECK(segment_width == 0);
754 continue;
755 }
756 // Skip painting segments outside the display rect area.
757 Rect segment_bounds(PointAtOffsetFromOrigin(line_offset) +
758 Vector2d(preceding_segment_widths, 0),
759 Size(segment_width, line.height));
760 if (!display_rect().Intersects(segment_bounds)) {
761 preceding_segment_widths += segment_width;
762 continue;
763 }
764
765 int segment_x = 0;
766 pos.resize(glyphs.length());
767 for (size_t g = glyphs.start(); g < glyphs.end(); ++g) {
768 pos[g - glyphs.start()].set(
769 SkIntToScalar(text_offset.x() + preceding_segment_widths +
770 segment_x + run->offsets[g].du),
771 SkIntToScalar(text_offset.y() + run->offsets[g].dv));
772 segment_x += run->advance_widths[g];
773 }
774
775 renderer.SetTextSize(run->font.GetFontSize());
776 renderer.SetFontFamilyWithStyle(run->font.GetFontName(), run->font_style);
777
778 for (BreakList<SkColor>::const_iterator it =
779 colors().GetBreak(run->range.start());
780 it != colors().breaks().end() && it->first < run->range.end();
781 ++it) {
782 ui::Range intersection = colors().GetRange(it).Intersect(
783 segment->char_pos);
784 ui::Range colored_glyphs = CharRangeToGlyphRange(*run, intersection);
785 DCHECK(glyphs.Contains(colored_glyphs));
786 if (colored_glyphs.is_empty())
787 continue;
788 renderer.SetForegroundColor(it->second);
789 renderer.DrawPosText(&pos[colored_glyphs.start() - glyphs.start()],
790 &run->glyphs[colored_glyphs.start()],
791 colored_glyphs.length());
792 SkScalar width = (colored_glyphs.end() < glyphs.end()
793 ? pos[colored_glyphs.end() - glyphs.start()].x()
794 : pos[0].x() + SkIntToScalar(segment_width))
795 - pos[colored_glyphs.start() - glyphs.start()].x();
796 renderer.DrawDecorations(
797 pos[colored_glyphs.start() - glyphs.start()].x(), text_offset.y(),
798 SkScalarCeilToInt(width), run->underline, run->strike,
799 run->diagonal_strike);
800 }
801
802 preceding_segment_widths += segment_width;
493 } 803 }
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 } 804 }
531 805
532 UndoCompositionAndSelectionStyles(); 806 UndoCompositionAndSelectionStyles();
533 } 807 }
534 808
535 void RenderTextWin::ItemizeLogicalText() { 809 void RenderTextWin::ItemizeLogicalText() {
536 runs_.clear(); 810 runs_.clear();
537 // Make |string_size_|'s height and |common_baseline_| tall enough to draw 811 // 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. 812 // often-used characters which are rendered with fonts in the font list.
539 string_size_ = Size(0, font_list().GetHeight()); 813 string_size_ = Size(0, font_list().GetHeight());
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 size_t position = LayoutIndexToTextIndex(run->range.end()); 1171 size_t position = LayoutIndexToTextIndex(run->range.end());
898 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); 1172 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD);
899 return SelectionModel(position, CURSOR_FORWARD); 1173 return SelectionModel(position, CURSOR_FORWARD);
900 } 1174 }
901 1175
902 RenderText* RenderText::CreateInstance() { 1176 RenderText* RenderText::CreateInstance() {
903 return new RenderTextWin; 1177 return new RenderTextWin;
904 } 1178 }
905 1179
906 } // namespace gfx 1180 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text_win.h ('k') | ui/views/examples/multiline_example.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698