Commit b27aab5a authored by Etienne Bergeron's avatar Etienne Bergeron Committed by Commit Bot

Expose the fallback fonts cache and add unittests

This CL is exposing the FontFallbacks cache and is adding some checks
in the current unittests.

The main goal of these refactoring is to add a cache to
GetFallbackFont(..). We are planing to test it the same way.

R=robliao@chromium.org

Bug: 1008531
Change-Id: I1970985ea92c46bc67fd9230803f04a521b0f3e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1829862
Commit-Queue: Etienne Bergeron <etienneb@chromium.org>
Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#701153}
parent 747eab64
......@@ -9,9 +9,7 @@
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/containers/mru_cache.h"
#include "base/lazy_instance.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
......@@ -26,16 +24,6 @@ namespace {
const char kFontFormatTrueType[] = "TrueType";
const char kFontFormatCFF[] = "CFF";
// The fallback cache is a mapping from a font family name to it's potential
// fallback fonts.
using FallbackCache = base::MRUCache<std::string, std::vector<Font>>;
constexpr int kFallbackCacheSize = 64;
FallbackCache* GetFallbackCacheInstance() {
static base::NoDestructor<FallbackCache> fallback_cache(kFallbackCacheSize);
return fallback_cache.get();
}
std::string GetFilenameFromFcPattern(FcPattern* pattern) {
const char* c_filename = nullptr;
if (FcPatternGetString(pattern, FC_FILE, 0,
......@@ -82,6 +70,13 @@ bool IsValidFontFromPattern(FcPattern* pattern) {
} // namespace
FallbackFontsCache* GetFallbackFontsCacheInstance() {
constexpr int kFallbackCacheSize = 64;
static base::NoDestructor<FallbackFontsCache> fallback_cache(
kFallbackCacheSize);
return fallback_cache.get();
}
bool GetFallbackFont(const Font& font,
const std::string& locale,
base::StringPiece16 text,
......@@ -147,7 +142,7 @@ std::vector<Font> GetFallbackFonts(const Font& font) {
std::string font_family = font.GetFontName();
// Lookup in the cache for already processed family.
FallbackCache* font_cache = GetFallbackCacheInstance();
FallbackFontsCache* font_cache = GetFallbackFontsCacheInstance();
auto cached_fallback_fonts = font_cache->Get(font_family);
if (cached_fallback_fonts != font_cache->end()) {
// Already in cache.
......
......@@ -6,13 +6,21 @@
#define UI_GFX_FONT_FALLBACK_LINUX_H_
#include <string>
#include <vector>
#include "base/containers/mru_cache.h"
#include "third_party/icu/source/common/unicode/uchar.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_fallback.h"
#include "ui/gfx/gfx_export.h"
namespace gfx {
// The fallback cache is a mapping from a font family name to its potential
// fallback fonts.
using FallbackFontsCache = base::MRUCache<std::string, std::vector<Font>>;
GFX_EXPORT FallbackFontsCache* GetFallbackFontsCacheInstance();
// Return a font family which provides a glyph for the Unicode code point
// specified by character.
// c: a UTF-32 code point
......
......@@ -15,10 +15,18 @@ namespace {
const char kDefaultApplicationLocale[] = "us-en";
} // namespace
class FontFallbackLinuxTest : public testing::Test {
public:
void SetUp() override {
// Clear the font fallback cache.
GetFallbackFontsCacheInstance()->Clear();
}
};
// If the Type 1 Symbol.pfb font is installed, it is returned as fallback font
// for the PUA character 0xf6db. This test ensures we're not returning Type 1
// fonts as fallback.
TEST(FontFallbackLinuxTest, NoType1InFallbackFonts) {
TEST_F(FontFallbackLinuxTest, NoType1InFallbackFonts) {
FallbackFontData font_fallback_data =
GetFallbackFontForChar(0xf6db, std::string());
if (font_fallback_data.filename.length() >= 3u) {
......@@ -30,7 +38,7 @@ TEST(FontFallbackLinuxTest, NoType1InFallbackFonts) {
}
}
TEST(FontFallbackLinuxTest, GetFallbackFont) {
TEST_F(FontFallbackLinuxTest, GetFallbackFont) {
Font base_font;
Font fallback_font_cjk;
......@@ -44,16 +52,24 @@ TEST(FontFallbackLinuxTest, GetFallbackFont) {
EXPECT_EQ(fallback_font_khmer.GetFontName(), "Noto Sans Khmer");
}
TEST(FontFallbackLinuxTest, Fallbacks) {
TEST_F(FontFallbackLinuxTest, Fallbacks) {
EXPECT_EQ(0U, GetFallbackFontsCacheInstance()->size());
Font default_font("sans", 13);
std::vector<Font> fallbacks = GetFallbackFonts(default_font);
EXPECT_FALSE(fallbacks.empty());
EXPECT_EQ(1U, GetFallbackFontsCacheInstance()->size());
// The first fallback should be 'DejaVu Sans' which is the default linux
// fonts. The fonts on linux are mock with test_fonts (see
// third_party/tests_font).
if (!fallbacks.empty())
EXPECT_EQ(fallbacks[0].GetFontName(), "DejaVu Sans");
// Second lookup should not increase the cache size.
fallbacks = GetFallbackFonts(default_font);
EXPECT_FALSE(fallbacks.empty());
EXPECT_EQ(1U, GetFallbackFontsCacheInstance()->size());
}
} // namespace gfx
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment