summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-1.2.2/src/GFTracer.cpp
blob: 69a078456a0260144a9f7947d2e7dd560fc88248 (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
/*************************************************************************
** GFTracer.cpp                                                         **
**                                                                      **
** This file is part of dvisvgm -- the DVI to SVG converter             **
** Copyright (C) 2005-2013 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 <iostream>
#include <fstream>
#include <sstream>
#include "GFTracer.h"
#include "Glyph.h"
#include "Message.h"

#ifdef __MSVC__
#include <potracelib.h>
#else
extern "C" {
#include <potracelib.h>
}
#endif

using namespace std;


GFTracer::GFTracer (istream &is)
	: GFReader(is), _unitsPerPoint(0.0)
{
}


/** Constructs a new GFTracer.
 *  @param[in] is GF file is read from this stream
 *  @param[in] upp target units per TeX point */
GFTracer::GFTracer (istream &is, double upp)
	: GFReader(is), _unitsPerPoint(upp)
{
}


void GFTracer::beginChar (UInt32 c) {
}


void GFTracer::endChar (UInt32 c) {
	const Bitmap &bitmap = getBitmap();
	if (bitmap.empty())
		return;

	// prepare potrace's bitmap structure
	vector<potrace_word> buffer;
	potrace_bitmap_t pobitmap;
	pobitmap.w = bitmap.width();
	pobitmap.h = bitmap.height();
	pobitmap.dy = bitmap.copy(buffer);
	pobitmap.map = &buffer[0];
	potrace_param_t *param = potrace_param_default();
	potrace_state_t *state = potrace_trace(param, &pobitmap);
	potrace_param_free(param);

	if (!state || state->status == POTRACE_STATUS_INCOMPLETE)
		Message::wstream(true) << "error while tracing character\n";
	else {
		double hsf=1.0, vsf=1.0; // horizontal a d vertical scale factor
		if (_unitsPerPoint != 0.0) {
			hsf = _unitsPerPoint/getHPixelsPerPoint();  // horizontal scale factor
			vsf = _unitsPerPoint/getVPixelsPerPoint();  // vertical scale factor
		}
		for (potrace_path_t *path = state->plist; path; path = path->next) {
			potrace_dpoint_t &p = path->curve.c[path->curve.n-1][2]; // start/end point
			moveTo(hsf*(p.x+bitmap.xshift()), vsf*(p.y+bitmap.yshift()));
			for (int i=0; i < path->curve.n; i++) {
				if (path->curve.tag[i] == POTRACE_CURVETO) {
					curveTo(hsf*(path->curve.c[i][0].x+bitmap.xshift()), vsf*(path->curve.c[i][0].y+bitmap.yshift()),
							  hsf*(path->curve.c[i][1].x+bitmap.xshift()), vsf*(path->curve.c[i][1].y+bitmap.yshift()),
							  hsf*(path->curve.c[i][2].x+bitmap.xshift()), vsf*(path->curve.c[i][2].y+bitmap.yshift()));
				}
				else {
					lineTo(hsf*(path->curve.c[i][1].x+bitmap.xshift()), vsf*(path->curve.c[i][1].y+bitmap.yshift()));
					if (i == path->curve.n-1)
						closePath();
					else
						lineTo(hsf*(path->curve.c[i][2].x+bitmap.xshift()), vsf*(path->curve.c[i][2].y+bitmap.yshift()));
				}
			}
		}
	}
	potrace_state_free(state);
}