/
qgsfontutils.cpp
537 lines (471 loc) · 15.3 KB
/
qgsfontutils.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/***************************************************************************
qgsfontutils.h
---------------------
begin : June 5, 2013
copyright : (C) 2013 by Larry Shaffer
email : larrys at dakotacarto dot 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsfontutils.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgssettings.h"
#include <QApplication>
#include <QFile>
#include <QFont>
#include <QFontDatabase>
#include <QFontInfo>
#include <QStringList>
#include <QMimeData>
#include <memory>
bool QgsFontUtils::fontMatchOnSystem( const QFont &f )
{
QFontInfo fi = QFontInfo( f );
return fi.exactMatch();
}
bool QgsFontUtils::fontFamilyOnSystem( const QString &family )
{
QFont tmpFont = QFont( family );
// compare just beginning of family string in case 'family [foundry]' differs
return tmpFont.family().startsWith( family, Qt::CaseInsensitive );
}
bool QgsFontUtils::fontFamilyHasStyle( const QString &family, const QString &style )
{
QFontDatabase fontDB;
if ( !fontFamilyOnSystem( family ) )
return false;
if ( fontDB.styles( family ).contains( style ) )
return true;
#ifdef Q_OS_WIN
QString modified( style );
if ( style == "Roman" )
modified = "Normal";
if ( style == "Oblique" )
modified = "Italic";
if ( style == "Bold Oblique" )
modified = "Bold Italic";
if ( fontDB.styles( family ).contains( modified ) )
return true;
#endif
return false;
}
bool QgsFontUtils::fontFamilyMatchOnSystem( const QString &family, QString *chosen, bool *match )
{
QFontDatabase fontDB;
QStringList fontFamilies = fontDB.families();
bool found = false;
QList<QString>::const_iterator it = fontFamilies.constBegin();
for ( ; it != fontFamilies.constEnd(); ++it )
{
// first compare just beginning of 'family [foundry]' string
if ( it->startsWith( family, Qt::CaseInsensitive ) )
{
found = true;
// keep looking if match info is requested
if ( match )
{
// full 'family [foundry]' strings have to match
*match = ( *it == family );
if ( *match )
break;
}
else
{
break;
}
}
}
if ( found )
{
if ( chosen )
{
// retrieve the family actually assigned by matching algorithm
QFont f = QFont( family );
*chosen = f.family();
}
}
else
{
if ( chosen )
{
*chosen = QString();
}
if ( match )
{
*match = false;
}
}
return found;
}
bool QgsFontUtils::updateFontViaStyle( QFont &f, const QString &fontstyle, bool fallback )
{
if ( fontstyle.isEmpty() )
{
return false;
}
QFontDatabase fontDB;
if ( !fallback )
{
// does the font even have the requested style?
bool hasstyle = fontFamilyHasStyle( f.family(), fontstyle );
if ( !hasstyle )
{
return false;
}
}
// is the font's style already the same as requested?
if ( fontstyle == fontDB.styleString( f ) )
{
return false;
}
QFont appfont = QApplication::font();
int defaultSize = appfont.pointSize(); // QFontDatabase::font() needs an integer for size
QFont styledfont;
bool foundmatch = false;
// if fontDB.font() fails, it returns the default app font; but, that may be the target style
styledfont = fontDB.font( f.family(), fontstyle, defaultSize );
if ( appfont != styledfont || fontstyle != fontDB.styleString( f ) )
{
foundmatch = true;
}
// default to first found style if requested style is unavailable
// this helps in the situations where the passed-in font has to have a named style applied
if ( fallback && !foundmatch )
{
QFont testFont = QFont( f );
testFont.setPointSize( defaultSize );
// prefer a style that mostly matches the passed-in font
Q_FOREACH ( const QString &style, fontDB.styles( f.family() ) )
{
styledfont = fontDB.font( f.family(), style, defaultSize );
styledfont = styledfont.resolve( f );
if ( testFont.toString() == styledfont.toString() )
{
foundmatch = true;
break;
}
}
// fallback to first style found that works
if ( !foundmatch )
{
Q_FOREACH ( const QString &style, fontDB.styles( f.family() ) )
{
styledfont = fontDB.font( f.family(), style, defaultSize );
if ( QApplication::font() != styledfont )
{
foundmatch = true;
break;
}
}
}
}
// similar to QFont::resolve, but font may already have pixel size set
// and we want to make sure that's preserved
if ( foundmatch )
{
if ( !qgsDoubleNear( f.pointSizeF(), -1 ) )
{
styledfont.setPointSizeF( f.pointSizeF() );
}
else if ( f.pixelSize() != -1 )
{
styledfont.setPixelSize( f.pixelSize() );
}
styledfont.setCapitalization( f.capitalization() );
styledfont.setUnderline( f.underline() );
styledfont.setStrikeOut( f.strikeOut() );
styledfont.setWordSpacing( f.wordSpacing() );
styledfont.setLetterSpacing( QFont::AbsoluteSpacing, f.letterSpacing() );
f = styledfont;
return true;
}
return false;
}
QString QgsFontUtils::standardTestFontFamily()
{
return QStringLiteral( "QGIS Vera Sans" );
}
bool QgsFontUtils::loadStandardTestFonts( const QStringList &loadstyles )
{
// load standard test font from filesystem or testdata.qrc (for unit tests and general testing)
bool fontsLoaded = false;
QString fontFamily = standardTestFontFamily();
QMap<QString, QString> fontStyles;
fontStyles.insert( QStringLiteral( "Roman" ), QStringLiteral( "QGIS-Vera/QGIS-Vera.ttf" ) );
fontStyles.insert( QStringLiteral( "Oblique" ), QStringLiteral( "QGIS-Vera/QGIS-VeraIt.ttf" ) );
fontStyles.insert( QStringLiteral( "Bold" ), QStringLiteral( "QGIS-Vera/QGIS-VeraBd.ttf" ) );
fontStyles.insert( QStringLiteral( "Bold Oblique" ), QStringLiteral( "QGIS-Vera/QGIS-VeraBI.ttf" ) );
QMap<QString, QString>::const_iterator f = fontStyles.constBegin();
for ( ; f != fontStyles.constEnd(); ++f )
{
QString fontstyle( f.key() );
QString fontpath( f.value() );
if ( !( loadstyles.contains( fontstyle ) || loadstyles.contains( QStringLiteral( "All" ) ) ) )
{
continue;
}
if ( fontFamilyHasStyle( fontFamily, fontstyle ) )
{
QgsDebugMsg( QString( "Test font '%1 %2' already available" ).arg( fontFamily, fontstyle ) );
}
else
{
bool loaded = false;
if ( QgsApplication::isRunningFromBuildDir() )
{
// workaround for bugs with Qt 4.8.5 (other versions?) on Mac 10.9, where fonts
// from qrc resources load but fail to work and default font is substituted [LS]:
// https://bugreports.qt.io/browse/QTBUG-30917
// https://bugreports.qt.io/browse/QTBUG-32789
QString fontPath( QgsApplication::buildSourcePath() + "/tests/testdata/font/" + fontpath );
int fontID = QFontDatabase::addApplicationFont( fontPath );
loaded = ( fontID != -1 );
fontsLoaded = ( fontsLoaded || loaded );
QgsDebugMsg( QString( "Test font '%1 %2' %3 from filesystem [%4]" )
.arg( fontFamily, fontstyle, loaded ? "loaded" : "FAILED to load", fontPath ) );
QFontDatabase db;
QgsDebugMsg( QString( "font families in %1: %2" ).arg( fontID ).arg( db.applicationFontFamilies( fontID ).join( "," ) ) );
}
else
{
QFile fontResource( ":/testdata/font/" + fontpath );
if ( fontResource.open( QIODevice::ReadOnly ) )
{
int fontID = QFontDatabase::addApplicationFontFromData( fontResource.readAll() );
loaded = ( fontID != -1 );
fontsLoaded = ( fontsLoaded || loaded );
}
QgsDebugMsg( QString( "Test font '%1' (%2) %3 from testdata.qrc" )
.arg( fontFamily, fontstyle, loaded ? "loaded" : "FAILED to load" ) );
}
}
}
return fontsLoaded;
}
QFont QgsFontUtils::getStandardTestFont( const QString &style, int pointsize )
{
if ( ! fontFamilyHasStyle( standardTestFontFamily(), style ) )
{
loadStandardTestFonts( QStringList() << style );
}
QFontDatabase fontDB;
QFont f = fontDB.font( standardTestFontFamily(), style, pointsize );
#ifdef Q_OS_WIN
if ( !f.exactMatch() )
{
QString modified;
if ( style == "Roman" )
modified = "Normal";
else if ( style == "Oblique" )
modified = "Italic";
else if ( style == "Bold Oblique" )
modified = "Bold Italic";
if ( !modified.isEmpty() )
f = fontDB.font( standardTestFontFamily(), modified, pointsize );
}
if ( !f.exactMatch() )
{
QgsDebugMsg( QString( "Inexact font match - consider installing the %1 font." ).arg( standardTestFontFamily() ) );
QgsDebugMsg( QString( "Requested: %1" ).arg( f.toString() ) );
QFontInfo fi( f );
QgsDebugMsg( QString( "Replaced: %1,%2,%3,%4,%5,%6,%7,%8,%9,%10" ).arg( fi.family() ).arg( fi.pointSizeF() ).arg( fi.pixelSize() ).arg( fi.styleHint() ).arg( fi.weight() ).arg( fi.style() ).arg( fi.underline() ).arg( fi.strikeOut() ).arg( fi.fixedPitch() ).arg( fi.rawMode() ) );
}
#endif
// in case above statement fails to set style
f.setBold( style.contains( QLatin1String( "Bold" ) ) );
f.setItalic( style.contains( QLatin1String( "Oblique" ) ) || style.contains( QLatin1String( "Italic" ) ) );
return f;
}
QDomElement QgsFontUtils::toXmlElement( const QFont &font, QDomDocument &document, const QString &elementName )
{
QDomElement fontElem = document.createElement( elementName );
fontElem.setAttribute( QStringLiteral( "description" ), font.toString() );
fontElem.setAttribute( QStringLiteral( "style" ), untranslateNamedStyle( font.styleName() ) );
return fontElem;
}
bool QgsFontUtils::setFromXmlElement( QFont &font, const QDomElement &element )
{
if ( element.isNull() )
{
return false;
}
font.fromString( element.attribute( QStringLiteral( "description" ) ) );
if ( element.hasAttribute( QStringLiteral( "style" ) ) )
{
( void )updateFontViaStyle( font, translateNamedStyle( element.attribute( QStringLiteral( "style" ) ) ) );
}
return true;
}
bool QgsFontUtils::setFromXmlChildNode( QFont &font, const QDomElement &element, const QString &childNode )
{
if ( element.isNull() )
{
return false;
}
QDomNodeList nodeList = element.elementsByTagName( childNode );
if ( !nodeList.isEmpty() )
{
QDomElement fontElem = nodeList.at( 0 ).toElement();
return setFromXmlElement( font, fontElem );
}
else
{
return false;
}
}
QMimeData *QgsFontUtils::toMimeData( const QFont &font )
{
std::unique_ptr< QMimeData >mimeData( new QMimeData );
QDomDocument fontDoc;
QDomElement fontElem = toXmlElement( font, fontDoc, QStringLiteral( "font" ) );
fontDoc.appendChild( fontElem );
mimeData->setText( fontDoc.toString() );
return mimeData.release();
}
QFont QgsFontUtils::fromMimeData( const QMimeData *data, bool *ok )
{
QFont font;
if ( ok )
*ok = false;
if ( !data )
return font;
QString text = data->text();
if ( !text.isEmpty() )
{
QDomDocument doc;
QDomElement elem;
if ( doc.setContent( text ) )
{
elem = doc.documentElement();
if ( elem.nodeName() != QStringLiteral( "font" ) )
elem = elem.firstChildElement( QStringLiteral( "font" ) );
if ( setFromXmlElement( font, elem ) )
{
if ( ok )
*ok = true;
}
return font;
}
}
return font;
}
static QMap<QString, QString> createTranslatedStyleMap()
{
QMap<QString, QString> translatedStyleMap;
QStringList words = QStringList() << QStringLiteral( "Normal" ) << QStringLiteral( "Light" ) << QStringLiteral( "Bold" ) << QStringLiteral( "Black" ) << QStringLiteral( "Demi" ) << QStringLiteral( "Italic" ) << QStringLiteral( "Oblique" );
Q_FOREACH ( const QString &word, words )
{
translatedStyleMap.insert( QCoreApplication::translate( "QFontDatabase", qPrintable( word ) ), word );
}
return translatedStyleMap;
}
QString QgsFontUtils::translateNamedStyle( const QString &namedStyle )
{
QStringList words = namedStyle.split( ' ', QString::SkipEmptyParts );
for ( int i = 0, n = words.length(); i < n; ++i )
{
words[i] = QCoreApplication::translate( "QFontDatabase", words[i].toUtf8(), nullptr, QCoreApplication::UnicodeUTF8 );
}
return words.join( QStringLiteral( " " ) );
}
QString QgsFontUtils::untranslateNamedStyle( const QString &namedStyle )
{
static QMap<QString, QString> translatedStyleMap = createTranslatedStyleMap();
QStringList words = namedStyle.split( ' ', QString::SkipEmptyParts );
for ( int i = 0, n = words.length(); i < n; ++i )
{
if ( translatedStyleMap.contains( words[i] ) )
{
words[i] = translatedStyleMap.value( words[i] );
}
else
{
QgsDebugMsg( QString( "Warning: style map does not contain %1" ).arg( words[i] ) );
}
}
return words.join( QStringLiteral( " " ) );
}
QString QgsFontUtils::asCSS( const QFont &font, double pointToPixelScale )
{
QString css = QStringLiteral( "font-family: " ) + font.family() + ';';
//style
css += QLatin1String( "font-style: " );
switch ( font.style() )
{
case QFont::StyleNormal:
css += QLatin1String( "normal" );
break;
case QFont::StyleItalic:
css += QLatin1String( "italic" );
break;
case QFont::StyleOblique:
css += QLatin1String( "oblique" );
break;
}
css += ';';
//weight
int cssWeight = 400;
switch ( font.weight() )
{
case QFont::Light:
cssWeight = 300;
break;
case QFont::Normal:
cssWeight = 400;
break;
case QFont::DemiBold:
cssWeight = 600;
break;
case QFont::Bold:
cssWeight = 700;
break;
case QFont::Black:
cssWeight = 900;
break;
#if QT_VERSION >= 0x050500
case QFont::Thin:
cssWeight = 100;
break;
case QFont::ExtraLight:
cssWeight = 200;
break;
case QFont::Medium:
cssWeight = 500;
break;
case QFont::ExtraBold:
cssWeight = 800;
break;
#endif
}
css += QStringLiteral( "font-weight: %1;" ).arg( cssWeight );
//size
css += QStringLiteral( "font-size: %1px;" ).arg( font.pointSizeF() >= 0 ? font.pointSizeF() * pointToPixelScale : font.pixelSize() );
return css;
}
void QgsFontUtils::addRecentFontFamily( const QString &family )
{
if ( family.isEmpty() )
{
return;
}
QgsSettings settings;
QStringList recentFamilies = settings.value( QStringLiteral( "fonts/recent" ) ).toStringList();
//remove matching families
recentFamilies.removeAll( family );
//then add to start of list
recentFamilies.prepend( family );
//trim to 10 fonts
recentFamilies = recentFamilies.mid( 0, 10 );
settings.setValue( QStringLiteral( "fonts/recent" ), recentFamilies );
}
QStringList QgsFontUtils::recentFontFamilies()
{
QgsSettings settings;
return settings.value( QStringLiteral( "fonts/recent" ) ).toStringList();
}