Flutter Engine
font_skia.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "font_skia.h"
18 
19 #include <minikin/MinikinFont.h>
20 
21 #include "third_party/skia/include/core/SkFont.h"
22 
23 namespace txt {
24 namespace {
25 
26 hb_blob_t* GetTable(hb_face_t* face, hb_tag_t tag, void* context) {
27  SkTypeface* typeface = reinterpret_cast<SkTypeface*>(context);
28 
29  const size_t table_size = typeface->getTableSize(tag);
30  if (table_size == 0)
31  return nullptr;
32  void* buffer = malloc(table_size);
33  if (buffer == nullptr)
34  return nullptr;
35 
36  size_t actual_size = typeface->getTableData(tag, 0, table_size, buffer);
37  if (table_size != actual_size) {
38  free(buffer);
39  return nullptr;
40  }
41  return hb_blob_create(reinterpret_cast<char*>(buffer), table_size,
42  HB_MEMORY_MODE_WRITABLE, buffer, free);
43 }
44 
45 } // namespace
46 
47 FontSkia::FontSkia(sk_sp<SkTypeface> typeface)
48  : MinikinFont(typeface->uniqueID()), typeface_(std::move(typeface)) {}
49 
50 FontSkia::~FontSkia() = default;
51 
52 static void FontSkia_SetSkiaFont(sk_sp<SkTypeface> typeface,
53  SkFont* skFont,
54  const minikin::MinikinPaint& paint) {
55  skFont->setTypeface(std::move(typeface));
56  skFont->setLinearMetrics((paint.paintFlags & minikin::LinearTextFlag) != 0);
57  // TODO: set more paint parameters from Minikin
58  skFont->setSize(paint.size);
59 }
60 
61 float FontSkia::GetHorizontalAdvance(uint32_t glyph_id,
62  const minikin::MinikinPaint& paint) const {
63  SkFont skFont;
64  uint16_t glyph16 = glyph_id;
65  SkScalar skWidth;
66  FontSkia_SetSkiaFont(typeface_, &skFont, paint);
67  skFont.getWidths(&glyph16, 1, &skWidth);
68  return skWidth;
69 }
70 
72  uint32_t glyph_id,
73  const minikin::MinikinPaint& paint) const {
74  SkFont skFont;
75  uint16_t glyph16 = glyph_id;
76  SkRect skBounds;
77  FontSkia_SetSkiaFont(typeface_, &skFont, paint);
78  skFont.getWidths(&glyph16, 1, NULL, &skBounds);
79  bounds->mLeft = skBounds.fLeft;
80  bounds->mTop = skBounds.fTop;
81  bounds->mRight = skBounds.fRight;
82  bounds->mBottom = skBounds.fBottom;
83 }
84 
85 hb_face_t* FontSkia::CreateHarfBuzzFace() const {
86  return hb_face_create_for_tables(GetTable, typeface_.get(), 0);
87 }
88 
89 const std::vector<minikin::FontVariation>& FontSkia::GetAxes() const {
90  return variations_;
91 }
92 
93 const sk_sp<SkTypeface>& FontSkia::GetSkTypeface() const {
94  return typeface_;
95 }
96 
97 } // namespace txt
Definition: ref_ptr.h:252
const std::vector< minikin::FontVariation > & GetAxes() const override
Definition: font_skia.cc:89
FontSkia(sk_sp< SkTypeface > typeface)
Definition: font_skia.cc:47
static void FontSkia_SetSkiaFont(sk_sp< SkTypeface > typeface, SkFont *skFont, const minikin::MinikinPaint &paint)
Definition: font_skia.cc:52
void GetBounds(minikin::MinikinRect *bounds, uint32_t glyph_id, const minikin::MinikinPaint &paint) const override
Definition: font_skia.cc:71
const sk_sp< SkTypeface > & GetSkTypeface() const
Definition: font_skia.cc:93
float GetHorizontalAdvance(uint32_t glyph_id, const minikin::MinikinPaint &paint) const override
Definition: font_skia.cc:61
hb_face_t * CreateHarfBuzzFace() const override
Definition: font_skia.cc:85