OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_harfbuzz.h" | 5 #include "ui/gfx/render_text_harfbuzz.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "ui/gfx/font.h" | 29 #include "ui/gfx/font.h" |
30 #include "ui/gfx/font_fallback.h" | 30 #include "ui/gfx/font_fallback.h" |
31 #include "ui/gfx/font_render_params.h" | 31 #include "ui/gfx/font_render_params.h" |
32 #include "ui/gfx/geometry/safe_integer_conversions.h" | 32 #include "ui/gfx/geometry/safe_integer_conversions.h" |
33 #include "ui/gfx/harfbuzz_font_skia.h" | 33 #include "ui/gfx/harfbuzz_font_skia.h" |
34 #include "ui/gfx/range/range_f.h" | 34 #include "ui/gfx/range/range_f.h" |
35 #include "ui/gfx/skia_util.h" | 35 #include "ui/gfx/skia_util.h" |
36 #include "ui/gfx/text_utils.h" | 36 #include "ui/gfx/text_utils.h" |
37 #include "ui/gfx/utf16_indexing.h" | 37 #include "ui/gfx/utf16_indexing.h" |
38 | 38 |
| 39 #if defined(OS_WIN) |
| 40 #include "ui/gfx/font_fallback_win.h" |
| 41 #endif |
| 42 |
39 namespace gfx { | 43 namespace gfx { |
40 | 44 |
41 namespace { | 45 namespace { |
42 | 46 |
43 // Text length limit. Longer strings are slow and not fully tested. | 47 // Text length limit. Longer strings are slow and not fully tested. |
44 const size_t kMaxTextLength = 10000; | 48 const size_t kMaxTextLength = 10000; |
45 | 49 |
46 // The maximum number of scripts a Unicode character can belong to. This value | 50 // The maximum number of scripts a Unicode character can belong to. This value |
47 // is arbitrarily chosen to be a good limit because it is unlikely for a single | 51 // is arbitrarily chosen to be a good limit because it is unlikely for a single |
48 // character to belong to more scripts. | 52 // character to belong to more scripts. |
(...skipping 1448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1497 Font best_font(primary_font); | 1501 Font best_font(primary_font); |
1498 FontRenderParams best_render_params; | 1502 FontRenderParams best_render_params; |
1499 size_t best_missing_glyphs = std::numeric_limits<size_t>::max(); | 1503 size_t best_missing_glyphs = std::numeric_limits<size_t>::max(); |
1500 | 1504 |
1501 for (const Font& font : font_list().GetFonts()) { | 1505 for (const Font& font : font_list().GetFonts()) { |
1502 if (CompareFamily(text, font, font.GetFontRenderParams(), run, &best_font, | 1506 if (CompareFamily(text, font, font.GetFontRenderParams(), run, &best_font, |
1503 &best_render_params, &best_missing_glyphs)) | 1507 &best_render_params, &best_missing_glyphs)) |
1504 return; | 1508 return; |
1505 } | 1509 } |
1506 | 1510 |
1507 std::string preferred_fallback_family; | 1511 #if defined(OS_WIN) |
1508 | |
1509 #if defined(OS_WIN) || defined(OS_MACOSX) | |
1510 Font fallback_font(primary_font); | 1512 Font fallback_font(primary_font); |
| 1513 std::string fallback_family; |
1511 const base::char16* run_text = &(text[run->range.start()]); | 1514 const base::char16* run_text = &(text[run->range.start()]); |
1512 if (GetFallbackFont(primary_font, run_text, run->range.length(), | 1515 if (GetFallbackFont(primary_font, run_text, run->range.length(), |
1513 &fallback_font)) { | 1516 &fallback_font)) { |
1514 preferred_fallback_family = fallback_font.GetFontName(); | 1517 fallback_family = fallback_font.GetFontName(); |
1515 if (CompareFamily(text, fallback_font, fallback_font.GetFontRenderParams(), | 1518 if (CompareFamily(text, fallback_font, fallback_font.GetFontRenderParams(), |
1516 run, &best_font, &best_render_params, | 1519 run, &best_font, &best_render_params, |
1517 &best_missing_glyphs)) | 1520 &best_missing_glyphs)) |
1518 return; | 1521 return; |
1519 } | 1522 } |
1520 #endif | 1523 #endif |
1521 | 1524 |
1522 std::vector<Font> fallback_font_list = GetFallbackFonts(primary_font); | 1525 std::vector<Font> fallback_font_list = GetFallbackFonts(primary_font); |
1523 | 1526 |
1524 #if defined(OS_WIN) | 1527 #if defined(OS_WIN) |
1525 // Append fonts in the fallback list of the preferred fallback font. | 1528 // Append fonts in the fallback list of the fallback font. |
1526 // TODO(tapted): Investigate whether there's a case that benefits from this on | 1529 if (!fallback_family.empty()) { |
1527 // Mac. | |
1528 if (!preferred_fallback_family.empty()) { | |
1529 std::vector<Font> fallback_fonts = GetFallbackFonts(fallback_font); | 1530 std::vector<Font> fallback_fonts = GetFallbackFonts(fallback_font); |
1530 fallback_font_list.insert(fallback_font_list.end(), fallback_fonts.begin(), | 1531 fallback_font_list.insert(fallback_font_list.end(), fallback_fonts.begin(), |
1531 fallback_fonts.end()); | 1532 fallback_fonts.end()); |
1532 } | 1533 } |
1533 | 1534 |
1534 // Add Segoe UI and its associated linked fonts to the fallback font list to | 1535 // Add Segoe UI and its associated linked fonts to the fallback font list to |
1535 // ensure that the fallback list covers the basic cases. | 1536 // ensure that the fallback list covers the basic cases. |
1536 // http://crbug.com/467459. On some Windows configurations the default font | 1537 // http://crbug.com/467459. On some Windows configurations the default font |
1537 // could be a raster font like System, which would not give us a reasonable | 1538 // could be a raster font like System, which would not give us a reasonable |
1538 // fallback font list. | 1539 // fallback font list. |
1539 if (!base::LowerCaseEqualsASCII(primary_font.GetFontName(), "segoe ui") && | 1540 if (!base::LowerCaseEqualsASCII(primary_font.GetFontName(), "segoe ui") && |
1540 !base::LowerCaseEqualsASCII(preferred_fallback_family, "segoe ui")) { | 1541 !base::LowerCaseEqualsASCII(fallback_family, "segoe ui")) { |
1541 std::vector<Font> default_fallback_families = | 1542 std::vector<Font> default_fallback_families = |
1542 GetFallbackFonts(Font("Segoe UI", 13)); | 1543 GetFallbackFonts(Font("Segoe UI", 13)); |
1543 fallback_font_list.insert(fallback_font_list.end(), | 1544 fallback_font_list.insert(fallback_font_list.end(), |
1544 default_fallback_families.begin(), default_fallback_families.end()); | 1545 default_fallback_families.begin(), default_fallback_families.end()); |
1545 } | 1546 } |
1546 #endif | 1547 #endif |
1547 | 1548 |
1548 // Use a set to track the fallback fonts and avoid duplicate entries. | 1549 // Use a set to track the fallback fonts and avoid duplicate entries. |
1549 std::set<Font, CaseInsensitiveCompare> fallback_fonts; | 1550 std::set<Font, CaseInsensitiveCompare> fallback_fonts; |
1550 | 1551 |
1551 // Try shaping with the fallback fonts. | 1552 // Try shaping with the fallback fonts. |
1552 for (const auto& font : fallback_font_list) { | 1553 for (const auto& font : fallback_font_list) { |
1553 std::string font_name = font.GetFontName(); | 1554 std::string font_name = font.GetFontName(); |
1554 | 1555 |
1555 if (font_name == primary_font.GetFontName() || | 1556 if (font_name == primary_font.GetFontName()) |
1556 font_name == preferred_fallback_family || fallback_fonts.count(font)) { | |
1557 continue; | 1557 continue; |
1558 } | 1558 #if defined(OS_WIN) |
| 1559 if (font_name == fallback_family) |
| 1560 continue; |
| 1561 #endif |
| 1562 if (fallback_fonts.find(font) != fallback_fonts.end()) |
| 1563 continue; |
1559 | 1564 |
1560 fallback_fonts.insert(font); | 1565 fallback_fonts.insert(font); |
1561 | 1566 |
1562 FontRenderParamsQuery query; | 1567 FontRenderParamsQuery query; |
1563 query.families.push_back(font_name); | 1568 query.families.push_back(font_name); |
1564 query.pixel_size = run->font_size; | 1569 query.pixel_size = run->font_size; |
1565 query.style = run->italic ? Font::ITALIC : 0; | 1570 query.style = run->italic ? Font::ITALIC : 0; |
1566 FontRenderParams fallback_render_params = GetFontRenderParams(query, NULL); | 1571 FontRenderParams fallback_render_params = GetFontRenderParams(query, NULL); |
1567 if (CompareFamily(text, font, fallback_render_params, run, &best_font, | 1572 if (CompareFamily(text, font, fallback_render_params, run, &best_font, |
1568 &best_render_params, &best_missing_glyphs)) | 1573 &best_render_params, &best_missing_glyphs)) |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1722 | 1727 |
1723 attribute.strike = run.strike; | 1728 attribute.strike = run.strike; |
1724 attribute.diagonal_strike = run.diagonal_strike; | 1729 attribute.diagonal_strike = run.diagonal_strike; |
1725 decorated_text->attributes.push_back(attribute); | 1730 decorated_text->attributes.push_back(attribute); |
1726 } | 1731 } |
1727 } | 1732 } |
1728 return true; | 1733 return true; |
1729 } | 1734 } |
1730 | 1735 |
1731 } // namespace gfx | 1736 } // namespace gfx |
OLD | NEW |