1
/****************************************************************************
3
** This file is part of the LibreCAD project, a 2D CAD program
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
9
** This file may be distributed and/or modified under the terms of the
10
** GNU General Public License version 2 as published by the Free Software
11
** Foundation and appearing in the file gpl-2.0.txt included in the
12
** packaging of this file.
14
** This program is distributed in the hope that it will be useful,
15
** but WITHOUT ANY WARRANTY; without even the implied warranty of
16
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
** GNU General Public License for more details.
19
** You should have received a copy of the GNU General Public License
20
** along with this program; if not, write to the Free Software
21
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
** This copyright notice MUST APPEAR in all copies of the script!
25
**********************************************************************/
28
#include "rs_filtercxf.h"
34
#include "rs_utility.h"
35
#include "rs_system.h"
37
#include <QStringList>
41
* Default constructor.
43
RS_FilterCXF::RS_FilterCXF() : RS_FilterInterface() {
45
RS_DEBUG->print("Setting up CXF filter...");
49
* Implementation of the method used for RS_Import to communicate
52
* @param g The graphic in which the entities from the file
53
* will be created or the graphics from which the entities are
54
* taken to be stored in a file.
56
bool RS_FilterCXF::fileImport(RS_Graphic& g, const QString& file, RS2::FormatType /*type*/) {
57
RS_DEBUG->print("CXF Filter: importing file '%s'...", file.toLatin1().data());
62
// Load font file as we normally do, but the font doesn't own the
63
// letters (we'll add them to the graphic instead. Hence 'false').
64
RS_Font font(file, false);
65
success = font.loadFont();
68
RS_DEBUG->print(RS_Debug::D_WARNING,
69
"Cannot open CXF file '%s'.", file.toLatin1().data());
73
g.addVariable("Names",
74
font.getNames().join(","), 0);
75
g.addVariable("LetterSpacing", font.getLetterSpacing(), 0);
76
g.addVariable("WordSpacing", font.getWordSpacing(), 0);
77
g.addVariable("LineSpacingFactor", font.getLineSpacingFactor(), 0);
78
g.addVariable("Authors", font.getAuthors().join(","), 0);
79
if (!font.getEncoding().isEmpty()) {
80
g.addVariable("Encoding", font.getEncoding(), 0);
83
RS_BlockList* letterList = font.getLetterList();
84
for (unsigned i=0; i<font.countLetters(); ++i) {
85
RS_Block* ch = font.letterAt(i);
88
uCode.setNum(ch->getName().at(0).unicode(), 16);
89
while (uCode.length()<4) {
92
//ch->setName("[" + uCode + "] " + ch->getName());
93
//letterList->rename(ch, QString("[%1]").arg(ch->getName()));
94
letterList->rename(ch,
95
QString("[%1] %2").arg(uCode).arg(ch->getName().at(0)));
97
g.addBlock(ch, false);
101
g.addBlockNotification();
109
* Implementation of the method used for RS_Export to communicate
112
* @param file Full path to the CXF file that will be written.
114
bool RS_FilterCXF::fileExport(RS_Graphic& g, const QString& file, RS2::FormatType /*type*/) {
116
RS_DEBUG->print("CXF Filter: exporting file '%s'...", file.toLatin1().data());
118
// crashes under windoze xp:
119
//std::ofstream fout;
121
RS_DEBUG->print("RS_FilterCXF::fileExport: open");
122
//fout.open((const char*)file.toLocal8Bit());
125
if ((fp = fopen(file.toLocal8Bit(), "wt")) != NULL) {
127
RS_DEBUG->print("RS_FilterCXF::fileExport: open: OK");
129
RS_DEBUG->print("RS_FilterCXF::fileExport: header");
132
fprintf(fp, "# Format: QCad II Font\n");
134
fprintf(fp, "# Creator: %s\n",
135
(const char*)RS_SYSTEM->getAppName().toLocal8Bit());
136
fprintf(fp, "# Version: %s\n",
137
(const char*)RS_SYSTEM->getAppVersion().toLocal8Bit());
139
RS_DEBUG->print("001");
140
QString ns = g.getVariableString("Names", "");
142
QStringList names = ns.split(',');
143
RS_DEBUG->print("002");
144
for (int i = 0; i < names.size(); ++i) {
145
fprintf(fp, "# Name: %s\n",
146
names.at(i).toLocal8Bit().data() );
150
RS_DEBUG->print("003");
152
QString es = g.getVariableString("Encoding", "");
154
fprintf(fp, "# Encoding: %s\n",
155
es.toLocal8Bit().data());
158
RS_DEBUG->print("004a");
160
fprintf(fp, "# LetterSpacing: %f\n",
161
g.getVariableDouble("LetterSpacing", 3.0));
162
fprintf(fp, "# WordSpacing: %f\n",
163
g.getVariableDouble("WordSpacing", 6.75));
164
fprintf(fp, "# LineSpacingFactor: %f\n",
165
g.getVariableDouble("LineSpacingFactor", 1.0));
167
QString sa = g.getVariableString("Authors", "");
168
RS_DEBUG->print("authors: %s", sa.toLocal8Bit().data());
170
QStringList authors = sa.split(',');
171
RS_DEBUG->print("006");
172
RS_DEBUG->print("count: %d", authors.count());
175
for (QStringList::Iterator it2 = authors.begin();
176
it2!=authors.end(); ++it2) {
178
RS_DEBUG->print("006a");
180
RS_DEBUG->print("006b");
181
RS_DEBUG->print("string is: %s", a.toLatin1().data());
182
RS_DEBUG->print("006b0");
183
fprintf(fp, "# Author: ");
184
RS_DEBUG->print("006b1");
185
fprintf(fp, "%s\n", a.toLatin1().data());
186
//fout << "# Author: " << a.ascii() << "\n";
188
RS_DEBUG->print("007");
191
RS_DEBUG->print("RS_FilterCXF::fileExport: header: OK");
193
RS_DEBUG->print("008");
194
// iterate through blocks (=letters of font)
195
for (unsigned i=0; i<g.countBlocks(); ++i) {
196
RS_Block* blk = g.blockAt(i);
198
RS_DEBUG->print("block: %d", i);
199
RS_DEBUG->print("001");
201
if (blk!=NULL && !blk->isUndone()) {
202
RS_DEBUG->print("002");
203
RS_DEBUG->print("002a: %s",
204
(blk->getName().toLocal8Bit().data()));
206
fprintf(fp, "\n%s\n",
207
(blk->getName().toLocal8Bit().data()));
210
// iterate through entities of this letter:
211
for (RS_Entity* e=blk->firstEntity(RS2::ResolveAll);
213
e=blk->nextEntity(RS2::ResolveAll)) {
215
if (!e->isUndone()) {
217
RS_DEBUG->print("004");
220
if (e->rtti()==RS2::EntityLine) {
221
RS_Line* l = (RS_Line*)e;
223
fprintf(fp, "L %f,%f,%f,%f\n",
224
l->getStartpoint().x,
225
l->getStartpoint().y,
231
else if (e->rtti()==RS2::EntityArc) {
232
RS_Arc* a = (RS_Arc*)e;
234
if (!a->isReversed()) {
240
fprintf(fp, "%f,%f,%f,%f,%f\n",
245
a->getAngle2()*ARAD);
247
// Ignore entities other than arcs / lines
251
RS_DEBUG->print("005");
253
RS_DEBUG->print("006");
255
RS_DEBUG->print("007");
259
RS_DEBUG->print("CXF Filter: exporting file: OK");
263
RS_DEBUG->print("CXF Filter: exporting file failed");
272
* Streams a double value to the gien stream cutting away trailing 0's.
274
* @param value A double value. e.g. 2.700000
276
void RS_FilterCXF::stream(std::ofstream& fs, double value) {
277
fs << RS_Utility::doubleToString(value).toLatin1().data();