| 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 // We need an "offset" into the ttf file
18 auto offset = stbtt_GetFontOffsetForIndex(typeface_mapping_->GetMapping(), 0);
19 if (stbtt_InitFont(font_info_.get(), typeface_mapping_->GetMapping(),
20 offset) == 0) {
21 FML_LOG(ERROR) << "Failed to initialize stb font from binary data.";
22 } else {
23 is_valid_ = true;
24 }
25 }
26
27 TypefaceSTB::~TypefaceSTB() = default;
28
29 bool TypefaceSTB::IsValid() const {
30 return is_valid_;
31 }
32
33 std::size_t TypefaceSTB::GetHash() const {
34 if (!IsValid()) {
35 return 0u;
36 }
37 return reinterpret_cast<size_t>(typeface_mapping_->GetMapping());
38 }
39
40 bool TypefaceSTB::IsEqual(const Typeface& other) const {
41 auto stb_other = reinterpret_cast<const TypefaceSTB*>(&other);
42 return stb_other->GetHash() == GetHash();
43 }
44
45 const uint8_t* TypefaceSTB::GetTypefaceFile() const {
46 return typeface_mapping_->GetMapping();
47 }
48
49 const stbtt_fontinfo* TypefaceSTB::GetFontInfo() const {
50 return font_info_.get();
51 }
52
53 } // namespace impeller
typeface_stb.h
impeller::TypefaceSTB::GetFontInfo
const stbtt_fontinfo * GetFontInfo() const
Definition: typeface_stb.cc:49
impeller::TypefaceSTB::GetHash
std::size_t GetHash() const override
Definition: typeface_stb.cc:33
impeller::TypefaceSTB::IsValid
bool IsValid() const override
Definition: typeface_stb.cc:29
impeller::TypefaceSTB
Definition: typeface_stb.h:16
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:20
std
Definition: comparable.h:95
impeller::TypefaceSTB::IsEqual
bool IsEqual(const Typeface &other) const override
Definition: typeface_stb.cc:40
impeller::TypefaceSTB::~TypefaceSTB
~TypefaceSTB() override
impeller::TypefaceSTB::GetTypefaceFile
const uint8_t * GetTypefaceFile() const
Definition: typeface_stb.cc:45
impeller
Definition: aiks_context.cc:10