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 // 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 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
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 Loading... |
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 25 matching lines...) Expand all Loading... |
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. |
441 for (Runs::iterator i(runs.begin()); i != runs.end(); ++i) { | 454 for (Runs::iterator i(runs.begin()); i != runs.end(); ++i) { |
442 const bool reverse_visible_order = (i->is_rtl != base::i18n::IsRTL()); | 455 const bool reverse_visible_order = (i->is_rtl != base::i18n::IsRTL()); |
443 int flags = gfx::Canvas::NO_ELLIPSIS; // We've already elided. | 456 if (reverse_visible_order) |
444 if (reverse_visible_order) { | |
445 std::reverse(i->classifications.begin(), i->classifications.end()); | 457 std::reverse(i->classifications.begin(), i->classifications.end()); |
446 if (i->is_rtl) | |
447 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; | |
448 } | |
449 for (Classifications::const_iterator j(i->classifications.begin()); | 458 for (Classifications::const_iterator j(i->classifications.begin()); |
450 j != i->classifications.end(); ++j) { | 459 j != i->classifications.end(); ++j) { |
451 const int left = | 460 const int left = |
452 mirroring_context_->mirrored_left_coord(x, x + j->pixel_size.width()); | 461 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 | 462 // Align the text runs to a common baseline. |
454 // tops of successive runs. This isn't actually what we want; we want to | 463 const int top = |
455 // align the baselines, but Canvas doesn't currently expose text | 464 y + normal_font_.GetBaseline() - j->render_text->GetBaseline(); |
456 // measurement APIs sufficient to make that happen. The problem here is | 465 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 | 466 j->render_text->SetDisplayRect(rect); |
458 // same font (in bold or normal styles), and thus the same height and same | 467 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(); | 468 x += j->pixel_size.width(); |
475 } | 469 } |
476 } | 470 } |
477 | 471 |
478 return x; | 472 return x; |
479 } | 473 } |
480 | 474 |
481 void OmniboxResultView::Elide(Runs* runs, int remaining_width) const { | 475 void OmniboxResultView::Elide(Runs* runs, int remaining_width) const { |
482 // The complexity of this function is due to edge cases like the following: | 476 // 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 | 477 // 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 | 478 // 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 | 479 // 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 | 480 // 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 | 481 // see the first classification -- it depends on how many subsequent |
488 // classifications follow, and some of those may be in the next run (or | 482 // 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 | 483 // 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 | 484 // 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 | 485 // backward again until we find a string that we can successfully do the |
492 // eliding on. | 486 // eliding on. |
493 bool first_classification = true; | 487 bool first_classification = true; |
494 for (Runs::reverse_iterator i(runs->rbegin()); i != runs->rend(); ++i) { | 488 for (Runs::reverse_iterator i(runs->rbegin()); i != runs->rend(); ++i) { |
495 for (Classifications::reverse_iterator j(i->classifications.rbegin()); | 489 for (Classifications::reverse_iterator j(i->classifications.rbegin()); |
496 j != i->classifications.rend(); ++j) { | 490 j != i->classifications.rend(); ++j) { |
497 if (!first_classification) { | 491 if (!first_classification) { |
498 // For all but the first classification we consider, we need to append | 492 // 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 | 493 // an ellipsis, since there isn't enough room to draw it after this |
500 // classification. | 494 // classification. |
501 j->text += kEllipsis; | 495 j->text += kEllipsis; |
| 496 j->render_text->SetText(j->text); |
502 | 497 |
503 // We also add this classification's width (sans ellipsis) back to the | 498 // 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 | 499 // available width since we want to consider the available space we'll |
505 // have when we draw this classification. | 500 // have when we draw this classification. |
506 remaining_width += j->pixel_size.width(); | 501 remaining_width += j->pixel_size.width(); |
507 } | 502 } |
508 first_classification = false; | 503 first_classification = false; |
509 | 504 |
510 // Can we fit at least an ellipsis? | 505 // Can we fit at least an ellipsis? |
511 string16 elided_text = | 506 string16 elided_text = |
(...skipping 14 matching lines...) Expand all Loading... |
526 // need to bother with this; see note below. | 521 // need to bother with this; see note below. |
527 elided_text = kEllipsis; | 522 elided_text = kEllipsis; |
528 } | 523 } |
529 if (!elided_text.empty()) { | 524 if (!elided_text.empty()) { |
530 // Success. Elide this classification and stop. | 525 // Success. Elide this classification and stop. |
531 j->text = elided_text; | 526 j->text = elided_text; |
532 | 527 |
533 // If we could only fit an ellipsis, then only make it bold if there was | 528 // 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 | 529 // an immediate prior classification in this run that was also bold, or |
535 // it will look orphaned. | 530 // it will look orphaned. |
536 if ((elided_text.length() == 1) && | 531 if ((j->font != &normal_font_) && (elided_text.length() == 1) && |
537 (on_first_classification || | 532 (on_first_classification || |
538 (prior_classification->font == &normal_font_))) | 533 (prior_classification->font == &normal_font_))) { |
539 j->font = &normal_font_; | 534 j->font = &normal_font_; |
| 535 j->render_text->SetFontList(gfx::FontList(*j->font)); |
| 536 } |
540 | 537 |
541 int width = 0; | 538 j->render_text->SetText(elided_text); |
542 int height = 0; | 539 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 | 540 |
547 // Erase any other classifications that come after the elided one. | 541 // Erase any other classifications that come after the elided one. |
548 i->classifications.erase(j.base(), i->classifications.end()); | 542 i->classifications.erase(j.base(), i->classifications.end()); |
549 runs->erase(i.base(), runs->end()); | 543 runs->erase(i.base(), runs->end()); |
550 return; | 544 return; |
551 } | 545 } |
552 | 546 |
553 // We couldn't fit an ellipsis. Move back one classification, | 547 // We couldn't fit an ellipsis. Move back one classification, |
554 // append an ellipsis, and try again. | 548 // append an ellipsis, and try again. |
555 // NOTE: In the edge case that a bold ellipsis doesn't fit but a | 549 // 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 Loading... |
625 int x = GetMirroredXForRect(keyword_text_bounds_); | 619 int x = GetMirroredXForRect(keyword_text_bounds_); |
626 mirroring_context_->Initialize(x, keyword_text_bounds_.width()); | 620 mirroring_context_->Initialize(x, keyword_text_bounds_.width()); |
627 PaintMatch(canvas, *match_.associated_keyword.get(), x); | 621 PaintMatch(canvas, *match_.associated_keyword.get(), x); |
628 } | 622 } |
629 } | 623 } |
630 | 624 |
631 void OmniboxResultView::AnimationProgressed(const ui::Animation* animation) { | 625 void OmniboxResultView::AnimationProgressed(const ui::Animation* animation) { |
632 Layout(); | 626 Layout(); |
633 SchedulePaint(); | 627 SchedulePaint(); |
634 } | 628 } |
OLD | NEW |