Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/render_text_win.h" | 5 #include "ui/gfx/render_text_win.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 } else { | 147 } else { |
| 148 result = ui::Range(run.logical_clusters[run_range.start()], | 148 result = ui::Range(run.logical_clusters[run_range.start()], |
| 149 run_range.end() < run.range.length() ? | 149 run_range.end() < run.range.length() ? |
| 150 run.logical_clusters[run_range.end()] : run.glyph_count); | 150 run.logical_clusters[run_range.end()] : run.glyph_count); |
| 151 } | 151 } |
| 152 DCHECK(!result.is_reversed()); | 152 DCHECK(!result.is_reversed()); |
| 153 DCHECK(ui::Range(0, run.glyph_count).Contains(result)); | 153 DCHECK(ui::Range(0, run.glyph_count).Contains(result)); |
| 154 return result; | 154 return result; |
| 155 } | 155 } |
| 156 | 156 |
| 157 // Starting from |start_char|, finds a suitable line break position at or before | |
| 158 // |available_width| using word break info from |breaks|. If |empty_line| is | |
| 159 // true, this function will not roll back to |start_char| and |*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 bool 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) { | |
| 173 DCHECK(run.range.Contains(ui::Range(start_char))); | |
| 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) { | |
| 182 word = next_word++; | |
| 183 word_width = 0; | |
| 184 } | |
| 185 | |
| 186 ui::Range glyphs = CharRangeToGlyphRange(run, ui::Range(i, i + 1)); | |
| 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 true; | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 *pos = run.range.end(); | |
| 210 return false; | |
| 211 } | |
| 212 | |
| 157 } // namespace | 213 } // namespace |
| 158 | 214 |
| 159 namespace internal { | 215 namespace internal { |
| 160 | 216 |
| 161 TextRun::TextRun() | 217 TextRun::TextRun() |
| 162 : font_style(0), | 218 : font_style(0), |
| 163 strike(false), | 219 strike(false), |
| 164 diagonal_strike(false), | 220 diagonal_strike(false), |
| 165 underline(false), | 221 underline(false), |
| 166 width(0), | 222 width(0), |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 190 run->glyph_count, | 246 run->glyph_count, |
| 191 run->logical_clusters.get(), | 247 run->logical_clusters.get(), |
| 192 run->visible_attributes.get(), | 248 run->visible_attributes.get(), |
| 193 run->advance_widths.get(), | 249 run->advance_widths.get(), |
| 194 &run->script_analysis, | 250 &run->script_analysis, |
| 195 &x); | 251 &x); |
| 196 DCHECK(SUCCEEDED(hr)); | 252 DCHECK(SUCCEEDED(hr)); |
| 197 return run->preceding_run_widths + x; | 253 return run->preceding_run_widths + x; |
| 198 } | 254 } |
| 199 | 255 |
| 256 // Internal class to help break a text into lines. If |multiline| is false, | |
| 257 // doesn't do any line breaking and outputs a single Line. | |
| 258 class LineBreaker { | |
| 259 public: | |
| 260 LineBreaker(int max_width, | |
| 261 bool multiline, | |
| 262 const BreakList<size_t>* words, | |
| 263 const ScopedVector<internal::TextRun>& runs) | |
| 264 : max_width_(max_width), | |
| 265 multiline_(multiline), | |
| 266 words_(words), | |
| 267 runs_(runs), | |
| 268 pos_(0), | |
| 269 text_x_(0), | |
| 270 line_x_(0), | |
| 271 preceding_line_heights_(0), | |
| 272 common_width_(0), | |
| 273 line_ascent_(0), | |
| 274 line_descent_(0) { | |
| 275 AdvanceLine(); | |
| 276 } | |
| 277 | |
| 278 // Breaks the run at given |run_index| into Line structs. | |
| 279 void AddRun(int run_index) { | |
| 280 internal::TextRun* run = runs_[run_index]; | |
|
Alexei Svitkine (slow)
2013/08/16 18:32:34
Nit: No need for internal::, since this is in the
ckocagil
2013/08/20 00:09:06
Done.
| |
| 281 pos_ = run->range.start(); | |
| 282 int width = run->width; | |
| 283 if (multiline_ && line_x_ + width > max_width_) | |
| 284 width = BreakRun(run_index); | |
| 285 // Remaining part of the run fits the line, add it as well. | |
| 286 AddSegment(run_index, run->range.end(), width); | |
| 287 } | |
| 288 | |
| 289 // Finishes line breaking and outputs the results. Can be called at most once. | |
| 290 void Finalize(std::vector<internal::Line>* lines, Size* size) { | |
| 291 DCHECK(!lines_.empty()); | |
| 292 // Add an empty line to finish the line size calculation and remove it. | |
| 293 AdvanceLine(); | |
| 294 lines_.pop_back(); | |
| 295 *size = Size(common_width_, | |
| 296 lines_.back().preceding_heights + lines_.back().height); | |
|
Alexei Svitkine (slow)
2013/08/16 18:32:34
I don't think Line::preceding_heights is needed -
ckocagil
2013/08/20 00:09:06
It is needed for |RenderText::GetLineOffset()|. I
| |
| 297 lines->swap(lines_); | |
| 298 } | |
| 299 | |
| 300 private: | |
| 301 // Breaks a run into segments of at most |max_width_| width, adds all but the | |
| 302 // final segment to |lines_.back()|, returns the width of that final segment. | |
| 303 int BreakRun(int run_index) { | |
| 304 DCHECK(words_); | |
| 305 int width = 0; | |
| 306 size_t next_pos = 0; | |
| 307 | |
| 308 // Break the run until it fits the current line. | |
| 309 while (BreakRunAtWidth(*runs_[run_index], *words_, pos_, | |
| 310 max_width_ - line_x_, line_x_ == 0, &width, | |
| 311 &next_pos)) { | |
| 312 DCHECK_LT(pos_, runs_[run_index]->range.end()); | |
| 313 AddSegment(run_index, next_pos, width); | |
| 314 AdvanceLine(); | |
| 315 } | |
| 316 | |
| 317 return width; | |
| 318 } | |
| 319 | |
| 320 // RTL runs are broken in logical order but displayed in visual order. To find | |
| 321 // the text-space coordinate (where it would fall in a single-line text) | |
| 322 // |x_pos| of RTL segments, segment widths are applied in reverse order. | |
| 323 // e.g. {[5, 10], [10, 40]} will become {[35, 40], [5, 35]}. | |
| 324 void PopRtl() { | |
| 325 if (rtl_segments_.empty()) | |
| 326 return; | |
| 327 int x = rtl_segments_[0]->x_pos.start(); | |
| 328 for (size_t i = rtl_segments_.size(); i > 0; --i) { | |
| 329 LineSegment* segment = rtl_segments_[i - 1]; | |
| 330 segment->x_pos = ui::Range(x, x + segment->x_pos.length()); | |
| 331 x += segment->x_pos.length(); | |
| 332 } | |
| 333 rtl_segments_.clear(); | |
| 334 } | |
| 335 | |
| 336 void AdvanceLine() { | |
| 337 if (!lines_.empty()) { | |
| 338 internal::Line* line = &lines_.back(); | |
| 339 line->baseline = line_ascent_; | |
| 340 line->height = line_ascent_ + line_descent_; | |
| 341 line->preceding_heights = preceding_line_heights_; | |
| 342 preceding_line_heights_ += line->height; | |
| 343 common_width_ = std::max(common_width_, line->width); | |
| 344 } | |
| 345 line_x_ = 0; | |
| 346 line_ascent_ = 0; | |
| 347 line_descent_ = 0; | |
| 348 lines_.push_back(internal::Line()); | |
| 349 } | |
| 350 | |
| 351 void AddSegment(int run_index, size_t new_pos, int width) { | |
|
Alexei Svitkine (slow)
2013/08/16 18:32:34
Rename new_pos to segment_end.
ckocagil
2013/08/20 00:09:06
Done.
| |
| 352 if (pos_ == new_pos) { | |
| 353 DCHECK_EQ(width, 0); | |
| 354 return; | |
| 355 } | |
| 356 const internal::TextRun* run = runs_[run_index]; | |
| 357 line_ascent_ = std::max(line_ascent_, run->font.GetBaseline()); | |
| 358 line_descent_ = std::max(line_descent_, | |
| 359 run->font.GetHeight() - run->font.GetBaseline()); | |
| 360 internal::LineSegment segment; | |
| 361 segment.run = run_index; | |
| 362 segment.char_pos = ui::Range(pos_, new_pos); | |
| 363 segment.x_pos = ui::Range(text_x_, text_x_ + width); | |
| 364 internal::Line* line = &lines_.back(); | |
| 365 line->segments.push_back(segment); | |
| 366 line->width += segment.x_pos.length(); | |
| 367 if (run->script_analysis.fRTL) { | |
| 368 rtl_segments_.push_back(&line->segments.back()); | |
| 369 if (new_pos == run->range.end()) | |
| 370 PopRtl(); | |
| 371 } | |
| 372 pos_ = new_pos; | |
| 373 text_x_ += width; | |
| 374 line_x_ += width; | |
| 375 } | |
| 376 | |
| 377 const int max_width_; | |
| 378 const bool multiline_; | |
| 379 const BreakList<size_t>* const words_; | |
| 380 const ScopedVector<internal::TextRun>& runs_; | |
| 381 | |
| 382 // Stores the resulting lines. | |
| 383 std::vector<internal::Line> lines_; | |
| 384 | |
| 385 // Position information of the last added segment. |pos_| is the segment's end | |
|
Alexei Svitkine (slow)
2013/08/16 18:32:34
I think it would be clearer to describe these in t
ckocagil
2013/08/20 00:09:06
Done.
| |
| 386 // character position. |text_x_| and |line_x_| are text-space and line-space x | |
| 387 // coordinates of |pos_|. | |
| 388 size_t pos_; | |
| 389 int text_x_; | |
| 390 int line_x_; | |
| 391 int preceding_line_heights_; | |
| 392 | |
| 393 int common_width_; | |
| 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()|. | |
| 400 std::vector<LineSegment*> 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 Loading... | |
| 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 Loading... | |
| 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 | |
| 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 Size(line.width, line.height)))) |
| 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_pos.length(); | |
| 728 const internal::TextRun* run = runs_[segment->run]; | |
| 729 DCHECK(!segment->char_pos.is_empty()); | |
| 730 DCHECK(run->range.Contains(segment->char_pos)); | |
| 731 ui::Range glyphs = CharRangeToGlyphRange(*run, segment->char_pos); | |
| 732 if (glyphs.is_empty()) { | |
| 733 DCHECK(segment_width == 0); | |
| 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.height)); | |
| 741 if (!display_rect().Intersects(segment_bounds)) { | |
| 742 preceding_segment_widths += segment_width; | |
| 743 continue; | |
| 744 } | |
| 745 } | |
| 746 | |
| 747 int segment_x = 0; | |
| 748 pos.resize(glyphs.length()); | |
| 749 for (size_t g = glyphs.start(); g < glyphs.end(); ++g) { | |
| 750 pos[g - glyphs.start()].set( | |
| 751 SkIntToScalar(text_offset.x() + preceding_segment_widths + | |
| 752 segment_x + run->offsets[g].du), | |
| 753 SkIntToScalar(text_offset.y() + run->offsets[g].dv)); | |
| 754 segment_x += run->advance_widths[g]; | |
| 755 } | |
| 756 | |
| 757 renderer.SetTextSize(run->font.GetFontSize()); | |
| 758 renderer.SetFontFamilyWithStyle(run->font.GetFontName(), run->font_style); | |
| 759 | |
| 760 for (BreakList<SkColor>::const_iterator it = | |
| 761 colors().GetBreak(run->range.start()); | |
| 762 it != colors().breaks().end() && it->first < run->range.end(); | |
| 763 ++it) { | |
| 764 const ui::Range intersection = | |
| 765 colors().GetRange(it).Intersect(segment->char_pos); | |
| 766 const ui::Range colored_glyphs = | |
| 767 CharRangeToGlyphRange(*run, intersection); | |
| 768 DCHECK(glyphs.Contains(colored_glyphs)); | |
| 769 if (colored_glyphs.is_empty()) | |
| 770 continue; | |
| 771 renderer.SetForegroundColor(it->second); | |
| 772 renderer.DrawPosText(&pos[colored_glyphs.start() - glyphs.start()], | |
| 773 &run->glyphs[colored_glyphs.start()], | |
| 774 colored_glyphs.length()); | |
| 775 const SkScalar width = | |
| 776 (colored_glyphs.end() < glyphs.end() ? | |
| 777 pos[colored_glyphs.end() - glyphs.start()].x() : | |
| 778 pos[0].x() + SkIntToScalar(segment_width)) | |
| 779 - pos[colored_glyphs.start() - glyphs.start()].x(); | |
| 780 renderer.DrawDecorations( | |
| 781 pos[colored_glyphs.start() - glyphs.start()].x(), text_offset.y(), | |
| 782 SkScalarCeilToInt(width), run->underline, run->strike, | |
| 783 run->diagonal_strike); | |
| 784 } | |
| 785 | |
| 786 preceding_segment_widths += segment_width; | |
| 493 } | 787 } |
| 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 } | 788 } |
| 531 | 789 |
| 532 UndoCompositionAndSelectionStyles(); | 790 UndoCompositionAndSelectionStyles(); |
| 533 } | 791 } |
| 534 | 792 |
| 535 void RenderTextWin::ItemizeLogicalText() { | 793 void RenderTextWin::ItemizeLogicalText() { |
| 536 runs_.clear(); | 794 runs_.clear(); |
| 537 // Make |string_size_|'s height and |common_baseline_| tall enough to draw | 795 // 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. | 796 // often-used characters which are rendered with fonts in the font list. |
| 539 string_size_ = Size(0, font_list().GetHeight()); | 797 string_size_ = Size(0, font_list().GetHeight()); |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 897 size_t position = LayoutIndexToTextIndex(run->range.end()); | 1155 size_t position = LayoutIndexToTextIndex(run->range.end()); |
| 898 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); | 1156 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); |
| 899 return SelectionModel(position, CURSOR_FORWARD); | 1157 return SelectionModel(position, CURSOR_FORWARD); |
| 900 } | 1158 } |
| 901 | 1159 |
| 902 RenderText* RenderText::CreateInstance() { | 1160 RenderText* RenderText::CreateInstance() { |
| 903 return new RenderTextWin; | 1161 return new RenderTextWin; |
| 904 } | 1162 } |
| 905 | 1163 |
| 906 } // namespace gfx | 1164 } // namespace gfx |
| OLD | NEW |