1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*
Copyright (C) 2026 Carl Hetherington <cth@carlh.net>
This file is part of libttf.
libttf is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
libttf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libttf. If not, see <http://www.gnu.org/licenses/>.
*/
#include "font.h"
#include "name_table.h"
#include "util.h"
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
using namespace ttf;
Font::Font(boost::filesystem::path const& ttf_file)
{
boost::filesystem::ifstream str(ttf_file, std::ios::binary);
std::vector<uint8_t> data(boost::filesystem::file_size(ttf_file));
str.read(reinterpret_cast<char*>(data.data()), data.size());
Source source(data);
read_source(source);
}
Font::Font(uint8_t const* data, int size)
{
Source source(data, size);
read_source(source);
}
Font::Font(Source& source)
{
read_source(source);
}
void
Font::read_source(Source& source)
{
_scalar_type = source.get_uint32();
if (_scalar_type != 0x00010000 && _scalar_type != 0x74727565) {
throw InvalidScalerType(_scalar_type);
}
_num_tables = source.get_uint16();
auto const search_range = source.get_uint16();
auto const entry_selector = source.get_uint16();
auto const range_shift = source.get_uint16();
for (auto table = 0; table < _num_tables; ++table) {
_tables.push_back(make_table(source, table));
}
std::sort(_tables.begin(), _tables.end(), [](std::shared_ptr<const Table> a, std::shared_ptr<const Table> b) { return a->offset() < b->offset(); });
}
void
Font::write(boost::filesystem::path const& ttf_file) const
{
std::vector<uint8_t> data(65536);
Sink header_sink(data);
header_sink.put(_scalar_type);
header_sink.put(_num_tables);
auto constexpr TABLE_SIZE = 16;
uint16_t const log_max_power = std::floor(std::log(_num_tables) / std::log(2));
uint16_t const search_range = pow(2, log_max_power) * TABLE_SIZE;
header_sink.put(search_range);
header_sink.put(log_max_power);
header_sink.put(static_cast<uint16_t>(_num_tables * TABLE_SIZE - search_range));
auto const tables_start = header_sink.offset();
Sink data_sink(data, tables_start + _tables.size() * TABLE_SIZE);
for (auto table: _tables) {
header_sink.set_offset(tables_start + table->header_index() * TABLE_SIZE);
table->write(header_sink, SubSink(data_sink));
}
auto const overall_checksum = ttf::checksum(data.data(), data_sink.offset());
for (auto table: _tables) {
if (auto head = std::dynamic_pointer_cast<HeadTable>(table)) {
head->set_checksum_adjustment(data_sink, 0xb1b0afba - overall_checksum);
}
}
boost::filesystem::ofstream file(ttf_file, std::ios::binary);
file.write(reinterpret_cast<char*>(data.data()), data_sink.offset());
}
std::shared_ptr<NameTable>
Font::name_table()
{
auto iter = std::find_if(_tables.begin(), _tables.end(), [](std::shared_ptr<Table> t) { return static_cast<bool>(std::dynamic_pointer_cast<NameTable>(t)); });
if (iter == _tables.end()) {
return {};
}
return std::dynamic_pointer_cast<NameTable>(*iter);
}
|