| OLD | NEW | 
|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "chrome/browser/android/vr_shell/font_fallback.h" | 5 #include "chrome/browser/android/vr_shell/font_fallback.h" | 
| 6 | 6 | 
| 7 #include <map> | 7 #include <map> | 
| 8 #include <set> | 8 #include <set> | 
| 9 #include <string> | 9 #include <string> | 
| 10 #include <vector> | 10 #include <vector> | 
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 78 | 78 | 
| 79   void SetLocale(const std::string& locale) { | 79   void SetLocale(const std::string& locale) { | 
| 80     // Store font list for one locale at a time. | 80     // Store font list for one locale at a time. | 
| 81     if (locale != locale_) { | 81     if (locale != locale_) { | 
| 82       font_ids_.clear(); | 82       font_ids_.clear(); | 
| 83       unknown_chars_.clear(); | 83       unknown_chars_.clear(); | 
| 84       locale_ = locale; | 84       locale_ = locale; | 
| 85     } | 85     } | 
| 86   } | 86   } | 
| 87 | 87 | 
| 88   std::string GetFallbackFontNameForChar(UChar32 c) { | 88   bool GetFallbackFontNameForChar(UChar32 c, std::string* font_name) { | 
| 89     if (unknown_chars_.find(c) != unknown_chars_.end()) | 89     if (unknown_chars_.find(c) != unknown_chars_.end()) | 
| 90       return ""; | 90       return false; | 
|  | 91 | 
| 91     for (SkFontID font_id : font_ids_) { | 92     for (SkFontID font_id : font_ids_) { | 
| 92       std::unique_ptr<CachedFont>& font = g_fonts.Get()[font_id]; | 93       std::unique_ptr<CachedFont>& font = g_fonts.Get()[font_id]; | 
| 93       if (font->HasGlyphForCharacter(c)) | 94       if (font->HasGlyphForCharacter(c)) { | 
| 94         return font->GetFontName(); | 95         *font_name = font->GetFontName(); | 
|  | 96         return true; | 
|  | 97       } | 
| 95     } | 98     } | 
| 96     sk_sp<SkFontMgr> font_mgr(SkFontMgr::RefDefault()); | 99     sk_sp<SkFontMgr> font_mgr(SkFontMgr::RefDefault()); | 
| 97     const char* bcp47_locales[] = {locale_.c_str()}; | 100     const char* bcp47_locales[] = {locale_.c_str()}; | 
| 98     sk_sp<SkTypeface> tf(font_mgr->matchFamilyStyleCharacter( | 101     sk_sp<SkTypeface> tf(font_mgr->matchFamilyStyleCharacter( | 
| 99         nullptr, SkFontStyle(), locale_.empty() ? nullptr : bcp47_locales, | 102         nullptr, SkFontStyle(), locale_.empty() ? nullptr : bcp47_locales, | 
| 100         locale_.empty() ? 0 : 1, c)); | 103         locale_.empty() ? 0 : 1, c)); | 
| 101     if (tf) { | 104     if (tf) { | 
| 102       SkFontID font_id = tf->uniqueID(); | 105       SkFontID font_id = tf->uniqueID(); | 
| 103       font_ids_.push_back(font_id); | 106       font_ids_.push_back(font_id); | 
| 104       std::unique_ptr<CachedFont>& cached_font = g_fonts.Get()[font_id]; | 107       std::unique_ptr<CachedFont>& cached_font = g_fonts.Get()[font_id]; | 
| 105       if (!cached_font) | 108       if (!cached_font) | 
| 106         cached_font = CachedFont::CreateForTypeface(tf); | 109         cached_font = CachedFont::CreateForTypeface(tf); | 
| 107       return cached_font->GetFontName(); | 110       *font_name = cached_font->GetFontName(); | 
|  | 111       return true; | 
| 108     } | 112     } | 
| 109     unknown_chars_.insert(c); | 113     unknown_chars_.insert(c); | 
| 110     return std::string(); | 114     return false; | 
| 111   } | 115   } | 
| 112 | 116 | 
| 113  private: | 117  private: | 
| 114   std::string locale_; | 118   std::string locale_; | 
| 115   std::vector<SkFontID> font_ids_; | 119   std::vector<SkFontID> font_ids_; | 
| 116   std::set<UChar32> unknown_chars_; | 120   std::set<UChar32> unknown_chars_; | 
| 117 | 121 | 
| 118   DISALLOW_COPY_AND_ASSIGN(CachedFontSet); | 122   DISALLOW_COPY_AND_ASSIGN(CachedFontSet); | 
| 119 }; | 123 }; | 
| 120 | 124 | 
| 121 base::LazyInstance<CachedFontSet>::Leaky g_cached_font_set = | 125 base::LazyInstance<CachedFontSet>::Leaky g_cached_font_set = | 
| 122     LAZY_INSTANCE_INITIALIZER; | 126     LAZY_INSTANCE_INITIALIZER; | 
| 123 | 127 | 
| 124 bool FontSupportsChar(const gfx::Font& font, UChar32 c) { | 128 bool FontSupportsChar(const gfx::Font& font, UChar32 c) { | 
| 125   sk_sp<SkTypeface> typeface = | 129   sk_sp<SkTypeface> typeface = | 
| 126       static_cast<gfx::PlatformFontLinux*>(font.platform_font())->typeface(); | 130       static_cast<gfx::PlatformFontLinux*>(font.platform_font())->typeface(); | 
| 127   std::unique_ptr<CachedFont>& cached_font = | 131   std::unique_ptr<CachedFont>& cached_font = | 
| 128       g_fonts.Get()[typeface->uniqueID()]; | 132       g_fonts.Get()[typeface->uniqueID()]; | 
| 129   if (!cached_font) | 133   if (!cached_font) | 
| 130     cached_font = CachedFont::CreateForTypeface(std::move(typeface)); | 134     cached_font = CachedFont::CreateForTypeface(std::move(typeface)); | 
| 131   return cached_font->HasGlyphForCharacter(c); | 135   return cached_font->HasGlyphForCharacter(c); | 
| 132 } | 136 } | 
| 133 | 137 | 
| 134 }  // namespace | 138 }  // namespace | 
| 135 | 139 | 
| 136 std::string GetFallbackFontNameForChar(const gfx::Font& default_font, | 140 bool GetFallbackFontNameForChar(const gfx::Font& default_font, | 
| 137                                        UChar32 c, | 141                                 UChar32 c, | 
| 138                                        const std::string& locale) { | 142                                 const std::string& locale, | 
| 139   if (FontSupportsChar(default_font, c)) | 143                                 std::string* font_name) { | 
| 140     return std::string(); | 144   if (FontSupportsChar(default_font, c)) { | 
|  | 145     *font_name = std::string(); | 
|  | 146     return true; | 
|  | 147   } | 
| 141   CachedFontSet& cached_font_set = g_cached_font_set.Get(); | 148   CachedFontSet& cached_font_set = g_cached_font_set.Get(); | 
| 142   cached_font_set.SetLocale(locale); | 149   cached_font_set.SetLocale(locale); | 
| 143   return cached_font_set.GetFallbackFontNameForChar(c); | 150   return cached_font_set.GetFallbackFontNameForChar(c, font_name); | 
| 144 } | 151 } | 
| 145 | 152 | 
| 146 }  // namespace vr_shell | 153 }  // namespace vr_shell | 
| OLD | NEW | 
|---|