summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-src/tests/FontCacheTest.cpp
blob: e4900f66756fef51ca79fa635e800e0de815610e (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*************************************************************************
** FontCacheTest.cpp                                                    **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2017 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/>. **
*************************************************************************/

#include <gtest/gtest.h>
#include <fstream>
#include <sstream>
#include "FileSystem.hpp"
#include "FontCache.hpp"

#ifndef SRCDIR
#define SRCDIR "."
#endif

using namespace std;

class FontCacheTest : public testing::Test
{
	protected:
		FontCacheTest () : testing::Test(), cachedir("data") {
			glyph1.moveto(0, 0);
			glyph1.lineto(10, 0);
			glyph1.lineto(10, 10);
			glyph1.lineto(0, 10);
			glyph1.closepath();

			glyph2.moveto(0, 0);
			glyph2.cubicto(10, 10, 20, 0, 50, 50);
			glyph2.lineto(30, 20);
			glyph2.conicto(20, 40, 20, 20);
			glyph2.closepath();
		}

		~FontCacheTest () {
			FileSystem::remove(cachedir+"/testfont.fgd");
		}

		Glyph glyph1, glyph2;
		FontCache cache;
		string cachedir;
};


static string toSVG (const Glyph &glyph) {
	ostringstream oss;
	glyph.writeSVG(oss, false);
	return oss.str();
}


static bool operator == (const Glyph &glyph1, const Glyph &glyph2) {
	return toSVG(glyph1) == toSVG(glyph2);
}


TEST_F(FontCacheTest, glyph) {
	EXPECT_EQ(toSVG(glyph1), "M0 0H10V10H0Z");
	EXPECT_EQ(toSVG(glyph2), "M0 0C10 10 20 0 50 50L30 20Q20 40 20 20Z");
	cache.setGlyph(1, glyph1);
	cache.setGlyph(10, glyph2);
	ASSERT_NE(cache.getGlyph(1), nullptr);
	ASSERT_EQ(cache.getGlyph(2), nullptr);
	ASSERT_NE(cache.getGlyph(10), nullptr);
	ASSERT_EQ(*cache.getGlyph(1), glyph1);
	ASSERT_EQ(*cache.getGlyph(10), glyph2);
}


TEST_F(FontCacheTest, write1) {
	cache.setGlyph(1, glyph1);
	ASSERT_TRUE(cache.fontname().empty());
	ASSERT_FALSE(cache.write(cachedir.c_str()));
}


TEST_F(FontCacheTest, write2) {
	cache.setGlyph(1, glyph1);
	ASSERT_TRUE(FileSystem::exists(cachedir));
	ASSERT_TRUE(cache.write("testfont", cachedir.c_str()));
	cache.setGlyph(10, glyph2);
	ASSERT_TRUE(cache.write("testfont", cachedir.c_str()));
	ASSERT_TRUE(cache.fontname().empty());
}


TEST_F(FontCacheTest, read) {
	cache.setGlyph(1, glyph1);
	cache.setGlyph(10, glyph2);
	ASSERT_TRUE(cache.write("testfont", cachedir.c_str()));
	// clear cache object
	cache.clear();
	ASSERT_EQ(cache.getGlyph(1), nullptr);
	ASSERT_EQ(cache.getGlyph(2), nullptr);
	ASSERT_EQ(cache.getGlyph(10), nullptr);
	// read glyph data from cache file
	ASSERT_TRUE(cache.read("testfont", cachedir.c_str()));
	ASSERT_EQ(cache.fontname(), "testfont");
	ASSERT_NE(cache.getGlyph(1), nullptr);
	ASSERT_EQ(cache.getGlyph(2), nullptr);
	ASSERT_NE(cache.getGlyph(10), nullptr);
	ASSERT_EQ(*cache.getGlyph(1), glyph1);
	ASSERT_EQ(*cache.getGlyph(10), glyph2);
}


TEST_F(FontCacheTest, fontinfo1) {
	ostringstream oss;
	cache.clear();
	FileSystem::remove(cachedir+"/testfont.fgd");
	cache.fontinfo(cachedir.c_str(), oss);
	ASSERT_EQ(oss.str(), "cache is empty\n");

	// check removal of invalid cache files
	ofstream cachefile(cachedir+"/invalid.fgd");
	cachefile << "invalid cache file";
	cachefile.close();
	ASSERT_TRUE(FileSystem::exists(cachedir+"/invalid.fgd"));
	oss.str("");
	cache.fontinfo(cachedir.c_str(), oss, true);
	ASSERT_EQ(oss.str(),
		"cache is empty\n"
		"invalid cache file invalid.fgd removed\n"
	);
	ASSERT_FALSE(FileSystem::exists(cachedir+"/invalid.fgd"));
}


TEST_F(FontCacheTest, fontinfo2) {
	cache.setGlyph(1, glyph1);
	cache.setGlyph(10, glyph2);
	ASSERT_TRUE(cache.write("testfont", cachedir.c_str()));

	ostringstream oss;
	cache.fontinfo(cachedir.c_str(), oss);
	ASSERT_EQ(oss.str(),
		"cache format version 5\n"
		"testfont      2 glyphs        10 cmds          58 bytes  crc:38cb5c67\n"
	);
}