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

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: rebased; lazy line break calculation; refactored LineBreaker 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
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 // Starting from |start_char|, finds the first character that doesn't fit the
158 // given |width_cap|. If |width_cap| is reached, returns true and fills the
159 // output arguments with overflow and rollback info. Otherwise returns false and
160 // only fills |width|.
161 // |overflow|: Index of the first character that doesn't fit the given width.
162 // |width|: If the run doesn't fit, holds the width of [start_char, *overflow].
163 // Otherwise, holds the width of [start_char, run.range.end()).
164 // |char_rollback_width|: Width of |*overflow|.
165 // |word_rollback_pos|: Index of the last word break before |*overflow|.
166 // |word_rollback_width|: Width of [*word_rollback_pos, *overflow].
167 bool BreakRunAtWidth(const internal::TextRun& run,
168 const BreakList<size_t>& breaks, size_t start_char,
Alexei Svitkine (slow) 2013/08/08 15:01:03 One parameter per line in the declaration, please.
ckocagil 2013/08/10 13:39:23 Done.
169 int width_cap, int* width, size_t* overflow,
170 int* char_rollback_width, size_t* word_rollback_pos,
171 int* word_rollback_width) {
Alexei Svitkine (slow) 2013/08/08 15:01:03 This is quite a lot of parameters to pass to this
ckocagil 2013/08/10 13:39:23 There isn't a lot of state tracking and the functi
172 DCHECK(run.range.Contains(ui::Range(start_char)));
173 size_t current_word = start_char;
174 int current_word_x = 0; // x from |current_word|
175 int x = 0;
176
177 for (size_t i = start_char; i < run.range.end(); ++i) {
178 size_t next_word = std::max(breaks.GetBreak(i)->first, start_char);
179 if (next_word != current_word) {
180 current_word = next_word;
181 current_word_x = 0;
182 }
183
184 ui::Range glyphs = CharRangeToGlyphRange(run, ui::Range(i, i + 1));
185 int char_width = 0;
186 for (size_t j = glyphs.start(); j < glyphs.end(); ++j)
187 char_width += run.advance_widths[j];
188
189 x += char_width;
190 current_word_x += char_width;
191
192 if (x > width_cap) {
193 *overflow = i;
194 *width = x;
195 *char_rollback_width = char_width;
196 *word_rollback_pos = current_word;
197 *word_rollback_width = current_word_x;
198 return true;
199 }
200 }
201
202 *width = x;
203 return false;
204 }
205
157 } // namespace 206 } // namespace
158 207
159 namespace internal { 208 namespace internal {
160 209
161 TextRun::TextRun() 210 TextRun::TextRun()
162 : font_style(0), 211 : font_style(0),
163 strike(false), 212 strike(false),
164 diagonal_strike(false), 213 diagonal_strike(false),
165 underline(false), 214 underline(false),
166 width(0), 215 width(0),
(...skipping 23 matching lines...) Expand all
190 run->glyph_count, 239 run->glyph_count,
191 run->logical_clusters.get(), 240 run->logical_clusters.get(),
192 run->visible_attributes.get(), 241 run->visible_attributes.get(),
193 run->advance_widths.get(), 242 run->advance_widths.get(),
194 &run->script_analysis, 243 &run->script_analysis,
195 &x); 244 &x);
196 DCHECK(SUCCEEDED(hr)); 245 DCHECK(SUCCEEDED(hr));
197 return run->preceding_run_widths + x; 246 return run->preceding_run_widths + x;
198 } 247 }
199 248
249 struct LineSegmentWin : LineSegment {
250 internal::TextRun* run;
251 };
252
253 // Internal class to help break a text into lines.
254 class LineBreaker {
255 public:
256 LineBreaker() : preceding_line_heights_(0),
257 pos_(0),
258 text_x_(0),
259 line_x_(0),
260 current_pos_(0),
261 current_delta_(0) {}
262
263 // Breaks the given |runs| into |lines|. Should be called at most once for
264 // each instance. If |multiline| is false, doesn't do any breaking and fills
265 // |lines| with a single Line.
266 void Break(const ScopedVector<internal::TextRun>& runs,
267 const BreakList<size_t>& words,
268 int max_width,
269 bool multiline,
270 int* visual_to_logical,
271 std::vector<internal::Line>* lines) {
Alexei Svitkine (slow) 2013/08/08 15:01:03 Instead of passing all of these params to a single
ckocagil 2013/08/10 13:39:23 Done.
272 SkipLine();
273
274 for (size_t i = 0; i < runs.size(); ++i) {
275 internal::TextRun* run = runs[visual_to_logical[i]];
276 ResetPos(run->range.start());
277 int width = multiline ? BreakRun(run, words, max_width) : run->width;
278 // Remaining part of the run fits the line, add it as well.
279 Advance(run->range.end(), width);
280 CommitSegment(run);
281 }
282
283 lines->swap(lines_);
284 }
285
286 private:
287 // RTL runs are broken in logical order but displayed in visual order. To find
288 // the text-space coordinate (where it would fall in a single-line text)
289 // |x_pos| of RTL segments, text-space coordinate of the run beginning is
290 // saved and the segment widths are applied in reverse order.
291 // e.g. {[5, 10], [10, 40]} will become {[35, 40], [5, 35]}.
292 struct RtlData {
293 int run_x_; // Beginning of the RTL run in text-space.
294 std::vector<LineSegment*> segments_;
295 };
296
297 // Breaks a run into segments of at most |max_width| width, adds the segments,
298 // returns the width of the final segment that fits the current line.
299 int BreakRun(internal::TextRun* run,
300 const BreakList<size_t>& words,
301 int max_width) {
302 int width = run->width;
303 size_t overflow;
304 int char_rollback_width;
305 size_t word_rollback_pos;
306 int word_rollback_width;
307 // Break the run until it fits the current line.
308 while (BreakRunAtWidth(*run, words, pos_, max_width - line_x_,
309 &width, &overflow, &char_rollback_width,
310 &word_rollback_pos, &word_rollback_width)) {
311 DCHECK(pos_ < run->range.end());
312 Advance(overflow + 1, width);
313 // First, try rolling back one word to move it to the next line. If
314 // it's not possible, roll back one character. If neither of these
315 // could be done, don't do any rollback since each line must have at
316 // least one character.
317 if (line_x_ > 0 || word_rollback_width < width)
318 Rollback(word_rollback_pos, word_rollback_width);
319 else if (line_x_ > 0 || char_rollback_width < width)
320 Rollback(overflow, char_rollback_width);
321 CommitSegment(run);
322 SkipLine();
323 }
324
325 return width;
326 }
327
328 // Applies |rtl_data_|.
329 void PopRtl() {
330 int x = rtl_data_.run_x_;
331 while (!rtl_data_.segments_.empty()) {
332 LineSegment* segment = rtl_data_.segments_.back();
333 rtl_data_.segments_.pop_back();
334 segment->x_pos = ui::Range(x, x + segment->x_pos.length());
335 x += segment->x_pos.length();
336 }
337 }
338
339 void ResetPos(size_t new_pos) {
340 pos_ = new_pos;
341 current_pos_ = new_pos;
342 current_delta_ = 0;
343 }
344
345 void Advance(size_t new_pos, int amount) {
346 current_pos_ = new_pos;
347 current_delta_ += amount;
348 }
349
350 void Rollback(size_t new_pos, int amount) {
351 Advance(new_pos, -amount);
352 }
353
354 void SkipLine() {
355 if (!lines_.empty())
356 preceding_line_heights_ += lines_.back().height;
357 line_x_ = 0;
358 lines_.push_back(internal::Line());
359 }
360
361 void CommitSegment(internal::TextRun* run) {
362 if (pos_ == current_pos_) {
363 DCHECK(current_delta_ == 0);
364 return;
365 }
366 internal::LineSegmentWin* segment = new internal::LineSegmentWin;
367 segment->run = run;
368 segment->char_pos = ui::Range(pos_, current_pos_);
369 segment->x_pos = ui::Range(text_x_, text_x_ + current_delta_);
370 lines_.back().segments.push_back(segment);
371 lines_.back().width += segment->x_pos.length();
372 lines_.back().height = std::max(lines_.back().height,
373 segment->run->font.GetHeight());
374 lines_.back().baseline = std::max(lines_.back().baseline,
375 segment->run->font.GetBaseline());
376 lines_.back().preceding_heights = preceding_line_heights_;
377 if (run->script_analysis.fRTL) {
378 if (pos_ == run->range.start())
379 rtl_data_.run_x_ = text_x_;
380 rtl_data_.segments_.push_back(segment);
381 if (current_pos_ == run->range.end())
382 PopRtl();
383 }
384 pos_ = current_pos_;
385 text_x_ += current_delta_;
386 line_x_ += current_delta_;
387 current_delta_ = 0;
388 }
389
390 std::vector<internal::Line> lines_;
391 int preceding_line_heights_;
392 size_t pos_;
393 int text_x_;
394 int line_x_;
395 size_t current_pos_;
396 int current_delta_;
397 RtlData rtl_data_;
398
399 DISALLOW_COPY_AND_ASSIGN(LineBreaker);
400 };
401
200 } // namespace internal 402 } // namespace internal
201 403
202 // static 404 // static
203 HDC RenderTextWin::cached_hdc_ = NULL; 405 HDC RenderTextWin::cached_hdc_ = NULL;
204 406
205 // static 407 // static
206 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_; 408 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_;
207 409
208 RenderTextWin::RenderTextWin() 410 RenderTextWin::RenderTextWin()
209 : RenderText(), 411 : RenderText(),
210 common_baseline_(0), 412 common_baseline_(0),
211 needs_layout_(false) { 413 needs_layout_(false) {
212 set_truncate_length(kMaxUniscribeTextLength); 414 set_truncate_length(kMaxUniscribeTextLength);
213 415
214 memset(&script_control_, 0, sizeof(script_control_)); 416 memset(&script_control_, 0, sizeof(script_control_));
215 memset(&script_state_, 0, sizeof(script_state_)); 417 memset(&script_state_, 0, sizeof(script_state_));
216 418
217 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT)); 419 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT));
218 } 420 }
219 421
220 RenderTextWin::~RenderTextWin() { 422 RenderTextWin::~RenderTextWin() {
221 } 423 }
222 424
223 Size RenderTextWin::GetStringSize() { 425 Size RenderTextWin::GetStringSize() {
224 EnsureLayout(); 426 EnsureLayout();
225 return string_size_; 427 return Size(string_size_.width(), string_size_.height());
Alexei Svitkine (slow) 2013/08/08 15:01:03 This is equivalent to return string_size_;, no?
ckocagil 2013/08/10 13:39:23 Oops, leftover from before. Done.
428 }
429
430 Size RenderTextWin::GetMultilineTextSize() {
Alexei Svitkine (slow) 2013/08/08 15:01:03 I think you should make GetStringSize() return the
ckocagil 2013/08/10 13:39:23 But if GetStringSize returns the display width and
Alexei Svitkine (slow) 2013/08/27 15:40:38 Re-visiting this discussion (I didn't follow-up on
ckocagil 2013/08/28 19:16:26 I said "_if_ GetStringSize returns the display wid
Alexei Svitkine (slow) 2013/08/28 21:27:46 I think for GetHeightForWidth() with different wid
msw 2013/08/30 00:42:28 I think the uses of View's GetPreferredSize and Ge
ckocagil 2013/08/30 16:53:36 Alexei's advice on change multiline mode to get si
Alexei Svitkine (slow) 2013/08/30 17:07:25 I think the current GetCursorBounds() logic is inc
431 EnsureLayout();
432 if (!multiline())
433 return Size(display_rect().width(), string_size_.height());
434 return Size(display_rect().width(),
435 lines().back().preceding_heights + lines().back().height);
226 } 436 }
227 437
228 int RenderTextWin::GetBaseline() { 438 int RenderTextWin::GetBaseline() {
229 EnsureLayout(); 439 EnsureLayout();
230 return common_baseline_; 440 return common_baseline_;
231 } 441 }
232 442
233 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { 443 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) {
234 if (text().empty()) 444 if (text().empty())
235 return SelectionModel(); 445 return SelectionModel();
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 GetGlyphXBoundary(run, layout_index, true)); 586 GetGlyphXBoundary(run, layout_index, true));
377 } 587 }
378 588
379 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) { 589 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) {
380 DCHECK(!needs_layout_); 590 DCHECK(!needs_layout_);
381 DCHECK(ui::Range(0, text().length()).Contains(range)); 591 DCHECK(ui::Range(0, text().length()).Contains(range));
382 ui::Range layout_range(TextIndexToLayoutIndex(range.start()), 592 ui::Range layout_range(TextIndexToLayoutIndex(range.start()),
383 TextIndexToLayoutIndex(range.end())); 593 TextIndexToLayoutIndex(range.end()));
384 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range)); 594 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range));
385 595
386 std::vector<Rect> bounds; 596 std::vector<ui::Range> bounds;
597 std::vector<Rect> rects;
387 if (layout_range.is_empty()) 598 if (layout_range.is_empty())
388 return bounds; 599 return rects;
389 600
390 // Add a Rect for each run/selection intersection. 601 // Add a Range for each run/selection intersection.
391 // TODO(msw): The bounds should probably not always be leading the range ends. 602 // TODO(msw): The bounds should probably not always be leading the range ends.
392 for (size_t i = 0; i < runs_.size(); ++i) { 603 for (size_t i = 0; i < runs_.size(); ++i) {
393 const internal::TextRun* run = runs_[visual_to_logical_[i]]; 604 const internal::TextRun* run = runs_[visual_to_logical_[i]];
394 ui::Range intersection = run->range.Intersect(layout_range); 605 ui::Range intersection = run->range.Intersect(layout_range);
395 if (intersection.IsValid()) { 606 if (intersection.IsValid()) {
396 DCHECK(!intersection.is_reversed()); 607 DCHECK(!intersection.is_reversed());
397 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false), 608 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false),
398 GetGlyphXBoundary(run, intersection.end(), false)); 609 GetGlyphXBoundary(run, intersection.end(), false));
399 Rect rect(range_x.GetMin(), 0, range_x.length(), run->font.GetHeight()); 610 if (range_x.is_empty())
400 rect.set_origin(ToViewPoint(rect.origin())); 611 continue;
401 // Union this with the last rect if they're adjacent. 612 range_x = ui::Range(range_x.GetMin(), range_x.GetMax());
402 if (!bounds.empty() && rect.SharesEdgeWith(bounds.back())) { 613 // Union this with the last range if they're adjacent.
403 rect.Union(bounds.back()); 614 DCHECK(bounds.empty() || bounds.back().GetMin() != range_x.GetMax());
615 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) {
616 range_x = ui::Range(bounds.back().GetMin(), range_x.GetMax());
404 bounds.pop_back(); 617 bounds.pop_back();
405 } 618 }
406 bounds.push_back(rect); 619 bounds.push_back(range_x);
407 } 620 }
408 } 621 }
409 return bounds; 622 for (size_t i = 0; i < bounds.size(); ++i) {
623 std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]);
624 rects.insert(rects.end(), current_rects.begin(), current_rects.end());
625 }
626 return rects;
410 } 627 }
411 628
412 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const { 629 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const {
413 DCHECK_LE(index, text().length()); 630 DCHECK_LE(index, text().length());
414 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index; 631 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index;
415 CHECK_GE(i, 0); 632 CHECK_GE(i, 0);
416 // Clamp layout indices to the length of the text actually used for layout. 633 // Clamp layout indices to the length of the text actually used for layout.
417 return std::min<size_t>(GetLayoutText().length(), i); 634 return std::min<size_t>(GetLayoutText().length(), i);
418 } 635 }
419 636
(...skipping 21 matching lines...) Expand all
441 position < LayoutIndexToTextIndex(GetLayoutText().length()) && 658 position < LayoutIndexToTextIndex(GetLayoutText().length()) &&
442 GetGlyphBounds(position) != GetGlyphBounds(position - 1); 659 GetGlyphBounds(position) != GetGlyphBounds(position - 1);
443 } 660 }
444 661
445 void RenderTextWin::ResetLayout() { 662 void RenderTextWin::ResetLayout() {
446 // Layout is performed lazily as needed for drawing/metrics. 663 // Layout is performed lazily as needed for drawing/metrics.
447 needs_layout_ = true; 664 needs_layout_ = true;
448 } 665 }
449 666
450 void RenderTextWin::EnsureLayout() { 667 void RenderTextWin::EnsureLayout() {
451 if (!needs_layout_) 668 if (needs_layout_) {
452 return; 669 // TODO(msw): Skip complex processing if ScriptIsComplex returns false.
453 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. 670 ItemizeLogicalText();
454 ItemizeLogicalText(); 671 if (!runs_.empty())
455 if (!runs_.empty()) 672 LayoutVisualText();
456 LayoutVisualText(); 673 needs_layout_ = false;
457 needs_layout_ = false; 674 std::vector<internal::Line> lines;
675 set_lines(&lines);
676 }
677 // Compute lines if they're not valid. This is separate from the layout steps
678 // above to avoid text layout and shaping when we resize |display_rect_|.
679 if (lines().empty())
680 ComputeLines();
681 }
682
683 void RenderTextWin::ComputeLines() {
684 DCHECK(!needs_layout_);
685 std::vector<internal::Line> lines;
686 internal::LineBreaker line_breaker;
687 line_breaker.Break(runs_, multiline() ? GetLineBreaks() : BreakList<size_t>(),
688 display_rect().width(), multiline(),
689 visual_to_logical_.get(), &lines);
690 set_lines(&lines);
Alexei Svitkine (slow) 2013/08/08 15:01:03 Can you add a DCHECK() that !lines().empty()? (Els
ckocagil 2013/08/10 13:39:23 Done.
458 } 691 }
459 692
460 void RenderTextWin::DrawVisualText(Canvas* canvas) { 693 void RenderTextWin::DrawVisualText(Canvas* canvas) {
461 DCHECK(!needs_layout_); 694 DCHECK(!needs_layout_);
462 695 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 696
469 std::vector<SkPoint> pos; 697 std::vector<SkPoint> pos;
470 698
471 internal::SkiaTextRenderer renderer(canvas); 699 internal::SkiaTextRenderer renderer(canvas);
472 ApplyFadeEffects(&renderer); 700 ApplyFadeEffects(&renderer);
473 ApplyTextShadows(&renderer); 701 ApplyTextShadows(&renderer);
474 702
475 bool smoothing_enabled; 703 bool smoothing_enabled;
476 bool cleartype_enabled; 704 bool cleartype_enabled;
477 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); 705 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled);
478 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. 706 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|.
479 renderer.SetFontSmoothingSettings( 707 renderer.SetFontSmoothingSettings(
480 smoothing_enabled, cleartype_enabled && !background_is_transparent()); 708 smoothing_enabled, cleartype_enabled && !background_is_transparent());
481 709
482 ApplyCompositionAndSelectionStyles(); 710 ApplyCompositionAndSelectionStyles();
483 711
484 for (size_t i = 0; i < runs_.size(); ++i) { 712 for (size_t i = 0; i < lines().size(); ++i) {
485 // Get the run specified by the visual-to-logical map. 713 const internal::Line& line = lines()[i];
486 internal::TextRun* run = runs_[visual_to_logical_[i]]; 714 Vector2d line_offset = GetLineOffset(i);
715 Vector2d text_offset = line_offset + Vector2d(0, line.baseline);
716 int preceding_segment_widths = 0;
487 717
488 // Skip painting empty runs and runs outside the display rect area. 718 // Skip painting empty lines or lines outside the display rect area.
489 if ((run->glyph_count == 0) || (x >= display_rect().right()) || 719 if (!display_rect().Intersects(Rect(PointAtOffsetFromOrigin(line_offset),
490 (x + run->width <= display_rect().x())) { 720 Size(line.width, line.height))))
491 x += run->width;
492 continue; 721 continue;
722
723 for (size_t j = 0; j < line.segments.size(); ++j) {
724 const internal::LineSegmentWin* segment =
725 static_cast<internal::LineSegmentWin*>(line.segments[j]);
726 const int segment_width = segment->x_pos.length();
727 internal::TextRun* run = segment->run;
728 DCHECK(!segment->char_pos.is_empty());
729 DCHECK(run->range.Contains(segment->char_pos));
730 ui::Range glyphs = CharRangeToGlyphRange(*run, segment->char_pos);
731 if (glyphs.is_empty()) {
732 DCHECK(segment_width == 0);
733 continue;
734 }
735 // Skip painting segments outside the display rect area.
736 Rect segment_bounds(PointAtOffsetFromOrigin(line_offset) +
737 Vector2d(preceding_segment_widths, 0),
738 Size(segment_width, line.height));
739 if (!display_rect().Intersects(segment_bounds)) {
740 preceding_segment_widths += segment_width;
741 continue;
742 }
743
744 int segment_x = 0;
745 pos.resize(glyphs.length());
746 for (size_t g = glyphs.start(); g < glyphs.end(); ++g) {
747 pos[g - glyphs.start()].set(
748 SkIntToScalar(text_offset.x() + preceding_segment_widths +
749 segment_x + run->offsets[g].du),
750 SkIntToScalar(text_offset.y() + run->offsets[g].dv));
751 segment_x += run->advance_widths[g];
752 }
753
754 renderer.SetTextSize(run->font.GetFontSize());
755 renderer.SetFontFamilyWithStyle(run->font.GetFontName(), run->font_style);
756
757 for (BreakList<SkColor>::const_iterator it =
758 colors().GetBreak(run->range.start());
759 it != colors().breaks().end() && it->first < run->range.end();
760 ++it) {
761 ui::Range intersection = colors().GetRange(it).Intersect(
762 segment->char_pos);
763 ui::Range colored_glyphs = CharRangeToGlyphRange(*run, intersection);
764 DCHECK(glyphs.Contains(colored_glyphs));
765 if (colored_glyphs.is_empty())
766 continue;
767 renderer.SetForegroundColor(it->second);
768 renderer.DrawPosText(&pos[colored_glyphs.start() - glyphs.start()],
769 &run->glyphs[colored_glyphs.start()],
770 colored_glyphs.length());
771 SkScalar width = (colored_glyphs.end() < glyphs.end()
772 ? pos[colored_glyphs.end() - glyphs.start()].x()
773 : pos[0].x() + SkIntToScalar(segment_width))
774 - pos[colored_glyphs.start() - glyphs.start()].x();
775 renderer.DrawDecorations(
776 pos[colored_glyphs.start() - glyphs.start()].x(), text_offset.y(),
777 SkScalarCeilToInt(width), run->underline, run->strike,
778 run->diagonal_strike);
779 }
780
781 preceding_segment_widths += segment_width;
493 } 782 }
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 } 783 }
531 784
532 UndoCompositionAndSelectionStyles(); 785 UndoCompositionAndSelectionStyles();
533 } 786 }
534 787
535 void RenderTextWin::ItemizeLogicalText() { 788 void RenderTextWin::ItemizeLogicalText() {
536 runs_.clear(); 789 runs_.clear();
537 // Make |string_size_|'s height and |common_baseline_| tall enough to draw 790 // 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. 791 // often-used characters which are rendered with fonts in the font list.
539 string_size_ = Size(0, font_list().GetHeight()); 792 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()); 1150 size_t position = LayoutIndexToTextIndex(run->range.end());
898 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); 1151 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD);
899 return SelectionModel(position, CURSOR_FORWARD); 1152 return SelectionModel(position, CURSOR_FORWARD);
900 } 1153 }
901 1154
902 RenderText* RenderText::CreateInstance() { 1155 RenderText* RenderText::CreateInstance() {
903 return new RenderTextWin; 1156 return new RenderTextWin;
904 } 1157 }
905 1158
906 } // namespace gfx 1159 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698