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/module.hpp"
9#include <span>
10#include <cstddef>
11
12namespace hi { inline namespace v1 {
13
14[[nodiscard]] inline auto otype_head_parse(std::span<std::byte const> bytes)
15{
16 struct header_type {
17 big_uint16_buf_t major_version;
18 big_uint16_buf_t minor_version;
19 otype_fixed15_16_buf_t font_revision;
20 big_uint32_buf_t check_sum_adjustment;
21 big_uint32_buf_t magic_number;
22 big_uint16_buf_t flags;
23 big_uint16_buf_t units_per_em;
24 big_uint64_buf_t created;
25 big_uint64_buf_t modified;
26 otype_fword_buf_t x_min;
27 otype_fword_buf_t y_min;
28 otype_fword_buf_t x_max;
29 otype_fword_buf_t y_max;
30 big_uint16_buf_t mac_style;
31 big_uint16_buf_t lowest_rec_PPEM;
32 big_int16_buf_t font_direction_hint;
33 big_int16_buf_t index_to_loc_format;
34 big_int16_buf_t glyph_data_format;
35 };
36
37 struct return_type {
38 bool loca_is_offset32;
39 float em_scale;
40 };
41
42 hilet& header = implicit_cast<header_type>(bytes);
43
44 hi_check(*header.major_version == 1 and *header.minor_version == 0, "'head' version is not 1.0");
45 hi_check(*header.magic_number == 0x5f0f3cf5, "'head' magic is not 0x5f0f3cf5");
46
47 // All data to be returned must be copied before it is checked, because the
48 // underlying data may be modified by an external application.
49 auto r = return_type{};
50
51 switch (*header.index_to_loc_format) {
52 case 0:
53 r.loca_is_offset32 = false;
54 break;
55 case 1:
56 r.loca_is_offset32 = true;
57 break;
58 default:
59 throw parse_error("'head' index_to_loc_format must be 0 or 1.");
60 }
61
62 if (hilet units_per_em = *header.units_per_em; units_per_em != 0.0) {
63 r.em_scale = 1.0f / units_per_em;
64 } else {
65 throw parse_error("'head' units_per_em must not be 0.0.");
66 }
67
68 return r;
69}
70
71}} // namespace hi::v1
#define hi_check(expression, message,...)
Check if the expression is valid, or throw a parse_error.
Definition assert.hpp:110
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11