Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/graphics/TextLayout.d @ 54:70388b0e6dad
[swt lin] compiles
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 27 Mar 2009 23:31:11 +0100 |
parents | 65761bc28ab2 |
children | 536e43f63c81 |
rev | line source |
---|---|
25 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.graphics.TextLayout; | |
14 | |
15 import java.lang.all; | |
16 | |
17 import org.eclipse.swt.internal.Compatibility; | |
54 | 18 import org.eclipse.swt.internal.cairo.Cairo : Cairo; |
25 | 19 import org.eclipse.swt.internal.gtk.OS; |
20 import org.eclipse.swt.internal.Converter; | |
21 import org.eclipse.swt.SWT; | |
22 import org.eclipse.swt.graphics.Color; | |
23 import org.eclipse.swt.graphics.Device; | |
24 import org.eclipse.swt.graphics.Font; | |
25 import org.eclipse.swt.graphics.FontMetrics; | |
26 import org.eclipse.swt.graphics.GC; | |
27 import org.eclipse.swt.graphics.GCData; | |
28 import org.eclipse.swt.graphics.GlyphMetrics; | |
29 import org.eclipse.swt.graphics.Point; | |
30 import org.eclipse.swt.graphics.Rectangle; | |
31 import org.eclipse.swt.graphics.Region; | |
32 import org.eclipse.swt.graphics.Resource; | |
33 import org.eclipse.swt.graphics.TextStyle; | |
34 import java.lang.all; | |
35 | |
26 | 36 version(Tango){ |
37 import tango.stdc.string : memmove; | |
38 import tango.text.convert.Utf; | |
39 } else { // Phobos | |
40 import std.c.string : memmove; | |
41 } | |
25 | 42 |
43 /** | |
44 * <code>TextLayout</code> is a graphic object that represents | |
45 * styled text. | |
46 * <p> | |
47 * Instances of this class provide support for drawing, cursor | |
48 * navigation, hit testing, text wrapping, alignment, tab expansion | |
49 * line breaking, etc. These are aspects required for rendering internationalized text. | |
50 * </p><p> | |
51 * Application code must explicitly invoke the <code>TextLayout#dispose()</code> | |
52 * method to release the operating system resources managed by each instance | |
53 * when those instances are no longer required. | |
54 * </p> | |
55 * | |
56 * @see <a href="http://www.eclipse.org/swt/snippets/#textlayout">TextLayout, TextStyle snippets</a> | |
57 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: CustomControlExample, StyledText tab</a> | |
58 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
59 * | |
60 * @since 3.0 | |
61 */ | |
62 public final class TextLayout : Resource { | |
63 | |
64 static class StyleItem { | |
65 TextStyle style; | |
66 int start; | |
67 | |
68 public override String toString () { | |
69 return Format( "StyleItem {{{}, {}}", start, style ); | |
70 } | |
71 } | |
72 | |
73 Font font; | |
74 String text; | |
75 int ascent, descent; | |
76 int[] segments; | |
77 int[] tabs; | |
78 StyleItem[] styles; | |
79 PangoLayout* layout; | |
80 PangoContext* context; | |
81 PangoAttrList* attrList; | |
82 int[] invalidOffsets; | |
83 // LTR_MARK LEFT-TO-RIGHT MARK | |
84 // RTL_MARK RIGHT-TO-LEFT MARK | |
85 // ZWS ZERO WIDTH SPACE | |
86 // ZWNBS ZERO WIDTH NO-BREAK SPACE | |
87 static const dchar LTR_MARK = '\u200E'; // x"E2 80 8E" LEFT-TO-RIGHT MARK | |
88 static const dchar RTL_MARK = '\u200F'; // x"E2 80 8F" RIGHT-TO-LEFT MARK | |
89 static const dchar ZWS = '\u200B'; // x"E2 80 8B" ZERO WIDTH SPACE | |
90 static const dchar ZWNBS = '\uFEFF'; // x"EF BB BF" ZERO WIDTH NO-BREAK SPACE | |
91 static const String STR_LTR_MARK = "\u200E"; | |
92 static const String STR_RTL_MARK = "\u200F"; | |
93 static const String STR_ZWS = "\u200B"; | |
94 static const String STR_ZWNBS = "\uFEFF"; | |
95 | |
96 /** | |
97 * Constructs a new instance of this class on the given device. | |
98 * <p> | |
99 * You must dispose the text layout when it is no longer required. | |
100 * </p> | |
101 * | |
102 * @param device the device on which to allocate the text layout | |
103 * | |
104 * @exception IllegalArgumentException <ul> | |
105 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> | |
106 * </ul> | |
107 * | |
108 * @see #dispose() | |
109 */ | |
110 public this (Device device) { | |
111 super(device); | |
112 device = this.device; | |
113 context = OS.gdk_pango_context_get(); | |
114 if (context is null) SWT.error(SWT.ERROR_NO_HANDLES); | |
115 OS.pango_context_set_language(context, OS.gtk_get_default_language()); | |
116 OS.pango_context_set_base_dir(context, OS.PANGO_DIRECTION_LTR); | |
117 OS.gdk_pango_context_set_colormap(context, OS.gdk_colormap_get_system()); | |
118 layout = OS.pango_layout_new(context); | |
119 if (layout is null) SWT.error(SWT.ERROR_NO_HANDLES); | |
120 OS.pango_layout_set_font_description(layout, device.systemFont.handle); | |
121 OS.pango_layout_set_wrap(layout, OS.PANGO_WRAP_WORD_CHAR); | |
122 OS.pango_layout_set_tabs(layout, device.emptyTab); | |
123 if (OS.GTK_VERSION >= OS.buildVERSION(2, 4, 0)) { | |
124 OS.pango_layout_set_auto_dir(layout, false); | |
125 } | |
126 text = ""; | |
127 ascent = descent = -1; | |
128 styles = new StyleItem[2]; | |
129 styles[0] = new StyleItem(); | |
130 styles[1] = new StyleItem(); | |
131 init_(); | |
132 } | |
133 | |
134 void checkLayout() { | |
135 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); | |
136 } | |
137 | |
138 void computeRuns () { | |
139 if (attrList !is null) return; | |
140 String segmentsText = getSegmentsText(); | |
141 OS.pango_layout_set_text (layout, segmentsText.ptr, segmentsText.length); | |
142 if (styles.length is 2 && styles[0].style is null && ascent is -1 && descent is -1 && segments is null) return; | |
143 auto ptr = OS.pango_layout_get_text(layout); | |
144 attrList = OS.pango_attr_list_new(); | |
145 PangoAttribute* attribute; | |
146 char[] chars = null; | |
147 int segementsLength = segmentsText.length; | |
148 if ((ascent !is -1 || descent !is -1) && segementsLength > 0) { | |
149 PangoRectangle rect; | |
150 if (ascent !is -1) rect.y = -(ascent * OS.PANGO_SCALE); | |
151 rect.height = (Math.max(0, ascent) + Math.max(0, descent)) * OS.PANGO_SCALE; | |
152 int lineCount = OS.pango_layout_get_line_count(layout); | |
153 chars = new char[segementsLength + lineCount * 6/*2*/]; | |
154 int oldPos = 0, lineIndex = 0; | |
155 while (lineIndex < lineCount) { | |
156 auto line = OS.pango_layout_get_line(layout, lineIndex); | |
157 int bytePos = line.start_index; | |
158 /* Note: The length in bytes of ZWS and ZWNBS are both equals to 3 */ | |
159 int offset = lineIndex * 6; | |
160 PangoAttribute* attr = OS.pango_attr_shape_new (&rect, &rect); | |
161 attribute = attr; | |
162 attribute.start_index = bytePos + offset; | |
163 attribute.end_index = bytePos + offset + 3; | |
164 OS.pango_attr_list_insert(attrList, attr); | |
165 attr = OS.pango_attr_shape_new (&rect, &rect); | |
166 attribute = attr; | |
167 attribute.start_index = bytePos + offset + 3; | |
168 attribute.end_index = bytePos + offset + 6; | |
169 OS.pango_attr_list_insert(attrList, attr); | |
170 int pos = bytePos;//OS.g_utf8_pointer_to_offset(ptr, ptr + bytePos); | |
171 chars[pos + lineIndex * 6 +0 .. pos + lineIndex * 6 + 3] = STR_ZWS; | |
172 chars[pos + lineIndex * 6 +3 .. pos + lineIndex * 6 + 6] = STR_ZWNBS; | |
173 chars[ oldPos + lineIndex*6 .. oldPos + lineIndex*6 + pos - oldPos ] = | |
174 segmentsText[ oldPos .. pos ]; | |
175 oldPos = pos; | |
176 lineIndex++; | |
177 } | |
178 segmentsText.getChars(oldPos, segementsLength, chars, oldPos + lineIndex * 6); | |
54 | 179 auto buffer = chars;// Converter.wcsToMbcs(null, chars, false); |
25 | 180 |
181 OS.pango_layout_set_text (layout, buffer.ptr, buffer.length); | |
182 ptr = OS.pango_layout_get_text(layout); | |
183 } else { | |
184 chars = segmentsText.dup; | |
185 } | |
186 int offsetCount = 0; | |
187 { | |
188 int i = 0; | |
189 while( i < chars.length ){ | |
190 int incr; | |
191 dchar c = firstCodePoint( chars[ i .. $ ], incr ); | |
192 if (c is LTR_MARK || c is RTL_MARK || c is ZWNBS || c is ZWS) { | |
193 offsetCount+=3; | |
194 } | |
195 i += incr; | |
196 } | |
197 } | |
198 invalidOffsets = new int[offsetCount]; | |
199 offsetCount = 0; | |
200 { | |
201 int i = 0; | |
202 while( i < chars.length ){ | |
203 int incr; | |
204 dchar c = firstCodePoint( chars[ i .. $ ], incr ); | |
205 if (c is LTR_MARK || c is RTL_MARK || c is ZWNBS || c is ZWS) { | |
206 invalidOffsets[offsetCount++] = i; | |
207 invalidOffsets[offsetCount++] = i+1; | |
208 invalidOffsets[offsetCount++] = i+2; | |
209 } | |
210 i += incr; | |
211 } | |
212 } | |
47
65761bc28ab2
swt linux again compilable for d1.
Frank Benoit <benoit@tionex.de>
parents:
26
diff
changeset
|
213 int slen = OS.strlen(ptr); |
25 | 214 Font defaultFont = font !is null ? font : device.systemFont; |
215 for (int i = 0; i < styles.length - 1; i++) { | |
216 StyleItem styleItem = styles[i]; | |
217 TextStyle style = styleItem.style; | |
218 if (style is null) continue; | |
219 int start = translateOffset(styleItem.start); | |
220 int end = translateOffset(styles[i+1].start - 1); | |
221 int byteStart = start;//(OS.g_utf8_offset_to_pointer(ptr, start) - ptr); | |
222 int byteEnd = end+1;//(OS.g_utf8_offset_to_pointer(ptr, end + 1) - ptr); | |
223 byteStart = Math.min(byteStart, slen); | |
224 byteEnd = Math.min(byteEnd, slen); | |
225 Font font = style.font; | |
226 if (font !is null && !font.isDisposed() && !defaultFont.opEquals(font)) { | |
227 auto attr = OS.pango_attr_font_desc_new (font.handle); | |
228 attr.start_index = byteStart; | |
229 attr.end_index = byteEnd; | |
230 OS.pango_attr_list_insert(attrList, attr); | |
231 } | |
232 if (style.underline) { | |
233 int underlineStyle = OS.PANGO_UNDERLINE_NONE; | |
234 switch (style.underlineStyle) { | |
235 case SWT.UNDERLINE_SINGLE: | |
236 underlineStyle = OS.PANGO_UNDERLINE_SINGLE; | |
237 break; | |
238 case SWT.UNDERLINE_DOUBLE: | |
239 underlineStyle = OS.PANGO_UNDERLINE_DOUBLE; | |
240 break; | |
241 case SWT.UNDERLINE_SQUIGGLE: | |
242 case SWT.UNDERLINE_ERROR: | |
243 if (OS.GTK_VERSION >= OS.buildVERSION(2, 4, 0)) { | |
244 underlineStyle = OS.PANGO_UNDERLINE_ERROR; | |
245 } | |
246 break; | |
247 } | |
248 if (underlineStyle !is OS.PANGO_UNDERLINE_NONE && style.underlineColor is null) { | |
249 auto attr = OS.pango_attr_underline_new(underlineStyle); | |
250 attr.start_index = byteStart; | |
251 attr.end_index = byteEnd; | |
252 OS.pango_attr_list_insert(attrList, attr); | |
253 } | |
254 } | |
255 if (style.strikeout && style.strikeoutColor is null) { | |
256 auto attr = OS.pango_attr_strikethrough_new(true); | |
257 attr.start_index = byteStart; | |
258 attr.end_index = byteEnd; | |
259 OS.pango_attr_list_insert(attrList, attr); | |
260 } | |
261 Color foreground = style.foreground; | |
262 if (foreground !is null && !foreground.isDisposed()) { | |
263 GdkColor* fg = foreground.handle; | |
264 auto attr = OS.pango_attr_foreground_new(fg.red, fg.green, fg.blue); | |
265 attr.start_index = byteStart; | |
266 attr.end_index = byteEnd; | |
267 OS.pango_attr_list_insert(attrList, attr); | |
268 } | |
269 Color background = style.background; | |
270 if (background !is null && !background.isDisposed()) { | |
271 GdkColor* bg = background.handle; | |
272 auto attr = OS.pango_attr_background_new(bg.red, bg.green, bg.blue); | |
273 attr.start_index = byteStart; | |
274 attr.end_index = byteEnd; | |
275 OS.pango_attr_list_insert(attrList, attr); | |
276 } | |
277 GlyphMetrics metrics = style.metrics; | |
278 if (metrics !is null) { | |
279 PangoRectangle rect; | |
280 rect.y = -(metrics.ascent * OS.PANGO_SCALE); | |
281 rect.height = (metrics.ascent + metrics.descent) * OS.PANGO_SCALE; | |
282 rect.width = metrics.width * OS.PANGO_SCALE; | |
283 auto attr = OS.pango_attr_shape_new (&rect, &rect); | |
284 attr.start_index = byteStart; | |
285 attr.end_index = byteEnd; | |
286 OS.pango_attr_list_insert(attrList, attr); | |
287 } | |
288 int rise = style.rise; | |
289 if (rise !is 0) { | |
290 auto attr = OS.pango_attr_rise_new (rise * OS.PANGO_SCALE); | |
291 attr.start_index = byteStart; | |
292 attr.end_index = byteEnd; | |
293 OS.pango_attr_list_insert(attrList, attr); | |
294 } | |
295 } | |
296 OS.pango_layout_set_attributes(layout, attrList); | |
297 } | |
298 | |
299 int[] computePolyline(int left, int top, int right, int bottom) { | |
300 int height = bottom - top; // can be any number | |
301 int width = 2 * height; // must be even | |
302 int peaks = Compatibility.ceil(right - left, width); | |
303 if (peaks is 0 && right - left > 2) { | |
304 peaks = 1; | |
305 } | |
306 int length_ = ((2 * peaks) + 1) * 2; | |
307 if (length_ < 0) return new int[0]; | |
308 | |
309 int[] coordinates = new int[length_]; | |
310 for (int i = 0; i < peaks; i++) { | |
311 int index = 4 * i; | |
312 coordinates[index] = left + (width * i); | |
313 coordinates[index+1] = bottom; | |
314 coordinates[index+2] = coordinates[index] + width / 2; | |
315 coordinates[index+3] = top; | |
316 } | |
317 coordinates[length_-2] = left + (width * peaks); | |
318 coordinates[length_-1] = bottom; | |
319 return coordinates; | |
320 } | |
321 | |
322 void destroy() { | |
323 font = null; | |
324 text = null; | |
325 styles = null; | |
326 freeRuns(); | |
327 if (layout !is null) OS.g_object_unref(layout); | |
328 layout = null; | |
329 if (context !is null) OS.g_object_unref(context); | |
330 context = null; | |
331 } | |
332 | |
333 /** | |
334 * Draws the receiver's text using the specified GC at the specified | |
335 * point. | |
336 * | |
337 * @param gc the GC to draw | |
338 * @param x the x coordinate of the top left corner of the rectangular area where the text is to be drawn | |
339 * @param y the y coordinate of the top left corner of the rectangular area where the text is to be drawn | |
340 * | |
341 * @exception SWTException <ul> | |
342 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
343 * </ul> | |
344 * @exception IllegalArgumentException <ul> | |
345 * <li>ERROR_NULL_ARGUMENT - if the gc is null</li> | |
346 * </ul> | |
347 */ | |
348 public void draw(GC gc, int x, int y) { | |
349 draw(gc, x, y, -1, -1, null, null); | |
350 } | |
351 | |
352 /** | |
353 * Draws the receiver's text using the specified GC at the specified | |
354 * point. | |
355 * | |
356 * @param gc the GC to draw | |
357 * @param x the x coordinate of the top left corner of the rectangular area where the text is to be drawn | |
358 * @param y the y coordinate of the top left corner of the rectangular area where the text is to be drawn | |
359 * @param selectionStart the offset where the selections starts, or -1 indicating no selection | |
360 * @param selectionEnd the offset where the selections ends, or -1 indicating no selection | |
361 * @param selectionForeground selection foreground, or NULL to use the system default color | |
362 * @param selectionBackground selection background, or NULL to use the system default color | |
363 * | |
364 * @exception SWTException <ul> | |
365 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
366 * </ul> | |
367 * @exception IllegalArgumentException <ul> | |
368 * <li>ERROR_NULL_ARGUMENT - if the gc is null</li> | |
369 * </ul> | |
370 */ | |
371 public void draw(GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) { | |
372 draw(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground, 0); | |
373 } | |
374 | |
375 /** | |
376 * Draws the receiver's text using the specified GC at the specified | |
377 * point. | |
378 * <p> | |
379 * The parameter <code>flags</code> can include one of <code>SWT.DELIMITER_SELECTION</code> | |
380 * or <code>SWT.FULL_SELECTION</code> to specify the selection behavior on all lines except | |
381 * for the last line, and can also include <code>SWT.LAST_LINE_SELECTION</code> to extend | |
382 * the specified selection behavior to the last line. | |
383 * </p> | |
384 * @param gc the GC to draw | |
385 * @param x the x coordinate of the top left corner of the rectangular area where the text is to be drawn | |
386 * @param y the y coordinate of the top left corner of the rectangular area where the text is to be drawn | |
387 * @param selectionStart the offset where the selections starts, or -1 indicating no selection | |
388 * @param selectionEnd the offset where the selections ends, or -1 indicating no selection | |
389 * @param selectionForeground selection foreground, or NULL to use the system default color | |
390 * @param selectionBackground selection background, or NULL to use the system default color | |
391 * @param flags drawing options | |
392 * | |
393 * @exception SWTException <ul> | |
394 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
395 * </ul> | |
396 * @exception IllegalArgumentException <ul> | |
397 * <li>ERROR_NULL_ARGUMENT - if the gc is null</li> | |
398 * </ul> | |
399 * | |
400 * @since 3.3 | |
401 */ | |
402 public void draw(GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground, int flags) { | |
403 checkLayout (); | |
404 computeRuns(); | |
405 if (gc is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
406 if (gc.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
407 if (selectionForeground !is null && selectionForeground.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
408 if (selectionBackground !is null && selectionBackground.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
409 gc.checkGC(GC.FOREGROUND); | |
410 int length_ = text.length; | |
411 bool hasSelection = selectionStart <= selectionEnd && selectionStart !is -1 && selectionEnd !is -1; | |
412 GCData data = gc.data; | |
413 auto cairo = data.cairo; | |
414 if (flags !is 0 && (hasSelection || (flags & SWT.LAST_LINE_SELECTION) !is 0)) { | |
415 PangoLogAttr* attrs; | |
416 int nAttrs; | |
417 PangoLogAttr* logAttr = new PangoLogAttr(); | |
418 PangoRectangle rect; | |
419 int lineCount = OS.pango_layout_get_line_count(layout); | |
420 auto ptr = OS.pango_layout_get_text(layout); | |
421 auto iter = OS.pango_layout_get_iter(layout); | |
422 if (selectionBackground is null) selectionBackground = device.getSystemColor(SWT.COLOR_LIST_SELECTION); | |
423 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
424 Cairo.cairo_save(cairo); | |
425 GdkColor* color = selectionBackground.handle; | |
426 Cairo.cairo_set_source_rgba(cairo, (color.red & 0xFFFF) / cast(float)0xFFFF, (color.green & 0xFFFF) / cast(float)0xFFFF, (color.blue & 0xFFFF) / cast(float)0xFFFF, data.alpha / cast(float)0xFF); | |
427 } else { | |
428 OS.gdk_gc_set_foreground(gc.handle, selectionBackground.handle); | |
429 } | |
430 int lineIndex = 0; | |
431 do { | |
432 int lineEnd; | |
433 OS.pango_layout_iter_get_line_extents(iter, null, &rect); | |
434 if (OS.pango_layout_iter_next_line(iter)) { | |
435 int bytePos = OS.pango_layout_iter_get_index(iter); | |
436 lineEnd = bytePos;//OS.g_utf8_pointer_to_offset(ptr, ptr + bytePos); | |
437 } else { | |
438 lineEnd = OS.g_utf8_strlen(ptr, -1); | |
439 } | |
440 bool extent = false; | |
441 if (lineIndex is lineCount - 1 && (flags & SWT.LAST_LINE_SELECTION) !is 0) { | |
442 extent = true; | |
443 } else { | |
444 if (attrs is null) OS.pango_layout_get_log_attrs(layout, &attrs, &nAttrs); | |
445 *logAttr = attrs[lineEnd]; | |
446 if (!( logAttr.bitfield0 & 0x01 /* PangoLogAttr.is_line_break is Bit0 */)) { | |
447 if (selectionStart <= lineEnd && lineEnd <= selectionEnd) extent = true; | |
448 } else { | |
449 if (selectionStart <= lineEnd && lineEnd < selectionEnd && (flags & SWT.FULL_SELECTION) !is 0) { | |
450 extent = true; | |
451 } | |
452 } | |
453 } | |
454 if (extent) { | |
455 int lineX = x + OS.PANGO_PIXELS(rect.x) + OS.PANGO_PIXELS(rect.width); | |
456 int lineY = y + OS.PANGO_PIXELS(rect.y); | |
457 int height = OS.PANGO_PIXELS(rect.height); | |
458 if (ascent !is -1 && descent !is -1) { | |
459 height = Math.max (height, ascent + descent); | |
460 } | |
461 int width = (flags & SWT.FULL_SELECTION) !is 0 ? 0x7fffffff : height / 3; | |
462 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
463 Cairo.cairo_rectangle(cairo, lineX, lineY, width, height); | |
464 Cairo.cairo_fill(cairo); | |
465 } else { | |
466 OS.gdk_draw_rectangle(data.drawable, gc.handle, 1, lineX, lineY, width, height); | |
467 } | |
468 } | |
469 lineIndex++; | |
470 } while (lineIndex < lineCount); | |
471 OS.pango_layout_iter_free(iter); | |
472 if (attrs !is null) OS.g_free(attrs); | |
473 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
474 Cairo.cairo_restore(cairo); | |
475 } else { | |
476 OS.gdk_gc_set_foreground(gc.handle, data.foreground); | |
477 } | |
478 } | |
479 if (length_ is 0) return; | |
480 if (!hasSelection) { | |
481 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
482 if ((data.style & SWT.MIRRORED) !is 0) { | |
483 Cairo.cairo_save(cairo); | |
484 Cairo.cairo_scale(cairo, -1, 1); | |
485 Cairo.cairo_translate(cairo, -2 * x - width(), 0); | |
486 } | |
487 Cairo.cairo_move_to(cairo, x, y); | |
488 OS.pango_cairo_show_layout(cairo, layout); | |
489 drawBorder(gc, x, y, null); | |
490 if ((data.style & SWT.MIRRORED) !is 0) { | |
491 Cairo.cairo_restore(cairo); | |
492 } | |
493 } else { | |
494 OS.gdk_draw_layout(data.drawable, gc.handle, x, y, layout); | |
495 drawBorder(gc, x, y, null); | |
496 } | |
497 } else { | |
498 selectionStart = Math.min(Math.max(0, selectionStart), length_ - 1); | |
499 selectionEnd = Math.min(Math.max(0, selectionEnd), length_ - 1); | |
500 length_ = OS.g_utf8_strlen(OS.pango_layout_get_text(layout), -1); | |
501 selectionStart = translateOffset(selectionStart); | |
502 selectionEnd = translateOffset(selectionEnd); | |
503 if (selectionForeground is null) selectionForeground = device.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT); | |
504 if (selectionBackground is null) selectionBackground = device.getSystemColor(SWT.COLOR_LIST_SELECTION); | |
505 bool fullSelection = selectionStart is 0 && selectionEnd is length_ - 1; | |
506 if (fullSelection) { | |
507 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
508 auto ptr = OS.pango_layout_get_text(layout); | |
509 if ((data.style & SWT.MIRRORED) !is 0) { | |
510 Cairo.cairo_save(cairo); | |
511 Cairo.cairo_scale(cairo, -1, 1); | |
512 Cairo.cairo_translate(cairo, -2 * x - width(), 0); | |
513 } | |
514 drawWithCairo(gc, x, y, 0, OS.strlen(ptr), fullSelection, selectionForeground.handle, selectionBackground.handle); | |
515 if ((data.style & SWT.MIRRORED) !is 0) { | |
516 Cairo.cairo_restore(cairo); | |
517 } | |
518 } else { | |
519 OS.gdk_draw_layout_with_colors(data.drawable, gc.handle, x, y, layout, selectionForeground.handle, selectionBackground.handle); | |
520 drawBorder(gc, x, y, selectionForeground.handle); | |
521 } | |
522 } else { | |
523 auto ptr = OS.pango_layout_get_text(layout); | |
524 int byteSelStart = selectionStart;//(OS.g_utf8_offset_to_pointer(ptr, selectionStart) - ptr); | |
525 int byteSelEnd = selectionEnd + 1;//(OS.g_utf8_offset_to_pointer(ptr, selectionEnd + 1) - ptr); | |
47
65761bc28ab2
swt linux again compilable for d1.
Frank Benoit <benoit@tionex.de>
parents:
26
diff
changeset
|
526 int slen = OS.strlen(ptr); |
25 | 527 byteSelStart = Math.min(byteSelStart, slen); |
528 byteSelEnd = Math.min(byteSelEnd, slen); | |
529 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
530 if ((data.style & SWT.MIRRORED) !is 0) { | |
531 Cairo.cairo_save(cairo); | |
532 Cairo.cairo_scale(cairo, -1, 1); | |
533 Cairo.cairo_translate(cairo, -2 * x - width(), 0); | |
534 } | |
535 drawWithCairo(gc, x, y, byteSelStart, byteSelEnd, fullSelection, selectionForeground.handle, selectionBackground.handle); | |
536 if ((data.style & SWT.MIRRORED) !is 0) { | |
537 Cairo.cairo_restore(cairo); | |
538 } | |
539 } else { | |
540 Region clipping = new Region(); | |
541 gc.getClipping(clipping); | |
542 OS.gdk_draw_layout(data.drawable, gc.handle, x, y, layout); | |
543 drawBorder(gc, x, y, null); | |
544 int[] ranges = [byteSelStart, byteSelEnd]; | |
545 auto rgn = OS.gdk_pango_layout_get_clip_region(layout, x, y, ranges.ptr, ranges.length / 2); | |
546 if (rgn !is null) { | |
547 OS.gdk_gc_set_clip_region(gc.handle, rgn); | |
548 OS.gdk_region_destroy(rgn); | |
549 } | |
550 OS.gdk_draw_layout_with_colors(data.drawable, gc.handle, x, y, layout, selectionForeground.handle, selectionBackground.handle); | |
551 drawBorder(gc, x, y, selectionForeground.handle); | |
552 gc.setClipping(clipping); | |
553 clipping.dispose(); | |
554 } | |
555 } | |
556 } | |
557 } | |
558 | |
559 void drawWithCairo(GC gc, int x, int y, int start, int end, bool fullSelection, GdkColor* fg, GdkColor* bg) { | |
560 GCData data = gc.data; | |
561 cairo_t* cairo = data.cairo; | |
562 Cairo.cairo_save(cairo); | |
563 if (!fullSelection) { | |
564 Cairo.cairo_move_to(cairo, x, y); | |
565 OS.pango_cairo_show_layout(cairo, layout); | |
566 drawBorder(gc, x, y, null); | |
567 } | |
568 int[] ranges = [start, end]; | |
569 auto rgn = OS.gdk_pango_layout_get_clip_region(layout, x, y, ranges.ptr, ranges.length / 2); | |
570 if (rgn !is null) { | |
571 OS.gdk_cairo_region(cairo, rgn); | |
572 Cairo.cairo_clip(cairo); | |
573 Cairo.cairo_set_source_rgba(cairo, (bg.red & 0xFFFF) / cast(float)0xFFFF, (bg.green & 0xFFFF) / cast(float)0xFFFF, (bg.blue & 0xFFFF) / cast(float)0xFFFF, data.alpha / cast(float)0xFF); | |
574 Cairo.cairo_paint(cairo); | |
575 OS.gdk_region_destroy(rgn); | |
576 } | |
577 Cairo.cairo_set_source_rgba(cairo, (fg.red & 0xFFFF) / cast(float)0xFFFF, (fg.green & 0xFFFF) / cast(float)0xFFFF, (fg.blue & 0xFFFF) / cast(float)0xFFFF, data.alpha / cast(float)0xFF); | |
578 Cairo.cairo_move_to(cairo, x, y); | |
579 OS.pango_cairo_show_layout(cairo, layout); | |
580 drawBorder(gc, x, y, fg); | |
581 Cairo.cairo_restore(cairo); | |
582 } | |
583 | |
584 void drawBorder(GC gc, int x, int y, GdkColor* selectionColor) { | |
585 GCData data = gc.data; | |
586 auto cairo = data.cairo; | |
587 auto gdkGC = gc.handle; | |
588 auto ptr = OS.pango_layout_get_text(layout); | |
589 GdkGCValues* gcValues = null; | |
590 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
591 Cairo.cairo_save(cairo); | |
592 } | |
593 for (int i = 0; i < styles.length - 1; i++) { | |
594 TextStyle style = styles[i].style; | |
595 if (style is null) continue; | |
596 | |
597 bool drawBorder = style.borderStyle !is SWT.NONE; | |
598 if (drawBorder && !style.isAdherentBorder(styles[i+1].style)) { | |
599 int start = styles[i].start; | |
600 for (int j = i; j > 0 && style.isAdherentBorder(styles[j-1].style); j--) { | |
601 start = styles[j - 1].start; | |
602 } | |
603 start = translateOffset(start); | |
604 int end = translateOffset(styles[i+1].start - 1); | |
605 int byteStart = cast(int)/*64*/(OS.g_utf8_offset_to_pointer(ptr, start) - ptr); | |
606 int byteEnd = cast(int)/*64*/(OS.g_utf8_offset_to_pointer(ptr, end + 1) - ptr); | |
607 int[] ranges = [byteStart, byteEnd]; | |
608 auto rgn = OS.gdk_pango_layout_get_clip_region(layout, x, y, ranges.ptr, ranges.length / 2); | |
609 if (rgn !is null) { | |
610 int nRects; | |
611 GdkRectangle* rects; | |
612 OS.gdk_region_get_rectangles(rgn, &rects, &nRects); | |
613 GdkRectangle rect; | |
614 GdkColor* color = null; | |
615 if (color is null && style.borderColor !is null) color = style.borderColor.handle; | |
616 if (color is null && selectionColor !is null) color = selectionColor; | |
617 if (color is null && style.foreground !is null) color = style.foreground.handle; | |
618 if (color is null) color = data.foreground; | |
619 int width = 1; | |
620 float[] dashes = null; | |
621 switch (style.borderStyle) { | |
622 case SWT.BORDER_SOLID: break; | |
623 case SWT.BORDER_DASH: dashes = width !is 0 ? GC.LINE_DASH : GC.LINE_DASH_ZERO; break; | |
624 case SWT.BORDER_DOT: dashes = width !is 0 ? GC.LINE_DOT : GC.LINE_DOT_ZERO; break; | |
625 } | |
626 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
627 Cairo.cairo_set_source_rgba(cairo, (color.red & 0xFFFF) / cast(float)0xFFFF, (color.green & 0xFFFF) / cast(float)0xFFFF, (color.blue & 0xFFFF) / cast(float)0xFFFF, data.alpha / cast(float)0xFF); | |
628 Cairo.cairo_set_line_width(cairo, width); | |
629 if (dashes !is null) { | |
630 double[] cairoDashes = new double[dashes.length]; | |
631 for (int j = 0; j < cairoDashes.length; j++) { | |
632 cairoDashes[j] = width is 0 || data.lineStyle is SWT.LINE_CUSTOM ? dashes[j] : dashes[j] * width; | |
633 } | |
634 Cairo.cairo_set_dash(cairo, cairoDashes.ptr, cairoDashes.length, 0); | |
635 } else { | |
636 Cairo.cairo_set_dash(cairo, null, 0, 0); | |
637 } | |
638 for (int j=0; j<nRects; j++) { | |
639 rect = rects[j]; | |
640 Cairo.cairo_rectangle(cairo, rect.x + 0.5, rect.y + 0.5, rect.width - 1, rect.height - 1); | |
641 } | |
642 Cairo.cairo_stroke(cairo); | |
643 } else { | |
644 if (gcValues is null) { | |
645 gcValues = new GdkGCValues(); | |
646 OS.gdk_gc_get_values(gdkGC, gcValues); | |
647 } | |
648 OS.gdk_gc_set_foreground(gdkGC, color); | |
649 int cap_style = OS.GDK_CAP_BUTT; | |
650 int join_style = OS.GDK_JOIN_MITER; | |
651 int line_style = 0; | |
652 if (dashes !is null) { | |
653 byte[] dash_list = new byte[dashes.length]; | |
654 for (int j = 0; j < dash_list.length; j++) { | |
655 dash_list[j] = cast(byte)(width is 0 || data.lineStyle is SWT.LINE_CUSTOM ? dashes[j] : dashes[j] * width); | |
656 } | |
657 OS.gdk_gc_set_dashes(gdkGC, 0, cast(char*)dash_list.ptr, dash_list.length); | |
658 line_style = OS.GDK_LINE_ON_OFF_DASH; | |
659 } else { | |
660 line_style = OS.GDK_LINE_SOLID; | |
661 } | |
662 OS.gdk_gc_set_line_attributes(gdkGC, width, line_style, cap_style, join_style); | |
663 for (int j=0; j<nRects; j++) { | |
664 rect = rects[j]; | |
665 OS.gdk_draw_rectangle(data.drawable, gdkGC, 0, rect.x, rect.y, rect.width - 1, rect.height - 1); | |
666 } | |
667 } | |
668 if (rects !is null) OS.g_free(rects); | |
669 OS.gdk_region_destroy(rgn); | |
670 } | |
671 } | |
672 | |
673 bool drawUnderline = false; | |
674 if (style.underline && style.underlineColor !is null) drawUnderline = true; | |
675 if (style.underline && (style.underlineStyle is SWT.UNDERLINE_ERROR || style.underlineStyle is SWT.UNDERLINE_SQUIGGLE)&& OS.GTK_VERSION < OS.buildVERSION(2, 4, 0)) drawUnderline = true; | |
676 if (drawUnderline && !style.isAdherentUnderline(styles[i+1].style)) { | |
677 int start = styles[i].start; | |
678 for (int j = i; j > 0 && style.isAdherentUnderline(styles[j-1].style); j--) { | |
679 start = styles[j - 1].start; | |
680 } | |
681 start = translateOffset(start); | |
682 int end = translateOffset(styles[i+1].start - 1); | |
683 int byteStart = cast(int)/*64*/(OS.g_utf8_offset_to_pointer(ptr, start) - ptr); | |
684 int byteEnd = cast(int)/*64*/(OS.g_utf8_offset_to_pointer(ptr, end + 1) - ptr); | |
685 int[] ranges = [byteStart, byteEnd]; | |
686 auto rgn = OS.gdk_pango_layout_get_clip_region(layout, x, y, ranges.ptr, ranges.length / 2); | |
687 if (rgn !is null) { | |
688 int nRects; | |
689 GdkRectangle* rects; | |
690 OS.gdk_region_get_rectangles(rgn, &rects, &nRects); | |
691 GdkRectangle rect; | |
692 GdkColor* color = null; | |
693 if (color is null && style.underlineColor !is null) color = style.underlineColor.handle; | |
694 if (color is null && selectionColor !is null) color = selectionColor; | |
695 if (color is null && style.foreground !is null) color = style.foreground.handle; | |
696 if (color is null) color = data.foreground; | |
697 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
698 Cairo.cairo_set_source_rgba(cairo, (color.red & 0xFFFF) / cast(float)0xFFFF, (color.green & 0xFFFF) / cast(float)0xFFFF, (color.blue & 0xFFFF) / cast(float)0xFFFF, data.alpha / cast(float)0xFF); | |
699 } else { | |
700 if (gcValues is null) { | |
701 gcValues = new GdkGCValues(); | |
702 OS.gdk_gc_get_values(gdkGC, gcValues); | |
703 } | |
704 OS.gdk_gc_set_foreground(gdkGC, color); | |
705 } | |
706 int underlinePosition = -1; | |
707 int underlineThickness = 1; | |
708 if (OS.GTK_VERSION >= OS.buildVERSION(2, 6, 0)) { | |
709 Font font = style.font; | |
710 if (font is null) font = this.font; | |
711 if (font is null) font = device.systemFont; | |
712 auto lang = OS.pango_context_get_language(context); | |
713 auto metrics = OS.pango_context_get_metrics(context, font.handle, lang); | |
714 underlinePosition = OS.PANGO_PIXELS(OS.pango_font_metrics_get_underline_position(metrics)); | |
715 underlineThickness = OS.PANGO_PIXELS(OS.pango_font_metrics_get_underline_thickness(metrics)); | |
716 OS.pango_font_metrics_unref(metrics); | |
717 } | |
718 for (int j=0; j<nRects; j++) { | |
719 rect = rects[j]; | |
720 int offset = getOffset(rect.x - x, rect.y - y, null); | |
721 int lineIndex = getLineIndex(offset); | |
722 FontMetrics metrics = getLineMetrics(lineIndex); | |
723 int underlineY = rect.y + metrics.ascent - underlinePosition - style.rise; | |
724 switch (style.underlineStyle) { | |
725 case SWT.UNDERLINE_SQUIGGLE: | |
726 case SWT.UNDERLINE_ERROR: { | |
727 int squigglyThickness = underlineThickness; | |
728 int squigglyHeight = 2 * squigglyThickness; | |
729 int squigglyY = Math.min(underlineY, rect.y + rect.height - squigglyHeight - 1); | |
730 int[] points = computePolyline(rect.x, squigglyY, rect.x + rect.width, squigglyY + squigglyHeight); | |
731 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
732 Cairo.cairo_set_line_width(cairo, squigglyThickness); | |
733 Cairo.cairo_set_line_cap(cairo, Cairo.CAIRO_LINE_CAP_BUTT); | |
734 Cairo.cairo_set_line_join(cairo, Cairo.CAIRO_LINE_JOIN_MITER); | |
735 if (points.length > 0) { | |
736 double xOffset = 0.5, yOffset = 0.5; | |
737 Cairo.cairo_move_to(cairo, points[0] + xOffset, points[1] + yOffset); | |
738 for (int k = 2; k < points.length; k += 2) { | |
739 Cairo.cairo_line_to(cairo, points[k] + xOffset, points[k + 1] + yOffset); | |
740 } | |
741 Cairo.cairo_stroke(cairo); | |
742 } | |
743 } else { | |
744 OS.gdk_gc_set_line_attributes(gdkGC, squigglyThickness, OS.GDK_LINE_SOLID, OS.GDK_CAP_BUTT, OS.GDK_JOIN_MITER); | |
745 OS.gdk_draw_lines(data.drawable, gdkGC, cast(GdkPoint*)points.ptr, points.length / 2); | |
746 } | |
747 break; | |
748 } | |
749 case SWT.UNDERLINE_DOUBLE: | |
750 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
751 Cairo.cairo_rectangle(cairo, rect.x, underlineY + underlineThickness * 2, rect.width, underlineThickness); | |
752 Cairo.cairo_fill(cairo); | |
753 } else { | |
754 OS.gdk_draw_rectangle(data.drawable, gdkGC, 1, rect.x, underlineY + underlineThickness * 2, rect.width, underlineThickness); | |
755 } | |
756 //FALLTHROUGH | |
757 case SWT.UNDERLINE_SINGLE: | |
758 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
759 Cairo.cairo_rectangle(cairo, rect.x, underlineY, rect.width, underlineThickness); | |
760 Cairo.cairo_fill(cairo); | |
761 } else { | |
762 OS.gdk_draw_rectangle(data.drawable, gdkGC, 1, rect.x, underlineY, rect.width, underlineThickness); | |
763 } | |
764 break; | |
765 } | |
766 } | |
767 if (rects !is null) OS.g_free(rects); | |
768 OS.gdk_region_destroy(rgn); | |
769 } | |
770 } | |
771 | |
772 bool drawStrikeout = false; | |
773 if (style.strikeout && style.strikeoutColor !is null) drawStrikeout = true; | |
774 if (drawStrikeout && !style.isAdherentStrikeout(styles[i+1].style)) { | |
775 int start = styles[i].start; | |
776 for (int j = i; j > 0 && style.isAdherentStrikeout(styles[j-1].style); j--) { | |
777 start = styles[j - 1].start; | |
778 } | |
779 start = translateOffset(start); | |
780 int end = translateOffset(styles[i+1].start - 1); | |
781 int byteStart = cast(int)/*64*/(OS.g_utf8_offset_to_pointer(ptr, start) - ptr); | |
782 int byteEnd = cast(int)/*64*/(OS.g_utf8_offset_to_pointer(ptr, end + 1) - ptr); | |
783 int[] ranges = [byteStart, byteEnd]; | |
784 auto rgn = OS.gdk_pango_layout_get_clip_region(layout, x, y, ranges.ptr, ranges.length / 2); | |
785 if (rgn !is null) { | |
786 int nRects; | |
787 GdkRectangle* rects; | |
788 OS.gdk_region_get_rectangles(rgn, &rects, &nRects); | |
789 GdkRectangle rect; | |
790 GdkColor* color = null; | |
791 if (color is null && style.strikeoutColor !is null) color = style.strikeoutColor.handle; | |
792 if (color is null && selectionColor !is null) color = selectionColor; | |
793 if (color is null && style.foreground !is null) color = style.foreground.handle; | |
794 if (color is null) color = data.foreground; | |
795 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
796 Cairo.cairo_set_source_rgba(cairo, (color.red & 0xFFFF) / cast(float)0xFFFF, (color.green & 0xFFFF) / cast(float)0xFFFF, (color.blue & 0xFFFF) / cast(float)0xFFFF, data.alpha / cast(float)0xFF); | |
797 } else { | |
798 if (gcValues is null) { | |
799 gcValues = new GdkGCValues(); | |
800 OS.gdk_gc_get_values(gdkGC, gcValues); | |
801 } | |
802 OS.gdk_gc_set_foreground(gdkGC, color); | |
803 } | |
804 int strikeoutPosition = -1; | |
805 int strikeoutThickness = 1; | |
806 if (OS.GTK_VERSION >= OS.buildVERSION(2, 6, 0)) { | |
807 Font font = style.font; | |
808 if (font is null) font = this.font; | |
809 if (font is null) font = device.systemFont; | |
810 auto lang = OS.pango_context_get_language(context); | |
811 auto metrics = OS.pango_context_get_metrics(context, font.handle, lang); | |
812 strikeoutPosition = OS.PANGO_PIXELS(OS.pango_font_metrics_get_strikethrough_position(metrics)); | |
813 strikeoutThickness = OS.PANGO_PIXELS(OS.pango_font_metrics_get_strikethrough_thickness(metrics)); | |
814 OS.pango_font_metrics_unref(metrics); | |
815 } | |
816 for (int j=0; j<nRects; j++) { | |
817 rect = rects[j]; | |
818 int strikeoutY = rect.y + rect.height / 2 - style.rise; | |
819 if (OS.GTK_VERSION >= OS.buildVERSION(2, 6, 0)) { | |
820 int offset = getOffset(rect.x - x, rect.y - y, null); | |
821 int lineIndex = getLineIndex(offset); | |
822 FontMetrics metrics = getLineMetrics(lineIndex); | |
823 strikeoutY = rect.y + metrics.ascent - strikeoutPosition - style.rise; | |
824 } | |
825 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
826 Cairo.cairo_rectangle(cairo, rect.x, strikeoutY, rect.width, strikeoutThickness); | |
827 Cairo.cairo_fill(cairo); | |
828 } else { | |
829 OS.gdk_draw_rectangle(data.drawable, gdkGC, 1, rect.x, strikeoutY, rect.width, strikeoutThickness); | |
830 } | |
831 } | |
832 if (rects !is null) OS.g_free(rects); | |
833 OS.gdk_region_destroy(rgn); | |
834 } | |
835 } | |
836 } | |
837 if (gcValues !is null) { | |
838 int mask = OS.GDK_GC_FOREGROUND | OS.GDK_GC_LINE_WIDTH | OS.GDK_GC_LINE_STYLE | OS.GDK_GC_CAP_STYLE | OS.GDK_GC_JOIN_STYLE; | |
839 OS.gdk_gc_set_values(gdkGC, gcValues, mask); | |
840 data.state &= ~GC.LINE_STYLE; | |
841 } | |
842 if (cairo !is null && OS.GTK_VERSION >= OS.buildVERSION(2, 8, 0)) { | |
843 Cairo.cairo_restore(cairo); | |
844 } | |
845 } | |
846 | |
847 void freeRuns() { | |
848 if (attrList is null) return; | |
849 OS.pango_layout_set_attributes(layout, null ); | |
850 OS.pango_attr_list_unref(attrList); | |
851 attrList = null; | |
852 invalidOffsets = null; | |
853 } | |
854 | |
855 /** | |
856 * Returns the receiver's horizontal text alignment, which will be one | |
857 * of <code>SWT.LEFT</code>, <code>SWT.CENTER</code> or | |
858 * <code>SWT.RIGHT</code>. | |
859 * | |
860 * @return the alignment used to positioned text horizontally | |
861 * | |
862 * @exception SWTException <ul> | |
863 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
864 * </ul> | |
865 */ | |
866 public int getAlignment() { | |
867 checkLayout(); | |
868 auto alignment = OS.pango_layout_get_alignment(layout); | |
869 bool rtl = OS.pango_context_get_base_dir(context) is OS.PANGO_DIRECTION_RTL; | |
870 switch ( cast(int)alignment) { | |
871 case OS.PANGO_ALIGN_LEFT: return rtl ? SWT.RIGHT : SWT.LEFT; | |
872 case OS.PANGO_ALIGN_RIGHT: return rtl ? SWT.LEFT : SWT.RIGHT; | |
873 default: | |
874 } | |
875 return SWT.CENTER; | |
876 } | |
877 | |
878 /** | |
879 * Returns the ascent of the receiver. | |
880 * | |
881 * @return the ascent | |
882 * | |
883 * @exception SWTException <ul> | |
884 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
885 * </ul> | |
886 * | |
887 * @see #getDescent() | |
888 * @see #setDescent(int) | |
889 * @see #setAscent(int) | |
890 * @see #getLineMetrics(int) | |
891 */ | |
892 public int getAscent () { | |
893 checkLayout(); | |
894 return ascent; | |
895 } | |
896 | |
897 /** | |
898 * Returns the bounds of the receiver. The width returned is either the | |
899 * width of the longest line or the width set using {@link TextLayout#setWidth(int)}. | |
900 * To obtain the text bounds of a line use {@link TextLayout#getLineBounds(int)}. | |
901 * | |
902 * @return the bounds of the receiver | |
903 * | |
904 * @exception SWTException <ul> | |
905 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
906 * </ul> | |
907 * | |
908 * @see #setWidth(int) | |
909 * @see #getLineBounds(int) | |
910 */ | |
911 public Rectangle getBounds() { | |
912 checkLayout(); | |
913 computeRuns(); | |
914 int w, h; | |
915 OS.pango_layout_get_size(layout, &w, &h); | |
916 int wrapWidth = OS.pango_layout_get_width(layout); | |
917 w = wrapWidth !is -1 ? wrapWidth : w + OS.pango_layout_get_indent(layout); | |
918 int width = OS.PANGO_PIXELS(w); | |
919 int height = OS.PANGO_PIXELS(h); | |
920 if (ascent !is -1 && descent !is -1) { | |
921 height = Math.max (height, ascent + descent); | |
922 } | |
923 return new Rectangle(0, 0, width, height); | |
924 } | |
925 | |
926 /** | |
927 * Returns the bounds for the specified range of characters. The | |
928 * bounds is the smallest rectangle that encompasses all characters | |
929 * in the range. The start and end offsets are inclusive and will be | |
930 * clamped if out of range. | |
931 * | |
932 * @param start the start offset | |
933 * @param end the end offset | |
934 * @return the bounds of the character range | |
935 * | |
936 * @exception SWTException <ul> | |
937 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
938 * </ul> | |
939 */ | |
940 public Rectangle getBounds(int start, int end) { | |
941 checkLayout(); | |
942 computeRuns(); | |
943 int length_ = text.length; | |
944 if (length_ is 0) return new Rectangle(0, 0, 0, 0); | |
945 if (start > end) return new Rectangle(0, 0, 0, 0); | |
946 start = Math.min(Math.max(0, start), length_ - 1); | |
947 end = Math.min(Math.max(0, end), length_ - 1); | |
948 start = translateOffset(start); | |
949 end = translateOffset(end); | |
950 auto ptr = OS.pango_layout_get_text(layout); | |
951 auto cont = fromStringz(ptr); | |
952 start = cont.utf8AdjustOffset( start ); | |
953 end = cont.utf8AdjustOffset( end ); | |
954 int incr = 1; | |
955 if( end < cont.length ){ | |
956 incr = cont.getRelativeCodePointOffset( end, 1 ); | |
957 } | |
958 int byteStart = start;//(OS.g_utf8_offset_to_pointer (ptr, start) - ptr); | |
959 int byteEnd = end + incr;//(OS.g_utf8_offset_to_pointer (ptr, end + 1) - ptr); | |
47
65761bc28ab2
swt linux again compilable for d1.
Frank Benoit <benoit@tionex.de>
parents:
26
diff
changeset
|
960 int slen = OS.strlen(ptr); |
25 | 961 byteStart = Math.min(byteStart, slen); |
962 byteEnd = Math.min(byteEnd, slen); | |
963 int[] ranges = [byteStart, byteEnd]; | |
964 auto clipRegion = OS.gdk_pango_layout_get_clip_region(layout, 0, 0, ranges.ptr, 1); | |
965 if (clipRegion is null) return new Rectangle(0, 0, 0, 0); | |
966 GdkRectangle rect; | |
967 | |
968 /* | |
969 * Bug in Pango. The region returned by gdk_pango_layout_get_clip_region() | |
970 * includes areas from lines outside of the requested range. The fix | |
971 * is to subtract these areas from the clip region. | |
972 */ | |
973 PangoRectangle* pangoRect = new PangoRectangle(); | |
974 auto iter = OS.pango_layout_get_iter(layout); | |
975 if (iter is null) SWT.error(SWT.ERROR_NO_HANDLES); | |
976 auto linesRegion = OS.gdk_region_new(); | |
977 if (linesRegion is null) SWT.error(SWT.ERROR_NO_HANDLES); | |
978 int lineEnd = 0; | |
979 do { | |
980 OS.pango_layout_iter_get_line_extents(iter, null, pangoRect); | |
981 if (OS.pango_layout_iter_next_line(iter)) { | |
982 lineEnd = OS.pango_layout_iter_get_index(iter) - 1; | |
983 } else { | |
984 lineEnd = slen; | |
985 } | |
986 if (byteStart > lineEnd) continue; | |
987 rect.x = OS.PANGO_PIXELS(pangoRect.x); | |
988 rect.y = OS.PANGO_PIXELS(pangoRect.y); | |
989 rect.width = OS.PANGO_PIXELS(pangoRect.width); | |
990 rect.height = OS.PANGO_PIXELS(pangoRect.height); | |
991 OS.gdk_region_union_with_rect(linesRegion, &rect); | |
992 } while (lineEnd + 1 <= byteEnd); | |
993 OS.gdk_region_intersect(clipRegion, linesRegion); | |
994 OS.gdk_region_destroy(linesRegion); | |
995 OS.pango_layout_iter_free(iter); | |
996 | |
997 OS.gdk_region_get_clipbox(clipRegion, &rect); | |
998 OS.gdk_region_destroy(clipRegion); | |
999 if (OS.pango_context_get_base_dir(context) is OS.PANGO_DIRECTION_RTL) { | |
1000 rect.x = width() - rect.x - rect.width; | |
1001 } | |
1002 return new Rectangle(rect.x, rect.y, rect.width, rect.height); | |
1003 } | |
1004 | |
1005 /** | |
1006 * Returns the descent of the receiver. | |
1007 * | |
1008 * @return the descent | |
1009 * | |
1010 * @exception SWTException <ul> | |
1011 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1012 * </ul> | |
1013 * | |
1014 * @see #getAscent() | |
1015 * @see #setAscent(int) | |
1016 * @see #setDescent(int) | |
1017 * @see #getLineMetrics(int) | |
1018 */ | |
1019 public int getDescent () { | |
1020 checkLayout(); | |
1021 return descent; | |
1022 } | |
1023 | |
1024 /** | |
1025 * Returns the default font currently being used by the receiver | |
1026 * to draw and measure text. | |
1027 * | |
1028 * @return the receiver's font | |
1029 * | |
1030 * @exception SWTException <ul> | |
1031 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1032 * </ul> | |
1033 */ | |
1034 public Font getFont () { | |
1035 checkLayout(); | |
1036 return font; | |
1037 } | |
1038 | |
1039 /** | |
1040 * Returns the receiver's indent. | |
1041 * | |
1042 * @return the receiver's indent | |
1043 * | |
1044 * @exception SWTException <ul> | |
1045 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1046 * </ul> | |
1047 * | |
1048 * @since 3.2 | |
1049 */ | |
1050 public int getIndent () { | |
1051 checkLayout(); | |
1052 return OS.PANGO_PIXELS(OS.pango_layout_get_indent(layout)); | |
1053 } | |
1054 | |
1055 /** | |
1056 * Returns the receiver's justification. | |
1057 * | |
1058 * @return the receiver's justification | |
1059 * | |
1060 * @exception SWTException <ul> | |
1061 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1062 * </ul> | |
1063 * | |
1064 * @since 3.2 | |
1065 */ | |
1066 public bool getJustify () { | |
1067 checkLayout(); | |
1068 return cast(bool) OS.pango_layout_get_justify(layout); | |
1069 } | |
1070 | |
1071 /** | |
1072 * Returns the embedding level for the specified character offset. The | |
1073 * embedding level is usually used to determine the directionality of a | |
1074 * character in bidirectional text. | |
1075 * | |
1076 * @param offset the character offset | |
1077 * @return the embedding level | |
1078 * | |
1079 * @exception IllegalArgumentException <ul> | |
1080 * <li>ERROR_INVALID_ARGUMENT - if the character offset is out of range</li> | |
1081 * </ul> | |
1082 * @exception SWTException <ul> | |
1083 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1084 */ | |
1085 public int getLevel(int offset) { | |
1086 checkLayout(); | |
1087 computeRuns(); | |
1088 int length_ = text.length; | |
1089 if (!(0 <= offset && offset <= length_)) SWT.error(SWT.ERROR_INVALID_RANGE); | |
1090 offset = translateOffset(offset); | |
1091 auto iter = OS.pango_layout_get_iter(layout); | |
1092 if (iter is null) SWT.error(SWT.ERROR_NO_HANDLES); | |
1093 int level = 0; | |
1094 PangoItem* item = new PangoItem(); | |
1095 PangoLayoutRun* run = new PangoLayoutRun(); | |
1096 auto ptr = OS.pango_layout_get_text(layout); | |
1097 auto byteOffset = offset;//OS.g_utf8_offset_to_pointer(ptr, offset) - ptr; | |
47
65761bc28ab2
swt linux again compilable for d1.
Frank Benoit <benoit@tionex.de>
parents:
26
diff
changeset
|
1098 int slen = OS.strlen(ptr); |
25 | 1099 byteOffset = Math.min(byteOffset, slen); |
1100 do { | |
1101 auto runPtr = OS.pango_layout_iter_get_run(iter); | |
1102 if (runPtr !is null) { | |
1103 memmove(run, runPtr, PangoLayoutRun.sizeof); | |
1104 memmove(item, run.item, PangoItem.sizeof); | |
1105 if (item.offset <= byteOffset && byteOffset < item.offset + item.length) { | |
1106 level = item.analysis.level; | |
1107 break; | |
1108 } | |
1109 } | |
1110 } while (OS.pango_layout_iter_next_run(iter)); | |
1111 OS.pango_layout_iter_free(iter); | |
1112 return level; | |
1113 } | |
1114 | |
1115 /** | |
1116 * Returns the bounds of the line for the specified line index. | |
1117 * | |
1118 * @param lineIndex the line index | |
1119 * @return the line bounds | |
1120 * | |
1121 * @exception IllegalArgumentException <ul> | |
1122 * <li>ERROR_INVALID_ARGUMENT - if the line index is out of range</li> | |
1123 * </ul> | |
1124 * @exception SWTException <ul> | |
1125 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1126 * </ul> | |
1127 */ | |
1128 public Rectangle getLineBounds(int lineIndex) { | |
1129 checkLayout(); | |
1130 computeRuns(); | |
1131 int lineCount = OS.pango_layout_get_line_count(layout); | |
1132 if (!(0 <= lineIndex && lineIndex < lineCount)) SWT.error(SWT.ERROR_INVALID_RANGE); | |
1133 auto iter = OS.pango_layout_get_iter(layout); | |
1134 if (iter is null) SWT.error(SWT.ERROR_NO_HANDLES); | |
1135 for (int i = 0; i < lineIndex; i++) OS.pango_layout_iter_next_line(iter); | |
1136 PangoRectangle rect; | |
1137 OS.pango_layout_iter_get_line_extents(iter, null, &rect); | |
1138 OS.pango_layout_iter_free(iter); | |
1139 int x = OS.PANGO_PIXELS(rect.x); | |
1140 int y = OS.PANGO_PIXELS(rect.y); | |
1141 int width_ = OS.PANGO_PIXELS(rect.width); | |
1142 int height = OS.PANGO_PIXELS(rect.height); | |
1143 if (ascent !is -1 && descent !is -1) { | |
1144 height = Math.max (height, ascent + descent); | |
1145 } | |
1146 if (OS.pango_context_get_base_dir(context) is OS.PANGO_DIRECTION_RTL) { | |
1147 x = width() - x - width_; | |
1148 } | |
1149 return new Rectangle(x, y, width_, height); | |
1150 } | |
1151 | |
1152 /** | |
1153 * Returns the receiver's line count. This includes lines caused | |
1154 * by wrapping. | |
1155 * | |
1156 * @return the line count | |
1157 * | |
1158 * @exception SWTException <ul> | |
1159 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1160 * </ul> | |
1161 */ | |
1162 public int getLineCount() { | |
1163 checkLayout (); | |
1164 computeRuns(); | |
1165 return OS.pango_layout_get_line_count(layout); | |
1166 } | |
1167 | |
1168 /** | |
1169 * Returns the index of the line that contains the specified | |
1170 * character offset. | |
1171 * | |
1172 * @param offset the character offset | |
1173 * @return the line index | |
1174 * | |
1175 * @exception IllegalArgumentException <ul> | |
1176 * <li>ERROR_INVALID_ARGUMENT - if the character offset is out of range</li> | |
1177 * </ul> | |
1178 * @exception SWTException <ul> | |
1179 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1180 * </ul> | |
1181 */ | |
1182 public int getLineIndex(int offset) { | |
1183 checkLayout (); | |
1184 computeRuns(); | |
1185 int length_ = text.length; | |
1186 if (!(0 <= offset && offset <= length_)) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
1187 offset = translateOffset(offset); | |
1188 int line = 0; | |
1189 auto ptr = OS.pango_layout_get_text(layout); | |
1190 auto byteOffset = offset;//OS.g_utf8_offset_to_pointer(ptr,offset) - ptr; | |
47
65761bc28ab2
swt linux again compilable for d1.
Frank Benoit <benoit@tionex.de>
parents:
26
diff
changeset
|
1191 int slen = OS.strlen(ptr); |
25 | 1192 byteOffset = Math.min(byteOffset, slen); |
1193 auto iter = OS.pango_layout_get_iter(layout); | |
1194 if (iter is null) SWT.error(SWT.ERROR_NO_HANDLES); | |
1195 while (OS.pango_layout_iter_next_line(iter)) { | |
1196 if (OS.pango_layout_iter_get_index(iter) > byteOffset) break; | |
1197 line++; | |
1198 } | |
1199 OS.pango_layout_iter_free(iter); | |
1200 return line; | |
1201 } | |
1202 | |
1203 /** | |
1204 * Returns the font metrics for the specified line index. | |
1205 * | |
1206 * @param lineIndex the line index | |
1207 * @return the font metrics | |
1208 * | |
1209 * @exception IllegalArgumentException <ul> | |
1210 * <li>ERROR_INVALID_ARGUMENT - if the line index is out of range</li> | |
1211 * </ul> | |
1212 * @exception SWTException <ul> | |
1213 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1214 * </ul> | |
1215 */ | |
1216 public FontMetrics getLineMetrics (int lineIndex) { | |
1217 checkLayout (); | |
1218 computeRuns(); | |
1219 int lineCount = OS.pango_layout_get_line_count(layout); | |
1220 if (!(0 <= lineIndex && lineIndex < lineCount)) SWT.error(SWT.ERROR_INVALID_RANGE); | |
1221 int ascent = 0, descent = 0; | |
1222 PangoLayoutLine* line = new PangoLayoutLine(); | |
1223 memmove(line, OS.pango_layout_get_line(layout, lineIndex), PangoLayoutLine.sizeof); | |
1224 if (line.runs is null) { | |
1225 auto font = this.font !is null ? this.font.handle : device.systemFont.handle; | |
1226 auto lang = OS.pango_context_get_language(context); | |
1227 auto metrics = OS.pango_context_get_metrics(context, font, lang); | |
1228 ascent = OS.pango_font_metrics_get_ascent(metrics); | |
1229 descent = OS.pango_font_metrics_get_descent(metrics); | |
1230 OS.pango_font_metrics_unref(metrics); | |
1231 } else { | |
1232 PangoRectangle rect; | |
1233 OS.pango_layout_line_get_extents(OS.pango_layout_get_line(layout, lineIndex), null, &rect); | |
1234 ascent = -rect.y; | |
1235 descent = rect.height - ascent; | |
1236 } | |
1237 ascent = Math.max(this.ascent, OS.PANGO_PIXELS(ascent)); | |
1238 descent = Math.max(this.descent, OS.PANGO_PIXELS(descent)); | |
1239 return FontMetrics.gtk_new(ascent, descent, 0, 0, ascent + descent); | |
1240 } | |
1241 | |
1242 /** | |
1243 * Returns the line offsets. Each value in the array is the | |
1244 * offset for the first character in a line except for the last | |
1245 * value, which contains the length of the text. | |
1246 * | |
1247 * @return the line offsets | |
1248 * | |
1249 * @exception SWTException <ul> | |
1250 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1251 * </ul> | |
1252 */ | |
1253 public int[] getLineOffsets() { | |
1254 checkLayout(); | |
1255 computeRuns(); | |
1256 int lineCount = OS.pango_layout_get_line_count(layout); | |
1257 int[] offsets = new int [lineCount + 1]; | |
1258 auto ptr = OS.pango_layout_get_text(layout); | |
1259 for (int i = 0; i < lineCount; i++) { | |
1260 auto line = OS.pango_layout_get_line(layout, i); | |
1261 int pos = cast(int)/*64*/OS.g_utf8_pointer_to_offset(ptr, ptr + line.start_index); | |
1262 offsets[i] = untranslateOffset(pos); | |
1263 } | |
1264 offsets[lineCount] = text.length; | |
1265 return offsets; | |
1266 } | |
1267 | |
1268 /** | |
1269 * Returns the location for the specified character offset. The | |
1270 * <code>trailing</code> argument indicates whether the offset | |
1271 * corresponds to the leading or trailing edge of the cluster. | |
1272 * | |
1273 * @param offset the character offset | |
1274 * @param trailing the trailing flag | |
1275 * @return the location of the character offset | |
1276 * | |
1277 * @exception SWTException <ul> | |
1278 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1279 * </ul> | |
1280 * | |
1281 * @see #getOffset(Point, int[]) | |
1282 * @see #getOffset(int, int, int[]) | |
1283 */ | |
1284 public Point getLocation(int offset, bool trailing) { | |
1285 checkLayout(); | |
1286 computeRuns(); | |
1287 int length_ = text.length; | |
1288 if (!(0 <= offset && offset <= length_)) SWT.error(SWT.ERROR_INVALID_RANGE); | |
1289 offset = translateOffset(offset); | |
1290 auto ptr = OS.pango_layout_get_text(layout); | |
1291 auto cont = fromStringz(ptr); | |
1292 offset = cont.utf8AdjustOffset(offset); | |
1293 // leading ZWS+ZWNBS are 2 codepoints in 6 bytes, so we miss 4 bytes here | |
1294 int byteOffset = offset;//(OS.g_utf8_offset_to_pointer(ptr, offset) - ptr); | |
1295 int slen = cont.length; | |
1296 byteOffset = Math.min(byteOffset, slen); | |
1297 PangoRectangle* pos = new PangoRectangle(); | |
1298 OS.pango_layout_index_to_pos(layout, byteOffset, pos); | |
1299 int x = trailing ? pos.x + pos.width : pos.x; | |
1300 int y = pos.y; | |
1301 x = OS.PANGO_PIXELS(x); | |
1302 if (OS.pango_context_get_base_dir(context) is OS.PANGO_DIRECTION_RTL) { | |
1303 x = width() - x; | |
1304 } | |
1305 return new Point(x, OS.PANGO_PIXELS(y)); | |
1306 } | |
1307 | |
1308 /** | |
1309 * Returns the next offset for the specified offset and movement | |
1310 * type. The movement is one of <code>SWT.MOVEMENT_CHAR</code>, | |
1311 * <code>SWT.MOVEMENT_CLUSTER</code>, <code>SWT.MOVEMENT_WORD</code>, | |
1312 * <code>SWT.MOVEMENT_WORD_END</code> or <code>SWT.MOVEMENT_WORD_START</code>. | |
1313 * | |
1314 * @param offset the start offset | |
1315 * @param movement the movement type | |
1316 * @return the next offset | |
1317 * | |
1318 * @exception IllegalArgumentException <ul> | |
1319 * <li>ERROR_INVALID_ARGUMENT - if the offset is out of range</li> | |
1320 * </ul> | |
1321 * @exception SWTException <ul> | |
1322 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1323 * </ul> | |
1324 * | |
1325 * @see #getPreviousOffset(int, int) | |
1326 */ | |
1327 public int getNextOffset (int offset, int movement) { | |
1328 return _getOffset(offset, movement, true); | |
1329 } | |
1330 | |
1331 int _getOffset (int offset, int movement, bool forward) { | |
1332 checkLayout(); | |
1333 computeRuns(); | |
1334 int length_ = text.length; | |
1335 if (!(0 <= offset && offset <= length_)) SWT.error(SWT.ERROR_INVALID_RANGE); | |
1336 if (forward) { | |
1337 if (offset is length_) return length_; | |
1338 } else { | |
1339 if (offset is 0) return 0; | |
1340 } | |
1341 auto cont = OS.pango_layout_get_text(layout); | |
1342 assert( cont ); | |
1343 auto dcont = fromStringz(cont); | |
1344 int step = forward ? 1 : -1; | |
1345 if ((movement & SWT.MOVEMENT_CHAR) !is 0){ | |
1346 //PORTING take care of utf8 | |
1347 int toffset = translateOffset(offset); | |
1348 toffset = dcont.utf8AdjustOffset( toffset ); | |
1349 int incr = dcont.getRelativeCodePointOffset( toffset, step ); | |
1350 return offset + incr; | |
1351 } | |
1352 PangoLogAttr* attrs; | |
1353 int nAttrs; | |
1354 // return one attr per codepoint (=char in pango) | |
1355 OS.pango_layout_get_log_attrs(layout, &attrs, &nAttrs); | |
1356 if (attrs is null) return offset + step; | |
1357 length_ = dcont.length;//OS.g_utf8_strlen(cont, -1); | |
1358 offset = translateOffset(offset); | |
1359 offset = dcont.utf8AdjustOffset( offset ); | |
1360 | |
1361 PangoLogAttr* logAttr; | |
1362 offset = validateOffset( dcont, offset, step); | |
1363 // the loop is byte oriented | |
1364 while (0 < offset && offset < length_) { | |
1365 logAttr = & attrs[ OS.g_utf8_pointer_to_offset( cont, cont+offset) ]; | |
1366 if (((movement & SWT.MOVEMENT_CLUSTER) !is 0) && logAttr.is_cursor_position ) break; | |
1367 if ((movement & SWT.MOVEMENT_WORD) !is 0) { | |
1368 if (forward) { | |
1369 if (logAttr.is_word_end ) break; | |
1370 } else { | |
1371 if (logAttr.is_word_start ) break; | |
1372 } | |
1373 } | |
1374 if ((movement & SWT.MOVEMENT_WORD_START) !is 0) { | |
1375 if (logAttr.is_word_start ) break; | |
1376 } | |
1377 if ((movement & SWT.MOVEMENT_WORD_END) !is 0) { | |
1378 if (logAttr.is_word_end ) break; | |
1379 } | |
1380 offset = validateOffset( dcont, offset, step); | |
1381 } | |
1382 OS.g_free(attrs); | |
1383 return Math.min(Math.max(0, untranslateOffset(offset)), text.length); | |
1384 } | |
1385 | |
1386 /** | |
1387 * Returns the character offset for the specified point. | |
1388 * For a typical character, the trailing argument will be filled in to | |
1389 * indicate whether the point is closer to the leading edge (0) or | |
1390 * the trailing edge (1). When the point is over a cluster composed | |
1391 * of multiple characters, the trailing argument will be filled with the | |
1392 * position of the character in the cluster that is closest to | |
1393 * the point. | |
1394 * | |
1395 * @param point the point | |
1396 * @param trailing the trailing buffer | |
1397 * @return the character offset | |
1398 * | |
1399 * @exception IllegalArgumentException <ul> | |
1400 * <li>ERROR_INVALID_ARGUMENT - if the trailing length is less than <code>1</code></li> | |
1401 * <li>ERROR_NULL_ARGUMENT - if the point is null</li> | |
1402 * </ul> | |
1403 * @exception SWTException <ul> | |
1404 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1405 * </ul> | |
1406 * | |
1407 * @see #getLocation(int, bool) | |
1408 */ | |
1409 public int getOffset(Point point, int[] trailing) { | |
1410 checkLayout(); | |
1411 if (point is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1412 return getOffset(point.x, point.y, trailing); | |
1413 } | |
1414 | |
1415 /** | |
1416 * Returns the character offset for the specified point. | |
1417 * For a typical character, the trailing argument will be filled in to | |
1418 * indicate whether the point is closer to the leading edge (0) or | |
1419 * the trailing edge (1). When the point is over a cluster composed | |
1420 * of multiple characters, the trailing argument will be filled with the | |
1421 * position of the character in the cluster that is closest to | |
1422 * the point. | |
1423 * | |
1424 * @param x the x coordinate of the point | |
1425 * @param y the y coordinate of the point | |
1426 * @param trailing the trailing buffer | |
1427 * @return the character offset | |
1428 * | |
1429 * @exception IllegalArgumentException <ul> | |
1430 * <li>ERROR_INVALID_ARGUMENT - if the trailing length is less than <code>1</code></li> | |
1431 * </ul> | |
1432 * @exception SWTException <ul> | |
1433 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1434 * </ul> | |
1435 * | |
1436 * @see #getLocation(int, bool) | |
1437 */ | |
1438 public int getOffset(int x, int y, int[] trailing) { | |
1439 checkLayout(); | |
1440 computeRuns(); | |
1441 if (trailing !is null && trailing.length < 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
1442 if (OS.pango_context_get_base_dir(context) is OS.PANGO_DIRECTION_RTL) { | |
1443 x = width() - x; | |
1444 } | |
1445 | |
1446 /* | |
1447 * Feature in GTK. pango_layout_xy_to_index() returns the | |
1448 * logical end/start offset of a line when the coordinates are outside | |
1449 * the line bounds. In SWT the correct behavior is to return the closest | |
1450 * visual offset. The fix is to clamp the coordinates inside the | |
1451 * line bounds. | |
1452 */ | |
1453 auto iter = OS.pango_layout_get_iter(layout); | |
1454 if (iter is null) SWT.error(SWT.ERROR_NO_HANDLES); | |
1455 PangoRectangle rect; | |
1456 do { | |
1457 OS.pango_layout_iter_get_line_extents(iter, null, &rect); | |
1458 rect.y = OS.PANGO_PIXELS(rect.y); | |
1459 rect.height = OS.PANGO_PIXELS(rect.height); | |
1460 if (rect.y <= y && y < rect.y + rect.height) { | |
1461 rect.x = OS.PANGO_PIXELS(rect.x); | |
1462 rect.width = OS.PANGO_PIXELS(rect.width); | |
1463 if (x >= rect.x + rect.width) x = rect.x + rect.width - 1; | |
1464 if (x < rect.x) x = rect.x; | |
1465 break; | |
1466 } | |
1467 } while (OS.pango_layout_iter_next_line(iter)); | |
1468 OS.pango_layout_iter_free(iter); | |
1469 | |
1470 int index; | |
1471 int piTrailing; | |
1472 OS.pango_layout_xy_to_index(layout, x * OS.PANGO_SCALE, y * OS.PANGO_SCALE, &index, &piTrailing); | |
1473 auto ptr = OS.pango_layout_get_text(layout); | |
1474 int offset = index;//OS.g_utf8_pointer_to_offset(ptr, ptr + index); | |
1475 if (trailing !is null) trailing[0] = piTrailing; | |
1476 return untranslateOffset(offset); | |
1477 } | |
1478 | |
1479 /** | |
1480 * Returns the orientation of the receiver. | |
1481 * | |
1482 * @return the orientation style | |
1483 * | |
1484 * @exception SWTException <ul> | |
1485 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1486 * </ul> | |
1487 */ | |
1488 public int getOrientation() { | |
1489 checkLayout(); | |
1490 int baseDir = OS.pango_context_get_base_dir(context); | |
1491 return baseDir is OS.PANGO_DIRECTION_RTL ? SWT.RIGHT_TO_LEFT : SWT.LEFT_TO_RIGHT; | |
1492 } | |
1493 | |
1494 /** | |
1495 * Returns the previous offset for the specified offset and movement | |
1496 * type. The movement is one of <code>SWT.MOVEMENT_CHAR</code>, | |
1497 * <code>SWT.MOVEMENT_CLUSTER</code> or <code>SWT.MOVEMENT_WORD</code>, | |
1498 * <code>SWT.MOVEMENT_WORD_END</code> or <code>SWT.MOVEMENT_WORD_START</code>. | |
1499 * | |
1500 * @param offset the start offset | |
1501 * @param movement the movement type | |
1502 * @return the previous offset | |
1503 * | |
1504 * @exception IllegalArgumentException <ul> | |
1505 * <li>ERROR_INVALID_ARGUMENT - if the offset is out of range</li> | |
1506 * </ul> | |
1507 * @exception SWTException <ul> | |
1508 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1509 * </ul> | |
1510 * | |
1511 * @see #getNextOffset(int, int) | |
1512 */ | |
1513 public int getPreviousOffset (int index, int movement) { | |
1514 return _getOffset(index, movement, false); | |
1515 } | |
1516 | |
1517 /** | |
1518 * Gets the ranges of text that are associated with a <code>TextStyle</code>. | |
1519 * | |
1520 * @return the ranges, an array of offsets representing the start and end of each | |
1521 * text style. | |
1522 * | |
1523 * @exception SWTException <ul> | |
1524 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1525 * </ul> | |
1526 * | |
1527 * @see #getStyles() | |
1528 * | |
1529 * @since 3.2 | |
1530 */ | |
1531 public int[] getRanges () { | |
1532 checkLayout(); | |
1533 int[] result = new int[styles.length * 2]; | |
1534 int count = 0; | |
1535 for (int i=0; i<styles.length - 1; i++) { | |
1536 if (styles[i].style !is null) { | |
1537 result[count++] = styles[i].start; | |
1538 result[count++] = styles[i + 1].start - 1; | |
1539 } | |
1540 } | |
1541 if (count !is result.length) { | |
1542 int[] newResult = new int[count]; | |
1543 System.arraycopy(result, 0, newResult, 0, count); | |
1544 result = newResult; | |
1545 } | |
1546 return result; | |
1547 } | |
1548 | |
1549 /** | |
1550 * Returns the text segments offsets of the receiver. | |
1551 * | |
1552 * @return the text segments offsets | |
1553 * | |
1554 * @exception SWTException <ul> | |
1555 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1556 * </ul> | |
1557 */ | |
1558 public int[] getSegments() { | |
1559 checkLayout(); | |
1560 return segments; | |
1561 } | |
1562 | |
1563 String getSegmentsText() { | |
1564 if (segments is null) return text; | |
1565 int nSegments = segments.length; | |
1566 if (nSegments <= 1) return text; | |
1567 int len = text.length; | |
1568 if (len is 0) return text; | |
1569 if (nSegments is 2) { | |
1570 if (segments[0] is 0 && segments[1] is len) return text; | |
1571 } | |
1572 char[] oldChars = text[0..len].dup; | |
1573 char[] newChars = new char[len + nSegments*3]; | |
1574 int charCount = 0, segmentCount = 0; | |
1575 String separator = getOrientation() is SWT.RIGHT_TO_LEFT ? STR_RTL_MARK : STR_LTR_MARK; | |
1576 while (charCount < len) { | |
1577 if (segmentCount < nSegments && charCount is segments[segmentCount]) { | |
1578 newChars[charCount + segmentCount .. charCount + segmentCount + separator.length ] = separator; | |
1579 segmentCount+=separator.length; | |
1580 } else { | |
1581 newChars[charCount + segmentCount] = oldChars[charCount]; | |
1582 charCount++; | |
1583 } | |
1584 } | |
1585 if (segmentCount < nSegments) { | |
1586 segments[segmentCount] = charCount; | |
1587 newChars[charCount + segmentCount .. charCount + segmentCount + separator.length ] = separator; | |
1588 segmentCount+=separator.length; | |
1589 } | |
54 | 1590 return cast(String)newChars[ 0 .. Math.min(charCount + segmentCount, newChars.length) ]; |
25 | 1591 } |
1592 | |
1593 /** | |
1594 * Returns the line spacing of the receiver. | |
1595 * | |
1596 * @return the line spacing | |
1597 * | |
1598 * @exception SWTException <ul> | |
1599 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1600 * </ul> | |
1601 */ | |
1602 public int getSpacing () { | |
1603 checkLayout(); | |
1604 return OS.PANGO_PIXELS(OS.pango_layout_get_spacing(layout)); | |
1605 } | |
1606 | |
1607 /** | |
1608 * Gets the style of the receiver at the specified character offset. | |
1609 * | |
1610 * @param offset the text offset | |
1611 * @return the style or <code>null</code> if not set | |
1612 * | |
1613 * @exception IllegalArgumentException <ul> | |
1614 * <li>ERROR_INVALID_ARGUMENT - if the character offset is out of range</li> | |
1615 * </ul> | |
1616 * @exception SWTException <ul> | |
1617 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1618 * </ul> | |
1619 */ | |
1620 public TextStyle getStyle (int offset) { | |
1621 checkLayout(); | |
1622 int length_ = text.length; | |
1623 if (!(0 <= offset && offset < length_)) SWT.error(SWT.ERROR_INVALID_RANGE); | |
1624 for (int i=1; i<styles.length; i++) { | |
1625 StyleItem item = styles[i]; | |
1626 if (item.start > offset) { | |
1627 return styles[i - 1].style; | |
1628 } | |
1629 } | |
1630 return null; | |
1631 } | |
1632 | |
1633 /** | |
1634 * Gets all styles of the receiver. | |
1635 * | |
1636 * @return the styles | |
1637 * | |
1638 * @exception SWTException <ul> | |
1639 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1640 * </ul> | |
1641 * | |
1642 * @see #getRanges() | |
1643 * | |
1644 * @since 3.2 | |
1645 */ | |
1646 public TextStyle[] getStyles () { | |
1647 checkLayout(); | |
1648 TextStyle[] result = new TextStyle[styles.length]; | |
1649 int count = 0; | |
1650 for (int i=0; i<styles.length; i++) { | |
1651 if (styles[i].style !is null) { | |
1652 result[count++] = styles[i].style; | |
1653 } | |
1654 } | |
1655 if (count !is result.length) { | |
1656 TextStyle[] newResult = new TextStyle[count]; | |
1657 System.arraycopy(result, 0, newResult, 0, count); | |
1658 result = newResult; | |
1659 } | |
1660 return result; | |
1661 } | |
1662 | |
1663 /** | |
1664 * Returns the tab list of the receiver. | |
1665 * | |
1666 * @return the tab list | |
1667 * | |
1668 * @exception SWTException <ul> | |
1669 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1670 * </ul> | |
1671 */ | |
1672 public int[] getTabs() { | |
1673 checkLayout(); | |
1674 return tabs; | |
1675 } | |
1676 | |
1677 /** | |
1678 * Gets the receiver's text, which will be an empty | |
1679 * string if it has never been set. | |
1680 * | |
1681 * @return the receiver's text | |
1682 * | |
1683 * @exception SWTException <ul> | |
1684 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1685 * </ul> | |
1686 */ | |
1687 public String getText () { | |
1688 checkLayout (); | |
1689 return text; | |
1690 } | |
1691 | |
1692 /** | |
1693 * Returns the width of the receiver. | |
1694 * | |
1695 * @return the width | |
1696 * | |
1697 * @exception SWTException <ul> | |
1698 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1699 * </ul> | |
1700 */ | |
1701 public int getWidth () { | |
1702 checkLayout (); | |
1703 int width = OS.pango_layout_get_width(layout); | |
1704 return width !is -1 ? OS.PANGO_PIXELS(width) : -1; | |
1705 } | |
1706 | |
1707 /** | |
1708 * Returns <code>true</code> if the text layout has been disposed, | |
1709 * and <code>false</code> otherwise. | |
1710 * <p> | |
1711 * This method gets the dispose state for the text layout. | |
1712 * When a text layout has been disposed, it is an error to | |
1713 * invoke any other method using the text layout. | |
1714 * </p> | |
1715 * | |
1716 * @return <code>true</code> when the text layout is disposed and <code>false</code> otherwise | |
1717 */ | |
1718 public override bool isDisposed () { | |
1719 return layout is null; | |
1720 } | |
1721 | |
1722 /** | |
1723 * Sets the text alignment for the receiver. The alignment controls | |
1724 * how a line of text is positioned horizontally. The argument should | |
1725 * be one of <code>SWT.LEFT</code>, <code>SWT.RIGHT</code> or <code>SWT.CENTER</code>. | |
1726 * <p> | |
1727 * The default alignment is <code>SWT.LEFT</code>. Note that the receiver's | |
1728 * width must be set in order to use <code>SWT.RIGHT</code> or <code>SWT.CENTER</code> | |
1729 * alignment. | |
1730 * </p> | |
1731 * | |
1732 * @param alignment the new alignment | |
1733 * | |
1734 * @exception SWTException <ul> | |
1735 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1736 * </ul> | |
1737 * | |
1738 * @see #setWidth(int) | |
1739 */ | |
1740 public void setAlignment (int alignment) { | |
1741 checkLayout(); | |
1742 int mask = SWT.LEFT | SWT.CENTER | SWT.RIGHT; | |
1743 alignment &= mask; | |
1744 if (alignment is 0) return; | |
1745 if ((alignment & SWT.LEFT) !is 0) alignment = SWT.LEFT; | |
1746 if ((alignment & SWT.RIGHT) !is 0) alignment = SWT.RIGHT; | |
1747 bool rtl = OS.pango_context_get_base_dir(context) is OS.PANGO_DIRECTION_RTL; | |
1748 int align_ = OS.PANGO_ALIGN_CENTER; | |
1749 switch (alignment) { | |
1750 case SWT.LEFT: | |
1751 align_ = rtl ? OS.PANGO_ALIGN_RIGHT : OS.PANGO_ALIGN_LEFT; | |
1752 break; | |
1753 case SWT.RIGHT: | |
1754 align_ = rtl ? OS.PANGO_ALIGN_LEFT : OS.PANGO_ALIGN_RIGHT; | |
1755 break; | |
1756 } | |
1757 OS.pango_layout_set_alignment(layout, align_); | |
1758 } | |
1759 | |
1760 /** | |
1761 * Sets the ascent of the receiver. The ascent is distance in pixels | |
1762 * from the baseline to the top of the line and it is applied to all | |
1763 * lines. The default value is <code>-1</code> which means that the | |
1764 * ascent is calculated from the line fonts. | |
1765 * | |
1766 * @param ascent the new ascent | |
1767 * | |
1768 * @exception IllegalArgumentException <ul> | |
1769 * <li>ERROR_INVALID_ARGUMENT - if the ascent is less than <code>-1</code></li> | |
1770 * </ul> | |
1771 * @exception SWTException <ul> | |
1772 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1773 * </ul> | |
1774 * | |
1775 * @see #setDescent(int) | |
1776 * @see #getLineMetrics(int) | |
1777 */ | |
1778 public void setAscent (int ascent) { | |
1779 checkLayout(); | |
1780 if (ascent < -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
1781 if (this.ascent is ascent) return; | |
1782 freeRuns(); | |
1783 this.ascent = ascent; | |
1784 } | |
1785 | |
1786 /** | |
1787 * Sets the descent of the receiver. The descent is distance in pixels | |
1788 * from the baseline to the bottom of the line and it is applied to all | |
1789 * lines. The default value is <code>-1</code> which means that the | |
1790 * descent is calculated from the line fonts. | |
1791 * | |
1792 * @param descent the new descent | |
1793 * | |
1794 * @exception IllegalArgumentException <ul> | |
1795 * <li>ERROR_INVALID_ARGUMENT - if the descent is less than <code>-1</code></li> | |
1796 * </ul> | |
1797 * @exception SWTException <ul> | |
1798 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1799 * </ul> | |
1800 * | |
1801 * @see #setAscent(int) | |
1802 * @see #getLineMetrics(int) | |
1803 */ | |
1804 public void setDescent (int descent) { | |
1805 checkLayout(); | |
1806 if (descent < -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
1807 if (this.descent is descent) return; | |
1808 freeRuns(); | |
1809 this.descent = descent; | |
1810 } | |
1811 | |
1812 /** | |
1813 * Sets the default font which will be used by the receiver | |
1814 * to draw and measure text. If the | |
1815 * argument is null, then a default font appropriate | |
1816 * for the platform will be used instead. Note that a text | |
1817 * style can override the default font. | |
1818 * | |
1819 * @param font the new font for the receiver, or null to indicate a default font | |
1820 * | |
1821 * @exception IllegalArgumentException <ul> | |
1822 * <li>ERROR_INVALID_ARGUMENT - if the font has been disposed</li> | |
1823 * </ul> | |
1824 * @exception SWTException <ul> | |
1825 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1826 * </ul> | |
1827 */ | |
1828 public void setFont (Font font) { | |
1829 checkLayout (); | |
1830 if (font !is null && font.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
1831 Font oldFont = this.font; | |
1832 if (oldFont is font) return; | |
1833 freeRuns(); | |
1834 this.font = font; | |
1835 if (oldFont !is null && oldFont.opEquals(font)) return; | |
1836 OS.pango_layout_set_font_description(layout, font !is null ? font.handle : device.systemFont.handle); | |
1837 } | |
1838 | |
1839 /** | |
1840 * Sets the indent of the receiver. This indent it applied of the first line of | |
1841 * each paragraph. | |
1842 * | |
1843 * @param indent new indent | |
1844 * | |
1845 * @exception SWTException <ul> | |
1846 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1847 * </ul> | |
1848 * | |
1849 * @since 3.2 | |
1850 */ | |
1851 public void setIndent (int indent) { | |
1852 checkLayout(); | |
1853 if (indent < 0) return; | |
1854 OS.pango_layout_set_indent(layout, indent * OS.PANGO_SCALE); | |
1855 } | |
1856 | |
1857 /** | |
1858 * Sets the justification of the receiver. Note that the receiver's | |
1859 * width must be set in order to use justification. | |
1860 * | |
1861 * @param justify new justify | |
1862 * | |
1863 * @exception SWTException <ul> | |
1864 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1865 * </ul> | |
1866 * | |
1867 * @since 3.2 | |
1868 */ | |
1869 public void setJustify (bool justify) { | |
1870 checkLayout(); | |
1871 OS.pango_layout_set_justify(layout, justify); | |
1872 } | |
1873 | |
1874 /** | |
1875 * Sets the orientation of the receiver, which must be one | |
1876 * of <code>SWT.LEFT_TO_RIGHT</code> or <code>SWT.RIGHT_TO_LEFT</code>. | |
1877 * | |
1878 * @param orientation new orientation style | |
1879 * | |
1880 * @exception SWTException <ul> | |
1881 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1882 * </ul> | |
1883 */ | |
1884 public void setOrientation(int orientation) { | |
1885 checkLayout(); | |
1886 int mask = SWT.RIGHT_TO_LEFT | SWT.LEFT_TO_RIGHT; | |
1887 orientation &= mask; | |
1888 if (orientation is 0) return; | |
1889 if ((orientation & SWT.LEFT_TO_RIGHT) !is 0) orientation = SWT.LEFT_TO_RIGHT; | |
1890 int baseDir = orientation is SWT.RIGHT_TO_LEFT ? OS.PANGO_DIRECTION_RTL : OS.PANGO_DIRECTION_LTR; | |
1891 if (OS.pango_context_get_base_dir(context) is baseDir) return; | |
1892 OS.pango_context_set_base_dir(context, baseDir); | |
1893 OS.pango_layout_context_changed(layout); | |
1894 int align_ = OS.pango_layout_get_alignment(layout); | |
1895 if (align_ !is OS.PANGO_ALIGN_CENTER) { | |
1896 align_ = align_ is OS.PANGO_ALIGN_LEFT ? OS.PANGO_ALIGN_RIGHT : OS.PANGO_ALIGN_LEFT; | |
1897 OS.pango_layout_set_alignment(layout, align_); | |
1898 } | |
1899 } | |
1900 | |
1901 /** | |
1902 * Sets the line spacing of the receiver. The line spacing | |
1903 * is the space left between lines. | |
1904 * | |
1905 * @param spacing the new line spacing | |
1906 * | |
1907 * @exception IllegalArgumentException <ul> | |
1908 * <li>ERROR_INVALID_ARGUMENT - if the spacing is negative</li> | |
1909 * </ul> | |
1910 * @exception SWTException <ul> | |
1911 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1912 * </ul> | |
1913 */ | |
1914 public void setSpacing (int spacing) { | |
1915 checkLayout(); | |
1916 if (spacing < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
1917 OS.pango_layout_set_spacing(layout, spacing * OS.PANGO_SCALE); | |
1918 } | |
1919 | |
1920 /** | |
1921 * Sets the offsets of the receiver's text segments. Text segments are used to | |
1922 * override the default behaviour of the bidirectional algorithm. | |
1923 * Bidirectional reordering can happen within a text segment but not | |
1924 * between two adjacent segments. | |
1925 * <p> | |
1926 * Each text segment is determined by two consecutive offsets in the | |
1927 * <code>segments</code> arrays. The first element of the array should | |
1928 * always be zero and the last one should always be equals to length of | |
1929 * the text. | |
1930 * </p> | |
1931 * | |
1932 * @param segments the text segments offset | |
1933 * | |
1934 * @exception SWTException <ul> | |
1935 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1936 * </ul> | |
1937 */ | |
1938 public void setSegments(int[] segments) { | |
1939 checkLayout(); | |
1940 if (this.segments is null && segments is null) return; | |
1941 if (this.segments !is null && segments !is null) { | |
1942 if (this.segments.length is segments.length) { | |
1943 int i; | |
1944 for (i = 0; i <segments.length; i++) { | |
1945 if (this.segments[i] !is segments[i]) break; | |
1946 } | |
1947 if (i is segments.length) return; | |
1948 } | |
1949 } | |
1950 freeRuns(); | |
1951 this.segments = segments; | |
1952 } | |
1953 | |
1954 /** | |
1955 * Sets the style of the receiver for the specified range. Styles previously | |
1956 * set for that range will be overwritten. The start and end offsets are | |
1957 * inclusive and will be clamped if out of range. | |
1958 * | |
1959 * @param style the style | |
1960 * @param start the start offset | |
1961 * @param end the end offset | |
1962 * | |
1963 * @exception SWTException <ul> | |
1964 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
1965 * </ul> | |
1966 */ | |
1967 public void setStyle (TextStyle style, int start, int end) { | |
1968 checkLayout(); | |
1969 int length_ = text.length; | |
1970 if (length_ is 0) return; | |
1971 if (start > end) return; | |
1972 start = Math.min(Math.max(0, start), length_ - 1); | |
1973 end = Math.min(Math.max(0, end), length_ - 1); | |
1974 start = text.utf8AdjustOffset( start ); | |
1975 end = text.utf8AdjustOffset( end ); | |
1976 | |
1977 | |
1978 /* | |
1979 * Bug in Pango. Pango 1.2.2 will cause a segmentation fault if a style | |
1980 * is not applied for a whole ligature. The fix is to applied the | |
1981 * style for the whole ligature. | |
1982 * | |
1983 * NOTE that fix only LamAlef ligatures. | |
1984 */ | |
1985 int relIndex; | |
1986 if ((start > 0 ) && isAlef(text[ start .. $ ].firstCodePoint()) && isLam(text.getRelativeCodePoint( start, -1, relIndex ))) { | |
1987 start += relIndex; | |
1988 } | |
1989 if ((end < length_ - 1) && isLam(text[ end .. $ ].firstCodePoint()) && isAlef(text.getRelativeCodePoint(end, 1,relIndex))) { | |
1990 end += relIndex; | |
1991 } | |
1992 | |
1993 int low = -1; | |
1994 int high = styles.length; | |
1995 while (high - low > 1) { | |
1996 int index = (high + low) / 2; | |
1997 if (styles[index + 1].start > start) { | |
1998 high = index; | |
1999 } else { | |
2000 low = index; | |
2001 } | |
2002 } | |
2003 if (0 <= high && high < styles.length) { | |
2004 StyleItem item = styles[high]; | |
2005 if (item.start is start && styles[high + 1].start - 1 is end) { | |
2006 if (style is null) { | |
2007 if (item.style is null) return; | |
2008 } else { | |
2009 if (style.opEquals(item.style)) return; | |
2010 } | |
2011 } | |
2012 } | |
2013 freeRuns(); | |
2014 int modifyStart = high; | |
2015 int modifyEnd = modifyStart; | |
2016 while (modifyEnd < styles.length) { | |
2017 if (styles[modifyEnd + 1].start > end) break; | |
2018 modifyEnd++; | |
2019 } | |
2020 if (modifyStart is modifyEnd) { | |
2021 int styleStart = styles[modifyStart].start; | |
2022 int styleEnd = styles[modifyEnd + 1].start - 1; | |
2023 if (styleStart is start && styleEnd is end) { | |
2024 styles[modifyStart].style = style; | |
2025 return; | |
2026 } | |
2027 if (styleStart !is start && styleEnd !is end) { | |
2028 StyleItem[] newStyles = new StyleItem[styles.length + 2]; | |
2029 System.arraycopy(styles, 0, newStyles, 0, modifyStart + 1); | |
2030 StyleItem item = new StyleItem(); | |
2031 item.start = start; | |
2032 item.style = style; | |
2033 newStyles[modifyStart + 1] = item; | |
2034 item = new StyleItem(); | |
2035 item.start = end + 1; | |
2036 item.style = styles[modifyStart].style; | |
2037 newStyles[modifyStart + 2] = item; | |
2038 System.arraycopy(styles, modifyEnd + 1, newStyles, modifyEnd + 3, styles.length - modifyEnd - 1); | |
2039 styles = newStyles; | |
2040 return; | |
2041 } | |
2042 } | |
2043 if (start is styles[modifyStart].start) modifyStart--; | |
2044 if (end is styles[modifyEnd + 1].start - 1) modifyEnd++; | |
2045 int newLength = styles.length + 1 - (modifyEnd - modifyStart - 1); | |
2046 StyleItem[] newStyles = new StyleItem[newLength]; | |
2047 System.arraycopy(styles, 0, newStyles, 0, modifyStart + 1); | |
2048 StyleItem item = new StyleItem(); | |
2049 item.start = start; | |
2050 item.style = style; | |
2051 newStyles[modifyStart + 1] = item; | |
2052 styles[modifyEnd].start = end + 1; | |
2053 System.arraycopy(styles, modifyEnd, newStyles, modifyStart + 2, styles.length - modifyEnd); | |
2054 styles = newStyles; | |
2055 } | |
2056 | |
2057 /** | |
2058 * Sets the receiver's tab list. Each value in the tab list specifies | |
2059 * the space in pixels from the origin of the text layout to the respective | |
2060 * tab stop. The last tab stop width is repeated continuously. | |
2061 * | |
2062 * @param tabs the new tab list | |
2063 * | |
2064 * @exception SWTException <ul> | |
2065 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
2066 * </ul> | |
2067 */ | |
2068 public void setTabs(int[] tabs) { | |
2069 checkLayout(); | |
2070 if (this.tabs is null && tabs is null) return; | |
2071 if (this.tabs!is null && tabs !is null) { | |
2072 if (this.tabs.length is tabs.length) { | |
2073 int i; | |
2074 for (i = 0; i <tabs.length; i++) { | |
2075 if (this.tabs[i] !is tabs[i]) break; | |
2076 } | |
2077 if (i is tabs.length) return; | |
2078 } | |
2079 } | |
2080 this.tabs = tabs; | |
2081 if (tabs is null) { | |
2082 OS.pango_layout_set_tabs(layout, device.emptyTab); | |
2083 } else { | |
2084 auto tabArray = OS.pango_tab_array_new(tabs.length, true); | |
2085 if (tabArray !is null) { | |
2086 for (int i = 0; i < tabs.length; i++) { | |
2087 OS.pango_tab_array_set_tab(tabArray, i, OS.PANGO_TAB_LEFT, tabs[i]); | |
2088 } | |
2089 OS.pango_layout_set_tabs(layout, tabArray); | |
2090 OS.pango_tab_array_free(tabArray); | |
2091 } | |
2092 } | |
2093 /* | |
2094 * Bug in Pango. A change in the tab stop array is not automatically reflected in the | |
2095 * pango layout object because the call pango_layout_set_tabs() does not free the | |
2096 * lines cache. The fix to use pango_layout_context_changed() to free the lines cache. | |
2097 */ | |
2098 OS.pango_layout_context_changed(layout); | |
2099 } | |
2100 | |
2101 /** | |
2102 * Sets the receiver's text. | |
2103 * | |
2104 * @param text the new text | |
2105 * | |
2106 * @exception SWTException <ul> | |
2107 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
2108 * </ul> | |
2109 */ | |
2110 public void setText (String text) { | |
2111 checkLayout (); | |
2112 if (text.equals(this.text)) return; | |
2113 freeRuns(); | |
2114 this.text = text; | |
2115 styles = new StyleItem[2]; | |
2116 styles[0] = new StyleItem(); | |
2117 styles[1] = new StyleItem(); | |
2118 styles[styles.length - 1].start = text.length; | |
2119 } | |
2120 | |
2121 /** | |
2122 * Sets the line width of the receiver, which determines how | |
2123 * text should be wrapped and aligned. The default value is | |
2124 * <code>-1</code> which means wrapping is disabled. | |
2125 * | |
2126 * @param width the new width | |
2127 * | |
2128 * @exception IllegalArgumentException <ul> | |
2129 * <li>ERROR_INVALID_ARGUMENT - if the width is <code>0</code> or less than <code>-1</code></li> | |
2130 * </ul> | |
2131 * @exception SWTException <ul> | |
2132 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
2133 * </ul> | |
2134 * | |
2135 * @see #setAlignment(int) | |
2136 */ | |
2137 public void setWidth (int width) { | |
2138 checkLayout (); | |
2139 if (width < -1 || width is 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
2140 freeRuns(); | |
2141 if (width is -1) { | |
2142 OS.pango_layout_set_width(layout, -1); | |
2143 bool rtl = OS.pango_context_get_base_dir(context) is OS.PANGO_DIRECTION_RTL; | |
2144 OS.pango_layout_set_alignment(layout, rtl ? OS.PANGO_ALIGN_RIGHT : OS.PANGO_ALIGN_LEFT); | |
2145 } else { | |
2146 OS.pango_layout_set_width(layout, width * OS.PANGO_SCALE); | |
2147 } | |
2148 } | |
2149 | |
2150 static final bool isLam(int ch) { | |
2151 return ch is 0x0644; | |
2152 } | |
2153 | |
2154 static final bool isAlef(int ch) { | |
2155 switch (ch) { | |
2156 case 0x0622: | |
2157 case 0x0623: | |
2158 case 0x0625: | |
2159 case 0x0627: | |
2160 case 0x0649: | |
2161 case 0x0670: | |
2162 case 0x0671: | |
2163 case 0x0672: | |
2164 case 0x0673: | |
2165 case 0x0675: | |
2166 return true; | |
2167 default: | |
2168 } | |
2169 return false; | |
2170 } | |
2171 | |
2172 /** | |
2173 * Returns a string containing a concise, human-readable | |
2174 * description of the receiver. | |
2175 * | |
2176 * @return a string representation of the receiver | |
2177 */ | |
2178 public override String toString () { | |
2179 if (isDisposed()) return "TextLayout {*DISPOSED*}"; | |
2180 return Format( "TextLayout {{{}}", layout ); | |
2181 } | |
2182 | |
2183 /* | |
2184 * Translate a client offset to an internal offset | |
2185 */ | |
2186 int translateOffset(int offset) { | |
2187 int length_ = text.length; | |
2188 if (length_ is 0) return offset; | |
2189 if (invalidOffsets is null) return offset; | |
2190 for (int i = 0; i < invalidOffsets.length; i++) { | |
2191 if (offset < invalidOffsets[i]) break; | |
2192 offset++; | |
2193 } | |
2194 return offset; | |
2195 } | |
2196 | |
2197 /* | |
2198 * Translate an internal offset to a client offset | |
2199 */ | |
2200 int untranslateOffset(int offset) { | |
2201 int length_ = text.length; | |
2202 if (length_ is 0) return offset; | |
2203 if (invalidOffsets is null) return offset; | |
2204 for (int i = 0; i < invalidOffsets.length; i++) { | |
2205 if (offset is invalidOffsets[i]) { | |
2206 offset++; | |
2207 continue; | |
2208 } | |
2209 if (offset < invalidOffsets[i]) { | |
2210 return offset - i; | |
2211 } | |
2212 } | |
2213 return offset - invalidOffsets.length; | |
2214 } | |
2215 | |
54 | 2216 int validateOffset( CString cont, int offset, int step) { |
25 | 2217 if (invalidOffsets is null) return offset + step; |
2218 int i = step > 0 ? 0 : invalidOffsets.length - 1; | |
2219 do { | |
2220 if( offset is 0 && step < 0 ){ | |
2221 offset += step; | |
2222 } | |
2223 else{ | |
2224 offset += cont.getRelativeCodePointOffset( offset, step ); | |
2225 } | |
2226 while (0 <= i && i < invalidOffsets.length) { | |
2227 if (invalidOffsets[i] is offset) break; | |
2228 i += step; | |
2229 } | |
2230 } while (0 <= i && i < invalidOffsets.length); | |
2231 return offset; | |
2232 } | |
2233 | |
2234 int width () { | |
2235 int wrapWidth = OS.pango_layout_get_width(layout); | |
2236 if (wrapWidth !is -1) return OS.PANGO_PIXELS(wrapWidth); | |
2237 int w, h; | |
2238 OS.pango_layout_get_size(layout, &w, &h); | |
2239 return OS.PANGO_PIXELS(w + OS.pango_layout_get_indent(layout)); | |
2240 } | |
2241 | |
2242 } |