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