summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/TrueTypeFont.hpp
blob: a287f305faab6d516b123f2d8707490df0b7a839 (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
/*************************************************************************
** TrueTypeFont.hpp                                                     **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2021 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program 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 3 of       **
** the License, or (at your option) any later version.                  **
**                                                                      **
** This program 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 this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#ifndef TRUETYPEFONT_HPP
#define TRUETYPEFONT_HPP

#include <istream>
#include <fstream>
#include <ostream>
#include <string>
#include <vector>
#include "MessageException.hpp"


class TrueTypeFont {
	struct TTFTableRecord {
		std::string name () const;
		uint32_t paddedSize () const {return (size+3) & ~3;}
		uint32_t computeChecksum () const;
		uint32_t tag;         ///< table tag
		uint32_t checksum;    ///< checksum of the table data
		uint32_t size;        ///< number of bytes in the table
		const uint8_t *data;  ///< points to the begin of the table
	};

	struct WOFFTableRecord {
		WOFFTableRecord (uint32_t off, const TTFTableRecord &rec) : offset(off), size(rec.size), data(rec.data), ttfRecord(rec) {}
		size_t paddedSize () const {return (size+3) & ~3;}
		bool compressTableData ();
		uint32_t offset;                        ///< file offset to first byte of the table
		uint32_t size;                          ///< number of bytes in the table
		const uint8_t *data;                    ///< points to the begin of the WOFF table
		std::vector<uint8_t> compressedData;    ///< compressed table data
		const TTFTableRecord &ttfRecord;        ///< corresponding TTF table record
	};

	public:
		TrueTypeFont () : _version(0) {}
		explicit TrueTypeFont (const std::string &fname) {read(fname);}
		bool read (const std::string &fname);
		void writeWOFF (std::ostream &os) const;
		void writeWOFF (const std::string &fname) const;
		bool writeWOFF2 (std::ostream &os) const;
		bool writeWOFF2 (const std::string &fname) const;

	private:
		uint32_t _version;        ///< TTF version
		std::string _buffer;      ///< contents of TTF file
		std::vector<TTFTableRecord> _tableRecords;
};


struct TrueTypeFontException : public MessageException {
	explicit TrueTypeFontException (const std::string &msg) : MessageException(msg) {}
};

#endif