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