Antkeeper  0.0.1
typeface.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <engine/type/typeface.hpp>
24 #include <stdexcept>
25 #include <ft2build.h>
26 #include FT_FREETYPE_H
27 
28 namespace type {
29 
31  style(style),
32  weight(weight)
33 {}
34 
36  style(typeface_style::normal),
37  weight(400)
38 {}
39 
41 {
42  this->style = style;
43 }
44 
45 void typeface::set_weight(int weight)
46 {
47  this->weight = weight;
48 }
49 
50 } // namespace type
51 
52 template <>
54 {
55  // Init FreeType library object
56  FT_Library library;
57  if (FT_Error error = FT_Init_FreeType(&library))
58  {
59  throw std::runtime_error("Failed to init FreeType library (error code " + std::to_string(error) + ")");
60  }
61 
62  // Read file into file buffer
63  std::unique_ptr<std::vector<FT_Byte>> file_buffer = std::make_unique<std::vector<FT_Byte>>(ctx.size());
64  ctx.read8(reinterpret_cast<std::byte*>(file_buffer->data()), ctx.size());
65 
66  // Load FreeType face from file buffer
67  FT_Face face;
68  if (FT_Error error = FT_New_Memory_Face(library, file_buffer->data(), static_cast<FT_Long>(file_buffer->size()), 0, &face))
69  {
70  FT_Done_FreeType(library);
71  throw deserialize_error("Failed to load FreeType face (error code " + std::to_string(error) + ")");
72  }
73 
74  return std::make_unique<type::ft_typeface>(library, face, std::move(file_buffer));
75 }
An exception of this type is thrown when an error occurs during deserialization.
static std::unique_ptr< T > load(::resource_manager &resource_manager, deserialize_context &ctx)
Loads a resource.
Manages the loading, caching, and saving of resources.
void set_weight(int weight)
Sets the weight of the typeface.
Definition: typeface.cpp:45
void set_style(typeface_style style)
Sets the style of the typeface.
Definition: typeface.cpp:40
typeface()
Creates an empty typeface.
Definition: typeface.cpp:35
Text and typography.
Definition: bitmap-font.cpp:24
typeface_style
Emumerates typeface styles.
Definition: typeface.hpp:33
Provides access to a deserialization state.
virtual std::size_t size() const noexcept=0
Returns the size of the file, in bytes.
virtual std::size_t read8(std::byte *data, std::size_t count) noexcept(false)=0
Reads 8-bit (byte) data.