1 /*
2  * DQt - D bindings for the Qt Toolkit
3  *
4  * GNU Lesser General Public License Usage
5  * This file may be used under the terms of the GNU Lesser
6  * General Public License version 3 as published by the Free Software
7  * Foundation and appearing in the file LICENSE.LGPL3 included in the
8  * packaging of this file. Please review the following information to
9  * ensure the GNU Lesser General Public License version 3 requirements
10  * will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
11  */
12 module qt.gui.textlayout;
13 extern(C++):
14 
15 import qt.config;
16 import qt.core.global;
17 import qt.core.list;
18 import qt.core.namespace;
19 import qt.core.point;
20 import qt.core.rect;
21 import qt.core.string;
22 import qt.gui.font;
23 import qt.gui.paintdevice;
24 import qt.gui.painter;
25 import qt.gui.textformat;
26 import qt.gui.textobject;
27 import qt.gui.textoption;
28 import qt.helpers;
29 version(QT_NO_RAWFONT){}else
30 {
31     import qt.gui.glyphrun;
32     import qt.gui.rawfont;
33 }
34 
35 extern(C++, class) struct QTextEngine;
36 /+ #ifndef QT_NO_RAWFONT
37 class QRawFont;
38 #endif +/
39 
40 /// Binding for C++ class [QTextInlineObject](https://doc.qt.io/qt-6/qtextinlineobject.html).
41 extern(C++, class) struct /+ Q_GUI_EXPORT +/ QTextInlineObject
42 {
43 public:
44     this(int i, QTextEngine* e)
45     {
46         this.itm = i;
47         this.eng = e;
48     }
49     @disable this();
50     /+pragma(inline, true) this()
51     {
52         this.itm = 0;
53         this.eng = null;
54     }+/
55     pragma(inline, true) bool isValid() const { return cast(bool)(eng); }
56 
57     QRectF rect() const;
58     qreal width() const;
59     qreal ascent() const;
60     qreal descent() const;
61     qreal height() const;
62 
63     /+ Qt:: +/qt.core.namespace.LayoutDirection textDirection() const;
64 
65     void setWidth(qreal w);
66     void setAscent(qreal a);
67     void setDescent(qreal d);
68 
69     int textPosition() const;
70 
71     int formatIndex() const;
72     QTextFormat format() const;
73 
74 private:
75     /+ friend class QTextLayout; +/
76     int itm = 0;
77     QTextEngine* eng = null;
78     mixin(CREATE_CONVENIENCE_WRAPPERS);
79 }
80 
81 
82 /// Binding for C++ class [QTextLayout](https://doc.qt.io/qt-6/qtextlayout.html).
83 extern(C++, class) struct /+ Q_GUI_EXPORT +/ QTextLayout
84 {
85 public:
86     // does itemization
87     @disable this();
88     pragma(mangle, defaultConstructorMangling(__traits(identifier, typeof(this))))
89     ref typeof(this) rawConstructor();
90     static typeof(this) create()
91     {
92         typeof(this) r = typeof(this).init;
93         r.rawConstructor();
94         return r;
95     }
96 
97     this(ref const(QString) text);
98     mixin(changeWindowsMangling(q{mangleClassesTailConst}, q{
99     this(ref const(QString) text, ref const(QFont) font, const(QPaintDevice) paintdevice = null);
100     }));
101     this(ref const(QTextBlock) b);
102     ~this();
103 
104     void setFont(ref const(QFont) f);
105     QFont font() const;
106 
107     version(QT_NO_RAWFONT){}else
108     {
109         void setRawFont(ref const(QRawFont) rawFont);
110     }
111 
112     void setText(ref const(QString) string);
113     QString text() const;
114 
115     void setTextOption(ref const(QTextOption) option);
116     ref const(QTextOption) textOption() const;
117 
118     void setPreeditArea(int position, ref const(QString) text);
119     int preeditAreaPosition() const;
120     QString preeditAreaText() const;
121 
122     // Workaround for https://issues.dlang.org/show_bug.cgi?id=20701
123     extern(C++, struct) struct FormatRange {
124         int start;
125         int length;
126         QTextCharFormat format;
127 
128         /+ friend bool operator==(const FormatRange &lhs, const FormatRange &rhs)
129         { return lhs.start == rhs.start && lhs.length == rhs.length && lhs.format == rhs.format; } +/
130         /+ friend bool operator!=(const FormatRange &lhs, const FormatRange &rhs)
131         { return !operator==(lhs, rhs); } +/
132     }
133     void setFormats(ref const(QList!(FormatRange)) overrides);
134     QList!(FormatRange) formats() const;
135     void clearFormats();
136 
137     void setCacheEnabled(bool enable);
138     bool cacheEnabled() const;
139 
140     void setCursorMoveStyle(/+ Qt:: +/qt.core.namespace.CursorMoveStyle style);
141     /+ Qt:: +/qt.core.namespace.CursorMoveStyle cursorMoveStyle() const;
142 
143     void beginLayout();
144     void endLayout();
145     void clearLayout();
146 
147     QTextLine createLine();
148 
149     int lineCount() const;
150     QTextLine lineAt(int i) const;
151     QTextLine lineForTextPosition(int pos) const;
152 
153     enum CursorMode {
154         SkipCharacters,
155         SkipWords
156     }
157     bool isValidCursorPosition(int pos) const;
158     int nextCursorPosition(int oldPos, CursorMode mode = CursorMode.SkipCharacters) const;
159     int previousCursorPosition(int oldPos, CursorMode mode = CursorMode.SkipCharacters) const;
160     int leftCursorPosition(int oldPos) const;
161     int rightCursorPosition(int oldPos) const;
162 
163     void draw(QPainter* p, ref const(QPointF) pos,
164                   ref const(QList!(FormatRange)) selections = globalInitVar!(QList!(FormatRange)),
165                   ref const(QRectF) clip = globalInitVar!QRectF) const;
166     void drawCursor(QPainter* p, ref const(QPointF) pos, int cursorPosition) const;
167     void drawCursor(QPainter* p, ref const(QPointF) pos, int cursorPosition, int width) const;
168 
169     QPointF position() const;
170     void setPosition(ref const(QPointF) p);
171 
172     QRectF boundingRect() const;
173 
174     qreal minimumWidth() const;
175     qreal maximumWidth() const;
176 
177     version(QT_NO_RAWFONT){}else
178     {
179         QList!(QGlyphRun) glyphRuns(int from = -1, int length = -1) const;
180     }
181 
182 //    QTextEngine* engine() const { return d; }
183     void setFlags(int flags);
184 private:
185     this(QTextEngine* e)
186     {
187         this.d = e;
188     }
189     /+ Q_DISABLE_COPY(QTextLayout) +/
190 @disable this(this);
191 /+this(ref const(QTextLayout));+//+ref QTextLayout operator =(ref const(QTextLayout));+/
192     /+ friend class QPainter; +/
193     /+ friend class QGraphicsSimpleTextItemPrivate; +/
194     /+ friend class QGraphicsSimpleTextItem; +/
195     /+ friend void qt_format_text(const QFont &font, const QRectF &_r, int tf, const QTextOption *, const QString& str,
196                                QRectF *brect, int tabstops, int* tabarray, int tabarraylen,
197                                QPainter *painter); +/
198     QTextEngine* d;
199     mixin(CREATE_CONVENIENCE_WRAPPERS);
200 }
201 /+ Q_DECLARE_TYPEINFO(QTextLayout::FormatRange, Q_RELOCATABLE_TYPE); +/
202 
203 
204 /// Binding for C++ class [QTextLine](https://doc.qt.io/qt-6/qtextline.html).
205 extern(C++, class) struct /+ Q_GUI_EXPORT +/ QTextLine
206 {
207 public:
208     @disable this();
209     /+pragma(inline, true) this()
210     {
211         this.index = 0;
212         this.eng = null;
213     }+/
214     pragma(inline, true) bool isValid() const { return cast(bool)(eng); }
215 
216     QRectF rect() const;
217     qreal x() const;
218     qreal y() const;
219     qreal width() const;
220     qreal ascent() const;
221     qreal descent() const;
222     qreal height() const;
223     qreal leading() const;
224 
225     void setLeadingIncluded(bool included);
226     bool leadingIncluded() const;
227 
228     qreal naturalTextWidth() const;
229     qreal horizontalAdvance() const;
230     QRectF naturalTextRect() const;
231 
232     enum Edge {
233         Leading,
234         Trailing
235     }
236     enum CursorPosition {
237         CursorBetweenCharacters,
238         CursorOnCharacter
239     }
240 
241     /* cursorPos gets set to the valid position */
242     qreal cursorToX(int* cursorPos, Edge edge = Edge.Leading) const;
243     pragma(inline, true) qreal cursorToX(int cursorPos, Edge edge = Edge.Leading) const { return cursorToX(&cursorPos, edge); }
244     int xToCursor(qreal x, CursorPosition /+ = CursorBetweenCharacters +/) const;
245 
246     void setLineWidth(qreal width);
247     void setNumColumns(int columns);
248     void setNumColumns(int columns, qreal alignmentWidth);
249 
250     void setPosition(ref const(QPointF) pos);
251     QPointF position() const;
252 
253     int textStart() const;
254     int textLength() const;
255 
256     int lineNumber() const { return index; }
257 
258     void draw(QPainter* painter, ref const(QPointF) position) const;
259 
260     version(QT_NO_RAWFONT){}else
261     {
262         QList!(QGlyphRun) glyphRuns(int from = -1, int length = -1) const;
263     }
264 
265 private:
266     this(int line, QTextEngine* e)
267     {
268         this.index = line;
269         this.eng = e;
270     }
271     void layout_helper(int numGlyphs);
272     void draw_internal(QPainter* p, ref const(QPointF) pos,
273                            const(QTextLayout.FormatRange)* selection) const;
274 
275     /+ friend class QTextLayout; +/
276     /+ friend class QTextFragment; +/
277     int index = 0;
278     QTextEngine* eng = null;
279     mixin(CREATE_CONVENIENCE_WRAPPERS);
280 }
281