29
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 *******************************************************************************/
|
|
11 module dwt.graphics.TextLayout;
|
|
12
|
|
13 import dwt.DWT;
|
|
14 import dwt.DWTException;
|
|
15 import dwt.internal.Compatibility;
|
|
16 import dwt.internal.gdip.Gdip;
|
|
17 import dwt.internal.win32.OS;
|
|
18
|
|
19 import dwt.graphics.Color;
|
|
20 import dwt.graphics.Device;
|
|
21 import dwt.graphics.Font;
|
|
22 import dwt.graphics.FontMetrics;
|
|
23 import dwt.graphics.GC;
|
|
24 import dwt.graphics.GCData;
|
|
25 import dwt.graphics.GlyphMetrics;
|
|
26 import dwt.graphics.Point;
|
|
27 import dwt.graphics.Rectangle;
|
|
28 import dwt.graphics.Region;
|
|
29 import dwt.graphics.Resource;
|
|
30 import dwt.graphics.TextStyle;
|
|
31
|
|
32 import tango.text.convert.Format;
|
|
33 import dwt.dwthelper.utils;
|
30
|
34 import dwt.dwthelper.System;
|
29
|
35
|
|
36 /**
|
|
37 * <code>TextLayout</code> is a graphic object that represents
|
|
38 * styled text.
|
|
39 * <p>
|
|
40 * Instances of this class provide support for drawing, cursor
|
|
41 * navigation, hit testing, text wrapping, alignment, tab expansion
|
|
42 * line breaking, etc. These are aspects required for rendering internationalized text.
|
|
43 * </p><p>
|
|
44 * Application code must explicitly invoke the <code>TextLayout#dispose()</code>
|
|
45 * method to release the operating system resources managed by each instance
|
|
46 * when those instances are no longer required.
|
|
47 * </p>
|
|
48 *
|
|
49 * @since 3.0
|
|
50 */
|
|
51 public final class TextLayout : Resource {
|
|
52 Font font;
|
|
53 char[] text, segmentsText;
|
|
54 int lineSpacing;
|
|
55 int ascent, descent;
|
|
56 int alignment;
|
|
57 int wrapWidth;
|
|
58 int orientation;
|
|
59 int indent;
|
|
60 bool justify;
|
|
61 int[] tabs;
|
|
62 int[] segments;
|
|
63 StyleItem[] styles;
|
|
64
|
|
65 StyleItem[] allRuns;
|
|
66 StyleItem[][] runs;
|
|
67 int[] lineOffset, lineY, lineWidth;
|
30
|
68 void* mLangFontLink2;
|
29
|
69
|
|
70 static const wchar LTR_MARK = '\u200E', RTL_MARK = '\u200F';
|
|
71 static const int SCRIPT_VISATTR_SIZEOF = 2;
|
|
72 static const int GOFFSET_SIZEOF = 8;
|
|
73 static byte[16] CLSID_CMultiLanguage;
|
|
74 static byte[16] IID_IMLangFontLink2;
|
|
75 static this() {
|
|
76 OS.IIDFromString("{275c23e2-3747-11d0-9fea-00aa003f8646}\0".toCharArray().ptr, CLSID_CMultiLanguage.ptr);
|
|
77 OS.IIDFromString("{DCCFC162-2B38-11d2-B7EC-00C04F8F5D9A}\0".toCharArray().ptr, IID_IMLangFontLink2.ptr);
|
|
78 }
|
|
79
|
|
80 class StyleItem {
|
|
81 TextStyle style;
|
|
82 int start, length;
|
|
83 bool lineBreak, softBreak, tab;
|
|
84
|
|
85 /*Script cache and analysis */
|
|
86 SCRIPT_ANALYSIS analysis;
|
|
87 SCRIPT_CACHE* psc;
|
|
88
|
|
89 /*Shape info (malloc when the run is shaped) */
|
|
90 WORD* glyphs;
|
|
91 int glyphCount;
|
|
92 WORD* clusters;
|
|
93 SCRIPT_VISATTR* visAttrs;
|
|
94
|
|
95 /*Place info (malloc when the run is placed) */
|
|
96 int* advances;
|
30
|
97 GOFFSET* goffsets;
|
|
98 int width;
|
|
99 int ascent;
|
|
100 int descent;
|
|
101 int leading;
|
|
102 int x;
|
29
|
103
|
|
104 /* Justify info (malloc during computeRuns) */
|
|
105 int* justify;
|
|
106
|
|
107 /* ScriptBreak */
|
|
108 SCRIPT_LOGATTR* psla;
|
|
109
|
30
|
110 HFONT fallbackFont;
|
29
|
111
|
|
112 void free() {
|
|
113 auto hHeap = OS.GetProcessHeap();
|
|
114 if (psc !is null) {
|
|
115 OS.ScriptFreeCache (psc);
|
|
116 OS.HeapFree(hHeap, 0, psc);
|
|
117 psc = null;
|
|
118 }
|
|
119 if (glyphs !is null) {
|
|
120 OS.HeapFree(hHeap, 0, glyphs);
|
|
121 glyphs = null;
|
|
122 glyphCount = 0;
|
|
123 }
|
|
124 if (clusters !is null) {
|
|
125 OS.HeapFree(hHeap, 0, clusters);
|
|
126 clusters = null;
|
|
127 }
|
|
128 if (visAttrs !is null) {
|
|
129 OS.HeapFree(hHeap, 0, visAttrs);
|
|
130 visAttrs = null;
|
|
131 }
|
|
132 if (advances !is null) {
|
|
133 OS.HeapFree(hHeap, 0, advances);
|
|
134 advances = null;
|
|
135 }
|
|
136 if (goffsets !is null) {
|
|
137 OS.HeapFree(hHeap, 0, goffsets);
|
|
138 goffsets = null;
|
|
139 }
|
|
140 if (justify !is null) {
|
|
141 OS.HeapFree(hHeap, 0, justify);
|
|
142 justify = null;
|
|
143 }
|
|
144 if (psla !is null) {
|
|
145 OS.HeapFree(hHeap, 0, psla);
|
|
146 psla = null;
|
|
147 }
|
|
148 if (fallbackFont !is null) {
|
30
|
149 if (mLangFontLink2 !is null) {
|
29
|
150 /* ReleaseFont() */
|
30
|
151 OS.VtblCall(8, mLangFontLink2, cast(int)fallbackFont);
|
29
|
152 }
|
|
153 fallbackFont = null;
|
|
154 }
|
30
|
155 width = 0;
|
|
156 ascent = 0;
|
|
157 descent = 0;
|
|
158 x = 0;
|
29
|
159 lineBreak = softBreak = false;
|
|
160 }
|
|
161 public char[] toString () {
|
|
162 return Format( "StyleItem {{{}, {}}", start, style );
|
|
163 }
|
|
164 }
|
|
165
|
|
166 /**
|
|
167 * Constructs a new instance of this class on the given device.
|
|
168 * <p>
|
|
169 * You must dispose the text layout when it is no longer required.
|
|
170 * </p>
|
|
171 *
|
|
172 * @param device the device on which to allocate the text layout
|
|
173 *
|
|
174 * @exception IllegalArgumentException <ul>
|
|
175 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
176 * </ul>
|
|
177 *
|
|
178 * @see #dispose()
|
|
179 */
|
|
180 public this (Device device) {
|
|
181 if (device is null) device = Device.getDevice();
|
|
182 if (device is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
|
|
183 this.device = device;
|
|
184 wrapWidth = ascent = descent = -1;
|
|
185 lineSpacing = 0;
|
|
186 orientation = DWT.LEFT_TO_RIGHT;
|
|
187 styles = new StyleItem[2];
|
|
188 styles[0] = new StyleItem();
|
|
189 styles[1] = new StyleItem();
|
|
190 text = ""; //$NON-NLS-1$
|
30
|
191 void* ppv;
|
|
192 OS.OleInitialize(null);
|
|
193 if (OS.CoCreateInstance(CLSID_CMultiLanguage.ptr, null, OS.CLSCTX_INPROC_SERVER, IID_IMLangFontLink2.ptr, cast(void*)&ppv) is OS.S_OK) {
|
|
194 mLangFontLink2 = ppv;
|
29
|
195 }
|
|
196 if (device.tracking) device.new_Object(this);
|
|
197 }
|
|
198
|
|
199 void breakRun(StyleItem run) {
|
30
|
200 if (run.psla !is null) return;
|
|
201 wchar[] chars = StrToWCHARs( segmentsText[ run.start .. run.start + run.length ] );
|
|
202 auto hHeap = OS.GetProcessHeap();
|
|
203 run.psla = cast(SCRIPT_LOGATTR*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, SCRIPT_LOGATTR.sizeof * chars.length);
|
|
204 if (run.psla is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
205 OS.ScriptBreak(chars.ptr, chars.length, &run.analysis, run.psla);
|
29
|
206 }
|
|
207
|
30
|
208 void checkItem (HDC hDC, StyleItem item) {
|
|
209 if (item.fallbackFont !is null) {
|
29
|
210 /*
|
|
211 * Feature in Windows. The fallback font returned by the MLang service
|
|
212 * can be disposed by some other client running in the same thread.
|
|
213 * For example, disposing a Browser widget internally releases all fonts
|
|
214 * in the MLang cache. The fix is to use GetObject() to detect if the
|
|
215 * font was disposed and reshape the run.
|
|
216 */
|
|
217 LOGFONT logFont;
|
30
|
218 if (OS.GetObject( item.fallbackFont, LOGFONT.sizeof, &logFont) is 0) {
|
29
|
219 item.free();
|
|
220 OS.SelectObject(hDC, getItemFont(item));
|
|
221 shape(hDC, item);
|
|
222 }
|
|
223 }
|
|
224 }
|
|
225
|
|
226 void checkLayout () {
|
|
227 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
|
|
228 }
|
|
229
|
|
230 /*
|
|
231 * Compute the runs: itemize, shape, place, and reorder the runs.
|
|
232 * Break paragraphs into lines, wraps the text, and initialize caches.
|
|
233 */
|
|
234 void computeRuns (GC gc) {
|
|
235 if (runs !is null) return;
|
30
|
236 auto hDC = gc !is null ? gc.handle : device.internal_new_GC(null);
|
|
237 auto srcHdc = OS.CreateCompatibleDC(hDC);
|
29
|
238 allRuns = itemize();
|
|
239 for (int i=0; i<allRuns.length - 1; i++) {
|
|
240 StyleItem run = allRuns[i];
|
|
241 OS.SelectObject(srcHdc, getItemFont(run));
|
|
242 shape(srcHdc, run);
|
|
243 }
|
30
|
244 SCRIPT_LOGATTR* logAttr;
|
|
245 SCRIPT_PROPERTIES* properties;
|
29
|
246 int lineWidth = indent, lineStart = 0, lineCount = 1;
|
|
247 for (int i=0; i<allRuns.length - 1; i++) {
|
|
248 StyleItem run = allRuns[i];
|
|
249 if (run.length is 1) {
|
|
250 char ch = segmentsText.charAt(run.start);
|
|
251 switch (ch) {
|
|
252 case '\t': {
|
|
253 run.tab = true;
|
|
254 if (tabs is null) break;
|
|
255 int tabsLength = tabs.length, j;
|
|
256 for (j = 0; j < tabsLength; j++) {
|
|
257 if (tabs[j] > lineWidth) {
|
|
258 run.width = tabs[j] - lineWidth;
|
|
259 break;
|
|
260 }
|
|
261 }
|
|
262 if (j is tabsLength) {
|
|
263 int tabX = tabs[tabsLength-1];
|
|
264 int lastTabWidth = tabsLength > 1 ? tabs[tabsLength-1] - tabs[tabsLength-2] : tabs[0];
|
|
265 if (lastTabWidth > 0) {
|
|
266 while (tabX <= lineWidth) tabX += lastTabWidth;
|
|
267 run.width = tabX - lineWidth;
|
|
268 }
|
|
269 }
|
|
270 break;
|
|
271 }
|
|
272 case '\n': {
|
|
273 run.lineBreak = true;
|
|
274 break;
|
|
275 }
|
|
276 case '\r': {
|
|
277 run.lineBreak = true;
|
|
278 StyleItem next = allRuns[i + 1];
|
|
279 if (next.length !is 0 && segmentsText.charAt(next.start) is '\n') {
|
|
280 run.length += 1;
|
|
281 next.free();
|
|
282 i++;
|
|
283 }
|
|
284 break;
|
|
285 }
|
|
286 }
|
|
287 }
|
|
288 if (wrapWidth !is -1 && lineWidth + run.width > wrapWidth && !run.tab) {
|
|
289 int start = 0;
|
|
290 int[] piDx = new int[run.length];
|
|
291 if (run.style !is null && run.style.metrics !is null) {
|
|
292 piDx[0] = run.width;
|
|
293 } else {
|
30
|
294 OS.ScriptGetLogicalWidths(&run.analysis, run.length, run.glyphCount, run.advances, run.clusters, run.visAttrs, piDx.ptr);
|
29
|
295 }
|
|
296 int width = 0, maxWidth = wrapWidth - lineWidth;
|
|
297 while (width + piDx[start] < maxWidth) {
|
|
298 width += piDx[start++];
|
|
299 }
|
|
300 int firstStart = start;
|
|
301 int firstIndice = i;
|
|
302 while (i >= lineStart) {
|
|
303 breakRun(run);
|
|
304 while (start >= 0) {
|
30
|
305 logAttr = run.psla + start;
|
|
306 //OS.MoveMemory(logAttr, run.psla + (start * SCRIPT_LOGATTR.sizeof), SCRIPT_LOGATTR.sizeof);
|
29
|
307 if (logAttr.fSoftBreak || logAttr.fWhiteSpace) break;
|
|
308 start--;
|
|
309 }
|
|
310
|
|
311 /*
|
|
312 * Bug in Windows. For some reason Uniscribe sets the fSoftBreak flag for the first letter
|
|
313 * after a letter with an accent. This cause a break line to be set in the middle of a word.
|
|
314 * The fix is to detect the case and ignore fSoftBreak forcing the algorithm keep searching.
|
|
315 */
|
|
316 if (start is 0 && i !is lineStart && !run.tab) {
|
|
317 if (logAttr.fSoftBreak && !logAttr.fWhiteSpace) {
|
30
|
318 properties = device.scripts[run.analysis.eScript];
|
|
319 //OS.MoveMemory(properties, device.scripts[run.analysis.eScript], SCRIPT_PROPERTIES.sizeof);
|
29
|
320 int langID = properties.langid;
|
|
321 StyleItem pRun = allRuns[i - 1];
|
30
|
322 //OS.MoveMemory(properties, device.scripts[pRun.analysis.eScript], SCRIPT_PROPERTIES.sizeof);
|
29
|
323 if (properties.langid is langID || langID is OS.LANG_NEUTRAL || properties.langid is OS.LANG_NEUTRAL) {
|
|
324 breakRun(pRun);
|
30
|
325 logAttr = pRun.psla + (pRun.length - 1);
|
|
326 //OS.MoveMemory(logAttr, pRun.psla + ((pRun.length - 1) * SCRIPT_LOGATTR.sizeof), SCRIPT_LOGATTR.sizeof);
|
29
|
327 if (!logAttr.fWhiteSpace) start = -1;
|
|
328 }
|
|
329 }
|
|
330 }
|
|
331 if (start >= 0 || i is lineStart) break;
|
|
332 run = allRuns[--i];
|
|
333 start = run.length - 1;
|
|
334 }
|
|
335 if (start is 0 && i !is lineStart && !run.tab) {
|
|
336 run = allRuns[--i];
|
|
337 } else if (start <= 0 && i is lineStart) {
|
|
338 i = firstIndice;
|
|
339 run = allRuns[i];
|
|
340 start = Math.max(1, firstStart);
|
|
341 }
|
|
342 breakRun(run);
|
|
343 while (start < run.length) {
|
30
|
344 logAttr = run.psla + start;
|
|
345 //OS.MoveMemory(logAttr, run.psla + (start * SCRIPT_LOGATTR.sizeof), SCRIPT_LOGATTR.sizeof);
|
29
|
346 if (!logAttr.fWhiteSpace) break;
|
|
347 start++;
|
|
348 }
|
|
349 if (0 < start && start < run.length) {
|
|
350 StyleItem newRun = new StyleItem();
|
|
351 newRun.start = run.start + start;
|
|
352 newRun.length = run.length - start;
|
|
353 newRun.style = run.style;
|
|
354 newRun.analysis = run.analysis;
|
|
355 run.free();
|
|
356 run.length = start;
|
|
357 OS.SelectObject(srcHdc, getItemFont(run));
|
|
358 shape (srcHdc, run);
|
|
359 OS.SelectObject(srcHdc, getItemFont(newRun));
|
|
360 shape (srcHdc, newRun);
|
|
361 StyleItem[] newAllRuns = new StyleItem[allRuns.length + 1];
|
|
362 System.arraycopy(allRuns, 0, newAllRuns, 0, i + 1);
|
|
363 System.arraycopy(allRuns, i + 1, newAllRuns, i + 2, allRuns.length - i - 1);
|
|
364 allRuns = newAllRuns;
|
|
365 allRuns[i + 1] = newRun;
|
|
366 }
|
|
367 if (i !is allRuns.length - 2) {
|
|
368 run.softBreak = run.lineBreak = true;
|
|
369 }
|
|
370 }
|
|
371 lineWidth += run.width;
|
|
372 if (run.lineBreak) {
|
|
373 lineStart = i + 1;
|
|
374 lineWidth = run.softBreak ? 0 : indent;
|
|
375 lineCount++;
|
|
376 }
|
|
377 }
|
|
378 lineWidth = 0;
|
30
|
379 runs = new StyleItem[][](lineCount);
|
29
|
380 lineOffset = new int[lineCount + 1];
|
|
381 lineY = new int[lineCount + 1];
|
|
382 this.lineWidth = new int[lineCount];
|
|
383 int lineRunCount = 0, line = 0;
|
|
384 int ascent = Math.max(0, this.ascent);
|
|
385 int descent = Math.max(0, this.descent);
|
|
386 StyleItem[] lineRuns = new StyleItem[allRuns.length];
|
|
387 for (int i=0; i<allRuns.length; i++) {
|
|
388 StyleItem run = allRuns[i];
|
|
389 lineRuns[lineRunCount++] = run;
|
|
390 lineWidth += run.width;
|
|
391 ascent = Math.max(ascent, run.ascent);
|
|
392 descent = Math.max(descent, run.descent);
|
|
393 if (run.lineBreak || i is allRuns.length - 1) {
|
|
394 /* Update the run metrics if the last run is a hard break. */
|
|
395 if (lineRunCount is 1 && i is allRuns.length - 1) {
|
|
396 TEXTMETRIC lptm;
|
|
397 OS.SelectObject(srcHdc, getItemFont(run));
|
|
398 OS.GetTextMetrics(srcHdc, &lptm);
|
|
399 run.ascent = lptm.tmAscent;
|
|
400 run.descent = lptm.tmDescent;
|
|
401 ascent = Math.max(ascent, run.ascent);
|
|
402 descent = Math.max(descent, run.descent);
|
|
403 }
|
|
404 runs[line] = new StyleItem[lineRunCount];
|
|
405 System.arraycopy(lineRuns, 0, runs[line], 0, lineRunCount);
|
|
406
|
|
407 if (justify && wrapWidth !is -1 && run.softBreak && lineWidth > 0) {
|
|
408 if (line is 0) {
|
|
409 lineWidth += indent;
|
|
410 } else {
|
|
411 StyleItem[] previousLine = runs[line - 1];
|
|
412 StyleItem previousRun = previousLine[previousLine.length - 1];
|
|
413 if (previousRun.lineBreak && !previousRun.softBreak) {
|
|
414 lineWidth += indent;
|
|
415 }
|
|
416 }
|
30
|
417 auto hHeap = OS.GetProcessHeap();
|
29
|
418 int newLineWidth = 0;
|
|
419 for (int j = 0; j < runs[line].length; j++) {
|
|
420 StyleItem item = runs[line][j];
|
|
421 int iDx = item.width * wrapWidth / lineWidth;
|
|
422 if (iDx !is item.width) {
|
30
|
423 item.justify = cast(int*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, item.glyphCount * 4);
|
|
424 if (item.justify is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
29
|
425 OS.ScriptJustify(item.visAttrs, item.advances, item.glyphCount, iDx - item.width, 2, item.justify);
|
|
426 item.width = iDx;
|
|
427 }
|
|
428 newLineWidth += item.width;
|
|
429 }
|
|
430 lineWidth = newLineWidth;
|
|
431 }
|
|
432 this.lineWidth[line] = lineWidth;
|
|
433
|
|
434 StyleItem lastRun = runs[line][lineRunCount - 1];
|
|
435 int lastOffset = lastRun.start + lastRun.length;
|
|
436 runs[line] = reorder(runs[line], i is allRuns.length - 1);
|
|
437 lastRun = runs[line][lineRunCount - 1];
|
|
438 if (run.softBreak && run !is lastRun) {
|
|
439 run.softBreak = run.lineBreak = false;
|
|
440 lastRun.softBreak = lastRun.lineBreak = true;
|
|
441 }
|
|
442
|
|
443 lineWidth = getLineIndent(line);
|
|
444 for (int j = 0; j < runs[line].length; j++) {
|
|
445 runs[line][j].x = lineWidth;
|
|
446 lineWidth += runs[line][j].width;
|
|
447 }
|
|
448 line++;
|
|
449 lineY[line] = lineY[line - 1] + ascent + descent + lineSpacing;
|
|
450 lineOffset[line] = lastOffset;
|
|
451 lineRunCount = lineWidth = 0;
|
|
452 ascent = Math.max(0, this.ascent);
|
|
453 descent = Math.max(0, this.descent);
|
|
454 }
|
|
455 }
|
30
|
456 if (srcHdc !is null) OS.DeleteDC(srcHdc);
|
29
|
457 if (gc is null) device.internal_dispose_GC(hDC, null);
|
|
458 }
|
|
459
|
|
460 /**
|
|
461 * Disposes of the operating system resources associated with
|
|
462 * the text layout. Applications must dispose of all allocated text layouts.
|
|
463 */
|
|
464 public void dispose () {
|
|
465 if (device is null) return;
|
|
466 freeRuns();
|
|
467 font = null;
|
|
468 text = null;
|
|
469 segmentsText = null;
|
|
470 tabs = null;
|
|
471 styles = null;
|
|
472 runs = null;
|
|
473 lineOffset = null;
|
|
474 lineY = null;
|
|
475 lineWidth = null;
|
30
|
476 if (mLangFontLink2 !is null) {
|
29
|
477 /* Release() */
|
|
478 OS.VtblCall(2, mLangFontLink2);
|
30
|
479 mLangFontLink2 = null;
|
29
|
480 }
|
|
481 OS.OleUninitialize();
|
|
482 if (device.tracking) device.dispose_Object(this);
|
|
483 device = null;
|
|
484 }
|
|
485
|
|
486 /**
|
|
487 * Draws the receiver's text using the specified GC at the specified
|
|
488 * point.
|
|
489 *
|
|
490 * @param gc the GC to draw
|
|
491 * @param x the x coordinate of the top left corner of the rectangular area where the text is to be drawn
|
|
492 * @param y the y coordinate of the top left corner of the rectangular area where the text is to be drawn
|
|
493 *
|
|
494 * @exception DWTException <ul>
|
|
495 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
496 * </ul>
|
|
497 * @exception IllegalArgumentException <ul>
|
|
498 * <li>ERROR_NULL_ARGUMENT - if the gc is null</li>
|
|
499 * </ul>
|
|
500 */
|
|
501 public void draw (GC gc, int x, int y) {
|
|
502 draw(gc, x, y, -1, -1, null, null);
|
|
503 }
|
|
504
|
|
505 /**
|
|
506 * Draws the receiver's text using the specified GC at the specified
|
|
507 * point.
|
|
508 *
|
|
509 * @param gc the GC to draw
|
|
510 * @param x the x coordinate of the top left corner of the rectangular area where the text is to be drawn
|
|
511 * @param y the y coordinate of the top left corner of the rectangular area where the text is to be drawn
|
|
512 * @param selectionStart the offset where the selections starts, or -1 indicating no selection
|
|
513 * @param selectionEnd the offset where the selections ends, or -1 indicating no selection
|
|
514 * @param selectionForeground selection foreground, or NULL to use the system default color
|
|
515 * @param selectionBackground selection background, or NULL to use the system default color
|
|
516 *
|
|
517 * @exception DWTException <ul>
|
|
518 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
519 * </ul>
|
|
520 * @exception IllegalArgumentException <ul>
|
|
521 * <li>ERROR_NULL_ARGUMENT - if the gc is null</li>
|
|
522 * </ul>
|
|
523 */
|
|
524 public void draw (GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) {
|
|
525 draw(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground, 0);
|
|
526 }
|
|
527
|
|
528 /**
|
|
529 * Draws the receiver's text using the specified GC at the specified
|
|
530 * point.
|
|
531 * <p>
|
|
532 * The parameter <code>flags</code> can include one of <code>DWT.DELIMITER_SELECTION</code>
|
|
533 * or <code>DWT.FULL_SELECTION</code> to specify the selection behavior on all lines except
|
|
534 * for the last line, and can also include <code>DWT.LAST_LINE_SELECTION</code> to extend
|
|
535 * the specified selection behavior to the last line.
|
|
536 * </p>
|
|
537 * @param gc the GC to draw
|
|
538 * @param x the x coordinate of the top left corner of the rectangular area where the text is to be drawn
|
|
539 * @param y the y coordinate of the top left corner of the rectangular area where the text is to be drawn
|
|
540 * @param selectionStart the offset where the selections starts, or -1 indicating no selection
|
|
541 * @param selectionEnd the offset where the selections ends, or -1 indicating no selection
|
|
542 * @param selectionForeground selection foreground, or NULL to use the system default color
|
|
543 * @param selectionBackground selection background, or NULL to use the system default color
|
|
544 * @param flags drawing options
|
|
545 *
|
|
546 * @exception DWTException <ul>
|
|
547 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
548 * </ul>
|
|
549 * @exception IllegalArgumentException <ul>
|
|
550 * <li>ERROR_NULL_ARGUMENT - if the gc is null</li>
|
|
551 * </ul>
|
|
552 *
|
|
553 * @since 3.3
|
|
554 */
|
|
555 public void draw (GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground, int flags) {
|
|
556 checkLayout();
|
|
557 computeRuns(gc);
|
|
558 if (gc is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
|
|
559 if (gc.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
560 if (selectionForeground !is null && selectionForeground.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
561 if (selectionBackground !is null && selectionBackground.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
30
|
562 int length = text.length;
|
29
|
563 if (length is 0 && flags is 0) return;
|
30
|
564 auto hdc = gc.handle;
|
29
|
565 Rectangle clip = gc.getClipping();
|
|
566 GCData data = gc.data;
|
30
|
567 auto gdipGraphics = data.gdipGraphics;
|
|
568 auto foreground = data.foreground;
|
|
569 auto alpha = data.alpha;
|
|
570 bool gdip = gdipGraphics !is null && (alpha !is 0xFF || data.foregroundPattern !is null);
|
|
571 HRGN clipRgn;
|
29
|
572 float[] lpXform = null;
|
30
|
573 Gdip.Rect gdipRect;
|
|
574 if (gdipGraphics !is null && !gdip) {
|
|
575 auto matrix = Gdip.Matrix_new(1, 0, 0, 1, 0, 0);
|
|
576 if (matrix is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
29
|
577 Gdip.Graphics_GetTransform(gdipGraphics, matrix);
|
30
|
578 auto identity_ = gc.identity();
|
|
579 Gdip.Matrix_Invert(identity_);
|
|
580 Gdip.Matrix_Multiply(matrix, identity_, Gdip.MatrixOrderAppend);
|
|
581 Gdip.Matrix_delete(identity_);
|
29
|
582 if (!Gdip.Matrix_IsIdentity(matrix)) {
|
|
583 lpXform = new float[6];
|
30
|
584 Gdip.Matrix_GetElements(matrix, lpXform.ptr);
|
29
|
585 }
|
|
586 Gdip.Matrix_delete(matrix);
|
|
587 if ((data.style & DWT.MIRRORED) !is 0 && lpXform !is null) {
|
|
588 gdip = true;
|
|
589 lpXform = null;
|
|
590 } else {
|
|
591 Gdip.Graphics_SetPixelOffsetMode(gdipGraphics, Gdip.PixelOffsetModeNone);
|
30
|
592 auto rgn = Gdip.Region_new();
|
29
|
593 Gdip.Graphics_GetClip(gdipGraphics, rgn);
|
|
594 if (!Gdip.Region_IsInfinite(rgn, gdipGraphics)) {
|
|
595 clipRgn = Gdip.Region_GetHRGN(rgn, gdipGraphics);
|
|
596 }
|
|
597 Gdip.Region_delete(rgn);
|
|
598 Gdip.Graphics_SetPixelOffsetMode(gdipGraphics, Gdip.PixelOffsetModeHalf);
|
|
599 hdc = Gdip.Graphics_GetHDC(gdipGraphics);
|
|
600 }
|
|
601 }
|
30
|
602 Gdip.Brush* foregroundBrush;
|
|
603 int state = 0;
|
29
|
604 if (gdip) {
|
|
605 gc.checkGC(GC.FOREGROUND);
|
|
606 foregroundBrush = gc.getFgBrush();
|
|
607 } else {
|
|
608 state = OS.SaveDC(hdc);
|
|
609 if ((data.style & DWT.MIRRORED) !is 0) {
|
|
610 OS.SetLayout(hdc, OS.GetLayout(hdc) | OS.LAYOUT_RTL);
|
|
611 }
|
|
612 if (lpXform !is null) {
|
|
613 OS.SetGraphicsMode(hdc, OS.GM_ADVANCED);
|
30
|
614 OS.SetWorldTransform(hdc, cast(XFORM*)lpXform.ptr);
|
29
|
615 }
|
30
|
616 if (clipRgn !is null) {
|
29
|
617 OS.SelectClipRgn(hdc, clipRgn);
|
|
618 OS.DeleteObject(clipRgn);
|
|
619 }
|
|
620 }
|
|
621 bool hasSelection = selectionStart <= selectionEnd && selectionStart !is -1 && selectionEnd !is -1;
|
|
622 if (hasSelection || (flags & DWT.LAST_LINE_SELECTION) !is 0) {
|
|
623 selectionStart = Math.min(Math.max(0, selectionStart), length - 1);
|
|
624 selectionEnd = Math.min(Math.max(0, selectionEnd), length - 1);
|
|
625 if (selectionForeground is null) selectionForeground = device.getSystemColor(DWT.COLOR_LIST_SELECTION_TEXT);
|
|
626 if (selectionBackground is null) selectionBackground = device.getSystemColor(DWT.COLOR_LIST_SELECTION);
|
|
627 selectionStart = translateOffset(selectionStart);
|
|
628 selectionEnd = translateOffset(selectionEnd);
|
|
629 }
|
30
|
630 RECT rect;
|
|
631 void* selBrush;
|
|
632 void* selPen;
|
|
633 void* selBrushFg;
|
|
634
|
29
|
635 if (hasSelection || (flags & DWT.LAST_LINE_SELECTION) !is 0) {
|
|
636 if (gdip) {
|
30
|
637 auto bg = selectionBackground.handle;
|
|
638 auto argb = ((alpha & 0xFF) << 24) | ((bg >> 16) & 0xFF) | (bg & 0xFF00) | ((bg & 0xFF) << 16);
|
|
639 auto color = Gdip.Color_new(argb);
|
29
|
640 selBrush = Gdip.SolidBrush_new(color);
|
|
641 Gdip.Color_delete(color);
|
30
|
642 auto fg = selectionForeground.handle;
|
29
|
643 argb = ((alpha & 0xFF) << 24) | ((fg >> 16) & 0xFF) | (fg & 0xFF00) | ((fg & 0xFF) << 16);
|
|
644 color = Gdip.Color_new(argb);
|
|
645 selBrushFg = Gdip.SolidBrush_new(color);
|
30
|
646 selPen = Gdip.Pen_new( cast(Gdip.Brush*)selBrushFg, 1);
|
29
|
647 Gdip.Color_delete(color);
|
|
648 } else {
|
|
649 selBrush = OS.CreateSolidBrush(selectionBackground.handle);
|
|
650 selPen = OS.CreatePen(OS.PS_SOLID, 1, selectionForeground.handle);
|
|
651 }
|
|
652 }
|
|
653 int offset = (orientation & DWT.RIGHT_TO_LEFT) !is 0 ? -1 : 0;
|
|
654 OS.SetBkMode(hdc, OS.TRANSPARENT);
|
|
655 for (int line=0; line<runs.length; line++) {
|
|
656 int drawX = x + getLineIndent(line);
|
|
657 int drawY = y + lineY[line];
|
|
658 StyleItem[] lineRuns = runs[line];
|
|
659 int lineHeight = lineY[line+1] - lineY[line];
|
|
660 if (flags !is 0 && (hasSelection || (flags & DWT.LAST_LINE_SELECTION) !is 0)) {
|
|
661 bool extents = false;
|
|
662 if (line is runs.length - 1 && (flags & DWT.LAST_LINE_SELECTION) !is 0) {
|
|
663 extents = true;
|
|
664 } else {
|
|
665 StyleItem run = lineRuns[lineRuns.length - 1];
|
|
666 if (run.lineBreak && !run.softBreak) {
|
|
667 if (selectionStart <= run.start && run.start <= selectionEnd) extents = true;
|
|
668 } else {
|
|
669 int endOffset = run.start + run.length - 1;
|
|
670 if (selectionStart <= endOffset && endOffset < selectionEnd && (flags & DWT.FULL_SELECTION) !is 0) {
|
|
671 extents = true;
|
|
672 }
|
|
673 }
|
|
674 }
|
|
675 if (extents) {
|
|
676 int width;
|
|
677 if ((flags & DWT.FULL_SELECTION) !is 0) {
|
|
678 width = OS.IsWin95 ? 0x7FFF : 0x6FFFFFF;
|
|
679 } else {
|
|
680 width = (lineHeight - lineSpacing) / 3;
|
|
681 }
|
|
682 if (gdip) {
|
30
|
683 Gdip.Graphics_FillRectangle(gdipGraphics, cast(Gdip.Brush*)selBrush, drawX + lineWidth[line], drawY, width, lineHeight - lineSpacing);
|
29
|
684 } else {
|
|
685 OS.SelectObject(hdc, selBrush);
|
|
686 OS.PatBlt(hdc, drawX + lineWidth[line], drawY, width, lineHeight - lineSpacing, OS.PATCOPY);
|
|
687 }
|
|
688 }
|
|
689 }
|
|
690 if (drawX > clip.x + clip.width) continue;
|
|
691 if (drawX + lineWidth[line] < clip.x) continue;
|
|
692 int baseline = Math.max(0, this.ascent);
|
|
693 for (int i = 0; i < lineRuns.length; i++) {
|
|
694 baseline = Math.max(baseline, lineRuns[i].ascent);
|
|
695 }
|
|
696 int alignmentX = drawX;
|
|
697 for (int i = 0; i < lineRuns.length; i++) {
|
|
698 StyleItem run = lineRuns[i];
|
|
699 if (run.length is 0) continue;
|
|
700 if (drawX > clip.x + clip.width) break;
|
|
701 if (drawX + run.width >= clip.x) {
|
|
702 if (!run.lineBreak || run.softBreak) {
|
|
703 int end = run.start + run.length - 1;
|
|
704 bool fullSelection = hasSelection && selectionStart <= run.start && selectionEnd >= end;
|
|
705 if (fullSelection) {
|
|
706 if (gdip) {
|
30
|
707 Gdip.Graphics_FillRectangle(gdipGraphics, cast(Gdip.Brush*)selBrush, drawX, drawY, run.width, lineHeight - lineSpacing);
|
29
|
708 } else {
|
|
709 OS.SelectObject(hdc, selBrush);
|
|
710 OS.PatBlt(hdc, drawX, drawY, run.width, lineHeight - lineSpacing, OS.PATCOPY);
|
|
711 }
|
|
712 } else {
|
|
713 if (run.style !is null && run.style.background !is null) {
|
30
|
714 auto bg = run.style.background.handle;
|
29
|
715 int drawRunY = drawY + (baseline - run.ascent);
|
|
716 if (gdip) {
|
|
717 int argb = ((alpha & 0xFF) << 24) | ((bg >> 16) & 0xFF) | (bg & 0xFF00) | ((bg & 0xFF) << 16);
|
30
|
718 auto color = Gdip.Color_new(argb);
|
|
719 auto brush = Gdip.SolidBrush_new(color);
|
|
720 Gdip.Graphics_FillRectangle(gdipGraphics, cast(Gdip.Brush*)brush, drawX, drawRunY, run.width, run.ascent + run.descent);
|
29
|
721 Gdip.Color_delete(color);
|
|
722 Gdip.SolidBrush_delete(brush);
|
|
723 } else {
|
30
|
724 auto hBrush = OS.CreateSolidBrush (bg);
|
|
725 auto oldBrush = OS.SelectObject(hdc, hBrush);
|
29
|
726 OS.PatBlt(hdc, drawX, drawRunY, run.width, run.ascent + run.descent, OS.PATCOPY);
|
|
727 OS.SelectObject(hdc, oldBrush);
|
|
728 OS.DeleteObject(hBrush);
|
|
729 }
|
|
730 }
|
|
731 bool partialSelection = hasSelection && !(selectionStart > end || run.start > selectionEnd);
|
|
732 if (partialSelection) {
|
|
733 int selStart = Math.max(selectionStart, run.start) - run.start;
|
|
734 int selEnd = Math.min(selectionEnd, end) - run.start;
|
|
735 int cChars = run.length;
|
|
736 int gGlyphs = run.glyphCount;
|
30
|
737 int piX;
|
|
738 int* advances = run.justify !is null ? run.justify : run.advances;
|
|
739 OS.ScriptCPtoX(selStart, false, cChars, gGlyphs, run.clusters, run.visAttrs, advances, &run.analysis, &piX);
|
|
740 int runX = (orientation & DWT.RIGHT_TO_LEFT) !is 0 ? run.width - piX : piX;
|
29
|
741 rect.left = drawX + runX;
|
|
742 rect.top = drawY;
|
30
|
743 OS.ScriptCPtoX(selEnd, true, cChars, gGlyphs, run.clusters, run.visAttrs, advances, &run.analysis, &piX);
|
|
744 runX = (orientation & DWT.RIGHT_TO_LEFT) !is 0 ? run.width - piX : piX;
|
29
|
745 rect.right = drawX + runX;
|
|
746 rect.bottom = drawY + lineHeight - lineSpacing;
|
|
747 if (gdip) {
|
30
|
748 Gdip.Graphics_FillRectangle(gdipGraphics, cast(Gdip.Brush*)selBrush, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
|
29
|
749 } else {
|
|
750 OS.SelectObject(hdc, selBrush);
|
|
751 OS.PatBlt(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, OS.PATCOPY);
|
|
752 }
|
|
753 }
|
|
754 }
|
|
755 }
|
|
756 }
|
|
757 drawX += run.width;
|
|
758 }
|
|
759 drawX = alignmentX;
|
|
760 for (int i = 0; i < lineRuns.length; i++) {
|
|
761 StyleItem run = lineRuns[i];
|
|
762 if (run.length is 0) continue;
|
|
763 if (drawX > clip.x + clip.width) break;
|
|
764 if (drawX + run.width >= clip.x) {
|
|
765 if (!run.tab && (!run.lineBreak || run.softBreak) && !(run.style !is null && run.style.metrics !is null)) {
|
|
766 int end = run.start + run.length - 1;
|
|
767 bool fullSelection = hasSelection && selectionStart <= run.start && selectionEnd >= end;
|
|
768 bool partialSelection = hasSelection && !fullSelection && !(selectionStart > end || run.start > selectionEnd);
|
|
769 checkItem(hdc, run);
|
|
770 OS.SelectObject(hdc, getItemFont(run));
|
|
771 int drawRunY = drawY + (baseline - run.ascent);
|
|
772 if (partialSelection) {
|
|
773 int selStart = Math.max(selectionStart, run.start) - run.start;
|
|
774 int selEnd = Math.min(selectionEnd, end) - run.start;
|
|
775 int cChars = run.length;
|
|
776 int gGlyphs = run.glyphCount;
|
30
|
777 int piX;
|
|
778 int* advances = run.justify !is null ? run.justify : run.advances;
|
|
779 OS.ScriptCPtoX(selStart, false, cChars, gGlyphs, run.clusters, run.visAttrs, advances, &run.analysis, &piX);
|
|
780 int runX = (orientation & DWT.RIGHT_TO_LEFT) !is 0 ? run.width - piX : piX;
|
29
|
781 rect.left = drawX + runX;
|
|
782 rect.top = drawY;
|
30
|
783 OS.ScriptCPtoX(selEnd, true, cChars, gGlyphs, run.clusters, run.visAttrs, advances, &run.analysis, &piX);
|
|
784 runX = (orientation & DWT.RIGHT_TO_LEFT) !is 0 ? run.width - piX : piX;
|
29
|
785 rect.right = drawX + runX;
|
|
786 rect.bottom = drawY + lineHeight;
|
|
787 }
|
|
788 if (gdip) {
|
|
789 OS.BeginPath(hdc);
|
30
|
790 OS.ScriptTextOut(hdc, run.psc, drawX, drawRunY, 0, null, &run.analysis , null, 0, run.glyphs, run.glyphCount, run.advances, run.justify, run.goffsets);
|
29
|
791 OS.EndPath(hdc);
|
|
792 int count = OS.GetPath(hdc, null, null, 0);
|
|
793 int[] points = new int[count*2];
|
30
|
794 ubyte[] types = new ubyte[count];
|
|
795 OS.GetPath(hdc, cast(POINT*)points.ptr, types.ptr, count);
|
29
|
796 for (int typeIndex = 0; typeIndex < types.length; typeIndex++) {
|
|
797 int newType = 0;
|
|
798 int type = types[typeIndex] & 0xFF;
|
|
799 switch (type & ~OS.PT_CLOSEFIGURE) {
|
|
800 case OS.PT_MOVETO: newType = Gdip.PathPointTypeStart; break;
|
|
801 case OS.PT_LINETO: newType = Gdip.PathPointTypeLine; break;
|
|
802 case OS.PT_BEZIERTO: newType = Gdip.PathPointTypeBezier; break;
|
|
803 }
|
|
804 if ((type & OS.PT_CLOSEFIGURE) !is 0) newType |= Gdip.PathPointTypeCloseSubpath;
|
|
805 types[typeIndex] = cast(byte)newType;
|
|
806 }
|
30
|
807 auto path = Gdip.GraphicsPath_new(cast(Gdip.Point*)points.ptr, cast(byte*)types.ptr, count, Gdip.FillModeAlternate);
|
|
808 if (path is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
809 auto brush = foregroundBrush;
|
29
|
810 if (fullSelection) {
|
30
|
811 brush = cast(Gdip.Brush*)selBrushFg;
|
29
|
812 } else {
|
|
813 if (run.style !is null && run.style.foreground !is null) {
|
30
|
814 auto fg = run.style.foreground.handle;
|
29
|
815 int argb = ((alpha & 0xFF) << 24) | ((fg >> 16) & 0xFF) | (fg & 0xFF00) | ((fg & 0xFF) << 16);
|
30
|
816 auto color = Gdip.Color_new(argb);
|
|
817 brush = cast(Gdip.Brush*)Gdip.SolidBrush_new(color);
|
29
|
818 Gdip.Color_delete(color);
|
|
819 }
|
|
820 }
|
|
821 int gstate = 0;
|
|
822 if (partialSelection) {
|
|
823 gdipRect.X = rect.left;
|
|
824 gdipRect.Y = rect.top;
|
|
825 gdipRect.Width = rect.right - rect.left;
|
|
826 gdipRect.Height = rect.bottom - rect.top;
|
|
827 gstate = Gdip.Graphics_Save(gdipGraphics);
|
30
|
828 Gdip.Graphics_SetClip(gdipGraphics, &gdipRect, Gdip.CombineModeExclude);
|
29
|
829 }
|
|
830 int antialias = Gdip.Graphics_GetSmoothingMode(gdipGraphics), textAntialias = 0;
|
|
831 int mode = Gdip.Graphics_GetTextRenderingHint(data.gdipGraphics);
|
|
832 switch (mode) {
|
|
833 case Gdip.TextRenderingHintSystemDefault: textAntialias = Gdip.SmoothingModeAntiAlias; break;
|
|
834 case Gdip.TextRenderingHintSingleBitPerPixel:
|
|
835 case Gdip.TextRenderingHintSingleBitPerPixelGridFit: textAntialias = Gdip.SmoothingModeNone; break;
|
|
836 case Gdip.TextRenderingHintAntiAlias:
|
|
837 case Gdip.TextRenderingHintAntiAliasGridFit:
|
|
838 case Gdip.TextRenderingHintClearTypeGridFit: textAntialias = Gdip.SmoothingModeAntiAlias; break;
|
|
839 }
|
|
840 Gdip.Graphics_SetSmoothingMode(gdipGraphics, textAntialias);
|
|
841 int gstate2 = 0;
|
|
842 if ((data.style & DWT.MIRRORED) !is 0) {
|
|
843 gstate2 = Gdip.Graphics_Save(gdipGraphics);
|
|
844 Gdip.Graphics_ScaleTransform(gdipGraphics, -1, 1, Gdip.MatrixOrderPrepend);
|
|
845 Gdip.Graphics_TranslateTransform(gdipGraphics, -2 * drawX - run.width, 0, Gdip.MatrixOrderPrepend);
|
|
846 }
|
|
847 Gdip.Graphics_FillPath(gdipGraphics, brush, path);
|
|
848 if ((data.style & DWT.MIRRORED) !is 0) {
|
|
849 Gdip.Graphics_Restore(gdipGraphics, gstate2);
|
|
850 }
|
|
851 Gdip.Graphics_SetSmoothingMode(gdipGraphics, antialias);
|
|
852 if (run.style !is null && (run.style.underline || run.style.strikeout)) {
|
30
|
853 auto newPen = hasSelection ? cast(Gdip.Pen*)selPen : Gdip.Pen_new(brush, 1);
|
29
|
854 Gdip.Graphics_SetPixelOffsetMode(gdipGraphics, Gdip.PixelOffsetModeNone);
|
|
855 if (run.style.underline) {
|
|
856 int underlineY = drawY + baseline + 1 - run.style.rise;
|
|
857 Gdip.Graphics_DrawLine(gdipGraphics, newPen, drawX, underlineY, drawX + run.width, underlineY);
|
|
858 }
|
|
859 if (run.style.strikeout) {
|
|
860 int strikeoutY = drawRunY + run.leading + (run.ascent - run.style.rise) / 2;
|
|
861 Gdip.Graphics_DrawLine(gdipGraphics, newPen, drawX, strikeoutY, drawX + run.width, strikeoutY);
|
|
862 }
|
|
863 if (newPen !is selPen) Gdip.Pen_delete(newPen);
|
|
864 Gdip.Graphics_SetPixelOffsetMode(gdipGraphics, Gdip.PixelOffsetModeHalf);
|
|
865 }
|
|
866 if (partialSelection) {
|
|
867 Gdip.Graphics_Restore(gdipGraphics, gstate);
|
|
868 gstate = Gdip.Graphics_Save(gdipGraphics);
|
30
|
869 Gdip.Graphics_SetClip(gdipGraphics, &gdipRect, Gdip.CombineModeIntersect);
|
29
|
870 Gdip.Graphics_SetSmoothingMode(gdipGraphics, textAntialias);
|
30
|
871 Gdip.Graphics_FillPath(gdipGraphics, cast(Gdip.Brush*)selBrushFg, path);
|
29
|
872 Gdip.Graphics_SetSmoothingMode(gdipGraphics, antialias);
|
|
873 if (run.style !is null && (run.style.underline || run.style.strikeout)) {
|
|
874 Gdip.Graphics_SetPixelOffsetMode(gdipGraphics, Gdip.PixelOffsetModeNone);
|
|
875 if (run.style.underline) {
|
|
876 int underlineY = drawY + baseline + 1 - run.style.rise;
|
30
|
877 Gdip.Graphics_DrawLine(gdipGraphics, cast(Gdip.Pen*)selPen, rect.left, underlineY, rect.right, underlineY);
|
29
|
878 }
|
|
879 if (run.style.strikeout) {
|
|
880 int strikeoutY = drawRunY + run.leading + (run.ascent - run.style.rise) / 2;
|
30
|
881 Gdip.Graphics_DrawLine(gdipGraphics, cast(Gdip.Pen*)selPen, rect.left, strikeoutY, rect.right, strikeoutY);
|
29
|
882 }
|
|
883 Gdip.Graphics_SetPixelOffsetMode(gdipGraphics, Gdip.PixelOffsetModeHalf);
|
|
884 }
|
|
885 Gdip.Graphics_Restore(gdipGraphics, gstate);
|
|
886 }
|
|
887 Gdip.GraphicsPath_delete(path);
|
30
|
888 if (brush !is selBrushFg && brush !is foregroundBrush) Gdip.SolidBrush_delete(cast(Gdip.SolidBrush*)brush);
|
29
|
889 } else {
|
|
890 int fg = foreground;
|
|
891 if (fullSelection) {
|
|
892 fg = selectionForeground.handle;
|
|
893 } else {
|
|
894 if (run.style !is null && run.style.foreground !is null) fg = run.style.foreground.handle;
|
|
895 }
|
|
896 OS.SetTextColor(hdc, fg);
|
30
|
897 OS.ScriptTextOut(hdc, run.psc, drawX + offset, drawRunY, 0, null, &run.analysis , null, 0, run.glyphs, run.glyphCount, run.advances, run.justify, run.goffsets);
|
29
|
898 if (run.style !is null && (run.style.underline || run.style.strikeout)) {
|
30
|
899 auto newPen = hasSelection && fg is selectionForeground.handle ? cast(HPEN)selPen : OS.CreatePen(OS.PS_SOLID, 1, fg);
|
|
900 auto oldPen = OS.SelectObject(hdc, newPen);
|
29
|
901 if (run.style.underline) {
|
|
902 int underlineY = drawY + baseline + 1 - run.style.rise;
|
30
|
903 OS.MoveToEx(hdc, drawX, underlineY, null);
|
29
|
904 OS.LineTo(hdc, drawX + run.width, underlineY);
|
|
905 }
|
|
906 if (run.style.strikeout) {
|
|
907 int strikeoutY = drawRunY + run.leading + (run.ascent - run.style.rise) / 2;
|
30
|
908 OS.MoveToEx(hdc, drawX, strikeoutY, null);
|
29
|
909 OS.LineTo(hdc, drawX + run.width, strikeoutY);
|
|
910 }
|
|
911 OS.SelectObject(hdc, oldPen);
|
|
912 if (!hasSelection || fg !is selectionForeground.handle) OS.DeleteObject(newPen);
|
|
913 }
|
|
914 if (partialSelection && fg !is selectionForeground.handle) {
|
|
915 OS.SetTextColor(hdc, selectionForeground.handle);
|
30
|
916 OS.ScriptTextOut(hdc, run.psc, drawX + offset, drawRunY, OS.ETO_CLIPPED, &rect, &run.analysis , null, 0, run.glyphs, run.glyphCount, run.advances, run.justify, run.goffsets);
|
29
|
917 if (run.style !is null && (run.style.underline || run.style.strikeout)) {
|
30
|
918 auto oldPen = OS.SelectObject(hdc, selPen);
|
29
|
919 if (run.style.underline) {
|
|
920 int underlineY = drawY + baseline + 1 - run.style.rise;
|
30
|
921 OS.MoveToEx(hdc, rect.left, underlineY, null);
|
29
|
922 OS.LineTo(hdc, rect.right, underlineY);
|
|
923 }
|
|
924 if (run.style.strikeout) {
|
|
925 int strikeoutY = drawRunY + run.leading + (run.ascent - run.style.rise) / 2;
|
30
|
926 OS.MoveToEx(hdc, rect.left, strikeoutY, null);
|
29
|
927 OS.LineTo(hdc, rect.right, strikeoutY);
|
|
928 }
|
|
929 OS.SelectObject(hdc, oldPen);
|
|
930 }
|
|
931 }
|
|
932 }
|
|
933 }
|
|
934 }
|
|
935 drawX += run.width;
|
|
936 }
|
|
937 }
|
|
938 if (gdip) {
|
30
|
939 if (selBrush !is null) Gdip.SolidBrush_delete(cast(Gdip.SolidBrush*)selBrush);
|
|
940 if (selBrushFg !is null) Gdip.SolidBrush_delete(cast(Gdip.SolidBrush*)selBrushFg);
|
|
941 if (selPen !is null) Gdip.Pen_delete(cast(Gdip.Pen*)selPen);
|
29
|
942 } else {
|
|
943 OS.RestoreDC(hdc, state);
|
30
|
944 if (gdipGraphics !is null) Gdip.Graphics_ReleaseHDC(gdipGraphics, hdc);
|
|
945 if (selBrush !is null) OS.DeleteObject (selBrush);
|
|
946 if (selPen !is null) OS.DeleteObject (selPen);
|
29
|
947 }
|
|
948 }
|
|
949
|
|
950 void freeRuns () {
|
|
951 if (allRuns is null) return;
|
|
952 for (int i=0; i<allRuns.length; i++) {
|
|
953 StyleItem run = allRuns[i];
|
|
954 run.free();
|
|
955 }
|
|
956 allRuns = null;
|
|
957 runs = null;
|
|
958 segmentsText = null;
|
|
959 }
|
|
960
|
|
961 /**
|
|
962 * Returns the receiver's horizontal text alignment, which will be one
|
|
963 * of <code>DWT.LEFT</code>, <code>DWT.CENTER</code> or
|
|
964 * <code>DWT.RIGHT</code>.
|
|
965 *
|
|
966 * @return the alignment used to positioned text horizontally
|
|
967 *
|
|
968 * @exception DWTException <ul>
|
|
969 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
970 * </ul>
|
|
971 */
|
|
972 public int getAlignment () {
|
|
973 checkLayout();
|
|
974 return alignment;
|
|
975 }
|
|
976
|
|
977 /**
|
|
978 * Returns the ascent of the receiver.
|
|
979 *
|
|
980 * @return the ascent
|
|
981 *
|
|
982 * @exception DWTException <ul>
|
|
983 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
984 * </ul>
|
|
985 *
|
|
986 * @see #getDescent()
|
|
987 * @see #setDescent(int)
|
|
988 * @see #setAscent(int)
|
|
989 * @see #getLineMetrics(int)
|
|
990 */
|
|
991 public int getAscent () {
|
|
992 checkLayout();
|
|
993 return ascent;
|
|
994 }
|
|
995
|
|
996 /**
|
|
997 * Returns the bounds of the receiver.
|
|
998 *
|
|
999 * @return the bounds of the receiver
|
|
1000 *
|
|
1001 * @exception DWTException <ul>
|
|
1002 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1003 * </ul>
|
|
1004 */
|
|
1005 public Rectangle getBounds () {
|
|
1006 checkLayout();
|
|
1007 computeRuns(null);
|
|
1008 int width = 0;
|
|
1009 if (wrapWidth !is -1) {
|
|
1010 width = wrapWidth;
|
|
1011 } else {
|
|
1012 for (int line=0; line<runs.length; line++) {
|
|
1013 width = Math.max(width, lineWidth[line] + getLineIndent(line));
|
|
1014 }
|
|
1015 }
|
|
1016 return new Rectangle (0, 0, width, lineY[lineY.length - 1]);
|
|
1017 }
|
|
1018
|
|
1019 /**
|
|
1020 * Returns the bounds for the specified range of characters. The
|
|
1021 * bounds is the smallest rectangle that encompasses all characters
|
|
1022 * in the range. The start and end offsets are inclusive and will be
|
|
1023 * clamped if out of range.
|
|
1024 *
|
|
1025 * @param start the start offset
|
|
1026 * @param end the end offset
|
|
1027 * @return the bounds of the character range
|
|
1028 *
|
|
1029 * @exception DWTException <ul>
|
|
1030 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1031 * </ul>
|
|
1032 */
|
|
1033 public Rectangle getBounds (int start, int end) {
|
|
1034 checkLayout();
|
|
1035 computeRuns(null);
|
30
|
1036 int length = text.length;
|
29
|
1037 if (length is 0) return new Rectangle(0, 0, 0, 0);
|
|
1038 if (start > end) return new Rectangle(0, 0, 0, 0);
|
|
1039 start = Math.min(Math.max(0, start), length - 1);
|
|
1040 end = Math.min(Math.max(0, end), length - 1);
|
|
1041 start = translateOffset(start);
|
|
1042 end = translateOffset(end);
|
|
1043 int left = 0x7fffffff, right = 0;
|
|
1044 int top = 0x7fffffff, bottom = 0;
|
|
1045 bool isRTL = (orientation & DWT.RIGHT_TO_LEFT) !is 0;
|
|
1046 for (int i = 0; i < allRuns.length - 1; i++) {
|
|
1047 StyleItem run = allRuns[i];
|
|
1048 int runEnd = run.start + run.length;
|
|
1049 if (runEnd <= start) continue;
|
|
1050 if (run.start > end) break;
|
|
1051 int runLead = run.x;
|
|
1052 int runTrail = run.x + run.width;
|
|
1053 if (run.start <= start && start < runEnd) {
|
|
1054 int cx = 0;
|
|
1055 if (run.style !is null && run.style.metrics !is null) {
|
|
1056 GlyphMetrics metrics = run.style.metrics;
|
|
1057 cx = metrics.width * (start - run.start);
|
|
1058 } else if (!run.tab) {
|
30
|
1059 int piX;
|
|
1060 int* advances = run.justify !is null ? run.justify : run.advances;
|
|
1061 OS.ScriptCPtoX(start - run.start, false, run.length, run.glyphCount, run.clusters, run.visAttrs, advances, &run.analysis, &piX);
|
|
1062 cx = isRTL ? run.width - piX : piX;
|
29
|
1063 }
|
|
1064 if (run.analysis.fRTL ^ isRTL) {
|
|
1065 runTrail = run.x + cx;
|
|
1066 } else {
|
|
1067 runLead = run.x + cx;
|
|
1068 }
|
|
1069 }
|
|
1070 if (run.start <= end && end < runEnd) {
|
|
1071 int cx = run.width;
|
|
1072 if (run.style !is null && run.style.metrics !is null) {
|
|
1073 GlyphMetrics metrics = run.style.metrics;
|
|
1074 cx = metrics.width * (end - run.start + 1);
|
|
1075 } else if (!run.tab) {
|
30
|
1076 int piX;
|
|
1077 int* advances = run.justify !is null ? run.justify : run.advances;
|
|
1078 OS.ScriptCPtoX(end - run.start, true, run.length, run.glyphCount, run.clusters, run.visAttrs, advances, &run.analysis, &piX);
|
|
1079 cx = isRTL ? run.width - piX : piX;
|
29
|
1080 }
|
|
1081 if (run.analysis.fRTL ^ isRTL) {
|
|
1082 runLead = run.x + cx;
|
|
1083 } else {
|
|
1084 runTrail = run.x + cx;
|
|
1085 }
|
|
1086 }
|
|
1087 int lineIndex = 0;
|
|
1088 while (lineIndex < runs.length && lineOffset[lineIndex + 1] <= run.start) {
|
|
1089 lineIndex++;
|
|
1090 }
|
|
1091 left = Math.min(left, runLead);
|
|
1092 right = Math.max(right, runTrail);
|
|
1093 top = Math.min(top, lineY[lineIndex]);
|
|
1094 bottom = Math.max(bottom, lineY[lineIndex + 1] - lineSpacing);
|
|
1095 }
|
|
1096 return new Rectangle(left, top, right - left, bottom - top);
|
|
1097 }
|
|
1098
|
|
1099 /**
|
|
1100 * Returns the descent of the receiver.
|
|
1101 *
|
|
1102 * @return the descent
|
|
1103 *
|
|
1104 * @exception DWTException <ul>
|
|
1105 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1106 * </ul>
|
|
1107 *
|
|
1108 * @see #getAscent()
|
|
1109 * @see #setAscent(int)
|
|
1110 * @see #setDescent(int)
|
|
1111 * @see #getLineMetrics(int)
|
|
1112 */
|
|
1113 public int getDescent () {
|
|
1114 checkLayout();
|
|
1115 return descent;
|
|
1116 }
|
|
1117
|
|
1118 /**
|
|
1119 * Returns the default font currently being used by the receiver
|
|
1120 * to draw and measure text.
|
|
1121 *
|
|
1122 * @return the receiver's font
|
|
1123 *
|
|
1124 * @exception DWTException <ul>
|
|
1125 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1126 * </ul>
|
|
1127 */
|
|
1128 public Font getFont () {
|
|
1129 checkLayout();
|
|
1130 return font;
|
|
1131 }
|
|
1132
|
|
1133 /**
|
|
1134 * Returns the receiver's indent.
|
|
1135 *
|
|
1136 * @return the receiver's indent
|
|
1137 *
|
|
1138 * @exception DWTException <ul>
|
|
1139 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1140 * </ul>
|
|
1141 *
|
|
1142 * @since 3.2
|
|
1143 */
|
|
1144 public int getIndent () {
|
|
1145 checkLayout();
|
|
1146 return indent;
|
|
1147 }
|
|
1148
|
|
1149 /**
|
|
1150 * Returns the receiver's justification.
|
|
1151 *
|
|
1152 * @return the receiver's justification
|
|
1153 *
|
|
1154 * @exception DWTException <ul>
|
|
1155 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1156 * </ul>
|
|
1157 *
|
|
1158 * @since 3.2
|
|
1159 */
|
|
1160 public bool getJustify () {
|
|
1161 checkLayout();
|
|
1162 return justify;
|
|
1163 }
|
|
1164
|
30
|
1165 HFONT getItemFont (StyleItem item) {
|
|
1166 if (item.fallbackFont !is null) return cast(HFONT) item.fallbackFont;
|
29
|
1167 if (item.style !is null && item.style.font !is null) {
|
|
1168 return item.style.font.handle;
|
|
1169 }
|
|
1170 if (this.font !is null) {
|
|
1171 return this.font.handle;
|
|
1172 }
|
|
1173 return device.systemFont;
|
|
1174 }
|
|
1175
|
|
1176 /**
|
|
1177 * Returns the embedding level for the specified character offset. The
|
|
1178 * embedding level is usually used to determine the directionality of a
|
|
1179 * character in bidirectional text.
|
|
1180 *
|
|
1181 * @param offset the character offset
|
|
1182 * @return the embedding level
|
|
1183 *
|
|
1184 * @exception IllegalArgumentException <ul>
|
|
1185 * <li>ERROR_INVALID_ARGUMENT - if the character offset is out of range</li>
|
|
1186 * </ul>
|
|
1187 * @exception DWTException <ul>
|
|
1188 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1189 */
|
|
1190 public int getLevel (int offset) {
|
|
1191 checkLayout();
|
|
1192 computeRuns(null);
|
30
|
1193 int length = text.length;
|
29
|
1194 if (!(0 <= offset && offset <= length)) DWT.error(DWT.ERROR_INVALID_RANGE);
|
|
1195 offset = translateOffset(offset);
|
|
1196 for (int i=1; i<allRuns.length; i++) {
|
|
1197 if (allRuns[i].start > offset) {
|
|
1198 return allRuns[i - 1].analysis.s.uBidiLevel;
|
|
1199 }
|
|
1200 }
|
|
1201 return (orientation & DWT.RIGHT_TO_LEFT) !is 0 ? 1 : 0;
|
|
1202 }
|
|
1203
|
|
1204 /**
|
|
1205 * Returns the bounds of the line for the specified line index.
|
|
1206 *
|
|
1207 * @param lineIndex the line index
|
|
1208 * @return the line bounds
|
|
1209 *
|
|
1210 * @exception IllegalArgumentException <ul>
|
|
1211 * <li>ERROR_INVALID_ARGUMENT - if the line index is out of range</li>
|
|
1212 * </ul>
|
|
1213 * @exception DWTException <ul>
|
|
1214 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1215 * </ul>
|
|
1216 */
|
|
1217 public Rectangle getLineBounds(int lineIndex) {
|
|
1218 checkLayout();
|
|
1219 computeRuns(null);
|
|
1220 if (!(0 <= lineIndex && lineIndex < runs.length)) DWT.error(DWT.ERROR_INVALID_RANGE);
|
|
1221 int x = getLineIndent(lineIndex);
|
|
1222 int y = lineY[lineIndex];
|
|
1223 int width = lineWidth[lineIndex];
|
|
1224 int height = lineY[lineIndex + 1] - y - lineSpacing;
|
|
1225 return new Rectangle (x, y, width, height);
|
|
1226 }
|
|
1227
|
|
1228 /**
|
|
1229 * Returns the receiver's line count. This includes lines caused
|
|
1230 * by wrapping.
|
|
1231 *
|
|
1232 * @return the line count
|
|
1233 *
|
|
1234 * @exception DWTException <ul>
|
|
1235 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1236 * </ul>
|
|
1237 */
|
|
1238 public int getLineCount () {
|
|
1239 checkLayout();
|
|
1240 computeRuns(null);
|
|
1241 return runs.length;
|
|
1242 }
|
|
1243
|
|
1244 int getLineIndent (int lineIndex) {
|
|
1245 int lineIndent = 0;
|
|
1246 if (lineIndex is 0) {
|
|
1247 lineIndent = indent;
|
|
1248 } else {
|
|
1249 StyleItem[] previousLine = runs[lineIndex - 1];
|
|
1250 StyleItem previousRun = previousLine[previousLine.length - 1];
|
|
1251 if (previousRun.lineBreak && !previousRun.softBreak) {
|
|
1252 lineIndent = indent;
|
|
1253 }
|
|
1254 }
|
|
1255 if (wrapWidth !is -1) {
|
|
1256 bool partialLine = true;
|
|
1257 if (justify) {
|
|
1258 StyleItem[] lineRun = runs[lineIndex];
|
|
1259 if (lineRun[lineRun.length - 1].softBreak) {
|
|
1260 partialLine = false;
|
|
1261 }
|
|
1262 }
|
|
1263 if (partialLine) {
|
|
1264 int lineWidth = this.lineWidth[lineIndex] + lineIndent;
|
|
1265 switch (alignment) {
|
|
1266 case DWT.CENTER: lineIndent += (wrapWidth - lineWidth) / 2; break;
|
|
1267 case DWT.RIGHT: lineIndent += wrapWidth - lineWidth; break;
|
|
1268 }
|
|
1269 }
|
|
1270 }
|
|
1271 return lineIndent;
|
|
1272 }
|
|
1273
|
|
1274 /**
|
|
1275 * Returns the index of the line that contains the specified
|
|
1276 * character offset.
|
|
1277 *
|
|
1278 * @param offset the character offset
|
|
1279 * @return the line index
|
|
1280 *
|
|
1281 * @exception IllegalArgumentException <ul>
|
|
1282 * <li>ERROR_INVALID_ARGUMENT - if the character offset is out of range</li>
|
|
1283 * </ul>
|
|
1284 * @exception DWTException <ul>
|
|
1285 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1286 * </ul>
|
|
1287 */
|
|
1288 public int getLineIndex (int offset) {
|
|
1289 checkLayout();
|
|
1290 computeRuns(null);
|
30
|
1291 int length = text.length;
|
29
|
1292 if (!(0 <= offset && offset <= length)) DWT.error(DWT.ERROR_INVALID_RANGE);
|
|
1293 offset = translateOffset(offset);
|
|
1294 for (int line=0; line<runs.length; line++) {
|
|
1295 if (lineOffset[line + 1] > offset) {
|
|
1296 return line;
|
|
1297 }
|
|
1298 }
|
|
1299 return runs.length - 1;
|
|
1300 }
|
|
1301
|
|
1302 /**
|
|
1303 * Returns the font metrics for the specified line index.
|
|
1304 *
|
|
1305 * @param lineIndex the line index
|
|
1306 * @return the font metrics
|
|
1307 *
|
|
1308 * @exception IllegalArgumentException <ul>
|
|
1309 * <li>ERROR_INVALID_ARGUMENT - if the line index is out of range</li>
|
|
1310 * </ul>
|
|
1311 * @exception DWTException <ul>
|
|
1312 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1313 * </ul>
|
|
1314 */
|
|
1315 public FontMetrics getLineMetrics (int lineIndex) {
|
|
1316 checkLayout();
|
|
1317 computeRuns(null);
|
|
1318 if (!(0 <= lineIndex && lineIndex < runs.length)) DWT.error(DWT.ERROR_INVALID_RANGE);
|
30
|
1319 auto hDC = device.internal_new_GC(null);
|
|
1320 auto srcHdc = OS.CreateCompatibleDC(hDC);
|
29
|
1321 TEXTMETRIC lptm;
|
|
1322 OS.SelectObject(srcHdc, font !is null ? font.handle : device.systemFont);
|
|
1323 OS.GetTextMetrics(srcHdc, &lptm);
|
|
1324 OS.DeleteDC(srcHdc);
|
|
1325 device.internal_dispose_GC(hDC, null);
|
|
1326
|
|
1327 int ascent = Math.max(lptm.tmAscent, this.ascent);
|
|
1328 int descent = Math.max(lptm.tmDescent, this.descent);
|
|
1329 int leading = lptm.tmInternalLeading;
|
30
|
1330 if (text.length !is 0) {
|
29
|
1331 StyleItem[] lineRuns = runs[lineIndex];
|
|
1332 for (int i = 0; i<lineRuns.length; i++) {
|
|
1333 StyleItem run = lineRuns[i];
|
|
1334 if (run.ascent > ascent) {
|
|
1335 ascent = run.ascent;
|
|
1336 leading = run.leading;
|
|
1337 }
|
|
1338 descent = Math.max(descent, run.descent);
|
|
1339 }
|
|
1340 }
|
|
1341 lptm.tmAscent = ascent;
|
|
1342 lptm.tmDescent = descent;
|
|
1343 lptm.tmHeight = ascent + descent;
|
|
1344 lptm.tmInternalLeading = leading;
|
|
1345 lptm.tmAveCharWidth = 0;
|
30
|
1346 return FontMetrics.win32_new(&lptm);
|
29
|
1347 }
|
|
1348
|
|
1349 /**
|
|
1350 * Returns the line offsets. Each value in the array is the
|
|
1351 * offset for the first character in a line except for the last
|
|
1352 * value, which contains the length of the text.
|
|
1353 *
|
|
1354 * @return the line offsets
|
|
1355 *
|
|
1356 * @exception DWTException <ul>
|
|
1357 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1358 * </ul>
|
|
1359 */
|
|
1360 public int[] getLineOffsets () {
|
|
1361 checkLayout();
|
|
1362 computeRuns(null);
|
|
1363 int[] offsets = new int[lineOffset.length];
|
|
1364 for (int i = 0; i < offsets.length; i++) {
|
|
1365 offsets[i] = untranslateOffset(lineOffset[i]);
|
|
1366 }
|
|
1367 return offsets;
|
|
1368 }
|
|
1369
|
|
1370 /**
|
|
1371 * Returns the location for the specified character offset. The
|
|
1372 * <code>trailing</code> argument indicates whether the offset
|
|
1373 * corresponds to the leading or trailing edge of the cluster.
|
|
1374 *
|
|
1375 * @param offset the character offset
|
|
1376 * @param trailing the trailing flag
|
|
1377 * @return the location of the character offset
|
|
1378 *
|
|
1379 * @exception DWTException <ul>
|
|
1380 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1381 * </ul>
|
|
1382 *
|
|
1383 * @see #getOffset(Point, int[])
|
|
1384 * @see #getOffset(int, int, int[])
|
|
1385 */
|
|
1386 public Point getLocation (int offset, bool trailing) {
|
|
1387 checkLayout();
|
|
1388 computeRuns(null);
|
30
|
1389 int length = text.length;
|
29
|
1390 if (!(0 <= offset && offset <= length)) DWT.error(DWT.ERROR_INVALID_RANGE);
|
30
|
1391 length = segmentsText.length;
|
29
|
1392 offset = translateOffset(offset);
|
|
1393 int line;
|
|
1394 for (line=0; line<runs.length; line++) {
|
|
1395 if (lineOffset[line + 1] > offset) break;
|
|
1396 }
|
|
1397 line = Math.min(line, runs.length - 1);
|
|
1398 StyleItem[] lineRuns = runs[line];
|
|
1399 Point result = null;
|
|
1400 if (offset is length) {
|
|
1401 result = new Point(lineWidth[line], lineY[line]);
|
|
1402 } else {
|
|
1403 int width = 0;
|
|
1404 for (int i=0; i<lineRuns.length; i++) {
|
|
1405 StyleItem run = lineRuns[i];
|
|
1406 int end = run.start + run.length;
|
|
1407 if (run.start <= offset && offset < end) {
|
|
1408 if (run.style !is null && run.style.metrics !is null) {
|
|
1409 GlyphMetrics metrics = run.style.metrics;
|
|
1410 width += metrics.width * (offset - run.start + (trailing ? 1 : 0));
|
|
1411 result = new Point(width, lineY[line]);
|
|
1412 } else if (run.tab) {
|
|
1413 if (trailing || (offset is length)) width += run.width;
|
|
1414 result = new Point(width, lineY[line]);
|
|
1415 } else {
|
|
1416 int runOffset = offset - run.start;
|
|
1417 int cChars = run.length;
|
|
1418 int gGlyphs = run.glyphCount;
|
30
|
1419 int piX;
|
|
1420 int* advances = run.justify !is null ? run.justify : run.advances;
|
|
1421 OS.ScriptCPtoX(runOffset, trailing, cChars, gGlyphs, run.clusters, run.visAttrs, advances, &run.analysis, &piX);
|
29
|
1422 if ((orientation & DWT.RIGHT_TO_LEFT) !is 0) {
|
30
|
1423 result = new Point(width + (run.width - piX), lineY[line]);
|
29
|
1424 } else {
|
30
|
1425 result = new Point(width + piX, lineY[line]);
|
29
|
1426 }
|
|
1427 }
|
|
1428 break;
|
|
1429 }
|
|
1430 width += run.width;
|
|
1431 }
|
|
1432 }
|
|
1433 if (result is null) result = new Point(0, 0);
|
|
1434 result.x += getLineIndent(line);
|
|
1435 return result;
|
|
1436 }
|
|
1437
|
|
1438 /**
|
|
1439 * Returns the next offset for the specified offset and movement
|
|
1440 * type. The movement is one of <code>DWT.MOVEMENT_CHAR</code>,
|
|
1441 * <code>DWT.MOVEMENT_CLUSTER</code>, <code>DWT.MOVEMENT_WORD</code>,
|
|
1442 * <code>DWT.MOVEMENT_WORD_END</code> or <code>DWT.MOVEMENT_WORD_START</code>.
|
|
1443 *
|
|
1444 * @param offset the start offset
|
|
1445 * @param movement the movement type
|
|
1446 * @return the next offset
|
|
1447 *
|
|
1448 * @exception IllegalArgumentException <ul>
|
|
1449 * <li>ERROR_INVALID_ARGUMENT - if the offset is out of range</li>
|
|
1450 * </ul>
|
|
1451 * @exception DWTException <ul>
|
|
1452 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1453 * </ul>
|
|
1454 *
|
|
1455 * @see #getPreviousOffset(int, int)
|
|
1456 */
|
|
1457 public int getNextOffset (int offset, int movement) {
|
|
1458 checkLayout();
|
|
1459 return _getOffset (offset, movement, true);
|
|
1460 }
|
|
1461
|
|
1462 int _getOffset(int offset, int movement, bool forward) {
|
|
1463 computeRuns(null);
|
30
|
1464 int length = text.length;
|
29
|
1465 if (!(0 <= offset && offset <= length)) DWT.error(DWT.ERROR_INVALID_RANGE);
|
|
1466 if (forward && offset is length) return length;
|
|
1467 if (!forward && offset is 0) return 0;
|
|
1468 int step = forward ? 1 : -1;
|
|
1469 if ((movement & DWT.MOVEMENT_CHAR) !is 0) return offset + step;
|
30
|
1470 length = segmentsText.length;
|
29
|
1471 offset = translateOffset(offset);
|
30
|
1472 SCRIPT_LOGATTR* logAttr;
|
|
1473 SCRIPT_PROPERTIES* properties;
|
29
|
1474 int i = forward ? 0 : allRuns.length - 1;
|
|
1475 offset = validadeOffset(offset, step);
|
|
1476 do {
|
|
1477 StyleItem run = allRuns[i];
|
|
1478 if (run.start <= offset && offset < run.start + run.length) {
|
|
1479 if (run.lineBreak && !run.softBreak) return untranslateOffset(run.start);
|
|
1480 if (run.tab) return untranslateOffset(run.start);
|
30
|
1481 properties = device.scripts[run.analysis.eScript];
|
29
|
1482 bool isComplex = properties.fNeedsCaretInfo || properties.fNeedsWordBreaking;
|
|
1483 if (isComplex) breakRun(run);
|
|
1484 while (run.start <= offset && offset < run.start + run.length) {
|
|
1485 if (isComplex) {
|
30
|
1486 logAttr = run.psla + (offset - run.start);
|
29
|
1487 }
|
|
1488 switch (movement) {
|
|
1489 case DWT.MOVEMENT_CLUSTER: {
|
|
1490 if (properties.fNeedsCaretInfo) {
|
|
1491 if (!logAttr.fInvalid && logAttr.fCharStop) return untranslateOffset(offset);
|
|
1492 } else {
|
|
1493 return untranslateOffset(offset);
|
|
1494 }
|
|
1495 break;
|
|
1496 }
|
|
1497 case DWT.MOVEMENT_WORD_START:
|
|
1498 case DWT.MOVEMENT_WORD: {
|
|
1499 if (properties.fNeedsWordBreaking) {
|
|
1500 if (!logAttr.fInvalid && logAttr.fWordStop) return untranslateOffset(offset);
|
|
1501 } else {
|
|
1502 if (offset > 0) {
|
|
1503 bool letterOrDigit = Compatibility.isLetterOrDigit(segmentsText.charAt(offset));
|
|
1504 bool previousLetterOrDigit = Compatibility.isLetterOrDigit(segmentsText.charAt(offset - 1));
|
|
1505 if (letterOrDigit !is previousLetterOrDigit || !letterOrDigit) {
|
|
1506 if (!Compatibility.isWhitespace(segmentsText.charAt(offset))) {
|
|
1507 return untranslateOffset(offset);
|
|
1508 }
|
|
1509 }
|
|
1510 }
|
|
1511 }
|
|
1512 break;
|
|
1513 }
|
|
1514 case DWT.MOVEMENT_WORD_END: {
|
|
1515 if (offset > 0) {
|
|
1516 bool isLetterOrDigit = Compatibility.isLetterOrDigit(segmentsText.charAt(offset));
|
|
1517 bool previousLetterOrDigit = Compatibility.isLetterOrDigit(segmentsText.charAt(offset - 1));
|
|
1518 if (!isLetterOrDigit && previousLetterOrDigit) {
|
|
1519 return untranslateOffset(offset);
|
|
1520 }
|
|
1521 }
|
|
1522 break;
|
|
1523 }
|
|
1524 }
|
|
1525 offset = validadeOffset(offset, step);
|
|
1526 }
|
|
1527 }
|
|
1528 i += step;
|
|
1529 } while (0 <= i && i < allRuns.length - 1 && 0 <= offset && offset < length);
|
30
|
1530 return forward ? text.length : 0;
|
29
|
1531 }
|
|
1532
|
|
1533 /**
|
|
1534 * Returns the character offset for the specified point.
|
|
1535 * For a typical character, the trailing argument will be filled in to
|
|
1536 * indicate whether the point is closer to the leading edge (0) or
|
|
1537 * the trailing edge (1). When the point is over a cluster composed
|
|
1538 * of multiple characters, the trailing argument will be filled with the
|
|
1539 * position of the character in the cluster that is closest to
|
|
1540 * the point.
|
|
1541 *
|
|
1542 * @param point the point
|
|
1543 * @param trailing the trailing buffer
|
|
1544 * @return the character offset
|
|
1545 *
|
|
1546 * @exception IllegalArgumentException <ul>
|
|
1547 * <li>ERROR_INVALID_ARGUMENT - if the trailing length is less than <code>1</code></li>
|
|
1548 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
|
|
1549 * </ul>
|
|
1550 * @exception DWTException <ul>
|
|
1551 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1552 * </ul>
|
|
1553 *
|
|
1554 * @see #getLocation(int, bool)
|
|
1555 */
|
|
1556 public int getOffset (Point point, int[] trailing) {
|
|
1557 checkLayout();
|
|
1558 if (point is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
|
|
1559 return getOffset (point.x, point.y, trailing) ;
|
|
1560 }
|
|
1561
|
|
1562 /**
|
|
1563 * Returns the character offset for the specified point.
|
|
1564 * For a typical character, the trailing argument will be filled in to
|
|
1565 * indicate whether the point is closer to the leading edge (0) or
|
|
1566 * the trailing edge (1). When the point is over a cluster composed
|
|
1567 * of multiple characters, the trailing argument will be filled with the
|
|
1568 * position of the character in the cluster that is closest to
|
|
1569 * the point.
|
|
1570 *
|
|
1571 * @param x the x coordinate of the point
|
|
1572 * @param y the y coordinate of the point
|
|
1573 * @param trailing the trailing buffer
|
|
1574 * @return the character offset
|
|
1575 *
|
|
1576 * @exception IllegalArgumentException <ul>
|
|
1577 * <li>ERROR_INVALID_ARGUMENT - if the trailing length is less than <code>1</code></li>
|
|
1578 * </ul>
|
|
1579 * @exception DWTException <ul>
|
|
1580 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1581 * </ul>
|
|
1582 *
|
|
1583 * @see #getLocation(int, bool)
|
|
1584 */
|
|
1585 public int getOffset (int x, int y, int[] trailing) {
|
|
1586 checkLayout();
|
|
1587 computeRuns(null);
|
|
1588 if (trailing !is null && trailing.length < 1) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
1589 int line;
|
|
1590 int lineCount = runs.length;
|
|
1591 for (line=0; line<lineCount; line++) {
|
|
1592 if (lineY[line + 1] > y) break;
|
|
1593 }
|
|
1594 line = Math.min(line, runs.length - 1);
|
|
1595 x -= getLineIndent(line);
|
|
1596 StyleItem[] lineRuns = runs[line];
|
|
1597 if (x >= lineWidth[line]) x = lineWidth[line] - 1;
|
|
1598 if (x < 0) x = 0;
|
|
1599 int width = 0;
|
|
1600 for (int i = 0; i < lineRuns.length; i++) {
|
|
1601 StyleItem run = lineRuns[i];
|
|
1602 if (run.lineBreak && !run.softBreak) return untranslateOffset(run.start);
|
|
1603 if (width + run.width > x) {
|
|
1604 int xRun = x - width;
|
|
1605 if (run.style !is null && run.style.metrics !is null) {
|
|
1606 GlyphMetrics metrics = run.style.metrics;
|
|
1607 if (metrics.width > 0) {
|
|
1608 if (trailing !is null) {
|
|
1609 trailing[0] = (xRun % metrics.width < metrics.width / 2) ? 0 : 1;
|
|
1610 }
|
|
1611 return untranslateOffset(run.start + xRun / metrics.width);
|
|
1612 }
|
|
1613 }
|
|
1614 if (run.tab) {
|
|
1615 if (trailing !is null) trailing[0] = x < (width + run.width / 2) ? 0 : 1;
|
|
1616 return untranslateOffset(run.start);
|
|
1617 }
|
|
1618 int cChars = run.length;
|
|
1619 int cGlyphs = run.glyphCount;
|
30
|
1620 int piCP;
|
|
1621 int piTrailing;
|
29
|
1622 if ((orientation & DWT.RIGHT_TO_LEFT) !is 0) {
|
|
1623 xRun = run.width - xRun;
|
|
1624 }
|
30
|
1625 int* advances = run.justify !is null ? run.justify : run.advances;
|
|
1626 OS.ScriptXtoCP(xRun, cChars, cGlyphs, run.clusters, run.visAttrs, advances, &run.analysis, &piCP, &piTrailing);
|
|
1627 if (trailing !is null) trailing[0] = piTrailing;
|
|
1628 return untranslateOffset(run.start + piCP);
|
29
|
1629 }
|
|
1630 width += run.width;
|
|
1631 }
|
|
1632 if (trailing !is null) trailing[0] = 0;
|
|
1633 return untranslateOffset(lineOffset[line + 1]);
|
|
1634 }
|
|
1635
|
|
1636 /**
|
|
1637 * Returns the orientation of the receiver.
|
|
1638 *
|
|
1639 * @return the orientation style
|
|
1640 *
|
|
1641 * @exception DWTException <ul>
|
|
1642 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1643 * </ul>
|
|
1644 */
|
|
1645 public int getOrientation () {
|
|
1646 checkLayout();
|
|
1647 return orientation;
|
|
1648 }
|
|
1649
|
|
1650 /**
|
|
1651 * Returns the previous offset for the specified offset and movement
|
|
1652 * type. The movement is one of <code>DWT.MOVEMENT_CHAR</code>,
|
|
1653 * <code>DWT.MOVEMENT_CLUSTER</code> or <code>DWT.MOVEMENT_WORD</code>,
|
|
1654 * <code>DWT.MOVEMENT_WORD_END</code> or <code>DWT.MOVEMENT_WORD_START</code>.
|
|
1655 *
|
|
1656 * @param offset the start offset
|
|
1657 * @param movement the movement type
|
|
1658 * @return the previous offset
|
|
1659 *
|
|
1660 * @exception IllegalArgumentException <ul>
|
|
1661 * <li>ERROR_INVALID_ARGUMENT - if the offset is out of range</li>
|
|
1662 * </ul>
|
|
1663 * @exception DWTException <ul>
|
|
1664 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1665 * </ul>
|
|
1666 *
|
|
1667 * @see #getNextOffset(int, int)
|
|
1668 */
|
|
1669 public int getPreviousOffset (int offset, int movement) {
|
|
1670 checkLayout();
|
|
1671 return _getOffset (offset, movement, false);
|
|
1672 }
|
|
1673
|
|
1674 /**
|
|
1675 * Gets the ranges of text that are associated with a <code>TextStyle</code>.
|
|
1676 *
|
|
1677 * @return the ranges, an array of offsets representing the start and end of each
|
|
1678 * text style.
|
|
1679 *
|
|
1680 * @exception DWTException <ul>
|
|
1681 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1682 * </ul>
|
|
1683 *
|
|
1684 * @see #getStyles()
|
|
1685 *
|
|
1686 * @since 3.2
|
|
1687 */
|
|
1688 public int[] getRanges () {
|
|
1689 checkLayout();
|
|
1690 int[] result = new int[styles.length * 2];
|
|
1691 int count = 0;
|
|
1692 for (int i=0; i<styles.length - 1; i++) {
|
|
1693 if (styles[i].style !is null) {
|
|
1694 result[count++] = styles[i].start;
|
|
1695 result[count++] = styles[i + 1].start - 1;
|
|
1696 }
|
|
1697 }
|
|
1698 if (count !is result.length) {
|
|
1699 int[] newResult = new int[count];
|
|
1700 System.arraycopy(result, 0, newResult, 0, count);
|
|
1701 result = newResult;
|
|
1702 }
|
|
1703 return result;
|
|
1704 }
|
|
1705
|
|
1706 /**
|
|
1707 * Returns the text segments offsets of the receiver.
|
|
1708 *
|
|
1709 * @return the text segments offsets
|
|
1710 *
|
|
1711 * @exception DWTException <ul>
|
|
1712 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1713 * </ul>
|
|
1714 */
|
|
1715 public int[] getSegments () {
|
|
1716 checkLayout();
|
|
1717 return segments;
|
|
1718 }
|
|
1719
|
|
1720 char[] getSegmentsText() {
|
|
1721 if (segments is null) return text;
|
|
1722 int nSegments = segments.length;
|
|
1723 if (nSegments <= 1) return text;
|
30
|
1724 int length = text.length;
|
29
|
1725 if (length is 0) return text;
|
|
1726 if (nSegments is 2) {
|
|
1727 if (segments[0] is 0 && segments[1] is length) return text;
|
|
1728 }
|
|
1729 char[] oldChars = new char[length];
|
|
1730 text.getChars(0, length, oldChars, 0);
|
|
1731 char[] newChars = new char[length + nSegments];
|
|
1732 int charCount = 0, segmentCount = 0;
|
30
|
1733 wchar separator = orientation is DWT.RIGHT_TO_LEFT ? RTL_MARK : LTR_MARK;
|
29
|
1734 while (charCount < length) {
|
|
1735 if (segmentCount < nSegments && charCount is segments[segmentCount]) {
|
|
1736 newChars[charCount + segmentCount++] = separator;
|
|
1737 } else {
|
|
1738 newChars[charCount + segmentCount] = oldChars[charCount++];
|
|
1739 }
|
|
1740 }
|
|
1741 if (segmentCount < nSegments) {
|
|
1742 segments[segmentCount] = charCount;
|
|
1743 newChars[charCount + segmentCount++] = separator;
|
|
1744 }
|
30
|
1745 return newChars[ 0 .. Math.min(charCount + segmentCount, newChars.length)].dup;
|
29
|
1746 }
|
|
1747
|
|
1748 /**
|
|
1749 * Returns the line spacing of the receiver.
|
|
1750 *
|
|
1751 * @return the line spacing
|
|
1752 *
|
|
1753 * @exception DWTException <ul>
|
|
1754 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1755 * </ul>
|
|
1756 */
|
|
1757 public int getSpacing () {
|
|
1758 checkLayout();
|
|
1759 return lineSpacing;
|
|
1760 }
|
|
1761
|
|
1762 /**
|
|
1763 * Gets the style of the receiver at the specified character offset.
|
|
1764 *
|
|
1765 * @param offset the text offset
|
|
1766 * @return the style or <code>null</code> if not set
|
|
1767 *
|
|
1768 * @exception IllegalArgumentException <ul>
|
|
1769 * <li>ERROR_INVALID_ARGUMENT - if the character offset is out of range</li>
|
|
1770 * </ul>
|
|
1771 * @exception DWTException <ul>
|
|
1772 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1773 * </ul>
|
|
1774 */
|
|
1775 public TextStyle getStyle (int offset) {
|
|
1776 checkLayout();
|
30
|
1777 int length = text.length;
|
29
|
1778 if (!(0 <= offset && offset < length)) DWT.error(DWT.ERROR_INVALID_RANGE);
|
|
1779 for (int i=1; i<styles.length; i++) {
|
|
1780 if (styles[i].start > offset) {
|
|
1781 return styles[i - 1].style;
|
|
1782 }
|
|
1783 }
|
|
1784 return null;
|
|
1785 }
|
|
1786
|
|
1787 /**
|
|
1788 * Gets all styles of the receiver.
|
|
1789 *
|
|
1790 * @return the styles
|
|
1791 *
|
|
1792 * @exception DWTException <ul>
|
|
1793 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1794 * </ul>
|
|
1795 *
|
|
1796 * @see #getRanges()
|
|
1797 *
|
|
1798 * @since 3.2
|
|
1799 */
|
|
1800 public TextStyle[] getStyles () {
|
|
1801 checkLayout();
|
|
1802 TextStyle[] result = new TextStyle[styles.length];
|
|
1803 int count = 0;
|
|
1804 for (int i=0; i<styles.length; i++) {
|
|
1805 if (styles[i].style !is null) {
|
|
1806 result[count++] = styles[i].style;
|
|
1807 }
|
|
1808 }
|
|
1809 if (count !is result.length) {
|
|
1810 TextStyle[] newResult = new TextStyle[count];
|
|
1811 System.arraycopy(result, 0, newResult, 0, count);
|
|
1812 result = newResult;
|
|
1813 }
|
|
1814 return result;
|
|
1815 }
|
|
1816
|
|
1817 /**
|
|
1818 * Returns the tab list of the receiver.
|
|
1819 *
|
|
1820 * @return the tab list
|
|
1821 *
|
|
1822 * @exception DWTException <ul>
|
|
1823 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1824 * </ul>
|
|
1825 */
|
|
1826 public int[] getTabs () {
|
|
1827 checkLayout();
|
|
1828 return tabs;
|
|
1829 }
|
|
1830
|
|
1831 /**
|
|
1832 * Gets the receiver's text, which will be an empty
|
|
1833 * string if it has never been set.
|
|
1834 *
|
|
1835 * @return the receiver's text
|
|
1836 *
|
|
1837 * @exception DWTException <ul>
|
|
1838 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1839 * </ul>
|
|
1840 */
|
|
1841 public char[] getText () {
|
|
1842 checkLayout();
|
|
1843 return text;
|
|
1844 }
|
|
1845
|
|
1846 /**
|
|
1847 * Returns the width of the receiver.
|
|
1848 *
|
|
1849 * @return the width
|
|
1850 *
|
|
1851 * @exception DWTException <ul>
|
|
1852 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1853 * </ul>
|
|
1854 */
|
|
1855 public int getWidth () {
|
|
1856 checkLayout();
|
|
1857 return wrapWidth;
|
|
1858 }
|
|
1859
|
|
1860 /**
|
|
1861 * Returns <code>true</code> if the text layout has been disposed,
|
|
1862 * and <code>false</code> otherwise.
|
|
1863 * <p>
|
|
1864 * This method gets the dispose state for the text layout.
|
|
1865 * When a text layout has been disposed, it is an error to
|
|
1866 * invoke any other method using the text layout.
|
|
1867 * </p>
|
|
1868 *
|
|
1869 * @return <code>true</code> when the text layout is disposed and <code>false</code> otherwise
|
|
1870 */
|
|
1871 public bool isDisposed () {
|
|
1872 return device is null;
|
|
1873 }
|
|
1874
|
|
1875 /*
|
|
1876 * Itemize the receiver text
|
|
1877 */
|
|
1878 StyleItem[] itemize () {
|
|
1879 segmentsText = getSegmentsText();
|
30
|
1880 int length = segmentsText.length;
|
|
1881 SCRIPT_CONTROL scriptControl;
|
|
1882 SCRIPT_STATE scriptState;
|
29
|
1883 final int MAX_ITEM = length + 1;
|
|
1884
|
|
1885 if ((orientation & DWT.RIGHT_TO_LEFT) !is 0) {
|
|
1886 scriptState.uBidiLevel = 1;
|
|
1887 scriptState.fArabicNumContext = true;
|
30
|
1888 SCRIPT_DIGITSUBSTITUTE psds;
|
|
1889 OS.ScriptRecordDigitSubstitution(OS.LOCALE_USER_DEFAULT, &psds);
|
|
1890 OS.ScriptApplyDigitSubstitution(&psds, &scriptControl, &scriptState);
|
29
|
1891 }
|
|
1892
|
30
|
1893 auto hHeap = OS.GetProcessHeap();
|
|
1894 auto pItems = cast(SCRIPT_ITEM*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, MAX_ITEM * SCRIPT_ITEM.sizeof);
|
|
1895 if (pItems is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
1896 int pcItems;
|
|
1897 wchar[] chars = StrToWCHARs( segmentsText );
|
|
1898 OS.ScriptItemize(chars.ptr, chars.length, MAX_ITEM, &scriptControl, &scriptState, pItems, &pcItems);
|
29
|
1899 // if (hr is E_OUTOFMEMORY) //TODO handle it
|
|
1900
|
30
|
1901 StyleItem[] runs = merge(pItems, pcItems);
|
29
|
1902 OS.HeapFree(hHeap, 0, pItems);
|
|
1903 return runs;
|
|
1904 }
|
|
1905
|
|
1906 /*
|
|
1907 * Merge styles ranges and script items
|
|
1908 */
|
30
|
1909 StyleItem[] merge (SCRIPT_ITEM* items, int itemCount) {
|
|
1910 int count = 0, start = 0, end = segmentsText.length, itemIndex = 0, styleIndex = 0;
|
29
|
1911 StyleItem[] runs = new StyleItem[itemCount + styles.length];
|
30
|
1912 SCRIPT_ITEM* scriptItem;
|
29
|
1913 bool linkBefore = false;
|
|
1914 while (start < end) {
|
|
1915 StyleItem item = new StyleItem();
|
|
1916 item.start = start;
|
|
1917 item.style = styles[styleIndex].style;
|
|
1918 runs[count++] = item;
|
30
|
1919 scriptItem = items + itemIndex;
|
29
|
1920 item.analysis = scriptItem.a;
|
|
1921 if (linkBefore) {
|
|
1922 item.analysis.fLinkBefore = true;
|
|
1923 linkBefore = false;
|
|
1924 }
|
30
|
1925 //scriptItem.a = new SCRIPT_ANALYSIS();
|
|
1926 scriptItem = items + (itemIndex + 1);
|
29
|
1927 int itemLimit = scriptItem.iCharPos;
|
|
1928 int styleLimit = translateOffset(styles[styleIndex + 1].start);
|
|
1929 if (styleLimit <= itemLimit) {
|
|
1930 styleIndex++;
|
|
1931 start = styleLimit;
|
|
1932 if (start < itemLimit && 0 < start && start < end) {
|
|
1933 char pChar = segmentsText.charAt(start - 1);
|
|
1934 char tChar = segmentsText.charAt(start);
|
|
1935 if (!Compatibility.isWhitespace(pChar) && !Compatibility.isWhitespace(tChar)) {
|
|
1936 item.analysis.fLinkAfter = true;
|
|
1937 linkBefore = true;
|
|
1938 }
|
|
1939 }
|
|
1940 }
|
|
1941 if (itemLimit <= styleLimit) {
|
|
1942 itemIndex++;
|
|
1943 start = itemLimit;
|
|
1944 }
|
|
1945 item.length = start - item.start;
|
|
1946 }
|
|
1947 StyleItem item = new StyleItem();
|
|
1948 item.start = end;
|
30
|
1949 scriptItem = items + itemCount;
|
29
|
1950 item.analysis = scriptItem.a;
|
|
1951 runs[count++] = item;
|
|
1952 if (runs.length !is count) {
|
|
1953 StyleItem[] result = new StyleItem[count];
|
|
1954 System.arraycopy(runs, 0, result, 0, count);
|
|
1955 return result;
|
|
1956 }
|
|
1957 return runs;
|
|
1958 }
|
|
1959
|
|
1960 /*
|
|
1961 * Reorder the run
|
|
1962 */
|
|
1963 StyleItem[] reorder (StyleItem[] runs, bool terminate) {
|
30
|
1964 int length_ = runs.length;
|
|
1965 if (length_ <= 1) return runs;
|
|
1966 ubyte[] bidiLevels = new ubyte[length_];
|
|
1967 for (int i=0; i<length_; i++) {
|
29
|
1968 bidiLevels[i] = cast(byte)(runs[i].analysis.s.uBidiLevel & 0x1F);
|
|
1969 }
|
|
1970 /*
|
|
1971 * Feature in Windows. If the orientation is RTL Uniscribe will
|
|
1972 * resolve the level of line breaks to 1, this can cause the line
|
|
1973 * break to be reorder to the middle of the line. The fix is to set
|
|
1974 * the level to zero to prevent it to be reordered.
|
|
1975 */
|
30
|
1976 StyleItem lastRun = runs[length_ - 1];
|
29
|
1977 if (lastRun.lineBreak && !lastRun.softBreak) {
|
30
|
1978 bidiLevels[length_ - 1] = 0;
|
29
|
1979 }
|
30
|
1980 int[] log2vis = new int[length_];
|
|
1981 OS.ScriptLayout(length_, bidiLevels.ptr, null, log2vis.ptr);
|
|
1982 StyleItem[] result = new StyleItem[length_];
|
|
1983 for (int i=0; i<length_; i++) {
|
29
|
1984 result[log2vis[i]] = runs[i];
|
|
1985 }
|
|
1986 if ((orientation & DWT.RIGHT_TO_LEFT) !is 0) {
|
30
|
1987 if (terminate) length_--;
|
|
1988 for (int i = 0; i < length_ / 2 ; i++) {
|
29
|
1989 StyleItem tmp = result[i];
|
30
|
1990 result[i] = result[length_ - i - 1];
|
|
1991 result[length_ - i - 1] = tmp;
|
29
|
1992 }
|
|
1993 }
|
|
1994 return result;
|
|
1995 }
|
|
1996
|
|
1997 /**
|
|
1998 * Sets the text alignment for the receiver. The alignment controls
|
|
1999 * how a line of text is positioned horizontally. The argument should
|
|
2000 * be one of <code>DWT.LEFT</code>, <code>DWT.RIGHT</code> or <code>DWT.CENTER</code>.
|
|
2001 * <p>
|
|
2002 * The default alignment is <code>DWT.LEFT</code>. Note that the receiver's
|
|
2003 * width must be set in order to use <code>DWT.RIGHT</code> or <code>DWT.CENTER</code>
|
|
2004 * alignment.
|
|
2005 * </p>
|
|
2006 *
|
|
2007 * @param alignment the new alignment
|
|
2008 *
|
|
2009 * @exception DWTException <ul>
|
|
2010 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2011 * </ul>
|
|
2012 *
|
|
2013 * @see #setWidth(int)
|
|
2014 */
|
|
2015 public void setAlignment (int alignment) {
|
|
2016 checkLayout();
|
|
2017 int mask = DWT.LEFT | DWT.CENTER | DWT.RIGHT;
|
|
2018 alignment &= mask;
|
|
2019 if (alignment is 0) return;
|
|
2020 if ((alignment & DWT.LEFT) !is 0) alignment = DWT.LEFT;
|
|
2021 if ((alignment & DWT.RIGHT) !is 0) alignment = DWT.RIGHT;
|
|
2022 if (this.alignment is alignment) return;
|
|
2023 freeRuns();
|
|
2024 this.alignment = alignment;
|
|
2025 }
|
|
2026
|
|
2027 /**
|
|
2028 * Sets the ascent of the receiver. The ascent is distance in pixels
|
|
2029 * from the baseline to the top of the line and it is applied to all
|
|
2030 * lines. The default value is <code>-1</code> which means that the
|
|
2031 * ascent is calculated from the line fonts.
|
|
2032 *
|
|
2033 * @param ascent the new ascent
|
|
2034 *
|
|
2035 * @exception IllegalArgumentException <ul>
|
|
2036 * <li>ERROR_INVALID_ARGUMENT - if the ascent is less than <code>-1</code></li>
|
|
2037 * </ul>
|
|
2038 * @exception DWTException <ul>
|
|
2039 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2040 * </ul>
|
|
2041 *
|
|
2042 * @see #setDescent(int)
|
|
2043 * @see #getLineMetrics(int)
|
|
2044 */
|
|
2045 public void setAscent(int ascent) {
|
|
2046 checkLayout();
|
|
2047 if (ascent < -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
2048 if (this.ascent is ascent) return;
|
|
2049 freeRuns();
|
|
2050 this.ascent = ascent;
|
|
2051 }
|
|
2052
|
|
2053 /**
|
|
2054 * Sets the descent of the receiver. The descent is distance in pixels
|
|
2055 * from the baseline to the bottom of the line and it is applied to all
|
|
2056 * lines. The default value is <code>-1</code> which means that the
|
|
2057 * descent is calculated from the line fonts.
|
|
2058 *
|
|
2059 * @param descent the new descent
|
|
2060 *
|
|
2061 * @exception IllegalArgumentException <ul>
|
|
2062 * <li>ERROR_INVALID_ARGUMENT - if the descent is less than <code>-1</code></li>
|
|
2063 * </ul>
|
|
2064 * @exception DWTException <ul>
|
|
2065 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2066 * </ul>
|
|
2067 *
|
|
2068 * @see #setAscent(int)
|
|
2069 * @see #getLineMetrics(int)
|
|
2070 */
|
|
2071 public void setDescent(int descent) {
|
|
2072 checkLayout();
|
|
2073 if (descent < -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
2074 if (this.descent is descent) return;
|
|
2075 freeRuns();
|
|
2076 this.descent = descent;
|
|
2077 }
|
|
2078
|
|
2079 /**
|
|
2080 * Sets the default font which will be used by the receiver
|
|
2081 * to draw and measure text. If the
|
|
2082 * argument is null, then a default font appropriate
|
|
2083 * for the platform will be used instead. Note that a text
|
|
2084 * style can override the default font.
|
|
2085 *
|
|
2086 * @param font the new font for the receiver, or null to indicate a default font
|
|
2087 *
|
|
2088 * @exception IllegalArgumentException <ul>
|
|
2089 * <li>ERROR_INVALID_ARGUMENT - if the font has been disposed</li>
|
|
2090 * </ul>
|
|
2091 * @exception DWTException <ul>
|
|
2092 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2093 * </ul>
|
|
2094 */
|
|
2095 public void setFont (Font font) {
|
|
2096 checkLayout();
|
|
2097 if (font !is null && font.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
2098 if (this.font is font) return;
|
30
|
2099 if (font !is null && font ==/*eq*/ this.font) return;
|
29
|
2100 freeRuns();
|
|
2101 this.font = font;
|
|
2102 }
|
|
2103
|
|
2104 /**
|
|
2105 * Sets the indent of the receiver. This indent it applied of the first line of
|
|
2106 * each paragraph.
|
|
2107 *
|
|
2108 * @param indent new indent
|
|
2109 *
|
|
2110 * @exception DWTException <ul>
|
|
2111 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2112 * </ul>
|
|
2113 *
|
|
2114 * @since 3.2
|
|
2115 */
|
|
2116 public void setIndent (int indent) {
|
|
2117 checkLayout();
|
|
2118 if (indent < 0) return;
|
|
2119 if (this.indent is indent) return;
|
|
2120 freeRuns();
|
|
2121 this.indent = indent;
|
|
2122 }
|
|
2123
|
|
2124 /**
|
|
2125 * Sets the justification of the receiver. Note that the receiver's
|
|
2126 * width must be set in order to use justification.
|
|
2127 *
|
|
2128 * @param justify new justify
|
|
2129 *
|
|
2130 * @exception DWTException <ul>
|
|
2131 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2132 * </ul>
|
|
2133 *
|
|
2134 * @since 3.2
|
|
2135 */
|
|
2136 public void setJustify (bool justify) {
|
|
2137 checkLayout();
|
|
2138 if (this.justify is justify) return;
|
|
2139 freeRuns();
|
|
2140 this.justify = justify;
|
|
2141 }
|
|
2142
|
|
2143 /**
|
|
2144 * Sets the orientation of the receiver, which must be one
|
|
2145 * of <code>DWT.LEFT_TO_RIGHT</code> or <code>DWT.RIGHT_TO_LEFT</code>.
|
|
2146 *
|
|
2147 * @param orientation new orientation style
|
|
2148 *
|
|
2149 * @exception DWTException <ul>
|
|
2150 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2151 * </ul>
|
|
2152 */
|
|
2153 public void setOrientation (int orientation) {
|
|
2154 checkLayout();
|
|
2155 int mask = DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT;
|
|
2156 orientation &= mask;
|
|
2157 if (orientation is 0) return;
|
|
2158 if ((orientation & DWT.LEFT_TO_RIGHT) !is 0) orientation = DWT.LEFT_TO_RIGHT;
|
|
2159 if (this.orientation is orientation) return;
|
|
2160 this.orientation = orientation;
|
|
2161 freeRuns();
|
|
2162 }
|
|
2163
|
|
2164 /**
|
|
2165 * Sets the offsets of the receiver's text segments. Text segments are used to
|
|
2166 * override the default behaviour of the bidirectional algorithm.
|
|
2167 * Bidirectional reordering can happen within a text segment but not
|
|
2168 * between two adjacent segments.
|
|
2169 * <p>
|
|
2170 * Each text segment is determined by two consecutive offsets in the
|
|
2171 * <code>segments</code> arrays. The first element of the array should
|
|
2172 * always be zero and the last one should always be equals to length of
|
|
2173 * the text.
|
|
2174 * </p>
|
|
2175 *
|
|
2176 * @param segments the text segments offset
|
|
2177 *
|
|
2178 * @exception DWTException <ul>
|
|
2179 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2180 * </ul>
|
|
2181 */
|
|
2182 public void setSegments(int[] segments) {
|
|
2183 checkLayout();
|
|
2184 if (this.segments is null && segments is null) return;
|
|
2185 if (this.segments !is null && segments !is null) {
|
|
2186 if (this.segments.length is segments.length) {
|
|
2187 int i;
|
|
2188 for (i = 0; i <segments.length; i++) {
|
|
2189 if (this.segments[i] !is segments[i]) break;
|
|
2190 }
|
|
2191 if (i is segments.length) return;
|
|
2192 }
|
|
2193 }
|
|
2194 freeRuns();
|
|
2195 this.segments = segments;
|
|
2196 }
|
|
2197
|
|
2198 /**
|
|
2199 * Sets the line spacing of the receiver. The line spacing
|
|
2200 * is the space left between lines.
|
|
2201 *
|
|
2202 * @param spacing the new line spacing
|
|
2203 *
|
|
2204 * @exception IllegalArgumentException <ul>
|
|
2205 * <li>ERROR_INVALID_ARGUMENT - if the spacing is negative</li>
|
|
2206 * </ul>
|
|
2207 * @exception DWTException <ul>
|
|
2208 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2209 * </ul>
|
|
2210 */
|
|
2211 public void setSpacing (int spacing) {
|
|
2212 checkLayout();
|
|
2213 if (spacing < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
2214 if (this.lineSpacing is spacing) return;
|
|
2215 freeRuns();
|
|
2216 this.lineSpacing = spacing;
|
|
2217 }
|
|
2218
|
|
2219 /**
|
|
2220 * Sets the style of the receiver for the specified range. Styles previously
|
|
2221 * set for that range will be overwritten. The start and end offsets are
|
|
2222 * inclusive and will be clamped if out of range.
|
|
2223 *
|
|
2224 * @param style the style
|
|
2225 * @param start the start offset
|
|
2226 * @param end the end offset
|
|
2227 *
|
|
2228 * @exception DWTException <ul>
|
|
2229 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2230 * </ul>
|
|
2231 */
|
|
2232 public void setStyle (TextStyle style, int start, int end) {
|
|
2233 checkLayout();
|
30
|
2234 int length_ = text.length;
|
|
2235 if (length_ is 0) return;
|
29
|
2236 if (start > end) return;
|
30
|
2237 start = Math.min(Math.max(0, start), length_ - 1);
|
|
2238 end = Math.min(Math.max(0, end), length_ - 1);
|
29
|
2239 int low = -1;
|
|
2240 int high = styles.length;
|
|
2241 while (high - low > 1) {
|
|
2242 int index = (high + low) / 2;
|
|
2243 if (styles[index + 1].start > start) {
|
|
2244 high = index;
|
|
2245 } else {
|
|
2246 low = index;
|
|
2247 }
|
|
2248 }
|
|
2249 if (0 <= high && high < styles.length) {
|
|
2250 StyleItem item = styles[high];
|
|
2251 if (item.start is start && styles[high + 1].start - 1 is end) {
|
|
2252 if (style is null) {
|
|
2253 if (item.style is null) return;
|
|
2254 } else {
|
30
|
2255 if (style ==/*eq*/ item.style) return;
|
29
|
2256 }
|
|
2257 }
|
|
2258 }
|
|
2259 freeRuns();
|
|
2260 int modifyStart = high;
|
|
2261 int modifyEnd = modifyStart;
|
|
2262 while (modifyEnd < styles.length) {
|
|
2263 if (styles[modifyEnd + 1].start > end) break;
|
|
2264 modifyEnd++;
|
|
2265 }
|
|
2266 if (modifyStart is modifyEnd) {
|
|
2267 int styleStart = styles[modifyStart].start;
|
|
2268 int styleEnd = styles[modifyEnd + 1].start - 1;
|
|
2269 if (styleStart is start && styleEnd is end) {
|
|
2270 styles[modifyStart].style = style;
|
|
2271 return;
|
|
2272 }
|
|
2273 if (styleStart !is start && styleEnd !is end) {
|
|
2274 StyleItem[] newStyles = new StyleItem[styles.length + 2];
|
|
2275 System.arraycopy(styles, 0, newStyles, 0, modifyStart + 1);
|
|
2276 StyleItem item = new StyleItem();
|
|
2277 item.start = start;
|
|
2278 item.style = style;
|
|
2279 newStyles[modifyStart + 1] = item;
|
|
2280 item = new StyleItem();
|
|
2281 item.start = end + 1;
|
|
2282 item.style = styles[modifyStart].style;
|
|
2283 newStyles[modifyStart + 2] = item;
|
|
2284 System.arraycopy(styles, modifyEnd + 1, newStyles, modifyEnd + 3, styles.length - modifyEnd - 1);
|
|
2285 styles = newStyles;
|
|
2286 return;
|
|
2287 }
|
|
2288 }
|
|
2289 if (start is styles[modifyStart].start) modifyStart--;
|
|
2290 if (end is styles[modifyEnd + 1].start - 1) modifyEnd++;
|
|
2291 int newLength = styles.length + 1 - (modifyEnd - modifyStart - 1);
|
|
2292 StyleItem[] newStyles = new StyleItem[newLength];
|
|
2293 System.arraycopy(styles, 0, newStyles, 0, modifyStart + 1);
|
|
2294 StyleItem item = new StyleItem();
|
|
2295 item.start = start;
|
|
2296 item.style = style;
|
|
2297 newStyles[modifyStart + 1] = item;
|
|
2298 styles[modifyEnd].start = end + 1;
|
|
2299 System.arraycopy(styles, modifyEnd, newStyles, modifyStart + 2, styles.length - modifyEnd);
|
|
2300 styles = newStyles;
|
|
2301 }
|
|
2302
|
|
2303 /**
|
|
2304 * Sets the receiver's tab list. Each value in the tab list specifies
|
|
2305 * the space in pixels from the origin of the text layout to the respective
|
|
2306 * tab stop. The last tab stop width is repeated continuously.
|
|
2307 *
|
|
2308 * @param tabs the new tab list
|
|
2309 *
|
|
2310 * @exception DWTException <ul>
|
|
2311 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2312 * </ul>
|
|
2313 */
|
|
2314 public void setTabs (int[] tabs) {
|
|
2315 checkLayout();
|
|
2316 if (this.tabs is null && tabs is null) return;
|
|
2317 if (this.tabs !is null && tabs !is null) {
|
|
2318 if (this.tabs.length is tabs.length) {
|
|
2319 int i;
|
|
2320 for (i = 0; i <tabs.length; i++) {
|
|
2321 if (this.tabs[i] !is tabs[i]) break;
|
|
2322 }
|
|
2323 if (i is tabs.length) return;
|
|
2324 }
|
|
2325 }
|
|
2326 freeRuns();
|
|
2327 this.tabs = tabs;
|
|
2328 }
|
|
2329
|
|
2330 /**
|
|
2331 * Sets the receiver's text.
|
|
2332 *
|
|
2333 * @param text the new text
|
|
2334 *
|
|
2335 * @exception IllegalArgumentException <ul>
|
|
2336 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
|
|
2337 * </ul>
|
|
2338 * @exception DWTException <ul>
|
|
2339 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2340 * </ul>
|
|
2341 */
|
|
2342 public void setText (char[] text) {
|
|
2343 checkLayout();
|
|
2344 if (text is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
|
30
|
2345 if (text==/*eq*/this.text) return;
|
29
|
2346 freeRuns();
|
|
2347 this.text = text;
|
|
2348 styles = new StyleItem[2];
|
|
2349 styles[0] = new StyleItem();
|
|
2350 styles[1] = new StyleItem();
|
30
|
2351 styles[1].start = text.length;
|
29
|
2352 }
|
|
2353
|
|
2354 /**
|
|
2355 * Sets the line width of the receiver, which determines how
|
|
2356 * text should be wrapped and aligned. The default value is
|
|
2357 * <code>-1</code> which means wrapping is disabled.
|
|
2358 *
|
|
2359 * @param width the new width
|
|
2360 *
|
|
2361 * @exception IllegalArgumentException <ul>
|
|
2362 * <li>ERROR_INVALID_ARGUMENT - if the width is <code>0</code> or less than <code>-1</code></li>
|
|
2363 * </ul>
|
|
2364 * @exception DWTException <ul>
|
|
2365 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
2366 * </ul>
|
|
2367 *
|
|
2368 * @see #setAlignment(int)
|
|
2369 */
|
|
2370 public void setWidth (int width) {
|
|
2371 checkLayout();
|
|
2372 if (width < -1 || width is 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
2373 if (this.wrapWidth is width) return;
|
|
2374 freeRuns();
|
|
2375 this.wrapWidth = width;
|
|
2376 }
|
|
2377
|
30
|
2378 bool shape (HDC hdc, StyleItem run, char[] chars, int[] glyphCount, int maxGlyphs) {
|
|
2379 wchar[] wchars = StrToWCHARs( chars );
|
|
2380 auto hr = OS.ScriptShape(hdc, run.psc, wchars.ptr, wchars.length, maxGlyphs, &run.analysis, run.glyphs, run.clusters, run.visAttrs, glyphCount.ptr);
|
29
|
2381 run.glyphCount = glyphCount[0];
|
|
2382 if (hr !is OS.USP_E_SCRIPT_NOT_IN_FONT) {
|
30
|
2383 SCRIPT_FONTPROPERTIES fp;
|
29
|
2384 fp.cBytes = SCRIPT_FONTPROPERTIES.sizeof;
|
30
|
2385 OS.ScriptGetFontProperties(hdc, run.psc, &fp);
|
|
2386 ushort[] glyphs = run.glyphs[ 0 .. glyphCount[0] ];
|
29
|
2387 int i;
|
|
2388 for (i = 0; i < glyphs.length; i++) {
|
|
2389 if (glyphs[i] is fp.wgDefault) break;
|
|
2390 }
|
|
2391 if (i is glyphs.length) return true;
|
|
2392 }
|
30
|
2393 if (run.psc !is null) {
|
29
|
2394 OS.ScriptFreeCache(run.psc);
|
|
2395 glyphCount[0] = 0;
|
30
|
2396 *cast(int*)run.psc = 0;
|
29
|
2397 }
|
|
2398 run.glyphCount = 0;
|
|
2399 return false;
|
|
2400 }
|
|
2401
|
|
2402 /*
|
|
2403 * Generate glyphs for one Run.
|
|
2404 */
|
|
2405 void shape (HDC hdc, StyleItem run) {
|
|
2406 int[] buffer = new int[1];
|
|
2407 char[] chars = new char[run.length];
|
|
2408 segmentsText.getChars(run.start, run.start + run.length, chars, 0);
|
30
|
2409 wchar[] wchars = StrToWCHARs( chars );
|
29
|
2410 int maxGlyphs = (chars.length * 3 / 2) + 16;
|
30
|
2411 auto hHeap = OS.GetProcessHeap();
|
|
2412 run.glyphs = cast(ushort*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, maxGlyphs * 2);
|
|
2413 if (run.glyphs is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
2414 run.clusters = cast(WORD*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, maxGlyphs * 2);
|
|
2415 if (run.clusters is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
2416 run.visAttrs = cast(SCRIPT_VISATTR*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, maxGlyphs * SCRIPT_VISATTR_SIZEOF);
|
|
2417 if (run.visAttrs is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
2418 run.psc = cast(SCRIPT_CACHE*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, 4);
|
|
2419 if (run.psc is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
29
|
2420 if (!shape(hdc, run, chars, buffer, maxGlyphs)) {
|
30
|
2421 if (mLangFontLink2 !is null) {
|
|
2422 int dwCodePages;
|
|
2423 int cchCodePages;
|
29
|
2424 /* GetStrCodePages() */
|
30
|
2425 OS.VtblCall(4, mLangFontLink2, cast(int)wchars.ptr, wchars.length, 0, cast(int)&dwCodePages, cast(int)&cchCodePages);
|
|
2426 HFONT hNewFont;
|
29
|
2427 /* MapFont() */
|
30
|
2428 if (OS.VtblCall(10, mLangFontLink2, cast(int)hdc, dwCodePages, cast(int)wchars[0], cast(int)hNewFont) is OS.S_OK) {
|
|
2429 auto hFont = OS.SelectObject(hdc, hNewFont);
|
29
|
2430 if (shape(hdc, run, chars, buffer, maxGlyphs)) {
|
30
|
2431 run.fallbackFont = hNewFont;
|
29
|
2432 } else {
|
|
2433 /* ReleaseFont() */
|
30
|
2434 OS.VtblCall(8, mLangFontLink2, cast(int)hNewFont);
|
29
|
2435 OS.SelectObject(hdc, hFont);
|
30
|
2436 SCRIPT_PROPERTIES* properties;
|
|
2437 properties = device.scripts[run.analysis.eScript];
|
29
|
2438 if (properties.fPrivateUseArea) {
|
|
2439 run.analysis.fNoGlyphIndex = true;
|
|
2440 }
|
30
|
2441 OS.ScriptShape(hdc, run.psc, wchars.ptr, wchars.length, maxGlyphs, &run.analysis, run.glyphs, run.clusters, run.visAttrs, buffer.ptr);
|
29
|
2442 run.glyphCount = buffer[0];
|
|
2443 }
|
|
2444 }
|
|
2445 }
|
|
2446 }
|
30
|
2447 ABC abc;
|
|
2448 run.advances = cast(int*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, run.glyphCount * 4);
|
|
2449 if (run.advances is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
2450 run.goffsets = cast(GOFFSET*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, run.glyphCount * GOFFSET_SIZEOF);
|
|
2451 if (run.goffsets is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
2452 OS.ScriptPlace(hdc, run.psc, run.glyphs, run.glyphCount, run.visAttrs, &run.analysis, run.advances, run.goffsets, &abc);
|
29
|
2453 if (run.style !is null && run.style.metrics !is null) {
|
|
2454 GlyphMetrics metrics = run.style.metrics;
|
|
2455 /*
|
|
2456 * Bug in Windows, on a Japanese machine, Uniscribe returns glyphcount
|
|
2457 * equals zero for FFFC (possibly other unicode code points), the fix
|
|
2458 * is to make sure the glyph is at least one pixel wide.
|
|
2459 */
|
|
2460 run.width = metrics.width * Math.max (1, run.glyphCount);
|
|
2461 run.ascent = metrics.ascent;
|
|
2462 run.descent = metrics.descent;
|
|
2463 run.leading = 0;
|
|
2464 } else {
|
30
|
2465 run.width = abc.abcA + abc.abcB + abc.abcC;
|
29
|
2466 TEXTMETRIC lptm;
|
|
2467 OS.GetTextMetrics(hdc, &lptm);
|
|
2468 run.ascent = lptm.tmAscent;
|
|
2469 run.descent = lptm.tmDescent;
|
|
2470 run.leading = lptm.tmInternalLeading;
|
|
2471 }
|
|
2472 if (run.style !is null) {
|
|
2473 run.ascent += run.style.rise;
|
|
2474 run.descent -= +run.style.rise;
|
|
2475 }
|
|
2476 }
|
|
2477
|
|
2478 int validadeOffset(int offset, int step) {
|
|
2479 offset += step;
|
|
2480 if (segments !is null && segments.length > 2) {
|
|
2481 for (int i = 0; i < segments.length; i++) {
|
|
2482 if (translateOffset(segments[i]) - 1 is offset) {
|
|
2483 offset += step;
|
|
2484 break;
|
|
2485 }
|
|
2486 }
|
|
2487 }
|
|
2488 return offset;
|
|
2489 }
|
|
2490
|
|
2491 /**
|
|
2492 * Returns a string containing a concise, human-readable
|
|
2493 * description of the receiver.
|
|
2494 *
|
|
2495 * @return a string representation of the receiver
|
|
2496 */
|
|
2497 public char[] toString () {
|
|
2498 if (isDisposed()) return "TextLayout {*DISPOSED*}";
|
|
2499 return "TextLayout {}";
|
|
2500 }
|
|
2501
|
|
2502 int translateOffset(int offset) {
|
|
2503 if (segments is null) return offset;
|
|
2504 int nSegments = segments.length;
|
|
2505 if (nSegments <= 1) return offset;
|
30
|
2506 int length_ = text.length;
|
|
2507 if (length_ is 0) return offset;
|
29
|
2508 if (nSegments is 2) {
|
30
|
2509 if (segments[0] is 0 && segments[1] is length_) return offset;
|
29
|
2510 }
|
|
2511 for (int i = 0; i < nSegments && offset - i >= segments[i]; i++) {
|
|
2512 offset++;
|
|
2513 }
|
|
2514 return offset;
|
|
2515 }
|
|
2516
|
|
2517 int untranslateOffset(int offset) {
|
|
2518 if (segments is null) return offset;
|
|
2519 int nSegments = segments.length;
|
|
2520 if (nSegments <= 1) return offset;
|
30
|
2521 int length_ = text.length;
|
|
2522 if (length_ is 0) return offset;
|
29
|
2523 if (nSegments is 2) {
|
30
|
2524 if (segments[0] is 0 && segments[1] is length_) return offset;
|
29
|
2525 }
|
|
2526 for (int i = 0; i < nSegments && offset > segments[i]; i++) {
|
|
2527 offset--;
|
|
2528 }
|
|
2529 return offset;
|
|
2530 }
|
|
2531 }
|