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