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

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 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 |*next_char| will
160 // be greater than |start_char| (to avoid constructing empty lines).
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 width 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)| to the current character.
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
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. |empty_baseline| and |empty_height| are the baseline and
msw 2013/09/11 17:32:37 I think these will have to be |min_baseline| and |
ckocagil 2013/09/11 20:31:16 Wouldn't this cause every line to have the same si
msw 2013/09/11 21:26:50 I'm not sure what the optimal behavior is, but it'
ckocagil 2013/09/12 02:46:49 Done, added TODO.
262 // height of an empty line.
263 // TODO(ckocagil): Expose the interface of this class in the header and test
264 // this class directly.
265 class LineBreaker {
266 public:
267 LineBreaker(int max_width,
268 int empty_baseline,
269 int empty_height,
270 bool multiline,
271 const BreakList<size_t>* words,
272 const ScopedVector<TextRun>& runs)
273 : max_width_(max_width),
274 empty_baseline_(empty_baseline),
275 empty_height_(empty_height),
276 multiline_(multiline),
277 words_(words),
278 runs_(runs),
279 text_x_(0),
280 line_x_(0),
281 line_ascent_(0),
282 line_descent_(0) {
283 AdvanceLine();
284 }
285
286 // Breaks the run at given |run_index| into Line structs.
287 void AddRun(int run_index) {
288 const TextRun* run = runs_[run_index];
289 if (multiline_ && line_x_ + run->width > max_width_)
290 BreakRun(run_index);
291 else
292 AddSegment(run_index, run->range, run->width);
293 }
294
295 // Finishes line breaking and outputs the results. Can be called at most once.
296 void Finalize(std::vector<Line>* lines, Size* size) {
297 DCHECK(!lines_.empty());
298 // Add an empty line to finish the line size calculation and remove it.
299 AdvanceLine();
300 lines_.pop_back();
301 *size = total_size_;
302 lines->swap(lines_);
303 }
304
305 private:
306 // A (line index, segment index) pair that specifies a segment in |lines_|.
307 typedef std::pair<size_t, size_t> SegmentHandle;
308
309 LineSegment* SegmentFromHandle(const SegmentHandle& handle) {
310 return &lines_[handle.first].segments[handle.second];
311 }
312
313 // Breaks a run into segments that fit in the last line in |lines_| and adds
314 // them. Adds a new Line to the back of |lines_| whenever a new segment can't
315 // be added without the Line's width exceeding |max_width_|.
316 void BreakRun(int run_index) {
317 DCHECK(words_);
318 const TextRun* const run = runs_[run_index];
319 int width = 0;
320 size_t next_char = run->range.start();
321
322 // Break the run until it fits the current line.
323 while (next_char < run->range.end()) {
324 const size_t current_char = next_char;
325 BreakRunAtWidth(*run, *words_, current_char, max_width_ - line_x_,
326 line_x_ == 0, &width, &next_char);
327 AddSegment(run_index, ui::Range(current_char, next_char), width);
328 if (next_char < run->range.end())
329 AdvanceLine();
330 }
331 }
332
333 // RTL runs are broken in logical order but displayed in visual order. To find
334 // the text-space coordinate (where it would fall in a single-line text)
335 // |x_range| of RTL segments, segment widths are applied in reverse order.
336 // e.g. {[5, 10], [10, 40]} will become {[35, 40], [5, 35]}.
337 void UpdateRTLSegmentRanges() {
338 if (rtl_segments_.empty())
339 return;
340 int x = SegmentFromHandle(rtl_segments_[0])->x_range.start();
341 for (size_t i = rtl_segments_.size(); i > 0; --i) {
342 LineSegment* segment = SegmentFromHandle(rtl_segments_[i - 1]);
343 const size_t segment_width = segment->x_range.length();
344 segment->x_range = ui::Range(x, x + segment_width);
345 x += segment_width;
346 }
347 rtl_segments_.clear();
348 }
349
350 // Finishes the size calculations of the last Line in |lines_|. Adds a new
351 // Line to the back of |lines_|.
352 void AdvanceLine() {
353 if (!lines_.empty()) {
354 Line* line = &lines_.back();
355 if (line_ascent_ + line_descent_ == 0) {
356 line_ascent_ = empty_baseline_;
357 line_descent_ = empty_height_ - empty_baseline_;
358 }
359 line->baseline = line_ascent_;
360 line->size.set_height(line_ascent_ + line_descent_);
361 line->preceding_heights = total_size_.height();
362 total_size_.set_height(total_size_.height() + line->size.height());
363 total_size_.set_width(std::max(total_size_.width(), line->size.width()));
364 }
365 line_x_ = 0;
366 line_ascent_ = 0;
367 line_descent_ = 0;
368 lines_.push_back(Line());
369 }
370
371 // Adds a new segment with the given properties to |lines_.back()|.
372 void AddSegment(int run_index, ui::Range char_range, int width) {
373 if (char_range.is_empty()) {
374 DCHECK_EQ(width, 0);
375 return;
376 }
377 const TextRun* run = runs_[run_index];
378 line_ascent_ = std::max(line_ascent_, run->font.GetBaseline());
379 line_descent_ = std::max(line_descent_,
380 run->font.GetHeight() - run->font.GetBaseline());
381
382 LineSegment segment;
383 segment.run = run_index;
384 segment.char_range = char_range;
385 segment.x_range = ui::Range(text_x_, text_x_ + width);
386
387 Line* line = &lines_.back();
388 line->segments.push_back(segment);
389 line->size.set_width(line->size.width() + segment.x_range.length());
390 if (run->script_analysis.fRTL) {
391 rtl_segments_.push_back(SegmentHandle(lines_.size() - 1,
392 line->segments.size() - 1));
393 // If this is the last segment of an RTL run, reprocess the text-space x
394 // ranges of all segments from the run.
395 if (char_range.end() == run->range.end())
396 UpdateRTLSegmentRanges();
397 }
398 text_x_ += width;
399 line_x_ += width;
400 }
401
402 const int max_width_;
403 const int empty_baseline_;
404 const int empty_height_;
405 const bool multiline_;
406 const BreakList<size_t>* const words_;
407 const ScopedVector<TextRun>& runs_;
408
409 // Stores the resulting lines.
410 std::vector<Line> lines_;
411
412 // Text space and line space x coordinates of the next segment to be added.
413 int text_x_;
414 int line_x_;
415
416 // Size of the multiline text, not including the currently processed line.
417 Size total_size_;
418
419 // Ascent and descent values of the current line, |lines_.back()|.
420 int line_ascent_;
421 int line_descent_;
422
423 // The current RTL run segments, to be applied by |UpdateRTLSegmentRanges()|.
424 std::vector<SegmentHandle> rtl_segments_;
425
426 DISALLOW_COPY_AND_ASSIGN(LineBreaker);
427 };
428
429 // For segments in the same run, checks the continuity and order of |x_range|
430 // and |char_range| fields.
431 void CheckLineIntegrity(const std::vector<Line>& lines,
msw 2013/09/11 17:32:37 Move this up into the anonymous namespace, unless
ckocagil 2013/09/11 20:31:16 This is no longer called from _unittests.cc anymor
432 const ScopedVector<TextRun>& runs) {
433 size_t previous_segment_line = 0;
434 const LineSegment* previous_segment = NULL;
435
436 for (size_t i = 0; i < lines.size(); ++i) {
437 for (size_t j = 0; j < lines[i].segments.size(); ++j) {
438 const LineSegment* segment = &lines[i].segments[j];
439 TextRun* run = runs[segment->run];
440
441 if (!previous_segment) {
442 previous_segment = segment;
443 } else if (runs[previous_segment->run] != run) {
444 previous_segment = NULL;
445 } else {
446 DCHECK_EQ(previous_segment->char_range.end(),
447 segment->char_range.start());
448 if (!run->script_analysis.fRTL) {
msw 2013/09/11 17:32:37 nit: remove braces.
ckocagil 2013/09/11 20:31:16 These are necessary here. DCHECK_EQ expands to a n
449 DCHECK_EQ(previous_segment->x_range.end(), segment->x_range.start());
450 } else {
451 DCHECK_EQ(segment->x_range.end(), previous_segment->x_range.start());
452 }
453
454 previous_segment = segment;
455 previous_segment_line = i;
456 }
457 }
458 }
459 }
460
200 } // namespace internal 461 } // namespace internal
201 462
202 // static 463 // static
203 HDC RenderTextWin::cached_hdc_ = NULL; 464 HDC RenderTextWin::cached_hdc_ = NULL;
204 465
205 // static 466 // static
206 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_; 467 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_;
207 468
208 RenderTextWin::RenderTextWin() 469 RenderTextWin::RenderTextWin()
209 : RenderText(), 470 : RenderText(),
210 common_baseline_(0),
211 needs_layout_(false) { 471 needs_layout_(false) {
212 set_truncate_length(kMaxUniscribeTextLength); 472 set_truncate_length(kMaxUniscribeTextLength);
213 473
214 memset(&script_control_, 0, sizeof(script_control_)); 474 memset(&script_control_, 0, sizeof(script_control_));
215 memset(&script_state_, 0, sizeof(script_state_)); 475 memset(&script_state_, 0, sizeof(script_state_));
216 476
217 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT)); 477 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT));
218 } 478 }
219 479
220 RenderTextWin::~RenderTextWin() { 480 RenderTextWin::~RenderTextWin() {
221 } 481 }
222 482
223 Size RenderTextWin::GetStringSize() { 483 Size RenderTextWin::GetStringSize() {
224 EnsureLayout(); 484 EnsureLayout();
225 return string_size_; 485 return multiline_string_size_;
226 } 486 }
227 487
228 int RenderTextWin::GetBaseline() { 488 int RenderTextWin::GetBaseline() {
229 EnsureLayout(); 489 EnsureLayout();
230 return common_baseline_; 490 return lines()[0].baseline;
231 } 491 }
232 492
233 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { 493 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) {
234 if (text().empty()) 494 if (text().empty())
235 return SelectionModel(); 495 return SelectionModel();
236 496
237 EnsureLayout(); 497 EnsureLayout();
238 // Find the run that contains the point and adjust the argument location. 498 // Find the run that contains the point and adjust the argument location.
239 int x = ToTextPoint(point).x(); 499 int x = ToTextPoint(point).x();
240 size_t run_index = GetRunContainingXCoord(x); 500 size_t run_index = GetRunContainingXCoord(x);
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 } 622 }
363 } 623 }
364 return SelectionModel(pos, CURSOR_FORWARD); 624 return SelectionModel(pos, CURSOR_FORWARD);
365 } 625 }
366 626
367 ui::Range RenderTextWin::GetGlyphBounds(size_t index) { 627 ui::Range RenderTextWin::GetGlyphBounds(size_t index) {
368 const size_t run_index = 628 const size_t run_index =
369 GetRunContainingCaret(SelectionModel(index, CURSOR_FORWARD)); 629 GetRunContainingCaret(SelectionModel(index, CURSOR_FORWARD));
370 // Return edge bounds if the index is invalid or beyond the layout text size. 630 // Return edge bounds if the index is invalid or beyond the layout text size.
371 if (run_index >= runs_.size()) 631 if (run_index >= runs_.size())
372 return ui::Range(string_size_.width()); 632 return ui::Range(string_width_);
373 internal::TextRun* run = runs_[run_index]; 633 internal::TextRun* run = runs_[run_index];
374 const size_t layout_index = TextIndexToLayoutIndex(index); 634 const size_t layout_index = TextIndexToLayoutIndex(index);
375 return ui::Range(GetGlyphXBoundary(run, layout_index, false), 635 return ui::Range(GetGlyphXBoundary(run, layout_index, false),
376 GetGlyphXBoundary(run, layout_index, true)); 636 GetGlyphXBoundary(run, layout_index, true));
377 } 637 }
378 638
379 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) { 639 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) {
380 DCHECK(!needs_layout_); 640 DCHECK(!needs_layout_);
381 DCHECK(ui::Range(0, text().length()).Contains(range)); 641 DCHECK(ui::Range(0, text().length()).Contains(range));
382 ui::Range layout_range(TextIndexToLayoutIndex(range.start()), 642 ui::Range layout_range(TextIndexToLayoutIndex(range.start()),
383 TextIndexToLayoutIndex(range.end())); 643 TextIndexToLayoutIndex(range.end()));
384 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range)); 644 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range));
385 645
386 std::vector<Rect> bounds; 646 std::vector<Rect> rects;
387 if (layout_range.is_empty()) 647 if (layout_range.is_empty())
388 return bounds; 648 return rects;
649 std::vector<ui::Range> bounds;
389 650
390 // Add a Rect for each run/selection intersection. 651 // Add a Range for each run/selection intersection.
391 // TODO(msw): The bounds should probably not always be leading the range ends. 652 // TODO(msw): The bounds should probably not always be leading the range ends.
392 for (size_t i = 0; i < runs_.size(); ++i) { 653 for (size_t i = 0; i < runs_.size(); ++i) {
393 const internal::TextRun* run = runs_[visual_to_logical_[i]]; 654 const internal::TextRun* run = runs_[visual_to_logical_[i]];
394 ui::Range intersection = run->range.Intersect(layout_range); 655 ui::Range intersection = run->range.Intersect(layout_range);
395 if (intersection.IsValid()) { 656 if (intersection.IsValid()) {
396 DCHECK(!intersection.is_reversed()); 657 DCHECK(!intersection.is_reversed());
397 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false), 658 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false),
398 GetGlyphXBoundary(run, intersection.end(), false)); 659 GetGlyphXBoundary(run, intersection.end(), false));
399 Rect rect(range_x.GetMin(), 0, range_x.length(), run->font.GetHeight()); 660 if (range_x.is_empty())
400 rect.set_origin(ToViewPoint(rect.origin())); 661 continue;
401 // Union this with the last rect if they're adjacent. 662 range_x = ui::Range(range_x.GetMin(), range_x.GetMax());
402 if (!bounds.empty() && rect.SharesEdgeWith(bounds.back())) { 663 // Union this with the last range if they're adjacent.
403 rect.Union(bounds.back()); 664 DCHECK(bounds.empty() || bounds.back().GetMax() <= range_x.GetMin());
665 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) {
666 range_x = ui::Range(bounds.back().GetMin(), range_x.GetMax());
404 bounds.pop_back(); 667 bounds.pop_back();
405 } 668 }
406 bounds.push_back(rect); 669 bounds.push_back(range_x);
407 } 670 }
408 } 671 }
409 return bounds; 672 for (size_t i = 0; i < bounds.size(); ++i) {
673 std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]);
674 rects.insert(rects.end(), current_rects.begin(), current_rects.end());
675 }
676 return rects;
410 } 677 }
411 678
412 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const { 679 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const {
413 DCHECK_LE(index, text().length()); 680 DCHECK_LE(index, text().length());
414 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index; 681 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index;
415 CHECK_GE(i, 0); 682 CHECK_GE(i, 0);
416 // Clamp layout indices to the length of the text actually used for layout. 683 // Clamp layout indices to the length of the text actually used for layout.
417 return std::min<size_t>(GetLayoutText().length(), i); 684 return std::min<size_t>(GetLayoutText().length(), i);
418 } 685 }
419 686
(...skipping 21 matching lines...) Expand all
441 position < LayoutIndexToTextIndex(GetLayoutText().length()) && 708 position < LayoutIndexToTextIndex(GetLayoutText().length()) &&
442 GetGlyphBounds(position) != GetGlyphBounds(position - 1); 709 GetGlyphBounds(position) != GetGlyphBounds(position - 1);
443 } 710 }
444 711
445 void RenderTextWin::ResetLayout() { 712 void RenderTextWin::ResetLayout() {
446 // Layout is performed lazily as needed for drawing/metrics. 713 // Layout is performed lazily as needed for drawing/metrics.
447 needs_layout_ = true; 714 needs_layout_ = true;
448 } 715 }
449 716
450 void RenderTextWin::EnsureLayout() { 717 void RenderTextWin::EnsureLayout() {
451 if (!needs_layout_) 718 if (needs_layout_) {
452 return; 719 // TODO(msw): Skip complex processing if ScriptIsComplex returns false.
453 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. 720 ItemizeLogicalText();
454 ItemizeLogicalText(); 721 if (!runs_.empty())
455 if (!runs_.empty()) 722 LayoutVisualText();
456 LayoutVisualText(); 723 needs_layout_ = false;
457 needs_layout_ = false; 724 std::vector<internal::Line> lines;
725 set_lines(&lines);
726 }
727
728 // Compute lines if they're not valid. This is separate from the layout steps
729 // above to avoid text layout and shaping when we resize |display_rect_|.
730 if (lines().empty()) {
731 DCHECK(!needs_layout_);
732 std::vector<internal::Line> lines;
733 internal::LineBreaker line_breaker(display_rect().width() - 1,
734 font_list().GetBaseline(),
735 font_list().GetHeight(), multiline(),
736 multiline() ? &GetLineBreaks() : NULL,
737 runs_);
738 for (size_t i = 0; i < runs_.size(); ++i)
739 line_breaker.AddRun(visual_to_logical_[i]);
740 line_breaker.Finalize(&lines, &multiline_string_size_);
741 DCHECK(!lines.empty());
742 set_lines(&lines);
743 #ifndef NDEBUG
744 CheckLineIntegrity(this->lines(), runs_);
msw 2013/09/11 17:32:37 nit: do this on |lines| before calling set_lines(&
ckocagil 2013/09/11 20:31:16 Done.
745 #endif
746 }
458 } 747 }
459 748
460 void RenderTextWin::DrawVisualText(Canvas* canvas) { 749 void RenderTextWin::DrawVisualText(Canvas* canvas) {
461 DCHECK(!needs_layout_); 750 DCHECK(!needs_layout_);
462 751 DCHECK(!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 752
469 std::vector<SkPoint> pos; 753 std::vector<SkPoint> pos;
470 754
471 internal::SkiaTextRenderer renderer(canvas); 755 internal::SkiaTextRenderer renderer(canvas);
472 ApplyFadeEffects(&renderer); 756 ApplyFadeEffects(&renderer);
473 ApplyTextShadows(&renderer); 757 ApplyTextShadows(&renderer);
474 758
475 bool smoothing_enabled; 759 bool smoothing_enabled;
476 bool cleartype_enabled; 760 bool cleartype_enabled;
477 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); 761 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled);
478 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. 762 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|.
479 renderer.SetFontSmoothingSettings( 763 renderer.SetFontSmoothingSettings(
480 smoothing_enabled, cleartype_enabled && !background_is_transparent()); 764 smoothing_enabled, cleartype_enabled && !background_is_transparent());
481 765
482 ApplyCompositionAndSelectionStyles(); 766 ApplyCompositionAndSelectionStyles();
483 767
484 for (size_t i = 0; i < runs_.size(); ++i) { 768 for (size_t i = 0; i < lines().size(); ++i) {
485 // Get the run specified by the visual-to-logical map. 769 const internal::Line& line = lines()[i];
486 internal::TextRun* run = runs_[visual_to_logical_[i]]; 770 const Vector2d line_offset = GetLineOffset(i);
487 771
488 // Skip painting empty runs and runs outside the display rect area. 772 // Skip painting empty lines or lines outside the display rect area.
489 if ((run->glyph_count == 0) || (x >= display_rect().right()) || 773 if (!display_rect().Intersects(Rect(PointAtOffsetFromOrigin(line_offset),
490 (x + run->width <= display_rect().x())) { 774 line.size)))
491 x += run->width;
492 continue; 775 continue;
776
777 const Vector2d text_offset = line_offset + Vector2d(0, line.baseline);
778 int preceding_segment_widths = 0;
779
780 for (size_t j = 0; j < line.segments.size(); ++j) {
781 const internal::LineSegment* segment = &line.segments[j];
782 const int segment_width = segment->x_range.length();
783 const internal::TextRun* run = runs_[segment->run];
784 DCHECK(!segment->char_range.is_empty());
785 DCHECK(run->range.Contains(segment->char_range));
786 ui::Range glyph_range = CharRangeToGlyphRange(*run, segment->char_range);
787 DCHECK(!glyph_range.is_empty());
788 // Skip painting segments outside the display rect area.
789 if (!multiline()) {
790 const Rect segment_bounds(PointAtOffsetFromOrigin(line_offset) +
791 Vector2d(preceding_segment_widths, 0),
792 Size(segment_width, line.size.height()));
793 if (!display_rect().Intersects(segment_bounds)) {
794 preceding_segment_widths += segment_width;
795 continue;
796 }
797 }
798
799 // |pos| contains the positions of glyphs. An extra terminal |pos| entry
800 // is added to simplify width calculations.
801 int segment_x = preceding_segment_widths;
802 pos.resize(glyph_range.length() + 1);
803 for (size_t k = glyph_range.start(); k < glyph_range.end(); ++k) {
804 pos[k - glyph_range.start()].set(
805 SkIntToScalar(text_offset.x() + run->offsets[k].du + segment_x),
806 SkIntToScalar(text_offset.y() + run->offsets[k].dv));
807 segment_x += run->advance_widths[k];
808 }
809 pos.back().set(SkIntToScalar(text_offset.x() + segment_x),
810 SkIntToScalar(text_offset.y()));
811
812 renderer.SetTextSize(run->font.GetFontSize());
813 renderer.SetFontFamilyWithStyle(run->font.GetFontName(), run->font_style);
814
815 for (BreakList<SkColor>::const_iterator it =
816 colors().GetBreak(segment->char_range.start());
817 it != colors().breaks().end() &&
818 it->first < segment->char_range.end();
819 ++it) {
820 const ui::Range intersection =
821 colors().GetRange(it).Intersect(segment->char_range);
822 const ui::Range colored_glyphs =
823 CharRangeToGlyphRange(*run, intersection);
824 DCHECK(glyph_range.Contains(colored_glyphs));
825 DCHECK(!colored_glyphs.is_empty());
826 const SkPoint& start_pos =
827 pos[colored_glyphs.start() - glyph_range.start()];
828 const SkPoint& end_pos =
829 pos[colored_glyphs.end() - glyph_range.start()];
830
831 renderer.SetForegroundColor(it->second);
832 renderer.DrawPosText(&start_pos, &run->glyphs[colored_glyphs.start()],
833 colored_glyphs.length());
834 renderer.DrawDecorations(start_pos.x(), text_offset.y(),
835 SkScalarCeilToInt(end_pos.x() - start_pos.x()),
836 run->underline, run->strike,
837 run->diagonal_strike);
838 }
839
840 preceding_segment_widths += segment_width;
493 } 841 }
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 } 842 }
531 843
532 UndoCompositionAndSelectionStyles(); 844 UndoCompositionAndSelectionStyles();
533 } 845 }
534 846
535 void RenderTextWin::ItemizeLogicalText() { 847 void RenderTextWin::ItemizeLogicalText() {
536 runs_.clear(); 848 runs_.clear();
537 // Make |string_size_|'s height and |common_baseline_| tall enough to draw 849 string_width_ = 0;
msw 2013/09/11 17:32:37 Clear multiline_string_size_ here, this seems like
ckocagil 2013/09/11 20:31:16 Done.
538 // often-used characters which are rendered with fonts in the font list.
539 string_size_ = Size(0, font_list().GetHeight());
540 common_baseline_ = font_list().GetBaseline();
541 850
542 // Set Uniscribe's base text direction. 851 // Set Uniscribe's base text direction.
543 script_state_.uBidiLevel = 852 script_state_.uBidiLevel =
544 (GetTextDirection() == base::i18n::RIGHT_TO_LEFT) ? 1 : 0; 853 (GetTextDirection() == base::i18n::RIGHT_TO_LEFT) ? 1 : 0;
545 854
546 if (text().empty()) 855 if (text().empty())
547 return; 856 return;
548 857
549 HRESULT hr = E_OUTOFMEMORY; 858 HRESULT hr = E_OUTOFMEMORY;
550 int script_items_count = 0; 859 int script_items_count = 0;
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 run->glyphs.get(), 945 run->glyphs.get(),
637 run->glyph_count, 946 run->glyph_count,
638 run->visible_attributes.get(), 947 run->visible_attributes.get(),
639 &(run->script_analysis), 948 &(run->script_analysis),
640 run->advance_widths.get(), 949 run->advance_widths.get(),
641 run->offsets.get(), 950 run->offsets.get(),
642 &(run->abc_widths)); 951 &(run->abc_widths));
643 DCHECK(SUCCEEDED(hr)); 952 DCHECK(SUCCEEDED(hr));
644 } 953 }
645 } 954 }
646 string_size_.set_height(ascent + descent);
647 common_baseline_ = ascent;
648 955
649 // Build the array of bidirectional embedding levels. 956 // Build the array of bidirectional embedding levels.
650 scoped_ptr<BYTE[]> levels(new BYTE[runs_.size()]); 957 scoped_ptr<BYTE[]> levels(new BYTE[runs_.size()]);
651 for (size_t i = 0; i < runs_.size(); ++i) 958 for (size_t i = 0; i < runs_.size(); ++i)
652 levels[i] = runs_[i]->script_analysis.s.uBidiLevel; 959 levels[i] = runs_[i]->script_analysis.s.uBidiLevel;
653 960
654 // Get the maps between visual and logical run indices. 961 // Get the maps between visual and logical run indices.
655 visual_to_logical_.reset(new int[runs_.size()]); 962 visual_to_logical_.reset(new int[runs_.size()]);
656 logical_to_visual_.reset(new int[runs_.size()]); 963 logical_to_visual_.reset(new int[runs_.size()]);
657 hr = ScriptLayout(runs_.size(), 964 hr = ScriptLayout(runs_.size(),
658 levels.get(), 965 levels.get(),
659 visual_to_logical_.get(), 966 visual_to_logical_.get(),
660 logical_to_visual_.get()); 967 logical_to_visual_.get());
661 DCHECK(SUCCEEDED(hr)); 968 DCHECK(SUCCEEDED(hr));
662 969
663 // Precalculate run width information. 970 // Precalculate run width information.
664 size_t preceding_run_widths = 0; 971 size_t preceding_run_widths = 0;
665 for (size_t i = 0; i < runs_.size(); ++i) { 972 for (size_t i = 0; i < runs_.size(); ++i) {
666 internal::TextRun* run = runs_[visual_to_logical_[i]]; 973 internal::TextRun* run = runs_[visual_to_logical_[i]];
667 run->preceding_run_widths = preceding_run_widths; 974 run->preceding_run_widths = preceding_run_widths;
668 const ABC& abc = run->abc_widths; 975 const ABC& abc = run->abc_widths;
669 run->width = abc.abcA + abc.abcB + abc.abcC; 976 run->width = abc.abcA + abc.abcB + abc.abcC;
670 preceding_run_widths += run->width; 977 preceding_run_widths += run->width;
671 } 978 }
672 string_size_.set_width(preceding_run_widths); 979 string_width_ = preceding_run_widths;
673 } 980 }
674 981
675 void RenderTextWin::LayoutTextRun(internal::TextRun* run) { 982 void RenderTextWin::LayoutTextRun(internal::TextRun* run) {
676 const size_t run_length = run->range.length(); 983 const size_t run_length = run->range.length();
677 const wchar_t* run_text = &(GetLayoutText()[run->range.start()]); 984 const wchar_t* run_text = &(GetLayoutText()[run->range.start()]);
678 Font original_font = run->font; 985 Font original_font = run->font;
679 LinkedFontsIterator fonts(original_font); 986 LinkedFontsIterator fonts(original_font);
680 bool tried_cached_font = false; 987 bool tried_cached_font = false;
681 bool tried_fallback = false; 988 bool tried_fallback = false;
682 // Keep track of the font that is able to display the greatest number of 989 // Keep track of the font that is able to display the greatest number of
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 size_t position = LayoutIndexToTextIndex(run->range.end()); 1204 size_t position = LayoutIndexToTextIndex(run->range.end());
898 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); 1205 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD);
899 return SelectionModel(position, CURSOR_FORWARD); 1206 return SelectionModel(position, CURSOR_FORWARD);
900 } 1207 }
901 1208
902 RenderText* RenderText::CreateInstance() { 1209 RenderText* RenderText::CreateInstance() {
903 return new RenderTextWin; 1210 return new RenderTextWin;
904 } 1211 }
905 1212
906 } // namespace gfx 1213 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698