Add SkFontMgr backend for FontCollection

Change-Id: Ic58127bb696f87254633b01706c26c4ae862f9be
This commit is contained in:
Adam Barth
2017-05-10 11:49:55 -07:00
parent a2baa5149f
commit a02c2f97e6
8 changed files with 297 additions and 20 deletions

View File

@@ -7,6 +7,7 @@ group("txt") {
deps = [
"libs/minikin",
"src",
"tests/unittest($host_toolchain)",
]
}

View File

@@ -96,22 +96,10 @@ public:
virtual void GetBounds(MinikinRect* bounds, uint32_t glyph_id,
const MinikinPaint &paint) const = 0;
// Override if font can provide access to raw data
virtual const void* GetFontData() const {
virtual hb_face_t* CreateHarfBuzzFace() const {
return nullptr;
}
// Override if font can provide access to raw data
virtual size_t GetFontSize() const {
return 0;
}
// Override if font can provide access to raw data.
// Returns index within OpenType collection
virtual int GetFontIndex() const {
return 0;
}
virtual const std::vector<minikin::FontVariation>& GetAxes() const = 0;
virtual std::shared_ptr<MinikinFont> createFontWithVariation(

View File

@@ -102,13 +102,7 @@ hb_font_t* getHbFontLocked(const MinikinFont* minikinFont) {
return hb_font_reference(font);
}
hb_face_t* face;
const void* buf = minikinFont->GetFontData();
size_t size = minikinFont->GetFontSize();
hb_blob_t* blob = hb_blob_create(reinterpret_cast<const char*>(buf), size,
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
face = hb_face_create(blob, minikinFont->GetFontIndex());
hb_blob_destroy(blob);
hb_face_t* face = minikinFont->CreateHarfBuzzFace();
hb_font_t* parent_font = hb_font_create(face);
hb_ot_font_set_funcs(parent_font);

View File

@@ -0,0 +1,27 @@
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
static_library("src") {
sources = [
"font_collection.cc",
"font_collection.h",
"font_skia.cc",
"font_skia.h",
]
deps = [
"//lib/txt/libs/minikin",
"//third_party/skia",
]
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lib/txt/src/font_collection.h"
#include <mutex>
#include "lib/ftl/logging.h"
#include "lib/txt/src/font_skia.h"
#include "third_party/skia/include/ports/SkFontMgr.h"
namespace txt {
namespace {
bool IsItalic(SkFontStyle::Slant slant) {
return slant != SkFontStyle::Slant::kUpright_Slant;
}
} // namespace
FontCollection& FontCollection::GetDefaultFontCollection() {
static FontCollection* collection = nullptr;
static std::once_flag once;
std::call_once(once, []() { collection = new FontCollection(); });
return *collection;
}
FontCollection::FontCollection() = default;
FontCollection::~FontCollection() = default;
std::shared_ptr<minikin::FontCollection>
FontCollection::GetAndroidFontCollectionForFamily(const std::string& family) {
// Get the Skia font manager.
auto skia_font_manager = SkFontMgr::RefDefault();
FTL_DCHECK(skia_font_manager != nullptr);
// Ask Skia to resolve a font style set for a font family name.
// FIXME(chinmaygarde): The name "Hevetica" is hardcoded because CoreText
// crashes when passed a null string. This seems to be a bug in Skia as
// SkFontMgr explicitly says passing in nullptr gives the default font.
auto font_style_set = skia_font_manager->matchFamily(
family.length() == 0 ? "Helvetica" : family.c_str());
FTL_DCHECK(font_style_set != nullptr);
std::vector<minikin::Font> fonts;
// Add fonts to the Minikin font family.
for (int i = 0, style_count = font_style_set->count(); i < style_count; ++i) {
auto skia_typeface =
sk_ref_sp<SkTypeface>(font_style_set->createTypeface(i));
auto typeface = std::make_shared<FontSkia>(std::move(skia_typeface));
SkFontStyle skia_font_style;
font_style_set->getStyle(i, &skia_font_style, nullptr);
minikin::FontStyle style(skia_font_style.weight(), IsItalic(skia_font_style.slant()));
fonts.push_back(minikin::Font(std::move(typeface), style));
}
auto minikin_family = std::make_shared<minikin::FontFamily>(std::move(fonts));
return std::make_shared<minikin::FontCollection>(std::move(minikin_family));
}
} // namespace txt

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIB_TXT_SRC_FONT_COLLECTION_H_
#define LIB_TXT_SRC_FONT_COLLECTION_H_
#include <memory>
#include <string>
#include <vector>
#include "lib/ftl/macros.h"
#include "lib/txt/include/minikin/FontCollection.h"
#include "lib/txt/include/minikin/FontFamily.h"
namespace txt {
class FontCollection {
public:
static FontCollection& GetDefaultFontCollection();
FontCollection();
~FontCollection();
std::shared_ptr<minikin::FontCollection> GetAndroidFontCollectionForFamily(
const std::string& family);
private:
// TODO(chinmaygarde): Caches go here.
FTL_DISALLOW_COPY_AND_ASSIGN(FontCollection);
};
} // namespace txt
#endif // LIB_TXT_SRC_FONT_COLLECTION_H_

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lib/txt/src/font_skia.h"
#include <minikin/MinikinFont.h>
namespace txt {
namespace {
hb_blob_t* GetTable(hb_face_t* face, hb_tag_t tag, void* context) {
SkTypeface* typeface = reinterpret_cast<SkTypeface*>(context);
const size_t table_size = typeface->getTableSize(tag);
if (table_size == 0)
return nullptr;
void* buffer = malloc(table_size);
if (buffer == nullptr)
return nullptr;
size_t actual_size = typeface->getTableData(tag, 0, table_size, buffer);
if (table_size != actual_size) {
free(buffer);
return nullptr;
}
return hb_blob_create(reinterpret_cast<char*>(buffer), table_size,
HB_MEMORY_MODE_WRITABLE, buffer, free);
}
} // namespace
FontSkia::FontSkia(sk_sp<SkTypeface> typeface)
: MinikinFont(typeface->uniqueID()), typeface_(std::move(typeface)) {}
FontSkia::~FontSkia() = default;
static void FontSkia_SetSkiaPaint(sk_sp<SkTypeface> typeface,
SkPaint* skPaint,
const minikin::MinikinPaint& paint) {
skPaint->setTypeface(std::move(typeface));
skPaint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
// TODO: set more paint parameters from Minikin
skPaint->setTextSize(paint.size);
}
float FontSkia::GetHorizontalAdvance(uint32_t glyph_id,
const minikin::MinikinPaint& paint) const {
SkPaint skPaint;
uint16_t glyph16 = glyph_id;
SkScalar skWidth;
FontSkia_SetSkiaPaint(typeface_, &skPaint, paint);
skPaint.getTextWidths(&glyph16, sizeof(glyph16), &skWidth, NULL);
return skWidth;
}
void FontSkia::GetBounds(minikin::MinikinRect* bounds,
uint32_t glyph_id,
const minikin::MinikinPaint& paint) const {
SkPaint skPaint;
uint16_t glyph16 = glyph_id;
SkRect skBounds;
FontSkia_SetSkiaPaint(typeface_, &skPaint, paint);
skPaint.getTextWidths(&glyph16, sizeof(glyph16), NULL, &skBounds);
bounds->mLeft = skBounds.fLeft;
bounds->mTop = skBounds.fTop;
bounds->mRight = skBounds.fRight;
bounds->mBottom = skBounds.fBottom;
}
hb_face_t* FontSkia::CreateHarfBuzzFace() const {
return hb_face_create_for_tables(GetTable, typeface_.get(), 0);
}
const sk_sp<SkTypeface>& FontSkia::GetSkTypeface() {
return typeface_;
}
} // namespace txt

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <SkPaint.h>
#include <SkTypeface.h>
#include <minikin/MinikinFont.h>
#include "lib/ftl/macros.h"
namespace txt {
class FontSkia : public minikin::MinikinFont {
public:
explicit FontSkia(sk_sp<SkTypeface> typeface);
~FontSkia();
float GetHorizontalAdvance(uint32_t glyph_id,
const minikin::MinikinPaint& paint) const override;
void GetBounds(minikin::MinikinRect* bounds,
uint32_t glyph_id,
const minikin::MinikinPaint& paint) const override;
hb_face_t* CreateHarfBuzzFace() const override;
const std::vector<minikin::FontVariation>& GetAxes() const override;
const sk_sp<SkTypeface>& GetSkTypeface();
private:
sk_sp<SkTypeface> typeface_;
std::vector<minikin::FontVariation> variations_;
FTL_DISALLOW_COPY_AND_ASSIGN(FontSkia);
};
} // namespace txt