aboutsummaryrefslogtreecommitdiff
path: root/stfexporter.cc
blob: 1503ae475d53e37879dfd0c56326407e8df0f3ab (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
/*
 * scribiu -- read notebooks and voice memos from Livescribe pens
 * Copyright (C) 2021 Javier S. Pedro <javier@javispedro.com>
 *
 * 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 <QtCore/QDebug>
#include <QtCore/QTextStream>
#include <QtCore/QXmlStreamWriter>

#include "stfexporter.h"

#define XMLNS_INK "http://www.w3.org/2003/InkML"

class StfToTXYP : public StfReader::StrokeHandler {
	QTextStream _out;
	qint64 _startTime;
	bool _relativeTime;

public:
	StfToTXYP(QIODevice *out, bool relativeTime)
	    : _out(out), _startTime(0), _relativeTime(relativeTime) {
		_out << "T\tX\tY\tP\n";
	}

	bool startStroke(const QPoint& p, int force, qint64 time) {
		if (_relativeTime && _startTime == 0) {
			_startTime = time;
		}
		_out << (time - _startTime) << '\t' << p.x() << '\t' << p.y() << '\t' << force << '\n';
		return true;
	}

	bool strokePoint(const QPoint& p, int force, qint64 time) {
		_out << (time - _startTime) << '\t' << p.x() << '\t' << p.y() << '\t' << force << '\n';
		return true;
	}

	bool endStroke(qint64 time) {
		// Force == 0 is used to detect strokes
		Q_UNUSED(time);
		return true;
	}
};

class StfToInkML : public StfReader::StrokeHandler {
	QXmlStreamWriter *_out;
	QPoint _lastP;
	qint64 _startTime;

public:
	StfToInkML(QXmlStreamWriter *out)
	    : _out(out), _lastP(), _startTime(0) {
	}

	bool startStroke(const QPoint& p, int force, qint64 time) {
		Q_UNUSED(force);
		if (_startTime == 0) {
			_startTime = time;
		}

		_out->writeStartElement(XMLNS_INK, "trace");
		_out->writeAttribute("timeOffset", QString::number(time - _startTime));

		_out->writeCharacters(QString("%1 %2").arg(p.x()).arg(p.y()));

		_lastP = p;
		return true;
	}

	bool strokePoint(const QPoint& p, int force, qint64 time) {
		Q_UNUSED(force);
		Q_UNUSED(time);
		QPoint delta = p - _lastP;

		_out->writeCharacters(QString(", %1 %2").arg(delta.x()).arg(delta.y()));

		_lastP = p;
		return true;
	}

	bool endStroke(qint64 time) {
		Q_UNUSED(time);
		_out->writeEndElement();
		return true;
	}
};

StfExporter::StfExporter(AfdNotebook *nb)
    : _nb(nb)
{
}

void StfExporter::exportToTXYP(QIODevice *out, int pageNum, bool relativeTime)
{
	QStringList pens = _nb->penSerials();
	if (pens.isEmpty()) return;

	StfToTXYP h(out, relativeTime);

	exportPage(&h, pageNum);
}

void StfExporter::exportToInkML(QIODevice *out, int pageNum)
{
	QStringList pens = _nb->penSerials();
	if (pens.isEmpty()) return;

	QXmlStreamWriter writer(out);
	writer.setAutoFormatting(true);
	writer.writeStartDocument();
	writer.writeDefaultNamespace(XMLNS_INK);
	writer.writeStartElement(XMLNS_INK, "ink");

#if 0 /* No need to write out inkSource element, since default trace format is OK for now */
	writer.writeStartElement(XMLNS_INK, "inkSource");
	writer.writeAttribute("manufacturer", "Livescribe");
	writer.writeAttribute("description", "Dumped by Scribiu");
	writer.writeStartElement(XMLNS_INK, "traceFormat");
	writer.writeEmptyElement(XMLNS_INK, "channel");
	writer.writeAttribute("name", "X");
	writer.writeAttribute("type", "integer");
	writer.writeEmptyElement(XMLNS_INK, "channel");
	writer.writeAttribute("name", "Y");
	writer.writeAttribute("type", "integer");
	writer.writeEndElement();
	writer.writeEmptyElement(XMLNS_INK, "sampleRate");
	writer.writeAttribute("uniform", "true");
	writer.writeAttribute("value", "75");
	writer.writeEndElement();
#endif

	StfToInkML h(&writer);

	exportPage(&h, pageNum);

	writer.writeEndElement();
	writer.writeEndDocument();
}

bool StfExporter::exportPage(StfReader::StrokeHandler *handler, int pageNum)
{
	QStringList pens = _nb->penSerials();
	if (pens.isEmpty()) return true; // No pen wrote on this page

	StfReader r;
	r.setStrokeHandler(handler);

	foreach (const QString &pen, pens) {
		QStringList strokeFiles = _nb->strokeFiles(pen, pageNum);
		foreach (const QString &strokeFile, strokeFiles) {
			QFile in(strokeFile);
			if (!in.open(QIODevice::ReadOnly)) {
				qWarning() << "Could not open stroke file:" << strokeFile;
				continue;
			}

			if (!r.parse(&in)) {
				qWarning() << "Could not parse stroke file:" << strokeFile;
				continue;
			}
		}
	}

	return true;
}