1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.graphics;
16 import org.eclipse.swt.*;
19 * <code>TextStyle</code> defines a set of styles that can be applied
22 * The hashCode() method in this class uses the values of the public
23 * fields to compute the hash value. When storing instances of the
24 * class in hashed collections, do not modify these fields after the
25 * object has been inserted.
28 * Application code does <em>not</em> need to explicitly release the
29 * resources managed by each instance when those instances are no longer
30 * required, and thus no <code>dispose()</code> method is provided.
36 * @see <a href="http://www.eclipse.org/swt/snippets/#textlayout">TextLayout, TextStyle snippets</a>
37 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
41 public class TextStyle {
44 * the font of the style
49 * the foreground of the style
51 public Color foreground;
54 * the background of the style
56 public Color background;
59 * the underline flag of the style. The default underline
60 * style is <code>SWT.UNDERLINE_SINGLE</code>.
65 public boolean underline;
68 * the underline color of the style
72 public Color underlineColor;
75 * the underline style. This style is ignored when
76 * <code>underline</code> is false.
78 * This value should be one of <code>SWT.UNDERLINE_SINGLE</code>,
79 * <code>SWT.UNDERLINE_DOUBLE</code>, <code>SWT.UNDERLINE_ERROR</code>,
80 * <code>SWT.UNDERLINE_SQUIGGLE</code>, or <code>SWT.UNDERLINE_LINK</code>.
83 * @see SWT#UNDERLINE_SINGLE
84 * @see SWT#UNDERLINE_DOUBLE
85 * @see SWT#UNDERLINE_ERROR
86 * @see SWT#UNDERLINE_SQUIGGLE
87 * @see SWT#UNDERLINE_LINK
91 public int underlineStyle;
94 * the strikeout flag of the style
98 public boolean strikeout;
101 * the strikeout color of the style
105 public Color strikeoutColor;
108 * the border style. The default border style is <code>SWT.NONE</code>.
110 * This value should be one of <code>SWT.BORDER_SOLID</code>,
111 * <code>SWT.BORDER_DASH</code>,<code>SWT.BORDER_DOT</code> or
112 * <code>SWT.NONE</code>.
115 * @see SWT#BORDER_SOLID
116 * @see SWT#BORDER_DASH
117 * @see SWT#BORDER_DOT
122 public int borderStyle;
125 * the border color of the style
129 public Color borderColor;
132 * the GlyphMetrics of the style
136 public GlyphMetrics metrics;
139 * the baseline rise of the style.
147 * the data. An user data field. It can be used to hold the HREF when the range
148 * is used as a link or the embed object when the range is used with <code>GlyphMetrics</code>.
157 * Create an empty text style.
161 public TextStyle () {
165 * Create a new text style with the specified font, foreground
168 * @param font the font of the style, <code>null</code> if none
169 * @param foreground the foreground color of the style, <code>null</code> if none
170 * @param background the background color of the style, <code>null</code> if none
172 public TextStyle (Font font, Color foreground, Color background) {
173 if (font != null && font.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
174 if (foreground != null && foreground.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
175 if (background != null && background.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
177 this.foreground = foreground;
178 this.background = background;
183 * Create a new text style from an existing text style.
185 * @param style the style to copy
189 public TextStyle (TextStyle style) {
190 if (style == null) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
192 foreground = style.foreground;
193 background = style.background;
194 underline = style.underline;
195 underlineColor = style.underlineColor;
196 underlineStyle = style.underlineStyle;
197 strikeout = style.strikeout;
198 strikeoutColor = style.strikeoutColor;
199 borderStyle = style.borderStyle;
200 borderColor = style.borderColor;
201 metrics = style.metrics;
207 * Compares the argument to the receiver, and returns true
208 * if they represent the <em>same</em> object using a class
209 * specific comparison.
211 * @param object the object to compare with this object
212 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
217 public boolean equals(Object object) {
218 if (object == this) return true;
219 if (object == null) return false;
220 if (!(object instanceof TextStyle)) return false;
221 TextStyle style = (TextStyle)object;
222 if (foreground != null) {
223 if (!foreground.equals(style.foreground)) return false;
224 } else if (style.foreground != null) return false;
225 if (background != null) {
226 if (!background.equals(style.background)) return false;
227 } else if (style.background != null) return false;
229 if (!font.equals(style.font)) return false;
230 } else if (style.font != null) return false;
231 if (metrics != null) {
232 if (!metrics.equals(style.metrics)) return false;
233 } else if (style.metrics != null) return false;
234 if (underline != style.underline) return false;
235 if (underlineStyle != style.underlineStyle) return false;
236 if (borderStyle != style.borderStyle) return false;
237 if (strikeout != style.strikeout) return false;
238 if (rise != style.rise) return false;
239 if (underlineColor != null) {
240 if (!underlineColor.equals(style.underlineColor)) return false;
241 } else if (style.underlineColor != null) return false;
242 if (strikeoutColor != null) {
243 if (!strikeoutColor.equals(style.strikeoutColor)) return false;
244 } else if (style.strikeoutColor != null) return false;
245 if (underlineStyle != style.underlineStyle) return false;
246 if (borderColor != null) {
247 if (!borderColor.equals(style.borderColor)) return false;
248 } else if (style.borderColor != null) return false;
250 if (!data.equals(style.data)) return false;
251 } else if (style.data != null) return false;
256 * Returns an integer hash code for the receiver. Any two
257 * objects that return <code>true</code> when passed to
258 * <code>equals</code> must return the same value for this
261 * @return the receiver's hash
263 * @see #equals(Object)
266 public int hashCode() {
268 if (foreground != null) hash ^= foreground.hashCode();
269 if (background != null) hash ^= background.hashCode();
270 if (font != null) hash ^= font.hashCode();
271 if (metrics != null) hash ^= metrics.hashCode();
272 if (underline) hash ^= (hash << 1);
273 if (strikeout) hash ^= (hash << 2);
275 if (underlineColor != null) hash ^= underlineColor.hashCode();
276 if (strikeoutColor != null) hash ^= strikeoutColor.hashCode();
277 if (borderColor != null) hash ^= borderColor.hashCode();
278 hash ^= underlineStyle;
282 boolean isAdherentBorder(TextStyle style) {
283 if (this == style) return true;
284 if (style == null) return false;
285 if (borderStyle != style.borderStyle) return false;
286 if (borderColor != null) {
287 if (!borderColor.equals(style.borderColor)) return false;
289 if (style.borderColor != null) return false;
290 if (foreground != null) {
291 if (!foreground.equals(style.foreground)) return false;
292 } else if (style.foreground != null) return false;
297 boolean isAdherentUnderline(TextStyle style) {
298 if (this == style) return true;
299 if (style == null) return false;
300 if (underline != style.underline) return false;
301 if (underlineStyle != style.underlineStyle) return false;
302 if (underlineColor != null) {
303 if (!underlineColor.equals(style.underlineColor)) return false;
305 if (style.underlineColor != null) return false;
306 if (foreground != null) {
307 if (!foreground.equals(style.foreground)) return false;
308 } else if (style.foreground != null) return false;
313 boolean isAdherentStrikeout(TextStyle style) {
314 if (this == style) return true;
315 if (style == null) return false;
316 if (strikeout != style.strikeout) return false;
317 if (strikeoutColor != null) {
318 if (!strikeoutColor.equals(style.strikeoutColor)) return false;
320 if (style.strikeoutColor != null) return false;
321 if (foreground != null) {
322 if (!foreground.equals(style.foreground)) return false;
323 } else if (style.foreground != null) return false;
329 * Returns a string containing a concise, human-readable
330 * description of the receiver.
332 * @return a string representation of the <code>TextStyle</code>
335 public String toString () {
336 StringBuilder buffer = new StringBuilder("TextStyle {"); //$NON-NLS-1$
337 int startLength = buffer.length();
339 if (buffer.length() > startLength) buffer.append(", "); //$NON-NLS-1$
340 buffer.append("font="); //$NON-NLS-1$
343 if (foreground != null) {
344 if (buffer.length() > startLength) buffer.append(", "); //$NON-NLS-1$
345 buffer.append("foreground="); //$NON-NLS-1$
346 buffer.append(foreground);
348 if (background != null) {
349 if (buffer.length() > startLength) buffer.append(", "); //$NON-NLS-1$
350 buffer.append("background="); //$NON-NLS-1$
351 buffer.append(background);
354 if (buffer.length() > startLength) buffer.append(", "); //$NON-NLS-1$
355 buffer.append("underline="); //$NON-NLS-1$
356 switch (underlineStyle) {
357 case SWT.UNDERLINE_SINGLE: buffer.append("single"); break; //$NON-NLS-1$
358 case SWT.UNDERLINE_DOUBLE: buffer.append("double"); break; //$NON-NLS-1$
359 case SWT.UNDERLINE_SQUIGGLE: buffer.append("squiggle"); break; //$NON-NLS-1$
360 case SWT.UNDERLINE_ERROR: buffer.append("error"); break; //$NON-NLS-1$
361 case SWT.UNDERLINE_LINK: buffer.append("link"); break; //$NON-NLS-1$
363 if (underlineColor != null) {
364 buffer.append(", underlineColor="); //$NON-NLS-1$
365 buffer.append(underlineColor);
369 if (buffer.length() > startLength) buffer.append(", "); //$NON-NLS-1$
370 buffer.append("striked out"); //$NON-NLS-1$
371 if (strikeoutColor != null) {
372 buffer.append(", strikeoutColor="); //$NON-NLS-1$
373 buffer.append(strikeoutColor);
376 if (borderStyle != SWT.NONE) {
377 if (buffer.length() > startLength) buffer.append(", "); //$NON-NLS-1$
378 buffer.append("border="); //$NON-NLS-1$
379 switch (borderStyle) {
380 case SWT.BORDER_SOLID: buffer.append("solid"); break; //$NON-NLS-1$
381 case SWT.BORDER_DOT: buffer.append("dot"); break; //$NON-NLS-1$
382 case SWT.BORDER_DASH: buffer.append("dash"); break; //$NON-NLS-1$
384 if (borderColor != null) {
385 buffer.append(", borderColor="); //$NON-NLS-1$
386 buffer.append(borderColor);
390 if (buffer.length() > startLength) buffer.append(", "); //$NON-NLS-1$
391 buffer.append("rise="); //$NON-NLS-1$
394 if (metrics != null) {
395 if (buffer.length() > startLength) buffer.append(", "); //$NON-NLS-1$
396 buffer.append("metrics="); //$NON-NLS-1$
397 buffer.append(metrics);
399 buffer.append("}"); //$NON-NLS-1$
400 return buffer.toString();