home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 21.0 KB | 644 lines |
- /*
- * @(#)TextAttributeSet.java 1.12 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- /*
- * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
- * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
- *
- * The original version of this source code and documentation is
- * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
- * of IBM. These materials are provided under terms of a License
- * Agreement between Taligent and Sun. This technology is protected
- * by multiple US and International patents.
- *
- * This notice and attribution to Taligent may not be removed.
- * Taligent is a registered trademark of Taligent, Inc.
- *
- */
-
- package java.awt.font;
-
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.text.AttributeSet;
- import java.text.MutableAttributeSet;
- import java.awt.Color;
- import java.awt.Font;
-
- /**
- * TextAttributeSet provides a simple implementation of MutableAttributeSet.
- */
- public final class TextAttributeSet implements MutableAttributeSet,
- java.io.Serializable
- {
- // For use by input system.
-
- /**
- * Constant for the attribute "language". The value of this attribute should
- * be an instance of Locale.
- * @see java.util.Locale
- */
- public static final String LANGUAGE = "egaugnal";
-
- /**
- * Constant for the attribute "reading". In languages where the written form
- * and the pronunciation of a word are only loosely related (such as
- * Japanese), it is often necessary to store the reading (pronunciation)
- * along with the written form. This is an annotation attribute. The value
- * should be an instance of Annotation holding an instance of String.
- * @see Annotation
- * @see java.lang.String
- */
- public static final String READING = "gnidaer";
-
- /**
- * Constant for the attribute "input method segment". Input methods often
- * break up text into segments, which usually correspond to words. This
- * is an annotation attribute. The value should be an instance of
- * Annotation holding a value of null.
- * @see java.lang.String
- */
- public static final String INPUT_METHOD_SEGMENT = "tnemges dohtem tupni";
-
- /**
- * Swap foreground and background colors (for input method use).
- * <p>Values are instances of Boolean.
- * The default is not to swap the colors.
- * If the foreground and backround color attributes are both defined,
- * this causes them to be swapped when rendering text. If either is
- * defaulted the exact effect is undefined-- generally it will produce
- * an 'inverted' appearance.
- */
- public static final String SWAP_COLORS = "swap_colors";
-
- /** Swap foreground and background colors. */
- public static final Boolean SWAP_COLORS_ON = new Boolean(true);
-
- /** Do not swap foreground and background colors. */
- public static final Boolean SWAP_COLORS_OFF = new Boolean(false);
-
- /** Default to not swap foreground and background colors. */
- public static final Boolean SWAP_COLORS_DEFAULT = SWAP_COLORS_OFF;
-
- // For use with Font.
- // values for the features are examples; final values, including whether they are
- // strings or numbers, are TBD.
- // It's recommended that all Fonts support these values. Custom fonts may add new
- // values for these features, and even new features.
-
- /**
- * The unlocalized font family name.
- * <p>Values are instances of String, e.g. "Serif", "Palatino".
- * The default value is unspecified.
- */
- public static final String FAMILY = "family";
-
- public static final String FAMILY_ALL = new String("All");
-
- /**
- * The weight of the font.
- * <p>Values are instances of Float. The value is between 0.0 and 2.0.
- * This roughly corresponds to the ratio of the stem width to the width of
- * regular characters
- * for this typeface. Lower values are lighter.
- */
- public static final String WEIGHT = "weight";
-
- /**
- * The default weight if WEIGHT is unspecified. This value is 1.0.
- * @see #WEIGHT
- */
- public static final Float WEIGHT_DEFAULT = new Float(1.0f);
-
- /**
- * The bold weight for the font. This value is 1.5.
- * @see #WEIGHT
- */
- public static final Float WEIGHT_BOLD = new Float(1.5f);
-
- /**
- * The light weight for the font. This value is 0.7.
- * @see #WEIGHT
- */
- public static final Float WEIGHT_LIGHT = new Float(0.7f);
-
- /**
- * The posture of the font.
- * <p>Values are instances of Float. The value is the run/rise, generally
- * between -1.0 and 1.0, where negative values indicate a leftwards slant,
- * positive a rightwards slant, and zero no slant. There is no guarantee,
- * however, that the slope of the text as returned by font will be the
- * same as the posture value.
- * @see Font#getItalicAngle
- */
- public static final String POSTURE = "posture";
-
- /**
- * The default posture if POSTURE is unspecified. This value is 0.0.
- * @see #POSTURE
- */
- public static final Float POSTURE_DEFAULT = new Float(0.0f);
-
- /**
- * The standard italic posture for the font. This value is 0.333.
- * @see #POSTURE
- */
- public static final Float POSTURE_ITALIC = new Float(0.333f);
-
- /**
- * The font size in points.
- * <p> Values are instances of Float. The value can be any positive
- * value, although most fonts will pin to a reasonable range.
- * The default value is unspecified.
- */
- public static final String SIZE = "size";
-
- /**
- * The transform of the font.
- * <p> Values are instances of Transform.
- * The default value is 'null', i.e. when not specified the font uses its default transform.
- */
- public static final String TRANSFORM = "transform";
-
- /**
- * The font to use to render this text.
- * <p>Values are instances of Font.
- * The default value is 'null', i.e. text layout
- * will try to resolve the font as best it can for the given text.
- */
- public static final String FONT = "font";
-
- /**
- * An embedded graphic to display in the text.
- * <p>Values are instances of GraphicAttribute. All characters with
- * this attribute appear as the graphic. Usually, the character
- * used to represent embedded graphics is the Unicode Replacement
- * Character, '\uFFFD'.
- */
- public static final String EMBEDDED_GRAPHIC = "graphic";
-
- /**
- * The embedding level for nested bidirectional runs.
- * <p>TextLayout assumes this feature has the same value for all text in a
- * paragraph.
- * <p>Values are instances of Integer, between 0 and 31 inclusive. Values
- * from 0 to 15 represent embeddings. If bit 4 is set (values from 16 to
- * 31) they represent directional overrides. Even values indicate
- * left-to-right, odd indicate right-to-left.
- * Within an embedding, text assumes a base line direction corresponding
- * to the embedding, and the standard bidirectional properties of the text
- * applies. Within an override, all text flows in the given direction,
- * and the standard bidirectional properties of the text are overridden.
- * See the Unicode Standard v. 2.0, section 3-11.
- */
- public static final String BIDI_EMBEDDING = "bidi_embedding";
-
- /**
- * The default embedding if BIDI_EMBEDDING is unspecified. This value is 0.
- * @see #BIDI_EMBEDDING
- */
- public static final Integer BIDI_EMBEDDING_DEFAULT = new Integer(0);
-
- /**
- * The background color.
- * <p>Values are instances of Color.
- * The default value is 'null', meaning a transparent background.
- */
- public static final String BACKGROUND = "background";
-
- /**
- * The foreground color.
- * <p>Values are instances of Color.
- * The default value is 'null', meaning use the current color in the Graphics.
- */
- public static final String FOREGROUND = "foreground";
-
- /**
- * Adorn the text with underlining.
- * <p>Values are instances of Byte. The standard underline is UNDERLINE_ON,
- * the low underlines are for input method highlighting.
- */
- public static final String UNDERLINE = "underline";
-
- /** No underline. */
- public static final Byte UNDERLINE_OFF = new Byte((byte)-1);
-
- /**
- * Standard underline at the roman baseline for roman text, and below
- * the decenders for other text.
- */
- public static final Byte UNDERLINE_ON = new Byte((byte)0);
-
- /** Single pixel solid low underline. */
- public static final Byte UNDERLINE_LOW_ONE_PIXEL = new Byte((byte)1);
-
- /** Double pixel solid low underline. */
- public static final Byte UNDERLINE_LOW_TWO_PIXEL = new Byte((byte)2);
-
- /** Single pixel dotted low underline. */
- public static final Byte UNDERLINE_LOW_DOTTED = new Byte((byte)3);
-
- /** Double pixel gray low underline. */
- public static final Byte UNDERLINE_LOW_GRAY = new Byte((byte)4);
-
- /** Single pixel dashed low underline. */
- public static final Byte UNDERLINE_LOW_DASHED = new Byte((byte)5);
-
- /**
- * Adorn the text with strikethrough.
- * <p>Values are instances of Boolean.
- */
- public static final String STRIKETHROUGH = "strikethrough";
-
- /**
- * The default strikethrough if STRIKETHROUGH is unspecified, the value is
- * false.
- * @see #STRIKETHROUGH
- */
- public static final Boolean STRIKETHROUGH_DEFAULT = new Boolean(false);
-
- /**
- * A single strikethrough. This value is true.
- * @see #STRIKETHROUGH
- */
- public static final Boolean STRIKETHROUGH_ON = new Boolean(true);
-
- /**
- * Super and subscripting.
- * <p>Values are arrays of two floats. The first value is is used to scale
- * the point size, the second computes the offset as a ratio of the point
- * size. For example, when applied to a 12 point font, a value of
- * [.75, -.5] requests a point size of 9, positioned 6 points above
- * the baseline.
- */
- public static final String SUPERSUBSCRIPT = "supersubscript";
-
- /**
- * The default supersubscript if SUPERSUBSCRIPT is unspecified. This
- * value is [1, 0].
- * @see #SUPERSUBSCRIPT
- */
- public static final float[] SUPERSUBSCRIPT_DEFAULT = { 1.0f, 0.0f };
-
- /**
- * Standard superscript. This value is [0.75, -0.5].
- * @see #SUPERSUBSCRIPT
- */
- public static final float[] SUPERSUBSCRIPT_UP1 = { 0.75f, -0.5f };
-
- /**
- * Standard subscript. This value is [0.75, 0.5].
- * @see #SUPERSUBSCRIPT
- */
- public static final float[] SUPERSUBSCRIPT_DOWN1 = { 0.75f, 0.5f };
-
- /**
- * Justification for the paragraph.
- * <p>TextLayout assumes this feature has the same value for all text in a
- * paragraph. Value is a float, extracted as a Double. This is a ratio
- * controlling the space adjustment. For example, if a line's natural
- * advance is 50 pixels, and it is asked to justify to a width of 100
- * pixels, a ratio of 1.0 results in a justified line with an advance of
- * 100 pixels, and a ratio of 0.5 results in a justified
- * line with an advance of 75 pixels.
- */
- public static final String JUSTIFICATION = "justification";
-
- /**
- * The default justification if JUSTIFICATION is unspecified. This value
- * is 1.0.
- * @see #JUSTIFICATION
- */
- public static final Float JUSTIFICATION_DEFAULT = new Float(1.0f);
-
- /**
- * Justify the line to the full requested width. This value is 1.0.
- * @see #JUSTIFICATION
- */
- public static final Float JUSTIFICATION_FULL = new Float(1.0f);
-
- /**
- * Do not allow the line to be justified. This value is 0.0.
- * @see #JUSTIFICATION
- */
- public static final Float JUSTIFICATION_NONE = new Float(0.0f);
-
- /**
- * The run direction of the line, either left-to-right or right-to-left.
- * On vertical lines, left-to-right means top-to-bottom.
- * <p>TextLayout assumes this feature has the same value for all text in
- * a paragraph.
- * <p>Values are instances of boolean.
- */
- public static final String RUN_DIRECTION = "run_direction";
-
- /**
- * The default direction if RUN_DIRECTION is unspecified. This value is
- * false, indicating left-to-right.
- * @see #RUN_DIRECTION
- */
- public static final Boolean RUN_DIRECTION_DEFAULT = new Boolean(false);
-
- /**
- * Left-to-right (top-to-bottom) run direction. This value is false.
- * @see #RUN_DIRECTION
- */
- public static final Boolean RUN_DIRECTION_LTR = new Boolean(false);
-
- /**
- * Right-to-left (bottom-to-top) run direction. This value is true.
- * @see #RUN_DIRECTION
- */
- public static final Boolean RUN_DIRECTION_RTL = new Boolean(true);
-
- /**
- * The bidirectional layout control for roman numeral text.
- * <p>Values are instances of Integer.
- */
- static final String BIDI_NUMERIC = "bidi_numeric";
-
- /**
- * Interpret roman numerals as roman numerals always.
- */
- static final Integer BIDI_NUMERIC_STRONG_ROMAN = new Integer(IncrementalBidi.EN);
-
- /**
- * Interpret roman numerals as arabic numerals always.
- */
- static final Integer BIDI_NUMERIC_STRONG_ARABIC = new Integer(IncrementalBidi.AN);
-
- /**
- * Interpret roman numerals as having arabic line order.
- */
- static final Integer BIDI_NUMERIC_WEAK_ARABIC = new Integer(IncrementalBidi.WA);
-
- /*
- * The default interpretation if BIDI_NUMERIC is unspecified. This value
- * is STRONG_ROMAN,
- * indicating that roman numerals should always be formatted as such.
- */
- static final Integer BIDI_NUMERIC_DEFAULT =
- BIDI_NUMERIC_STRONG_ROMAN;
-
- /**
- * Baseline to which to align lines.
- * <p>TextLayout assumes this feature has the same value for all text
- * in a paragraph. Regardless of the baselines to which characters align,
- * the layout as a whole will align to this line.
- * <p>Values are instances of Integer.
- * @see Font#ROMAN_BASELINE
- * @see Font#CENTER_BASELINE
- * @see Font#HANGING_BASELINE
- * @see Font#getBaselineOffsetsFor
- * @see Font#getBaselineFor
- */
- public static final String BASELINE = "baseline";
-
- /**
- * The default baseline if BASELINE is unspecified. This value is
- * Font.ROMAN_BASELINE.
- * @see #BASELINE
- */
- public static final Byte BASELINE_DEFAULT =
- new Byte(Font.ROMAN_BASELINE);
- /**
- * Use a roman baseline for the line. This value is Font.ROMAN_BASELINE.
- * @see #BASELINE
- */
- public static final Byte BASELINE_ROMAN =
- new Byte(Font.ROMAN_BASELINE);
- /**
- * Use a center baseline for the line. This value is Font.CENTER_BASELINE.
- * @see #BASELINE
- */
- public static final Byte BASELINE_IDEOGRAPHIC =
- new Byte(Font.CENTER_BASELINE);
-
- /**
- * Use a hanging baseline for the line. This value is
- * Font.HANGING_BASELINE.
- * @see #BASELINE
- */
- public static final Byte BASELINE_HANGING =
- new Byte(Font.HANGING_BASELINE);
-
- /**
- * Baseline offsets used by a line.
- * <p>TextLayout assumes this feature has the same value for all text
- * in a paragraph.
- * <p>Values are arrays of three floats. The distance from one baseline
- * to another is <code>offsets[b] - offsets[a]</code>, where a is the
- * starting baseline. For example, the distance from the hanging
- * baseline to the ideographic baseline is:
- * <code>offsets[BASELINE_IDEOGRAPHIC.byteValue()]-
- * offsets[BASELINE_HANGING.byteValue()]</code>
- * <p>If defaulted, TextLayout attempts to find a
- * reasonable value for all text in the paragraph.
- */
- public static final String BASELINE_OFFSETS = "baseline_offsets";
-
-
- /**
- * An empty attribute set, for convenience.
- */
- public static final AttributeSet EMPTY = new TextAttributeSet();
-
- private Hashtable table = new Hashtable();
-
- /**
- * Construct a new mutable attribute set.
- */
- public TextAttributeSet() {
- }
-
- /**
- * Construct a new mutable attribute set containing the provided attributes.
- * @param source the attributes to copy into this set.
- */
- public TextAttributeSet(AttributeSet source) {
- add(source);
- }
-
- /**
- * Construct a new mutable attribute set containing the provided attribute.
- * @param name the name of the attribute.
- * @param value the value of the attribute.
- */
- public TextAttributeSet(String name, Object value) {
- add(name, value);
- }
-
- // internal only
- private TextAttributeSet(Hashtable table) {
- this.table = table;
- }
-
- /**
- * Returns true if the set is empty.
- */
- public boolean isEmpty() {
- return table.isEmpty();
- }
-
- /**
- * Returns the number of attributes.
- */
- public int getSize() {
- return table.size();
- }
-
- /**
- * Returns an enumeration over the names of the attributes in the set.
- * The elements of the enumeration are all Strings.
- */
- public Enumeration names() {
- return table.keys();
- }
-
- /**
- * Returns the value of the attribute with this name, or null if the
- * attribute is not defined.
- */
- public Object get(String name) {
- return table.get(name);
- }
-
- /**
- * Returns true if this set contains this attribute with an equal value,
- * or if the value is null and the attribute is not defined.
- */
- public boolean contains(String name, Object value) {
- Object myValue = get(name);
- return value == null ? myValue == null : value.equals(myValue);
- }
-
- /**
- * Returns true if this set contains all the attributes with equal values.
- */
- public boolean contains(AttributeSet rhs) {
- if (table.size() < rhs.getSize()) {
- return false;
- }
-
- Enumeration names = rhs.names();
- while (names.hasMoreElements()) {
- String name = (String)names.nextElement();
- if (!rhs.get(name).equals(table.get(name))) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * Return true if the argument is an attribute set containing the same
- * attributes as this.
- * @param rhs the object which may be an attribute set.
- */
- public boolean equals(Object rhs) {
- try {
- return equals((AttributeSet) rhs);
- }
- catch (ClassCastException e) {
- return false;
- }
- }
-
- /**
- * Return true if the argument contains the same attributes as this.
- * @param rhs the attribute set to test against.
- */
- public boolean equals(AttributeSet rhs) {
- return rhs != null && getSize() == rhs.getSize() &&
- rhs.contains(this);
- }
-
- /**
- * Return a hash code.
- */
- public int hashCode() {
- // equal sets must have equal hash code
- // so default hash code is no good, it is unique
- int hash = table.size();
-
- /* this is too expensive unless we cache
- Enumeration e = table.elements();
- while (e.hasMoreElements()) {
- hash = (hash << 2) ^ e.nextElement().hashCode();
- }
- */
-
- return hash;
- }
-
- /**
- * Remove any existing attribute with the same name, and add a new
- * attribute with the given name and value. The value must be
- * immutable, or not mutated by any client.
- * @param name the name of the attribute to add
- * @param value the value of the attribute to add
- */
- public void add(String name, Object value) {
- table.put(name, value);
- }
-
- /**
- * Remove any existing attributes with the same names, and add the
- * new attributes.
- * @param attributes the set of attributes to add
- */
- public void add(AttributeSet attributes) {
- Enumeration names = attributes.names();
- while (names.hasMoreElements()) {
- String name = (String)names.nextElement();
- table.put(name, attributes.get(name));
- }
- }
-
- /**
- * Remove any existing attribute with the given name.
- * @param name the name of the attribute to remove
- */
- public void remove(String name) {
- table.remove(name);
- }
-
- /**
- * Remove any existing attributes with the given names.
- * @param names an enumeration over the names of attributes to remove. The
- * elements of the enumeration are Strings.
- */
- public void remove(Enumeration names) {
- while (names.hasMoreElements())
- table.remove(names.nextElement());
- }
-
- /**
- * Removes all attributes, then adds the new attributes.
- */
- public void set(AttributeSet attributes) {
- table.clear();
- add(attributes);
- }
-
- /**
- * Return a copy of this attribute set. Attributes are shared with the
- * copy.
- */
- public Object clone() {
- return new TextAttributeSet((Hashtable)table.clone());
- }
- }
-