All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.swt.graphics.TextLayout Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2000, 2020 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.graphics;

import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.cairo.*;
import org.eclipse.swt.internal.gtk.*;

/**
 * TextLayout is a graphic object that represents
 * styled text.
 * 

* Instances of this class provide support for drawing, cursor * navigation, hit testing, text wrapping, alignment, tab expansion * line breaking, etc. These are aspects required for rendering internationalized text. *

* Application code must explicitly invoke the TextLayout#dispose() * method to release the operating system resources managed by each instance * when those instances are no longer required. *

* * @see TextLayout, TextStyle snippets * @see SWT Example: CustomControlExample, StyledText tab * @see Sample code and further information * * @since 3.0 */ public final class TextLayout extends Resource { static class StyleItem { TextStyle style; int start; @Override public String toString () { return "StyleItem {" + start + ", " + style + "}"; } } Font font; String text; int ascentInPoints, descentInPoints; int indent, wrapIndent, wrapWidth; int[] segments; char[] segmentsChars; int[] tabs; StyleItem[] styles; int stylesCount; long layout, context, attrList, selAttrList; int[] invalidOffsets; int verticalIndentInPoints; MetricsAdapter metricsAdapter = new MetricsAdapter(); static final char LTR_MARK = '\u200E', RTL_MARK = '\u200F', ZWS = '\u200B', ZWNBS = '\uFEFF'; /** * Adapts necessary Pango APIs to enforce fixed line metrics (when set) */ private static class MetricsAdapter { private FontMetrics lineMetricsInPixels; /** * Calculates Y offset from line metrics configured in * {@link #lineMetricsInPixels} to real text position for painting. */ private int wantToRealInPango(PangoRectangle realMetrics) { int wantHeightInPixels = lineMetricsInPixels.getHeight(); int realHeightInPixels = OS.PANGO_PIXELS(realMetrics.height); if (realHeightInPixels == wantHeightInPixels) { return 0; } // The idea is to preserve baseline location, this looks best. // This is the behavior documented in `TextLayout#setFixedLineMetrics()`. int wantAboveInPango = OS.PANGO_SCALE * lineMetricsInPixels.getAscent(); int realAboveInPango = -realMetrics.y; return wantAboveInPango - realAboveInPango; } private int wantToRealInPango(long line) { PangoRectangle rect = new PangoRectangle(); // Pango caches result, so the API is very cheap to call multiple times OS.pango_layout_line_get_extents(line, null, rect); return wantToRealInPango(rect); } public boolean isFixedMetrics() { return (lineMetricsInPixels != null); } public FontMetrics getFixedLineMetrics(Device device) { if (lineMetricsInPixels == null) { return null; } FontMetrics result = new FontMetrics(); result.ascentInPoints = DPIUtil.autoScaleDown(device, lineMetricsInPixels.ascentInPoints); result.descentInPoints = DPIUtil.autoScaleDown(device, lineMetricsInPixels.descentInPoints); result.averageCharWidthInPoints = DPIUtil.autoScaleDown(device, lineMetricsInPixels.averageCharWidthInPoints); return result; } public void setFixedLineMetrics(Device device, FontMetrics metrics) { if (metrics == null) { lineMetricsInPixels = null; return; } FontMetrics result = new FontMetrics(); result.ascentInPoints = DPIUtil.autoScaleUp(device, metrics.ascentInPoints); result.descentInPoints = DPIUtil.autoScaleUp(device, metrics.descentInPoints); result.averageCharWidthInPoints = DPIUtil.autoScaleUp(device, metrics.averageCharWidthInPoints); lineMetricsInPixels = result; } private void validateLayout (long layout) { // Pango caches result, so the API is very cheap to call multiple times if (OS.pango_layout_get_line_count(layout) > 1) { // Multi-line layouts (including word wrapping) are not yet supported. // Note that `StyledText` uses separate `TextLayout` for every line. SWT.error (SWT.ERROR_INVALID_ARGUMENT); } } public long gdk_pango_layout_get_clip_region (long layout, int x_origin, int y_origin, int[] index_ranges, int n_ranges) { // In order to get proper text clip, adjust Y in the same way // as in pango_cairo_show_layout() int yAdjustInPixels = 0; if (isFixedMetrics()) { validateLayout(layout); long line0 = OS.pango_layout_get_line(layout, 0); yAdjustInPixels = OS.PANGO_PIXELS(wantToRealInPango(line0)); } long rgn = GDK.gdk_pango_layout_get_clip_region(layout, x_origin, y_origin + yAdjustInPixels, index_ranges, n_ranges); // Adjust region via intersecting with desired region if (isFixedMetrics()) { cairo_rectangle_int_t wantRect = new cairo_rectangle_int_t(); // Use real x,width with desired y,height Cairo.cairo_region_get_extents(rgn, wantRect); wantRect.y = y_origin; wantRect.height = lineMetricsInPixels.getHeight(); long limitRgn = Cairo.cairo_region_create_rectangle(wantRect); Cairo.cairo_region_intersect(rgn, limitRgn); Cairo.cairo_region_destroy(limitRgn); } return rgn; } public void pango_cairo_show_layout (long cairo, long layout, double x, double y) { int yAdjustInPixels = 0; if (isFixedMetrics()) { validateLayout(layout); long line0 = OS.pango_layout_get_line(layout, 0); yAdjustInPixels = OS.PANGO_PIXELS(wantToRealInPango(line0)); } Cairo.cairo_move_to(cairo, x, y + yAdjustInPixels); OS.pango_cairo_show_layout (cairo, layout); } public void pango_layout_get_size (long layout, int[] width, int[] height) { OS.pango_layout_get_size(layout, width, height); if (isFixedMetrics()) { validateLayout(layout); height[0] = lineMetricsInPixels.getHeight(); } } public void pango_layout_iter_get_line_extents (long iter, PangoRectangle ink_rect, PangoRectangle logical_rect) { OS.pango_layout_iter_get_line_extents(iter, ink_rect, logical_rect); if (isFixedMetrics()) { if (ink_rect != null) { // SWT doesn't use that, so I didn't implement SWT.error(SWT.ERROR_INVALID_ARGUMENT); } if (logical_rect != null) { logical_rect.height = OS.PANGO_SCALE * lineMetricsInPixels.getHeight(); } } } public void pango_layout_line_get_extents (long line, PangoRectangle ink_rect, PangoRectangle logical_rect) { OS.pango_layout_line_get_extents(line, ink_rect, logical_rect); if (isFixedMetrics()) { if (ink_rect != null) { // SWT doesn't use that, so I didn't implement SWT.error(SWT.ERROR_INVALID_ARGUMENT); } if (logical_rect != null) { logical_rect.height = OS.PANGO_SCALE * lineMetricsInPixels.getHeight(); } } } } /** * Constructs a new instance of this class on the given device. *

* You must dispose the text layout when it is no longer required. *

* * @param device the device on which to allocate the text layout * * @exception IllegalArgumentException
    *
  • ERROR_NULL_ARGUMENT - if device is null and there is no current device
  • *
* * @see #dispose() */ public TextLayout (Device device) { super(device); device = this.device; if (GTK.GTK4) { long fontMap = OS.pango_cairo_font_map_get_default (); context = OS.pango_font_map_create_context (fontMap); } else { context = GDK.gdk_pango_context_get(); } if (context == 0) SWT.error(SWT.ERROR_NO_HANDLES); OS.pango_context_set_language(context, GTK.gtk_get_default_language()); OS.pango_context_set_base_dir(context, OS.PANGO_DIRECTION_LTR); layout = OS.pango_layout_new(context); if (layout == 0) SWT.error(SWT.ERROR_NO_HANDLES); OS.pango_layout_set_font_description(layout, device.systemFont.handle); OS.pango_layout_set_wrap(layout, OS.PANGO_WRAP_WORD_CHAR); OS.pango_layout_set_tabs(layout, device.emptyTab); OS.pango_layout_set_auto_dir(layout, false); text = ""; wrapWidth = ascentInPoints = descentInPoints = -1; styles = new StyleItem[2]; styles[0] = new StyleItem(); styles[1] = new StyleItem(); stylesCount = 2; init(); } void checkLayout() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); } void computeRuns () { if (attrList != 0) return; String segmentsText = getSegmentsText(); byte[] buffer = Converter.wcsToMbcs(segmentsText, false); OS.pango_layout_set_text (layout, buffer, buffer.length); attrList = OS.pango_attr_list_new(); selAttrList = OS.pango_attr_list_new(); // pango_attr_insert_hyphens_new function is available only on Pango 1.44.0+ // pango_version (used for version check) encodes the version in an integer via // the following formula: // MAJOR*10000+MINOR*100+MICRO if ((1 * 10000 + 44 * 100 + 0) <= OS.pango_version()) { long hyphenAttr = OS.pango_attr_insert_hyphens_new(false); OS.pango_attr_list_insert(attrList, hyphenAttr); OS.pango_attr_list_insert(selAttrList, OS.pango_attribute_copy(hyphenAttr)); } if (stylesCount == 2 && styles[0].style == null && ascentInPoints == -1 && descentInPoints == -1 && segments == null) { OS.pango_layout_set_attributes(layout, attrList); return; } long ptr = OS.pango_layout_get_text(layout); PangoAttribute attribute = new PangoAttribute(); char[] chars = null; int segementsLength = segmentsText.length(); int nSegments = segementsLength - text.length(); int offsetCount = nSegments; int[] lineOffsets = null; // Set minimum line ascent/descent. Feature in Pango: glyphs affected // by `pango_attr_shape_new()` become invisible. Workaround: insert // additional control characters and shape these instead. boolean useMinAscentDescent = !metricsAdapter.isFixedMetrics() && (ascentInPoints != -1 || descentInPoints != -1); if (useMinAscentDescent && segementsLength > 0) { PangoRectangle rect = new PangoRectangle(); if (ascentInPoints != -1) rect.y = -(DPIUtil.autoScaleUp(getDevice(), ascentInPoints) * OS.PANGO_SCALE); rect.height = DPIUtil.autoScaleUp(getDevice(), (Math.max(0, ascentInPoints) + Math.max(0, descentInPoints))) * OS.PANGO_SCALE; int lineCount = OS.pango_layout_get_line_count(layout); chars = new char[segementsLength + lineCount * 2]; lineOffsets = new int [lineCount]; int oldPos = 0, lineIndex = 0; PangoLayoutLine line = new PangoLayoutLine(); while (lineIndex < lineCount) { long linePtr = OS.pango_layout_get_line(layout, lineIndex); OS.memmove(line, linePtr, PangoLayoutLine.sizeof); int bytePos = line.start_index; /* Note: The length in bytes of ZWS and ZWNBS are both equals to 3 */ int offset = lineIndex * 6; long attr = OS.pango_attr_shape_new (rect, rect); OS.memmove (attribute, attr, PangoAttribute.sizeof); attribute.start_index = bytePos + offset; attribute.end_index = bytePos + offset + 3; OS.memmove (attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); OS.pango_attr_list_insert(selAttrList, OS.pango_attribute_copy(attr)); attr = OS.pango_attr_shape_new (rect, rect); OS.memmove (attribute, attr, PangoAttribute.sizeof); attribute.start_index = bytePos + offset + 3; attribute.end_index = bytePos + offset + 6; OS.memmove (attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); OS.pango_attr_list_insert(selAttrList, OS.pango_attribute_copy(attr)); int pos = (int)OS.g_utf16_pointer_to_offset(ptr, ptr + bytePos); chars[pos + lineIndex * 2] = ZWS; chars[pos + lineIndex * 2 + 1] = ZWNBS; segmentsText.getChars(oldPos, pos, chars, oldPos + lineIndex * 2); lineOffsets[lineIndex] = pos + lineIndex * 2; oldPos = pos; lineIndex++; } segmentsText.getChars(oldPos, segementsLength, chars, oldPos + lineIndex * 2); buffer = Converter.wcsToMbcs(chars, false); OS.pango_layout_set_text (layout, buffer, buffer.length); ptr = OS.pango_layout_get_text(layout); offsetCount += 2 * lineCount; } else { chars = new char[segementsLength]; segmentsText.getChars(0, segementsLength, chars, 0); } invalidOffsets = new int[offsetCount]; if (offsetCount > 0) { offsetCount = 0; int lineIndex = 0; int segmentCount = 0; for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (c == ZWS && lineOffsets != null && lineIndex < lineOffsets.length && i == lineOffsets[lineIndex]) { invalidOffsets[offsetCount++] = i; //ZWS invalidOffsets[offsetCount++] = ++i; //ZWNBS lineIndex++; } else if (segmentCount < nSegments && i - offsetCount == segments[segmentCount]) { invalidOffsets[offsetCount++] = i; segmentCount++; } } } int strlen = C.strlen(ptr); Font defaultFont = font != null ? font : device.systemFont; for (int i = 0; i < stylesCount - 1; i++) { StyleItem styleItem = styles[i]; TextStyle style = styleItem.style; if (style == null) continue; int start = translateOffset(styleItem.start); int end = translateOffset(styles[i+1].start - 1); int byteStart = (int)(OS.g_utf16_offset_to_pointer(ptr, start) - ptr); int byteEnd = (int)(OS.g_utf16_offset_to_pointer(ptr, end + 1) - ptr); byteStart = Math.min(byteStart, strlen); byteEnd = Math.min(byteEnd, strlen); Font font = style.font; if (font != null && !font.isDisposed() && !defaultFont.equals(font)) { long attr = OS.pango_attr_font_desc_new (font.handle); OS.memmove (attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; OS.memmove (attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); OS.pango_attr_list_insert(selAttrList, OS.pango_attribute_copy(attr)); } if (style.underline) { int underlineStyle = OS.PANGO_UNDERLINE_NONE; switch (style.underlineStyle) { case SWT.UNDERLINE_SINGLE: underlineStyle = OS.PANGO_UNDERLINE_SINGLE; break; case SWT.UNDERLINE_DOUBLE: underlineStyle = OS.PANGO_UNDERLINE_DOUBLE; break; case SWT.UNDERLINE_SQUIGGLE: case SWT.UNDERLINE_ERROR: underlineStyle = OS.PANGO_UNDERLINE_ERROR; break; case SWT.UNDERLINE_LINK: { if (style.foreground == null) { // Bug 497071: use COLOR_LINK_FOREGROUND for StyledText links long attr; GdkRGBA linkRGBA = device.getSystemColor(SWT.COLOR_LINK_FOREGROUND).handle; // Manual conversion since PangoAttrColor is a special case. // It uses GdkColor style colors but is supported on GTK3. attr = OS.pango_attr_foreground_new((short)(linkRGBA.red * 0xFFFF), (short)(linkRGBA.green * 0xFFFF), (short)(linkRGBA.blue * 0xFFFF)); OS.memmove (attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; OS.memmove (attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); } underlineStyle = OS.PANGO_UNDERLINE_SINGLE; break; } } long attr = OS.pango_attr_underline_new(underlineStyle); OS.memmove(attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; OS.memmove(attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); OS.pango_attr_list_insert(selAttrList, OS.pango_attribute_copy(attr)); if (style.underlineColor != null) { GdkRGBA rgba = style.underlineColor.handle; attr = OS.pango_attr_underline_color_new((short)(rgba.red * 0xFFFF), (short)(rgba.green * 0xFFFF), (short)(rgba.blue * 0xFFFF)); if (attr != 0) { OS.memmove(attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; OS.memmove(attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); OS.pango_attr_list_insert(selAttrList, OS.pango_attribute_copy(attr)); } } } if (style.strikeout) { long attr = OS.pango_attr_strikethrough_new(true); OS.memmove(attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; OS.memmove(attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); OS.pango_attr_list_insert(selAttrList, OS.pango_attribute_copy(attr)); if (style.strikeoutColor != null) { GdkRGBA rgba = style.strikeoutColor.handle; attr = OS.pango_attr_strikethrough_color_new((short)(rgba.red * 0xFFFF), (short)(rgba.green * 0xFFFF), (short)(rgba.blue * 0xFFFF)); if (attr != 0) { OS.memmove(attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; OS.memmove(attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); OS.pango_attr_list_insert(selAttrList, OS.pango_attribute_copy(attr)); } } } Color foreground = style.foreground; if (foreground != null && !foreground.isDisposed()) { long attr; GdkRGBA rgba = foreground.handle; attr = OS.pango_attr_foreground_new((short)(rgba.red * 0xFFFF), (short)(rgba.green * 0xFFFF), (short)(rgba.blue * 0xFFFF)); OS.memmove (attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; OS.memmove (attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); } Color background = style.background; if (background != null && !background.isDisposed()) { long attr; GdkRGBA rgba = background.handle; attr = OS.pango_attr_background_new((short)(rgba.red * 0xFFFF), (short)(rgba.green * 0xFFFF), (short)(rgba.blue * 0xFFFF)); OS.memmove (attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; OS.memmove (attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); } GlyphMetrics metrics = style.metrics; if (metrics != null) { PangoRectangle rect = new PangoRectangle(); rect.y = -(DPIUtil.autoScaleUp(getDevice(), metrics.ascent) * OS.PANGO_SCALE); rect.height = DPIUtil.autoScaleUp(getDevice(), (metrics.ascent + metrics.descent)) * OS.PANGO_SCALE; rect.width = DPIUtil.autoScaleUp(getDevice(), metrics.width) * OS.PANGO_SCALE; long attr = OS.pango_attr_shape_new (rect, rect); OS.memmove (attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; OS.memmove (attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); OS.pango_attr_list_insert(selAttrList, OS.pango_attribute_copy(attr)); } int rise = style.rise; if (rise != 0) { long attr = OS.pango_attr_rise_new (DPIUtil.autoScaleUp(getDevice(), rise) * OS.PANGO_SCALE); OS.memmove (attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; OS.memmove (attr, attribute, PangoAttribute.sizeof); OS.pango_attr_list_insert(attrList, attr); OS.pango_attr_list_insert(selAttrList, OS.pango_attribute_copy(attr)); } } OS.pango_layout_set_attributes(layout, attrList); } int[] computePolyline(int left, int top, int right, int bottom) { int height = bottom - top; // can be any number int width = 2 * height; // must be even int peaks = Compatibility.ceil(right - left, width); if (peaks == 0 && right - left > 2) { peaks = 1; } int length = ((2 * peaks) + 1) * 2; if (length < 0) return new int[0]; int[] coordinates = new int[length]; for (int i = 0; i < peaks; i++) { int index = 4 * i; coordinates[index] = left + (width * i); coordinates[index+1] = bottom; coordinates[index+2] = coordinates[index] + width / 2; coordinates[index+3] = top; } coordinates[length-2] = left + (width * peaks); coordinates[length-1] = bottom; return coordinates; } @Override void destroy() { font = null; text = null; styles = null; freeRuns(); segments = null; segmentsChars = null; if (layout != 0) OS.g_object_unref(layout); layout = 0; if (context != 0) OS.g_object_unref(context); context = 0; } /** * Draws the receiver's text using the specified GC at the specified * point. * * @param gc the GC to draw * @param x the x coordinate of the top left corner of the rectangular area where the text is to be drawn * @param y the y coordinate of the top left corner of the rectangular area where the text is to be drawn * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* @exception IllegalArgumentException
    *
  • ERROR_NULL_ARGUMENT - if the gc is null
  • *
*/ public void draw(GC gc, int x, int y) { x = DPIUtil.autoScaleUp(getDevice(), x); y = DPIUtil.autoScaleUp(getDevice(), y); drawInPixels(gc, x, y); } void drawInPixels(GC gc, int x, int y) { drawInPixels(gc, x, y, -1, -1, null, null); } /** * Draws the receiver's text using the specified GC at the specified * point. * * @param gc the GC to draw * @param x the x coordinate of the top left corner of the rectangular area where the text is to be drawn * @param y the y coordinate of the top left corner of the rectangular area where the text is to be drawn * @param selectionStart the offset where the selections starts, or -1 indicating no selection * @param selectionEnd the offset where the selections ends, or -1 indicating no selection * @param selectionForeground selection foreground, or NULL to use the system default color * @param selectionBackground selection background, or NULL to use the system default color * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* @exception IllegalArgumentException
    *
  • ERROR_NULL_ARGUMENT - if the gc is null
  • *
*/ public void draw(GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) { checkLayout (); x = DPIUtil.autoScaleUp(getDevice(), x); y = DPIUtil.autoScaleUp(getDevice(), y); drawInPixels(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground); } void drawInPixels(GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) { drawInPixels(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground, 0); } /** * Draws the receiver's text using the specified GC at the specified * point. *

* The parameter flags can include one of SWT.DELIMITER_SELECTION * or SWT.FULL_SELECTION to specify the selection behavior on all lines except * for the last line, and can also include SWT.LAST_LINE_SELECTION to extend * the specified selection behavior to the last line. *

* @param gc the GC to draw * @param x the x coordinate of the top left corner of the rectangular area where the text is to be drawn * @param y the y coordinate of the top left corner of the rectangular area where the text is to be drawn * @param selectionStart the offset where the selections starts, or -1 indicating no selection * @param selectionEnd the offset where the selections ends, or -1 indicating no selection * @param selectionForeground selection foreground, or NULL to use the system default color * @param selectionBackground selection background, or NULL to use the system default color * @param flags drawing options * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* @exception IllegalArgumentException
    *
  • ERROR_NULL_ARGUMENT - if the gc is null
  • *
* * @since 3.3 */ public void draw(GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground, int flags) { checkLayout (); x = DPIUtil.autoScaleUp(getDevice(), x); y = DPIUtil.autoScaleUp(getDevice(), y); drawInPixels(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground, flags); } void drawInPixels(GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground, int flags) { checkLayout (); computeRuns(); if (gc == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (gc.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (selectionForeground != null && selectionForeground.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (selectionBackground != null && selectionBackground.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); gc.checkGC(GC.FOREGROUND); int length = text.length(); x += Math.min (indent, wrapIndent); y += getScaledVerticalIndent(); boolean hasSelection = selectionStart <= selectionEnd && selectionStart != -1 && selectionEnd != -1; GCData data = gc.data; long cairo = data.cairo; boolean extent = false; if ((flags & (SWT.FULL_SELECTION | SWT.DELIMITER_SELECTION)) != 0 && (hasSelection || (flags & SWT.LAST_LINE_SELECTION) != 0)) { long [] attrs = new long [1]; int[] nAttrs = new int[1]; PangoLogAttr logAttr = new PangoLogAttr(); PangoRectangle rect = new PangoRectangle(); int lineCount = OS.pango_layout_get_line_count(layout); long ptr = OS.pango_layout_get_text(layout); long iter = OS.pango_layout_get_iter(layout); if (selectionBackground == null) selectionBackground = device.getSystemColor(SWT.COLOR_LIST_SELECTION); Cairo.cairo_save(cairo); GdkRGBA rgba = selectionBackground.handle; Cairo.cairo_set_source_rgba(cairo, rgba.red, rgba.green, rgba.blue, rgba.alpha); int lineIndex = 0; do { int lineEnd; metricsAdapter.pango_layout_iter_get_line_extents(iter, null, rect); if (OS.pango_layout_iter_next_line(iter)) { int bytePos = OS.pango_layout_iter_get_index(iter); lineEnd = (int)OS.g_utf16_pointer_to_offset(ptr, ptr + bytePos); } else { lineEnd = (int)OS.g_utf16_strlen(ptr, -1); } if (lineIndex == lineCount - 1 && (flags & SWT.LAST_LINE_SELECTION) != 0) { extent = true; } else { if (attrs[0] == 0) OS.pango_layout_get_log_attrs(layout, attrs, nAttrs); OS.memmove(logAttr, attrs[0] + lineEnd * PangoLogAttr.sizeof, PangoLogAttr.sizeof); if (!logAttr.is_line_break) { if (selectionStart <= lineEnd && lineEnd <= selectionEnd) extent = true; } else { if (selectionStart <= lineEnd && lineEnd < selectionEnd && (flags & SWT.FULL_SELECTION) != 0) { extent = true; } } } if (extent) { int lineX = x + OS.PANGO_PIXELS(rect.x) + OS.PANGO_PIXELS(rect.width); int lineY = y + OS.PANGO_PIXELS(rect.y); int height = OS.PANGO_PIXELS(rect.height); if (ascentInPoints != -1 && descentInPoints != -1) { height = Math.max (height, DPIUtil.autoScaleUp(getDevice(), ascentInPoints + descentInPoints)); } height += getSpacingInPixels(); int width = (flags & SWT.FULL_SELECTION) != 0 ? 0x7fff : height / 3; Cairo.cairo_rectangle(cairo, lineX, lineY, width, height); Cairo.cairo_fill(cairo); } lineIndex++; } while (lineIndex < lineCount); OS.pango_layout_iter_free(iter); if (attrs[0] != 0) OS.g_free(attrs[0]); Cairo.cairo_restore(cairo); } if (length == 0) return; if (!hasSelection) { if ((data.style & SWT.MIRRORED) != 0) { Cairo.cairo_save(cairo); Cairo.cairo_scale(cairo, -1, 1); Cairo.cairo_translate(cairo, -2 * x - width(), 0); } metricsAdapter.pango_cairo_show_layout(cairo, layout, x, y); drawBorder(gc, x, y, null); if ((data.style & SWT.MIRRORED) != 0) { Cairo.cairo_restore(cairo); } } else { selectionStart = Math.min(Math.max(0, selectionStart), length - 1); selectionEnd = Math.min(Math.max(0, selectionEnd), length - 1); length = (int)OS.g_utf16_strlen(OS.pango_layout_get_text(layout), -1); selectionStart = translateOffset(selectionStart); selectionEnd = translateOffset(selectionEnd); if (selectionForeground == null) selectionForeground = device.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT); if (selectionBackground == null) selectionBackground = device.getSystemColor(SWT.COLOR_LIST_SELECTION); int yExtent = extent ? getSpacingInPixels() : 0; boolean fullSelection = selectionStart == 0 && selectionEnd == length - 1; if (fullSelection) { long ptr = OS.pango_layout_get_text(layout); if ((data.style & SWT.MIRRORED) != 0) { Cairo.cairo_save(cairo); Cairo.cairo_scale(cairo, -1, 1); Cairo.cairo_translate(cairo, -2 * x - width(), 0); } drawWithCairo(gc, x, y, 0, C.strlen(ptr), yExtent, fullSelection, selectionForeground.handle, selectionBackground.handle); if ((data.style & SWT.MIRRORED) != 0) { Cairo.cairo_restore(cairo); } } else { long ptr = OS.pango_layout_get_text(layout); int byteSelStart = (int)(OS.g_utf16_offset_to_pointer(ptr, selectionStart) - ptr); int byteSelEnd = (int)(OS.g_utf16_offset_to_pointer(ptr, selectionEnd + 1) - ptr); int strlen = C.strlen(ptr); byteSelStart = Math.min(byteSelStart, strlen); byteSelEnd = Math.min(byteSelEnd, strlen); if ((data.style & SWT.MIRRORED) != 0) { Cairo.cairo_save(cairo); Cairo.cairo_scale(cairo, -1, 1); Cairo.cairo_translate(cairo, -2 * x - width(), 0); } drawWithCairo(gc, x, y, byteSelStart, byteSelEnd, yExtent, fullSelection, selectionForeground.handle, selectionBackground.handle); if ((data.style & SWT.MIRRORED) != 0) { Cairo.cairo_restore(cairo); } } } Cairo.cairo_new_path(cairo); } void drawWithCairo(GC gc, int x, int y, int start, int end, int yExtent, boolean fullSelection, GdkRGBA fg, GdkRGBA bg) { GCData data = gc.data; long cairo = data.cairo; Cairo.cairo_save(cairo); if (!fullSelection) { metricsAdapter.pango_cairo_show_layout(cairo, layout, x, y); drawBorder(gc, x, y, null); } int[] ranges = new int[]{start, end}; long rgn = metricsAdapter.gdk_pango_layout_get_clip_region(layout, x, y, ranges, ranges.length / 2); if (rgn != 0) { if (yExtent > 0) { cairo_rectangle_int_t rect = new cairo_rectangle_int_t(); Cairo.cairo_region_get_extents(rgn, rect); rect.height += yExtent; long extendRgn = Cairo.cairo_region_create_rectangle(rect); Cairo.cairo_region_union(rgn, extendRgn); Cairo.cairo_region_destroy(extendRgn); } GDK.gdk_cairo_region(cairo, rgn); Cairo.cairo_clip(cairo); Cairo.cairo_set_source_rgba(cairo, bg.red, bg.green, bg.blue, bg.alpha); Cairo.cairo_paint(cairo); Cairo.cairo_region_destroy(rgn); } Cairo.cairo_set_source_rgba(cairo, fg.red, fg.green, fg.blue, fg.alpha); OS.pango_layout_set_attributes(layout, selAttrList); metricsAdapter.pango_cairo_show_layout(cairo, layout, x, y); OS.pango_layout_set_attributes(layout, attrList); drawBorder(gc, x, y, fg); Cairo.cairo_restore(cairo); } void drawBorder(GC gc, int x, int y, GdkRGBA selectionColor) { GCData data = gc.data; long cairo = data.cairo; long ptr = OS.pango_layout_get_text(layout); Cairo.cairo_save(cairo); for (int i = 0; i < stylesCount - 1; i++) { TextStyle style = styles[i].style; if (style == null) continue; boolean drawBorder = style.borderStyle != SWT.NONE; if (drawBorder && !style.isAdherentBorder(styles[i+1].style)) { int start = styles[i].start; for (int j = i; j > 0 && style.isAdherentBorder(styles[j-1].style); j--) { start = styles[j - 1].start; } start = translateOffset(start); int end = translateOffset(styles[i+1].start - 1); int byteStart = (int)(OS.g_utf16_offset_to_pointer(ptr, start) - ptr); int byteEnd = (int)(OS.g_utf16_offset_to_pointer(ptr, end + 1) - ptr); int[] ranges = new int[]{byteStart, byteEnd}; long rgn = metricsAdapter.gdk_pango_layout_get_clip_region(layout, x, y, ranges, ranges.length / 2); if (rgn != 0) { int[] nRects = new int[1]; long [] rects = new long [1]; Region.cairo_region_get_rectangles(rgn, rects, nRects); cairo_rectangle_int_t rect = new cairo_rectangle_int_t(); GdkRGBA colorRGBA = null; if (colorRGBA == null && style.borderColor != null) colorRGBA = style.borderColor.handle; if (colorRGBA == null && selectionColor != null) colorRGBA = selectionColor; if (colorRGBA == null && style.foreground != null) colorRGBA = style.foreground.handle; if (colorRGBA == null) colorRGBA = data.foregroundRGBA; int width = 1; float[] dashes = null; switch (style.borderStyle) { case SWT.BORDER_SOLID: break; case SWT.BORDER_DASH: dashes = width != 0 ? GC.LINE_DASH : GC.LINE_DASH_ZERO; break; case SWT.BORDER_DOT: dashes = width != 0 ? GC.LINE_DOT : GC.LINE_DOT_ZERO; break; } Cairo.cairo_set_source_rgba(cairo, colorRGBA.red, colorRGBA.green, colorRGBA.blue, colorRGBA.alpha); Cairo.cairo_set_line_width(cairo, width); if (dashes != null) { double[] cairoDashes = new double[dashes.length]; for (int j = 0; j < cairoDashes.length; j++) { cairoDashes[j] = width == 0 || data.lineStyle == SWT.LINE_CUSTOM ? dashes[j] : dashes[j] * width; } Cairo.cairo_set_dash(cairo, cairoDashes, cairoDashes.length, 0); } else { Cairo.cairo_set_dash(cairo, null, 0, 0); } for (int j=0; jSWT.LEFT
, SWT.CENTER or * SWT.RIGHT. * * @return the alignment used to positioned text horizontally * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
*/ public int getAlignment() { checkLayout(); int align = OS.pango_layout_get_alignment(layout); boolean rtl = OS.pango_context_get_base_dir(context) == OS.PANGO_DIRECTION_RTL; switch (align) { case OS.PANGO_ALIGN_LEFT: return rtl ? SWT.RIGHT : SWT.LEFT; case OS.PANGO_ALIGN_RIGHT: return rtl ? SWT.LEFT : SWT.RIGHT; } return SWT.CENTER; } /** * Returns the ascent of the receiver. * * @return the ascent * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @see #getDescent() * @see #setDescent(int) * @see #setAscent(int) * @see #getLineMetrics(int) */ public int getAscent () { checkLayout(); return ascentInPoints; } /** * Returns the bounds of the receiver. The width returned is either the * width of the longest line or the width set using {@link TextLayout#setWidth(int)}. * To obtain the text bounds of a line use {@link TextLayout#getLineBounds(int)}. * * @return the bounds of the receiver * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @see #setWidth(int) * @see #getLineBounds(int) */ public Rectangle getBounds() { int spacingInPixels = getSpacingInPixels(); return DPIUtil.autoScaleDown(getDevice(), getBoundsInPixels(spacingInPixels)); } Rectangle getBoundsInPixels(int spacingInPixels) { checkLayout(); computeRuns(); int[] w = new int[1], h = new int[1]; metricsAdapter.pango_layout_get_size(layout, w, h); int wrapWidth = OS.pango_layout_get_width(layout); w[0] = wrapWidth != -1 ? wrapWidth : w[0] + OS.pango_layout_get_indent(layout); int width = OS.PANGO_PIXELS(w[0]); int height = OS.PANGO_PIXELS(h[0]); if (ascentInPoints != -1 && descentInPoints != -1) { height = Math.max (height, DPIUtil.autoScaleUp(getDevice(), ascentInPoints + descentInPoints)); } height += spacingInPixels; return new Rectangle(0, 0, width, height + getScaledVerticalIndent()); } /** * Returns the bounds for the specified range of characters. The * bounds is the smallest rectangle that encompasses all characters * in the range. The start and end offsets are inclusive and will be * clamped if out of range. * * @param start the start offset * @param end the end offset * @return the bounds of the character range * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
*/ public Rectangle getBounds(int start, int end) { checkLayout(); return DPIUtil.autoScaleDown(getDevice(), getBoundsInPixels(start, end)); } Rectangle getBoundsInPixels(int start, int end) { checkLayout(); computeRuns(); int length = text.length(); if (length == 0) return new Rectangle(0, 0, 0, 0); if (start > end) return new Rectangle(0, 0, 0, 0); start = Math.min(Math.max(0, start), length - 1); end = Math.min(Math.max(0, end), length - 1); start = translateOffset(start); end = translateOffset(end); long ptr = OS.pango_layout_get_text(layout); int byteStart = (int)(OS.g_utf16_offset_to_pointer (ptr, start) - ptr); int byteEnd = (int)(OS.g_utf16_offset_to_pointer (ptr, end + 1) - ptr); int strlen = C.strlen(ptr); byteStart = Math.min(byteStart, strlen); byteEnd = Math.min(byteEnd, strlen); int[] ranges = new int[]{byteStart, byteEnd}; long clipRegion = metricsAdapter.gdk_pango_layout_get_clip_region(layout, 0, 0, ranges, 1); if (clipRegion == 0) return new Rectangle(0, 0, 0, 0); cairo_rectangle_int_t rect = new cairo_rectangle_int_t(); /* * Bug in Pango. The region returned by gdk_pango_layout_get_clip_region() * includes areas from lines outside of the requested range. The fix * is to subtract these areas from the clip region. */ PangoRectangle pangoRect = new PangoRectangle(); long iter = OS.pango_layout_get_iter(layout); if (iter == 0) SWT.error(SWT.ERROR_NO_HANDLES); long linesRegion = Cairo.cairo_region_create(); if (linesRegion == 0) SWT.error(SWT.ERROR_NO_HANDLES); int lineEnd = 0; do { metricsAdapter.pango_layout_iter_get_line_extents(iter, null, pangoRect); if (OS.pango_layout_iter_next_line(iter)) { lineEnd = OS.pango_layout_iter_get_index(iter) - 1; } else { lineEnd = strlen; } if (byteStart > lineEnd) continue; rect.x = OS.PANGO_PIXELS(pangoRect.x); rect.y = OS.PANGO_PIXELS(pangoRect.y); rect.width = OS.PANGO_PIXELS(pangoRect.width); rect.height = OS.PANGO_PIXELS(pangoRect.height); Cairo.cairo_region_union_rectangle(linesRegion, rect); } while (lineEnd + 1 <= byteEnd); Cairo.cairo_region_intersect(clipRegion, linesRegion); Cairo.cairo_region_destroy(linesRegion); OS.pango_layout_iter_free(iter); Cairo.cairo_region_get_extents(clipRegion, rect); Cairo.cairo_region_destroy(clipRegion); rect.x += Math.min (indent, wrapIndent); return new Rectangle(rect.x, rect.y, rect.width, rect.height + getScaledVerticalIndent()); } /** * Returns the descent of the receiver. * * @return the descent * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @see #getAscent() * @see #setAscent(int) * @see #setDescent(int) * @see #getLineMetrics(int) */ public int getDescent () { checkLayout(); return descentInPoints; } /** * Returns the default font currently being used by the receiver * to draw and measure text. * * @return the receiver's font * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
*/ public Font getFont () { checkLayout(); return font; } /** * Returns the receiver's indent. * * @return the receiver's indent * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @since 3.2 */ public int getIndent () { checkLayout(); return DPIUtil.autoScaleDown(getDevice(), getIndentInPixels()); } int getIndentInPixels () { return indent; } /** * Returns the receiver's justification. * * @return the receiver's justification * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @since 3.2 */ public boolean getJustify () { checkLayout(); return OS.pango_layout_get_justify(layout); } /** * Returns the embedding level for the specified character offset. The * embedding level is usually used to determine the directionality of a * character in bidirectional text. * * @param offset the character offset * @return the embedding level * * @exception IllegalArgumentException
    *
  • ERROR_INVALID_ARGUMENT - if the character offset is out of range
  • *
* @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
*/ public int getLevel(int offset) { checkLayout(); computeRuns(); int length = text.length(); if (!(0 <= offset && offset <= length)) SWT.error(SWT.ERROR_INVALID_RANGE); offset = translateOffset(offset); long iter = OS.pango_layout_get_iter(layout); if (iter == 0) SWT.error(SWT.ERROR_NO_HANDLES); int level = 0; PangoItem item = new PangoItem(); PangoLayoutRun run = new PangoLayoutRun(); long ptr = OS.pango_layout_get_text(layout); long byteOffset = OS.g_utf16_offset_to_pointer(ptr, offset) - ptr; int strlen = C.strlen(ptr); byteOffset = Math.min(byteOffset, strlen); do { long runPtr = OS.pango_layout_iter_get_run(iter); if (runPtr != 0) { OS.memmove(run, runPtr, PangoLayoutRun.sizeof); OS.memmove(item, run.item, PangoItem.sizeof); if (item.offset <= byteOffset && byteOffset < item.offset + item.length) { level = item.analysis_level; break; } } } while (OS.pango_layout_iter_next_run(iter)); OS.pango_layout_iter_free(iter); return level; } /** * Returns the bounds of the line for the specified line index. * * @param lineIndex the line index * @return the line bounds * * @exception IllegalArgumentException
    *
  • ERROR_INVALID_ARGUMENT - if the line index is out of range
  • *
* @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
*/ public Rectangle getLineBounds(int lineIndex) { checkLayout(); return DPIUtil.autoScaleDown(getDevice(), getLineBoundsInPixels(lineIndex)); } Rectangle getLineBoundsInPixels(int lineIndex) { computeRuns(); int lineCount = OS.pango_layout_get_line_count(layout); if (!(0 <= lineIndex && lineIndex < lineCount)) SWT.error(SWT.ERROR_INVALID_RANGE); long iter = OS.pango_layout_get_iter(layout); for (int i = 0; i < lineIndex; i++) { OS.pango_layout_iter_next_line(iter); } Rectangle lineBoundsInPixels = getLineBoundsInPixels(lineIndex, iter); OS.pango_layout_iter_free(iter); return lineBoundsInPixels; } private Rectangle getLineBoundsInPixels(int lineIndex, long iter) { if (iter == 0) SWT.error(SWT.ERROR_NO_HANDLES); PangoRectangle rect = new PangoRectangle(); metricsAdapter.pango_layout_iter_get_line_extents(iter, null, rect); int x = OS.PANGO_PIXELS(rect.x); int y = OS.PANGO_PIXELS(rect.y); int width = OS.PANGO_PIXELS(rect.width); int height = OS.PANGO_PIXELS(rect.height); if (ascentInPoints != -1 && descentInPoints != -1) { height = Math.max (height, DPIUtil.autoScaleUp(getDevice(), ascentInPoints + descentInPoints)); } x += Math.min (indent, wrapIndent); return new Rectangle(x, y, width, height); } /** * Returns the receiver's line count. This includes lines caused * by wrapping. * * @return the line count * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
*/ public int getLineCount() { checkLayout (); computeRuns(); return OS.pango_layout_get_line_count(layout); } /** * Returns the index of the line that contains the specified * character offset. * * @param offset the character offset * @return the line index * * @exception IllegalArgumentException
    *
  • ERROR_INVALID_ARGUMENT - if the character offset is out of range
  • *
* @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
*/ public int getLineIndex(int offset) { checkLayout (); computeRuns(); int length = text.length(); if (!(0 <= offset && offset <= length)) SWT.error(SWT.ERROR_INVALID_ARGUMENT); offset = translateOffset(offset); int line = 0; long ptr = OS.pango_layout_get_text(layout); long byteOffset = OS.g_utf16_offset_to_pointer(ptr,offset) - ptr; int strlen = C.strlen(ptr); byteOffset = Math.min(byteOffset, strlen); long iter = OS.pango_layout_get_iter(layout); if (iter == 0) SWT.error(SWT.ERROR_NO_HANDLES); while (OS.pango_layout_iter_next_line(iter)) { if (OS.pango_layout_iter_get_index(iter) > byteOffset) break; line++; } OS.pango_layout_iter_free(iter); return line; } /** * Returns the font metrics for the specified line index. * * @param lineIndex the line index * @return the font metrics * * @exception IllegalArgumentException
    *
  • ERROR_INVALID_ARGUMENT - if the line index is out of range
  • *
* @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
*/ public FontMetrics getLineMetrics (int lineIndex) { if (metricsAdapter.isFixedMetrics()) { return metricsAdapter.getFixedLineMetrics(getDevice()); } checkLayout (); computeRuns(); int lineCount = OS.pango_layout_get_line_count(layout); if (!(0 <= lineIndex && lineIndex < lineCount)) SWT.error(SWT.ERROR_INVALID_RANGE); PangoLayoutLine line = new PangoLayoutLine(); OS.memmove(line, OS.pango_layout_get_line(layout, lineIndex), PangoLayoutLine.sizeof); int heightInPoints; int ascentInPoints; if (line.runs == 0) { long font = this.font != null ? this.font.handle : device.systemFont.handle; long lang = OS.pango_context_get_language(context); long metrics = OS.pango_context_get_metrics(context, font, lang); int ascent = OS.pango_font_metrics_get_ascent(metrics); int descent = OS.pango_font_metrics_get_descent(metrics); ascentInPoints = DPIUtil.autoScaleDown(getDevice(), OS.PANGO_PIXELS(ascent)); heightInPoints = DPIUtil.autoScaleDown(getDevice(), OS.PANGO_PIXELS(ascent + descent)); OS.pango_font_metrics_unref(metrics); } else { PangoRectangle rect = new PangoRectangle(); metricsAdapter.pango_layout_line_get_extents(OS.pango_layout_get_line(layout, lineIndex), null, rect); ascentInPoints = DPIUtil.autoScaleDown(getDevice(), OS.PANGO_PIXELS(-rect.y)); heightInPoints = DPIUtil.autoScaleDown(getDevice(), OS.PANGO_PIXELS(rect.height)); } heightInPoints = Math.max(this.ascentInPoints + this.descentInPoints, heightInPoints); ascentInPoints = Math.max(this.ascentInPoints, ascentInPoints); int descentInPoints = heightInPoints - ascentInPoints; FontMetrics fm = new FontMetrics(); fm.ascentInPoints = ascentInPoints; fm.descentInPoints = descentInPoints; fm.averageCharWidthInPoints = 0; return fm; } /** * Returns the line offsets. Each value in the array is the * offset for the first character in a line except for the last * value, which contains the length of the text. * * @return the line offsets * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
*/ public int[] getLineOffsets() { checkLayout(); computeRuns(); int lineCount = OS.pango_layout_get_line_count(layout); int[] offsets = new int [lineCount + 1]; long ptr = OS.pango_layout_get_text(layout); PangoLayoutLine line = new PangoLayoutLine(); for (int i = 0; i < lineCount; i++) { long linePtr = OS.pango_layout_get_line(layout, i); OS.memmove(line, linePtr, PangoLayoutLine.sizeof); int pos = (int)OS.g_utf16_pointer_to_offset(ptr, ptr + line.start_index); offsets[i] = untranslateOffset(pos); } offsets[lineCount] = text.length(); return offsets; } /** * Returns the location for the specified character offset. The * trailing argument indicates whether the offset * corresponds to the leading or trailing edge of the cluster. * * @param offset the character offset * @param trailing the trailing flag * @return the location of the character offset * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @see #getOffset(Point, int[]) * @see #getOffset(int, int, int[]) */ public Point getLocation(int offset, boolean trailing) { checkLayout(); return DPIUtil.autoScaleDown(getDevice(), getLocationInPixels(offset, trailing)); } Point getLocationInPixels(int offset, boolean trailing) { computeRuns(); int length = text.length(); if (!(0 <= offset && offset <= length)) SWT.error(SWT.ERROR_INVALID_RANGE); offset = translateOffset(offset); long ptr = OS.pango_layout_get_text(layout); int byteOffset = (int)(OS.g_utf16_offset_to_pointer(ptr, offset) - ptr); int strlen = C.strlen(ptr); byteOffset = Math.min(byteOffset, strlen); PangoRectangle pos = new PangoRectangle(); OS.pango_layout_index_to_pos(layout, byteOffset, pos); int x = trailing ? pos.x + pos.width : pos.x; int y = pos.y; x = OS.PANGO_PIXELS(x); if (OS.pango_context_get_base_dir(context) == OS.PANGO_DIRECTION_RTL) { x = width() - x; } x += Math.min (indent, wrapIndent); return new Point(x, OS.PANGO_PIXELS(y) + getScaledVerticalIndent()); } /** * Returns the next offset for the specified offset and movement * type. The movement is one of SWT.MOVEMENT_CHAR, * SWT.MOVEMENT_CLUSTER, SWT.MOVEMENT_WORD, * SWT.MOVEMENT_WORD_END or SWT.MOVEMENT_WORD_START. * * @param offset the start offset * @param movement the movement type * @return the next offset * * @exception IllegalArgumentException
    *
  • ERROR_INVALID_ARGUMENT - if the offset is out of range
  • *
* @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @see #getPreviousOffset(int, int) */ public int getNextOffset (int offset, int movement) { return _getOffset(offset, movement, true); } int _getOffset (int offset, int movement, boolean forward) { checkLayout(); computeRuns(); int length = text.length(); if (!(0 <= offset && offset <= length)) SWT.error(SWT.ERROR_INVALID_RANGE); if (forward) { if (offset == length) return length; } else { if (offset == 0) return 0; } int step = forward ? 1 : -1; if ((movement & SWT.MOVEMENT_CHAR) != 0) return offset + step; long [] attrs = new long [1]; int[] nAttrs = new int[1]; OS.pango_layout_get_log_attrs(layout, attrs, nAttrs); if (attrs[0] == 0) return offset + step; long ptr = OS.pango_layout_get_text(layout); int utf8Offset = (int)OS.g_utf16_offset_to_utf8_offset (ptr, translateOffset(offset)); int utf8Length = (int)OS.g_utf8_strlen(ptr, -1); utf8Offset += step; PangoLogAttr logAttr = new PangoLogAttr(); while (0 <= utf8Offset && utf8Offset <= utf8Length) { OS.memmove(logAttr, attrs[0] + utf8Offset * PangoLogAttr.sizeof, PangoLogAttr.sizeof); boolean found = false, limit = false; if (((movement & SWT.MOVEMENT_CLUSTER) != 0) && logAttr.is_cursor_position) found = true; if ((movement & SWT.MOVEMENT_WORD) != 0) { if (forward) { if (logAttr.is_word_end) found = true; } else { if (logAttr.is_word_start) found = true; } } if ((movement & SWT.MOVEMENT_WORD_START) != 0) { if (logAttr.is_word_start) found = true; if (logAttr.is_sentence_end) found = true; } if ((movement & SWT.MOVEMENT_WORD_END) != 0) { if (logAttr.is_word_end) found = true; if (logAttr.is_sentence_start) found = true; } if (forward) { if (utf8Offset == utf8Length) limit = true; } else { if (utf8Offset == 0) limit = true; } if (found || limit) { int testOffset = (int)OS.g_utf8_offset_to_utf16_offset (ptr, utf8Offset); if (found && invalidOffsets != null) { for (int i = 0; i < invalidOffsets.length; i++) { if (testOffset == invalidOffsets[i]) { found = false; break; } } } if (found || limit) { offset = untranslateOffset(testOffset); break; } } utf8Offset += step; } OS.g_free(attrs[0]); return Math.min(Math.max(0, offset), length); } /** * Returns the character offset for the specified point. * For a typical character, the trailing argument will be filled in to * indicate whether the point is closer to the leading edge (0) or * the trailing edge (1). When the point is over a cluster composed * of multiple characters, the trailing argument will be filled with the * position of the character in the cluster that is closest to * the point. * * @param point the point * @param trailing the trailing buffer * @return the character offset * * @exception IllegalArgumentException
    *
  • ERROR_INVALID_ARGUMENT - if the trailing length is less than 1
  • *
  • ERROR_NULL_ARGUMENT - if the point is null
  • *
* @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @see #getLocation(int, boolean) */ public int getOffset(Point point, int[] trailing) { checkLayout(); return getOffsetInPixels(DPIUtil.autoScaleUp(getDevice(), point), trailing); } int getOffsetInPixels(Point point, int[] trailing) { if (point == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); return getOffsetInPixels(point.x, point.y, trailing); } /** * Returns the character offset for the specified point. * For a typical character, the trailing argument will be filled in to * indicate whether the point is closer to the leading edge (0) or * the trailing edge (1). When the point is over a cluster composed * of multiple characters, the trailing argument will be filled with the * position of the character in the cluster that is closest to * the point. * * @param x the x coordinate of the point * @param y the y coordinate of the point * @param trailing the trailing buffer * @return the character offset * * @exception IllegalArgumentException
    *
  • ERROR_INVALID_ARGUMENT - if the trailing length is less than 1
  • *
* @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @see #getLocation(int, boolean) */ public int getOffset(int x, int y, int[] trailing) { checkLayout(); return getOffset(new Point(x, y), trailing); } int getOffsetInPixels(int x, int y, int[] trailing) { computeRuns(); if (trailing != null && trailing.length < 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); x -= Math.min (indent, wrapIndent); if (OS.pango_context_get_base_dir(context) == OS.PANGO_DIRECTION_RTL) { x = width() - x; } /* * Feature in GTK. pango_layout_xy_to_index() returns the * logical end/start offset of a line when the coordinates are outside * the line bounds. In SWT the correct behavior is to return the closest * visual offset. The fix is to clamp the coordinates inside the * line bounds. */ long iter = OS.pango_layout_get_iter(layout); if (iter == 0) SWT.error(SWT.ERROR_NO_HANDLES); PangoRectangle rect = new PangoRectangle(); do { metricsAdapter.pango_layout_iter_get_line_extents(iter, null, rect); rect.y = OS.PANGO_PIXELS(rect.y); rect.height = OS.PANGO_PIXELS(rect.height); if (rect.y <= y && y < rect.y + rect.height) { rect.x = OS.PANGO_PIXELS(rect.x); rect.width = OS.PANGO_PIXELS(rect.width); if (x >= rect.x + rect.width) x = rect.x + rect.width - 1; if (x < rect.x) x = rect.x; break; } } while (OS.pango_layout_iter_next_line(iter)); OS.pango_layout_iter_free(iter); int[] index = new int[1]; int[] piTrailing = new int[1]; OS.pango_layout_xy_to_index(layout, x * OS.PANGO_SCALE, y * OS.PANGO_SCALE, index, piTrailing); long ptr = OS.pango_layout_get_text(layout); int offset = (int)OS.g_utf16_pointer_to_offset(ptr, ptr + index[0]); if (trailing != null) { trailing[0] = piTrailing[0]; if (piTrailing[0] != 0) { trailing[0] = (int)OS.g_utf8_offset_to_utf16_offset(ptr, OS.g_utf8_pointer_to_offset(ptr, ptr + index[0]) + piTrailing[0]) - offset; } } return untranslateOffset(offset); } /** * Returns the orientation of the receiver. * * @return the orientation style * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
*/ public int getOrientation() { checkLayout(); int baseDir = OS.pango_context_get_base_dir(context); return baseDir == OS.PANGO_DIRECTION_RTL ? SWT.RIGHT_TO_LEFT : SWT.LEFT_TO_RIGHT; } /** * Returns the previous offset for the specified offset and movement * type. The movement is one of SWT.MOVEMENT_CHAR, * SWT.MOVEMENT_CLUSTER or SWT.MOVEMENT_WORD, * SWT.MOVEMENT_WORD_END or SWT.MOVEMENT_WORD_START. * * @param offset the start offset * @param movement the movement type * @return the previous offset * * @exception IllegalArgumentException
    *
  • ERROR_INVALID_ARGUMENT - if the offset is out of range
  • *
* @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @see #getNextOffset(int, int) */ public int getPreviousOffset (int offset, int movement) { return _getOffset(offset, movement, false); } /** * Gets the ranges of text that are associated with a TextStyle. * * @return the ranges, an array of offsets representing the start and end of each * text style. * * @exception SWTException
    *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • *
* * @see #getStyles() * * @since 3.2 */ public int[] getRanges () { checkLayout(); int[] result = new int[stylesCount * 2]; int count = 0; for (int i=0; i *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • * */ public int[] getSegments() { checkLayout(); return segments; } /** * Returns the segments characters of the receiver. * * @return the segments characters * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @since 3.6 */ public char[] getSegmentsChars () { checkLayout(); return segmentsChars; } String getSegmentsText() { int length = text.length(); if (length == 0) return text; if (segments == null) return text; int nSegments = segments.length; if (nSegments == 0) return text; if (segmentsChars == null) { if (nSegments == 1) return text; if (nSegments == 2) { if (segments[0] == 0 && segments[1] == length) return text; } } char[] oldChars = new char[length]; text.getChars(0, length, oldChars, 0); char[] newChars = new char[length + nSegments]; int charCount = 0, segmentCount = 0; char defaultSeparator = getOrientation() == SWT.RIGHT_TO_LEFT ? RTL_MARK : LTR_MARK; while (charCount < length) { if (segmentCount < nSegments && charCount == segments[segmentCount]) { char separator = segmentsChars != null && segmentsChars.length > segmentCount ? segmentsChars[segmentCount] : defaultSeparator; newChars[charCount + segmentCount++] = separator; } else { newChars[charCount + segmentCount] = oldChars[charCount++]; } } while (segmentCount < nSegments) { segments[segmentCount] = charCount; char separator = segmentsChars != null && segmentsChars.length > segmentCount ? segmentsChars[segmentCount] : defaultSeparator; newChars[charCount + segmentCount++] = separator; } return new String(newChars, 0, newChars.length); } /** * Returns the line spacing of the receiver. * * @return the line spacing * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    */ public int getSpacing () { checkLayout(); return DPIUtil.autoScaleDown(getDevice(), getSpacingInPixels()); } int getSpacingInPixels () { return OS.PANGO_PIXELS(OS.pango_layout_get_spacing(layout)); } /** * Returns the vertical indent of the receiver. * * @return the vertical indent * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * @since 3.109 */ public int getVerticalIndent () { checkLayout(); return verticalIndentInPoints; } /** * Returns the scaled vertical indent. * * @return the scaled vertical indent. * @since 3.109 */ private int getScaledVerticalIndent() { if (verticalIndentInPoints == 0) { return verticalIndentInPoints; } return DPIUtil.autoScaleUp(getDevice(), verticalIndentInPoints); } /** * Gets the style of the receiver at the specified character offset. * * @param offset the text offset * @return the style or null if not set * * @exception IllegalArgumentException
      *
    • ERROR_INVALID_ARGUMENT - if the character offset is out of range
    • *
    * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    */ public TextStyle getStyle (int offset) { checkLayout(); int length = text.length(); if (!(0 <= offset && offset < length)) SWT.error(SWT.ERROR_INVALID_RANGE); for (int i=1; i offset) { return styles[i - 1].style; } } return null; } /** * Gets all styles of the receiver. * * @return the styles * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @see #getRanges() * * @since 3.2 */ public TextStyle[] getStyles () { checkLayout(); TextStyle[] result = new TextStyle[stylesCount]; int count = 0; for (int i=0; i *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • * */ public int[] getTabs() { checkLayout(); return DPIUtil.autoScaleDown (getDevice(), getTabsInPixels ()); } int[] getTabsInPixels () { return tabs; } /** * Gets the receiver's text, which will be an empty * string if it has never been set. * * @return the receiver's text * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    */ public String getText () { checkLayout (); return text; } /** * Returns the text direction of the receiver. * * @return the text direction value * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * @since 3.103 */ public int getTextDirection () { return getOrientation (); } /** * Returns the width of the receiver. * * @return the width * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    */ public int getWidth () { checkLayout (); return DPIUtil.autoScaleDown(getDevice(), getWidthInPixels()); } int getWidthInPixels () { return wrapWidth; } /** * Returns the receiver's wrap indent. * * @return the receiver's wrap indent * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @since 3.6 */ public int getWrapIndent () { checkLayout (); return DPIUtil.autoScaleDown(getDevice(), getWrapIndentInPixels()); } int getWrapIndentInPixels () { return wrapIndent; } /** * Returns true if the text layout has been disposed, * and false otherwise. *

    * This method gets the dispose state for the text layout. * When a text layout has been disposed, it is an error to * invoke any other method (except {@link #dispose()}) using the text layout. *

    * * @return true when the text layout is disposed and false otherwise */ @Override public boolean isDisposed () { return layout == 0; } /** * Sets the text alignment for the receiver. The alignment controls * how a line of text is positioned horizontally. The argument should * be one of SWT.LEFT, SWT.RIGHT or SWT.CENTER. *

    * The default alignment is SWT.LEFT. Note that the receiver's * width must be set in order to use SWT.RIGHT or SWT.CENTER * alignment. *

    * * @param alignment the new alignment * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @see #setWidth(int) */ public void setAlignment (int alignment) { checkLayout(); int mask = SWT.LEFT | SWT.CENTER | SWT.RIGHT; alignment &= mask; if (alignment == 0) return; if ((alignment & SWT.LEFT) != 0) alignment = SWT.LEFT; if ((alignment & SWT.RIGHT) != 0) alignment = SWT.RIGHT; boolean rtl = OS.pango_context_get_base_dir(context) == OS.PANGO_DIRECTION_RTL; int align = OS.PANGO_ALIGN_CENTER; switch (alignment) { case SWT.LEFT: align = rtl ? OS.PANGO_ALIGN_RIGHT : OS.PANGO_ALIGN_LEFT; break; case SWT.RIGHT: align = rtl ? OS.PANGO_ALIGN_LEFT : OS.PANGO_ALIGN_RIGHT; break; } OS.pango_layout_set_alignment(layout, align); } /** * Sets the ascent of the receiver. The ascent is distance in points * from the baseline to the top of the line and it is applied to all * lines. The default value is -1 which means that the * ascent is calculated from the line fonts. * * @param ascent the new ascent * * @exception IllegalArgumentException
      *
    • ERROR_INVALID_ARGUMENT - if the ascent is less than -1
    • *
    * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @see #setDescent(int) * @see #getLineMetrics(int) */ public void setAscent (int ascent) { checkLayout(); if (ascent < -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (this.ascentInPoints == ascent) return; freeRuns(); this.ascentInPoints = ascent; } /** * Sets the descent of the receiver. The descent is distance in points * from the baseline to the bottom of the line and it is applied to all * lines. The default value is -1 which means that the * descent is calculated from the line fonts. * * @param descent the new descent * * @exception IllegalArgumentException
      *
    • ERROR_INVALID_ARGUMENT - if the descent is less than -1
    • *
    * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @see #setAscent(int) * @see #getLineMetrics(int) */ public void setDescent (int descent) { checkLayout(); if (descent < -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (this.descentInPoints == descent) return; freeRuns(); this.descentInPoints = descent; } /** * Forces line heights in receiver to obey provided value. This is * useful with texts that contain glyphs from different scripts, * such as mixing latin glyphs with hieroglyphs or emojis. *

    * Text lines with different metrics will be forced to fit. This means * painting text in such a way that its baseline is where specified by * given 'metrics'. This can sometimes introduce small visual artifacs, * such as taller lines overpainting or being clipped by content above * and below. *

    * The possible ways to set FontMetrics include: *
      *
    • Obtaining 'FontMetrics' via {@link GC#getFontMetrics}. Note that * this will only obtain metrics for currently selected font and will not * account for font fallbacks (for example, with a latin font selected, * painting hieroglyphs usually involves a fallback font).
    • *
    • Obtaining 'FontMetrics' via a temporary 'TextLayout'. This would * involve setting a desired text sample to 'TextLayout', then measuring * it with {@link TextLayout#getLineMetrics(int)}. This approach will also * take fallback fonts into account.
    • *
    * * NOTE: Does not currently support (as in, undefined behavior) multi-line * layouts, including those caused by word wrapping. StyledText uses one * TextLayout per line and is only affected by word wrap restriction. * * @since 3.125 */ public void setFixedLineMetrics (FontMetrics metrics) { metricsAdapter.setFixedLineMetrics(getDevice(), metrics); } /** * Sets the default font which will be used by the receiver * to draw and measure text. If the * argument is null, then a default font appropriate * for the platform will be used instead. Note that a text * style can override the default font. * * @param font the new font for the receiver, or null to indicate a default font * * @exception IllegalArgumentException
      *
    • ERROR_INVALID_ARGUMENT - if the font has been disposed
    • *
    * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    */ public void setFont (Font font) { checkLayout (); if (font != null && font.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); Font oldFont = this.font; if (oldFont == font) return; freeRuns(); this.font = font; if (oldFont != null && oldFont.equals(font)) return; OS.pango_layout_set_font_description(layout, font != null ? font.handle : device.systemFont.handle); } /** * Sets the indent of the receiver. This indent is applied to the first line of * each paragraph. * * @param indent new indent * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @see #setWrapIndent(int) * * @since 3.2 */ public void setIndent (int indent) { checkLayout (); setIndentInPixels(DPIUtil.autoScaleUp(getDevice(), indent)); } void setIndentInPixels (int indent) { checkLayout(); if (indent < 0) return; if (this.indent == indent) return; this.indent = indent; OS.pango_layout_set_indent(layout, (indent - wrapIndent) * OS.PANGO_SCALE); if (wrapWidth != -1) setWidth(); } /** * Sets the justification of the receiver. Note that the receiver's * width must be set in order to use justification. * * @param justify new justify * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @since 3.2 */ public void setJustify (boolean justify) { checkLayout(); OS.pango_layout_set_justify(layout, justify); } /** * Sets the orientation of the receiver, which must be one * of SWT.LEFT_TO_RIGHT or SWT.RIGHT_TO_LEFT. * * @param orientation new orientation style * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    */ public void setOrientation(int orientation) { checkLayout(); int mask = SWT.RIGHT_TO_LEFT | SWT.LEFT_TO_RIGHT; orientation &= mask; if (orientation == 0) return; if ((orientation & SWT.LEFT_TO_RIGHT) != 0) orientation = SWT.LEFT_TO_RIGHT; int baseDir = orientation == SWT.RIGHT_TO_LEFT ? OS.PANGO_DIRECTION_RTL : OS.PANGO_DIRECTION_LTR; if (OS.pango_context_get_base_dir(context) == baseDir) return; freeRuns(); OS.pango_context_set_base_dir(context, baseDir); OS.pango_layout_context_changed(layout); int align = OS.pango_layout_get_alignment(layout); if (align != OS.PANGO_ALIGN_CENTER) { align = align == OS.PANGO_ALIGN_LEFT ? OS.PANGO_ALIGN_RIGHT : OS.PANGO_ALIGN_LEFT; OS.pango_layout_set_alignment(layout, align); } } /** * Sets the line spacing of the receiver. The line spacing * is the space left between lines. * * @param spacing the new line spacing * * @exception IllegalArgumentException
      *
    • ERROR_INVALID_ARGUMENT - if the spacing is negative
    • *
    * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    */ public void setSpacing (int spacing) { checkLayout(); if (spacing < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); setSpacingInPixels(DPIUtil.autoScaleUp(getDevice(), spacing)); } void setSpacingInPixels (int spacing) { OS.pango_layout_set_spacing(layout, spacing * OS.PANGO_SCALE); } /** * Sets the vertical indent of the receiver. The vertical indent * is the space left before the first line. * * @param verticalIndent the new vertical indent * * @exception IllegalArgumentException
      *
    • ERROR_INVALID_ARGUMENT - if the vertical indent is negative
    • *
    * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * @since 3.109 */ public void setVerticalIndent (int verticalIndent) { checkLayout(); if (verticalIndent < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (this.verticalIndentInPoints == verticalIndent) return; this.verticalIndentInPoints = verticalIndent; } /** * Sets the offsets of the receiver's text segments. Text segments are used to * override the default behavior of the bidirectional algorithm. * Bidirectional reordering can happen within a text segment but not * between two adjacent segments. *

    * Each text segment is determined by two consecutive offsets in the * segments arrays. The first element of the array should * always be zero and the last one should always be equals to length of * the text. *

    *

    * When segments characters are set, the segments are the offsets where * the characters are inserted in the text. *

    * * @param segments the text segments offset * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @see #setSegmentsChars(char[]) */ public void setSegments(int[] segments) { checkLayout(); if (this.segments == null && segments == null) return; if (this.segments != null && segments !=null) { if (this.segments.length == segments.length) { int i; for (i = 0; i setSegments(int[]). The application can * use this API to insert Unicode Control Characters in the text to control * the display of the text and bidi reordering. The characters are not * accessible by any other API in TextLayout. * * @param segmentsChars the segments characters * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @see #setSegments(int[]) * * @since 3.6 */ public void setSegmentsChars(char[] segmentsChars) { checkLayout(); if (this.segmentsChars == null && segmentsChars == null) return; if (this.segmentsChars != null && segmentsChars != null) { if (this.segmentsChars.length == segmentsChars.length) { int i; for (i = 0; i *
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • * */ public void setStyle (TextStyle style, int start, int end) { checkLayout(); int length = text.length(); if (length == 0) return; if (start > end) return; start = Math.min(Math.max(0, start), length - 1); end = Math.min(Math.max(0, end), length - 1); /* * Bug in Pango. Pango 1.2.2 will cause a segmentation fault if a style * is not applied for a whole ligature. The fix is to applied the * style for the whole ligature. * * NOTE that fix only LamAlef ligatures. */ if (start > 0 && isAlef(text.charAt(start)) && isLam(text.charAt(start - 1))) { start--; } if (end < length - 1 && isLam(text.charAt(end)) && isAlef(text.charAt(end + 1))) { end++; } int low = -1; int high = stylesCount; while (high - low > 1) { int index = (high + low) / 2; if (styles[index + 1].start > start) { high = index; } else { low = index; } } if (0 <= high && high < stylesCount) { StyleItem item = styles[high]; if (item.start == start && styles[high + 1].start - 1 == end) { if (style == null) { if (item.style == null) return; } else { if (style.equals(item.style)) return; } } } freeRuns(); int modifyStart = high; int modifyEnd = modifyStart; while (modifyEnd < stylesCount) { if (styles[modifyEnd + 1].start > end) break; modifyEnd++; } if (modifyStart == modifyEnd) { int styleStart = styles[modifyStart].start; int styleEnd = styles[modifyEnd + 1].start - 1; if (styleStart == start && styleEnd == end) { styles[modifyStart].style = style; return; } if (styleStart != start && styleEnd != end) { int newLength = stylesCount + 2; if (newLength > styles.length) { int newSize = Math.min(newLength + 1024, Math.max(64, newLength * 2)); StyleItem[] newStyles = new StyleItem[newSize]; System.arraycopy(styles, 0, newStyles, 0, stylesCount); styles = newStyles; } System.arraycopy(styles, modifyEnd + 1, styles, modifyEnd + 3, stylesCount - modifyEnd - 1); StyleItem item = new StyleItem(); item.start = start; item.style = style; styles[modifyStart + 1] = item; item = new StyleItem(); item.start = end + 1; item.style = styles[modifyStart].style; styles[modifyStart + 2] = item; stylesCount = newLength; return; } } if (start == styles[modifyStart].start) modifyStart--; if (end == styles[modifyEnd + 1].start - 1) modifyEnd++; int newLength = stylesCount + 1 - (modifyEnd - modifyStart - 1); if (newLength > styles.length) { int newSize = Math.min(newLength + 1024, Math.max(64, newLength * 2)); StyleItem[] newStyles = new StyleItem[newSize]; System.arraycopy(styles, 0, newStyles, 0, stylesCount); styles = newStyles; } System.arraycopy(styles, modifyEnd, styles, modifyStart + 2, stylesCount - modifyEnd); StyleItem item = new StyleItem(); item.start = start; item.style = style; styles[modifyStart + 1] = item; styles[modifyStart + 2].start = end + 1; stylesCount = newLength; } /** * Sets the receiver's tab list. Each value in the tab list specifies * the space in points from the origin of the text layout to the respective * tab stop. The last tab stop width is repeated continuously. * * @param tabs the new tab list * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    */ public void setTabs(int[] tabs) { checkLayout(); if (this.tabs == null && tabs == null) return; setTabsInPixels (DPIUtil.autoScaleUp (getDevice(), tabs)); } void setTabsInPixels (int[] tabs) { if (Arrays.equals (this.tabs, tabs)) return; this.tabs = tabs; if (tabs == null) { OS.pango_layout_set_tabs(layout, device.emptyTab); } else { long tabArray = OS.pango_tab_array_new(tabs.length, true); if (tabArray != 0) { for (int i = 0; i < tabs.length; i++) { OS.pango_tab_array_set_tab(tabArray, i, OS.PANGO_TAB_LEFT, tabs[i]); } OS.pango_layout_set_tabs(layout, tabArray); OS.pango_tab_array_free(tabArray); } } /* * Bug in Pango. A change in the tab stop array is not automatically reflected in the * pango layout object because the call pango_layout_set_tabs() does not free the * lines cache. The fix to use pango_layout_context_changed() to free the lines cache. */ OS.pango_layout_context_changed(layout); } /** * Sets the receiver's text. *

    * Note: Setting the text also clears all the styles. This method * returns without doing anything if the new text is the same as * the current text. *

    * * @param text the new text * * @exception IllegalArgumentException
      *
    • ERROR_NULL_ARGUMENT - if the text is null
    • *
    * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    */ public void setText (String text) { checkLayout (); if (text == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (text.equals(this.text)) return; freeRuns(); this.text = text; styles = new StyleItem[2]; styles[0] = new StyleItem(); styles[1] = new StyleItem(); styles[1].start = text.length(); stylesCount = 2; } /** * Sets the text direction of the receiver, which must be one * of SWT.LEFT_TO_RIGHT, SWT.RIGHT_TO_LEFT * or SWT.AUTO_TEXT_DIRECTION. * *

    * Warning: This API is currently only implemented on Windows. * It doesn't set the base text direction on GTK and Cocoa. *

    * * @param textDirection the new text direction * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * @since 3.103 */ public void setTextDirection (int textDirection) { checkLayout(); } /** * Sets the line width of the receiver, which determines how * text should be wrapped and aligned. The default value is * -1 which means wrapping is disabled. * * @param width the new width * * @exception IllegalArgumentException
      *
    • ERROR_INVALID_ARGUMENT - if the width is 0 or less than -1
    • *
    * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @see #setAlignment(int) */ public void setWidth (int width) { checkLayout (); if (width < -1 || width == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); setWidthInPixels(DPIUtil.autoScaleUp(getDevice(), width)); } void setWidthInPixels (int width) { if (wrapWidth == width) return; freeRuns(); wrapWidth = width; setWidth(); } void setWidth () { if (wrapWidth == -1) { OS.pango_layout_set_width(layout, -1); boolean rtl = OS.pango_context_get_base_dir(context) == OS.PANGO_DIRECTION_RTL; OS.pango_layout_set_alignment(layout, rtl ? OS.PANGO_ALIGN_RIGHT : OS.PANGO_ALIGN_LEFT); } else { int margin = Math.min (indent, wrapIndent); OS.pango_layout_set_width(layout, (wrapWidth - margin) * OS.PANGO_SCALE); } } /** * Sets the wrap indent of the receiver. This indent is applied to all lines * in the paragraph except the first line. * * @param wrapIndent new wrap indent * * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @see #setIndent(int) * * @since 3.6 */ public void setWrapIndent (int wrapIndent) { checkLayout(); if (wrapIndent < 0) return; setWrapIndentInPixels(DPIUtil.autoScaleUp(getDevice(), wrapIndent)); } void setWrapIndentInPixels (int wrapIndent) { if (this.wrapIndent == wrapIndent) return; this.wrapIndent = wrapIndent; OS.pango_layout_set_indent(layout, (indent - wrapIndent) * OS.PANGO_SCALE); if (wrapWidth != -1) setWidth(); } static final boolean isLam(int ch) { return ch == 0x0644; } static final boolean isAlef(int ch) { switch (ch) { case 0x0622: case 0x0623: case 0x0625: case 0x0627: case 0x0649: case 0x0670: case 0x0671: case 0x0672: case 0x0673: case 0x0675: return true; } return false; } /** * Returns a string containing a concise, human-readable * description of the receiver. * * @return a string representation of the receiver */ @Override public String toString () { if (isDisposed()) return "TextLayout {*DISPOSED*}"; return "TextLayout {" + layout + "}"; } /* * Translate a client offset to an internal offset */ int translateOffset(int offset) { int length = text.length(); if (length == 0) return offset; if (invalidOffsets == null) return offset; for (int i = 0; i < invalidOffsets.length; i++) { if (offset < invalidOffsets[i]) break; offset++; } return offset; } /* * Translate an internal offset to a client offset */ int untranslateOffset(int offset) { int length = text.length(); if (length == 0) return offset; if (invalidOffsets == null) return offset; int i = 0; while (i < invalidOffsets.length && offset > invalidOffsets[i]) { i++; } return offset - i; } int width () { int wrapWidth = OS.pango_layout_get_width(layout); if (wrapWidth != -1) return OS.PANGO_PIXELS(wrapWidth); int[] w = new int[1], h = new int[1]; OS.pango_layout_get_pixel_size(layout, w, h); return w[0]; } /** * Sets Default Tab Width in terms if number of space characters. * * @param tabLength in number of characters * * @exception IllegalArgumentException
      *
    • ERROR_INVALID_ARGUMENT - if the tabLength is less than 0
    • *
    * @exception SWTException
      *
    • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • *
    * * @noreference This method is not intended to be referenced by clients. * * DO NOT USE This might be removed in 4.8 * @since 3.107 */ public void setDefaultTabWidth(int tabLength) { //unused in GTK } }




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy