summaryrefslogtreecommitdiff
path: root/src/table.h
blob: 70fbc56c4e2c01eb0d1102f38d379ab21255c54c (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
116
117
118
119
120
121
122
123
124
125
126
127
/*
    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/>.

*/


#pragma once

#include <boost/variant.hpp>
#include <fmt/format.h>
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>


namespace ttf {


class Sink;
class Source;


class InvalidChecksumError : public std::runtime_error
{
public:
	InvalidChecksumError() : std::runtime_error("Invalid checksum") {}
};


class UnsupportedPlatformIdentifier : public std::runtime_error
{
public:
	UnsupportedPlatformIdentifier(uint16_t platform_id) : std::runtime_error(fmt::format("Unsupported string platform ID {}", platform_id)) {}
};


class InvalidTableSize : public std::runtime_error
{
public:
	InvalidTableSize() : std::runtime_error("Invalid table size") {}
};


/** One table in a TTF font */
class Table
{
public:
	Table(Source& source, int header_index);

	/** @return Index of the table's entry in the header.  This
	 *  is used to preserve header order.
	 */
	int header_index() const {
		return _header_index;
	}

	/** @return offset of table within the font file */
	uint32_t offset() const {
		return _offset;
	}

	/** @return length of the table in bytes */
	uint32_t length() const {
		return _length;
	}

	virtual void write(Sink& header_sink, Sink&& data_sink) const = 0;

protected:
	int _header_index;
	uint32_t _checksum;
	uint32_t _offset;
	uint32_t _length;
};


/** A table that we just pass through blindly */
class OtherTable : public Table
{
public:
	OtherTable(Source& source, int header_index, uint32_t tag);

	void write(Sink& header_sink, Sink&& data_sink) const override;

protected:
	uint32_t _tag;
	std::vector<uint8_t> _data;
};


/** The 'head' table */
class HeadTable : public Table
{
public:
	HeadTable(Source& source, int header_index);

	void write(Sink& header_sink, Sink&& data_sink) const override;
	void set_checksum_adjustment(Sink& data_sink, uint32_t adjustment);

private:
	std::vector<uint8_t> _data;
};


std::shared_ptr<Table> make_table(Source& source, int header_index);


}