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