summaryrefslogtreecommitdiff
path: root/src/collection.cc
blob: 1482b4241d57369a5d2fe249fa3e4c7b064ac198 (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
/*
    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 "collection.h"
#include "util.h"
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <iostream>


using namespace ttf;


Collection::Collection(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);
}


Collection::Collection(uint8_t const* data, int size)
{
	Source source(data, size);
	read_source(source);
}


void
Collection::read_source(Source& source)
{
	auto const ttc_tag = source.get_uint32();
	if (ttc_tag != 0x74746366) {
		throw InvalidTTCTag(ttc_tag);
	}

	auto const major_version = source.get_uint16();
	auto const minor_version = source.get_uint16();
	auto const num_fonts = source.get_uint32();

	if (num_fonts == 0) {
		return;
	}

	uint32_t offset = source.get_uint32();
	for (uint32_t i = 0; i < num_fonts; ++i) {
		Source font_source(source, offset);
		_fonts.push_back(Font(font_source));
	}
}