Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/text/qfontmetrics.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qfont.h" | - | ||||||||||||
35 | #include "qpaintdevice.h" | - | ||||||||||||
36 | #include "qfontmetrics.h" | - | ||||||||||||
37 | - | |||||||||||||
38 | #include "qfont_p.h" | - | ||||||||||||
39 | #include "qfontengine_p.h" | - | ||||||||||||
40 | - | |||||||||||||
41 | QT_BEGIN_NAMESPACE | - | ||||||||||||
42 | - | |||||||||||||
43 | - | |||||||||||||
44 | extern void qt_format_text(const QFont& font, const QRectF &_r, | - | ||||||||||||
45 | int tf, const QString &text, QRectF *brect, | - | ||||||||||||
46 | int tabStops, int *tabArray, int tabArrayLen, | - | ||||||||||||
47 | QPainter *painter); | - | ||||||||||||
48 | - | |||||||||||||
49 | /***************************************************************************** | - | ||||||||||||
50 | QFontMetrics member functions | - | ||||||||||||
51 | *****************************************************************************/ | - | ||||||||||||
52 | - | |||||||||||||
53 | /*! | - | ||||||||||||
54 | \class QFontMetrics | - | ||||||||||||
55 | \reentrant | - | ||||||||||||
56 | \inmodule QtGui | - | ||||||||||||
57 | - | |||||||||||||
58 | \brief The QFontMetrics class provides font metrics information. | - | ||||||||||||
59 | - | |||||||||||||
60 | \ingroup painting | - | ||||||||||||
61 | \ingroup shared | - | ||||||||||||
62 | - | |||||||||||||
63 | QFontMetrics functions calculate the size of characters and | - | ||||||||||||
64 | strings for a given font. There are three ways you can create a | - | ||||||||||||
65 | QFontMetrics object: | - | ||||||||||||
66 | - | |||||||||||||
67 | \list 1 | - | ||||||||||||
68 | \li Calling the QFontMetrics constructor with a QFont creates a | - | ||||||||||||
69 | font metrics object for a screen-compatible font, i.e. the font | - | ||||||||||||
70 | cannot be a printer font. If the font is changed | - | ||||||||||||
71 | later, the font metrics object is \e not updated. | - | ||||||||||||
72 | - | |||||||||||||
73 | (Note: If you use a printer font the values returned may be | - | ||||||||||||
74 | inaccurate. Printer fonts are not always accessible so the nearest | - | ||||||||||||
75 | screen font is used if a printer font is supplied.) | - | ||||||||||||
76 | - | |||||||||||||
77 | \li QWidget::fontMetrics() returns the font metrics for a widget's | - | ||||||||||||
78 | font. This is equivalent to QFontMetrics(widget->font()). If the | - | ||||||||||||
79 | widget's font is changed later, the font metrics object is \e not | - | ||||||||||||
80 | updated. | - | ||||||||||||
81 | - | |||||||||||||
82 | \li QPainter::fontMetrics() returns the font metrics for a | - | ||||||||||||
83 | painter's current font. If the painter's font is changed later, the | - | ||||||||||||
84 | font metrics object is \e not updated. | - | ||||||||||||
85 | \endlist | - | ||||||||||||
86 | - | |||||||||||||
87 | Once created, the object provides functions to access the | - | ||||||||||||
88 | individual metrics of the font, its characters, and for strings | - | ||||||||||||
89 | rendered in the font. | - | ||||||||||||
90 | - | |||||||||||||
91 | There are several functions that operate on the font: ascent(), | - | ||||||||||||
92 | descent(), height(), leading() and lineSpacing() return the basic | - | ||||||||||||
93 | size properties of the font. The underlinePos(), overlinePos(), | - | ||||||||||||
94 | strikeOutPos() and lineWidth() functions, return the properties of | - | ||||||||||||
95 | the line that underlines, overlines or strikes out the | - | ||||||||||||
96 | characters. These functions are all fast. | - | ||||||||||||
97 | - | |||||||||||||
98 | There are also some functions that operate on the set of glyphs in | - | ||||||||||||
99 | the font: minLeftBearing(), minRightBearing() and maxWidth(). | - | ||||||||||||
100 | These are by necessity slow, and we recommend avoiding them if | - | ||||||||||||
101 | possible. | - | ||||||||||||
102 | - | |||||||||||||
103 | For each character, you can get its width(), leftBearing() and | - | ||||||||||||
104 | rightBearing() and find out whether it is in the font using | - | ||||||||||||
105 | inFont(). You can also treat the character as a string, and use | - | ||||||||||||
106 | the string functions on it. | - | ||||||||||||
107 | - | |||||||||||||
108 | The string functions include width(), to return the width of a | - | ||||||||||||
109 | string in pixels (or points, for a printer), boundingRect(), to | - | ||||||||||||
110 | return a rectangle large enough to contain the rendered string, | - | ||||||||||||
111 | and size(), to return the size of that rectangle. | - | ||||||||||||
112 | - | |||||||||||||
113 | Example: | - | ||||||||||||
114 | \snippet code/src_gui_text_qfontmetrics.cpp 0 | - | ||||||||||||
115 | - | |||||||||||||
116 | \sa QFont, QFontInfo, QFontDatabase, {Character Map Example} | - | ||||||||||||
117 | */ | - | ||||||||||||
118 | - | |||||||||||||
119 | /*! | - | ||||||||||||
120 | \fn QRect QFontMetrics::boundingRect(int x, int y, int width, int height, | - | ||||||||||||
121 | int flags, const QString &text, int tabStops, int *tabArray) const | - | ||||||||||||
122 | \overload | - | ||||||||||||
123 | - | |||||||||||||
124 | Returns the bounding rectangle for the given \a text within the | - | ||||||||||||
125 | rectangle specified by the \a x and \a y coordinates, \a width, and | - | ||||||||||||
126 | \a height. | - | ||||||||||||
127 | - | |||||||||||||
128 | If Qt::TextExpandTabs is set in \a flags and \a tabArray is | - | ||||||||||||
129 | non-null, it specifies a 0-terminated sequence of pixel-positions | - | ||||||||||||
130 | for tabs; otherwise, if \a tabStops is non-zero, it is used as the | - | ||||||||||||
131 | tab spacing (in pixels). | - | ||||||||||||
132 | */ | - | ||||||||||||
133 | - | |||||||||||||
134 | /*! | - | ||||||||||||
135 | Constructs a font metrics object for \a font. | - | ||||||||||||
136 | - | |||||||||||||
137 | The font metrics will be compatible with the paintdevice used to | - | ||||||||||||
138 | create \a font. | - | ||||||||||||
139 | - | |||||||||||||
140 | The font metrics object holds the information for the font that is | - | ||||||||||||
141 | passed in the constructor at the time it is created, and is not | - | ||||||||||||
142 | updated if the font's attributes are changed later. | - | ||||||||||||
143 | - | |||||||||||||
144 | Use QFontMetrics(const QFont &, QPaintDevice *) to get the font | - | ||||||||||||
145 | metrics that are compatible with a certain paint device. | - | ||||||||||||
146 | */ | - | ||||||||||||
147 | QFontMetrics::QFontMetrics(const QFont &font) | - | ||||||||||||
148 | : d(font.d.data()) | - | ||||||||||||
149 | { | - | ||||||||||||
150 | } never executed: end of block | 0 | ||||||||||||
151 | - | |||||||||||||
152 | /*! | - | ||||||||||||
153 | Constructs a font metrics object for \a font and \a paintdevice. | - | ||||||||||||
154 | - | |||||||||||||
155 | The font metrics will be compatible with the paintdevice passed. | - | ||||||||||||
156 | If the \a paintdevice is 0, the metrics will be screen-compatible, | - | ||||||||||||
157 | ie. the metrics you get if you use the font for drawing text on a | - | ||||||||||||
158 | \l{QWidget}{widgets} or \l{QPixmap}{pixmaps}, | - | ||||||||||||
159 | not on a QPicture or QPrinter. | - | ||||||||||||
160 | - | |||||||||||||
161 | The font metrics object holds the information for the font that is | - | ||||||||||||
162 | passed in the constructor at the time it is created, and is not | - | ||||||||||||
163 | updated if the font's attributes are changed later. | - | ||||||||||||
164 | */ | - | ||||||||||||
165 | QFontMetrics::QFontMetrics(const QFont &font, QPaintDevice *paintdevice) | - | ||||||||||||
166 | { | - | ||||||||||||
167 | int dpi = paintdevice ? paintdevice->logicalDpiY() : qt_defaultDpi();
| 0 | ||||||||||||
168 | const int screen = 0; | - | ||||||||||||
169 | if (font.d->dpi != dpi || font.d->screen != screen ) {
| 0 | ||||||||||||
170 | d = new QFontPrivate(*font.d); | - | ||||||||||||
171 | d->dpi = dpi; | - | ||||||||||||
172 | d->screen = screen; | - | ||||||||||||
173 | } else { never executed: end of block | 0 | ||||||||||||
174 | d = font.d.data(); | - | ||||||||||||
175 | } never executed: end of block | 0 | ||||||||||||
176 | - | |||||||||||||
177 | } | - | ||||||||||||
178 | - | |||||||||||||
179 | /*! | - | ||||||||||||
180 | Constructs a copy of \a fm. | - | ||||||||||||
181 | */ | - | ||||||||||||
182 | QFontMetrics::QFontMetrics(const QFontMetrics &fm) | - | ||||||||||||
183 | : d(fm.d.data()) | - | ||||||||||||
184 | { | - | ||||||||||||
185 | } never executed: end of block | 0 | ||||||||||||
186 | - | |||||||||||||
187 | /*! | - | ||||||||||||
188 | Destroys the font metrics object and frees all allocated | - | ||||||||||||
189 | resources. | - | ||||||||||||
190 | */ | - | ||||||||||||
191 | QFontMetrics::~QFontMetrics() | - | ||||||||||||
192 | { | - | ||||||||||||
193 | } | - | ||||||||||||
194 | - | |||||||||||||
195 | /*! | - | ||||||||||||
196 | Assigns the font metrics \a fm. | - | ||||||||||||
197 | */ | - | ||||||||||||
198 | QFontMetrics &QFontMetrics::operator=(const QFontMetrics &fm) | - | ||||||||||||
199 | { | - | ||||||||||||
200 | d = fm.d.data(); | - | ||||||||||||
201 | return *this; never executed: return *this; | 0 | ||||||||||||
202 | } | - | ||||||||||||
203 | - | |||||||||||||
204 | /*! | - | ||||||||||||
205 | \fn QFontMetrics &QFontMetrics::operator=(QFontMetrics &&other) | - | ||||||||||||
206 | - | |||||||||||||
207 | Move-assigns \a other to this QFontMetrics instance. | - | ||||||||||||
208 | - | |||||||||||||
209 | \since 5.2 | - | ||||||||||||
210 | */ | - | ||||||||||||
211 | /*! | - | ||||||||||||
212 | \fn QFontMetricsF &QFontMetricsF::operator=(QFontMetricsF &&other) | - | ||||||||||||
213 | - | |||||||||||||
214 | Move-assigns \a other to this QFontMetricsF instance. | - | ||||||||||||
215 | */ | - | ||||||||||||
216 | - | |||||||||||||
217 | /*! | - | ||||||||||||
218 | \fn void QFontMetrics::swap(QFontMetrics &other) | - | ||||||||||||
219 | \since 5.0 | - | ||||||||||||
220 | - | |||||||||||||
221 | Swaps this font metrics instance with \a other. This function is | - | ||||||||||||
222 | very fast and never fails. | - | ||||||||||||
223 | */ | - | ||||||||||||
224 | - | |||||||||||||
225 | /*! | - | ||||||||||||
226 | Returns \c true if \a other is equal to this object; otherwise | - | ||||||||||||
227 | returns \c false. | - | ||||||||||||
228 | - | |||||||||||||
229 | Two font metrics are considered equal if they were constructed | - | ||||||||||||
230 | from the same QFont and the paint devices they were constructed | - | ||||||||||||
231 | for are considered compatible. | - | ||||||||||||
232 | - | |||||||||||||
233 | \sa operator!=() | - | ||||||||||||
234 | */ | - | ||||||||||||
235 | bool QFontMetrics::operator ==(const QFontMetrics &other) const | - | ||||||||||||
236 | { | - | ||||||||||||
237 | return d == other.d; never executed: return d == other.d; | 0 | ||||||||||||
238 | } | - | ||||||||||||
239 | - | |||||||||||||
240 | /*! | - | ||||||||||||
241 | \fn bool QFontMetrics::operator !=(const QFontMetrics &other) const | - | ||||||||||||
242 | - | |||||||||||||
243 | Returns \c true if \a other is not equal to this object; otherwise returns \c false. | - | ||||||||||||
244 | - | |||||||||||||
245 | Two font metrics are considered equal if they were constructed | - | ||||||||||||
246 | from the same QFont and the paint devices they were constructed | - | ||||||||||||
247 | for are considered compatible. | - | ||||||||||||
248 | - | |||||||||||||
249 | \sa operator==() | - | ||||||||||||
250 | */ | - | ||||||||||||
251 | - | |||||||||||||
252 | /*! | - | ||||||||||||
253 | Returns the ascent of the font. | - | ||||||||||||
254 | - | |||||||||||||
255 | The ascent of a font is the distance from the baseline to the | - | ||||||||||||
256 | highest position characters extend to. In practice, some font | - | ||||||||||||
257 | designers break this rule, e.g. when they put more than one accent | - | ||||||||||||
258 | on top of a character, or to accommodate an unusual character in | - | ||||||||||||
259 | an exotic language, so it is possible (though rare) that this | - | ||||||||||||
260 | value will be too small. | - | ||||||||||||
261 | - | |||||||||||||
262 | \sa descent() | - | ||||||||||||
263 | */ | - | ||||||||||||
264 | int QFontMetrics::ascent() const | - | ||||||||||||
265 | { | - | ||||||||||||
266 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
267 | Q_ASSERT(engine != 0); | - | ||||||||||||
268 | return qRound(engine->ascent()); never executed: return qRound(engine->ascent()); | 0 | ||||||||||||
269 | } | - | ||||||||||||
270 | - | |||||||||||||
271 | - | |||||||||||||
272 | /*! | - | ||||||||||||
273 | Returns the descent of the font. | - | ||||||||||||
274 | - | |||||||||||||
275 | The descent is the distance from the base line to the lowest point | - | ||||||||||||
276 | characters extend to. In practice, some font designers break this rule, | - | ||||||||||||
277 | e.g. to accommodate an unusual character in an exotic language, so | - | ||||||||||||
278 | it is possible (though rare) that this value will be too small. | - | ||||||||||||
279 | - | |||||||||||||
280 | \sa ascent() | - | ||||||||||||
281 | */ | - | ||||||||||||
282 | int QFontMetrics::descent() const | - | ||||||||||||
283 | { | - | ||||||||||||
284 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
285 | Q_ASSERT(engine != 0); | - | ||||||||||||
286 | return qRound(engine->descent()); never executed: return qRound(engine->descent()); | 0 | ||||||||||||
287 | } | - | ||||||||||||
288 | - | |||||||||||||
289 | /*! | - | ||||||||||||
290 | Returns the height of the font. | - | ||||||||||||
291 | - | |||||||||||||
292 | This is always equal to ascent()+descent(). | - | ||||||||||||
293 | - | |||||||||||||
294 | \sa leading(), lineSpacing() | - | ||||||||||||
295 | */ | - | ||||||||||||
296 | int QFontMetrics::height() const | - | ||||||||||||
297 | { | - | ||||||||||||
298 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
299 | Q_ASSERT(engine != 0); | - | ||||||||||||
300 | return qRound(engine->ascent()) + qRound(engine->descent()); never executed: return qRound(engine->ascent()) + qRound(engine->descent()); | 0 | ||||||||||||
301 | } | - | ||||||||||||
302 | - | |||||||||||||
303 | /*! | - | ||||||||||||
304 | Returns the leading of the font. | - | ||||||||||||
305 | - | |||||||||||||
306 | This is the natural inter-line spacing. | - | ||||||||||||
307 | - | |||||||||||||
308 | \sa height(), lineSpacing() | - | ||||||||||||
309 | */ | - | ||||||||||||
310 | int QFontMetrics::leading() const | - | ||||||||||||
311 | { | - | ||||||||||||
312 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
313 | Q_ASSERT(engine != 0); | - | ||||||||||||
314 | return qRound(engine->leading()); never executed: return qRound(engine->leading()); | 0 | ||||||||||||
315 | } | - | ||||||||||||
316 | - | |||||||||||||
317 | /*! | - | ||||||||||||
318 | Returns the distance from one base line to the next. | - | ||||||||||||
319 | - | |||||||||||||
320 | This value is always equal to leading()+height(). | - | ||||||||||||
321 | - | |||||||||||||
322 | \sa height(), leading() | - | ||||||||||||
323 | */ | - | ||||||||||||
324 | int QFontMetrics::lineSpacing() const | - | ||||||||||||
325 | { | - | ||||||||||||
326 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
327 | Q_ASSERT(engine != 0); | - | ||||||||||||
328 | return qRound(engine->leading()) + qRound(engine->ascent()) + qRound(engine->descent()); never executed: return qRound(engine->leading()) + qRound(engine->ascent()) + qRound(engine->descent()); | 0 | ||||||||||||
329 | } | - | ||||||||||||
330 | - | |||||||||||||
331 | /*! | - | ||||||||||||
332 | Returns the minimum left bearing of the font. | - | ||||||||||||
333 | - | |||||||||||||
334 | This is the smallest leftBearing(char) of all characters in the | - | ||||||||||||
335 | font. | - | ||||||||||||
336 | - | |||||||||||||
337 | Note that this function can be very slow if the font is large. | - | ||||||||||||
338 | - | |||||||||||||
339 | \sa minRightBearing(), leftBearing() | - | ||||||||||||
340 | */ | - | ||||||||||||
341 | int QFontMetrics::minLeftBearing() const | - | ||||||||||||
342 | { | - | ||||||||||||
343 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
344 | Q_ASSERT(engine != 0); | - | ||||||||||||
345 | return qRound(engine->minLeftBearing()); never executed: return qRound(engine->minLeftBearing()); | 0 | ||||||||||||
346 | } | - | ||||||||||||
347 | - | |||||||||||||
348 | /*! | - | ||||||||||||
349 | Returns the minimum right bearing of the font. | - | ||||||||||||
350 | - | |||||||||||||
351 | This is the smallest rightBearing(char) of all characters in the | - | ||||||||||||
352 | font. | - | ||||||||||||
353 | - | |||||||||||||
354 | Note that this function can be very slow if the font is large. | - | ||||||||||||
355 | - | |||||||||||||
356 | \sa minLeftBearing(), rightBearing() | - | ||||||||||||
357 | */ | - | ||||||||||||
358 | int QFontMetrics::minRightBearing() const | - | ||||||||||||
359 | { | - | ||||||||||||
360 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
361 | Q_ASSERT(engine != 0); | - | ||||||||||||
362 | return qRound(engine->minRightBearing()); never executed: return qRound(engine->minRightBearing()); | 0 | ||||||||||||
363 | } | - | ||||||||||||
364 | - | |||||||||||||
365 | /*! | - | ||||||||||||
366 | Returns the width of the widest character in the font. | - | ||||||||||||
367 | */ | - | ||||||||||||
368 | int QFontMetrics::maxWidth() const | - | ||||||||||||
369 | { | - | ||||||||||||
370 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
371 | Q_ASSERT(engine != 0); | - | ||||||||||||
372 | return qRound(engine->maxCharWidth()); never executed: return qRound(engine->maxCharWidth()); | 0 | ||||||||||||
373 | } | - | ||||||||||||
374 | - | |||||||||||||
375 | /*! | - | ||||||||||||
376 | Returns the 'x' height of the font. This is often but not always | - | ||||||||||||
377 | the same as the height of the character 'x'. | - | ||||||||||||
378 | */ | - | ||||||||||||
379 | int QFontMetrics::xHeight() const | - | ||||||||||||
380 | { | - | ||||||||||||
381 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
382 | Q_ASSERT(engine != 0); | - | ||||||||||||
383 | if (d->capital == QFont::SmallCaps)
| 0 | ||||||||||||
384 | return qRound(d->smallCapsFontPrivate()->engineForScript(QChar::Script_Common)->ascent()); never executed: return qRound(d->smallCapsFontPrivate()->engineForScript(QChar::Script_Common)->ascent()); | 0 | ||||||||||||
385 | return qRound(engine->xHeight()); never executed: return qRound(engine->xHeight()); | 0 | ||||||||||||
386 | } | - | ||||||||||||
387 | - | |||||||||||||
388 | /*! | - | ||||||||||||
389 | \since 4.2 | - | ||||||||||||
390 | - | |||||||||||||
391 | Returns the average width of glyphs in the font. | - | ||||||||||||
392 | */ | - | ||||||||||||
393 | int QFontMetrics::averageCharWidth() const | - | ||||||||||||
394 | { | - | ||||||||||||
395 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
396 | Q_ASSERT(engine != 0); | - | ||||||||||||
397 | return qRound(engine->averageCharWidth()); never executed: return qRound(engine->averageCharWidth()); | 0 | ||||||||||||
398 | } | - | ||||||||||||
399 | - | |||||||||||||
400 | /*! | - | ||||||||||||
401 | Returns \c true if character \a ch is a valid character in the font; | - | ||||||||||||
402 | otherwise returns \c false. | - | ||||||||||||
403 | */ | - | ||||||||||||
404 | bool QFontMetrics::inFont(QChar ch) const | - | ||||||||||||
405 | { | - | ||||||||||||
406 | return inFontUcs4(ch.unicode()); never executed: return inFontUcs4(ch.unicode()); | 0 | ||||||||||||
407 | } | - | ||||||||||||
408 | - | |||||||||||||
409 | /*! | - | ||||||||||||
410 | Returns \c true if the character \a ucs4 encoded in UCS-4/UTF-32 is a valid | - | ||||||||||||
411 | character in the font; otherwise returns \c false. | - | ||||||||||||
412 | */ | - | ||||||||||||
413 | bool QFontMetrics::inFontUcs4(uint ucs4) const | - | ||||||||||||
414 | { | - | ||||||||||||
415 | const int script = QChar::script(ucs4); | - | ||||||||||||
416 | QFontEngine *engine = d->engineForScript(script); | - | ||||||||||||
417 | Q_ASSERT(engine != 0); | - | ||||||||||||
418 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
419 | return false; never executed: return false; | 0 | ||||||||||||
420 | return engine->canRender(ucs4); never executed: return engine->canRender(ucs4); | 0 | ||||||||||||
421 | } | - | ||||||||||||
422 | - | |||||||||||||
423 | /*! | - | ||||||||||||
424 | Returns the left bearing of character \a ch in the font. | - | ||||||||||||
425 | - | |||||||||||||
426 | The left bearing is the right-ward distance of the left-most pixel | - | ||||||||||||
427 | of the character from the logical origin of the character. This | - | ||||||||||||
428 | value is negative if the pixels of the character extend to the | - | ||||||||||||
429 | left of the logical origin. | - | ||||||||||||
430 | - | |||||||||||||
431 | See width(QChar) for a graphical description of this metric. | - | ||||||||||||
432 | - | |||||||||||||
433 | \sa rightBearing(), minLeftBearing(), width() | - | ||||||||||||
434 | */ | - | ||||||||||||
435 | int QFontMetrics::leftBearing(QChar ch) const | - | ||||||||||||
436 | { | - | ||||||||||||
437 | const int script = ch.script(); | - | ||||||||||||
438 | QFontEngine *engine; | - | ||||||||||||
439 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
440 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
441 | else | - | ||||||||||||
442 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
443 | Q_ASSERT(engine != 0); | - | ||||||||||||
444 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
445 | return 0; never executed: return 0; | 0 | ||||||||||||
446 | - | |||||||||||||
447 | d->alterCharForCapitalization(ch); | - | ||||||||||||
448 | - | |||||||||||||
449 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
450 | - | |||||||||||||
451 | qreal lb; | - | ||||||||||||
452 | engine->getGlyphBearings(glyph, &lb); | - | ||||||||||||
453 | return qRound(lb); never executed: return qRound(lb); | 0 | ||||||||||||
454 | } | - | ||||||||||||
455 | - | |||||||||||||
456 | /*! | - | ||||||||||||
457 | Returns the right bearing of character \a ch in the font. | - | ||||||||||||
458 | - | |||||||||||||
459 | The right bearing is the left-ward distance of the right-most | - | ||||||||||||
460 | pixel of the character from the logical origin of a subsequent | - | ||||||||||||
461 | character. This value is negative if the pixels of the character | - | ||||||||||||
462 | extend to the right of the width() of the character. | - | ||||||||||||
463 | - | |||||||||||||
464 | See width() for a graphical description of this metric. | - | ||||||||||||
465 | - | |||||||||||||
466 | \sa leftBearing(), minRightBearing(), width() | - | ||||||||||||
467 | */ | - | ||||||||||||
468 | int QFontMetrics::rightBearing(QChar ch) const | - | ||||||||||||
469 | { | - | ||||||||||||
470 | const int script = ch.script(); | - | ||||||||||||
471 | QFontEngine *engine; | - | ||||||||||||
472 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
473 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
474 | else | - | ||||||||||||
475 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
476 | Q_ASSERT(engine != 0); | - | ||||||||||||
477 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
478 | return 0; never executed: return 0; | 0 | ||||||||||||
479 | - | |||||||||||||
480 | d->alterCharForCapitalization(ch); | - | ||||||||||||
481 | - | |||||||||||||
482 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
483 | - | |||||||||||||
484 | qreal rb; | - | ||||||||||||
485 | engine->getGlyphBearings(glyph, 0, &rb); | - | ||||||||||||
486 | return qRound(rb); never executed: return qRound(rb); | 0 | ||||||||||||
487 | } | - | ||||||||||||
488 | - | |||||||||||||
489 | /*! | - | ||||||||||||
490 | Returns the width in pixels of the first \a len characters of \a | - | ||||||||||||
491 | text. If \a len is negative (the default), the entire string is | - | ||||||||||||
492 | used. | - | ||||||||||||
493 | - | |||||||||||||
494 | Note that this value is \e not equal to boundingRect().width(); | - | ||||||||||||
495 | boundingRect() returns a rectangle describing the pixels this | - | ||||||||||||
496 | string will cover whereas width() returns the distance to where | - | ||||||||||||
497 | the next string should be drawn. | - | ||||||||||||
498 | - | |||||||||||||
499 | \sa boundingRect() | - | ||||||||||||
500 | */ | - | ||||||||||||
501 | int QFontMetrics::width(const QString &text, int len) const | - | ||||||||||||
502 | { | - | ||||||||||||
503 | return width(text, len, 0); never executed: return width(text, len, 0); | 0 | ||||||||||||
504 | } | - | ||||||||||||
505 | - | |||||||||||||
506 | /*! | - | ||||||||||||
507 | \internal | - | ||||||||||||
508 | */ | - | ||||||||||||
509 | int QFontMetrics::width(const QString &text, int len, int flags) const | - | ||||||||||||
510 | { | - | ||||||||||||
511 | int pos = text.indexOf(QLatin1Char('\x9c')); | - | ||||||||||||
512 | if (pos != -1) {
| 0 | ||||||||||||
513 | len = (len < 0) ? pos : qMin(pos, len);
| 0 | ||||||||||||
514 | } else if (len < 0) { never executed: end of block
| 0 | ||||||||||||
515 | len = text.length(); | - | ||||||||||||
516 | } never executed: end of block | 0 | ||||||||||||
517 | if (len == 0)
| 0 | ||||||||||||
518 | return 0; never executed: return 0; | 0 | ||||||||||||
519 | - | |||||||||||||
520 | if (flags & Qt::TextBypassShaping) {
| 0 | ||||||||||||
521 | // Skip complex shaping, only use advances | - | ||||||||||||
522 | int numGlyphs = len; | - | ||||||||||||
523 | QVarLengthGlyphLayoutArray glyphs(numGlyphs); | - | ||||||||||||
524 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
525 | if (!engine->stringToCMap(text.data(), len, &glyphs, &numGlyphs, 0))
| 0 | ||||||||||||
526 | Q_UNREACHABLE(); never executed: end of block | 0 | ||||||||||||
527 | - | |||||||||||||
528 | QFixed width; | - | ||||||||||||
529 | for (int i = 0; i < numGlyphs; ++i)
| 0 | ||||||||||||
530 | width += glyphs.advances[i]; never executed: width += glyphs.advances[i]; | 0 | ||||||||||||
531 | return qRound(width); never executed: return qRound(width); | 0 | ||||||||||||
532 | } | - | ||||||||||||
533 | - | |||||||||||||
534 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
535 | return qRound(layout.width(0, len)); never executed: return qRound(layout.width(0, len)); | 0 | ||||||||||||
536 | } | - | ||||||||||||
537 | - | |||||||||||||
538 | /*! | - | ||||||||||||
539 | \overload | - | ||||||||||||
540 | - | |||||||||||||
541 | \image bearings.png Bearings | - | ||||||||||||
542 | - | |||||||||||||
543 | Returns the logical width of character \a ch in pixels. This is a | - | ||||||||||||
544 | distance appropriate for drawing a subsequent character after \a | - | ||||||||||||
545 | ch. | - | ||||||||||||
546 | - | |||||||||||||
547 | Some of the metrics are described in the image to the right. The | - | ||||||||||||
548 | central dark rectangles cover the logical width() of each | - | ||||||||||||
549 | character. The outer pale rectangles cover the leftBearing() and | - | ||||||||||||
550 | rightBearing() of each character. Notice that the bearings of "f" | - | ||||||||||||
551 | in this particular font are both negative, while the bearings of | - | ||||||||||||
552 | "o" are both positive. | - | ||||||||||||
553 | - | |||||||||||||
554 | \warning This function will produce incorrect results for Arabic | - | ||||||||||||
555 | characters or non-spacing marks in the middle of a string, as the | - | ||||||||||||
556 | glyph shaping and positioning of marks that happens when | - | ||||||||||||
557 | processing strings cannot be taken into account. When implementing | - | ||||||||||||
558 | an interactive text control, use QTextLayout instead. | - | ||||||||||||
559 | - | |||||||||||||
560 | \sa boundingRect() | - | ||||||||||||
561 | */ | - | ||||||||||||
562 | int QFontMetrics::width(QChar ch) const | - | ||||||||||||
563 | { | - | ||||||||||||
564 | if (QChar::category(ch.unicode()) == QChar::Mark_NonSpacing)
| 0 | ||||||||||||
565 | return 0; never executed: return 0; | 0 | ||||||||||||
566 | - | |||||||||||||
567 | const int script = ch.script(); | - | ||||||||||||
568 | QFontEngine *engine; | - | ||||||||||||
569 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
570 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
571 | else | - | ||||||||||||
572 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
573 | Q_ASSERT(engine != 0); | - | ||||||||||||
574 | - | |||||||||||||
575 | d->alterCharForCapitalization(ch); | - | ||||||||||||
576 | - | |||||||||||||
577 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
578 | QFixed advance; | - | ||||||||||||
579 | - | |||||||||||||
580 | QGlyphLayout glyphs; | - | ||||||||||||
581 | glyphs.numGlyphs = 1; | - | ||||||||||||
582 | glyphs.glyphs = &glyph; | - | ||||||||||||
583 | glyphs.advances = &advance; | - | ||||||||||||
584 | engine->recalcAdvances(&glyphs, 0); | - | ||||||||||||
585 | - | |||||||||||||
586 | return qRound(advance); never executed: return qRound(advance); | 0 | ||||||||||||
587 | } | - | ||||||||||||
588 | - | |||||||||||||
589 | #if QT_VERSION < QT_VERSION_CHECK(6,0,0) | - | ||||||||||||
590 | /*! \obsolete | - | ||||||||||||
591 | - | |||||||||||||
592 | Returns the width of the character at position \a pos in the | - | ||||||||||||
593 | string \a text. | - | ||||||||||||
594 | - | |||||||||||||
595 | The whole string is needed, as the glyph drawn may change | - | ||||||||||||
596 | depending on the context (the letter before and after the current | - | ||||||||||||
597 | one) for some languages (e.g. Arabic). | - | ||||||||||||
598 | - | |||||||||||||
599 | This function also takes non spacing marks and ligatures into | - | ||||||||||||
600 | account. | - | ||||||||||||
601 | */ | - | ||||||||||||
602 | int QFontMetrics::charWidth(const QString &text, int pos) const | - | ||||||||||||
603 | { | - | ||||||||||||
604 | int width = 0; | - | ||||||||||||
605 | if (pos < 0 || pos > (int)text.length())
| 0 | ||||||||||||
606 | return width; never executed: return width; | 0 | ||||||||||||
607 | - | |||||||||||||
608 | QChar ch = text.at(pos); | - | ||||||||||||
609 | const int script = ch.script(); | - | ||||||||||||
610 | if (script != QChar::Script_Common) {
| 0 | ||||||||||||
611 | // complex script shaping. Have to do some hard work | - | ||||||||||||
612 | int from = qMax(0, pos - 8); | - | ||||||||||||
613 | int to = qMin(text.length(), pos + 8); | - | ||||||||||||
614 | QString cstr = QString::fromRawData(text.unicode() + from, to - from); | - | ||||||||||||
615 | QStackTextEngine layout(cstr, QFont(d.data())); | - | ||||||||||||
616 | layout.ignoreBidi = true; | - | ||||||||||||
617 | layout.itemize(); | - | ||||||||||||
618 | width = qRound(layout.width(pos-from, 1)); | - | ||||||||||||
619 | } else if (ch.category() != QChar::Mark_NonSpacing) { never executed: end of block
| 0 | ||||||||||||
620 | QFontEngine *engine; | - | ||||||||||||
621 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
622 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
623 | else | - | ||||||||||||
624 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
625 | Q_ASSERT(engine != 0); | - | ||||||||||||
626 | - | |||||||||||||
627 | d->alterCharForCapitalization(ch); | - | ||||||||||||
628 | - | |||||||||||||
629 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
630 | QFixed advance; | - | ||||||||||||
631 | - | |||||||||||||
632 | QGlyphLayout glyphs; | - | ||||||||||||
633 | glyphs.numGlyphs = 1; | - | ||||||||||||
634 | glyphs.glyphs = &glyph; | - | ||||||||||||
635 | glyphs.advances = &advance; | - | ||||||||||||
636 | engine->recalcAdvances(&glyphs, 0); | - | ||||||||||||
637 | - | |||||||||||||
638 | width = qRound(advance); | - | ||||||||||||
639 | } never executed: end of block | 0 | ||||||||||||
640 | return width; never executed: return width; | 0 | ||||||||||||
641 | } | - | ||||||||||||
642 | #endif | - | ||||||||||||
643 | - | |||||||||||||
644 | /*! | - | ||||||||||||
645 | Returns the bounding rectangle of the characters in the string | - | ||||||||||||
646 | specified by \a text. The bounding rectangle always covers at least | - | ||||||||||||
647 | the set of pixels the text would cover if drawn at (0, 0). | - | ||||||||||||
648 | - | |||||||||||||
649 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
650 | e.g. for italicized fonts, and that the width of the returned | - | ||||||||||||
651 | rectangle might be different than what the width() method returns. | - | ||||||||||||
652 | - | |||||||||||||
653 | If you want to know the advance width of the string (to lay out | - | ||||||||||||
654 | a set of strings next to each other), use width() instead. | - | ||||||||||||
655 | - | |||||||||||||
656 | Newline characters are processed as normal characters, \e not as | - | ||||||||||||
657 | linebreaks. | - | ||||||||||||
658 | - | |||||||||||||
659 | The height of the bounding rectangle is at least as large as the | - | ||||||||||||
660 | value returned by height(). | - | ||||||||||||
661 | - | |||||||||||||
662 | \sa width(), height(), QPainter::boundingRect(), tightBoundingRect() | - | ||||||||||||
663 | */ | - | ||||||||||||
664 | QRect QFontMetrics::boundingRect(const QString &text) const | - | ||||||||||||
665 | { | - | ||||||||||||
666 | if (text.length() == 0)
| 0 | ||||||||||||
667 | return QRect(); never executed: return QRect(); | 0 | ||||||||||||
668 | - | |||||||||||||
669 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
670 | layout.itemize(); | - | ||||||||||||
671 | glyph_metrics_t gm = layout.boundingBox(0, text.length()); | - | ||||||||||||
672 | return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); never executed: return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); | 0 | ||||||||||||
673 | } | - | ||||||||||||
674 | - | |||||||||||||
675 | /*! | - | ||||||||||||
676 | Returns the rectangle that is covered by ink if character \a ch | - | ||||||||||||
677 | were to be drawn at the origin of the coordinate system. | - | ||||||||||||
678 | - | |||||||||||||
679 | Note that the bounding rectangle may extend to the left of (0, 0) | - | ||||||||||||
680 | (e.g., for italicized fonts), and that the text output may cover \e | - | ||||||||||||
681 | all pixels in the bounding rectangle. For a space character the rectangle | - | ||||||||||||
682 | will usually be empty. | - | ||||||||||||
683 | - | |||||||||||||
684 | Note that the rectangle usually extends both above and below the | - | ||||||||||||
685 | base line. | - | ||||||||||||
686 | - | |||||||||||||
687 | \warning The width of the returned rectangle is not the advance width | - | ||||||||||||
688 | of the character. Use boundingRect(const QString &) or width() instead. | - | ||||||||||||
689 | - | |||||||||||||
690 | \sa width() | - | ||||||||||||
691 | */ | - | ||||||||||||
692 | QRect QFontMetrics::boundingRect(QChar ch) const | - | ||||||||||||
693 | { | - | ||||||||||||
694 | const int script = ch.script(); | - | ||||||||||||
695 | QFontEngine *engine; | - | ||||||||||||
696 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
697 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
698 | else | - | ||||||||||||
699 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
700 | Q_ASSERT(engine != 0); | - | ||||||||||||
701 | - | |||||||||||||
702 | d->alterCharForCapitalization(ch); | - | ||||||||||||
703 | - | |||||||||||||
704 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
705 | - | |||||||||||||
706 | glyph_metrics_t gm = engine->boundingBox(glyph); | - | ||||||||||||
707 | return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); never executed: return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); | 0 | ||||||||||||
708 | } | - | ||||||||||||
709 | - | |||||||||||||
710 | /*! | - | ||||||||||||
711 | \overload | - | ||||||||||||
712 | - | |||||||||||||
713 | Returns the bounding rectangle of the characters in the string | - | ||||||||||||
714 | specified by \a text, which is the set of pixels the text would | - | ||||||||||||
715 | cover if drawn at (0, 0). The drawing, and hence the bounding | - | ||||||||||||
716 | rectangle, is constrained to the rectangle \a rect. | - | ||||||||||||
717 | - | |||||||||||||
718 | The \a flags argument is the bitwise OR of the following flags: | - | ||||||||||||
719 | \list | - | ||||||||||||
720 | \li Qt::AlignLeft aligns to the left border, except for | - | ||||||||||||
721 | Arabic and Hebrew where it aligns to the right. | - | ||||||||||||
722 | \li Qt::AlignRight aligns to the right border, except for | - | ||||||||||||
723 | Arabic and Hebrew where it aligns to the left. | - | ||||||||||||
724 | \li Qt::AlignJustify produces justified text. | - | ||||||||||||
725 | \li Qt::AlignHCenter aligns horizontally centered. | - | ||||||||||||
726 | \li Qt::AlignTop aligns to the top border. | - | ||||||||||||
727 | \li Qt::AlignBottom aligns to the bottom border. | - | ||||||||||||
728 | \li Qt::AlignVCenter aligns vertically centered | - | ||||||||||||
729 | \li Qt::AlignCenter (== \c{Qt::AlignHCenter | Qt::AlignVCenter}) | - | ||||||||||||
730 | \li Qt::TextSingleLine ignores newline characters in the text. | - | ||||||||||||
731 | \li Qt::TextExpandTabs expands tabs (see below) | - | ||||||||||||
732 | \li Qt::TextShowMnemonic interprets "&x" as \underline{x}; i.e., underlined. | - | ||||||||||||
733 | \li Qt::TextWordWrap breaks the text to fit the rectangle. | - | ||||||||||||
734 | \endlist | - | ||||||||||||
735 | - | |||||||||||||
736 | Qt::Horizontal alignment defaults to Qt::AlignLeft and vertical | - | ||||||||||||
737 | alignment defaults to Qt::AlignTop. | - | ||||||||||||
738 | - | |||||||||||||
739 | If several of the horizontal or several of the vertical alignment | - | ||||||||||||
740 | flags are set, the resulting alignment is undefined. | - | ||||||||||||
741 | - | |||||||||||||
742 | If Qt::TextExpandTabs is set in \a flags, then: if \a tabArray is | - | ||||||||||||
743 | non-null, it specifies a 0-terminated sequence of pixel-positions | - | ||||||||||||
744 | for tabs; otherwise if \a tabStops is non-zero, it is used as the | - | ||||||||||||
745 | tab spacing (in pixels). | - | ||||||||||||
746 | - | |||||||||||||
747 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
748 | e.g. for italicized fonts, and that the text output may cover \e | - | ||||||||||||
749 | all pixels in the bounding rectangle. | - | ||||||||||||
750 | - | |||||||||||||
751 | Newline characters are processed as linebreaks. | - | ||||||||||||
752 | - | |||||||||||||
753 | Despite the different actual character heights, the heights of the | - | ||||||||||||
754 | bounding rectangles of "Yes" and "yes" are the same. | - | ||||||||||||
755 | - | |||||||||||||
756 | The bounding rectangle returned by this function is somewhat larger | - | ||||||||||||
757 | than that calculated by the simpler boundingRect() function. This | - | ||||||||||||
758 | function uses the \l{minLeftBearing()}{maximum left} and | - | ||||||||||||
759 | \l{minRightBearing()}{right} font bearings as is | - | ||||||||||||
760 | necessary for multi-line text to align correctly. Also, | - | ||||||||||||
761 | fontHeight() and lineSpacing() are used to calculate the height, | - | ||||||||||||
762 | rather than individual character heights. | - | ||||||||||||
763 | - | |||||||||||||
764 | \sa width(), QPainter::boundingRect(), Qt::Alignment | - | ||||||||||||
765 | */ | - | ||||||||||||
766 | QRect QFontMetrics::boundingRect(const QRect &rect, int flags, const QString &text, int tabStops, | - | ||||||||||||
767 | int *tabArray) const | - | ||||||||||||
768 | { | - | ||||||||||||
769 | int tabArrayLen = 0; | - | ||||||||||||
770 | if (tabArray)
| 0 | ||||||||||||
771 | while (tabArray[tabArrayLen])
| 0 | ||||||||||||
772 | tabArrayLen++; never executed: tabArrayLen++; | 0 | ||||||||||||
773 | - | |||||||||||||
774 | QRectF rb; | - | ||||||||||||
775 | QRectF rr(rect); | - | ||||||||||||
776 | qt_format_text(QFont(d.data()), rr, flags | Qt::TextDontPrint, text, &rb, tabStops, tabArray, | - | ||||||||||||
777 | tabArrayLen, 0); | - | ||||||||||||
778 | - | |||||||||||||
779 | return rb.toAlignedRect(); never executed: return rb.toAlignedRect(); | 0 | ||||||||||||
780 | } | - | ||||||||||||
781 | - | |||||||||||||
782 | /*! | - | ||||||||||||
783 | Returns the size in pixels of \a text. | - | ||||||||||||
784 | - | |||||||||||||
785 | The \a flags argument is the bitwise OR of the following flags: | - | ||||||||||||
786 | \list | - | ||||||||||||
787 | \li Qt::TextSingleLine ignores newline characters. | - | ||||||||||||
788 | \li Qt::TextExpandTabs expands tabs (see below) | - | ||||||||||||
789 | \li Qt::TextShowMnemonic interprets "&x" as \underline{x}; i.e., underlined. | - | ||||||||||||
790 | \li Qt::TextWordWrap breaks the text to fit the rectangle. | - | ||||||||||||
791 | \endlist | - | ||||||||||||
792 | - | |||||||||||||
793 | If Qt::TextExpandTabs is set in \a flags, then: if \a tabArray is | - | ||||||||||||
794 | non-null, it specifies a 0-terminated sequence of pixel-positions | - | ||||||||||||
795 | for tabs; otherwise if \a tabStops is non-zero, it is used as the | - | ||||||||||||
796 | tab spacing (in pixels). | - | ||||||||||||
797 | - | |||||||||||||
798 | Newline characters are processed as linebreaks. | - | ||||||||||||
799 | - | |||||||||||||
800 | Despite the different actual character heights, the heights of the | - | ||||||||||||
801 | bounding rectangles of "Yes" and "yes" are the same. | - | ||||||||||||
802 | - | |||||||||||||
803 | \sa boundingRect() | - | ||||||||||||
804 | */ | - | ||||||||||||
805 | QSize QFontMetrics::size(int flags, const QString &text, int tabStops, int *tabArray) const | - | ||||||||||||
806 | { | - | ||||||||||||
807 | return boundingRect(QRect(0,0,0,0), flags | Qt::TextLongestVariant, text, tabStops, tabArray).size(); never executed: return boundingRect(QRect(0,0,0,0), flags | Qt::TextLongestVariant, text, tabStops, tabArray).size(); | 0 | ||||||||||||
808 | } | - | ||||||||||||
809 | - | |||||||||||||
810 | /*! | - | ||||||||||||
811 | \since 4.3 | - | ||||||||||||
812 | - | |||||||||||||
813 | Returns a tight bounding rectangle around the characters in the | - | ||||||||||||
814 | string specified by \a text. The bounding rectangle always covers | - | ||||||||||||
815 | at least the set of pixels the text would cover if drawn at (0, | - | ||||||||||||
816 | 0). | - | ||||||||||||
817 | - | |||||||||||||
818 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
819 | e.g. for italicized fonts, and that the width of the returned | - | ||||||||||||
820 | rectangle might be different than what the width() method returns. | - | ||||||||||||
821 | - | |||||||||||||
822 | If you want to know the advance width of the string (to lay out | - | ||||||||||||
823 | a set of strings next to each other), use width() instead. | - | ||||||||||||
824 | - | |||||||||||||
825 | Newline characters are processed as normal characters, \e not as | - | ||||||||||||
826 | linebreaks. | - | ||||||||||||
827 | - | |||||||||||||
828 | \warning Calling this method is very slow on Windows. | - | ||||||||||||
829 | - | |||||||||||||
830 | \sa width(), height(), boundingRect() | - | ||||||||||||
831 | */ | - | ||||||||||||
832 | QRect QFontMetrics::tightBoundingRect(const QString &text) const | - | ||||||||||||
833 | { | - | ||||||||||||
834 | if (text.length() == 0)
| 0 | ||||||||||||
835 | return QRect(); never executed: return QRect(); | 0 | ||||||||||||
836 | - | |||||||||||||
837 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
838 | layout.itemize(); | - | ||||||||||||
839 | glyph_metrics_t gm = layout.tightBoundingBox(0, text.length()); | - | ||||||||||||
840 | return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); never executed: return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); | 0 | ||||||||||||
841 | } | - | ||||||||||||
842 | - | |||||||||||||
843 | - | |||||||||||||
844 | /*! | - | ||||||||||||
845 | \since 4.2 | - | ||||||||||||
846 | - | |||||||||||||
847 | If the string \a text is wider than \a width, returns an elided | - | ||||||||||||
848 | version of the string (i.e., a string with "..." in it). | - | ||||||||||||
849 | Otherwise, returns the original string. | - | ||||||||||||
850 | - | |||||||||||||
851 | The \a mode parameter specifies whether the text is elided on the | - | ||||||||||||
852 | left (e.g., "...tech"), in the middle (e.g., "Tr...ch"), or on | - | ||||||||||||
853 | the right (e.g., "Trol..."). | - | ||||||||||||
854 | - | |||||||||||||
855 | The \a width is specified in pixels, not characters. | - | ||||||||||||
856 | - | |||||||||||||
857 | The \a flags argument is optional and currently only supports | - | ||||||||||||
858 | Qt::TextShowMnemonic as value. | - | ||||||||||||
859 | - | |||||||||||||
860 | The elide mark follows the \l{Qt::LayoutDirection}{layoutdirection}. | - | ||||||||||||
861 | For example, it will be on the right side of the text for right-to-left | - | ||||||||||||
862 | layouts if the \a mode is \c{Qt::ElideLeft}, and on the left side of the | - | ||||||||||||
863 | text if the \a mode is \c{Qt::ElideRight}. | - | ||||||||||||
864 | - | |||||||||||||
865 | */ | - | ||||||||||||
866 | QString QFontMetrics::elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags) const | - | ||||||||||||
867 | { | - | ||||||||||||
868 | QString _text = text; | - | ||||||||||||
869 | if (!(flags & Qt::TextLongestVariant)) {
| 0 | ||||||||||||
870 | int posA = 0; | - | ||||||||||||
871 | int posB = _text.indexOf(QLatin1Char('\x9c')); | - | ||||||||||||
872 | while (posB >= 0) {
| 0 | ||||||||||||
873 | QString portion = _text.mid(posA, posB - posA); | - | ||||||||||||
874 | if (size(flags, portion).width() <= width)
| 0 | ||||||||||||
875 | return portion; never executed: return portion; | 0 | ||||||||||||
876 | posA = posB + 1; | - | ||||||||||||
877 | posB = _text.indexOf(QLatin1Char('\x9c'), posA); | - | ||||||||||||
878 | } never executed: end of block | 0 | ||||||||||||
879 | _text = _text.mid(posA); | - | ||||||||||||
880 | } never executed: end of block | 0 | ||||||||||||
881 | QStackTextEngine engine(_text, QFont(d.data())); | - | ||||||||||||
882 | return engine.elidedText(mode, width, flags); never executed: return engine.elidedText(mode, width, flags); | 0 | ||||||||||||
883 | } | - | ||||||||||||
884 | - | |||||||||||||
885 | /*! | - | ||||||||||||
886 | Returns the distance from the base line to where an underscore | - | ||||||||||||
887 | should be drawn. | - | ||||||||||||
888 | - | |||||||||||||
889 | \sa overlinePos(), strikeOutPos(), lineWidth() | - | ||||||||||||
890 | */ | - | ||||||||||||
891 | int QFontMetrics::underlinePos() const | - | ||||||||||||
892 | { | - | ||||||||||||
893 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
894 | Q_ASSERT(engine != 0); | - | ||||||||||||
895 | return qRound(engine->underlinePosition()); never executed: return qRound(engine->underlinePosition()); | 0 | ||||||||||||
896 | } | - | ||||||||||||
897 | - | |||||||||||||
898 | /*! | - | ||||||||||||
899 | Returns the distance from the base line to where an overline | - | ||||||||||||
900 | should be drawn. | - | ||||||||||||
901 | - | |||||||||||||
902 | \sa underlinePos(), strikeOutPos(), lineWidth() | - | ||||||||||||
903 | */ | - | ||||||||||||
904 | int QFontMetrics::overlinePos() const | - | ||||||||||||
905 | { | - | ||||||||||||
906 | return ascent() + 1; never executed: return ascent() + 1; | 0 | ||||||||||||
907 | } | - | ||||||||||||
908 | - | |||||||||||||
909 | /*! | - | ||||||||||||
910 | Returns the distance from the base line to where the strikeout | - | ||||||||||||
911 | line should be drawn. | - | ||||||||||||
912 | - | |||||||||||||
913 | \sa underlinePos(), overlinePos(), lineWidth() | - | ||||||||||||
914 | */ | - | ||||||||||||
915 | int QFontMetrics::strikeOutPos() const | - | ||||||||||||
916 | { | - | ||||||||||||
917 | int pos = ascent() / 3; | - | ||||||||||||
918 | return pos > 0 ? pos : 1; never executed: return pos > 0 ? pos : 1;
| 0 | ||||||||||||
919 | } | - | ||||||||||||
920 | - | |||||||||||||
921 | /*! | - | ||||||||||||
922 | Returns the width of the underline and strikeout lines, adjusted | - | ||||||||||||
923 | for the point size of the font. | - | ||||||||||||
924 | - | |||||||||||||
925 | \sa underlinePos(), overlinePos(), strikeOutPos() | - | ||||||||||||
926 | */ | - | ||||||||||||
927 | int QFontMetrics::lineWidth() const | - | ||||||||||||
928 | { | - | ||||||||||||
929 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
930 | Q_ASSERT(engine != 0); | - | ||||||||||||
931 | return qRound(engine->lineThickness()); never executed: return qRound(engine->lineThickness()); | 0 | ||||||||||||
932 | } | - | ||||||||||||
933 | - | |||||||||||||
934 | - | |||||||||||||
935 | - | |||||||||||||
936 | - | |||||||||||||
937 | /***************************************************************************** | - | ||||||||||||
938 | QFontMetricsF member functions | - | ||||||||||||
939 | *****************************************************************************/ | - | ||||||||||||
940 | - | |||||||||||||
941 | /*! | - | ||||||||||||
942 | \class QFontMetricsF | - | ||||||||||||
943 | \reentrant | - | ||||||||||||
944 | \inmodule QtGui | - | ||||||||||||
945 | - | |||||||||||||
946 | \brief The QFontMetricsF class provides font metrics information. | - | ||||||||||||
947 | - | |||||||||||||
948 | \ingroup painting | - | ||||||||||||
949 | \ingroup shared | - | ||||||||||||
950 | - | |||||||||||||
951 | QFontMetricsF functions calculate the size of characters and | - | ||||||||||||
952 | strings for a given font. You can construct a QFontMetricsF object | - | ||||||||||||
953 | with an existing QFont to obtain metrics for that font. If the | - | ||||||||||||
954 | font is changed later, the font metrics object is \e not updated. | - | ||||||||||||
955 | - | |||||||||||||
956 | Once created, the object provides functions to access the | - | ||||||||||||
957 | individual metrics of the font, its characters, and for strings | - | ||||||||||||
958 | rendered in the font. | - | ||||||||||||
959 | - | |||||||||||||
960 | There are several functions that operate on the font: ascent(), | - | ||||||||||||
961 | descent(), height(), leading() and lineSpacing() return the basic | - | ||||||||||||
962 | size properties of the font. The underlinePos(), overlinePos(), | - | ||||||||||||
963 | strikeOutPos() and lineWidth() functions, return the properties of | - | ||||||||||||
964 | the line that underlines, overlines or strikes out the | - | ||||||||||||
965 | characters. These functions are all fast. | - | ||||||||||||
966 | - | |||||||||||||
967 | There are also some functions that operate on the set of glyphs in | - | ||||||||||||
968 | the font: minLeftBearing(), minRightBearing() and maxWidth(). | - | ||||||||||||
969 | These are by necessity slow, and we recommend avoiding them if | - | ||||||||||||
970 | possible. | - | ||||||||||||
971 | - | |||||||||||||
972 | For each character, you can get its width(), leftBearing() and | - | ||||||||||||
973 | rightBearing() and find out whether it is in the font using | - | ||||||||||||
974 | inFont(). You can also treat the character as a string, and use | - | ||||||||||||
975 | the string functions on it. | - | ||||||||||||
976 | - | |||||||||||||
977 | The string functions include width(), to return the width of a | - | ||||||||||||
978 | string in pixels (or points, for a printer), boundingRect(), to | - | ||||||||||||
979 | return a rectangle large enough to contain the rendered string, | - | ||||||||||||
980 | and size(), to return the size of that rectangle. | - | ||||||||||||
981 | - | |||||||||||||
982 | Example: | - | ||||||||||||
983 | \snippet code/src_gui_text_qfontmetrics.cpp 1 | - | ||||||||||||
984 | - | |||||||||||||
985 | \sa QFont, QFontInfo, QFontDatabase | - | ||||||||||||
986 | */ | - | ||||||||||||
987 | - | |||||||||||||
988 | /*! | - | ||||||||||||
989 | \since 4.2 | - | ||||||||||||
990 | - | |||||||||||||
991 | Constructs a font metrics object with floating point precision | - | ||||||||||||
992 | from the given \a fontMetrics object. | - | ||||||||||||
993 | */ | - | ||||||||||||
994 | QFontMetricsF::QFontMetricsF(const QFontMetrics &fontMetrics) | - | ||||||||||||
995 | : d(fontMetrics.d.data()) | - | ||||||||||||
996 | { | - | ||||||||||||
997 | } never executed: end of block | 0 | ||||||||||||
998 | - | |||||||||||||
999 | /*! | - | ||||||||||||
1000 | \since 4.2 | - | ||||||||||||
1001 | - | |||||||||||||
1002 | Assigns \a other to this object. | - | ||||||||||||
1003 | */ | - | ||||||||||||
1004 | QFontMetricsF &QFontMetricsF::operator=(const QFontMetrics &other) | - | ||||||||||||
1005 | { | - | ||||||||||||
1006 | d = other.d.data(); | - | ||||||||||||
1007 | return *this; never executed: return *this; | 0 | ||||||||||||
1008 | } | - | ||||||||||||
1009 | - | |||||||||||||
1010 | /*! | - | ||||||||||||
1011 | \fn void QFontMetricsF::swap(QFontMetricsF &other) | - | ||||||||||||
1012 | \since 5.0 | - | ||||||||||||
1013 | - | |||||||||||||
1014 | Swaps this font metrics instance with \a other. This function is | - | ||||||||||||
1015 | very fast and never fails. | - | ||||||||||||
1016 | */ | - | ||||||||||||
1017 | - | |||||||||||||
1018 | - | |||||||||||||
1019 | - | |||||||||||||
1020 | /*! | - | ||||||||||||
1021 | Constructs a font metrics object for \a font. | - | ||||||||||||
1022 | - | |||||||||||||
1023 | The font metrics will be compatible with the paintdevice used to | - | ||||||||||||
1024 | create \a font. | - | ||||||||||||
1025 | - | |||||||||||||
1026 | The font metrics object holds the information for the font that is | - | ||||||||||||
1027 | passed in the constructor at the time it is created, and is not | - | ||||||||||||
1028 | updated if the font's attributes are changed later. | - | ||||||||||||
1029 | - | |||||||||||||
1030 | Use QFontMetricsF(const QFont &, QPaintDevice *) to get the font | - | ||||||||||||
1031 | metrics that are compatible with a certain paint device. | - | ||||||||||||
1032 | */ | - | ||||||||||||
1033 | QFontMetricsF::QFontMetricsF(const QFont &font) | - | ||||||||||||
1034 | : d(font.d.data()) | - | ||||||||||||
1035 | { | - | ||||||||||||
1036 | } never executed: end of block | 0 | ||||||||||||
1037 | - | |||||||||||||
1038 | /*! | - | ||||||||||||
1039 | Constructs a font metrics object for \a font and \a paintdevice. | - | ||||||||||||
1040 | - | |||||||||||||
1041 | The font metrics will be compatible with the paintdevice passed. | - | ||||||||||||
1042 | If the \a paintdevice is 0, the metrics will be screen-compatible, | - | ||||||||||||
1043 | ie. the metrics you get if you use the font for drawing text on a | - | ||||||||||||
1044 | \l{QWidget}{widgets} or \l{QPixmap}{pixmaps}, | - | ||||||||||||
1045 | not on a QPicture or QPrinter. | - | ||||||||||||
1046 | - | |||||||||||||
1047 | The font metrics object holds the information for the font that is | - | ||||||||||||
1048 | passed in the constructor at the time it is created, and is not | - | ||||||||||||
1049 | updated if the font's attributes are changed later. | - | ||||||||||||
1050 | */ | - | ||||||||||||
1051 | QFontMetricsF::QFontMetricsF(const QFont &font, QPaintDevice *paintdevice) | - | ||||||||||||
1052 | { | - | ||||||||||||
1053 | int dpi = paintdevice ? paintdevice->logicalDpiY() : qt_defaultDpi();
| 0 | ||||||||||||
1054 | const int screen = 0; | - | ||||||||||||
1055 | if (font.d->dpi != dpi || font.d->screen != screen ) {
| 0 | ||||||||||||
1056 | d = new QFontPrivate(*font.d); | - | ||||||||||||
1057 | d->dpi = dpi; | - | ||||||||||||
1058 | d->screen = screen; | - | ||||||||||||
1059 | } else { never executed: end of block | 0 | ||||||||||||
1060 | d = font.d.data(); | - | ||||||||||||
1061 | } never executed: end of block | 0 | ||||||||||||
1062 | - | |||||||||||||
1063 | } | - | ||||||||||||
1064 | - | |||||||||||||
1065 | /*! | - | ||||||||||||
1066 | Constructs a copy of \a fm. | - | ||||||||||||
1067 | */ | - | ||||||||||||
1068 | QFontMetricsF::QFontMetricsF(const QFontMetricsF &fm) | - | ||||||||||||
1069 | : d(fm.d.data()) | - | ||||||||||||
1070 | { | - | ||||||||||||
1071 | } never executed: end of block | 0 | ||||||||||||
1072 | - | |||||||||||||
1073 | /*! | - | ||||||||||||
1074 | Destroys the font metrics object and frees all allocated | - | ||||||||||||
1075 | resources. | - | ||||||||||||
1076 | */ | - | ||||||||||||
1077 | QFontMetricsF::~QFontMetricsF() | - | ||||||||||||
1078 | { | - | ||||||||||||
1079 | } | - | ||||||||||||
1080 | - | |||||||||||||
1081 | /*! | - | ||||||||||||
1082 | Assigns the font metrics \a fm to this font metrics object. | - | ||||||||||||
1083 | */ | - | ||||||||||||
1084 | QFontMetricsF &QFontMetricsF::operator=(const QFontMetricsF &fm) | - | ||||||||||||
1085 | { | - | ||||||||||||
1086 | d = fm.d.data(); | - | ||||||||||||
1087 | return *this; never executed: return *this; | 0 | ||||||||||||
1088 | } | - | ||||||||||||
1089 | - | |||||||||||||
1090 | /*! | - | ||||||||||||
1091 | Returns \c true if the font metrics are equal to the \a other font | - | ||||||||||||
1092 | metrics; otherwise returns \c false. | - | ||||||||||||
1093 | - | |||||||||||||
1094 | Two font metrics are considered equal if they were constructed from the | - | ||||||||||||
1095 | same QFont and the paint devices they were constructed for are | - | ||||||||||||
1096 | considered to be compatible. | - | ||||||||||||
1097 | */ | - | ||||||||||||
1098 | bool QFontMetricsF::operator ==(const QFontMetricsF &other) const | - | ||||||||||||
1099 | { | - | ||||||||||||
1100 | return d == other.d; never executed: return d == other.d; | 0 | ||||||||||||
1101 | } | - | ||||||||||||
1102 | - | |||||||||||||
1103 | /*! | - | ||||||||||||
1104 | \fn bool QFontMetricsF::operator !=(const QFontMetricsF &other) const | - | ||||||||||||
1105 | \overload | - | ||||||||||||
1106 | - | |||||||||||||
1107 | Returns \c true if the font metrics are not equal to the \a other font | - | ||||||||||||
1108 | metrics; otherwise returns \c false. | - | ||||||||||||
1109 | - | |||||||||||||
1110 | \sa operator==() | - | ||||||||||||
1111 | */ | - | ||||||||||||
1112 | - | |||||||||||||
1113 | /*! | - | ||||||||||||
1114 | Returns the ascent of the font. | - | ||||||||||||
1115 | - | |||||||||||||
1116 | The ascent of a font is the distance from the baseline to the | - | ||||||||||||
1117 | highest position characters extend to. In practice, some font | - | ||||||||||||
1118 | designers break this rule, e.g. when they put more than one accent | - | ||||||||||||
1119 | on top of a character, or to accommodate an unusual character in | - | ||||||||||||
1120 | an exotic language, so it is possible (though rare) that this | - | ||||||||||||
1121 | value will be too small. | - | ||||||||||||
1122 | - | |||||||||||||
1123 | \sa descent() | - | ||||||||||||
1124 | */ | - | ||||||||||||
1125 | qreal QFontMetricsF::ascent() const | - | ||||||||||||
1126 | { | - | ||||||||||||
1127 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1128 | Q_ASSERT(engine != 0); | - | ||||||||||||
1129 | return engine->ascent().toReal(); never executed: return engine->ascent().toReal(); | 0 | ||||||||||||
1130 | } | - | ||||||||||||
1131 | - | |||||||||||||
1132 | - | |||||||||||||
1133 | /*! | - | ||||||||||||
1134 | Returns the descent of the font. | - | ||||||||||||
1135 | - | |||||||||||||
1136 | The descent is the distance from the base line to the lowest point | - | ||||||||||||
1137 | characters extend to. (Note that this is different from X, which | - | ||||||||||||
1138 | adds 1 pixel.) In practice, some font designers break this rule, | - | ||||||||||||
1139 | e.g. to accommodate an unusual character in an exotic language, so | - | ||||||||||||
1140 | it is possible (though rare) that this value will be too small. | - | ||||||||||||
1141 | - | |||||||||||||
1142 | \sa ascent() | - | ||||||||||||
1143 | */ | - | ||||||||||||
1144 | qreal QFontMetricsF::descent() const | - | ||||||||||||
1145 | { | - | ||||||||||||
1146 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1147 | Q_ASSERT(engine != 0); | - | ||||||||||||
1148 | return engine->descent().toReal(); never executed: return engine->descent().toReal(); | 0 | ||||||||||||
1149 | } | - | ||||||||||||
1150 | - | |||||||||||||
1151 | /*! | - | ||||||||||||
1152 | Returns the height of the font. | - | ||||||||||||
1153 | - | |||||||||||||
1154 | This is always equal to ascent()+descent(). | - | ||||||||||||
1155 | - | |||||||||||||
1156 | \sa leading(), lineSpacing() | - | ||||||||||||
1157 | */ | - | ||||||||||||
1158 | qreal QFontMetricsF::height() const | - | ||||||||||||
1159 | { | - | ||||||||||||
1160 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1161 | Q_ASSERT(engine != 0); | - | ||||||||||||
1162 | - | |||||||||||||
1163 | return (engine->ascent() + engine->descent()).toReal(); never executed: return (engine->ascent() + engine->descent()).toReal(); | 0 | ||||||||||||
1164 | } | - | ||||||||||||
1165 | - | |||||||||||||
1166 | /*! | - | ||||||||||||
1167 | Returns the leading of the font. | - | ||||||||||||
1168 | - | |||||||||||||
1169 | This is the natural inter-line spacing. | - | ||||||||||||
1170 | - | |||||||||||||
1171 | \sa height(), lineSpacing() | - | ||||||||||||
1172 | */ | - | ||||||||||||
1173 | qreal QFontMetricsF::leading() const | - | ||||||||||||
1174 | { | - | ||||||||||||
1175 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1176 | Q_ASSERT(engine != 0); | - | ||||||||||||
1177 | return engine->leading().toReal(); never executed: return engine->leading().toReal(); | 0 | ||||||||||||
1178 | } | - | ||||||||||||
1179 | - | |||||||||||||
1180 | /*! | - | ||||||||||||
1181 | Returns the distance from one base line to the next. | - | ||||||||||||
1182 | - | |||||||||||||
1183 | This value is always equal to leading()+height(). | - | ||||||||||||
1184 | - | |||||||||||||
1185 | \sa height(), leading() | - | ||||||||||||
1186 | */ | - | ||||||||||||
1187 | qreal QFontMetricsF::lineSpacing() const | - | ||||||||||||
1188 | { | - | ||||||||||||
1189 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1190 | Q_ASSERT(engine != 0); | - | ||||||||||||
1191 | return (engine->leading() + engine->ascent() + engine->descent()).toReal(); never executed: return (engine->leading() + engine->ascent() + engine->descent()).toReal(); | 0 | ||||||||||||
1192 | } | - | ||||||||||||
1193 | - | |||||||||||||
1194 | /*! | - | ||||||||||||
1195 | Returns the minimum left bearing of the font. | - | ||||||||||||
1196 | - | |||||||||||||
1197 | This is the smallest leftBearing(char) of all characters in the | - | ||||||||||||
1198 | font. | - | ||||||||||||
1199 | - | |||||||||||||
1200 | Note that this function can be very slow if the font is large. | - | ||||||||||||
1201 | - | |||||||||||||
1202 | \sa minRightBearing(), leftBearing() | - | ||||||||||||
1203 | */ | - | ||||||||||||
1204 | qreal QFontMetricsF::minLeftBearing() const | - | ||||||||||||
1205 | { | - | ||||||||||||
1206 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1207 | Q_ASSERT(engine != 0); | - | ||||||||||||
1208 | return engine->minLeftBearing(); never executed: return engine->minLeftBearing(); | 0 | ||||||||||||
1209 | } | - | ||||||||||||
1210 | - | |||||||||||||
1211 | /*! | - | ||||||||||||
1212 | Returns the minimum right bearing of the font. | - | ||||||||||||
1213 | - | |||||||||||||
1214 | This is the smallest rightBearing(char) of all characters in the | - | ||||||||||||
1215 | font. | - | ||||||||||||
1216 | - | |||||||||||||
1217 | Note that this function can be very slow if the font is large. | - | ||||||||||||
1218 | - | |||||||||||||
1219 | \sa minLeftBearing(), rightBearing() | - | ||||||||||||
1220 | */ | - | ||||||||||||
1221 | qreal QFontMetricsF::minRightBearing() const | - | ||||||||||||
1222 | { | - | ||||||||||||
1223 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1224 | Q_ASSERT(engine != 0); | - | ||||||||||||
1225 | return engine->minRightBearing(); never executed: return engine->minRightBearing(); | 0 | ||||||||||||
1226 | } | - | ||||||||||||
1227 | - | |||||||||||||
1228 | /*! | - | ||||||||||||
1229 | Returns the width of the widest character in the font. | - | ||||||||||||
1230 | */ | - | ||||||||||||
1231 | qreal QFontMetricsF::maxWidth() const | - | ||||||||||||
1232 | { | - | ||||||||||||
1233 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1234 | Q_ASSERT(engine != 0); | - | ||||||||||||
1235 | return engine->maxCharWidth(); never executed: return engine->maxCharWidth(); | 0 | ||||||||||||
1236 | } | - | ||||||||||||
1237 | - | |||||||||||||
1238 | /*! | - | ||||||||||||
1239 | Returns the 'x' height of the font. This is often but not always | - | ||||||||||||
1240 | the same as the height of the character 'x'. | - | ||||||||||||
1241 | */ | - | ||||||||||||
1242 | qreal QFontMetricsF::xHeight() const | - | ||||||||||||
1243 | { | - | ||||||||||||
1244 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1245 | Q_ASSERT(engine != 0); | - | ||||||||||||
1246 | if (d->capital == QFont::SmallCaps)
| 0 | ||||||||||||
1247 | return d->smallCapsFontPrivate()->engineForScript(QChar::Script_Common)->ascent().toReal(); never executed: return d->smallCapsFontPrivate()->engineForScript(QChar::Script_Common)->ascent().toReal(); | 0 | ||||||||||||
1248 | return engine->xHeight().toReal(); never executed: return engine->xHeight().toReal(); | 0 | ||||||||||||
1249 | } | - | ||||||||||||
1250 | - | |||||||||||||
1251 | /*! | - | ||||||||||||
1252 | \since 4.2 | - | ||||||||||||
1253 | - | |||||||||||||
1254 | Returns the average width of glyphs in the font. | - | ||||||||||||
1255 | */ | - | ||||||||||||
1256 | qreal QFontMetricsF::averageCharWidth() const | - | ||||||||||||
1257 | { | - | ||||||||||||
1258 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1259 | Q_ASSERT(engine != 0); | - | ||||||||||||
1260 | return engine->averageCharWidth().toReal(); never executed: return engine->averageCharWidth().toReal(); | 0 | ||||||||||||
1261 | } | - | ||||||||||||
1262 | - | |||||||||||||
1263 | /*! | - | ||||||||||||
1264 | Returns \c true if character \a ch is a valid character in the font; | - | ||||||||||||
1265 | otherwise returns \c false. | - | ||||||||||||
1266 | */ | - | ||||||||||||
1267 | bool QFontMetricsF::inFont(QChar ch) const | - | ||||||||||||
1268 | { | - | ||||||||||||
1269 | return inFontUcs4(ch.unicode()); never executed: return inFontUcs4(ch.unicode()); | 0 | ||||||||||||
1270 | } | - | ||||||||||||
1271 | - | |||||||||||||
1272 | /*! | - | ||||||||||||
1273 | \fn bool QFontMetricsF::inFontUcs4(uint ch) const | - | ||||||||||||
1274 | - | |||||||||||||
1275 | Returns \c true if the character given by \a ch, encoded in UCS-4/UTF-32, | - | ||||||||||||
1276 | is a valid character in the font; otherwise returns \c false. | - | ||||||||||||
1277 | */ | - | ||||||||||||
1278 | bool QFontMetricsF::inFontUcs4(uint ucs4) const | - | ||||||||||||
1279 | { | - | ||||||||||||
1280 | const int script = QChar::script(ucs4); | - | ||||||||||||
1281 | QFontEngine *engine = d->engineForScript(script); | - | ||||||||||||
1282 | Q_ASSERT(engine != 0); | - | ||||||||||||
1283 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
1284 | return false; never executed: return false; | 0 | ||||||||||||
1285 | return engine->canRender(ucs4); never executed: return engine->canRender(ucs4); | 0 | ||||||||||||
1286 | } | - | ||||||||||||
1287 | - | |||||||||||||
1288 | /*! | - | ||||||||||||
1289 | Returns the left bearing of character \a ch in the font. | - | ||||||||||||
1290 | - | |||||||||||||
1291 | The left bearing is the right-ward distance of the left-most pixel | - | ||||||||||||
1292 | of the character from the logical origin of the character. This | - | ||||||||||||
1293 | value is negative if the pixels of the character extend to the | - | ||||||||||||
1294 | left of the logical origin. | - | ||||||||||||
1295 | - | |||||||||||||
1296 | See width(QChar) for a graphical description of this metric. | - | ||||||||||||
1297 | - | |||||||||||||
1298 | \sa rightBearing(), minLeftBearing(), width() | - | ||||||||||||
1299 | */ | - | ||||||||||||
1300 | qreal QFontMetricsF::leftBearing(QChar ch) const | - | ||||||||||||
1301 | { | - | ||||||||||||
1302 | const int script = ch.script(); | - | ||||||||||||
1303 | QFontEngine *engine; | - | ||||||||||||
1304 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
1305 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
1306 | else | - | ||||||||||||
1307 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
1308 | Q_ASSERT(engine != 0); | - | ||||||||||||
1309 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
1310 | return 0; never executed: return 0; | 0 | ||||||||||||
1311 | - | |||||||||||||
1312 | d->alterCharForCapitalization(ch); | - | ||||||||||||
1313 | - | |||||||||||||
1314 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
1315 | - | |||||||||||||
1316 | qreal lb; | - | ||||||||||||
1317 | engine->getGlyphBearings(glyph, &lb); | - | ||||||||||||
1318 | return lb; never executed: return lb; | 0 | ||||||||||||
1319 | } | - | ||||||||||||
1320 | - | |||||||||||||
1321 | /*! | - | ||||||||||||
1322 | Returns the right bearing of character \a ch in the font. | - | ||||||||||||
1323 | - | |||||||||||||
1324 | The right bearing is the left-ward distance of the right-most | - | ||||||||||||
1325 | pixel of the character from the logical origin of a subsequent | - | ||||||||||||
1326 | character. This value is negative if the pixels of the character | - | ||||||||||||
1327 | extend to the right of the width() of the character. | - | ||||||||||||
1328 | - | |||||||||||||
1329 | See width() for a graphical description of this metric. | - | ||||||||||||
1330 | - | |||||||||||||
1331 | \sa leftBearing(), minRightBearing(), width() | - | ||||||||||||
1332 | */ | - | ||||||||||||
1333 | qreal QFontMetricsF::rightBearing(QChar ch) const | - | ||||||||||||
1334 | { | - | ||||||||||||
1335 | const int script = ch.script(); | - | ||||||||||||
1336 | QFontEngine *engine; | - | ||||||||||||
1337 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
1338 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
1339 | else | - | ||||||||||||
1340 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
1341 | Q_ASSERT(engine != 0); | - | ||||||||||||
1342 | if (engine->type() == QFontEngine::Box)
| 0 | ||||||||||||
1343 | return 0; never executed: return 0; | 0 | ||||||||||||
1344 | - | |||||||||||||
1345 | d->alterCharForCapitalization(ch); | - | ||||||||||||
1346 | - | |||||||||||||
1347 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
1348 | - | |||||||||||||
1349 | qreal rb; | - | ||||||||||||
1350 | engine->getGlyphBearings(glyph, 0, &rb); | - | ||||||||||||
1351 | return rb; never executed: return rb; | 0 | ||||||||||||
1352 | - | |||||||||||||
1353 | } | - | ||||||||||||
1354 | - | |||||||||||||
1355 | /*! | - | ||||||||||||
1356 | Returns the width in pixels of the characters in the given \a text. | - | ||||||||||||
1357 | - | |||||||||||||
1358 | Note that this value is \e not equal to the width returned by | - | ||||||||||||
1359 | boundingRect().width() because boundingRect() returns a rectangle | - | ||||||||||||
1360 | describing the pixels this string will cover whereas width() | - | ||||||||||||
1361 | returns the distance to where the next string should be drawn. | - | ||||||||||||
1362 | - | |||||||||||||
1363 | \sa boundingRect() | - | ||||||||||||
1364 | */ | - | ||||||||||||
1365 | qreal QFontMetricsF::width(const QString &text) const | - | ||||||||||||
1366 | { | - | ||||||||||||
1367 | int pos = text.indexOf(QLatin1Char('\x9c')); | - | ||||||||||||
1368 | int len = (pos != -1) ? pos : text.length();
| 0 | ||||||||||||
1369 | - | |||||||||||||
1370 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
1371 | layout.itemize(); | - | ||||||||||||
1372 | return layout.width(0, len).toReal(); never executed: return layout.width(0, len).toReal(); | 0 | ||||||||||||
1373 | } | - | ||||||||||||
1374 | - | |||||||||||||
1375 | /*! | - | ||||||||||||
1376 | \overload | - | ||||||||||||
1377 | - | |||||||||||||
1378 | \image bearings.png Bearings | - | ||||||||||||
1379 | - | |||||||||||||
1380 | Returns the logical width of character \a ch in pixels. This is a | - | ||||||||||||
1381 | distance appropriate for drawing a subsequent character after \a | - | ||||||||||||
1382 | ch. | - | ||||||||||||
1383 | - | |||||||||||||
1384 | Some of the metrics are described in the image to the right. The | - | ||||||||||||
1385 | central dark rectangles cover the logical width() of each | - | ||||||||||||
1386 | character. The outer pale rectangles cover the leftBearing() and | - | ||||||||||||
1387 | rightBearing() of each character. Notice that the bearings of "f" | - | ||||||||||||
1388 | in this particular font are both negative, while the bearings of | - | ||||||||||||
1389 | "o" are both positive. | - | ||||||||||||
1390 | - | |||||||||||||
1391 | \warning This function will produce incorrect results for Arabic | - | ||||||||||||
1392 | characters or non-spacing marks in the middle of a string, as the | - | ||||||||||||
1393 | glyph shaping and positioning of marks that happens when | - | ||||||||||||
1394 | processing strings cannot be taken into account. When implementing | - | ||||||||||||
1395 | an interactive text control, use QTextLayout instead. | - | ||||||||||||
1396 | - | |||||||||||||
1397 | \sa boundingRect() | - | ||||||||||||
1398 | */ | - | ||||||||||||
1399 | qreal QFontMetricsF::width(QChar ch) const | - | ||||||||||||
1400 | { | - | ||||||||||||
1401 | if (ch.category() == QChar::Mark_NonSpacing)
| 0 | ||||||||||||
1402 | return 0.; never executed: return 0.; | 0 | ||||||||||||
1403 | - | |||||||||||||
1404 | const int script = ch.script(); | - | ||||||||||||
1405 | QFontEngine *engine; | - | ||||||||||||
1406 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
1407 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
1408 | else | - | ||||||||||||
1409 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
1410 | Q_ASSERT(engine != 0); | - | ||||||||||||
1411 | - | |||||||||||||
1412 | d->alterCharForCapitalization(ch); | - | ||||||||||||
1413 | - | |||||||||||||
1414 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
1415 | QFixed advance; | - | ||||||||||||
1416 | - | |||||||||||||
1417 | QGlyphLayout glyphs; | - | ||||||||||||
1418 | glyphs.numGlyphs = 1; | - | ||||||||||||
1419 | glyphs.glyphs = &glyph; | - | ||||||||||||
1420 | glyphs.advances = &advance; | - | ||||||||||||
1421 | engine->recalcAdvances(&glyphs, 0); | - | ||||||||||||
1422 | - | |||||||||||||
1423 | return advance.toReal(); never executed: return advance.toReal(); | 0 | ||||||||||||
1424 | } | - | ||||||||||||
1425 | - | |||||||||||||
1426 | /*! | - | ||||||||||||
1427 | Returns the bounding rectangle of the characters in the string | - | ||||||||||||
1428 | specified by \a text. The bounding rectangle always covers at least | - | ||||||||||||
1429 | the set of pixels the text would cover if drawn at (0, 0). | - | ||||||||||||
1430 | - | |||||||||||||
1431 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
1432 | e.g. for italicized fonts, and that the width of the returned | - | ||||||||||||
1433 | rectangle might be different than what the width() method returns. | - | ||||||||||||
1434 | - | |||||||||||||
1435 | If you want to know the advance width of the string (to lay out | - | ||||||||||||
1436 | a set of strings next to each other), use width() instead. | - | ||||||||||||
1437 | - | |||||||||||||
1438 | Newline characters are processed as normal characters, \e not as | - | ||||||||||||
1439 | linebreaks. | - | ||||||||||||
1440 | - | |||||||||||||
1441 | The height of the bounding rectangle is at least as large as the | - | ||||||||||||
1442 | value returned height(). | - | ||||||||||||
1443 | - | |||||||||||||
1444 | \sa width(), height(), QPainter::boundingRect() | - | ||||||||||||
1445 | */ | - | ||||||||||||
1446 | QRectF QFontMetricsF::boundingRect(const QString &text) const | - | ||||||||||||
1447 | { | - | ||||||||||||
1448 | int len = text.length(); | - | ||||||||||||
1449 | if (len == 0)
| 0 | ||||||||||||
1450 | return QRectF(); never executed: return QRectF(); | 0 | ||||||||||||
1451 | - | |||||||||||||
1452 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
1453 | layout.itemize(); | - | ||||||||||||
1454 | glyph_metrics_t gm = layout.boundingBox(0, len); | - | ||||||||||||
1455 | return QRectF(gm.x.toReal(), gm.y.toReal(), never executed: return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); | 0 | ||||||||||||
1456 | gm.width.toReal(), gm.height.toReal()); never executed: return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); | 0 | ||||||||||||
1457 | } | - | ||||||||||||
1458 | - | |||||||||||||
1459 | /*! | - | ||||||||||||
1460 | Returns the bounding rectangle of the character \a ch relative to | - | ||||||||||||
1461 | the left-most point on the base line. | - | ||||||||||||
1462 | - | |||||||||||||
1463 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
1464 | e.g. for italicized fonts, and that the text output may cover \e | - | ||||||||||||
1465 | all pixels in the bounding rectangle. | - | ||||||||||||
1466 | - | |||||||||||||
1467 | Note that the rectangle usually extends both above and below the | - | ||||||||||||
1468 | base line. | - | ||||||||||||
1469 | - | |||||||||||||
1470 | \sa width() | - | ||||||||||||
1471 | */ | - | ||||||||||||
1472 | QRectF QFontMetricsF::boundingRect(QChar ch) const | - | ||||||||||||
1473 | { | - | ||||||||||||
1474 | const int script = ch.script(); | - | ||||||||||||
1475 | QFontEngine *engine; | - | ||||||||||||
1476 | if (d->capital == QFont::SmallCaps && ch.isLower())
| 0 | ||||||||||||
1477 | engine = d->smallCapsFontPrivate()->engineForScript(script); never executed: engine = d->smallCapsFontPrivate()->engineForScript(script); | 0 | ||||||||||||
1478 | else | - | ||||||||||||
1479 | engine = d->engineForScript(script); never executed: engine = d->engineForScript(script); | 0 | ||||||||||||
1480 | Q_ASSERT(engine != 0); | - | ||||||||||||
1481 | - | |||||||||||||
1482 | d->alterCharForCapitalization(ch); | - | ||||||||||||
1483 | - | |||||||||||||
1484 | glyph_t glyph = engine->glyphIndex(ch.unicode()); | - | ||||||||||||
1485 | - | |||||||||||||
1486 | glyph_metrics_t gm = engine->boundingBox(glyph); | - | ||||||||||||
1487 | return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); never executed: return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); | 0 | ||||||||||||
1488 | } | - | ||||||||||||
1489 | - | |||||||||||||
1490 | /*! | - | ||||||||||||
1491 | \overload | - | ||||||||||||
1492 | - | |||||||||||||
1493 | Returns the bounding rectangle of the characters in the given \a text. | - | ||||||||||||
1494 | This is the set of pixels the text would cover if drawn when constrained | - | ||||||||||||
1495 | to the bounding rectangle specified by \a rect. | - | ||||||||||||
1496 | - | |||||||||||||
1497 | The \a flags argument is the bitwise OR of the following flags: | - | ||||||||||||
1498 | \list | - | ||||||||||||
1499 | \li Qt::AlignLeft aligns to the left border, except for | - | ||||||||||||
1500 | Arabic and Hebrew where it aligns to the right. | - | ||||||||||||
1501 | \li Qt::AlignRight aligns to the right border, except for | - | ||||||||||||
1502 | Arabic and Hebrew where it aligns to the left. | - | ||||||||||||
1503 | \li Qt::AlignJustify produces justified text. | - | ||||||||||||
1504 | \li Qt::AlignHCenter aligns horizontally centered. | - | ||||||||||||
1505 | \li Qt::AlignTop aligns to the top border. | - | ||||||||||||
1506 | \li Qt::AlignBottom aligns to the bottom border. | - | ||||||||||||
1507 | \li Qt::AlignVCenter aligns vertically centered | - | ||||||||||||
1508 | \li Qt::AlignCenter (== \c{Qt::AlignHCenter | Qt::AlignVCenter}) | - | ||||||||||||
1509 | \li Qt::TextSingleLine ignores newline characters in the text. | - | ||||||||||||
1510 | \li Qt::TextExpandTabs expands tabs (see below) | - | ||||||||||||
1511 | \li Qt::TextShowMnemonic interprets "&x" as \underline{x}; i.e., underlined. | - | ||||||||||||
1512 | \li Qt::TextWordWrap breaks the text to fit the rectangle. | - | ||||||||||||
1513 | \endlist | - | ||||||||||||
1514 | - | |||||||||||||
1515 | Qt::Horizontal alignment defaults to Qt::AlignLeft and vertical | - | ||||||||||||
1516 | alignment defaults to Qt::AlignTop. | - | ||||||||||||
1517 | - | |||||||||||||
1518 | If several of the horizontal or several of the vertical alignment | - | ||||||||||||
1519 | flags are set, the resulting alignment is undefined. | - | ||||||||||||
1520 | - | |||||||||||||
1521 | These flags are defined in \l{Qt::AlignmentFlag}. | - | ||||||||||||
1522 | - | |||||||||||||
1523 | If Qt::TextExpandTabs is set in \a flags, the following behavior is | - | ||||||||||||
1524 | used to interpret tab characters in the text: | - | ||||||||||||
1525 | \list | - | ||||||||||||
1526 | \li If \a tabArray is non-null, it specifies a 0-terminated sequence of | - | ||||||||||||
1527 | pixel-positions for tabs in the text. | - | ||||||||||||
1528 | \li If \a tabStops is non-zero, it is used as the tab spacing (in pixels). | - | ||||||||||||
1529 | \endlist | - | ||||||||||||
1530 | - | |||||||||||||
1531 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
1532 | e.g. for italicized fonts. | - | ||||||||||||
1533 | - | |||||||||||||
1534 | Newline characters are processed as line breaks. | - | ||||||||||||
1535 | - | |||||||||||||
1536 | Despite the different actual character heights, the heights of the | - | ||||||||||||
1537 | bounding rectangles of "Yes" and "yes" are the same. | - | ||||||||||||
1538 | - | |||||||||||||
1539 | The bounding rectangle returned by this function is somewhat larger | - | ||||||||||||
1540 | than that calculated by the simpler boundingRect() function. This | - | ||||||||||||
1541 | function uses the \l{minLeftBearing()}{maximum left} and | - | ||||||||||||
1542 | \l{minRightBearing()}{right} font bearings as is | - | ||||||||||||
1543 | necessary for multi-line text to align correctly. Also, | - | ||||||||||||
1544 | fontHeight() and lineSpacing() are used to calculate the height, | - | ||||||||||||
1545 | rather than individual character heights. | - | ||||||||||||
1546 | - | |||||||||||||
1547 | \sa width(), QPainter::boundingRect(), Qt::Alignment | - | ||||||||||||
1548 | */ | - | ||||||||||||
1549 | QRectF QFontMetricsF::boundingRect(const QRectF &rect, int flags, const QString& text, | - | ||||||||||||
1550 | int tabStops, int *tabArray) const | - | ||||||||||||
1551 | { | - | ||||||||||||
1552 | int tabArrayLen = 0; | - | ||||||||||||
1553 | if (tabArray)
| 0 | ||||||||||||
1554 | while (tabArray[tabArrayLen])
| 0 | ||||||||||||
1555 | tabArrayLen++; never executed: tabArrayLen++; | 0 | ||||||||||||
1556 | - | |||||||||||||
1557 | QRectF rb; | - | ||||||||||||
1558 | qt_format_text(QFont(d.data()), rect, flags | Qt::TextDontPrint, text, &rb, tabStops, tabArray, | - | ||||||||||||
1559 | tabArrayLen, 0); | - | ||||||||||||
1560 | return rb; never executed: return rb; | 0 | ||||||||||||
1561 | } | - | ||||||||||||
1562 | - | |||||||||||||
1563 | /*! | - | ||||||||||||
1564 | Returns the size in pixels of the characters in the given \a text. | - | ||||||||||||
1565 | - | |||||||||||||
1566 | The \a flags argument is the bitwise OR of the following flags: | - | ||||||||||||
1567 | \list | - | ||||||||||||
1568 | \li Qt::TextSingleLine ignores newline characters. | - | ||||||||||||
1569 | \li Qt::TextExpandTabs expands tabs (see below) | - | ||||||||||||
1570 | \li Qt::TextShowMnemonic interprets "&x" as \underline{x}; i.e., underlined. | - | ||||||||||||
1571 | \li Qt::TextWordWrap breaks the text to fit the rectangle. | - | ||||||||||||
1572 | \endlist | - | ||||||||||||
1573 | - | |||||||||||||
1574 | These flags are defined in the \l{Qt::TextFlag} enum. | - | ||||||||||||
1575 | - | |||||||||||||
1576 | If Qt::TextExpandTabs is set in \a flags, the following behavior is | - | ||||||||||||
1577 | used to interpret tab characters in the text: | - | ||||||||||||
1578 | \list | - | ||||||||||||
1579 | \li If \a tabArray is non-null, it specifies a 0-terminated sequence of | - | ||||||||||||
1580 | pixel-positions for tabs in the text. | - | ||||||||||||
1581 | \li If \a tabStops is non-zero, it is used as the tab spacing (in pixels). | - | ||||||||||||
1582 | \endlist | - | ||||||||||||
1583 | - | |||||||||||||
1584 | Newline characters are processed as line breaks. | - | ||||||||||||
1585 | - | |||||||||||||
1586 | Note: Despite the different actual character heights, the heights of the | - | ||||||||||||
1587 | bounding rectangles of "Yes" and "yes" are the same. | - | ||||||||||||
1588 | - | |||||||||||||
1589 | \sa boundingRect() | - | ||||||||||||
1590 | */ | - | ||||||||||||
1591 | QSizeF QFontMetricsF::size(int flags, const QString &text, int tabStops, int *tabArray) const | - | ||||||||||||
1592 | { | - | ||||||||||||
1593 | return boundingRect(QRectF(), flags | Qt::TextLongestVariant, text, tabStops, tabArray).size(); never executed: return boundingRect(QRectF(), flags | Qt::TextLongestVariant, text, tabStops, tabArray).size(); | 0 | ||||||||||||
1594 | } | - | ||||||||||||
1595 | - | |||||||||||||
1596 | /*! | - | ||||||||||||
1597 | \since 4.3 | - | ||||||||||||
1598 | - | |||||||||||||
1599 | Returns a tight bounding rectangle around the characters in the | - | ||||||||||||
1600 | string specified by \a text. The bounding rectangle always covers | - | ||||||||||||
1601 | at least the set of pixels the text would cover if drawn at (0, | - | ||||||||||||
1602 | 0). | - | ||||||||||||
1603 | - | |||||||||||||
1604 | Note that the bounding rectangle may extend to the left of (0, 0), | - | ||||||||||||
1605 | e.g. for italicized fonts, and that the width of the returned | - | ||||||||||||
1606 | rectangle might be different than what the width() method returns. | - | ||||||||||||
1607 | - | |||||||||||||
1608 | If you want to know the advance width of the string (to lay out | - | ||||||||||||
1609 | a set of strings next to each other), use width() instead. | - | ||||||||||||
1610 | - | |||||||||||||
1611 | Newline characters are processed as normal characters, \e not as | - | ||||||||||||
1612 | linebreaks. | - | ||||||||||||
1613 | - | |||||||||||||
1614 | \warning Calling this method is very slow on Windows. | - | ||||||||||||
1615 | - | |||||||||||||
1616 | \sa width(), height(), boundingRect() | - | ||||||||||||
1617 | */ | - | ||||||||||||
1618 | QRectF QFontMetricsF::tightBoundingRect(const QString &text) const | - | ||||||||||||
1619 | { | - | ||||||||||||
1620 | if (text.length() == 0)
| 0 | ||||||||||||
1621 | return QRect(); never executed: return QRect(); | 0 | ||||||||||||
1622 | - | |||||||||||||
1623 | QStackTextEngine layout(text, QFont(d.data())); | - | ||||||||||||
1624 | layout.itemize(); | - | ||||||||||||
1625 | glyph_metrics_t gm = layout.tightBoundingBox(0, text.length()); | - | ||||||||||||
1626 | return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); never executed: return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal()); | 0 | ||||||||||||
1627 | } | - | ||||||||||||
1628 | - | |||||||||||||
1629 | /*! | - | ||||||||||||
1630 | \since 4.2 | - | ||||||||||||
1631 | - | |||||||||||||
1632 | If the string \a text is wider than \a width, returns an elided | - | ||||||||||||
1633 | version of the string (i.e., a string with "..." in it). | - | ||||||||||||
1634 | Otherwise, returns the original string. | - | ||||||||||||
1635 | - | |||||||||||||
1636 | The \a mode parameter specifies whether the text is elided on the | - | ||||||||||||
1637 | left (for example, "...tech"), in the middle (for example, "Tr...ch"), or | - | ||||||||||||
1638 | on the right (for example, "Trol..."). | - | ||||||||||||
1639 | - | |||||||||||||
1640 | The \a width is specified in pixels, not characters. | - | ||||||||||||
1641 | - | |||||||||||||
1642 | The \a flags argument is optional and currently only supports | - | ||||||||||||
1643 | Qt::TextShowMnemonic as value. | - | ||||||||||||
1644 | - | |||||||||||||
1645 | The elide mark follows the \l{Qt::LayoutDirection}{layoutdirection}. | - | ||||||||||||
1646 | For example, it will be on the right side of the text for right-to-left | - | ||||||||||||
1647 | layouts if the \a mode is \c{Qt::ElideLeft}, and on the left side of the | - | ||||||||||||
1648 | text if the \a mode is \c{Qt::ElideRight}. | - | ||||||||||||
1649 | */ | - | ||||||||||||
1650 | QString QFontMetricsF::elidedText(const QString &text, Qt::TextElideMode mode, qreal width, int flags) const | - | ||||||||||||
1651 | { | - | ||||||||||||
1652 | QString _text = text; | - | ||||||||||||
1653 | if (!(flags & Qt::TextLongestVariant)) {
| 0 | ||||||||||||
1654 | int posA = 0; | - | ||||||||||||
1655 | int posB = _text.indexOf(QLatin1Char('\x9c')); | - | ||||||||||||
1656 | while (posB >= 0) {
| 0 | ||||||||||||
1657 | QString portion = _text.mid(posA, posB - posA); | - | ||||||||||||
1658 | if (size(flags, portion).width() <= width)
| 0 | ||||||||||||
1659 | return portion; never executed: return portion; | 0 | ||||||||||||
1660 | posA = posB + 1; | - | ||||||||||||
1661 | posB = _text.indexOf(QLatin1Char('\x9c'), posA); | - | ||||||||||||
1662 | } never executed: end of block | 0 | ||||||||||||
1663 | _text = _text.mid(posA); | - | ||||||||||||
1664 | } never executed: end of block | 0 | ||||||||||||
1665 | QStackTextEngine engine(_text, QFont(d.data())); | - | ||||||||||||
1666 | return engine.elidedText(mode, QFixed::fromReal(width), flags); never executed: return engine.elidedText(mode, QFixed::fromReal(width), flags); | 0 | ||||||||||||
1667 | } | - | ||||||||||||
1668 | - | |||||||||||||
1669 | /*! | - | ||||||||||||
1670 | Returns the distance from the base line to where an underscore | - | ||||||||||||
1671 | should be drawn. | - | ||||||||||||
1672 | - | |||||||||||||
1673 | \sa overlinePos(), strikeOutPos(), lineWidth() | - | ||||||||||||
1674 | */ | - | ||||||||||||
1675 | qreal QFontMetricsF::underlinePos() const | - | ||||||||||||
1676 | { | - | ||||||||||||
1677 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1678 | Q_ASSERT(engine != 0); | - | ||||||||||||
1679 | return engine->underlinePosition().toReal(); never executed: return engine->underlinePosition().toReal(); | 0 | ||||||||||||
1680 | } | - | ||||||||||||
1681 | - | |||||||||||||
1682 | /*! | - | ||||||||||||
1683 | Returns the distance from the base line to where an overline | - | ||||||||||||
1684 | should be drawn. | - | ||||||||||||
1685 | - | |||||||||||||
1686 | \sa underlinePos(), strikeOutPos(), lineWidth() | - | ||||||||||||
1687 | */ | - | ||||||||||||
1688 | qreal QFontMetricsF::overlinePos() const | - | ||||||||||||
1689 | { | - | ||||||||||||
1690 | return ascent() + 1; never executed: return ascent() + 1; | 0 | ||||||||||||
1691 | } | - | ||||||||||||
1692 | - | |||||||||||||
1693 | /*! | - | ||||||||||||
1694 | Returns the distance from the base line to where the strikeout | - | ||||||||||||
1695 | line should be drawn. | - | ||||||||||||
1696 | - | |||||||||||||
1697 | \sa underlinePos(), overlinePos(), lineWidth() | - | ||||||||||||
1698 | */ | - | ||||||||||||
1699 | qreal QFontMetricsF::strikeOutPos() const | - | ||||||||||||
1700 | { | - | ||||||||||||
1701 | return ascent() / 3.; never executed: return ascent() / 3.; | 0 | ||||||||||||
1702 | } | - | ||||||||||||
1703 | - | |||||||||||||
1704 | /*! | - | ||||||||||||
1705 | Returns the width of the underline and strikeout lines, adjusted | - | ||||||||||||
1706 | for the point size of the font. | - | ||||||||||||
1707 | - | |||||||||||||
1708 | \sa underlinePos(), overlinePos(), strikeOutPos() | - | ||||||||||||
1709 | */ | - | ||||||||||||
1710 | qreal QFontMetricsF::lineWidth() const | - | ||||||||||||
1711 | { | - | ||||||||||||
1712 | QFontEngine *engine = d->engineForScript(QChar::Script_Common); | - | ||||||||||||
1713 | Q_ASSERT(engine != 0); | - | ||||||||||||
1714 | return engine->lineThickness().toReal(); never executed: return engine->lineThickness().toReal(); | 0 | ||||||||||||
1715 | } | - | ||||||||||||
1716 | - | |||||||||||||
1717 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |