~ubuntu-branches/debian/sid/librecad/sid

« back to all changes in this revision

Viewing changes to src/lib/engine/rs_font.cpp

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-12-17 20:08:57 UTC
  • mfrom: (0.2.4)
  • Revision ID: package-import@ubuntu.com-20111217200857-r9wmynaloj230qm1
Tags: 1.0.0+nolibs-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
/**
39
39
 * Constructor.
40
40
 *
41
 
 * @param owner true if the font owns the letters (blocks). Otherwise 
 
41
 * @param owner true if the font owns the letters (blocks). Otherwise
42
42
 *              the letters will be deleted when the font is deleted.
43
43
 */
44
44
RS_Font::RS_Font(const QString& fileName, bool owner)
45
 
        :       letterList(owner) {
 
45
    :   letterList(owner) {
46
46
    this->fileName = fileName;
47
47
    encoding = "";
48
48
    loaded = false;
50
50
    wordSpacing = 6.75;
51
51
    lineSpacingFactor = 1.0;
52
52
    fileLicense = "unknown";
 
53
    rawLffFontList.clear();
53
54
}
54
55
 
55
56
 
77
78
 
78
79
        QFileInfo file;
79
80
        for (QStringList::Iterator it = fonts.begin();
80
 
                it!=fonts.end();
81
 
                it++) {
 
81
             it!=fonts.end();
 
82
             it++) {
82
83
 
83
84
            if (QFileInfo(*it).baseName().toLower()==fileName.toLower()) {
84
85
                path = *it;
95
96
    // No font paths found:
96
97
    if (path.isEmpty()) {
97
98
        RS_DEBUG->print(RS_Debug::D_WARNING,
98
 
                        "RS_Font::loadFont: No fonts available.");
 
99
                        "RS_Font::loadFont: No fonts available.");
99
100
        return false;
100
101
    }
101
102
 
103
104
    QFile f(path);
104
105
    if (!f.open(QIODevice::ReadOnly)) {
105
106
        RS_DEBUG->print(RS_Debug::D_WARNING,
106
 
                        "RS_Font::loadFont: Cannot open font file: %s", 
 
107
                        "RS_Font::loadFont: Cannot open font file: %s",
107
108
                        path.toLatin1().data());
108
109
        return false;
109
110
    } else {
110
111
        RS_DEBUG->print("RS_Font::loadFont: "
111
 
                        "Successfully opened font file: %s", 
 
112
                        "Successfully opened font file: %s",
112
113
                        path.toLatin1().data());
113
114
    }
114
115
    f.close();
136
137
    while (!ts.atEnd()) {
137
138
        line = ts.readLine();
138
139
 
139
 
        if (line.isEmpty()) 
 
140
        if (line.isEmpty())
140
141
            continue;
141
142
 
142
143
        // Read font settings:
143
144
        if (line.at(0)=='#') {
144
145
            QStringList lst =
145
 
                ( line.right(line.length()-1) ).split(':', QString::SkipEmptyParts);
 
146
                    ( line.right(line.length()-1) ).split(':', QString::SkipEmptyParts);
146
147
            QStringList::Iterator it3 = lst.begin();
147
148
 
148
 
                        // RVT_PORT sometimes it happens that the size is < 2
149
 
                        if (lst.size()<2) 
150
 
                                continue;
151
 
                        
 
149
            // RVT_PORT sometimes it happens that the size is < 2
 
150
            if (lst.size()<2)
 
151
                continue;
 
152
 
152
153
            QString identifier = (*it3).trimmed();
153
154
            it3++;
154
155
            QString value = (*it3).trimmed();
162
163
            } else if (identifier.toLower()=="author") {
163
164
                authors.append(value);
164
165
            } else if (identifier.toLower()=="name") {
165
 
                names.append(value);
 
166
                names.append(value);
166
167
            } else if (identifier.toLower()=="encoding") {
167
 
                                ts.setCodec(QTextCodec::codecForName(value.toLatin1()));
168
 
                                encoding = value;
 
168
                ts.setCodec(QTextCodec::codecForName(value.toLatin1()));
 
169
                encoding = value;
169
170
            }
170
171
        }
171
172
 
198
199
 
199
200
            // create new letter:
200
201
            RS_FontChar* letter =
201
 
                new RS_FontChar(NULL, ch, RS_Vector(0.0, 0.0));
202
 
                                
 
202
                    new RS_FontChar(NULL, ch, RS_Vector(0.0, 0.0));
 
203
 
203
204
            // Read entities of this letter:
204
205
            QString coordsStr;
205
206
            QStringList coords;
207
208
            do {
208
209
                line = ts.readLine();
209
210
 
210
 
                                if (line.isEmpty()) {
211
 
                                        continue;
212
 
                                }
213
 
                                
 
211
                if (line.isEmpty()) {
 
212
                    continue;
 
213
                }
 
214
 
214
215
                coordsStr = line.right(line.length()-2);
215
 
//                coords = QStringList::split(',', coordsStr);
 
216
                //                coords = QStringList::split(',', coordsStr);
216
217
                coords = coordsStr.split(',', QString::SkipEmptyParts);
217
218
                it2 = coords.begin();
218
219
 
296
297
            } else if (identifier.toLower()=="license") {
297
298
                fileLicense = value;
298
299
            } else if (identifier.toLower()=="encoding") {
299
 
                                ts.setCodec(QTextCodec::codecForName(value.toLatin1()));
300
 
                                encoding = value;
 
300
                ts.setCodec(QTextCodec::codecForName(value.toLatin1()));
 
301
                encoding = value;
301
302
            } else if (identifier.toLower()=="created") {
302
303
                fileCreate = value;
303
304
            }
322
323
                continue;
323
324
            }
324
325
 
325
 
            // create new letter:
326
 
            RS_FontChar* letter =
327
 
                new RS_FontChar(NULL, ch, RS_Vector(0.0, 0.0));
328
 
 
329
 
            // Read entities of this letter:
330
 
            QStringList vertex;
331
 
            QStringList coords;
332
 
 
 
326
            QStringList fontData;
333
327
            do {
334
328
                line = ts.readLine();
335
 
 
336
 
                if (line.isEmpty()) {
 
329
                if(line.isEmpty()) break;
 
330
                fontData.push_back(line);
 
331
            } while(true);
 
332
            if(fontData.size()>0) {
 
333
                rawLffFontList[QString(ch)]=fontData;
 
334
            }
 
335
 
 
336
 
 
337
        }
 
338
    }
 
339
    f.close();
 
340
}
 
341
 
 
342
void RS_Font::generateAllFonts(){
 
343
    QMap<QString, QStringList>::const_iterator i = rawLffFontList.constBegin();
 
344
    while (i != rawLffFontList.constEnd()) {
 
345
        generateLffFont(i.key());
 
346
        ++i;
 
347
    }
 
348
}
 
349
 
 
350
RS_Block* RS_Font::generateLffFont(const QString& ch){
 
351
        if(rawLffFontList.contains(ch) == false ){
 
352
                RS_DEBUG->print("RS_Font::generateLffFont(QChar %s ) : can not find the letter in given lff font file",qPrintable(ch));
 
353
                return NULL;
 
354
        }
 
355
    // create new letter:
 
356
    RS_FontChar* letter =
 
357
            new RS_FontChar(NULL, ch, RS_Vector(0.0, 0.0));
 
358
 
 
359
    // Read entities of this letter:
 
360
    QStringList vertex;
 
361
    QStringList coords;
 
362
    QStringList fontData=rawLffFontList[ch];
 
363
    QString line;
 
364
 
 
365
    while(fontData.isEmpty() == false) {
 
366
        line = fontData.takeFirst();
 
367
 
 
368
        if (line.isEmpty()) {
 
369
            continue;
 
370
        }
 
371
 
 
372
        // Defined char:
 
373
        if (line.at(0)=='C') {
 
374
            line.remove(0,1);
 
375
            int uCode = line.toInt(NULL, 16);
 
376
            QChar ch = QChar(uCode);
 
377
            RS_Block* bk = letterList.find(ch);
 
378
            if (bk == NULL && rawLffFontList.contains(ch) == true) {
 
379
                generateLffFont(ch);
 
380
                bk = letterList.find(ch);
 
381
            }
 
382
            if (bk != NULL) {
 
383
                RS_Entity* bk2 = bk->clone();
 
384
                bk2->setPen(RS_Pen(RS2::FlagInvalid));
 
385
                bk2->setLayer(NULL);
 
386
                letter->addEntity(bk2);
 
387
            }
 
388
        }
 
389
        //sequence:
 
390
        else {
 
391
            vertex = line.split(';', QString::SkipEmptyParts);
 
392
            //at least is required two vertex
 
393
            if (vertex.size()<2)
 
394
                continue;
 
395
            RS_Polyline* pline = new RS_Polyline(letter, RS_PolylineData());
 
396
            pline->setPen(RS_Pen(RS2::FlagInvalid));
 
397
            pline->setLayer(NULL);
 
398
            for (int i = 0; i < vertex.size(); ++i) {
 
399
                double x1, y1;
 
400
                double bulge = 0;
 
401
 
 
402
                coords = vertex.at(i).split(',', QString::SkipEmptyParts);
 
403
                //at least X,Y is required
 
404
                if (coords.size()<2)
337
405
                    continue;
338
 
                }
339
 
 
340
 
                // Defined char:
341
 
                if (line.at(0)=='C') {
342
 
                    line.remove(0,1);
343
 
                    int uCode = line.toInt(NULL, 16);
344
 
                    QChar ch = QChar(uCode);
345
 
                    RS_Block* bk = letterList.find(ch);
346
 
                    if (bk != NULL) {
347
 
                        RS_Entity* bk2 = bk->clone();
348
 
                        bk2->setPen(RS_Pen(RS2::FlagInvalid));
349
 
                        bk2->setLayer(NULL);
350
 
                        letter->addEntity(bk2);
351
 
                    }
352
 
                }
353
 
                //sequence:
354
 
                else {
355
 
                    vertex = line.split(';', QString::SkipEmptyParts);
356
 
                    //at least is required two vertex
357
 
                    if (vertex.size()<2)
358
 
                        continue;
359
 
                    RS_Polyline* pline = new RS_Polyline(letter, RS_PolylineData());
360
 
                    pline->setPen(RS_Pen(RS2::FlagInvalid));
361
 
                    pline->setLayer(NULL);
362
 
                    for (int i = 0; i < vertex.size(); ++i) {
363
 
                        double x1, y1;
364
 
                        double bulge = 0;
365
 
 
366
 
                        coords = vertex.at(i).split(',', QString::SkipEmptyParts);
367
 
                        //at least X,Y is required
368
 
                        if (coords.size()<2)
369
 
                            continue;
370
 
                        x1 = coords.at(0).toDouble();
371
 
                        y1 = coords.at(1).toDouble();
372
 
                        //check presence of bulge
373
 
                        if (coords.size() == 3 && coords.at(2).at(0) == QChar('A')){
374
 
                            QString bulgeStr = coords.at(2);
375
 
                            bulge = bulgeStr.remove(0,1).toDouble();
376
 
                        }
377
 
                        pline->setNextBulge(bulge);
378
 
                        pline->addVertex(RS_Vector(x1, y1), bulge);
379
 
                    }
380
 
                    letter->addEntity(pline);
381
 
                }
382
 
 
383
 
            } while (!line.isEmpty());
384
 
 
385
 
            if (letter->isEmpty()) {
386
 
                delete letter;
387
 
            } else {
388
 
                letter->calculateBorders();
389
 
                letterList.add(letter);
 
406
                x1 = coords.at(0).toDouble();
 
407
                y1 = coords.at(1).toDouble();
 
408
                //check presence of bulge
 
409
                if (coords.size() == 3 && coords.at(2).at(0) == QChar('A')){
 
410
                    QString bulgeStr = coords.at(2);
 
411
                    bulge = bulgeStr.remove(0,1).toDouble();
 
412
                }
 
413
                pline->setNextBulge(bulge);
 
414
                pline->addVertex(RS_Vector(x1, y1), bulge);
390
415
            }
 
416
            letter->addEntity(pline);
391
417
        }
392
 
    }
393
 
    f.close();
394
 
}
395
 
 
396
 
 
 
418
 
 
419
    }
 
420
 
 
421
    if (letter->isEmpty()) {
 
422
        delete letter;
 
423
            return NULL;
 
424
    } else {
 
425
        letter->calculateBorders();
 
426
        letterList.add(letter);
 
427
        return letter;
 
428
    }
 
429
}
 
430
 
 
431
RS_Block* RS_Font::findLetter(const QString& name) {
 
432
    RS_Block* ret= letterList.find(name);
 
433
    if (ret != NULL) return ret;
 
434
    return generateLffFont(name);
 
435
 
 
436
}
397
437
/**
398
438
 * Dumps the fonts data to stdout.
399
439
 */