1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef DISTANCEFIELDMODEL_H
5#define DISTANCEFIELDMODEL_H
6
7#include <QAbstractListModel>
8#include <QRawFont>
9#include <QtGui/qpainterpath.h>
10#include <QtGui/private/qtextengine_p.h>
11#include <QMultiHash>
12#include <QScopedPointer>
13#include <QMetaEnum>
14#include <QThread>
15
16QT_BEGIN_NAMESPACE
17
18class QThread;
19class DistanceFieldModelWorker;
20class DistanceFieldModel : public QAbstractListModel
21{
22 Q_OBJECT
23public:
24 enum UnicodeRange {
25 Other = 0x00,
26 BasicLatin = 0x20,
27 Latin1Supplement = 0xa0,
28 LatinExtendedA = 0x100,
29 LatinExtendedB = 0x180,
30 IPAExtensions = 0x250,
31 SpacingModifierLetters = 0x2b0,
32 CombiningDiacriticalMarks = 0x300,
33 GreekAndCoptic = 0x370,
34 Cyrillic = 0x400,
35 CyrillicSupplementary = 0x500,
36 Armenian = 0x530,
37 Hebrew = 0x590,
38 Arabic = 0x600,
39 Syriac = 0x700,
40 Thaana = 0x780,
41 Devanagari = 0x900,
42 Bengali = 0x980,
43 Gurmukhi = 0xa00,
44 Gujarati = 0xa80,
45 Oriya = 0xb00,
46 Tamil = 0xb80,
47 Telugu = 0xc00,
48 Kannada = 0xc80,
49 Malayalam = 0xd00,
50 Sinhala = 0xd80,
51 Thai = 0xe00,
52 Lao = 0xe80,
53 Tibetan = 0xf00,
54 Myanmar = 0x1000,
55 Georgian = 0x10a0,
56 HangulJamo = 0x1100,
57 Ethiopic = 0x1200,
58 Cherokee = 0x13a0,
59 UnifiedCanadianAboriginalSyllabics = 0x1400,
60 Ogham = 0x1680,
61 Runic = 0x16a0,
62 Tagalog = 0x1700,
63 Hanunoo = 0x1720,
64 Buhid = 0x1740,
65 Tagbanwa = 0x1760,
66 Khmer = 0x1780,
67 Mongolian = 0x1800,
68 Limbu = 0x1900,
69 TaiLe = 0x1950,
70 KhmerSymbols = 0x19e0,
71 PhoneticExtensions = 0x1d00,
72 LatinExtendedAdditional = 0x1e00,
73 GreekExtended = 0x1f00,
74 GeneralPunctuation = 0x2000,
75 SuperscriptsAndSubscripts = 0x2070,
76 CurrencySymbols = 0x20a0,
77 CombiningDiacriticalMarksForSymbols = 0x20d0,
78 LetterlikeSymbols = 0x2100,
79 NumberForms = 0x2150,
80 Arrows = 0x2190,
81 MathematicalOperators = 0x2200,
82 MiscellaneousTechnical = 0x2300,
83 ControlPictures = 0x2400,
84 OpticalCharacterRecognition = 0x2440,
85 EnclosedAlphanumerics = 0x2460,
86 BoxDrawing = 0x2500,
87 BlockElements = 0x2580,
88 GeometricShapes = 0x25a0,
89 MiscellaneousSymbols = 0x2600,
90 Dingbats = 0x2700,
91 MiscellaneousMathematicalSymbolsA = 0x27c0,
92 SupplementalArrowsA = 0x27f0,
93 BraillePatterns = 0x2800,
94 SupplementalArrowsB = 0x2900,
95 MiscellaneousMathematicalSymbolsB = 0x2980,
96 SupplementalMathematicalOperators = 0x2a00,
97 MiscellaneousSymbolsAndArrows = 0x2b00,
98 CJKRadicalsSupplement = 0x2e80,
99 KangxiRadicals = 0x2f00,
100 IdeographicDescriptionCharacters = 0x2ff0,
101 CJKSymbolsAndPunctuation = 0x3000,
102 Hiragana = 0x3040,
103 Katakana = 0x30a0,
104 Bopomofo = 0x3100,
105 HangulCompatibilityJamo = 0x3130,
106 Kanbun = 0x3190,
107 BopomofoExtended = 0x31a0,
108 KatakanaPhoneticExtensions = 0x31f0,
109 EnclosedCJKLettersAndMonths = 0x3200,
110 CJKCompatibility = 0x3300,
111 CJKUnifiedIdeographsExtensionA = 0x3400,
112 YijingHexagramSymbols = 0x4dc0,
113 CJKUnifiedIdeographs = 0x4e00,
114 YiSyllables = 0xa000,
115 YiRadicals = 0xa490,
116 HangulSyllables = 0xac00,
117 HighSurrogates = 0xd800,
118 HighPrivateUseSurrogates = 0xdb80,
119 LowSurrogates = 0xdc00,
120 PrivateUseArea = 0xe000,
121 CJKCompatibilityIdeographs = 0xf900,
122 AlphabeticPresentationForms = 0xfb00,
123 ArabicPresentationFormsA = 0xfb50,
124 VariationSelectors = 0xfe00,
125 CombiningHalfMarks = 0xfe20,
126 CJKCompatibilityForms = 0xfe30,
127 SmallFormVariants = 0xfe50,
128 ArabicPresentationFormsB = 0xfe70,
129 HalfwidthAndFullwidthForms = 0xff00,
130 Specials = 0xfff0,
131 LinearBSyllabary = 0x10000,
132 LinearBIdeograms = 0x10080,
133 AegeanNumbers = 0x10100,
134 OldItalic = 0x10300,
135 Gothic = 0x10330,
136 Ugaritic = 0x10380,
137 Deseret = 0x10400,
138 Shavian = 0x10450,
139 Osmanya = 0x10480,
140 CypriotSyllabary = 0x10800,
141 ByzantineMusicalSymbols = 0x1d000,
142 MusicalSymbols = 0x1d100,
143 TaiXuanJingSymbols = 0x1d300,
144 MathematicalAlphanumericSymbols = 0x1d400,
145 CJKUnifiedIdeographsExtensionB = 0x20000,
146 CJKCompatibilityIdeographsSupplement = 0x2f800,
147 Tags = 0xe0000,
148 End = 0xe007f
149 };
150 Q_ENUM(UnicodeRange)
151
152 explicit DistanceFieldModel(QObject *parent = nullptr);
153 ~DistanceFieldModel() override;
154
155 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
156
157 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
158 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
159
160 void setFont(const QString &fileName);
161
162 QList<UnicodeRange> unicodeRanges() const;
163 QList<glyph_t> glyphIndexesForUnicodeRange(UnicodeRange range) const;
164 QString nameForUnicodeRange(UnicodeRange range) const;
165 glyph_t glyphIndexForUcs4(quint32 ucs4) const;
166
167 QImage distanceField(int row) const
168 {
169 return m_distanceFields.at(i: row);
170 }
171
172 QPainterPath path(int row) const
173 {
174 return m_paths.at(i: row);
175 }
176
177 qreal pixelSize() const { return m_pixelSize; }
178 bool doubleGlyphResolution() const { return m_doubleGlyphResolution; }
179
180signals:
181 void startGeneration(quint16 glyphCount);
182 void stopGeneration();
183 void distanceFieldGenerated();
184 void error(const QString &errorString);
185
186private slots:
187 void addDistanceField(const QImage &distanceField,
188 const QPainterPath &path,
189 glyph_t glyphId,
190 quint32 ucs4);
191 void reserveSpace(quint16 glyphCount,
192 bool doubleResolution,
193 qreal pixelSize);
194
195private:
196 UnicodeRange unicodeRangeForUcs4(quint32 ucs4) const;
197
198 QRawFont m_font;
199 DistanceFieldModelWorker *m_worker;
200 QScopedPointer<QThread> m_workerThread;
201 quint16 m_glyphCount;
202 QList<QImage> m_distanceFields;
203 QList<QPainterPath> m_paths;
204 QMultiHash<UnicodeRange, glyph_t> m_glyphsPerUnicodeRange;
205 QHash<quint32, glyph_t> m_glyphsPerUcs4;
206 bool m_doubleGlyphResolution;
207 qreal m_pixelSize;
208 QMetaEnum m_rangeEnum;
209};
210
211QT_END_NAMESPACE
212
213#endif // DISTANCEFIELDMODEL_H
214

source code of qttools/src/distancefieldgenerator/distancefieldmodel.h