summaryrefslogtreecommitdiff
path: root/src/table.cc
blob: c6b2e79a0bb304b6042df56157b2bdda6e35ffcb (plain)
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
/*
    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 "name_table.h"
#include "table.h"
#include "tags.h"
#include "util.h"
#include <algorithm>


using namespace ttf;


Table::Table(Source& source, int header_index)
	: _header_index(header_index)
	, _checksum(source.get_uint32())
	, _offset(source.get_uint32())
	, _length(source.get_uint32())
{

}


OtherTable::OtherTable(Source& source, int header_index, uint32_t tag)
	: Table(source, header_index)
	, _tag(tag)
{
	_data = source.get_block(_offset, _length);
}


void
OtherTable::write(Sink& header_sink, Sink&& data_sink) const
{
	header_sink.put(_tag);
	header_sink.put(checksum(_data));
	header_sink.put(data_sink.offset());
	header_sink.put(static_cast<uint32_t>(_data.size()));
	data_sink.put(_data);
	data_sink.pad();
}


HeadTable::HeadTable(Source& source, int header_index)
	: Table(source, header_index)
{
	_data = source.get_block(_offset, _length);
}


void
HeadTable::write(Sink& header_sink, Sink&& data_sink) const
{
	header_sink.put(HEAD);

	if (_data.size() < 12) {
		throw InvalidTableSize();
	}

	std::vector<uint8_t> data = _data;

	/* Zero out the checkSumAdjustment */
	data[8] = 0;
	data[9] = 0;
	data[10] = 0;
	data[11] = 0;

	header_sink.put(checksum(data));
	header_sink.put(data_sink.offset());
	header_sink.put(static_cast<uint32_t>(data.size()));
	data_sink.put(data);
	data_sink.pad();
}


void
HeadTable::set_checksum_adjustment(Sink& data_sink, uint32_t adjustment)
{
	data_sink.put(adjustment, _offset + 8);
}


std::shared_ptr<Table>
ttf::make_table(Source& source, int header_index)
{
	auto const tag = source.get_uint32();
	std::shared_ptr<Table> table;
	switch (tag) {
	case NAME:
		return std::make_shared<NameTable>(source, header_index);
	case HEAD:
		return std::make_shared<HeadTable>(source, header_index);
	default:
		return std::make_shared<OtherTable>(source, header_index, tag);
	}
}