HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
otype_head.hpp
1// Copyright Take Vos 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "otype_utilities.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
10#include <span>
11#include <cstddef>
12
13hi_export_module(hikogui.font.otype_head);
14
15hi_export namespace hi { inline namespace v1 {
16
17[[nodiscard]] inline auto otype_head_parse(std::span<std::byte const> bytes)
18{
19 struct header_type {
20 big_uint16_buf_t major_version;
21 big_uint16_buf_t minor_version;
22 otype_fixed15_16_buf_t font_revision;
23 big_uint32_buf_t check_sum_adjustment;
24 big_uint32_buf_t magic_number;
25 big_uint16_buf_t flags;
26 big_uint16_buf_t units_per_em;
27 big_uint64_buf_t created;
28 big_uint64_buf_t modified;
29 otype_fword_buf_t x_min;
30 otype_fword_buf_t y_min;
31 otype_fword_buf_t x_max;
32 otype_fword_buf_t y_max;
33 big_uint16_buf_t mac_style;
34 big_uint16_buf_t lowest_rec_PPEM;
35 big_int16_buf_t font_direction_hint;
36 big_int16_buf_t index_to_loc_format;
37 big_int16_buf_t glyph_data_format;
38 };
39
40 struct return_type {
41 bool loca_is_offset32;
42 float em_scale;
43 };
44
45 auto const& header = implicit_cast<header_type>(bytes);
46
47 hi_check(*header.major_version == 1 and *header.minor_version == 0, "'head' version is not 1.0");
48 hi_check(*header.magic_number == 0x5f0f3cf5, "'head' magic is not 0x5f0f3cf5");
49
50 // All data to be returned must be copied before it is checked, because the
51 // underlying data may be modified by an external application.
52 auto r = return_type{};
53
54 switch (*header.index_to_loc_format) {
55 case 0:
56 r.loca_is_offset32 = false;
57 break;
58 case 1:
59 r.loca_is_offset32 = true;
60 break;
61 default:
62 throw parse_error("'head' index_to_loc_format must be 0 or 1.");
63 }
64
65 if (auto const units_per_em = *header.units_per_em; units_per_em != 0.0) {
66 r.em_scale = 1.0f / units_per_em;
67 } else {
68 throw parse_error("'head' units_per_em must not be 0.0.");
69 }
70
71 return r;
72}
73
74}} // namespace hi::v1
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20