Flutter Engine
The Flutter Engine
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
6
7#include <cstring>
8
9#include "flutter/fml/logging.h"
10
11namespace impeller {
12
13// Instantiate a typeface based on a .ttf or other font file
14TypefaceSTB::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
28
30 return is_valid_;
31}
32
33std::size_t TypefaceSTB::GetHash() const {
34 if (!IsValid()) {
35 return 0u;
36 }
37 return reinterpret_cast<size_t>(typeface_mapping_->GetMapping());
38}
39
40bool TypefaceSTB::IsEqual(const Typeface& other) const {
41 auto stb_other = reinterpret_cast<const TypefaceSTB*>(&other);
42 return stb_other->GetHash() == GetHash();
43}
44
45const uint8_t* TypefaceSTB::GetTypefaceFile() const {
46 return typeface_mapping_->GetMapping();
47}
48
49const stbtt_fontinfo* TypefaceSTB::GetFontInfo() const {
50 return font_info_.get();
51}
52
53} // namespace impeller
bool IsValid() const override
Definition: typeface_stb.cc:29
const stbtt_fontinfo * GetFontInfo() const
Definition: typeface_stb.cc:49
std::size_t GetHash() const override
Definition: typeface_stb.cc:33
TypefaceSTB(std::unique_ptr< fml::Mapping > typeface_mapping)
Definition: typeface_stb.cc:14
bool IsEqual(const Typeface &other) const override
Definition: typeface_stb.cc:40
const uint8_t * GetTypefaceFile() const
Definition: typeface_stb.cc:45
A typeface, usually obtained from a font-file, on disk describes the intrinsic properties of the font...
Definition: typeface.h:18
#define FML_LOG(severity)
Definition: logging.h:82
Definition: ref_ptr.h:256
SeparatedVector2 offset
#define ERROR(message)
Definition: elf_loader.cc:260