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

Side by Side Diff: chrome/browser/ui/views/omnibox/omnibox_result_view.cc

Issue 10692101: Use RenderText directly in omnibox_result_view.cc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // For WinDDK ATL compatibility, these ATL headers must come first. 5 // For WinDDK ATL compatibility, these ATL headers must come first.
6 #include "build/build_config.h" 6 #include "build/build_config.h"
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <atlbase.h> // NOLINT 8 #include <atlbase.h> // NOLINT
9 #include <atlwin.h> // NOLINT 9 #include <atlwin.h> // NOLINT
10 #endif 10 #endif
11 11
12 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h" 12 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h"
13 13
14 #include <algorithm> // NOLINT 14 #include <algorithm> // NOLINT
15 15
16 #include "base/i18n/bidi_line_iterator.h" 16 #include "base/i18n/bidi_line_iterator.h"
17 #include "base/memory/scoped_vector.h"
17 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" 18 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
18 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" 19 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
19 #include "chrome/browser/ui/views/omnibox/omnibox_result_view_model.h" 20 #include "chrome/browser/ui/views/omnibox/omnibox_result_view_model.h"
20 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
21 #include "grit/theme_resources.h" 22 #include "grit/theme_resources.h"
22 #include "ui/base/l10n/l10n_util.h" 23 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/native_theme/native_theme.h" 24 #include "ui/base/native_theme/native_theme.h"
24 #include "ui/base/resource/resource_bundle.h" 25 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/base/text/text_elider.h" 26 #include "ui/base/text/text_elider.h"
26 #include "ui/gfx/canvas.h" 27 #include "ui/gfx/canvas.h"
27 #include "ui/gfx/color_utils.h" 28 #include "ui/gfx/color_utils.h"
29 #include "ui/gfx/render_text.h"
28 30
29 namespace { 31 namespace {
30 32
31 const char16 kEllipsis[] = { 0x2026, 0x0 }; 33 const char16 kEllipsis[] = { 0x2026, 0x0 };
32 34
33 // The minimum distance between the top and bottom of the {icon|text} and the 35 // The minimum distance between the top and bottom of the {icon|text} and the
34 // top or bottom of the row. 36 // top or bottom of the row.
35 const int kMinimumIconVerticalPadding = 2; 37 const int kMinimumIconVerticalPadding = 2;
36 const int kMinimumTextVerticalPadding = 3; 38 const int kMinimumTextVerticalPadding = 3;
37 39
38 } // namespace 40 } // namespace
39 41
40 //////////////////////////////////////////////////////////////////////////////// 42 ////////////////////////////////////////////////////////////////////////////////
41 // OmniboxResultView, public: 43 // OmniboxResultView, public:
42 44
43 // Precalculated data used to draw the portion of a match classification that 45 // Precalculated data used to draw the portion of a match classification that
44 // fits entirely within one run. 46 // fits entirely within one run.
45 struct OmniboxResultView::ClassificationData { 47 struct OmniboxResultView::ClassificationData {
46 string16 text; 48 string16 text;
47 const gfx::Font* font; 49 const gfx::Font* font;
48 SkColor color; 50 SkColor color;
49 gfx::Size pixel_size; 51 gfx::Size pixel_size;
52 gfx::RenderText* render_text; // Weak.
50 }; 53 };
51 54
52 // Precalculated data used to draw a complete visual run within the match. 55 // Precalculated data used to draw a complete visual run within the match.
53 // This will include all or part of at leasdt one, and possibly several, 56 // This will include all or part of at leasdt one, and possibly several,
54 // classifications. 57 // classifications.
55 struct OmniboxResultView::RunData { 58 struct OmniboxResultView::RunData {
56 size_t run_start; // Offset within the match text where this run begins. 59 size_t run_start; // Offset within the match text where this run begins.
57 int visual_order; // Where this run occurs in visual order. The earliest 60 int visual_order; // Where this run occurs in visual order. The earliest
58 // run drawn is run 0. 61 // run drawn is run 0.
59 bool is_rtl; 62 bool is_rtl;
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 // regardless of locale. 327 // regardless of locale.
325 bool is_url = true; 328 bool is_url = true;
326 for (ACMatchClassifications::const_iterator i(classifications.begin()); 329 for (ACMatchClassifications::const_iterator i(classifications.begin());
327 i != classifications.end(); ++i) { 330 i != classifications.end(); ++i) {
328 if (!(i->style & ACMatchClassification::URL)) { 331 if (!(i->style & ACMatchClassification::URL)) {
329 is_url = false; 332 is_url = false;
330 break; 333 break;
331 } 334 }
332 } 335 }
333 336
334 // Split the text into visual runs. We do this first so that we don't need to 337 // Split the text into visual runs. We do this first so that we don't need to
msw 2012/07/23 15:48:12 We should try to accomplish this styling and elidi
Alexei Svitkine (slow) 2012/07/23 15:59:27 I agree that it would be nice, but currently there
msw 2012/07/23 18:56:08 ok, sgtm.
335 // worry about whether our eliding might change the visual display in 338 // worry about whether our eliding might change the visual display in
336 // unintended ways, e.g. by removing directional markings or by adding an 339 // unintended ways, e.g. by removing directional markings or by adding an
337 // ellipsis that's not enclosed in appropriate markings. 340 // ellipsis that's not enclosed in appropriate markings.
338 base::i18n::BiDiLineIterator bidi_line; 341 base::i18n::BiDiLineIterator bidi_line;
339 if (!bidi_line.Open(text, base::i18n::IsRTL(), is_url)) 342 if (!bidi_line.Open(text, base::i18n::IsRTL(), is_url))
340 return x; 343 return x;
341 const int num_runs = bidi_line.CountRuns(); 344 const int num_runs = bidi_line.CountRuns();
345 ScopedVector<gfx::RenderText> render_texts;
342 Runs runs; 346 Runs runs;
343 for (int run = 0; run < num_runs; ++run) { 347 for (int run = 0; run < num_runs; ++run) {
344 int run_start_int = 0, run_length_int = 0; 348 int run_start_int = 0, run_length_int = 0;
345 // The index we pass to GetVisualRun corresponds to the position of the run 349 // The index we pass to GetVisualRun corresponds to the position of the run
346 // in the displayed text. For example, the string "Google in HEBREW" (where 350 // in the displayed text. For example, the string "Google in HEBREW" (where
347 // HEBREW is text in the Hebrew language) has two runs: "Google in " which 351 // HEBREW is text in the Hebrew language) has two runs: "Google in " which
348 // is an LTR run, and "HEBREW" which is an RTL run. In an LTR context, the 352 // is an LTR run, and "HEBREW" which is an RTL run. In an LTR context, the
349 // run "Google in " has the index 0 (since it is the leftmost run 353 // run "Google in " has the index 0 (since it is the leftmost run
350 // displayed). In an RTL context, the same run has the index 1 because it 354 // displayed). In an RTL context, the same run has the index 1 because it
351 // is the rightmost run. This is why the order in which we traverse the 355 // is the rightmost run. This is why the order in which we traverse the
(...skipping 30 matching lines...) Expand all
382 const int style = classifications[i].style; 386 const int style = classifications[i].style;
383 const bool use_bold_font = !!(style & ACMatchClassification::MATCH); 387 const bool use_bold_font = !!(style & ACMatchClassification::MATCH);
384 current_data->font = &(use_bold_font ? bold_font_ : normal_font_); 388 current_data->font = &(use_bold_font ? bold_font_ : normal_font_);
385 const ResultViewState state = GetState(); 389 const ResultViewState state = GetState();
386 if (style & ACMatchClassification::URL) 390 if (style & ACMatchClassification::URL)
387 current_data->color = GetColor(state, URL); 391 current_data->color = GetColor(state, URL);
388 else if (style & ACMatchClassification::DIM) 392 else if (style & ACMatchClassification::DIM)
389 current_data->color = GetColor(state, DIMMED_TEXT); 393 current_data->color = GetColor(state, DIMMED_TEXT);
390 else 394 else
391 current_data->color = GetColor(state, force_dim ? DIMMED_TEXT : TEXT); 395 current_data->color = GetColor(state, force_dim ? DIMMED_TEXT : TEXT);
392 int width = 0; 396
393 int height = 0; 397 render_texts.push_back(gfx::RenderText::CreateInstance());
394 gfx::Canvas::SizeStringInt(current_data->text, *current_data->font, 398 current_data->render_text = render_texts.back();
395 &width, &height, gfx::Canvas::NO_ELLIPSIS); 399 current_data->render_text->SetFontList(
396 current_data->pixel_size = gfx::Size(width, height); 400 gfx::FontList(*current_data->font));
397 current_run->pixel_width += width; 401 current_data->render_text->SetText(current_data->text);
402
403 gfx::StyleRange style_range;
404 style_range.foreground = current_data->color;
405 style_range.font_style = current_data->font->GetStyle();
406 current_data->render_text->set_default_style(style_range);
407 current_data->render_text->ApplyDefaultStyle();
408
409 current_data->pixel_size = current_data->render_text->GetStringSize();
410 current_run->pixel_width += current_data->pixel_size.width();
398 } 411 }
399 DCHECK(!current_run->classifications.empty()); 412 DCHECK(!current_run->classifications.empty());
400 } 413 }
401 DCHECK(!runs.empty()); 414 DCHECK(!runs.empty());
402 415
403 // Sort into logical order so we can elide logically. 416 // Sort into logical order so we can elide logically.
404 std::sort(runs.begin(), runs.end(), &SortRunsLogically); 417 std::sort(runs.begin(), runs.end(), &SortRunsLogically);
405 418
406 // Now determine what to elide, if anything. Several subtle points: 419 // Now determine what to elide, if anything. Several subtle points:
407 // * Because we have the run data, we can get edge cases correct, like 420 // * Because we have the run data, we can get edge cases correct, like
(...skipping 23 matching lines...) Expand all
431 } 444 }
432 break; 445 break;
433 } 446 }
434 remaining_width -= i->pixel_width; 447 remaining_width -= i->pixel_width;
435 } 448 }
436 449
437 // Sort back into visual order so we can display the runs correctly. 450 // Sort back into visual order so we can display the runs correctly.
438 std::sort(runs.begin(), runs.end(), &SortRunsVisually); 451 std::sort(runs.begin(), runs.end(), &SortRunsVisually);
439 452
440 // Draw the runs. 453 // Draw the runs.
454 const int text_height = GetTextHeight();
msw 2012/07/23 18:56:08 nit: remove unused local variable.
Alexei Svitkine (slow) 2012/07/23 19:38:12 Done.
441 for (Runs::iterator i(runs.begin()); i != runs.end(); ++i) { 455 for (Runs::iterator i(runs.begin()); i != runs.end(); ++i) {
442 const bool reverse_visible_order = (i->is_rtl != base::i18n::IsRTL()); 456 const bool reverse_visible_order = (i->is_rtl != base::i18n::IsRTL());
443 int flags = gfx::Canvas::NO_ELLIPSIS; // We've already elided. 457 if (reverse_visible_order)
444 if (reverse_visible_order) {
445 std::reverse(i->classifications.begin(), i->classifications.end()); 458 std::reverse(i->classifications.begin(), i->classifications.end());
446 if (i->is_rtl)
447 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY;
Peter Kasting 2012/07/12 17:51:48 It worries me that you have no equivalent of this
Alexei Svitkine (slow) 2012/07/12 17:58:02 RenderText will use RTL directionality in the pres
Peter Kasting 2012/07/12 18:05:14 We may get bad results for URLs with Hebrew charac
msw 2012/07/23 15:48:12 I'll write a patch to force base LTR/RTL with a fl
448 }
449 for (Classifications::const_iterator j(i->classifications.begin()); 459 for (Classifications::const_iterator j(i->classifications.begin());
450 j != i->classifications.end(); ++j) { 460 j != i->classifications.end(); ++j) {
451 const int left = 461 const int left =
452 mirroring_context_->mirrored_left_coord(x, x + j->pixel_size.width()); 462 mirroring_context_->mirrored_left_coord(x, x + j->pixel_size.width());
453 // By passing the same y-coordinate for each run, we vertically align the 463 // Align the text runs to a common baseline.
454 // tops of successive runs. This isn't actually what we want; we want to 464 const int top =
455 // align the baselines, but Canvas doesn't currently expose text 465 y + normal_font_.GetBaseline() - j->render_text->GetBaseline();
456 // measurement APIs sufficient to make that happen. The problem here is 466 gfx::Rect rect(left, top, j->pixel_size.width(), j->pixel_size.height());
457 // font substitution: if no fonts are substituted, then all runs have the 467 j->render_text->SetDisplayRect(rect);
458 // same font (in bold or normal styles), and thus the same height and same 468 j->render_text->Draw(canvas);
459 // baseline. If fonts are substituted within a run, the characters are
460 // baseline-aligned within the run, but using the same top coordinate as
461 // for other runs is only correct if the overall ascent for this run is
462 // the same as for other runs -- that is, if the tallest ascent of all
463 // fonts in the run is equal to the ascent of the normal font. If this
464 // condition doesn't hold, the baseline for this run will be drawn too
465 // high or too low, depending on whether the run's tallest ascent is
466 // shorter or higher than the normal font's ascent, respectively.
467 //
468 // TODO(asvitkine): Fix this by replacing the SizeStringInt() calls
469 // elsewhere in this file with calls that can calculate actual baselines
470 // even in the face of font fallback. Tracked as: http://crbug.com/128027
471 canvas->DrawStringInt(j->text, *j->font, j->color, left, y,
472 j->pixel_size.width(), j->pixel_size.height(),
473 flags);
474 x += j->pixel_size.width(); 469 x += j->pixel_size.width();
475 } 470 }
476 } 471 }
477 472
478 return x; 473 return x;
479 } 474 }
480 475
481 void OmniboxResultView::Elide(Runs* runs, int remaining_width) const { 476 void OmniboxResultView::Elide(Runs* runs, int remaining_width) const {
482 // The complexity of this function is due to edge cases like the following: 477 // The complexity of this function is due to edge cases like the following:
483 // We have 100 px of available space, an initial classification that takes 86 478 // We have 100 px of available space, an initial classification that takes 86
484 // px, and a font that has a 15 px wide ellipsis character. Now if the first 479 // px, and a font that has a 15 px wide ellipsis character. Now if the first
485 // classification is followed by several very narrow classifications (e.g. 3 480 // classification is followed by several very narrow classifications (e.g. 3
486 // px wide each), we don't know whether we need to elide or not at the time we 481 // px wide each), we don't know whether we need to elide or not at the time we
487 // see the first classification -- it depends on how many subsequent 482 // see the first classification -- it depends on how many subsequent
488 // classifications follow, and some of those may be in the next run (or 483 // classifications follow, and some of those may be in the next run (or
489 // several runs!). This is why instead we let our caller move forward until 484 // several runs!). This is why instead we let our caller move forward until
490 // we know we definitely need to elide, and then in this function we move 485 // we know we definitely need to elide, and then in this function we move
491 // backward again until we find a string that we can successfully do the 486 // backward again until we find a string that we can successfully do the
492 // eliding on. 487 // eliding on.
493 bool first_classification = true; 488 bool first_classification = true;
494 for (Runs::reverse_iterator i(runs->rbegin()); i != runs->rend(); ++i) { 489 for (Runs::reverse_iterator i(runs->rbegin()); i != runs->rend(); ++i) {
495 for (Classifications::reverse_iterator j(i->classifications.rbegin()); 490 for (Classifications::reverse_iterator j(i->classifications.rbegin());
496 j != i->classifications.rend(); ++j) { 491 j != i->classifications.rend(); ++j) {
497 if (!first_classification) { 492 if (!first_classification) {
498 // For all but the first classification we consider, we need to append 493 // For all but the first classification we consider, we need to append
499 // an ellipsis, since there isn't enough room to draw it after this 494 // an ellipsis, since there isn't enough room to draw it after this
500 // classification. 495 // classification.
501 j->text += kEllipsis; 496 j->text += kEllipsis;
497 j->render_text->SetText(j->text);
502 498
503 // We also add this classification's width (sans ellipsis) back to the 499 // We also add this classification's width (sans ellipsis) back to the
504 // available width since we want to consider the available space we'll 500 // available width since we want to consider the available space we'll
505 // have when we draw this classification. 501 // have when we draw this classification.
506 remaining_width += j->pixel_size.width(); 502 remaining_width += j->pixel_size.width();
507 } 503 }
508 first_classification = false; 504 first_classification = false;
509 505
510 // Can we fit at least an ellipsis? 506 // Can we fit at least an ellipsis?
511 string16 elided_text = 507 string16 elided_text =
(...skipping 14 matching lines...) Expand all
526 // need to bother with this; see note below. 522 // need to bother with this; see note below.
527 elided_text = kEllipsis; 523 elided_text = kEllipsis;
528 } 524 }
529 if (!elided_text.empty()) { 525 if (!elided_text.empty()) {
530 // Success. Elide this classification and stop. 526 // Success. Elide this classification and stop.
531 j->text = elided_text; 527 j->text = elided_text;
532 528
533 // If we could only fit an ellipsis, then only make it bold if there was 529 // If we could only fit an ellipsis, then only make it bold if there was
534 // an immediate prior classification in this run that was also bold, or 530 // an immediate prior classification in this run that was also bold, or
535 // it will look orphaned. 531 // it will look orphaned.
536 if ((elided_text.length() == 1) && 532 if ((j->font != &normal_font_) &&
533 (elided_text.length() == 1) &&
msw 2012/07/23 18:56:08 optional nit: fit on line above.
Alexei Svitkine (slow) 2012/07/23 19:38:12 Done.
537 (on_first_classification || 534 (on_first_classification ||
538 (prior_classification->font == &normal_font_))) 535 (prior_classification->font == &normal_font_))) {
msw 2012/07/23 18:56:08 nit: re-indent by one space.
Alexei Svitkine (slow) 2012/07/23 19:38:12 Done.
539 j->font = &normal_font_; 536 j->font = &normal_font_;
537 j->render_text->SetFontList(gfx::FontList(*j->font));
538 }
540 539
541 int width = 0; 540 j->render_text->SetText(elided_text);
542 int height = 0; 541 j->pixel_size = j->render_text->GetStringSize();
543 gfx::Canvas::SizeStringInt(elided_text, *j->font, &width, &height,
544 gfx::Canvas::NO_ELLIPSIS);
545 j->pixel_size = gfx::Size(width, height);
546 542
547 // Erase any other classifications that come after the elided one. 543 // Erase any other classifications that come after the elided one.
548 i->classifications.erase(j.base(), i->classifications.end()); 544 i->classifications.erase(j.base(), i->classifications.end());
549 runs->erase(i.base(), runs->end()); 545 runs->erase(i.base(), runs->end());
550 return; 546 return;
551 } 547 }
552 548
553 // We couldn't fit an ellipsis. Move back one classification, 549 // We couldn't fit an ellipsis. Move back one classification,
554 // append an ellipsis, and try again. 550 // append an ellipsis, and try again.
555 // NOTE: In the edge case that a bold ellipsis doesn't fit but a 551 // NOTE: In the edge case that a bold ellipsis doesn't fit but a
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
625 int x = GetMirroredXForRect(keyword_text_bounds_); 621 int x = GetMirroredXForRect(keyword_text_bounds_);
626 mirroring_context_->Initialize(x, keyword_text_bounds_.width()); 622 mirroring_context_->Initialize(x, keyword_text_bounds_.width());
627 PaintMatch(canvas, *match_.associated_keyword.get(), x); 623 PaintMatch(canvas, *match_.associated_keyword.get(), x);
628 } 624 }
629 } 625 }
630 626
631 void OmniboxResultView::AnimationProgressed(const ui::Animation* animation) { 627 void OmniboxResultView::AnimationProgressed(const ui::Animation* animation) {
632 Layout(); 628 Layout();
633 SchedulePaint(); 629 SchedulePaint();
634 } 630 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698