| 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.FontMetrics; |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
import dwt.internal.win32.WINTYPES; |
|---|
| 17 |
|
|---|
| 18 |
/** |
|---|
| 19 |
* Instances of this class provide measurement information |
|---|
| 20 |
* about fonts including ascent, descent, height, leading |
|---|
| 21 |
* space between rows, and average character width. |
|---|
| 22 |
* <code>FontMetrics</code> are obtained from <code>GC</code>s |
|---|
| 23 |
* using the <code>getFontMetrics()</code> method. |
|---|
| 24 |
* |
|---|
| 25 |
* @see GC#getFontMetrics |
|---|
| 26 |
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> |
|---|
| 27 |
*/ |
|---|
| 28 |
|
|---|
| 29 |
public final class FontMetrics { |
|---|
| 30 |
|
|---|
| 31 |
/** |
|---|
| 32 |
* On Windows, handle is a Win32 TEXTMETRIC struct |
|---|
| 33 |
* On Photon, handle is a Photon FontQueryInfo struct |
|---|
| 34 |
* (Warning: This field is platform dependent) |
|---|
| 35 |
* <p> |
|---|
| 36 |
* <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT |
|---|
| 37 |
* public API. It is marked public only so that it can be shared |
|---|
| 38 |
* within the packages provided by DWT. It is not available on all |
|---|
| 39 |
* platforms and should never be accessed from application code. |
|---|
| 40 |
* </p> |
|---|
| 41 |
*/ |
|---|
| 42 |
public TEXTMETRIC handle; |
|---|
| 43 |
|
|---|
| 44 |
/** |
|---|
| 45 |
* Prevents instances from being created outside the package. |
|---|
| 46 |
*/ |
|---|
| 47 |
this() { |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
/** |
|---|
| 51 |
* Compares the argument to the receiver, and returns true |
|---|
| 52 |
* if they represent the <em>same</em> object using a class |
|---|
| 53 |
* specific comparison. |
|---|
| 54 |
* |
|---|
| 55 |
* @param object the object to compare with this object |
|---|
| 56 |
* @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise |
|---|
| 57 |
* |
|---|
| 58 |
* @see #hashCode |
|---|
| 59 |
*/ |
|---|
| 60 |
override public int opEquals (Object object) { |
|---|
| 61 |
if (object is this) return true; |
|---|
| 62 |
if( auto metricObj = cast(FontMetrics)object ){ |
|---|
| 63 |
auto metric = metricObj.handle; |
|---|
| 64 |
return handle.tmHeight is metric.tmHeight && |
|---|
| 65 |
handle.tmAscent is metric.tmAscent && |
|---|
| 66 |
handle.tmDescent is metric.tmDescent && |
|---|
| 67 |
handle.tmInternalLeading is metric.tmInternalLeading && |
|---|
| 68 |
handle.tmExternalLeading is metric.tmExternalLeading && |
|---|
| 69 |
handle.tmAveCharWidth is metric.tmAveCharWidth && |
|---|
| 70 |
handle.tmMaxCharWidth is metric.tmMaxCharWidth && |
|---|
| 71 |
handle.tmWeight is metric.tmWeight && |
|---|
| 72 |
handle.tmOverhang is metric.tmOverhang && |
|---|
| 73 |
handle.tmDigitizedAspectX is metric.tmDigitizedAspectX && |
|---|
| 74 |
handle.tmDigitizedAspectY is metric.tmDigitizedAspectY && |
|---|
| 75 |
// handle.tmFirstChar is metric.tmFirstChar && |
|---|
| 76 |
// handle.tmLastChar is metric.tmLastChar && |
|---|
| 77 |
// handle.tmDefaultChar is metric.tmDefaultChar && |
|---|
| 78 |
// handle.tmBreakChar is metric.tmBreakChar && |
|---|
| 79 |
handle.tmItalic is metric.tmItalic && |
|---|
| 80 |
handle.tmUnderlined is metric.tmUnderlined && |
|---|
| 81 |
handle.tmStruckOut is metric.tmStruckOut && |
|---|
| 82 |
handle.tmPitchAndFamily is metric.tmPitchAndFamily && |
|---|
| 83 |
handle.tmCharSet is metric.tmCharSet; |
|---|
| 84 |
} |
|---|
| 85 |
return false; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
/** |
|---|
| 89 |
* Returns the ascent of the font described by the receiver. A |
|---|
| 90 |
* font's <em>ascent</em> is the distance from the baseline to the |
|---|
| 91 |
* top of actual characters, not including any of the leading area, |
|---|
| 92 |
* measured in pixels. |
|---|
| 93 |
* |
|---|
| 94 |
* @return the ascent of the font |
|---|
| 95 |
*/ |
|---|
| 96 |
public int getAscent() { |
|---|
| 97 |
return handle.tmAscent - handle.tmInternalLeading; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
/** |
|---|
| 101 |
* Returns the average character width, measured in pixels, |
|---|
| 102 |
* of the font described by the receiver. |
|---|
| 103 |
* |
|---|
| 104 |
* @return the average character width of the font |
|---|
| 105 |
*/ |
|---|
| 106 |
public int getAverageCharWidth() { |
|---|
| 107 |
return handle.tmAveCharWidth; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
/** |
|---|
| 111 |
* Returns the descent of the font described by the receiver. A |
|---|
| 112 |
* font's <em>descent</em> is the distance from the baseline to the |
|---|
| 113 |
* bottom of actual characters, not including any of the leading area, |
|---|
| 114 |
* measured in pixels. |
|---|
| 115 |
* |
|---|
| 116 |
* @return the descent of the font |
|---|
| 117 |
*/ |
|---|
| 118 |
public int getDescent() { |
|---|
| 119 |
return handle.tmDescent; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
/** |
|---|
| 123 |
* Returns the height of the font described by the receiver, |
|---|
| 124 |
* measured in pixels. A font's <em>height</em> is the sum of |
|---|
| 125 |
* its ascent, descent and leading area. |
|---|
| 126 |
* |
|---|
| 127 |
* @return the height of the font |
|---|
| 128 |
* |
|---|
| 129 |
* @see #getAscent |
|---|
| 130 |
* @see #getDescent |
|---|
| 131 |
* @see #getLeading |
|---|
| 132 |
*/ |
|---|
| 133 |
public int getHeight() { |
|---|
| 134 |
return handle.tmHeight; |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
/** |
|---|
| 138 |
* Returns the leading area of the font described by the |
|---|
| 139 |
* receiver. A font's <em>leading area</em> is the space |
|---|
| 140 |
* above its ascent which may include accents or other marks. |
|---|
| 141 |
* |
|---|
| 142 |
* @return the leading space of the font |
|---|
| 143 |
*/ |
|---|
| 144 |
public int getLeading() { |
|---|
| 145 |
return handle.tmInternalLeading; |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
/** |
|---|
| 149 |
* Returns an integer hash code for the receiver. Any two |
|---|
| 150 |
* objects that return <code>true</code> when passed to |
|---|
| 151 |
* <code>equals</code> must return the same value for this |
|---|
| 152 |
* method. |
|---|
| 153 |
* |
|---|
| 154 |
* @return the receiver's hash |
|---|
| 155 |
* |
|---|
| 156 |
* @see #equals |
|---|
| 157 |
*/ |
|---|
| 158 |
override public hash_t toHash() { |
|---|
| 159 |
return handle.tmHeight ^ handle.tmAscent ^ handle.tmDescent ^ |
|---|
| 160 |
handle.tmInternalLeading ^ handle.tmExternalLeading ^ |
|---|
| 161 |
handle.tmAveCharWidth ^ handle.tmMaxCharWidth ^ handle.tmWeight ^ |
|---|
| 162 |
handle.tmOverhang ^ handle.tmDigitizedAspectX ^ handle.tmDigitizedAspectY ^ |
|---|
| 163 |
// handle.tmFirstChar ^ handle.tmLastChar ^ handle.tmDefaultChar ^ handle.tmBreakChar ^ |
|---|
| 164 |
handle.tmItalic ^ handle.tmUnderlined ^ handle.tmStruckOut ^ |
|---|
| 165 |
handle.tmPitchAndFamily ^ handle.tmCharSet; |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
/** |
|---|
| 169 |
* Invokes platform specific functionality to allocate a new font metrics. |
|---|
| 170 |
* <p> |
|---|
| 171 |
* <b>IMPORTANT:</b> This method is <em>not</em> part of the public |
|---|
| 172 |
* API for <code>FontMetrics</code>. It is marked public only so that |
|---|
| 173 |
* it can be shared within the packages provided by DWT. It is not |
|---|
| 174 |
* available on all platforms, and should never be called from |
|---|
| 175 |
* application code. |
|---|
| 176 |
* </p> |
|---|
| 177 |
* |
|---|
| 178 |
* @param handle the <code>TEXTMETRIC</code> containing information about a font |
|---|
| 179 |
* @return a new font metrics object containing the specified <code>TEXTMETRIC</code> |
|---|
| 180 |
*/ |
|---|
| 181 |
public static FontMetrics win32_new(TEXTMETRIC* handle) { |
|---|
| 182 |
FontMetrics fontMetrics = new FontMetrics(); |
|---|
| 183 |
fontMetrics.handle = *handle; |
|---|
| 184 |
return fontMetrics; |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
} |
|---|