source: trunk/src/gui/text/qtextengine_p.h@ 858

Last change on this file since 858 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 19.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QTEXTENGINE_P_H
43#define QTEXTENGINE_P_H
44
45//
46// W A R N I N G
47// -------------
48//
49// This file is not part of the Qt API. It exists for the convenience
50// of other Qt classes. This header file may change from version to
51// version without notice, or even be removed.
52//
53// We mean it.
54//
55
56#include "QtCore/qglobal.h"
57#include "QtCore/qstring.h"
58#include "QtCore/qvarlengtharray.h"
59#include "QtCore/qnamespace.h"
60#include "QtGui/qtextlayout.h"
61#include "private/qtextformat_p.h"
62#include "private/qfont_p.h"
63#include "QtCore/qvector.h"
64#include "QtGui/qpaintengine.h"
65#include "QtGui/qtextobject.h"
66#include "QtGui/qtextoption.h"
67#include "QtCore/qset.h"
68#include "QtCore/qdebug.h"
69#ifndef QT_BUILD_COMPAT_LIB
70#include "private/qtextdocument_p.h"
71#endif
72#include "private/qharfbuzz_p.h"
73#include "private/qfixed_p.h"
74
75#include <stdlib.h>
76
77QT_BEGIN_NAMESPACE
78
79class QFontPrivate;
80class QFontEngine;
81
82class QString;
83class QPainter;
84
85class QAbstractTextDocumentLayout;
86
87
88// this uses the same coordinate system as Qt, but a different one to freetype.
89// * y is usually negative, and is equal to the ascent.
90// * negative yoff means the following stuff is drawn higher up.
91// the characters bounding rect is given by QRect(x,y,width,height), its advance by
92// xoo and yoff
93struct glyph_metrics_t
94{
95 inline glyph_metrics_t()
96 : x(100000), y(100000) {}
97 inline glyph_metrics_t(QFixed _x, QFixed _y, QFixed _width, QFixed _height, QFixed _xoff, QFixed _yoff)
98 : x(_x),
99 y(_y),
100 width(_width),
101 height(_height),
102 xoff(_xoff),
103 yoff(_yoff)
104 {}
105 QFixed x;
106 QFixed y;
107 QFixed width;
108 QFixed height;
109 QFixed xoff;
110 QFixed yoff;
111
112 glyph_metrics_t transformed(const QTransform &xform) const;
113 inline bool isValid() const {return x != 100000 && y != 100000;}
114};
115Q_DECLARE_TYPEINFO(glyph_metrics_t, Q_PRIMITIVE_TYPE);
116
117struct Q_AUTOTEST_EXPORT QScriptAnalysis
118{
119 enum Flags {
120 None = 0,
121 Lowercase = 1,
122 Uppercase = 2,
123 SmallCaps = 3,
124 LineOrParagraphSeparator = 4,
125 Space = 5,
126 SpaceTabOrObject = Space,
127 Tab = 6,
128 TabOrObject = Tab,
129 Object = 7
130 };
131 unsigned short script : 8;
132 unsigned short bidiLevel : 6; // Unicode Bidi algorithm embedding level (0-61)
133 unsigned short flags : 3;
134 inline bool operator == (const QScriptAnalysis &other) const {
135 return script == other.script && bidiLevel == other.bidiLevel && flags == other.flags;
136 }
137};
138Q_DECLARE_TYPEINFO(QScriptAnalysis, Q_PRIMITIVE_TYPE);
139
140struct QGlyphJustification
141{
142 inline QGlyphJustification()
143 : type(0), nKashidas(0), space_18d6(0)
144 {}
145
146 enum JustificationType {
147 JustifyNone,
148 JustifySpace,
149 JustifyKashida
150 };
151
152 uint type :2;
153 uint nKashidas : 6; // more do not make sense...
154 uint space_18d6 : 24;
155};
156Q_DECLARE_TYPEINFO(QGlyphJustification, Q_PRIMITIVE_TYPE);
157
158struct QGlyphLayoutInstance
159{
160 QFixedPoint offset;
161 QFixedPoint advance;
162 HB_Glyph glyph;
163 QGlyphJustification justification;
164 HB_GlyphAttributes attributes;
165};
166
167struct QGlyphLayout
168{
169 // init to 0 not needed, done when shaping
170 QFixedPoint *offsets; // 8 bytes per element
171 HB_Glyph *glyphs; // 4 bytes per element
172 QFixed *advances_x; // 4 bytes per element
173 QFixed *advances_y; // 4 bytes per element
174 QGlyphJustification *justifications; // 4 bytes per element
175 HB_GlyphAttributes *attributes; // 2 bytes per element
176
177 int numGlyphs;
178
179 inline QGlyphLayout() : numGlyphs(0) {}
180
181 inline explicit QGlyphLayout(char *address, int totalGlyphs)
182 {
183 offsets = reinterpret_cast<QFixedPoint *>(address);
184 int offset = totalGlyphs * sizeof(HB_FixedPoint);
185 glyphs = reinterpret_cast<HB_Glyph *>(address + offset);
186 offset += totalGlyphs * sizeof(HB_Glyph);
187 advances_x = reinterpret_cast<QFixed *>(address + offset);
188 offset += totalGlyphs * sizeof(QFixed);
189 advances_y = reinterpret_cast<QFixed *>(address + offset);
190 offset += totalGlyphs * sizeof(QFixed);
191 justifications = reinterpret_cast<QGlyphJustification *>(address + offset);
192 offset += totalGlyphs * sizeof(QGlyphJustification);
193 attributes = reinterpret_cast<HB_GlyphAttributes *>(address + offset);
194 numGlyphs = totalGlyphs;
195 }
196
197 inline QGlyphLayout mid(int position, int n = -1) const {
198 QGlyphLayout copy = *this;
199 copy.glyphs += position;
200 copy.advances_x += position;
201 copy.advances_y += position;
202 copy.offsets += position;
203 copy.justifications += position;
204 copy.attributes += position;
205 if (n == -1)
206 copy.numGlyphs -= position;
207 else
208 copy.numGlyphs = n;
209 return copy;
210 }
211
212 static inline int spaceNeededForGlyphLayout(int totalGlyphs) {
213 return totalGlyphs * (sizeof(HB_Glyph) + sizeof(HB_GlyphAttributes)
214 + sizeof(QFixed) + sizeof(QFixed) + sizeof(QFixedPoint)
215 + sizeof(QGlyphJustification));
216 }
217
218 inline QFixed effectiveAdvance(int item) const
219 { return (advances_x[item] + QFixed::fromFixed(justifications[item].space_18d6)) * !attributes[item].dontPrint; }
220
221 inline QGlyphLayoutInstance instance(int position) const {
222 QGlyphLayoutInstance g;
223 g.offset.x = offsets[position].x;
224 g.offset.y = offsets[position].y;
225 g.glyph = glyphs[position];
226 g.advance.x = advances_x[position];
227 g.advance.y = advances_y[position];
228 g.justification = justifications[position];
229 g.attributes = attributes[position];
230 return g;
231 }
232
233 inline void setInstance(int position, const QGlyphLayoutInstance &g) {
234 offsets[position].x = g.offset.x;
235 offsets[position].y = g.offset.y;
236 glyphs[position] = g.glyph;
237 advances_x[position] = g.advance.x;
238 advances_y[position] = g.advance.y;
239 justifications[position] = g.justification;
240 attributes[position] = g.attributes;
241 }
242
243 inline void clear(int first = 0, int last = -1) {
244 if (last == -1)
245 last = numGlyphs;
246 if (first == 0 && last == numGlyphs
247 && reinterpret_cast<char *>(offsets + numGlyphs) == reinterpret_cast<char *>(glyphs)) {
248 memset(offsets, 0, spaceNeededForGlyphLayout(numGlyphs));
249 } else {
250 const int num = last - first;
251 memset(offsets + first, 0, num * sizeof(QFixedPoint));
252 memset(glyphs + first, 0, num * sizeof(HB_Glyph));
253 memset(advances_x + first, 0, num * sizeof(QFixed));
254 memset(advances_y + first, 0, num * sizeof(QFixed));
255 memset(justifications + first, 0, num * sizeof(QGlyphJustification));
256 memset(attributes + first, 0, num * sizeof(HB_GlyphAttributes));
257 }
258 }
259
260 inline char *data() {
261 return reinterpret_cast<char *>(offsets);
262 }
263
264 void grow(char *address, int totalGlyphs);
265};
266
267class QVarLengthGlyphLayoutArray : private QVarLengthArray<void *>, public QGlyphLayout
268{
269private:
270 typedef QVarLengthArray<void *> Array;
271public:
272 QVarLengthGlyphLayoutArray(int totalGlyphs)
273 : Array(spaceNeededForGlyphLayout(totalGlyphs) / sizeof(void *) + 1)
274 , QGlyphLayout(reinterpret_cast<char *>(Array::data()), totalGlyphs)
275 {
276 memset(Array::data(), 0, Array::size() * sizeof(void *));
277 }
278
279 void resize(int totalGlyphs)
280 {
281 Array::resize(spaceNeededForGlyphLayout(totalGlyphs) / sizeof(void *) + 1);
282
283 *((QGlyphLayout *)this) = QGlyphLayout(reinterpret_cast<char *>(Array::data()), totalGlyphs);
284 memset(Array::data(), 0, Array::size() * sizeof(void *));
285 }
286};
287
288template <int N> struct QGlyphLayoutArray : public QGlyphLayout
289{
290public:
291 QGlyphLayoutArray()
292 : QGlyphLayout(reinterpret_cast<char *>(buffer), N)
293 {
294 memset(buffer, 0, sizeof(buffer));
295 }
296
297private:
298 void *buffer[(N * (sizeof(HB_Glyph) + sizeof(HB_GlyphAttributes)
299 + sizeof(QFixed) + sizeof(QFixed) + sizeof(QFixedPoint)
300 + sizeof(QGlyphJustification)))
301 / sizeof(void *) + 1];
302};
303
304struct QScriptItem;
305/// Internal QTextItem
306class QTextItemInt : public QTextItem
307{
308public:
309 inline QTextItemInt()
310 : justified(false), underlineStyle(QTextCharFormat::NoUnderline), num_chars(0), chars(0),
311 logClusters(0), f(0), fontEngine(0)
312 {}
313 QTextItemInt(const QScriptItem &si, QFont *font, const QTextCharFormat &format = QTextCharFormat());
314 QTextItemInt(const QGlyphLayout &g, QFont *font, const QChar *chars, int numChars, QFontEngine *fe);
315
316 /// copy the structure items, adjusting the glyphs arrays to the right subarrays.
317 /// the width of the returned QTextItemInt is not adjusted, for speed reasons
318 QTextItemInt midItem(QFontEngine *fontEngine, int firstGlyphIndex, int numGlyphs) const;
319
320 QFixed descent;
321 QFixed ascent;
322 QFixed width;
323
324 RenderFlags flags;
325 bool justified;
326 QTextCharFormat::UnderlineStyle underlineStyle;
327 const QTextCharFormat charFormat;
328 int num_chars;
329 const QChar *chars;
330 const unsigned short *logClusters;
331 const QFont *f;
332
333 QGlyphLayout glyphs;
334 QFontEngine *fontEngine;
335};
336
337inline bool qIsControlChar(ushort uc)
338{
339 return uc >= 0x200b && uc <= 0x206f
340 && (uc <= 0x200f /* ZW Space, ZWNJ, ZWJ, LRM and RLM */
341 || (uc >= 0x2028 && uc <= 0x202f /* LS, PS, LRE, RLE, PDF, LRO, RLO, NNBSP */)
342 || uc >= 0x206a /* ISS, ASS, IAFS, AFS, NADS, NODS */);
343}
344
345struct Q_AUTOTEST_EXPORT QScriptItem
346{
347 inline QScriptItem()
348 : position(0),
349 num_glyphs(0), descent(-1), ascent(-1), leading(-1), width(-1),
350 glyph_data_offset(0) {}
351 inline QScriptItem(int p, const QScriptAnalysis &a)
352 : position(p), analysis(a),
353 num_glyphs(0), descent(-1), ascent(-1), leading(-1), width(-1),
354 glyph_data_offset(0) {}
355
356 int position;
357 QScriptAnalysis analysis;
358 unsigned short num_glyphs;
359 QFixed descent;
360 QFixed ascent;
361 QFixed leading;
362 QFixed width;
363 int glyph_data_offset;
364 QFixed height() const { return ascent + descent + 1; }
365};
366
367
368Q_DECLARE_TYPEINFO(QScriptItem, Q_MOVABLE_TYPE);
369
370typedef QVector<QScriptItem> QScriptItemArray;
371
372struct Q_AUTOTEST_EXPORT QScriptLine
373{
374 // created and filled in QTextLine::layout_helper
375 QScriptLine()
376 : from(0), length(0),
377 justified(0), gridfitted(0),
378 hasTrailingSpaces(0), leadingIncluded(0) {}
379 QFixed descent;
380 QFixed ascent;
381 QFixed leading;
382 QFixed x;
383 QFixed y;
384 QFixed width;
385 QFixed textWidth;
386 QFixed textAdvance;
387 int from;
388 signed int length : 29;
389 mutable uint justified : 1;
390 mutable uint gridfitted : 1;
391 uint hasTrailingSpaces : 1;
392 uint leadingIncluded : 1;
393 QFixed height() const { return (ascent + descent).ceil() + 1
394 + (leadingIncluded? qMax(QFixed(),leading) : QFixed()); }
395 QFixed base() const { return ascent
396 + (leadingIncluded ? qMax(QFixed(),leading) : QFixed()); }
397 void setDefaultHeight(QTextEngine *eng);
398 void operator+=(const QScriptLine &other);
399};
400Q_DECLARE_TYPEINFO(QScriptLine, Q_PRIMITIVE_TYPE);
401
402
403inline void QScriptLine::operator+=(const QScriptLine &other)
404{
405 leading= qMax(leading + ascent, other.leading + other.ascent) - qMax(ascent, other.ascent);
406 descent = qMax(descent, other.descent);
407 ascent = qMax(ascent, other.ascent);
408 textWidth += other.textWidth;
409 length += other.length;
410}
411
412typedef QVector<QScriptLine> QScriptLineArray;
413
414class QFontPrivate;
415class QTextFormatCollection;
416
417class Q_GUI_EXPORT QTextEngine {
418public:
419 enum LayoutState {
420 LayoutEmpty,
421 InLayout,
422 LayoutFailed,
423 };
424 struct LayoutData {
425 LayoutData(const QString &str, void **stack_memory, int mem_size);
426 LayoutData();
427 ~LayoutData();
428 mutable QScriptItemArray items;
429 int allocated;
430 int available_glyphs;
431 void **memory;
432 unsigned short *logClustersPtr;
433 QGlyphLayout glyphLayout;
434 mutable int used;
435 uint hasBidi : 1;
436 uint layoutState : 2;
437 uint memory_on_stack : 1;
438 bool haveCharAttributes;
439 QString string;
440 bool reallocate(int totalGlyphs);
441 };
442
443 QTextEngine(LayoutData *data);
444 QTextEngine();
445 QTextEngine(const QString &str, const QFont &f);
446 ~QTextEngine();
447
448 enum Mode {
449 WidthOnly = 0x07
450 };
451
452 // keep in sync with QAbstractFontEngine::TextShapingFlag!!
453 enum ShaperFlag {
454 RightToLeft = 0x0001,
455 DesignMetrics = 0x0002,
456 GlyphIndicesOnly = 0x0004
457 };
458 Q_DECLARE_FLAGS(ShaperFlags, ShaperFlag)
459
460 void invalidate();
461 void clearLineData();
462
463 void validate() const;
464 void itemize() const;
465
466 bool isRightToLeft() const;
467 static void bidiReorder(int numRuns, const quint8 *levels, int *visualOrder);
468
469 const HB_CharAttributes *attributes() const;
470
471 void shape(int item) const;
472
473 void justify(const QScriptLine &si);
474
475 QFixed width(int charFrom, int numChars) const;
476 glyph_metrics_t boundingBox(int from, int len) const;
477 glyph_metrics_t tightBoundingBox(int from, int len) const;
478
479 int length(int item) const {
480 const QScriptItem &si = layoutData->items[item];
481 int from = si.position;
482 item++;
483 return (item < layoutData->items.size() ? layoutData->items[item].position : layoutData->string.length()) - from;
484 }
485 int length(const QScriptItem *si) const {
486 int end;
487 if (si + 1 < layoutData->items.constData()+ layoutData->items.size())
488 end = (si+1)->position;
489 else
490 end = layoutData->string.length();
491 return end - si->position;
492 }
493
494 QFontEngine *fontEngine(const QScriptItem &si, QFixed *ascent = 0, QFixed *descent = 0, QFixed *leading = 0) const;
495 QFont font(const QScriptItem &si) const;
496 inline QFont font() const { return fnt; }
497
498 /**
499 * Returns a pointer to an array of log clusters, offset at the script item.
500 * Each item in the array is a unsigned short. For each character in the original string there is an entry in the table
501 * so there is a one to one correlation in indexes between the original text and the index in the logcluster.
502 * The value of each item is the position in the glyphs array. Multiple similar pointers in the logclusters array imply
503 * that one glyph is used for more than one character.
504 * \sa glyphs()
505 */
506 inline unsigned short *logClusters(const QScriptItem *si) const
507 { return layoutData->logClustersPtr+si->position; }
508 /**
509 * Returns an array of QGlyphLayout items, offset at the script item.
510 * Each item in the array matches one glyph in the text, storing the advance, position etc.
511 * The returned item's length equals to the number of available glyphs. This may be more
512 * than what was actually shaped.
513 * \sa logClusters()
514 */
515 inline QGlyphLayout availableGlyphs(const QScriptItem *si) const {
516 return layoutData->glyphLayout.mid(si->glyph_data_offset);
517 }
518 /**
519 * Returns an array of QGlyphLayout items, offset at the script item.
520 * Each item in the array matches one glyph in the text, storing the advance, position etc.
521 * The returned item's length equals to the number of shaped glyphs.
522 * \sa logClusters()
523 */
524 inline QGlyphLayout shapedGlyphs(const QScriptItem *si) const {
525 return layoutData->glyphLayout.mid(si->glyph_data_offset, si->num_glyphs);
526 }
527
528 inline bool ensureSpace(int nGlyphs) const {
529 if (layoutData->glyphLayout.numGlyphs - layoutData->used < nGlyphs)
530 return layoutData->reallocate((((layoutData->used + nGlyphs)*3/2 + 15) >> 4) << 4);
531 return true;
532 }
533
534 void freeMemory();
535
536 int findItem(int strPos) const;
537 inline QTextFormatCollection *formats() const {
538#ifdef QT_BUILD_COMPAT_LIB
539 return 0; // Compat should never reference this symbol
540#else
541 return block.docHandle()->formatCollection();
542#endif
543 }
544 QTextCharFormat format(const QScriptItem *si) const;
545 inline QAbstractTextDocumentLayout *docLayout() const {
546#ifdef QT_BUILD_COMPAT_LIB
547 return 0; // Compat should never reference this symbol
548#else
549 return block.docHandle()->document()->documentLayout();
550#endif
551 }
552 int formatIndex(const QScriptItem *si) const;
553
554 /// returns the width of tab at index (in the tabs array) with the tab-start at position x
555 QFixed calculateTabWidth(int index, QFixed x) const;
556
557 mutable QScriptLineArray lines;
558
559 QString text;
560 QFont fnt;
561 QTextBlock block;
562
563 QTextOption option;
564
565 QFixed minWidth;
566 QFixed maxWidth;
567 QPointF position;
568 uint ignoreBidi : 1;
569 uint cacheGlyphs : 1;
570 uint stackEngine : 1;
571 uint forceJustification : 1;
572
573 int *underlinePositions;
574
575 mutable LayoutData *layoutData;
576
577 inline bool hasFormats() const { return (block.docHandle() || specialData); }
578
579 struct SpecialData {
580 int preeditPosition;
581 QString preeditText;
582 QList<QTextLayout::FormatRange> addFormats;
583 QVector<int> addFormatIndices;
584 QVector<int> resolvedFormatIndices;
585 };
586 SpecialData *specialData;
587
588 bool atWordSeparator(int position) const;
589 bool atSpace(int position) const;
590 void indexAdditionalFormats();
591
592 QString elidedText(Qt::TextElideMode mode, const QFixed &width, int flags = 0) const;
593
594 void shapeLine(const QScriptLine &line);
595
596private:
597 void setBoundary(int strPos) const;
598 void addRequiredBoundaries() const;
599 void shapeText(int item) const;
600 void shapeTextWithHarfbuzz(int item) const;
601#if defined(Q_WS_WINCE)
602 void shapeTextWithCE(int item) const;
603#endif
604#if defined(Q_WS_MAC)
605 void shapeTextMac(int item) const;
606#endif
607 void splitItem(int item, int pos) const;
608
609 void resolveAdditionalFormats() const;
610};
611
612class QStackTextEngine : public QTextEngine {
613public:
614 enum { MemSize = 256*40/sizeof(void *) };
615 QStackTextEngine(const QString &string, const QFont &f);
616 LayoutData _layoutData;
617 void *_memory[MemSize];
618};
619
620
621Q_DECLARE_OPERATORS_FOR_FLAGS(QTextEngine::ShaperFlags)
622
623QT_END_NAMESPACE
624
625#endif // QTEXTENGINE_P_H
Note: See TracBrowser for help on using the repository browser.