| Flutter Impeller |

typeface_stb.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/typographer/backends/stb/typeface_stb.h"
6
7 #include <cstring>
8
9 #include "flutter/fml/logging.h"
10
11 namespace impeller {
12
13 // Instantiate a typeface based on a .ttf or other font file
14 TypefaceSTB::TypefaceSTB(std::unique_ptr<fml::Mapping> typeface_mapping)
15 : typeface_mapping_(std::move(typeface_mapping)),
16 font_info_(std::make_unique<stbtt_fontinfo>()),
17 is_valid_(false) {
18 // We need an "offset" into the ttf file
19 auto offset = stbtt_GetFontOffsetForIndex(typeface_mapping_->GetMapping(), 0);
20 if (stbtt_InitFont(font_info_.get(), typeface_mapping_->GetMapping(),
21 offset) == 0) {
22 FML_LOG(ERROR) << "Failed to initialize stb font from binary data.";
23 } else {
24 is_valid_ = true;
25 }
26 }
27
28 TypefaceSTB::~TypefaceSTB() = default;
29
30 bool TypefaceSTB::IsValid() const {
31 return is_valid_;
32 }
33
34 std::size_t TypefaceSTB::GetHash() const {
35 if (!IsValid()) {
36 return 0u;
37 }
38 return reinterpret_cast<size_t>(typeface_mapping_->GetMapping());
39 }
40
41 bool TypefaceSTB::IsEqual(const Typeface& other) const {
42 auto stb_other = reinterpret_cast<const TypefaceSTB*>(&other);
43 return stb_other->GetHash() == GetHash();
44 }
45
46 const uint8_t* TypefaceSTB::GetTypefaceFile() const {
47 return typeface_mapping_->GetMapping();
48 }
49
50 const stbtt_fontinfo* TypefaceSTB::GetFontInfo() const {
51 return font_info_.get();
52 }
53
54 } // namespace impeller
typeface_stb.h
impeller::TypefaceSTB::GetFontInfo
const stbtt_fontinfo * GetFontInfo() const
Definition: typeface_stb.cc:50
impeller::TypefaceSTB::GetHash
std::size_t GetHash() const override
Definition: typeface_stb.cc:34
impeller::TypefaceSTB::IsValid
bool IsValid() const override
Definition: typeface_stb.cc:30
impeller::TypefaceSTB
Definition: typeface_stb.h:15
impeller::TypefaceSTB::TypefaceSTB
TypefaceSTB(std::unique_ptr< fml::Mapping > typeface_mapping)
Definition: typeface_stb.cc:14
impeller::Typeface
A typeface, usually obtained from a font-file, on disk describes the intrinsic properties of the font...
Definition: typeface.h:19
std
Definition: comparable.h:98
impeller::TypefaceSTB::IsEqual
bool IsEqual(const Typeface &other) const override
Definition: typeface_stb.cc:41
impeller::TypefaceSTB::~TypefaceSTB
~TypefaceSTB() override
impeller::TypefaceSTB::GetTypefaceFile
const uint8_t * GetTypefaceFile() const
Definition: typeface_stb.cc:46
impeller
Definition: aiks_context.cc:10