| 1 |
/******************************************************************************* |
|---|
| 2 |
* Copyright (c) 2000, 2008 IBM Corporation and others. |
|---|
| 3 |
* All rights reserved. This program and the accompanying materials |
|---|
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
|---|
| 5 |
* which accompanies this distribution, and is available at |
|---|
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
|---|
| 7 |
* |
|---|
| 8 |
* Contributors: |
|---|
| 9 |
* IBM Corporation - initial API and implementation |
|---|
| 10 |
* Port to the D programming language: |
|---|
| 11 |
* Frank Benoit <benoit@tionex.de> |
|---|
| 12 |
*******************************************************************************/ |
|---|
| 13 |
module dwt.graphics.TextStyle; |
|---|
| 14 |
|
|---|
| 15 |
import dwt.DWT; |
|---|
| 16 |
import dwt.graphics.Font; |
|---|
| 17 |
import dwt.graphics.Color; |
|---|
| 18 |
import dwt.graphics.GlyphMetrics; |
|---|
| 19 |
|
|---|
| 20 |
import tango.util.Convert; |
|---|
| 21 |
import dwt.dwthelper.utils; |
|---|
| 22 |
|
|---|
| 23 |
/** |
|---|
| 24 |
* <code>TextStyle</code> defines a set of styles that can be applied |
|---|
| 25 |
* to a range of text. |
|---|
| 26 |
* <p> |
|---|
| 27 |
* The hashCode() method in this class uses the values of the public |
|---|
| 28 |
* fields to compute the hash value. When storing instances of the |
|---|
| 29 |
* class in hashed collections, do not modify these fields after the |
|---|
| 30 |
* object has been inserted. |
|---|
| 31 |
* </p> |
|---|
| 32 |
* <p> |
|---|
| 33 |
* Application code does <em>not</em> need to explicitly release the |
|---|
| 34 |
* resources managed by each instance when those instances are no longer |
|---|
| 35 |
* required, and thus no <code>dispose()</code> method is provided. |
|---|
| 36 |
* </p> |
|---|
| 37 |
* |
|---|
| 38 |
* @see TextLayout |
|---|
| 39 |
* @see Font |
|---|
| 40 |
* @see Color |
|---|
| 41 |
* @see <a href="http://www.eclipse.org/swt/snippets/#textlayout">TextLayout, TextStyle snippets</a> |
|---|
| 42 |
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> |
|---|
| 43 |
* |
|---|
| 44 |
* @since 3.0 |
|---|
| 45 |
*/ |
|---|
| 46 |
public class TextStyle { |
|---|
| 47 |
|
|---|
| 48 |
/** |
|---|
| 49 |
* the font of the style |
|---|
| 50 |
*/ |
|---|
| 51 |
public Font font; |
|---|
| 52 |
|
|---|
| 53 |
/** |
|---|
| 54 |
* the foreground of the style |
|---|
| 55 |
*/ |
|---|
| 56 |
public Color foreground; |
|---|
| 57 |
|
|---|
| 58 |
/** |
|---|
| 59 |
* the background of the style |
|---|
| 60 |
*/ |
|---|
| 61 |
public Color background; |
|---|
| 62 |
|
|---|
| 63 |
/** |
|---|
| 64 |
* the underline flag of the style. The default underline |
|---|
| 65 |
* style is <code>DWT.UNDERLINE_SINGLE</code>. |
|---|
| 66 |
* |
|---|
| 67 |
* |
|---|
| 68 |
* @since 3.1 |
|---|
| 69 |
*/ |
|---|
| 70 |
public bool underline; |
|---|
| 71 |
|
|---|
| 72 |
/** |
|---|
| 73 |
* the underline color of the style |
|---|
| 74 |
* |
|---|
| 75 |
* @since 3.4 |
|---|
| 76 |
*/ |
|---|
| 77 |
public Color underlineColor; |
|---|
| 78 |
|
|---|
| 79 |
/** |
|---|
| 80 |
* the underline style. This style is ignored when |
|---|
| 81 |
* <code>underline</code> is false. |
|---|
| 82 |
* <p> |
|---|
| 83 |
* This value should be one of <code>DWT.UNDERLINE_SINGLE</code>, |
|---|
| 84 |
* <code>DWT.UNDERLINE_DOUBLE</code>, <code>DWT.UNDERLINE_ERROR</code>, |
|---|
| 85 |
* or <code>DWT.UNDERLINE_SQUIGGLE</code>. |
|---|
| 86 |
* </p> |
|---|
| 87 |
* |
|---|
| 88 |
* @see DWT#UNDERLINE_SINGLE |
|---|
| 89 |
* @see DWT#UNDERLINE_DOUBLE |
|---|
| 90 |
* @see DWT#UNDERLINE_ERROR |
|---|
| 91 |
* @see DWT#UNDERLINE_SQUIGGLE |
|---|
| 92 |
* |
|---|
| 93 |
* @since 3.4 |
|---|
| 94 |
*/ |
|---|
| 95 |
public int underlineStyle; |
|---|
| 96 |
|
|---|
| 97 |
/** |
|---|
| 98 |
* the strikeout flag of the style |
|---|
| 99 |
* |
|---|
| 100 |
* @since 3.1 |
|---|
| 101 |
*/ |
|---|
| 102 |
public bool strikeout; |
|---|
| 103 |
|
|---|
| 104 |
/** |
|---|
| 105 |
* the strikeout color of the style |
|---|
| 106 |
* |
|---|
| 107 |
* @since 3.4 |
|---|
| 108 |
*/ |
|---|
| 109 |
public Color strikeoutColor; |
|---|
| 110 |
|
|---|
| 111 |
/** |
|---|
| 112 |
* the border style. The default border style is <code>DWT.NONE</code>. |
|---|
| 113 |
* <p> |
|---|
| 114 |
* This value should be one of <code>DWT.BORDER_SOLID</code>, |
|---|
| 115 |
* <code>DWT.BORDER_DASH</code>,<code>DWT.BORDER_DOT</code> or |
|---|
| 116 |
* <code>DWT.NONE</code>. |
|---|
| 117 |
* </p> |
|---|
| 118 |
* |
|---|
| 119 |
* @see DWT#BORDER_SOLID |
|---|
| 120 |
* @see DWT#BORDER_DASH |
|---|
| 121 |
* @see DWT#BORDER_DOT |
|---|
| 122 |
* @see DWT#NONE |
|---|
| 123 |
* |
|---|
| 124 |
* @since 3.4 |
|---|
| 125 |
*/ |
|---|
| 126 |
public int borderStyle; |
|---|
| 127 |
|
|---|
| 128 |
/** |
|---|
| 129 |
* the border color of the style |
|---|
| 130 |
* |
|---|
| 131 |
* @since 3.4 |
|---|
| 132 |
*/ |
|---|
| 133 |
public Color borderColor; |
|---|
| 134 |
|
|---|
| 135 |
/** |
|---|
| 136 |
* the GlyphMetrics of the style |
|---|
| 137 |
* |
|---|
| 138 |
* @since 3.2 |
|---|
| 139 |
*/ |
|---|
| 140 |
public GlyphMetrics metrics; |
|---|
| 141 |
|
|---|
| 142 |
/** |
|---|
| 143 |
* the baseline rise of the style. |
|---|
| 144 |
* |
|---|
| 145 |
* @since 3.2 |
|---|
| 146 |
*/ |
|---|
| 147 |
public int rise; |
|---|
| 148 |
|
|---|
| 149 |
/** |
|---|
| 150 |
* Create an empty text style. |
|---|
| 151 |
* |
|---|
| 152 |
* @since 3.4 |
|---|
| 153 |
*/ |
|---|
| 154 |
public this () { |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
/** |
|---|
| 158 |
* Create a new text style with the specified font, foreground |
|---|
| 159 |
* and background. |
|---|
| 160 |
* |
|---|
| 161 |
* @param font the font of the style, <code>null</code> if none |
|---|
| 162 |
* @param foreground the foreground color of the style, <code>null</code> if none |
|---|
| 163 |
* @param background the background color of the style, <code>null</code> if none |
|---|
| 164 |
*/ |
|---|
| 165 |
public this (Font font, Color foreground, Color background) { |
|---|
| 166 |
if (font !is null && font.isDisposed()) DWT.error (DWT.ERROR_INVALID_ARGUMENT); |
|---|
| 167 |
if (foreground !is null && foreground.isDisposed()) DWT.error (DWT.ERROR_INVALID_ARGUMENT); |
|---|
| 168 |
if (background !is null && background.isDisposed()) DWT.error (DWT.ERROR_INVALID_ARGUMENT); |
|---|
| 169 |
this.font = font; |
|---|
| 170 |
this.foreground = foreground; |
|---|
| 171 |
this.background = background; |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
/** |
|---|
| 176 |
* Create a new text style from an existing text style. |
|---|
| 177 |
* |
|---|
| 178 |
* @param style the style to copy |
|---|
| 179 |
* |
|---|
| 180 |
* @since 3.4 |
|---|
| 181 |
*/ |
|---|
| 182 |
public this (TextStyle style) { |
|---|
| 183 |
if (style is null) DWT.error (DWT.ERROR_INVALID_ARGUMENT); |
|---|
| 184 |
font = style.font; |
|---|
| 185 |
foreground = style.foreground; |
|---|
| 186 |
background = style.background; |
|---|
| 187 |
underline = style.underline; |
|---|
| 188 |
underlineColor = style.underlineColor; |
|---|
| 189 |
underlineStyle = style.underlineStyle; |
|---|
| 190 |
strikeout = style.strikeout; |
|---|
| 191 |
strikeoutColor = style.strikeoutColor; |
|---|
| 192 |
borderStyle = style.borderStyle; |
|---|
| 193 |
borderColor = style.borderColor; |
|---|
| 194 |
metrics = style.metrics; |
|---|
| 195 |
rise = style.rise; |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
/** |
|---|
| 199 |
* Compares the argument to the receiver, and returns true |
|---|
| 200 |
* if they represent the <em>same</em> object using a class |
|---|
| 201 |
* specific comparison. |
|---|
| 202 |
* |
|---|
| 203 |
* @param object the object to compare with this object |
|---|
| 204 |
* @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise |
|---|
| 205 |
* |
|---|
| 206 |
* @see #hashCode() |
|---|
| 207 |
*/ |
|---|
| 208 |
public override int opEquals(Object object) { |
|---|
| 209 |
if (object is this) return true; |
|---|
| 210 |
if (object is null) return false; |
|---|
| 211 |
if (!(cast(TextStyle)object)) return false; |
|---|
| 212 |
TextStyle style = cast(TextStyle)object; |
|---|
| 213 |
if (foreground !is null) { |
|---|
| 214 |
if ( foreground !is style.foreground ) return false; |
|---|
| 215 |
} else if (style.foreground !is null) return false; |
|---|
| 216 |
if (background !is null) { |
|---|
| 217 |
if ( background !is style.background ) return false; |
|---|
| 218 |
} else if (style.background !is null) return false; |
|---|
| 219 |
if (font !is null) { |
|---|
| 220 |
if (font !is style.font) return false; |
|---|
| 221 |
} else if (style.font !is null) return false; |
|---|
| 222 |
if (metrics !is null || style.metrics !is null) return false; |
|---|
| 223 |
if (underline !is style.underline) return false; |
|---|
| 224 |
if (underlineStyle !is style.underlineStyle) return false; |
|---|
| 225 |
if (borderStyle !is style.borderStyle) return false; |
|---|
| 226 |
if (strikeout !is style.strikeout) return false; |
|---|
| 227 |
if (rise !is style.rise) return false; |
|---|
| 228 |
if (underlineColor !is null) { |
|---|
| 229 |
if (!underlineColor.equals(style.underlineColor)) return false; |
|---|
| 230 |
} else if (style.underlineColor !is null) return false; |
|---|
| 231 |
if (strikeoutColor !is null) { |
|---|
| 232 |
if (!strikeoutColor.equals(style.strikeoutColor)) return false; |
|---|
| 233 |
} else if (style.strikeoutColor !is null) return false; |
|---|
| 234 |
if (underlineStyle !is style.underlineStyle) return false; |
|---|
| 235 |
if (borderColor !is null) { |
|---|
| 236 |
if (!borderColor.equals(style.borderColor)) return false; |
|---|
| 237 |
} else if (style.borderColor !is null) return false; |
|---|
| 238 |
return true; |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
/** |
|---|
| 242 |
* Returns an integer hash code for the receiver. Any two |
|---|
| 243 |
* objects that return <code>true</code> when passed to |
|---|
| 244 |
* <code>equals</code> must return the same value for this |
|---|
| 245 |
* method. |
|---|
| 246 |
* |
|---|
| 247 |
* @return the receiver's hash |
|---|
| 248 |
* |
|---|
| 249 |
* @see #equals(Object) |
|---|
| 250 |
*/ |
|---|
| 251 |
public override hash_t toHash() { |
|---|
| 252 |
int hash = 0; |
|---|
| 253 |
if (foreground !is null) hash ^= foreground.toHash(); |
|---|
| 254 |
if (background !is null) hash ^= background.toHash(); |
|---|
| 255 |
if (font !is null) hash ^= font.toHash(); |
|---|
| 256 |
if (metrics !is null) hash ^= metrics.toHash(); |
|---|
| 257 |
if (underline) hash ^= hash; |
|---|
| 258 |
if (strikeout) hash ^= hash; |
|---|
| 259 |
hash ^= rise; |
|---|
| 260 |
if (underlineColor !is null) hash ^= underlineColor.hashCode(); |
|---|
| 261 |
if (strikeoutColor !is null) hash ^= strikeoutColor.hashCode(); |
|---|
| 262 |
if (borderColor !is null) hash ^= borderColor.hashCode(); |
|---|
| 263 |
hash ^= underlineStyle; |
|---|
| 264 |
return hash; |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|
| 267 |
bool isAdherentBorder(TextStyle style) { |
|---|
| 268 |
if (this is style) return true; |
|---|
| 269 |
if (style is null) return false; |
|---|
| 270 |
if (borderStyle !is style.borderStyle) return false; |
|---|
| 271 |
if (borderColor !is null) { |
|---|
| 272 |
if (!borderColor.equals(style.borderColor)) return false; |
|---|
| 273 |
} else if (style.borderColor !is null) return false; |
|---|
| 274 |
return true; |
|---|
| 275 |
} |
|---|
| 276 |
|
|---|
| 277 |
bool isAdherentUnderline(TextStyle style) { |
|---|
| 278 |
if (this is style) return true; |
|---|
| 279 |
if (style is null) return false; |
|---|
| 280 |
if (underline !is style.underline) return false; |
|---|
| 281 |
if (underlineStyle !is style.underlineStyle) return false; |
|---|
| 282 |
if (underlineColor !is null) { |
|---|
| 283 |
if (!underlineColor.equals(style.underlineColor)) return false; |
|---|
| 284 |
} else if (style.underlineColor !is null) return false; |
|---|
| 285 |
return true; |
|---|
| 286 |
} |
|---|
| 287 |
|
|---|
| 288 |
bool isAdherentStrikeout(TextStyle style) { |
|---|
| 289 |
if (this is style) return true; |
|---|
| 290 |
if (style is null) return false; |
|---|
| 291 |
if (strikeout !is style.strikeout) return false; |
|---|
| 292 |
if (strikeoutColor !is null) { |
|---|
| 293 |
if (!strikeoutColor.equals(style.strikeoutColor)) return false; |
|---|
| 294 |
} else if (style.strikeoutColor !is null) return false; |
|---|
| 295 |
return true; |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
/** |
|---|
| 299 |
* Returns a string containing a concise, human-readable |
|---|
| 300 |
* description of the receiver. |
|---|
| 301 |
* |
|---|
| 302 |
* @return a string representation of the <code>TextStyle</code> |
|---|
| 303 |
*/ |
|---|
| 304 |
override public String toString () { |
|---|
| 305 |
String buffer = "TextStyle {"; |
|---|
| 306 |
int startLength = buffer.length; |
|---|
| 307 |
if (font !is null) { |
|---|
| 308 |
if (buffer.length > startLength) buffer ~= ", "; |
|---|
| 309 |
buffer ~= "font="; |
|---|
| 310 |
buffer ~= font.toString; |
|---|
| 311 |
} |
|---|
| 312 |
if (foreground !is null) { |
|---|
| 313 |
if (buffer.length > startLength) buffer ~= ", "; |
|---|
| 314 |
buffer ~= "foreground="; |
|---|
| 315 |
buffer ~= foreground.toString; |
|---|
| 316 |
} |
|---|
| 317 |
if (background !is null) { |
|---|
| 318 |
if (buffer.length > startLength) buffer ~= ", "; |
|---|
| 319 |
buffer ~= "background="; |
|---|
| 320 |
buffer ~= background.toString; |
|---|
| 321 |
} |
|---|
| 322 |
if (underline) { |
|---|
| 323 |
if (buffer.length > startLength) buffer ~= ", "; |
|---|
| 324 |
buffer ~= "underlined"; |
|---|
| 325 |
} |
|---|
| 326 |
if (strikeout) { |
|---|
| 327 |
if (buffer.length > startLength) buffer ~= ", "; |
|---|
| 328 |
buffer ~= "striked out"; |
|---|
| 329 |
} |
|---|
| 330 |
if (rise !is 0) { |
|---|
| 331 |
if (buffer.length > startLength) buffer ~= ", "; |
|---|
| 332 |
buffer ~= "rise="; |
|---|
| 333 |
buffer ~= to!(String)(rise); |
|---|
| 334 |
} |
|---|
| 335 |
if (metrics !is null) { |
|---|
| 336 |
if (buffer.length > startLength) buffer ~= ", "; |
|---|
| 337 |
buffer ~= "metrics="; |
|---|
| 338 |
buffer ~= metrics.toString; |
|---|
| 339 |
} |
|---|
| 340 |
buffer ~= "}"; |
|---|
| 341 |
return buffer; |
|---|
| 342 |
} |
|---|
| 343 |
|
|---|
| 344 |
} |
|---|