This source file includes following definitions.
- QueryFontsFromRegistry
- GetFontNamesFromFilename
- ContainsOnlyDigits
- AppendFont
- QueryLinkedFontsFromRegistry
- GetInstance
- GetLinkedFonts
- ParseFontLinkEntry
- ParseFontFamilyString
- linked_font_index_
- SetNextFont
- NextFont
- GetLinkedFonts
#include "ui/gfx/font_fallback_win.h"
#include <map>
#include "base/memory/singleton.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
#include "ui/gfx/font.h"
namespace gfx {
namespace {
void QueryFontsFromRegistry(std::map<std::string, std::string>* map) {
const wchar_t* kFonts =
L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";
base::win::RegistryValueIterator it(HKEY_LOCAL_MACHINE, kFonts);
for (; it.Valid(); ++it) {
const std::string filename =
StringToLowerASCII(base::WideToUTF8(it.Value()));
(*map)[filename] = base::WideToUTF8(it.Name());
}
}
void GetFontNamesFromFilename(const std::string& filename,
std::map<std::string, std::string>* font_map,
std::vector<std::string>* font_names) {
if (font_map->empty())
QueryFontsFromRegistry(font_map);
std::map<std::string, std::string>::const_iterator it =
font_map->find(StringToLowerASCII(filename));
if (it == font_map->end())
return;
internal::ParseFontFamilyString(it->second, font_names);
}
bool ContainsOnlyDigits(const std::string& text) {
return text.find_first_not_of("0123456789") == base::string16::npos;
}
void AppendFont(const std::string& name, int size, std::vector<Font>* fonts) {
if (fonts->empty() || fonts->back().GetFontName() != name)
fonts->push_back(Font(name, size));
}
void QueryLinkedFontsFromRegistry(const Font& font,
std::map<std::string, std::string>* font_map,
std::vector<Font>* linked_fonts) {
const wchar_t* kSystemLink =
L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontLink\\SystemLink";
base::win::RegKey key;
if (FAILED(key.Open(HKEY_LOCAL_MACHINE, kSystemLink, KEY_READ)))
return;
const std::wstring original_font_name = base::UTF8ToWide(font.GetFontName());
std::vector<std::wstring> values;
if (FAILED(key.ReadValues(original_font_name.c_str(), &values))) {
key.Close();
return;
}
std::string filename;
std::string font_name;
for (size_t i = 0; i < values.size(); ++i) {
internal::ParseFontLinkEntry(
base::WideToUTF8(values[i]), &filename, &font_name);
if (!font_name.empty()) {
AppendFont(font_name, font.GetFontSize(), linked_fonts);
} else if (!filename.empty()) {
std::vector<std::string> font_names;
GetFontNamesFromFilename(filename, font_map, &font_names);
for (size_t i = 0; i < font_names.size(); ++i)
AppendFont(font_names[i], font.GetFontSize(), linked_fonts);
}
}
key.Close();
}
class CachedFontLinkSettings {
public:
static CachedFontLinkSettings* GetInstance();
const std::vector<Font>* GetLinkedFonts(const Font& font);
private:
friend struct DefaultSingletonTraits<CachedFontLinkSettings>;
CachedFontLinkSettings();
virtual ~CachedFontLinkSettings();
std::map<std::string, std::string> cached_system_fonts_;
std::map<std::string, std::vector<Font> > cached_linked_fonts_;
DISALLOW_COPY_AND_ASSIGN(CachedFontLinkSettings);
};
CachedFontLinkSettings* CachedFontLinkSettings::GetInstance() {
return Singleton<CachedFontLinkSettings,
LeakySingletonTraits<CachedFontLinkSettings> >::get();
}
const std::vector<Font>* CachedFontLinkSettings::GetLinkedFonts(
const Font& font) {
const std::string& font_name = font.GetFontName();
std::map<std::string, std::vector<Font> >::const_iterator it =
cached_linked_fonts_.find(font_name);
if (it != cached_linked_fonts_.end())
return &it->second;
cached_linked_fonts_[font_name] = std::vector<Font>();
std::vector<Font>* linked_fonts = &cached_linked_fonts_[font_name];
QueryLinkedFontsFromRegistry(font, &cached_system_fonts_, linked_fonts);
return linked_fonts;
}
CachedFontLinkSettings::CachedFontLinkSettings() {
}
CachedFontLinkSettings::~CachedFontLinkSettings() {
}
}
namespace internal {
void ParseFontLinkEntry(const std::string& entry,
std::string* filename,
std::string* font_name) {
std::vector<std::string> parts;
base::SplitString(entry, ',', &parts);
filename->clear();
font_name->clear();
if (parts.size() > 0)
*filename = parts[0];
if (parts.size() > 1 && !ContainsOnlyDigits(parts[1]))
*font_name = parts[1];
}
void ParseFontFamilyString(const std::string& family,
std::vector<std::string>* font_names) {
base::SplitString(family, '&', font_names);
if (!font_names->empty()) {
const size_t index = font_names->back().find('(');
if (index != std::string::npos) {
font_names->back().resize(index);
base::TrimWhitespace(font_names->back(), base::TRIM_TRAILING,
&font_names->back());
}
}
}
}
LinkedFontsIterator::LinkedFontsIterator(Font font)
: original_font_(font),
next_font_set_(false),
linked_fonts_(NULL),
linked_font_index_(0) {
SetNextFont(original_font_);
}
LinkedFontsIterator::~LinkedFontsIterator() {
}
void LinkedFontsIterator::SetNextFont(Font font) {
next_font_ = font;
next_font_set_ = true;
}
bool LinkedFontsIterator::NextFont(Font* font) {
if (next_font_set_) {
next_font_set_ = false;
current_font_ = next_font_;
*font = current_font_;
return true;
}
if (linked_fonts_ == NULL)
linked_fonts_ = GetLinkedFonts();
if (linked_font_index_ == linked_fonts_->size())
return false;
current_font_ = linked_fonts_->at(linked_font_index_++);
*font = current_font_;
return true;
}
const std::vector<Font>* LinkedFontsIterator::GetLinkedFonts() const {
CachedFontLinkSettings* font_link = CachedFontLinkSettings::GetInstance();
const std::vector<Font>* fonts = font_link->GetLinkedFonts(original_font_);
if (fonts->empty())
fonts = font_link->GetLinkedFonts(current_font_);
return fonts;
}
}