diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index c496555dc0..4e19aa46f0 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -7,6 +7,7 @@ group("txt") { deps = [ "libs/minikin", + "src", "tests/unittest($host_toolchain)", ] } diff --git a/engine/src/flutter/include/minikin/MinikinFont.h b/engine/src/flutter/include/minikin/MinikinFont.h index 01af786aa6..01bfe46c16 100644 --- a/engine/src/flutter/include/minikin/MinikinFont.h +++ b/engine/src/flutter/include/minikin/MinikinFont.h @@ -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& GetAxes() const = 0; virtual std::shared_ptr createFontWithVariation( diff --git a/engine/src/flutter/libs/minikin/HbFontCache.cpp b/engine/src/flutter/libs/minikin/HbFontCache.cpp index a7647c016c..0de40113a0 100644 --- a/engine/src/flutter/libs/minikin/HbFontCache.cpp +++ b/engine/src/flutter/libs/minikin/HbFontCache.cpp @@ -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(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); diff --git a/engine/src/flutter/src/BUILD.gn b/engine/src/flutter/src/BUILD.gn new file mode 100644 index 0000000000..e10013e470 --- /dev/null +++ b/engine/src/flutter/src/BUILD.gn @@ -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", + ] +} diff --git a/engine/src/flutter/src/font_collection.cc b/engine/src/flutter/src/font_collection.cc new file mode 100644 index 0000000000..d94df028fa --- /dev/null +++ b/engine/src/flutter/src/font_collection.cc @@ -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 + +#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 +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 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(font_style_set->createTypeface(i)); + auto typeface = std::make_shared(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(std::move(fonts)); + return std::make_shared(std::move(minikin_family)); +} + +} // namespace txt diff --git a/engine/src/flutter/src/font_collection.h b/engine/src/flutter/src/font_collection.h new file mode 100644 index 0000000000..d4df45b0e9 --- /dev/null +++ b/engine/src/flutter/src/font_collection.h @@ -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 +#include +#include + +#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 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_ diff --git a/engine/src/flutter/src/font_skia.cc b/engine/src/flutter/src/font_skia.cc new file mode 100644 index 0000000000..a70c391858 --- /dev/null +++ b/engine/src/flutter/src/font_skia.cc @@ -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 + +namespace txt { +namespace { + +hb_blob_t* GetTable(hb_face_t* face, hb_tag_t tag, void* context) { + SkTypeface* typeface = reinterpret_cast(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(buffer), table_size, + HB_MEMORY_MODE_WRITABLE, buffer, free); +} + +} // namespace + +FontSkia::FontSkia(sk_sp typeface) + : MinikinFont(typeface->uniqueID()), typeface_(std::move(typeface)) {} + +FontSkia::~FontSkia() = default; + +static void FontSkia_SetSkiaPaint(sk_sp 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& FontSkia::GetSkTypeface() { + return typeface_; +} + +} // namespace txt diff --git a/engine/src/flutter/src/font_skia.h b/engine/src/flutter/src/font_skia.h new file mode 100644 index 0000000000..788af74ad3 --- /dev/null +++ b/engine/src/flutter/src/font_skia.h @@ -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 +#include +#include +#include "lib/ftl/macros.h" + +namespace txt { + +class FontSkia : public minikin::MinikinFont { + public: + explicit FontSkia(sk_sp 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& GetAxes() const override; + + const sk_sp& GetSkTypeface(); + + private: + sk_sp typeface_; + std::vector variations_; + + FTL_DISALLOW_COPY_AND_ASSIGN(FontSkia); +}; + +} // namespace txt