All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.swt.graphics.FontData Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2000, 2011 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: *

*
height
the height of the font in points
*
name
the face name of the font, which may include the foundry
*
style
A bitwise combination of NORMAL, ITALIC and BOLD
*
* If extra, platform-dependent functionality is required: *
    *
  • On Windows, the data member of the FontData * corresponds to a Windows LOGFONT structure whose fields * may be retrieved and modified.
  • *
  • On X, the fields of the FontData correspond * to the entries in the font's XLFD name and may be retrieved and modified. *
* Application code does not need to explicitly release the * resources managed by each instance when those instances are no longer * required, and thus no dispose() method is provided. * * @see Font * @see Sample code and further information */ public final class FontData { /** * the font 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 name; /** * The height of the font data in points * (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 float height; /** * the font style * (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 style; /** * the NSFont font 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 nsName; /** * The locales of the font */ String lang, country, variant; /** * Constructs a new uninitialized font data. */ public FontData () { this("", 12, SWT.NORMAL); } /** * Constructs a new FontData given a string representation * in the form generated by the FontData.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 a FontData (must not be null) * * @exception IllegalArgumentException
    *
  • ERROR_NULL_ARGUMENT - if the argument is null
  • *
  • ERROR_INVALID_ARGUMENT - if the argument does not represent a valid description
  • *
* * @see #toString */ public FontData(String string) { if (string == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); int start = 0; int end = string.indexOf('|'); if (end == -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); String version1 = string.substring(start, end); try { if (Integer.parseInt(version1) != 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); } catch (NumberFormatException e) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } start = end + 1; end = string.indexOf('|', start); if (end == -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); String name = string.substring(start, end); start = end + 1; end = string.indexOf('|', start); if (end == -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); float height = 0; try { height = Float.parseFloat(string.substring(start, end)); } catch (NumberFormatException e) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } start = end + 1; end = string.indexOf('|', start); if (end == -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); int style = 0; try { style = Integer.parseInt(string.substring(start, end)); } catch (NumberFormatException e) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } start = end + 1; end = string.indexOf('|', start); setName(name); setHeight(height); setStyle(style); if (end == -1) return; String platform = string.substring(start, end); start = end + 1; end = string.indexOf('|', start); if (end == -1) return; String version2 = string.substring(start, end); if (platform.equals("COCOA") && version2.equals("1")) { start = end + 1; end = string.length(); if (start < end) nsName = string.substring(start, end); } } /** * Constructs a new font data given a font name, * the height of the desired font in points, * and a font style. * * @param name the name of the font (must not be null) * @param height the font height in points * @param style a bit or combination of NORMAL, BOLD, ITALIC * * @exception IllegalArgumentException
    *
  • ERROR_NULL_ARGUMENT - when the font name is null
  • *
  • ERROR_INVALID_ARGUMENT - if the height is negative
  • *
*/ public FontData(String name, int height, int style) { setName(name); setHeight(height); setStyle(style); } /*public*/ FontData(String name, float height, int style) { setName(name); setHeight(height); setStyle(style); } /** * Compares the argument to the receiver, and returns true * if they represent the same object using a class * specific comparison. * * @param object the object to compare with this object * @return true if the object is the same as this object and false otherwise * * @see #hashCode */ @Override public boolean equals (Object object) { if (object == this) return true; if (!(object instanceof FontData)) return false; FontData data = (FontData)object; return name.equals(data.name) && height == data.height && style == data.style; } /** * Returns the height of the receiver in points. * * @return the height of this FontData * * @see #setHeight(int) */ public int getHeight() { return (int)height; } /*public*/ float getHeightF() { return height; } /** * 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 the String 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() { return name; } /** * 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() { return style; } /** * 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 */ @Override public int hashCode () { return name.hashCode() ^ getHeight() << 8 ^ style; } /** * 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
    *
  • ERROR_INVALID_ARGUMENT - if the height is negative
  • *
* * @see #getHeight */ public void setHeight(int height) { if (height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); this.height = height; } /*public*/ void setHeight(float height) { if (height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); this.height = height; } /** * Sets 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. *

* * @param locale the String 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 name of the receiver. *

* Some platforms support font foundries. On these platforms, the name * of the font specified in setName() may have one of the following forms: *

    *
  1. a face name (for example, "courier")
  2. *
  3. a foundry followed by a dash ("-") followed by a face name (for example, "adobe-courier")
  4. *
* In either case, the name returned from getName() will include the * foundry. *

*

* On platforms that do not support font foundries, only the face name * (for example, "courier") is used in setName() and * getName(). *

* * @param name the name of the font data (must not be null) * @exception IllegalArgumentException
    *
  • ERROR_NULL_ARGUMENT - when the font name is null
  • *
* * @see #getName */ public void setName(String name) { if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.name = name; nsName = null; } /** * 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) { this.style = style; nsName = 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 */ @Override public String toString() { StringBuffer buffer = new StringBuffer(128); buffer.append("1|"); buffer.append(getName()); buffer.append("|"); buffer.append(getHeightF()); buffer.append("|"); buffer.append(getStyle()); buffer.append("|"); buffer.append("COCOA|1|"); if (nsName != null) buffer.append(nsName); return buffer.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy