summaryrefslogtreecommitdiff
path: root/dviware/dvi2bitmap/test/t5.cc
blob: a350d0853cc9271eba81c022989e908e2ad99760 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Testing Bitmap

#include <config.h>
#include <iostream>

#include <DviFile.h>
#include <PkFont.h>
#include <Bitmap.h>

#if HAVE_CSTD_INCLUDE
#include <cstdlib>
#else
#include <stdlib.h>
#endif

using STD::cerr;
using STD::cout;
using STD::exit;
using STD::endl;

#define CHECKNEXTFONT(iter,checkname)	\
    if (iter == fs->end()) {	 \
	cerr << "Unexpected end of list: expected "	\
	     << checkname << ", but got nothing"	\
	     << endl;					\
	nfails++;					\
    } else {						\
	f = *iter;					\
	if (f->name() != checkname) {			\
	    cerr << "Unexpected font: was " << f->name() \
		 << ", expected " << checkname << endl;	 \
	    nfails++;					 \
	}						 \
    }							 \
    if (iter != fs->end())				\
	++iter;

#define CHECKBB(context,bb0,bb1,bb2,bb3)				\
    {									\
	int exp[] = {bb0,bb1,bb2,bb3};					\
	for (int ti=0; ti<4; ti++)					\
	    if (bb[ti] != exp[ti]) {					\
		cerr << context						\
		     << ": unexpected value of bb[" << ti << "]: expected " \
		     << exp[ti] << ", got " << bb[ti] << endl;		\
		nfails++;						\
	    }								\
    }

#define CHECKINT(context,expected,actual) \
    if ((expected) != (actual)) {			\
	cerr << context << ": expected " << (expected)	\
	     << ", got " << (actual) << endl;		\
	nfails++;					\
    }	    
	    

#define NBITMAPROWS 10
#define NBITMAPCOLS 20
// Note that bitmap coordinates have (0,0) at the top left, so
// this result picture, and the F[] array below, are both right way up
const char *picture[NBITMAPROWS] = {
    "                  **",
    "                  **",
    " *****            **",
    " * *                ",
    " * * **********     ",
    "     **********     ",
    "                    ",
    "                    ",
    "                    ",
    "***                 ",
};
const char *picturePostCrop[NBITMAPROWS] = {
    "                  **",
    "                  **",
    " *****            **",
    " * *                ",
    " * * **********     ",
    "     **********     ",
};
const char *pictureLarge[2*NBITMAPROWS] = {
    "                                        ",
    "                                        ",
    " *****                          *****   ",
    " * *                            * *     ",
    " * * **********                 * *   **",
    "     **********                       * ",
    "                                      * ",
    "                                        ",
    "                                        ",
    "***                                     ",
    "***                                     ",
    "***                                     ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
};
const char *pictureSmall[20] = {
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "   *****            ",
    "   *****            ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
    "                    ",
};
const char *pictureSmallCrop[4] = {
    "       ",
    " ***** ",
    " ***** ",
    "       ",
};
const char *pictureSmallCropStrut[] = {
    "  *****",
    "  *****",
    "       ",
    "       ",
    "       ",
};
const Byte F[] = { 1,1,1,1,1,
		   1,0,1,0,0,
		   1,0,1,0,0, };

int nfails = 0;

void compareBitmaps(Bitmap *testB, const char **picture,
		    const int ncols, const int nrows,
		    bool showActual=false)
{
    Byte *row;
    int rown = 0;
    if (showActual) {
	for (Bitmap::const_iterator bi = testB->begin();
	     bi != testB->end();
	     ++bi) {
	    row = *bi;
	    for (int i=0; i<ncols; i++)
		cerr << (row[i] ? '*' : ' ');
	    cerr << endl;
	}
    }
    for (Bitmap::const_iterator bi = testB->begin();
	 bi != testB->end();
	 ++bi) {
	if (rown >= nrows) {
	    cerr << "Oooops, we've received more bitmap rows than we should"
		 << endl;
	    nfails++;
	} else {
	    row = *bi;
	    for (int i=0; i<ncols; i++) {
		if ((row[i] != 0) != (picture[rown][i] != ' ')) {
		    cerr << "Row " << rown << ": expected" << endl
			 << "  <" << picture[rown] << ">, got" << endl
			 << "  <";
		    for (int j=0; j<ncols; j++) 
			cerr << (row[j] ? '*' : ' ');
		    cerr << ">" << endl;
		    nfails++;
		    break;
		}
	    }
	}
	rown++;
    }
    if (rown != nrows) {
	cerr << "At end of iterator, should have " << nrows << " rows, have "
	     << rown << " instead" << endl;
	nfails++;
    }
}

int main (int argc, char** argv)
{
    try {
	string arg;
	if (argc > 1)
	    arg = argv[1];
	else
	    arg = "t5.dvi";
	//DviFile *dvif = new DviFile(argc > 1 ? argv[1] : "t5.dvi",
	DviFile *dvif = new DviFile(arg,
				    PkFont::dpiBase(), // resolution
				    1.0, // overall magnification
				    true); // read postamble
	const DviFile::FontSet* fs = dvif->getFontSet();
    
	DviFile::FontSet::const_iterator dvii = fs->begin();
	const PkFont* f;
	CHECKNEXTFONT(dvii,"cmr10");
	CHECKNEXTFONT(dvii,"cmcsc10");
	if (dvii != fs->end()) {
	    f = *dvii;
	    cerr << "Expected end of list, but got font "
		 << f->name() << " instead" << endl;
	    nfails++;
	}

	//Bitmap::verbosity(debug);

	int bw = NBITMAPCOLS;
	int bh = NBITMAPROWS;
	Bitmap *b = new Bitmap(bw, bh, 1, false);
	b->rule(5, 5, 10, 2);
	b->rule(-2, 11, 5, 3);	// partly off the bitmap, to bottom-left
	b->rule(18, 2, 5, 3);	// ...to top-right
	b->rule(-5, 0, 2, 2);	// completely off, to left
	b->rule(21, 5, 2, 2);	// ...to right
	b->paint(1, 2, 5, 3, F);

	int* bb;
	b->freeze();
	bb = b->boundingBox();
	CHECKBB("picture", 0, 0, NBITMAPCOLS, NBITMAPROWS);
	//     cerr << "BB: L=" << bb[0] << " T=" << bb[1]
	// 	 << " R=" << bb[2] << " B=" << bb[3]
	// 	 << endl;

	compareBitmaps(b, picture, bw, bh);

	// Now crop

	b->crop(Bitmap::Left, 2);
	b->crop(Bitmap::Bottom, 6, true);
	b->crop();
	bb = b->boundingBox();
	CHECKBB("postcrop", 0, 0, NBITMAPCOLS, 6);
	compareBitmaps(b, picturePostCrop, bw, 6);
	//     cerr << "BB: L=" << bb[0] << " T=" << bb[1]
	// 	 << " R=" << bb[2] << " B=" << bb[3]
	// 	 << endl;
	//     for (Bitmap::iterator bi = b->begin();
	// 	 bi != b->end();
	// 	 ++bi) {
	// 	Byte* row = *bi;
	// 	for (int i=0; i<bw; i++)
	// 	    cerr << (row[i] ? '*' : ' ');
	// 	cerr << endl;
	//     }

	delete b;

    
	b = new Bitmap(bw, bh, 1, true, 2*bw);
	b->rule(5, 5, 10, 2);
	b->paint(1, 2, 5, 3, F); // no need for expansion
	b->paint(32, 2, 5, 3, F); // bitmap needs to expand
	b->paint(38, 4, 5, 3, F); // ...but not by this much
	b->rule(-2, 11, 5, 3);	// expand in y direction

	b->freeze();
	bb = b->boundingBox();
	CHECKBB("large", 0, 2, 40, 12);
	compareBitmaps(b, pictureLarge, 40, 15);

	b->clear();
	b->rule(3,5,5,2);
	b->freeze();
	bb = b->boundingBox();
	CHECKBB("small", 3, 4, 8, 6);
	CHECKINT("bb[2]-bb[0]", 5, bb[2]-bb[0]);
	CHECKINT("bb[3]-bb[1]", 2, bb[3]-bb[1]);
	compareBitmaps(b, pictureSmall, 20, 15);
	// bitmap still expanded size, 15

	b->crop(Bitmap::All, 1);
	b->crop();
	bb = b->boundingBox();
	CHECKBB("smallcrop", 2, 3, 9, 7);
	CHECKINT("bb[2]-bb[0]", 7, bb[2]-bb[0]);
	CHECKINT("bb[3]-bb[1]", 4, bb[3]-bb[1]);
	compareBitmaps(b, pictureSmallCrop, bb[2]-bb[0], bb[3]-bb[1]);

	// same picture -- `small' -- but this time with a strut
	b->clear();
	b->strut(3,5,0,5,2,0);
	bb = b->boundingBox();
	CHECKBB("smallStrut1", 3, 4, 8, 6); // same bb as rule(3,5,5,2)
	b->rule(3,5,5,2);
	bb = b->boundingBox();
	CHECKBB("smallStrut2", 3, 4, 8, 6); // unchanged
	b->strut(4,4,3,0,1,3);
	b->freeze();
	bb = b->boundingBox();
	CHECKBB("smallStrut3", 1, 4, 8, 8);
	CHECKINT("bb[2]-bb[0]", 7, bb[2]-bb[0]);
	CHECKINT("bb[3]-bb[1]", 4, bb[3]-bb[1]);
	compareBitmaps(b, pictureSmall, 20, 15);

	b->crop();
	bb = b->boundingBox();
	CHECKBB("smallStrutCrop", 1, 4, 8, 8);
	CHECKINT("bb[2]-bb[0]", 7, bb[2]-bb[0]);
	CHECKINT("bb[3]-bb[1]", 4, bb[3]-bb[1]);
	compareBitmaps(b, pictureSmallCropStrut, bb[2]-bb[0], bb[3]-bb[1]);

    } catch (DviError& e) {
	cerr << "Caught DviError: " << e.problem() << endl;
    }
    
    exit (nfails);
}