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

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: Mike's comments 2 Created 7 years, 3 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 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 |*pos| will be
160 // 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* pos) {
Alexei Svitkine (slow) 2013/08/26 21:34:41 Nit: Rename |pos| to |next_char|, or better yet |n
ckocagil 2013/08/28 18:01:45 Done. I made it |next_char| instead of |next_char_
msw 2013/08/30 00:42:28 |end_char| corresponds w/|start_char| better, but
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 if (next_word != breaks.breaks().end() && i >= next_word->first) {
Alexei Svitkine (slow) 2013/08/27 15:40:38 Add a comment above this explaining the logic.
ckocagil 2013/08/28 19:16:26 Done.
182 word = next_word++;
183 word_width = 0;
184 }
185
186 ui::Range glyphs = CharRangeToGlyphRange(run, ui::Range(i, i + 1));
Alexei Svitkine (slow) 2013/08/27 15:40:38 Nit: glyph_range
ckocagil 2013/08/28 19:16:26 Done.
187 int char_width = 0;
188 for (size_t j = glyphs.start(); j < glyphs.end(); ++j)
189 char_width += run.advance_widths[j];
190
191 *width += char_width;
192 word_width += char_width;
193
194 if (*width > available_width) {
195 if (!empty_line || word_width < *width) {
196 *width -= word_width;
197 *pos = std::max(word->first, start_char);
198 } else if (char_width < *width) {
199 *width -= char_width;
200 *pos = i;
201 } else {
202 *pos = i + 1;
203 }
204
205 return;
206 }
207 }
208
209 *pos = run.range.end();
210 }
211
157 } // namespace 212 } // namespace
158 213
159 namespace internal { 214 namespace internal {
160 215
161 TextRun::TextRun() 216 TextRun::TextRun()
162 : font_style(0), 217 : font_style(0),
163 strike(false), 218 strike(false),
164 diagonal_strike(false), 219 diagonal_strike(false),
165 underline(false), 220 underline(false),
166 width(0), 221 width(0),
(...skipping 23 matching lines...) Expand all
190 run->glyph_count, 245 run->glyph_count,
191 run->logical_clusters.get(), 246 run->logical_clusters.get(),
192 run->visible_attributes.get(), 247 run->visible_attributes.get(),
193 run->advance_widths.get(), 248 run->advance_widths.get(),
194 &run->script_analysis, 249 &run->script_analysis,
195 &x); 250 &x);
196 DCHECK(SUCCEEDED(hr)); 251 DCHECK(SUCCEEDED(hr));
197 return run->preceding_run_widths + x; 252 return run->preceding_run_widths + x;
198 } 253 }
199 254
255 // Internal class to generate Line structures. If |multiline| is true, the text
256 // is broken into lines at |words| boundaries such that each line is no longer
257 // than |max_width|. If |multiline| is false, only outputs a single Line from
258 // the given runs.
Alexei Svitkine (slow) 2013/08/27 15:40:38 Nit: "the given run." -> "a given run."
ckocagil 2013/08/28 19:16:26 I don't understand. A Line can contain multiple ru
Alexei Svitkine (slow) 2013/08/28 21:27:46 Ah right - my comment was wrong. Disregard.
259 class LineBreaker {
Alexei Svitkine (slow) 2013/08/27 15:40:38 I think it may be worth exposing the interface of
ckocagil 2013/08/28 19:16:26 Good idea, that should make testing easier and bet
260 public:
261 LineBreaker(int max_width,
262 bool multiline,
263 const BreakList<size_t>* words,
264 const ScopedVector<TextRun>& runs)
265 : max_width_(max_width),
266 multiline_(multiline),
267 words_(words),
268 runs_(runs),
269 text_x_(0),
270 line_x_(0),
271 line_ascent_(0),
272 line_descent_(0) {
273 AdvanceLine();
274 }
275
276 // Breaks the run at given |run_index| into Line structs.
277 void AddRun(int run_index) {
278 TextRun* run = runs_[run_index];
Alexei Svitkine (slow) 2013/08/27 15:40:38 Nit: const
ckocagil 2013/08/28 19:16:26 Done.
279 if (multiline_ && line_x_ + run->width > max_width_)
280 BreakRun(run_index);
281 else
282 AddSegment(run_index, run->range, run->width);
283 }
284
285 // Finishes line breaking and outputs the results. Can be called at most once.
286 void Finalize(std::vector<Line>* lines, Size* size) {
287 DCHECK(!lines_.empty());
288 // Add an empty line to finish the line size calculation and remove it.
289 AdvanceLine();
290 lines_.pop_back();
291 *size = total_size_;
292 lines->swap(lines_);
293 }
294
295 private:
296 // A (line index, segment index) pair that specifies a segment in |lines_|.
297 typedef std::pair<size_t, size_t> SegmentHandle;
298
299 LineSegment* SegmentFromHandle(const SegmentHandle& handle) {
300 return &lines_[handle.first].segments[handle.second];
301 }
302
303 // Breaks a run into segments of at most |max_width_| width, adds all segments
304 // to |lines_.back()|.
msw 2013/08/27 01:30:52 nit: This doesn't sound right. BreakRun appends li
ckocagil 2013/08/28 18:01:45 Updated.
305 void BreakRun(int run_index) {
306 DCHECK(words_);
307 TextRun* const run = runs_[run_index];
308 int width = 0;
309 size_t next_char = run->range.start();
310
311 // Break the run until it fits the current line.
312 while (next_char < run->range.end()) {
313 const int current_char = next_char;
msw 2013/08/27 01:30:52 nit: size_t ?
ckocagil 2013/08/28 18:01:45 Done.
314 BreakRunAtWidth(*run, *words_, next_char, max_width_ - line_x_,
315 line_x_ == 0, &width, &next_char);
316 AddSegment(run_index, ui::Range(current_char, next_char), width);
317 if (next_char < run->range.end())
318 AdvanceLine();
319 }
320 }
321
322 // RTL runs are broken in logical order but displayed in visual order. To find
323 // the text-space coordinate (where it would fall in a single-line text)
324 // |x_range| of RTL segments, segment widths are applied in reverse order.
325 // e.g. {[5, 10], [10, 40]} will become {[35, 40], [5, 35]}.
326 void PopRtl() {
Alexei Svitkine (slow) 2013/08/27 15:40:38 I think a more descriptive name would be better he
ckocagil 2013/08/28 19:16:26 Done.
327 if (rtl_segments_.empty())
328 return;
329 int x = SegmentFromHandle(rtl_segments_[0])->x_range.start();
330 for (size_t i = rtl_segments_.size(); i > 0; --i) {
331 LineSegment* const segment = SegmentFromHandle(rtl_segments_[i - 1]);
Alexei Svitkine (slow) 2013/08/27 15:40:38 Nit: Remove const here, since |segment|'s data is
ckocagil 2013/08/28 19:16:26 Done.
332 segment->x_range = ui::Range(x, x + segment->x_range.length());
Alexei Svitkine (slow) 2013/08/27 15:40:38 Nit: Extract segment->x_range.length() into a loca
ckocagil 2013/08/28 19:16:26 Done.
333 x += segment->x_range.length();
334 }
335 rtl_segments_.clear();
336 }
337
338 void AdvanceLine() {
Alexei Svitkine (slow) 2013/08/27 15:40:38 Nit: Add a brief comment.
ckocagil 2013/08/28 19:16:26 Done.
339 if (!lines_.empty()) {
340 Line* line = &lines_.back();
341 line->baseline = line_ascent_;
342 line->size.set_height(line_ascent_ + line_descent_);
343 line->preceding_heights = total_size_.height();
344 total_size_.set_height(total_size_.height() + line->size.height());
345 total_size_.set_width(std::max(total_size_.width(), line->size.width()));
346 }
347 line_x_ = 0;
348 line_ascent_ = 0;
349 line_descent_ = 0;
350 lines_.push_back(Line());
351 }
352
353 void AddSegment(int run_index, ui::Range char_range, int width) {
Alexei Svitkine (slow) 2013/08/27 15:40:38 Nit: Add a brief comment.
ckocagil 2013/08/28 19:16:26 Done.
354 if (char_range.is_empty()) {
355 DCHECK_EQ(width, 0);
356 return;
357 }
358 const TextRun* run = runs_[run_index];
359 line_ascent_ = std::max(line_ascent_, run->font.GetBaseline());
360 line_descent_ = std::max(line_descent_,
361 run->font.GetHeight() - run->font.GetBaseline());
362 LineSegment segment;
Alexei Svitkine (slow) 2013/08/27 15:40:38 Nit: Add a blank line above this.
ckocagil 2013/08/28 19:16:26 Done.
363 segment.run = run_index;
364 segment.char_range = char_range;
365 segment.x_range = ui::Range(text_x_, text_x_ + width);
366 Line* line = &lines_.back();
Alexei Svitkine (slow) 2013/08/27 15:40:38 Nit: Add a blank line above this.
ckocagil 2013/08/28 19:16:26 Done.
367 line->segments.push_back(segment);
368 line->size.set_width(line->size.width() + segment.x_range.length());
369 if (run->script_analysis.fRTL) {
370 rtl_segments_.push_back(SegmentHandle(lines_.size() - 1,
371 line->segments.size() - 1));
372 if (char_range.end() == run->range.end())
Alexei Svitkine (slow) 2013/08/27 15:40:38 Add a comment about this (i.e. mention that this r
ckocagil 2013/08/28 19:16:26 Done.
373 PopRtl();
374 }
375 text_x_ += width;
376 line_x_ += width;
377 }
378
379 const int max_width_;
380 const bool multiline_;
381 const BreakList<size_t>* const words_;
382 const ScopedVector<TextRun>& runs_;
383
384 // Stores the resulting lines.
385 std::vector<Line> lines_;
386
387 // |text_x_| and |line_x_| are text-space and line-space x coordinates of the
388 // next segment to be added.
389 int text_x_;
390 int line_x_;
391
392 // Size of the multi-line text, not including the currently processed line.
393 Size total_size_;
394
395 // Ascent and descent values of the current line, |lines_.back()|.
396 int line_ascent_;
397 int line_descent_;
398
399 // Segments to be applied by |PopRtl()|.
Alexei Svitkine (slow) 2013/08/27 15:40:38 Mention that this contains segments of the current
ckocagil 2013/08/28 19:16:26 Done.
400 std::vector<SegmentHandle> rtl_segments_;
401
402 DISALLOW_COPY_AND_ASSIGN(LineBreaker);
403 };
404
200 } // namespace internal 405 } // namespace internal
201 406
202 // static 407 // static
203 HDC RenderTextWin::cached_hdc_ = NULL; 408 HDC RenderTextWin::cached_hdc_ = NULL;
204 409
205 // static 410 // static
206 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_; 411 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_;
207 412
208 RenderTextWin::RenderTextWin() 413 RenderTextWin::RenderTextWin()
209 : RenderText(), 414 : RenderText(),
210 common_baseline_(0), 415 common_baseline_(0),
211 needs_layout_(false) { 416 needs_layout_(false) {
212 set_truncate_length(kMaxUniscribeTextLength); 417 set_truncate_length(kMaxUniscribeTextLength);
213 418
214 memset(&script_control_, 0, sizeof(script_control_)); 419 memset(&script_control_, 0, sizeof(script_control_));
215 memset(&script_state_, 0, sizeof(script_state_)); 420 memset(&script_state_, 0, sizeof(script_state_));
216 421
217 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT)); 422 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT));
218 } 423 }
219 424
220 RenderTextWin::~RenderTextWin() { 425 RenderTextWin::~RenderTextWin() {
221 } 426 }
222 427
223 Size RenderTextWin::GetStringSize() { 428 Size RenderTextWin::GetStringSize() {
224 EnsureLayout(); 429 EnsureLayout();
225 return string_size_; 430 return string_size_;
226 } 431 }
227 432
433 Size RenderTextWin::GetMultilineTextSize() {
434 if (!multiline())
435 return GetStringSize();
436 EnsureLayout();
437 return multiline_string_size_;
438 }
439
228 int RenderTextWin::GetBaseline() { 440 int RenderTextWin::GetBaseline() {
229 EnsureLayout(); 441 EnsureLayout();
230 return common_baseline_; 442 return common_baseline_;
231 } 443 }
232 444
233 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { 445 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) {
234 if (text().empty()) 446 if (text().empty())
235 return SelectionModel(); 447 return SelectionModel();
236 448
237 EnsureLayout(); 449 EnsureLayout();
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 GetGlyphXBoundary(run, layout_index, true)); 588 GetGlyphXBoundary(run, layout_index, true));
377 } 589 }
378 590
379 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) { 591 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) {
380 DCHECK(!needs_layout_); 592 DCHECK(!needs_layout_);
381 DCHECK(ui::Range(0, text().length()).Contains(range)); 593 DCHECK(ui::Range(0, text().length()).Contains(range));
382 ui::Range layout_range(TextIndexToLayoutIndex(range.start()), 594 ui::Range layout_range(TextIndexToLayoutIndex(range.start()),
383 TextIndexToLayoutIndex(range.end())); 595 TextIndexToLayoutIndex(range.end()));
384 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range)); 596 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range));
385 597
386 std::vector<Rect> bounds; 598 std::vector<Rect> rects;
387 if (layout_range.is_empty()) 599 if (layout_range.is_empty())
388 return bounds; 600 return rects;
601 std::vector<ui::Range> bounds;
389 602
390 // Add a Rect for each run/selection intersection. 603 // Add a Range for each run/selection intersection.
391 // TODO(msw): The bounds should probably not always be leading the range ends. 604 // TODO(msw): The bounds should probably not always be leading the range ends.
392 for (size_t i = 0; i < runs_.size(); ++i) { 605 for (size_t i = 0; i < runs_.size(); ++i) {
393 const internal::TextRun* run = runs_[visual_to_logical_[i]]; 606 const internal::TextRun* run = runs_[visual_to_logical_[i]];
394 ui::Range intersection = run->range.Intersect(layout_range); 607 ui::Range intersection = run->range.Intersect(layout_range);
395 if (intersection.IsValid()) { 608 if (intersection.IsValid()) {
396 DCHECK(!intersection.is_reversed()); 609 DCHECK(!intersection.is_reversed());
397 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false), 610 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false),
398 GetGlyphXBoundary(run, intersection.end(), false)); 611 GetGlyphXBoundary(run, intersection.end(), false));
399 Rect rect(range_x.GetMin(), 0, range_x.length(), run->font.GetHeight()); 612 if (range_x.is_empty())
400 rect.set_origin(ToViewPoint(rect.origin())); 613 continue;
401 // Union this with the last rect if they're adjacent. 614 range_x = ui::Range(range_x.GetMin(), range_x.GetMax());
402 if (!bounds.empty() && rect.SharesEdgeWith(bounds.back())) { 615 // Union this with the last range if they're adjacent.
403 rect.Union(bounds.back()); 616 DCHECK(bounds.empty() || bounds.back().GetMin() != range_x.GetMax());
617 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) {
618 range_x = ui::Range(bounds.back().GetMin(), range_x.GetMax());
404 bounds.pop_back(); 619 bounds.pop_back();
405 } 620 }
406 bounds.push_back(rect); 621 bounds.push_back(range_x);
407 } 622 }
408 } 623 }
409 return bounds; 624 for (size_t i = 0; i < bounds.size(); ++i) {
625 std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]);
626 rects.insert(rects.end(), current_rects.begin(), current_rects.end());
627 }
628 return rects;
410 } 629 }
411 630
412 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const { 631 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const {
413 DCHECK_LE(index, text().length()); 632 DCHECK_LE(index, text().length());
414 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index; 633 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index;
415 CHECK_GE(i, 0); 634 CHECK_GE(i, 0);
416 // Clamp layout indices to the length of the text actually used for layout. 635 // Clamp layout indices to the length of the text actually used for layout.
417 return std::min<size_t>(GetLayoutText().length(), i); 636 return std::min<size_t>(GetLayoutText().length(), i);
418 } 637 }
419 638
(...skipping 21 matching lines...) Expand all
441 position < LayoutIndexToTextIndex(GetLayoutText().length()) && 660 position < LayoutIndexToTextIndex(GetLayoutText().length()) &&
442 GetGlyphBounds(position) != GetGlyphBounds(position - 1); 661 GetGlyphBounds(position) != GetGlyphBounds(position - 1);
443 } 662 }
444 663
445 void RenderTextWin::ResetLayout() { 664 void RenderTextWin::ResetLayout() {
446 // Layout is performed lazily as needed for drawing/metrics. 665 // Layout is performed lazily as needed for drawing/metrics.
447 needs_layout_ = true; 666 needs_layout_ = true;
448 } 667 }
449 668
450 void RenderTextWin::EnsureLayout() { 669 void RenderTextWin::EnsureLayout() {
451 if (!needs_layout_) 670 if (needs_layout_) {
452 return; 671 // TODO(msw): Skip complex processing if ScriptIsComplex returns false.
453 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. 672 ItemizeLogicalText();
454 ItemizeLogicalText(); 673 if (!runs_.empty())
455 if (!runs_.empty()) 674 LayoutVisualText();
456 LayoutVisualText(); 675 needs_layout_ = false;
457 needs_layout_ = false; 676 std::vector<internal::Line> lines;
677 set_lines(&lines);
678 }
679 // Compute lines if they're not valid. This is separate from the layout steps
Alexei Svitkine (slow) 2013/08/26 21:34:41 Nit: Add a blank line above this.
ckocagil 2013/08/28 18:01:45 Done.
680 // above to avoid text layout and shaping when we resize |display_rect_|.
681 if (lines().empty()) {
682 DCHECK(!needs_layout_);
683 std::vector<internal::Line> lines;
684 internal::LineBreaker line_breaker(display_rect().width() - 1, multiline(),
685 multiline() ? &GetLineBreaks() : NULL,
686 runs_);
687 for (size_t i = 0; i < runs_.size(); ++i)
688 line_breaker.AddRun(visual_to_logical_[i]);
689 line_breaker.Finalize(&lines, &multiline_string_size_);
690 DCHECK(!lines.empty());
691 set_lines(&lines);
692 }
458 } 693 }
459 694
460 void RenderTextWin::DrawVisualText(Canvas* canvas) { 695 void RenderTextWin::DrawVisualText(Canvas* canvas) {
461 DCHECK(!needs_layout_); 696 DCHECK(!needs_layout_);
462 697 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 698
469 std::vector<SkPoint> pos; 699 std::vector<SkPoint> pos;
470 700
471 internal::SkiaTextRenderer renderer(canvas); 701 internal::SkiaTextRenderer renderer(canvas);
472 ApplyFadeEffects(&renderer); 702 ApplyFadeEffects(&renderer);
473 ApplyTextShadows(&renderer); 703 ApplyTextShadows(&renderer);
474 704
475 bool smoothing_enabled; 705 bool smoothing_enabled;
476 bool cleartype_enabled; 706 bool cleartype_enabled;
477 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); 707 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled);
478 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. 708 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|.
479 renderer.SetFontSmoothingSettings( 709 renderer.SetFontSmoothingSettings(
480 smoothing_enabled, cleartype_enabled && !background_is_transparent()); 710 smoothing_enabled, cleartype_enabled && !background_is_transparent());
481 711
482 ApplyCompositionAndSelectionStyles(); 712 ApplyCompositionAndSelectionStyles();
483 713
484 for (size_t i = 0; i < runs_.size(); ++i) { 714 for (size_t i = 0; i < lines().size(); ++i) {
485 // Get the run specified by the visual-to-logical map. 715 const internal::Line& line = lines()[i];
486 internal::TextRun* run = runs_[visual_to_logical_[i]]; 716 Vector2d line_offset = GetLineOffset(i);
717 Vector2d text_offset = line_offset + Vector2d(0, line.baseline);
718 int preceding_segment_widths = 0;
487 719
488 // Skip painting empty runs and runs outside the display rect area. 720 // Skip painting empty lines or lines outside the display rect area.
489 if ((run->glyph_count == 0) || (x >= display_rect().right()) || 721 if (!display_rect().Intersects(Rect(PointAtOffsetFromOrigin(line_offset),
490 (x + run->width <= display_rect().x())) { 722 line.size)))
491 x += run->width;
492 continue; 723 continue;
724
725 for (size_t j = 0; j < line.segments.size(); ++j) {
726 const internal::LineSegment* segment = &line.segments[j];
727 const int segment_width = segment->x_range.length();
728 const internal::TextRun* run = runs_[segment->run];
729 DCHECK(!segment->char_range.is_empty());
730 DCHECK(run->range.Contains(segment->char_range));
731 ui::Range glyphs = CharRangeToGlyphRange(*run, segment->char_range);
Alexei Svitkine (slow) 2013/08/26 21:34:41 Nit: |glyph_range|
ckocagil 2013/08/28 18:01:45 Done.
732 if (glyphs.is_empty()) {
733 DCHECK(segment_width == 0);
Alexei Svitkine (slow) 2013/08/26 21:34:41 DCHECK_EQ
ckocagil 2013/08/28 18:01:45 Done.
734 continue;
735 }
736 // Skip painting segments outside the display rect area.
737 if (!multiline()) {
738 const Rect segment_bounds(PointAtOffsetFromOrigin(line_offset) +
739 Vector2d(preceding_segment_widths, 0),
740 Size(segment_width, line.size.height()));
741 if (!display_rect().Intersects(segment_bounds)) {
742 preceding_segment_widths += segment_width;
743 continue;
744 }
745 }
746
747 // |pos| contains the positions of glyphs. An extra terminal |pos| entry
748 // is added to simplify width calculations.
749 int segment_x = preceding_segment_widths;
750 pos.resize(glyphs.length() + 1);
751 for (size_t g = glyphs.start(); g < glyphs.end(); ++g) {
Alexei Svitkine (slow) 2013/08/26 21:34:41 Either use a descriptive name (like glyph or glyph
ckocagil 2013/08/28 18:01:45 Done, I went with "k".
752 pos[g - glyphs.start()].set(
753 SkIntToScalar(text_offset.x() + segment_x + run->offsets[g].du),
754 SkIntToScalar(text_offset.y() + run->offsets[g].dv));
755 segment_x += run->advance_widths[g];
756 }
757 pos.back().set(
758 SkIntToScalar(text_offset.x() + segment_x),
759 SkIntToScalar(text_offset.y()));
760
761 renderer.SetTextSize(run->font.GetFontSize());
762 renderer.SetFontFamilyWithStyle(run->font.GetFontName(), run->font_style);
763
764 for (BreakList<SkColor>::const_iterator it =
765 colors().GetBreak(run->range.start());
766 it != colors().breaks().end() && it->first < run->range.end();
767 ++it) {
768 const ui::Range intersection =
769 colors().GetRange(it).Intersect(segment->char_range);
770 const ui::Range colored_glyphs =
771 CharRangeToGlyphRange(*run, intersection);
772 DCHECK(glyphs.Contains(colored_glyphs));
773 if (colored_glyphs.is_empty())
774 continue;
775 renderer.SetForegroundColor(it->second);
776 renderer.DrawPosText(&pos[colored_glyphs.start() - glyphs.start()],
Alexei Svitkine (slow) 2013/08/26 21:34:41 You repeat |colored_glyphs.start() - glyphs.start(
Alexei Svitkine (slow) 2013/08/26 21:35:33 Er, rather: const SkPoint& start_pos = pos[col
ckocagil 2013/08/28 18:01:45 Done.
777 &run->glyphs[colored_glyphs.start()],
778 colored_glyphs.length());
779 const SkScalar width = pos[colored_glyphs.end() - glyphs.start()].x() -
780 pos[colored_glyphs.start() - glyphs.start()].x();
781 renderer.DrawDecorations(
782 pos[colored_glyphs.start() - glyphs.start()].x(), text_offset.y(),
783 SkScalarCeilToInt(width), run->underline, run->strike,
784 run->diagonal_strike);
785 }
786
787 preceding_segment_widths += segment_width;
493 } 788 }
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 } 789 }
531 790
532 UndoCompositionAndSelectionStyles(); 791 UndoCompositionAndSelectionStyles();
533 } 792 }
534 793
535 void RenderTextWin::ItemizeLogicalText() { 794 void RenderTextWin::ItemizeLogicalText() {
536 runs_.clear(); 795 runs_.clear();
537 // Make |string_size_|'s height and |common_baseline_| tall enough to draw 796 // 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. 797 // often-used characters which are rendered with fonts in the font list.
539 string_size_ = Size(0, font_list().GetHeight()); 798 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()); 1156 size_t position = LayoutIndexToTextIndex(run->range.end());
898 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); 1157 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD);
899 return SelectionModel(position, CURSOR_FORWARD); 1158 return SelectionModel(position, CURSOR_FORWARD);
900 } 1159 }
901 1160
902 RenderText* RenderText::CreateInstance() { 1161 RenderText* RenderText::CreateInstance() {
903 return new RenderTextWin; 1162 return new RenderTextWin;
904 } 1163 }
905 1164
906 } // namespace gfx 1165 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698