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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 | 166 |
167 RenderTextWin::~RenderTextWin() { | 167 RenderTextWin::~RenderTextWin() { |
168 STLDeleteContainerPointers(runs_.begin(), runs_.end()); | 168 STLDeleteContainerPointers(runs_.begin(), runs_.end()); |
169 } | 169 } |
170 | 170 |
171 int RenderTextWin::GetStringWidth() { | 171 int RenderTextWin::GetStringWidth() { |
172 EnsureLayout(); | 172 EnsureLayout(); |
173 return string_width_; | 173 return string_width_; |
174 } | 174 } |
175 | 175 |
176 void RenderTextWin::Draw(Canvas* canvas) { | |
177 EnsureLayout(); | |
178 DrawSelection(canvas); | |
179 DrawVisualText(canvas); | |
180 DrawCursor(canvas); | |
181 } | |
182 | |
183 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { | 176 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { |
184 if (text().empty()) | 177 if (text().empty()) |
185 return SelectionModel(); | 178 return SelectionModel(); |
186 | 179 |
187 EnsureLayout(); | 180 EnsureLayout(); |
188 // Find the run that contains the point and adjust the argument location. | 181 // Find the run that contains the point and adjust the argument location. |
189 Point p(ToTextPoint(point)); | 182 Point p(ToTextPoint(point)); |
190 size_t run_index = GetRunContainingPoint(p); | 183 size_t run_index = GetRunContainingPoint(p); |
191 if (run_index == runs_.size()) | 184 if (run_index == runs_.size()) |
192 return (p.x() < 0) ? LeftEndSelectionModel() : RightEndSelectionModel(); | 185 return (p.x() < 0) ? LeftEndSelectionModel() : RightEndSelectionModel(); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 EnsureLayout(); | 305 EnsureLayout(); |
313 size_t cursor = base::i18n::IsRTL() ? 0 : text().length(); | 306 size_t cursor = base::i18n::IsRTL() ? 0 : text().length(); |
314 internal::TextRun* run = runs_[visual_to_logical_[runs_.size() - 1]]; | 307 internal::TextRun* run = runs_[visual_to_logical_[runs_.size() - 1]]; |
315 bool rtl = run->script_analysis.fRTL; | 308 bool rtl = run->script_analysis.fRTL; |
316 size_t caret = rtl ? run->range.start() : run->range.end() - 1; | 309 size_t caret = rtl ? run->range.start() : run->range.end() - 1; |
317 SelectionModel::CaretPlacement placement = | 310 SelectionModel::CaretPlacement placement = |
318 rtl ? SelectionModel::LEADING : SelectionModel::TRAILING; | 311 rtl ? SelectionModel::LEADING : SelectionModel::TRAILING; |
319 return SelectionModel(cursor, caret, placement); | 312 return SelectionModel(cursor, caret, placement); |
320 } | 313 } |
321 | 314 |
322 std::vector<Rect> RenderTextWin::GetSubstringBounds(size_t from, size_t to) { | 315 void RenderTextWin::GetSubstringBounds(size_t from, |
316 size_t to, | |
317 std::vector<Rect>* bounds) { | |
323 DCHECK(!needs_layout_); | 318 DCHECK(!needs_layout_); |
324 ui::Range range(from, to); | 319 ui::Range range(from, to); |
325 DCHECK(ui::Range(0, text().length()).Contains(range)); | 320 DCHECK(ui::Range(0, text().length()).Contains(range)); |
326 Point display_offset(GetUpdatedDisplayOffset()); | 321 Point display_offset(GetUpdatedDisplayOffset()); |
327 std::vector<Rect> bounds; | |
328 HRESULT hr = 0; | 322 HRESULT hr = 0; |
323 bounds->clear(); | |
329 | 324 |
330 // Add a Rect for each run/selection intersection. | 325 // Add a Rect for each run/selection intersection. |
331 // TODO(msw): The bounds should probably not always be leading the range ends. | 326 // TODO(msw): The bounds should probably not always be leading the range ends. |
332 for (size_t i = 0; i < runs_.size(); ++i) { | 327 for (size_t i = 0; i < runs_.size(); ++i) { |
333 internal::TextRun* run = runs_[visual_to_logical_[i]]; | 328 internal::TextRun* run = runs_[visual_to_logical_[i]]; |
334 ui::Range intersection = run->range.Intersect(range); | 329 ui::Range intersection = run->range.Intersect(range); |
335 if (intersection.IsValid()) { | 330 if (intersection.IsValid()) { |
336 DCHECK(!intersection.is_reversed()); | 331 DCHECK(!intersection.is_reversed()); |
337 int start_offset = 0; | 332 int start_offset = 0; |
338 hr = ScriptCPtoX(intersection.start() - run->range.start(), | 333 hr = ScriptCPtoX(intersection.start() - run->range.start(), |
(...skipping 18 matching lines...) Expand all Loading... | |
357 &end_offset); | 352 &end_offset); |
358 DCHECK(SUCCEEDED(hr)); | 353 DCHECK(SUCCEEDED(hr)); |
359 if (start_offset > end_offset) | 354 if (start_offset > end_offset) |
360 std::swap(start_offset, end_offset); | 355 std::swap(start_offset, end_offset); |
361 Rect rect(run->preceding_run_widths + start_offset, 0, | 356 Rect rect(run->preceding_run_widths + start_offset, 0, |
362 end_offset - start_offset, run->font.GetHeight()); | 357 end_offset - start_offset, run->font.GetHeight()); |
363 // Center the rect vertically in the display area. | 358 // Center the rect vertically in the display area. |
364 rect.Offset(0, (display_rect().height() - rect.height()) / 2); | 359 rect.Offset(0, (display_rect().height() - rect.height()) / 2); |
365 rect.set_origin(ToViewPoint(rect.origin())); | 360 rect.set_origin(ToViewPoint(rect.origin())); |
366 // Union this with the last rect if they're adjacent. | 361 // Union this with the last rect if they're adjacent. |
367 if (!bounds.empty() && rect.SharesEdgeWith(bounds.back())) { | 362 if (!bounds->empty() && rect.SharesEdgeWith(bounds->back())) { |
368 rect = rect.Union(bounds.back()); | 363 rect = rect.Union(bounds->back()); |
369 bounds.pop_back(); | 364 bounds->pop_back(); |
370 } | 365 } |
371 bounds.push_back(rect); | 366 bounds->push_back(rect); |
372 } | 367 } |
373 } | 368 } |
374 return bounds; | 369 return; |
msw
2011/11/30 00:20:37
Just remove this.
xji
2011/11/30 01:17:17
Duh. Removed.
| |
375 } | 370 } |
376 | 371 |
377 bool RenderTextWin::IsCursorablePosition(size_t position) { | 372 bool RenderTextWin::IsCursorablePosition(size_t position) { |
378 if (position == 0 || position == text().length()) | 373 if (position == 0 || position == text().length()) |
379 return true; | 374 return true; |
380 | 375 |
381 EnsureLayout(); | 376 EnsureLayout(); |
382 size_t run_index = GetRunContainingPosition(position); | 377 size_t run_index = GetRunContainingPosition(position); |
383 if (run_index >= runs_.size()) | 378 if (run_index >= runs_.size()) |
384 return false; | 379 return false; |
385 | 380 |
386 internal::TextRun* run = runs_[run_index]; | 381 internal::TextRun* run = runs_[run_index]; |
387 size_t start = run->range.start(); | 382 size_t start = run->range.start(); |
388 if (position == start) | 383 if (position == start) |
389 return true; | 384 return true; |
390 return run->logical_clusters[position - start] != | 385 return run->logical_clusters[position - start] != |
391 run->logical_clusters[position - start - 1]; | 386 run->logical_clusters[position - start - 1]; |
392 } | 387 } |
393 | 388 |
394 void RenderTextWin::UpdateLayout() { | 389 void RenderTextWin::UpdateLayout() { |
395 // Layout is performed lazily as needed for drawing/metrics. | 390 // Layout is performed lazily as needed for drawing/metrics. |
396 needs_layout_ = true; | 391 needs_layout_ = true; |
397 } | 392 } |
398 | 393 |
394 void RenderTextWin::EnsureLayout() { | |
395 if (!needs_layout_) | |
396 return; | |
397 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. | |
398 ItemizeLogicalText(); | |
399 if (!runs_.empty()) | |
400 LayoutVisualText(); | |
401 needs_layout_ = false; | |
402 } | |
403 | |
404 void RenderTextWin::DrawVisualText(Canvas* canvas) { | |
405 SkCanvas* canvas_skia = canvas->GetSkCanvas(); | |
406 | |
407 Point offset(ToViewPoint(Point())); | |
408 // TODO(msw): Establish a vertical baseline for strings of mixed font heights. | |
409 size_t height = default_style().font.GetHeight(); | |
410 | |
411 SkScalar x = SkIntToScalar(offset.x()); | |
412 SkScalar y = SkIntToScalar(offset.y()); | |
413 // Center the text vertically in the display area. | |
414 y += (display_rect().height() - height) / 2; | |
415 // Offset by the font size to account for Skia expecting y to be the bottom. | |
416 y += default_style().font.GetFontSize(); | |
417 | |
418 SkPaint paint; | |
419 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
420 paint.setStyle(SkPaint::kFill_Style); | |
421 paint.setAntiAlias(true); | |
422 paint.setSubpixelText(true); | |
423 paint.setLCDRenderText(true); | |
424 | |
425 std::vector<SkPoint> pos; | |
426 for (size_t i = 0; i < runs_.size(); ++i) { | |
427 // Get the run specified by the visual-to-logical map. | |
428 internal::TextRun* run = runs_[visual_to_logical_[i]]; | |
429 | |
430 // TODO(msw): Font default/fallback and style integration. | |
431 SkTypeface::Style style = SkTypeface::kNormal; | |
432 SkTypeface* typeface = | |
433 SkTypeface::CreateFromName(run->font.GetFontName().c_str(), style); | |
434 if (typeface) { | |
435 paint.setTypeface(typeface); | |
436 // |paint| adds its own ref. Release the ref from CreateFromName. | |
437 typeface->unref(); | |
438 } | |
439 paint.setTextSize(run->font.GetFontSize()); | |
440 paint.setColor(run->foreground); | |
441 | |
442 SkScalar run_x = x; | |
443 | |
444 // Based on WebCore::skiaDrawText. | |
445 pos.resize(run->glyph_count); | |
446 for (int glyph = 0; glyph < run->glyph_count; glyph++) { | |
447 pos[glyph].set(x + run->offsets[glyph].du, | |
448 y + run->offsets[glyph].dv); | |
449 x += SkIntToScalar(run->advance_widths[glyph]); | |
450 } | |
451 | |
452 size_t byte_length = run->glyph_count * sizeof(WORD); | |
453 canvas_skia->drawPosText(run->glyphs.get(), byte_length, &pos[0], paint); | |
454 | |
455 if (run->strike || run->underline) | |
456 DrawTextRunDecorations(canvas_skia, paint, *run, run_x, y); | |
457 } | |
458 } | |
459 | |
399 size_t RenderTextWin::IndexOfAdjacentGrapheme(size_t index, bool next) { | 460 size_t RenderTextWin::IndexOfAdjacentGrapheme(size_t index, bool next) { |
400 EnsureLayout(); | 461 EnsureLayout(); |
401 size_t run_index = GetRunContainingPosition(index); | 462 size_t run_index = GetRunContainingPosition(index); |
402 internal::TextRun* run = run_index < runs_.size() ? runs_[run_index] : NULL; | 463 internal::TextRun* run = run_index < runs_.size() ? runs_[run_index] : NULL; |
403 int start = run ? run->range.start() : 0; | 464 int start = run ? run->range.start() : 0; |
404 int length = run ? run->range.length() : text().length(); | 465 int length = run ? run->range.length() : text().length(); |
405 int ch = index - start; | 466 int ch = index - start; |
406 WORD cluster = run ? run->logical_clusters[ch] : 0; | 467 WORD cluster = run ? run->logical_clusters[ch] : 0; |
407 | 468 |
408 if (!next) { | 469 if (!next) { |
409 do { | 470 do { |
410 ch--; | 471 ch--; |
411 } while (ch >= 0 && run && run->logical_clusters[ch] == cluster); | 472 } while (ch >= 0 && run && run->logical_clusters[ch] == cluster); |
412 } else { | 473 } else { |
413 while (ch < length && run && run->logical_clusters[ch] == cluster) | 474 while (ch < length && run && run->logical_clusters[ch] == cluster) |
414 ch++; | 475 ch++; |
415 } | 476 } |
416 return std::max(std::min(ch, length) + start, 0); | 477 return std::max(std::min(ch, length) + start, 0); |
417 } | 478 } |
418 | 479 |
419 void RenderTextWin::EnsureLayout() { | |
420 if (!needs_layout_) | |
421 return; | |
422 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. | |
423 ItemizeLogicalText(); | |
424 if (!runs_.empty()) | |
425 LayoutVisualText(); | |
426 needs_layout_ = false; | |
427 } | |
428 | |
429 void RenderTextWin::ItemizeLogicalText() { | 480 void RenderTextWin::ItemizeLogicalText() { |
430 STLDeleteContainerPointers(runs_.begin(), runs_.end()); | 481 STLDeleteContainerPointers(runs_.begin(), runs_.end()); |
431 runs_.clear(); | 482 runs_.clear(); |
432 string_width_ = 0; | 483 string_width_ = 0; |
433 if (text().empty()) | 484 if (text().empty()) |
434 return; | 485 return; |
435 | 486 |
436 const wchar_t* raw_text = text().c_str(); | 487 const wchar_t* raw_text = text().c_str(); |
437 const int text_length = text().length(); | 488 const int text_length = text().length(); |
438 | 489 |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
683 | 734 |
684 // The character is at the end of its run; go to the next visual run. | 735 // The character is at the end of its run; go to the next visual run. |
685 size_t visual_index = logical_to_visual_[run_index]; | 736 size_t visual_index = logical_to_visual_[run_index]; |
686 if (visual_index == runs_.size() - 1) | 737 if (visual_index == runs_.size() - 1) |
687 return RightEndSelectionModel(); | 738 return RightEndSelectionModel(); |
688 internal::TextRun* next = runs_[visual_to_logical_[visual_index + 1]]; | 739 internal::TextRun* next = runs_[visual_to_logical_[visual_index + 1]]; |
689 return next->script_analysis.fRTL ? LastSelectionModelInsideRun(next) : | 740 return next->script_analysis.fRTL ? LastSelectionModelInsideRun(next) : |
690 FirstSelectionModelInsideRun(next); | 741 FirstSelectionModelInsideRun(next); |
691 } | 742 } |
692 | 743 |
693 void RenderTextWin::DrawSelection(Canvas* canvas) { | |
694 std::vector<Rect> sel( | |
695 GetSubstringBounds(GetSelectionStart(), GetCursorPosition())); | |
696 SkColor color = focused() ? kFocusedSelectionColor : kUnfocusedSelectionColor; | |
697 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | |
698 canvas->FillRect(color, *i); | |
699 } | |
700 | |
701 void RenderTextWin::DrawVisualText(Canvas* canvas) { | |
702 if (text().empty()) | |
703 return; | |
704 | |
705 SkCanvas* canvas_skia = canvas->GetSkCanvas(); | |
706 | |
707 Point offset(ToViewPoint(Point())); | |
708 // TODO(msw): Establish a vertical baseline for strings of mixed font heights. | |
709 size_t height = default_style().font.GetHeight(); | |
710 | |
711 SkScalar x = SkIntToScalar(offset.x()); | |
712 SkScalar y = SkIntToScalar(offset.y()); | |
713 // Center the text vertically in the display area. | |
714 y += (display_rect().height() - height) / 2; | |
715 // Offset by the font size to account for Skia expecting y to be the bottom. | |
716 y += default_style().font.GetFontSize(); | |
717 | |
718 SkPaint paint; | |
719 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
720 paint.setStyle(SkPaint::kFill_Style); | |
721 paint.setAntiAlias(true); | |
722 paint.setSubpixelText(true); | |
723 paint.setLCDRenderText(true); | |
724 | |
725 std::vector<SkPoint> pos; | |
726 for (size_t i = 0; i < runs_.size(); ++i) { | |
727 // Get the run specified by the visual-to-logical map. | |
728 internal::TextRun* run = runs_[visual_to_logical_[i]]; | |
729 | |
730 // TODO(msw): Font default/fallback and style integration. | |
731 SkTypeface::Style style = SkTypeface::kNormal; | |
732 SkTypeface* typeface = | |
733 SkTypeface::CreateFromName(run->font.GetFontName().c_str(), style); | |
734 if (typeface) { | |
735 paint.setTypeface(typeface); | |
736 // |paint| adds its own ref. Release the ref from CreateFromName. | |
737 typeface->unref(); | |
738 } | |
739 paint.setTextSize(run->font.GetFontSize()); | |
740 paint.setColor(run->foreground); | |
741 | |
742 SkScalar run_x = x; | |
743 | |
744 // Based on WebCore::skiaDrawText. | |
745 pos.resize(run->glyph_count); | |
746 for (int glyph = 0; glyph < run->glyph_count; glyph++) { | |
747 pos[glyph].set(x + run->offsets[glyph].du, | |
748 y + run->offsets[glyph].dv); | |
749 x += SkIntToScalar(run->advance_widths[glyph]); | |
750 } | |
751 | |
752 size_t byte_length = run->glyph_count * sizeof(WORD); | |
753 canvas_skia->drawPosText(run->glyphs.get(), byte_length, &pos[0], paint); | |
754 | |
755 if (run->strike || run->underline) | |
756 DrawTextRunDecorations(canvas_skia, paint, *run, run_x, y); | |
757 } | |
758 } | |
759 | |
760 void RenderTextWin::DrawCursor(Canvas* canvas) { | |
761 // Paint cursor. Replace cursor is drawn as rectangle for now. | |
762 // TODO(msw): Draw a better cursor with a better indication of association. | |
763 if (cursor_visible() && focused()) { | |
764 Rect r(GetUpdatedCursorBounds()); | |
765 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); | |
766 } | |
767 } | |
768 | |
769 RenderText* RenderText::CreateRenderText() { | 744 RenderText* RenderText::CreateRenderText() { |
770 return new RenderTextWin; | 745 return new RenderTextWin; |
771 } | 746 } |
772 | 747 |
773 } // namespace gfx | 748 } // namespace gfx |
OLD | NEW |