1 /******************************************************************************* 2 * Copyright (c) 2000, 2008 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 * Port to the D programming language: 11 * Frank Benoit <benoit@tionex.de> 12 *******************************************************************************/ 13 module org.eclipse.swt.graphics.Font; 14 15 import java.lang.all; 16 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.graphics.Resource; 20 import org.eclipse.swt.graphics.FontData; 21 import org.eclipse.swt.graphics.Device; 22 import org.eclipse.swt.internal.Converter; 23 import org.eclipse.swt.internal.gtk.OS; 24 25 26 /** 27 * Instances of this class manage operating system resources that 28 * define how text looks when it is displayed. Fonts may be constructed 29 * by providing a device and either name, size and style information 30 * or a <code>FontData</code> object which encapsulates this data. 31 * <p> 32 * Application code must explicitly invoke the <code>Font.dispose()</code> 33 * method to release the operating system resources managed by each instance 34 * when those instances are no longer required. 35 * </p> 36 * 37 * @see FontData 38 * @see <a href="http://www.eclipse.org/swt/snippets/#font">Font snippets</a> 39 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Examples: GraphicsExample, PaintExample</a> 40 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> 41 */ 42 public final class Font : Resource { 43 44 alias Resource.init_ init_; 45 /** 46 * the handle to the OS font resource 47 * (Warning: This field is platform dependent) 48 * <p> 49 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT 50 * public API. It is marked public only so that it can be shared 51 * within the packages provided by SWT. It is not available on all 52 * platforms and should never be accessed from application code. 53 * </p> 54 */ 55 public PangoFontDescription* handle; 56 57 this(Device device) { 58 super(device); 59 } 60 61 /** 62 * Constructs a new font given a device and font data 63 * which describes the desired font's appearance. 64 * <p> 65 * You must dispose the font when it is no longer required. 66 * </p> 67 * 68 * @param device the device to create the font on 69 * @param fd the FontData that describes the desired font (must not be null) 70 * 71 * @exception IllegalArgumentException <ul> 72 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> 73 * <li>ERROR_NULL_ARGUMENT - if the fd argument is null</li> 74 * </ul> 75 * @exception SWTError <ul> 76 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> 77 * </ul> 78 */ 79 public this(Device device, FontData fd) { 80 super(device); 81 if (fd is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 82 init_(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.str); 83 init_(); 84 } 85 86 /** 87 * Constructs a new font given a device and an array 88 * of font data which describes the desired font's 89 * appearance. 90 * <p> 91 * You must dispose the font when it is no longer required. 92 * </p> 93 * 94 * @param device the device to create the font on 95 * @param fds the array of FontData that describes the desired font (must not be null) 96 * 97 * @exception IllegalArgumentException <ul> 98 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> 99 * <li>ERROR_NULL_ARGUMENT - if the fds argument is null</li> 100 * <li>ERROR_INVALID_ARGUMENT - if the length of fds is zero</li> 101 * <li>ERROR_NULL_ARGUMENT - if any fd in the array is null</li> 102 * </ul> 103 * @exception SWTError <ul> 104 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> 105 * </ul> 106 * 107 * @since 2.1 108 */ 109 public this(Device device, FontData[] fds) { 110 super(device); 111 if (fds is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 112 if (fds.length is 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 113 for (int i=0; i<fds.length; i++) { 114 if (fds[i] is null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 115 } 116 FontData fd = fds[0]; 117 init_(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.str); 118 init_(); 119 } 120 121 /** 122 * Constructs a new font given a device, a font name, 123 * the height of the desired font in points, and a font 124 * style. 125 * <p> 126 * You must dispose the font when it is no longer required. 127 * </p> 128 * 129 * @param device the device to create the font on 130 * @param name the name of the font (must not be null) 131 * @param height the font height in points 132 * @param style a bit or combination of NORMAL, BOLD, ITALIC 133 * 134 * @exception IllegalArgumentException <ul> 135 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> 136 * <li>ERROR_NULL_ARGUMENT - if the name argument is null</li> 137 * <li>ERROR_INVALID_ARGUMENT - if the height is negative</li> 138 * </ul> 139 * @exception SWTError <ul> 140 * <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li> 141 * </ul> 142 */ 143 public this(Device device, String name, int height, int style) { 144 super(device); 145 init_(name, height, style, null); 146 init_(); 147 } 148 149 /*public*/ this(Device device, String name, float height, int style) { 150 super(device); 151 init_(name, height, style, null); 152 init_(); 153 } 154 155 override 156 void destroy() { 157 OS.pango_font_description_free(handle); 158 handle = null; 159 } 160 161 /** 162 * Compares the argument to the receiver, and returns true 163 * if they represent the <em>same</em> object using a class 164 * specific comparison. 165 * 166 * @param object the object to compare with this object 167 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise 168 * 169 * @see #hashCode 170 */ 171 public override equals_t opEquals(Object object) { 172 if (object is this) return true; 173 if ( auto font = cast(Font)object ){ 174 return handle is font.handle; 175 } 176 return false; 177 } 178 179 180 /** 181 * Returns an array of <code>FontData</code>s representing the receiver. 182 * On Windows, only one FontData will be returned per font. On X however, 183 * a <code>Font</code> object <em>may</em> be composed of multiple X 184 * fonts. To support this case, we return an array of font data objects. 185 * 186 * @return an array of font data objects describing the receiver 187 * 188 * @exception SWTException <ul> 189 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> 190 * </ul> 191 */ 192 public FontData[] getFontData() { 193 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 194 195 auto family = OS.pango_font_description_get_family(handle); 196 String name = fromStringz( family )._idup(); 197 float height = cast(float)OS.pango_font_description_get_size(handle) / OS.PANGO_SCALE; 198 ptrdiff_t pangoStyle = OS.pango_font_description_get_style(handle); 199 ptrdiff_t pangoWeight = OS.pango_font_description_get_weight(handle); 200 int style = SWT.NORMAL; 201 if (pangoStyle is OS.PANGO_STYLE_ITALIC) style |= SWT.ITALIC; 202 if (pangoStyle is OS.PANGO_STYLE_OBLIQUE) style |= SWT.ROMAN; 203 if (pangoWeight >= OS.PANGO_WEIGHT_BOLD) style |= SWT.BOLD; 204 auto fontString = OS.pango_font_description_to_string (handle); 205 auto buffer = fromStringz( fontString )._idup(); 206 FontData data = new FontData( buffer , height, style); 207 OS.g_free (fontString); 208 data.str = buffer; 209 return [data]; 210 } 211 212 /** 213 * Invokes platform specific functionality to allocate a new font. 214 * <p> 215 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public 216 * API for <code>Font</code>. It is marked public only so that it 217 * can be shared within the packages provided by SWT. It is not 218 * available on all platforms, and should never be called from 219 * application code. 220 * </p> 221 * 222 * @param device the device on which to allocate the color 223 * @param handle the handle for the font 224 * 225 * @private 226 */ 227 public static Font gtk_new(Device device, PangoFontDescription* handle) { 228 Font font = new Font(device); 229 font.handle = handle; 230 return font; 231 } 232 233 /** 234 * Returns an integer hash code for the receiver. Any two 235 * objects that return <code>true</code> when passed to 236 * <code>equals</code> must return the same value for this 237 * method. 238 * 239 * @return the receiver's hash 240 * 241 * @see #equals 242 */ 243 public override hash_t toHash() { 244 return cast(hash_t)handle; 245 } 246 247 void init_( String name, float height, int style, String fontString) { 248 if (name is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 249 if (height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 250 if (fontString !is null) { 251 handle = OS.pango_font_description_from_string (toStringz(fontString)); 252 if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES); 253 } else { 254 handle = OS.pango_font_description_new(); 255 if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES); 256 //byte[] buffer = Converter.wcsToMbcs(null, name, true); 257 OS.pango_font_description_set_family(handle, toStringz(name) ); 258 if (height > 0) { 259 OS.pango_font_description_set_size(handle, cast(int)(0.5f + height * OS.PANGO_SCALE)); 260 } 261 OS.pango_font_description_set_stretch(handle, OS.PANGO_STRETCH_NORMAL); 262 int pangoStyle = OS.PANGO_STYLE_NORMAL; 263 int pangoWeight = OS.PANGO_WEIGHT_NORMAL; 264 if ((style & SWT.ITALIC) !is 0) pangoStyle = OS.PANGO_STYLE_ITALIC; 265 if ((style & SWT.ROMAN) !is 0) pangoStyle = OS.PANGO_STYLE_OBLIQUE; 266 if ((style & SWT.BOLD) !is 0) pangoWeight = OS.PANGO_WEIGHT_BOLD; 267 OS.pango_font_description_set_style(handle, pangoStyle); 268 OS.pango_font_description_set_weight(handle, pangoWeight); 269 } 270 } 271 272 /** 273 * Returns <code>true</code> if the font has been disposed, 274 * and <code>false</code> otherwise. 275 * <p> 276 * This method gets the dispose state for the font. 277 * When a font has been disposed, it is an error to 278 * invoke any other method using the font. 279 * 280 * @return <code>true</code> when the font is disposed and <code>false</code> otherwise 281 */ 282 public override bool isDisposed() { 283 return handle is null; 284 } 285 286 /** 287 * Returns a string containing a concise, human-readable 288 * description of the receiver. 289 * 290 * @return a string representation of the receiver 291 */ 292 override 293 public String toString () { 294 if (isDisposed()) return "Font {*DISPOSED*}"; 295 return Format( "Font {{{}}", handle ); 296 } 297 298 }