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