OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 return RenderText::GetRightSelectionModel(selection, break_type); | 284 return RenderText::GetRightSelectionModel(selection, break_type); |
285 } | 285 } |
286 | 286 |
287 SelectionModel RenderTextWin::LeftEndSelectionModel() { | 287 SelectionModel RenderTextWin::LeftEndSelectionModel() { |
288 if (text().empty()) | 288 if (text().empty()) |
289 return SelectionModel(0, 0, SelectionModel::LEADING); | 289 return SelectionModel(0, 0, SelectionModel::LEADING); |
290 | 290 |
291 EnsureLayout(); | 291 EnsureLayout(); |
292 size_t cursor = base::i18n::IsRTL() ? text().length() : 0; | 292 size_t cursor = base::i18n::IsRTL() ? text().length() : 0; |
293 internal::TextRun* run = runs_[visual_to_logical_[0]]; | 293 internal::TextRun* run = runs_[visual_to_logical_[0]]; |
294 bool rtl = run->script_analysis.fRTL; | 294 size_t caret; |
295 size_t caret = rtl ? run->range.end() - 1 : run->range.start(); | 295 SelectionModel::CaretPlacement placement; |
296 SelectionModel::CaretPlacement placement = | 296 if (run->script_analysis.fRTL) { |
297 rtl ? SelectionModel::TRAILING : SelectionModel::LEADING; | 297 caret = IndexOfAdjacentGrapheme(run->range.end(), false); |
298 placement = SelectionModel::TRAILING; | |
299 } else { | |
300 caret = run->range.start(); | |
301 placement = SelectionModel::LEADING; | |
302 } | |
298 return SelectionModel(cursor, caret, placement); | 303 return SelectionModel(cursor, caret, placement); |
299 } | 304 } |
300 | 305 |
301 SelectionModel RenderTextWin::RightEndSelectionModel() { | 306 SelectionModel RenderTextWin::RightEndSelectionModel() { |
302 if (text().empty()) | 307 if (text().empty()) |
303 return SelectionModel(0, 0, SelectionModel::LEADING); | 308 return SelectionModel(0, 0, SelectionModel::LEADING); |
304 | 309 |
305 EnsureLayout(); | 310 EnsureLayout(); |
306 size_t cursor = base::i18n::IsRTL() ? 0 : text().length(); | 311 size_t cursor = base::i18n::IsRTL() ? 0 : text().length(); |
307 internal::TextRun* run = runs_[visual_to_logical_[runs_.size() - 1]]; | 312 internal::TextRun* run = runs_[visual_to_logical_[runs_.size() - 1]]; |
308 bool rtl = run->script_analysis.fRTL; | 313 size_t caret; |
309 size_t caret = rtl ? run->range.start() : run->range.end() - 1; | 314 SelectionModel::CaretPlacement placement; |
310 SelectionModel::CaretPlacement placement = | 315 if (run->script_analysis.fRTL) { |
311 rtl ? SelectionModel::LEADING : SelectionModel::TRAILING; | 316 caret = run->range.start(); |
317 placement = SelectionModel::LEADING; | |
318 } else { | |
319 caret = IndexOfAdjacentGrapheme(run->range.end(), false); | |
320 placement = SelectionModel::TRAILING; | |
321 } | |
312 return SelectionModel(cursor, caret, placement); | 322 return SelectionModel(cursor, caret, placement); |
313 } | 323 } |
314 | 324 |
315 void RenderTextWin::GetSubstringBounds(size_t from, | 325 void RenderTextWin::GetSubstringBounds(size_t from, |
316 size_t to, | 326 size_t to, |
317 std::vector<Rect>* bounds) { | 327 std::vector<Rect>* bounds) { |
318 DCHECK(!needs_layout_); | 328 DCHECK(!needs_layout_); |
319 ui::Range range(from, to); | 329 ui::Range range(from, to); |
320 DCHECK(ui::Range(0, text().length()).Contains(range)); | 330 DCHECK(ui::Range(0, text().length()).Contains(range)); |
321 Point display_offset(GetUpdatedDisplayOffset()); | 331 Point display_offset(GetUpdatedDisplayOffset()); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
452 } | 462 } |
453 | 463 |
454 size_t byte_length = run->glyph_count * sizeof(WORD); | 464 size_t byte_length = run->glyph_count * sizeof(WORD); |
455 canvas_skia->drawPosText(run->glyphs.get(), byte_length, &pos[0], paint); | 465 canvas_skia->drawPosText(run->glyphs.get(), byte_length, &pos[0], paint); |
456 | 466 |
457 if (run->strike || run->underline) | 467 if (run->strike || run->underline) |
458 DrawTextRunDecorations(canvas_skia, paint, *run, run_x, y); | 468 DrawTextRunDecorations(canvas_skia, paint, *run, run_x, y); |
459 } | 469 } |
460 } | 470 } |
461 | 471 |
462 size_t RenderTextWin::IndexOfAdjacentGrapheme(size_t index, bool next) { | 472 size_t RenderTextWin::IndexOfAdjacentGrapheme(size_t index, bool next) { |
msw
2011/12/03 00:03:58
Eh, this isn't very clean, but it seems to get the
| |
463 EnsureLayout(); | 473 EnsureLayout(); |
474 | |
475 if (text().empty()) | |
476 return 0; | |
477 | |
478 if (index >= text().length()) { | |
479 if (next || index > text().length()) { | |
480 return text().length(); | |
481 } else { | |
482 // The requested |index| is at the end of the text. Use the index of the | |
483 // last character to find the grapheme. | |
484 index = text().length() - 1; | |
485 if (IsCursorablePosition(index)) | |
486 return index; | |
487 } | |
488 } | |
489 | |
464 size_t run_index = GetRunContainingPosition(index); | 490 size_t run_index = GetRunContainingPosition(index); |
465 internal::TextRun* run = run_index < runs_.size() ? runs_[run_index] : NULL; | 491 DCHECK(run_index < runs_.size()); |
466 int start = run ? run->range.start() : 0; | 492 internal::TextRun* run = runs_[run_index]; |
467 int length = run ? run->range.length() : text().length(); | 493 int start = run->range.start(); |
468 int ch = index - start; | 494 int ch = index - start; |
469 WORD cluster = run ? run->logical_clusters[ch] : 0; | |
470 | 495 |
471 if (!next) { | 496 if (!next) { |
497 // If |ch| is the start of the run, use the preceding run, if any. | |
498 if (ch == 0) { | |
499 if (run_index == 0) | |
500 return 0; | |
501 run = runs_[run_index - 1]; | |
502 start = run->range.start(); | |
503 ch = run->range.length(); | |
504 } | |
505 | |
506 // Loop to find the start of the grapheme. | |
507 WORD cluster = run->logical_clusters[ch - 1]; | |
472 do { | 508 do { |
473 ch--; | 509 ch--; |
474 } while (ch >= 0 && run && run->logical_clusters[ch] == cluster); | 510 } while (ch > 0 && run->logical_clusters[ch - 1] == cluster); |
475 } else { | 511 } else { |
476 while (ch < length && run && run->logical_clusters[ch] == cluster) | 512 int length = run->range.length(); |
513 WORD cluster = run->logical_clusters[ch]; | |
514 while (ch < length && run->logical_clusters[ch] == cluster) | |
msw
2011/12/03 00:03:58
nit: Inline |run->range.length()| here and remove
| |
477 ch++; | 515 ch++; |
478 } | 516 } |
479 return std::max(std::min(ch, length) + start, 0); | 517 |
518 return start + ch; | |
480 } | 519 } |
481 | 520 |
482 void RenderTextWin::ItemizeLogicalText() { | 521 void RenderTextWin::ItemizeLogicalText() { |
483 STLDeleteContainerPointers(runs_.begin(), runs_.end()); | 522 STLDeleteContainerPointers(runs_.begin(), runs_.end()); |
484 runs_.clear(); | 523 runs_.clear(); |
485 string_width_ = 0; | 524 string_width_ = 0; |
486 if (text().empty()) | 525 if (text().empty()) |
487 return; | 526 return; |
488 | 527 |
489 const wchar_t* raw_text = text().c_str(); | 528 const wchar_t* raw_text = text().c_str(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
538 } | 577 } |
539 | 578 |
540 void RenderTextWin::LayoutVisualText() { | 579 void RenderTextWin::LayoutVisualText() { |
541 HRESULT hr = E_FAIL; | 580 HRESULT hr = E_FAIL; |
542 base::win::ScopedCreateDC hdc(CreateCompatibleDC(NULL)); | 581 base::win::ScopedCreateDC hdc(CreateCompatibleDC(NULL)); |
543 std::vector<internal::TextRun*>::const_iterator run_iter; | 582 std::vector<internal::TextRun*>::const_iterator run_iter; |
544 for (run_iter = runs_.begin(); run_iter < runs_.end(); ++run_iter) { | 583 for (run_iter = runs_.begin(); run_iter < runs_.end(); ++run_iter) { |
545 internal::TextRun* run = *run_iter; | 584 internal::TextRun* run = *run_iter; |
546 size_t run_length = run->range.length(); | 585 size_t run_length = run->range.length(); |
547 const wchar_t* run_text = &(text()[run->range.start()]); | 586 const wchar_t* run_text = &(text()[run->range.start()]); |
587 bool tried_fallback = false; | |
548 | 588 |
549 // Select the font desired for glyph generation. | 589 // Select the font desired for glyph generation. |
550 SelectObject(hdc, run->font.GetNativeFont()); | 590 SelectObject(hdc, run->font.GetNativeFont()); |
551 | 591 |
552 run->logical_clusters.reset(new WORD[run_length]); | 592 run->logical_clusters.reset(new WORD[run_length]); |
553 run->glyph_count = 0; | 593 run->glyph_count = 0; |
554 // Max glyph guess: http://msdn.microsoft.com/en-us/library/dd368564.aspx | 594 // Max glyph guess: http://msdn.microsoft.com/en-us/library/dd368564.aspx |
555 size_t max_glyphs = static_cast<size_t>(1.5 * run_length + 16); | 595 size_t max_glyphs = static_cast<size_t>(1.5 * run_length + 16); |
556 while (max_glyphs < kMaxGlyphs) { | 596 while (max_glyphs < kMaxGlyphs) { |
557 run->glyphs.reset(new WORD[max_glyphs]); | 597 run->glyphs.reset(new WORD[max_glyphs]); |
558 run->visible_attributes.reset(new SCRIPT_VISATTR[max_glyphs]); | 598 run->visible_attributes.reset(new SCRIPT_VISATTR[max_glyphs]); |
559 hr = ScriptShape(hdc, | 599 hr = ScriptShape(hdc, |
560 &run->script_cache, | 600 &run->script_cache, |
561 run_text, | 601 run_text, |
562 run_length, | 602 run_length, |
563 max_glyphs, | 603 max_glyphs, |
564 &(run->script_analysis), | 604 &(run->script_analysis), |
565 run->glyphs.get(), | 605 run->glyphs.get(), |
566 run->logical_clusters.get(), | 606 run->logical_clusters.get(), |
567 run->visible_attributes.get(), | 607 run->visible_attributes.get(), |
568 &(run->glyph_count)); | 608 &(run->glyph_count)); |
569 if (hr == E_OUTOFMEMORY) { | 609 if (hr == E_OUTOFMEMORY) { |
570 max_glyphs *= 2; | 610 max_glyphs *= 2; |
571 } else if (hr == USP_E_SCRIPT_NOT_IN_FONT) { | 611 } else if (hr == USP_E_SCRIPT_NOT_IN_FONT) { |
572 // TODO(msw): Don't use SCRIPT_UNDEFINED. Apparently Uniscribe can crash | 612 // Only try font fallback if it hasn't yet been attempted for this run. |
573 // on certain surrogate pairs with SCRIPT_UNDEFINED. | 613 if (tried_fallback) { |
574 // See https://bugzilla.mozilla.org/show_bug.cgi?id=341500 | 614 // TODO(msw): Don't use SCRIPT_UNDEFINED. Apparently Uniscribe can |
575 // And http://maxradi.us/documents/uniscribe/ | 615 // crash on certain surrogate pairs with SCRIPT_UNDEFINED. |
576 if (run->script_analysis.eScript == SCRIPT_UNDEFINED) | 616 // See https://bugzilla.mozilla.org/show_bug.cgi?id=341500 |
577 break; | 617 // And http://maxradi.us/documents/uniscribe/ |
618 run->script_analysis.eScript = SCRIPT_UNDEFINED; | |
619 break; | |
620 } | |
578 | 621 |
579 // The run's font doesn't contain the required glyphs, use an alternate. | 622 // The run's font doesn't contain the required glyphs, use an alternate. |
580 if (ChooseFallbackFont(hdc, run->font, run_text, run_length, | 623 if (ChooseFallbackFont(hdc, run->font, run_text, run_length, |
581 &run->font)) { | 624 &run->font)) { |
582 ScriptFreeCache(&run->script_cache); | 625 ScriptFreeCache(&run->script_cache); |
583 SelectObject(hdc, run->font.GetNativeFont()); | 626 SelectObject(hdc, run->font.GetNativeFont()); |
584 } | 627 } |
585 | 628 |
586 run->script_analysis.eScript = SCRIPT_UNDEFINED; | 629 tried_fallback = true; |
587 } else { | 630 } else { |
588 break; | 631 break; |
589 } | 632 } |
590 } | 633 } |
591 DCHECK(SUCCEEDED(hr)); | 634 DCHECK(SUCCEEDED(hr)); |
592 | 635 |
593 if (run->glyph_count > 0) { | 636 if (run->glyph_count > 0) { |
594 run->advance_widths.reset(new int[run->glyph_count]); | 637 run->advance_widths.reset(new int[run->glyph_count]); |
595 run->offsets.reset(new GOFFSET[run->glyph_count]); | 638 run->offsets.reset(new GOFFSET[run->glyph_count]); |
596 hr = ScriptPlace(hdc, | 639 hr = ScriptPlace(hdc, |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
741 internal::TextRun* next = runs_[visual_to_logical_[visual_index + 1]]; | 784 internal::TextRun* next = runs_[visual_to_logical_[visual_index + 1]]; |
742 return next->script_analysis.fRTL ? LastSelectionModelInsideRun(next) : | 785 return next->script_analysis.fRTL ? LastSelectionModelInsideRun(next) : |
743 FirstSelectionModelInsideRun(next); | 786 FirstSelectionModelInsideRun(next); |
744 } | 787 } |
745 | 788 |
746 RenderText* RenderText::CreateRenderText() { | 789 RenderText* RenderText::CreateRenderText() { |
747 return new RenderTextWin; | 790 return new RenderTextWin; |
748 } | 791 } |
749 | 792 |
750 } // namespace gfx | 793 } // namespace gfx |
OLD | NEW |