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