/******************************************************************************* * Copyright (c) 2000, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.graphics; import org.eclipse.swt.*; /** * Instances of this class describe operating system fonts. *
* For platform-independent behaviour, use the get and set methods * corresponding to the following properties: *
FontData
* corresponds to a Windows LOGFONT
structure whose fields
* may be retrieved and modified.FontData
correspond
* to the entries in the font's XLFD name and may be retrieved and modified.
* dispose()
method is provided.
*
* @see Font
* @see Sample code and further information
*/
public final class FontData {
/**
* The company that produced the font
* (Warning: This field is platform dependent)
* * IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public String foundry; /** * The common name of the font * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public String fontFamily; /** * The weight ("medium", "bold") * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public String weight; /** * The slant ("o" for oblique, "i" for italic) * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public String slant; /** * The set width of the font * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public String setWidth; /** * Additional font styles * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public String addStyle; /** * The height of the font in pixels * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public int pixels; /** * The height of the font in tenths of a point * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public int points; /** * The horizontal screen resolution for which the font was designed * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public int horizontalResolution; /** * The vertical screen resolution for which the font was designed * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public int verticalResolution; /** * The font spacing ("m" for monospace, "p" for proportional) * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public String spacing; /** * The average character width for the font * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public int averageWidth; /** * The ISO character set registry * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public String characterSetRegistry; /** * The ISO character set name * (Warning: This field is platform dependent) ** IMPORTANT: This field is not part of the SWT * public API. It is marked public only so that it can be shared * within the packages provided by SWT. It is not available on all * platforms and should never be accessed from application code. *
* * @noreference This field is not intended to be referenced by clients. */ public String characterSetName; /** * The locales of the font */ String lang, country, variant; /** * Constructs a new uninitialized font data. */ public FontData () { } /** * Constructs a new FontData given a string representation * in the form generated by theFontData.toString
* method.
* * Note that the representation varies between platforms, * and a FontData can only be created from a string that was * generated on the same platform. *
* * @param string the string representation of aFontData
(must not be null)
*
* @exception IllegalArgumentException true
if the object is the same as this object and false
otherwise
*
* @see #hashCode
*/
public boolean equals (Object object) {
return (object == this) || ((object instanceof FontData) &&
getXlfd().equals(((FontData)object).getXlfd()));
}
/**
* Returns the height of the receiver in points.
*
* @return the height of this FontData
*
* @see #setHeight(int)
*/
public int getHeight() {
return points / 10;
}
/*public*/ float getHeightF() {
return points / 10f;
}
/**
* Returns the locale of the receiver.
* * The locale determines which platform character set this * font is going to use. Widgets and graphics operations that * use this font will convert UNICODE strings to the platform * character set of the specified locale. *
** On platforms where there are multiple character sets for a * given language/country locale, the variant portion of the * locale will determine the character set. *
* * @return theString
representing a Locale object
* @since 3.0
*/
public String getLocale () {
StringBuffer buffer = new StringBuffer ();
char sep = '_';
if (lang != null) {
buffer.append (lang);
buffer.append (sep);
}
if (country != null) {
buffer.append (country);
buffer.append (sep);
}
if (variant != null) {
buffer.append (variant);
}
String result = buffer.toString ();
int length = result.length ();
if (length > 0) {
if (result.charAt (length - 1) == sep) {
result = result.substring (0, length - 1);
}
}
return result;
}
/**
* Returns the name of the receiver.
* On platforms that support font foundries, the return value will
* be the foundry followed by a dash ("-") followed by the face name.
*
* @return the name of this FontData
*
* @see #setName
*/
public String getName() {
StringBuffer buffer = new StringBuffer();
if (foundry != null) {
buffer.append(foundry);
buffer.append("-");
}
if (fontFamily != null) buffer.append(fontFamily);
return buffer.toString();
}
/**
* Returns the style of the receiver which is a bitwise OR of
* one or more of the SWT
constants NORMAL, BOLD
* and ITALIC.
*
* @return the style of this FontData
*
* @see #setStyle
*/
public int getStyle() {
int style = 0;
if (weight != null && weight.equals("bold")) style |= SWT.BOLD;
if (slant != null && slant.equals("i")) style |= SWT.ITALIC;
return style;
}
String getXlfd() {
String s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14;
s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = s9 = s10 = s11 = s12 = s13 = s14 = "*";
if (foundry != null) s1 = foundry;
if (fontFamily != null) s2 = fontFamily;
if (weight != null) s3 = weight;
if (slant != null) s4 = slant;
if (setWidth != null) s5 = setWidth;
if (addStyle != null) s6 = addStyle;
if (pixels != 0) s7 = Integer.toString(pixels);
if (points != 0) s8 = Integer.toString(points);
if (horizontalResolution != 0) s9 = Integer.toString(horizontalResolution);
if (verticalResolution != 0) s10 = Integer.toString(verticalResolution);
if (spacing != null) s11 = spacing;
if (averageWidth != 0) s12 = Integer.toString(averageWidth);
if (characterSetRegistry != null) s13 = characterSetRegistry;
if (characterSetName != null) s14 = characterSetName;
return "-" + s1+ "-" + s2 + "-" + s3 + "-" + s4 + "-" + s5 + "-" + s6 + "-" + s7 + "-" + s8 + "-"
+ s9 + "-" + s10 + "-" + s11 + "-" + s12 + "-" + s13 + "-" + s14;
}
/**
* Returns an integer hash code for the receiver. Any two
* objects that return true
when passed to
* equals
must return the same value for this
* method.
*
* @return the receiver's hash
*
* @see #equals
*/
public int hashCode () {
return getXlfd().hashCode();
}
public static FontData motif_new(String xlfd) {
FontData fontData = new FontData();
fontData.setXlfd(xlfd);
return fontData;
}
/**
* Sets the height of the receiver. The parameter is
* specified in terms of points, where a point is one
* seventy-second of an inch.
*
* @param height the height of the FontData
*
* @exception IllegalArgumentException * Some platforms support font foundries. On these platforms, the name * of the font specified in setName() may have one of the following forms: *
* On platforms that do not support font foundries, only the face name
* (for example, "courier") is used in setName()
and
* getName()
.
*
* The locale determines which platform character set this * font is going to use. Widgets and graphics operations that * use this font will convert UNICODE strings to the platform * character set of the specified locale. *
** On platforms where there are multiple character sets for a * given language/country locale, the variant portion of the * locale will determine the character set. *
* * @param locale theString
representing a Locale object
* @see java.util.Locale#toString
*/
public void setLocale(String locale) {
lang = country = variant = null;
if (locale != null) {
char sep = '_';
int length = locale.length();
int firstSep, secondSep;
firstSep = locale.indexOf(sep);
if (firstSep == -1) {
firstSep = secondSep = length;
} else {
secondSep = locale.indexOf(sep, firstSep + 1);
if (secondSep == -1) secondSep = length;
}
if (firstSep > 0) lang = locale.substring(0, firstSep);
if (secondSep > firstSep + 1) country = locale.substring(firstSep + 1, secondSep);
if (length > secondSep + 1) variant = locale.substring(secondSep + 1);
}
}
/**
* Sets the style of the receiver to the argument which must
* be a bitwise OR of one or more of the SWT
* constants NORMAL, BOLD and ITALIC. All other style bits are
* ignored.
*
* @param style the new style for this FontData
*
* @see #getStyle
*/
public void setStyle(int style) {
weight = (style & SWT.BOLD) != 0 ? "bold" : "medium";
slant = (style & SWT.ITALIC) != 0 ? "i" : "r";
averageWidth = 0;
}
void setXlfd(String xlfd) {
int start, stop;
start = 1;
stop = xlfd.indexOf ("-", start);
foundry = xlfd.substring(start, stop);
if (foundry.equals("*")) foundry = null;
start = stop + 1;
stop = xlfd.indexOf ("-", start);
fontFamily = xlfd.substring(start, stop);
if (fontFamily.equals("*")) fontFamily = null;
start = stop + 1;
stop = xlfd.indexOf ("-", start);
weight = xlfd.substring(start, stop);
if (weight.equals("*")) weight = null;
start = stop + 1;
stop = xlfd.indexOf ("-", start);
slant = xlfd.substring(start, stop);
if (slant.equals("*")) slant = null;
start = stop + 1;
stop = xlfd.indexOf ("-", start);
setWidth = xlfd.substring(start, stop);
if (setWidth.equals("*")) setWidth = null;
start = stop + 1;
stop = xlfd.indexOf ("-", start);
addStyle = xlfd.substring(start, stop);
if (addStyle.equals("*")) addStyle = null;
start = stop + 1;
stop = xlfd.indexOf ("-", start);
String s = xlfd.substring(start, stop);
if (!s.equals("") && !s.equals("*"))
pixels = Integer.parseInt(s);
start = stop + 1;
stop = xlfd.indexOf ("-", start);
s = xlfd.substring(start, stop);
if (!s.equals("") && !s.equals("*"))
points = Integer.parseInt(s);
start = stop + 1;
stop = xlfd.indexOf ("-", start);
s = xlfd.substring(start, stop);
if (!s.equals("") && !s.equals("*"))
horizontalResolution = Integer.parseInt(s);
start = stop + 1;
stop = xlfd.indexOf ("-", start);
s = xlfd.substring(start, stop);
if (!s.equals("") && !s.equals("*"))
verticalResolution = Integer.parseInt(s);
start = stop + 1;
stop = xlfd.indexOf ("-", start);
spacing = xlfd.substring(start, stop);
start = stop + 1;
stop = xlfd.indexOf ("-", start);
s = xlfd.substring(start, stop);
if (!s.equals("") && !s.equals("*")) {
if (s.startsWith ("~")) {
s = "-" + s.substring(1);
}
averageWidth = Integer.parseInt(s);
}
start = stop + 1;
stop = xlfd.indexOf ("-", start);
characterSetRegistry = xlfd.substring(start, stop);
if (characterSetRegistry.equals("*")) characterSetRegistry = null;
start = stop + 1;
stop = xlfd.indexOf ("-", start);
characterSetName = xlfd.substring(start);
if (characterSetName.equals("*")) characterSetName = null;
}
/**
* Returns a string representation of the receiver which is suitable
* for constructing an equivalent instance using the
* FontData(String)
constructor.
*
* @return a string representation of the FontData
*
* @see FontData
*/
public String toString() {
return "1|" + fontFamily + "|" + getHeightF() + "|" + getStyle() + "|" +
"MOTIF|1|" + getXlfd();
}
}