1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
3 *
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/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.graphics;
15
16 import org.eclipse.swt.*;
17
18 /**
19 * <code>TextStyle</code> defines a set of styles that can be applied
20 * to a range of text.
21 * <p>
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.
26 * </p>
27 * <p>
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.
31 * </p>
32 *
33 * @see TextLayout
34 * @see Font
35 * @see Color
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>
38 *
39 * @since 3.0
40 */
41 public class TextStyle {
42
43 /**
44 * the font of the style
45 */
46 public Font font;
47
48 /**
49 * the foreground of the style
50 */
51 public Color foreground;
52
53 /**
54 * the background of the style
55 */
56 public Color background;
57
58 /**
59 * the underline flag of the style. The default underline
60 * style is <code>SWT.UNDERLINE_SINGLE</code>.
61 *
62 *
63 * @since 3.1
64 */
65 public boolean underline;
66
67 /**
68 * the underline color of the style
69 *
70 * @since 3.4
71 */
72 public Color underlineColor;
73
74 /**
75 * the underline style. This style is ignored when
76 * <code>underline</code> is false.
77 * <p>
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>.
81 * </p>
82 *
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
88 *
89 * @since 3.4
90 */
91 public int underlineStyle;
92
93 /**
94 * the strikeout flag of the style
95 *
96 * @since 3.1
97 */
98 public boolean strikeout;
99
100 /**
101 * the strikeout color of the style
102 *
103 * @since 3.4
104 */
105 public Color strikeoutColor;
106
107 /**
108 * the border style. The default border style is <code>SWT.NONE</code>.
109 * <p>
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>.
113 * </p>
114 *
115 * @see SWT#BORDER_SOLID
116 * @see SWT#BORDER_DASH
117 * @see SWT#BORDER_DOT
118 * @see SWT#NONE
119 *
120 * @since 3.4
121 */
122 public int borderStyle;
123
124 /**
125 * the border color of the style
126 *
127 * @since 3.4
128 */
129 public Color borderColor;
130
131 /**
132 * the GlyphMetrics of the style
133 *
134 * @since 3.2
135 */
136 public GlyphMetrics metrics;
137
138 /**
139 * the baseline rise of the style.
140 *
141 * @since 3.2
142 */
143 public int rise;
144
145
146 /**
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>.
149 * <p>
150 *
151 * @since 3.5
152 */
153 public Object data;
154
155
156 /**
157 * Create an empty text style.
158 *
159 * @since 3.4
160 */
TextStyle()161 public TextStyle () {
162 }
163
164 /**
165 * Create a new text style with the specified font, foreground
166 * and background.
167 *
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
171 */
TextStyle(Font font, Color foreground, Color background)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);
176 this.font = font;
177 this.foreground = foreground;
178 this.background = background;
179 }
180
181
182 /**
183 * Create a new text style from an existing text style.
184 *
185 * @param style the style to copy
186 *
187 * @since 3.4
188 */
TextStyle(TextStyle style)189 public TextStyle (TextStyle style) {
190 if (style == null) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
191 font = style.font;
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;
202 rise = style.rise;
203 data = style.data;
204 }
205
206 /**
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.
210 *
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
213 *
214 * @see #hashCode()
215 */
216 @Override
equals(Object object)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;
228 if (font != null) {
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;
249 if (data != null) {
250 if (!data.equals(style.data)) return false;
251 } else if (style.data != null) return false;
252 return true;
253 }
254
255 /**
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
259 * method.
260 *
261 * @return the receiver's hash
262 *
263 * @see #equals(Object)
264 */
265 @Override
hashCode()266 public int hashCode() {
267 int hash = 0;
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);
274 hash ^= rise;
275 if (underlineColor != null) hash ^= underlineColor.hashCode();
276 if (strikeoutColor != null) hash ^= strikeoutColor.hashCode();
277 if (borderColor != null) hash ^= borderColor.hashCode();
278 hash ^= underlineStyle;
279 return hash;
280 }
281
isAdherentBorder(TextStyle style)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;
288 } else {
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;
293 }
294 return true;
295 }
296
isAdherentUnderline(TextStyle style)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;
304 } else {
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;
309 }
310 return true;
311 }
312
isAdherentStrikeout(TextStyle style)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;
319 } else {
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;
324 }
325 return true;
326 }
327
328 /**
329 * Returns a string containing a concise, human-readable
330 * description of the receiver.
331 *
332 * @return a string representation of the <code>TextStyle</code>
333 */
334 @Override
toString()335 public String toString () {
336 StringBuilder buffer = new StringBuilder("TextStyle {"); //$NON-NLS-1$
337 int startLength = buffer.length();
338 if (font != null) {
339 if (buffer.length() > startLength) buffer.append(", "); //$NON-NLS-1$
340 buffer.append("font="); //$NON-NLS-1$
341 buffer.append(font);
342 }
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);
347 }
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);
352 }
353 if (underline) {
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$
362 }
363 if (underlineColor != null) {
364 buffer.append(", underlineColor="); //$NON-NLS-1$
365 buffer.append(underlineColor);
366 }
367 }
368 if (strikeout) {
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);
374 }
375 }
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$
383 }
384 if (borderColor != null) {
385 buffer.append(", borderColor="); //$NON-NLS-1$
386 buffer.append(borderColor);
387 }
388 }
389 if (rise != 0) {
390 if (buffer.length() > startLength) buffer.append(", "); //$NON-NLS-1$
391 buffer.append("rise="); //$NON-NLS-1$
392 buffer.append(rise);
393 }
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);
398 }
399 buffer.append("}"); //$NON-NLS-1$
400 return buffer.toString();
401 }
402
403 }
404