1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
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.
13 **
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.
18 **
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
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 
27 #include <iostream>
28 #include <QHash>
29 #include "rs_fontlist.h"
30 #include "rs_debug.h"
31 #include "rs_font.h"
32 #include "rs_system.h"
33 
34 RS_FontList* RS_FontList::uniqueInstance = nullptr;
35 
instance()36 RS_FontList* RS_FontList::instance() {
37 	if (!uniqueInstance) {
38 		uniqueInstance = new RS_FontList();
39 	}
40 	return uniqueInstance;
41 }
42 
43 
44 /**
45  * Initializes the font list by creating empty RS_Font
46  * objects, one for each font that could be found.
47  */
init()48 void RS_FontList::init() {
49     RS_DEBUG->print("RS_FontList::initFonts");
50 
51     QStringList list = RS_SYSTEM->getNewFontList();
52     list.append(RS_SYSTEM->getFontList());
53     QHash<QString, int> added; //used to remember added fonts (avoid duplication)
54 
55     for (int i = 0; i < list.size(); ++i) {
56         RS_DEBUG->print("font: %s:", list.at(i).toLatin1().data());
57 
58         QFileInfo fi( list.at(i) );
59         if ( !added.contains(fi.baseName()) ) {
60 			fonts.emplace_back(new RS_Font(fi.baseName()));
61             added.insert(fi.baseName(), 1);
62         }
63 
64         RS_DEBUG->print("base: %s", fi.baseName().toLatin1().data());
65     }
66 }
67 
countFonts() const68 size_t RS_FontList::countFonts() const{
69 	return fonts.size();
70 }
71 
begin() const72 std::vector<std::unique_ptr<RS_Font> >::const_iterator RS_FontList::begin() const
73 {
74 	return fonts.begin();
75 }
76 
end() const77 std::vector<std::unique_ptr<RS_Font> >::const_iterator RS_FontList::end() const
78 {
79 	return fonts.end();
80 }
81 
82 /**
83  * Removes all fonts in the fontlist.
84  */
clearFonts()85 void RS_FontList::clearFonts() {
86 	fonts.clear();
87 }
88 
89 /**
90  * @return Pointer to the font with the given name or
91  * \p NULL if no such font was found. The font will be loaded into
92  * memory if it's not already.
93  */
requestFont(const QString & name)94 RS_Font* RS_FontList::requestFont(const QString& name) {
95     RS_DEBUG->print("RS_FontList::requestFont %s",  name.toLatin1().data());
96 
97     QString name2 = name.toLower();
98     RS_Font* foundFont = NULL;
99 
100     // QCAD 1 compatibility:
101     if (name2.contains('#') && name2.contains('_')) {
102         name2 = name2.left(name2.indexOf('_'));
103     } else if (name2.contains('#')) {
104         name2 = name2.left(name2.indexOf('#'));
105     }
106 
107     RS_DEBUG->print("name2: %s", name2.toLatin1().data());
108 
109 	// Search our list of available fonts:
110 	for( auto const& f: fonts){
111 
112         if (f->getFileName()==name2) {
113             // Make sure this font is loaded into memory:
114             f->loadFont();
115 			foundFont = f.get();
116             break;
117         }
118     }
119 
120 	if (!foundFont && name!="standard") {
121         foundFont = requestFont("standard");
122     }
123 
124     return foundFont;
125 }
126 
127 /**
128  * Dumps the fonts to stdout.
129  */
operator <<(std::ostream & os,RS_FontList & l)130 std::ostream& operator << (std::ostream& os, RS_FontList& l) {
131 
132     os << "Fontlist: \n";
133 	for(auto const& f: l.fonts){
134         os << *f << "\n";
135     }
136 
137     return os;
138 }
139 
140 
141 
142 
143