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