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