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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*************************************************************************
** FontCacheTest.cpp **
** **
** This file is part of dvisvgm -- a fast DVI to SVG converter **
** Copyright (C) 2005-2025 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 BUILDDIR
#define BUILDDIR "."
#endif
using namespace std;
class LocalCache {
public:
LocalCache (const string &cachedir)
: _cachedir(cachedir),
_created(!FileSystem::exists(cachedir) && FileSystem::mkdir(cachedir))
{
}
~LocalCache () {
if (_created)
FileSystem::rmdir(_cachedir);
}
string cachedir () const {return _cachedir;}
private:
string _cachedir;
bool _created;
};
static LocalCache localCache(BUILDDIR"/data");
class FontCacheTest : public testing::Test {
protected:
FontCacheTest () : testing::Test(), cachedir(localCache.cachedir()) {
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.quadto(20, 40, 20, 20);
glyph2.closepath();
}
~FontCacheTest () override {
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();
}
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));
}
TEST_F(FontCacheTest, write2) {
cache.setGlyph(1, glyph1);
ASSERT_TRUE(FileSystem::exists(cachedir));
ASSERT_TRUE(cache.write("testfont", cachedir));
cache.setGlyph(10, glyph2);
EXPECT_TRUE(cache.write("testfont", cachedir));
EXPECT_TRUE(cache.fontname().empty());
}
TEST_F(FontCacheTest, read) {
cache.setGlyph(1, glyph1);
cache.setGlyph(10, glyph2);
ASSERT_TRUE(cache.write("testfont", cachedir));
// clear cache object
cache.clear();
EXPECT_EQ(cache.getGlyph(1), nullptr);
EXPECT_EQ(cache.getGlyph(2), nullptr);
EXPECT_EQ(cache.getGlyph(10), nullptr);
// read glyph data from cache file
ASSERT_TRUE(cache.read("testfont", cachedir));
EXPECT_EQ(cache.fontname(), "testfont");
ASSERT_NE(cache.getGlyph(1), nullptr);
EXPECT_EQ(cache.getGlyph(2), nullptr);
ASSERT_NE(cache.getGlyph(10), nullptr);
EXPECT_EQ(*cache.getGlyph(1), glyph1);
EXPECT_EQ(*cache.getGlyph(10), glyph2);
}
TEST_F(FontCacheTest, fontinfo1) {
ostringstream oss;
cache.clear();
FileSystem::remove(cachedir+"/testfont.fgd");
cache.fontinfo(cachedir, 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, 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));
ostringstream oss;
cache.fontinfo(cachedir, oss);
ASSERT_EQ(oss.str(),
"cache format version 5\n"
"testfont 2 glyphs 10 cmds 58 bytes hash:3cb32ab6\n"
);
}
|