From 5f7a92e6c00bbbd32e615b54f57a9f2d205a47f2 Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Thu, 20 Apr 2000 17:33:07 +0000 Subject: [PATCH] RichEdit control - JDK 1.1 compatibility classes X-SVN-Rev: 1180 --- .../ibm/richtext/textlayout/FontUtils.java | 29 ++ .../textlayout/Graphics2DConversion.java | 56 ++++ .../textlayout/attributes/AttributeKey.java | 165 ++++++++++++ .../richtext/textlayout/attributes/Map.java | 51 ++++ .../textlayout/attributes/TextAttribute.java | 248 ++++++++++++++++++ icu4j/src/com/ibm/textlayout/FontUtils.java | 29 ++ .../ibm/textlayout/Graphics2DConversion.java | 56 ++++ .../textlayout/attributes/AttributeKey.java | 165 ++++++++++++ .../com/ibm/textlayout/attributes/Map.java | 51 ++++ .../textlayout/attributes/TextAttribute.java | 248 ++++++++++++++++++ 10 files changed, 1098 insertions(+) create mode 100755 icu4j/src/com/ibm/richtext/textlayout/FontUtils.java create mode 100755 icu4j/src/com/ibm/richtext/textlayout/Graphics2DConversion.java create mode 100755 icu4j/src/com/ibm/richtext/textlayout/attributes/AttributeKey.java create mode 100755 icu4j/src/com/ibm/richtext/textlayout/attributes/Map.java create mode 100755 icu4j/src/com/ibm/richtext/textlayout/attributes/TextAttribute.java create mode 100755 icu4j/src/com/ibm/textlayout/FontUtils.java create mode 100755 icu4j/src/com/ibm/textlayout/Graphics2DConversion.java create mode 100755 icu4j/src/com/ibm/textlayout/attributes/AttributeKey.java create mode 100755 icu4j/src/com/ibm/textlayout/attributes/Map.java create mode 100755 icu4j/src/com/ibm/textlayout/attributes/TextAttribute.java diff --git a/icu4j/src/com/ibm/richtext/textlayout/FontUtils.java b/icu4j/src/com/ibm/richtext/textlayout/FontUtils.java new file mode 100755 index 00000000000..9d09528e0ee --- /dev/null +++ b/icu4j/src/com/ibm/richtext/textlayout/FontUtils.java @@ -0,0 +1,29 @@ +/* + * @(#)$RCSfile: FontUtils.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:30:44 $ + * + * (C) Copyright IBM Corp. 1998-1999. All Rights Reserved. + * + * The program is provided "as is" without any warranty express or + * implied, including the warranty of non-infringement and the implied + * warranties of merchantibility and fitness for a particular purpose. + * IBM will not be liable for any damages suffered by you as a result + * of using the Program. In no event will IBM be liable for any + * special, indirect or consequential damages or lost profits even if + * IBM has been advised of the possibility of their occurrence. IBM + * will not be liable for any third party claims against you. + */ +// Requires Java2 +package com.ibm.textlayout; + +import java.awt.Font; + +public final class FontUtils { + + static final String COPYRIGHT = + "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved"; + + public static Font getFont(java.util.Map attributes) { + + return Font.getFont(attributes); + } +} \ No newline at end of file diff --git a/icu4j/src/com/ibm/richtext/textlayout/Graphics2DConversion.java b/icu4j/src/com/ibm/richtext/textlayout/Graphics2DConversion.java new file mode 100755 index 00000000000..ab4e927b508 --- /dev/null +++ b/icu4j/src/com/ibm/richtext/textlayout/Graphics2DConversion.java @@ -0,0 +1,56 @@ +/* + * @(#)$RCSfile: Graphics2DConversion.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:30:44 $ + * + * (C) Copyright IBM Corp. 1998-1999. All Rights Reserved. + * + * The program is provided "as is" without any warranty express or + * implied, including the warranty of non-infringement and the implied + * warranties of merchantibility and fitness for a particular purpose. + * IBM will not be liable for any damages suffered by you as a result + * of using the Program. In no event will IBM be liable for any + * special, indirect or consequential damages or lost profits even if + * IBM has been advised of the possibility of their occurrence. IBM + * will not be liable for any third party claims against you. + */ +// Requires Java2 +package com.ibm.textlayout; + +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Paint; + +/** + * This class allows JDK 1.1 code to use a "fake" Graphics2D + * for source-code compatibility with Java2 code. On Java2 it's + * a trivial class. The JDK 1.1 version of this class does interesting + * work. + */ +public final class Graphics2DConversion { + + static final String COPYRIGHT = + "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved"; + + public static Graphics2D getGraphics2D(Graphics g) { + return (Graphics2D) g; + } + + public static Graphics getGraphics(Graphics2D g) { + return g; + } + + /** + * Will return an instance of Paint. + */ + public static Object getColorState(Graphics2D g) { + + return g.getPaint(); + } + + /** + * State must be an instance of Paint. + */ + public static void restoreColorState(Graphics2D g, Object state) { + + g.setPaint((Paint) state); + } +} \ No newline at end of file diff --git a/icu4j/src/com/ibm/richtext/textlayout/attributes/AttributeKey.java b/icu4j/src/com/ibm/richtext/textlayout/attributes/AttributeKey.java new file mode 100755 index 00000000000..6f44db4b1c5 --- /dev/null +++ b/icu4j/src/com/ibm/richtext/textlayout/attributes/AttributeKey.java @@ -0,0 +1,165 @@ +/* + * @(#)$RCSfile: AttributeKey.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:33:07 $ + * + * (C) Copyright IBM Corp. 1998-1999. All Rights Reserved. + * + * The program is provided "as is" without any warranty express or + * implied, including the warranty of non-infringement and the implied + * warranties of merchantibility and fitness for a particular purpose. + * IBM will not be liable for any damages suffered by you as a result + * of using the Program. In no event will IBM be liable for any + * special, indirect or consequential damages or lost profits even if + * IBM has been advised of the possibility of their occurrence. IBM + * will not be liable for any third party claims against you. + */ +package com.ibm.textlayout.attributes; + +import java.io.Serializable; + +/** + * This class provides a cannonical mapping between fields in TextAttribute + * and instances of itself. It is used by AttributeMap to serialize + * and deserialize TextAttribute to preserve uniqueness of TextAttribute + * instances (ie so that TextAttribute instances remain singletons), + * and to provide compatability between 1.1 and 1.2 versions of + * TextAttribute. + *

+ * Example use - instead of doing this: + *

+ *     out.writeObject(anAttribute);
+ * 
+ * do this: + *
+ *     out.writeObject(AttributeKey.mapAttributeToKey(anAttribute));
+ * 
+ * Similarly, instead of this: + *
+ *     anAttribute = in.readObject();
+ * 
+ * do this: + *
+ *     anAttribute = AttributeKey.mapKeyToAttribute(in.readObject());
+ * 
+ *

+ * If anAttribute is not a known TextAttribute, then mapAttributeToKey + * will just return its argument. Similarly, mapKeyToAttribute will + * return its argument if the argument is not a known AttributeKey. + */ + +/*public*/ final class AttributeKey implements Serializable { + +/* + In this implementation, two parallel Vectors are + maintained. TextAttribute(i) maps to AttributeKey(i). + For compatability with existing data, this mapping must + be maintained in the future! So, when new attributes + are added, add them to the end of the list. +*/ + static final String COPYRIGHT = + "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved"; + private static final long serialVersionUID = 3772371253277107294L; + + private static Object[] fgTextAttributes; + private static Object[] fgAttributeKeys; + + static { + fgTextAttributes = new Object[] { + TextAttribute.FONT, + TextAttribute.FAMILY, + TextAttribute.WEIGHT, + TextAttribute.POSTURE, + TextAttribute.SIZE, + TextAttribute.SUPERSCRIPT, + TextAttribute.FOREGROUND, + TextAttribute.BACKGROUND, + TextAttribute.UNDERLINE, + TextAttribute.STRIKETHROUGH, + TextAttribute.CHAR_REPLACEMENT, + TextAttribute.EXTRA_LINE_SPACING, + TextAttribute.FIRST_LINE_INDENT, + TextAttribute.MIN_LINE_SPACING, + TextAttribute.LINE_FLUSH, + TextAttribute.LEADING_MARGIN, + TextAttribute.TRAILING_MARGIN, + TextAttribute.TAB_RULER, + TextAttribute.RUN_DIRECTION, + TextAttribute.BIDI_EMBEDDING, + TextAttribute.JUSTIFICATION, + }; + + final int attrCount = fgTextAttributes.length; + fgAttributeKeys = new Object[attrCount]; + + for (int i=0; i < attrCount; i += 1) { + fgAttributeKeys[i] = new AttributeKey(i); + } + } + + /** + * Return the TextAttribute corresponding to the given key. + * If key is an instance of AttributeKey it will be mapped to + * a TextAttribute. Otherwise, the key is returned. + * @param key the key to map to a TextAttribute field + * @return the TextAttribute for key if key + * is an AttributeKey; otherwise key is returned + */ + /*public*/ static Object mapKeyToAttribute(Object key) { + + try { + AttributeKey aKey = (AttributeKey) key; + if (aKey.fId < fgTextAttributes.length) { + return fgTextAttributes[aKey.fId]; + } + else { + return key; + } + } + catch(ClassCastException e) { + return key; + } + } + + /** + * If attribute is a known TextAttribute, return an AttributeKey + * for it. Otherwise the object is returned. + * @param attribute the attribute to map to an AttributeKey + * @return an AttributeKey for attribute + * if attribute is a known attribute; otherwise + * attribute is returned + */ + /*public*/ static Object mapAttributeToKey(Object attribute) { + + final int attrCount = fgTextAttributes.length; + + for (int index = 0; index < attrCount; index += 1) { + if (fgTextAttributes[index].equals(attribute)) { + return fgAttributeKeys[index]; + } + } + + return attribute; + } + + + private int fId; + + private AttributeKey(int id) { + + fId = id; + } + + public boolean equals(Object rhs) { + + try { + return ((AttributeKey)rhs).fId == fId; + } + catch(ClassCastException e) { + return false; + } + } + + public int hashCode() { + + return fId; + } +} \ No newline at end of file diff --git a/icu4j/src/com/ibm/richtext/textlayout/attributes/Map.java b/icu4j/src/com/ibm/richtext/textlayout/attributes/Map.java new file mode 100755 index 00000000000..da5b38db5bc --- /dev/null +++ b/icu4j/src/com/ibm/richtext/textlayout/attributes/Map.java @@ -0,0 +1,51 @@ +/* + * @(#)$RCSfile: Map.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:33:07 $ + * + * (C) Copyright IBM Corp. 1998-1999. All Rights Reserved. + * + * The program is provided "as is" without any warranty express or + * implied, including the warranty of non-infringement and the implied + * warranties of merchantibility and fitness for a particular purpose. + * IBM will not be liable for any damages suffered by you as a result + * of using the Program. In no event will IBM be liable for any + * special, indirect or consequential damages or lost profits even if + * IBM has been advised of the possibility of their occurrence. IBM + * will not be liable for any third party claims against you. + */ +// Requires Java2 +package com.ibm.textlayout.attributes; + +/** + * A Map is a collection of key-value pairs (or entries), where each + * key in the Map is unique. This interface is a subset of the + * JDK 1.2 Map interface. It is used by JDK 1.1-compatible code. + */ +public interface Map { + + static final String COPYRIGHT = + "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved"; + /** + * Return the number of entries in this Map. + * @return the number of entries in this Map + */ + public int size(); + + /** + * Return true if this Map has no entries. + * @return true if this Map has no entries + */ + public boolean isEmpty(); + + /** + * Return the value of the given key. + * @return the value of the given key. If the key does not have + * a value in this Map, null is returned. + */ + public Object get(Object key); + + /** + * Return true if this Map contains the given key. + * @return true if this Map contains the given key + */ + public boolean containsKey(Object key); +} \ No newline at end of file diff --git a/icu4j/src/com/ibm/richtext/textlayout/attributes/TextAttribute.java b/icu4j/src/com/ibm/richtext/textlayout/attributes/TextAttribute.java new file mode 100755 index 00000000000..6aeaf5eaaca --- /dev/null +++ b/icu4j/src/com/ibm/richtext/textlayout/attributes/TextAttribute.java @@ -0,0 +1,248 @@ +/* + * @(#)$RCSfile: TextAttribute.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:33:07 $ + * + * (C) Copyright IBM Corp. 1998-1999. All Rights Reserved. + * + * The program is provided "as is" without any warranty express or + * implied, including the warranty of non-infringement and the implied + * warranties of merchantibility and fitness for a particular purpose. + * IBM will not be liable for any damages suffered by you as a result + * of using the Program. In no event will IBM be liable for any + * special, indirect or consequential damages or lost profits even if + * IBM has been advised of the possibility of their occurrence. IBM + * will not be liable for any third party claims against you. + */ +// Requires Java2 +package com.ibm.textlayout.attributes; + +import java.util.Hashtable; +import java.text.AttributedCharacterIterator.Attribute; +import java.io.InvalidObjectException; + +/** + * This class contains a number of keys (or attributes) for specifying text styles + * in a Map. A text style is a collection of key-value pairs, where + * the key represents a certain characteristic (such as size) and + * the value tells how that characteristic is realized (ie what the + * size is). + *

+ * All of the attributes in TextAttribute specify a class for their value. + * Map does not enforce these class specifications; however, text rendering code + * will tend to fail with a ClassCastException if a key's value + * has an unexpected class. + *

+ * Some attributes are meaningful for entire paragraphs, not individual + * characters. These attributes are documented as applying to paragraphs. + * All other attributes apply to characters. + *

+ * Many of the field names and values in this class + * are taken from the JDK 1.2 + * java.awt.font.TextAttribute class. + * @see Map + */ +public class TextAttribute extends Attribute { + + private static final Hashtable instanceMap = new Hashtable(9); + + protected TextAttribute(String name) { + super(name); + if (this.getClass() == TextAttribute.class) { + instanceMap.put(name, this); + } + } + + protected Object readResolve() throws InvalidObjectException { + if (this.getClass() != TextAttribute.class) { + throw new InvalidObjectException("subclass didn't correctly implement readResolve"); + } + + TextAttribute instance = (TextAttribute) instanceMap.get(getName()); + if (instance != null) { + return instance; + } else { + throw new InvalidObjectException("unknown attribute name"); + }; + } + + public static final Object FONT = java.awt.font.TextAttribute.FONT; + + public static final Object FAMILY = java.awt.font.TextAttribute.FAMILY; + + public static final Object WEIGHT = java.awt.font.TextAttribute.WEIGHT; + public static final Float WEIGHT_BOLD = java.awt.font.TextAttribute.WEIGHT_BOLD; + + public static final Object POSTURE = java.awt.font.TextAttribute.POSTURE; + public static final Float POSTURE_OBLIQUE = java.awt.font.TextAttribute.POSTURE_OBLIQUE; + + public static final Object SIZE = java.awt.font.TextAttribute.SIZE; + + public static final Object SUPERSCRIPT = java.awt.font.TextAttribute.SUPERSCRIPT; + public static final Integer SUPERSCRIPT_SUPER = java.awt.font.TextAttribute.SUPERSCRIPT_SUPER; + public static final Integer SUPERSCRIPT_SUB = java.awt.font.TextAttribute.SUPERSCRIPT_SUB; + + /** + * Attribute key for the foreground and background color adornment. + */ + public static final Object FOREGROUND = java.awt.font.TextAttribute.FOREGROUND; + public static final Object BACKGROUND = java.awt.font.TextAttribute.BACKGROUND; + + public static final Object UNDERLINE = java.awt.font.TextAttribute.UNDERLINE; + public static final Integer UNDERLINE_ON = java.awt.font.TextAttribute.UNDERLINE_ON; + + public static final Object STRIKETHROUGH = java.awt.font.TextAttribute.STRIKETHROUGH; + public static final Boolean STRIKETHROUGH_ON = java.awt.font.TextAttribute.STRIKETHROUGH_ON; + + public static final Object OFFSET = new TextAttribute("offset"); + + public static final Object CHAR_REPLACEMENT = java.awt.font.TextAttribute.CHAR_REPLACEMENT; +// Paragraph Styles + +// values are Floats: + public static final Object EXTRA_LINE_SPACING = new TextAttribute("extra_line_spacing"); + public static final Object EXTRA_FIRST_LINE_SPACING = new TextAttribute("extra_first_line_spacing"); + + /** + * Amount beyond leading margin to indent the first line of a paragraph. + */ + public static final Object FIRST_LINE_INDENT = new TextAttribute("first_line_indent"); + + public static final Object MIN_LINE_SPACING = new TextAttribute("min_line_spacing"); + public static final Object MIN_FIRST_LINE_SPACING = new TextAttribute("min_first_line_spacing"); + + /** + * Flush lines left, right, or center. + */ + public static final Object LINE_FLUSH = new TextAttribute("line_flush"); + + public static final Integer FLUSH_LEADING = new Integer(0); + public static final Integer FLUSH_CENTER = new Integer(1); + public static final Integer FLUSH_TRAILING = new Integer(2); + /** + * Value of LINE_FLUSH for full justification. + */ + public static final Integer FULLY_JUSTIFIED = new Integer(3); + + /** + * Leading and trailing margin in paragraph. + */ + public static final Object LEADING_MARGIN = new TextAttribute("leading_margin"); + public static final Object TRAILING_MARGIN = new TextAttribute("trailing_margin"); + + /** + * Tab ruler. Values are MTabRuler instances. + */ + public static final Object TAB_RULER = new TextAttribute("tab_ruler"); + + /** + * Attribute key for the run direction of the line. + * + *

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *

Key

RUN_DIRECTION

Value

Boolean

Constants

RUN_DIRECTION_LTR = true, RUN_DIRECTION_RTL = false + *

Default

Use the default Unicode base direction from the BIDI + * algorithm.

Description

Specifies which base run direction to use when + * positioning mixed directional runs within a paragraph. If this value is + * RUN_DIRECTION_DEFAULT, TextLayout uses the default Unicode + * base direction from the BIDI algorithm.

+ *

This attribute should have the same value over the whole + * paragraph.

+ */ + public static final Object RUN_DIRECTION = java.awt.font.TextAttribute.RUN_DIRECTION; + + /** + * Left-to-right run direction. + * @see #RUN_DIRECTION + */ + public static final Boolean RUN_DIRECTION_LTR = java.awt.font.TextAttribute.RUN_DIRECTION_LTR; + + /** + * Right-to-left run direction. + * @see #RUN_DIRECTION + */ + public static final Boolean RUN_DIRECTION_RTL = java.awt.font.TextAttribute.RUN_DIRECTION_RTL; + + /** + * Attribute key for the embedding level for nested bidirectional runs. + * + *

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *

Key

BIDI_EMBEDDING

Value

Integer

Limits

Positive values 1 through 15 are embedding + * levels, negative values
through -15 are override levels + *

Default

Use standard BIDI to compute levels from formatting + * characters in the text.

Description

Specifies the bidi embedding level of the character. + * When this attribute is present anywhere in a paragraph, then the + * Unicode characters RLO, LRO, RLE, LRE, PDF are disregarded in the BIDI + * analysis of that paragraph. + * See the Unicode Standard v. 2.0, section 3-11. + *
+ */ + public static final Object BIDI_EMBEDDING = java.awt.font.TextAttribute.BIDI_EMBEDDING; + + /** + * Attribute key for the justification of a paragraph. + * + *

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *

Key

JUSTIFICATION

Value

Float

Limits

0.0 through1.0

Default

1.0

Description

Specifies which fraction of the extra space to use + * when justification is requested. For example, if the line is 50 points + * wide and the margins are 70 points apart, a value of 0.5 means that the + * line is padded to reach a width of 60 points.

+ *

This attribute should have the same value over the whole + * paragraph.

+ */ + public static final Object JUSTIFICATION = java.awt.font.TextAttribute.JUSTIFICATION; + + /** + * Justify the line to the full requested width. + * @see #JUSTIFICATION + */ + public static final Float JUSTIFICATION_FULL = java.awt.font.TextAttribute.JUSTIFICATION_FULL; + + /** + * Do not allow the line to be justified. + * @see #JUSTIFICATION + */ + public static final Float JUSTIFICATION_NONE = java.awt.font.TextAttribute.JUSTIFICATION_NONE; +} diff --git a/icu4j/src/com/ibm/textlayout/FontUtils.java b/icu4j/src/com/ibm/textlayout/FontUtils.java new file mode 100755 index 00000000000..9d09528e0ee --- /dev/null +++ b/icu4j/src/com/ibm/textlayout/FontUtils.java @@ -0,0 +1,29 @@ +/* + * @(#)$RCSfile: FontUtils.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:30:44 $ + * + * (C) Copyright IBM Corp. 1998-1999. All Rights Reserved. + * + * The program is provided "as is" without any warranty express or + * implied, including the warranty of non-infringement and the implied + * warranties of merchantibility and fitness for a particular purpose. + * IBM will not be liable for any damages suffered by you as a result + * of using the Program. In no event will IBM be liable for any + * special, indirect or consequential damages or lost profits even if + * IBM has been advised of the possibility of their occurrence. IBM + * will not be liable for any third party claims against you. + */ +// Requires Java2 +package com.ibm.textlayout; + +import java.awt.Font; + +public final class FontUtils { + + static final String COPYRIGHT = + "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved"; + + public static Font getFont(java.util.Map attributes) { + + return Font.getFont(attributes); + } +} \ No newline at end of file diff --git a/icu4j/src/com/ibm/textlayout/Graphics2DConversion.java b/icu4j/src/com/ibm/textlayout/Graphics2DConversion.java new file mode 100755 index 00000000000..ab4e927b508 --- /dev/null +++ b/icu4j/src/com/ibm/textlayout/Graphics2DConversion.java @@ -0,0 +1,56 @@ +/* + * @(#)$RCSfile: Graphics2DConversion.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:30:44 $ + * + * (C) Copyright IBM Corp. 1998-1999. All Rights Reserved. + * + * The program is provided "as is" without any warranty express or + * implied, including the warranty of non-infringement and the implied + * warranties of merchantibility and fitness for a particular purpose. + * IBM will not be liable for any damages suffered by you as a result + * of using the Program. In no event will IBM be liable for any + * special, indirect or consequential damages or lost profits even if + * IBM has been advised of the possibility of their occurrence. IBM + * will not be liable for any third party claims against you. + */ +// Requires Java2 +package com.ibm.textlayout; + +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Paint; + +/** + * This class allows JDK 1.1 code to use a "fake" Graphics2D + * for source-code compatibility with Java2 code. On Java2 it's + * a trivial class. The JDK 1.1 version of this class does interesting + * work. + */ +public final class Graphics2DConversion { + + static final String COPYRIGHT = + "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved"; + + public static Graphics2D getGraphics2D(Graphics g) { + return (Graphics2D) g; + } + + public static Graphics getGraphics(Graphics2D g) { + return g; + } + + /** + * Will return an instance of Paint. + */ + public static Object getColorState(Graphics2D g) { + + return g.getPaint(); + } + + /** + * State must be an instance of Paint. + */ + public static void restoreColorState(Graphics2D g, Object state) { + + g.setPaint((Paint) state); + } +} \ No newline at end of file diff --git a/icu4j/src/com/ibm/textlayout/attributes/AttributeKey.java b/icu4j/src/com/ibm/textlayout/attributes/AttributeKey.java new file mode 100755 index 00000000000..6f44db4b1c5 --- /dev/null +++ b/icu4j/src/com/ibm/textlayout/attributes/AttributeKey.java @@ -0,0 +1,165 @@ +/* + * @(#)$RCSfile: AttributeKey.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:33:07 $ + * + * (C) Copyright IBM Corp. 1998-1999. All Rights Reserved. + * + * The program is provided "as is" without any warranty express or + * implied, including the warranty of non-infringement and the implied + * warranties of merchantibility and fitness for a particular purpose. + * IBM will not be liable for any damages suffered by you as a result + * of using the Program. In no event will IBM be liable for any + * special, indirect or consequential damages or lost profits even if + * IBM has been advised of the possibility of their occurrence. IBM + * will not be liable for any third party claims against you. + */ +package com.ibm.textlayout.attributes; + +import java.io.Serializable; + +/** + * This class provides a cannonical mapping between fields in TextAttribute + * and instances of itself. It is used by AttributeMap to serialize + * and deserialize TextAttribute to preserve uniqueness of TextAttribute + * instances (ie so that TextAttribute instances remain singletons), + * and to provide compatability between 1.1 and 1.2 versions of + * TextAttribute. + *

+ * Example use - instead of doing this: + *

+ *     out.writeObject(anAttribute);
+ * 
+ * do this: + *
+ *     out.writeObject(AttributeKey.mapAttributeToKey(anAttribute));
+ * 
+ * Similarly, instead of this: + *
+ *     anAttribute = in.readObject();
+ * 
+ * do this: + *
+ *     anAttribute = AttributeKey.mapKeyToAttribute(in.readObject());
+ * 
+ *

+ * If anAttribute is not a known TextAttribute, then mapAttributeToKey + * will just return its argument. Similarly, mapKeyToAttribute will + * return its argument if the argument is not a known AttributeKey. + */ + +/*public*/ final class AttributeKey implements Serializable { + +/* + In this implementation, two parallel Vectors are + maintained. TextAttribute(i) maps to AttributeKey(i). + For compatability with existing data, this mapping must + be maintained in the future! So, when new attributes + are added, add them to the end of the list. +*/ + static final String COPYRIGHT = + "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved"; + private static final long serialVersionUID = 3772371253277107294L; + + private static Object[] fgTextAttributes; + private static Object[] fgAttributeKeys; + + static { + fgTextAttributes = new Object[] { + TextAttribute.FONT, + TextAttribute.FAMILY, + TextAttribute.WEIGHT, + TextAttribute.POSTURE, + TextAttribute.SIZE, + TextAttribute.SUPERSCRIPT, + TextAttribute.FOREGROUND, + TextAttribute.BACKGROUND, + TextAttribute.UNDERLINE, + TextAttribute.STRIKETHROUGH, + TextAttribute.CHAR_REPLACEMENT, + TextAttribute.EXTRA_LINE_SPACING, + TextAttribute.FIRST_LINE_INDENT, + TextAttribute.MIN_LINE_SPACING, + TextAttribute.LINE_FLUSH, + TextAttribute.LEADING_MARGIN, + TextAttribute.TRAILING_MARGIN, + TextAttribute.TAB_RULER, + TextAttribute.RUN_DIRECTION, + TextAttribute.BIDI_EMBEDDING, + TextAttribute.JUSTIFICATION, + }; + + final int attrCount = fgTextAttributes.length; + fgAttributeKeys = new Object[attrCount]; + + for (int i=0; i < attrCount; i += 1) { + fgAttributeKeys[i] = new AttributeKey(i); + } + } + + /** + * Return the TextAttribute corresponding to the given key. + * If key is an instance of AttributeKey it will be mapped to + * a TextAttribute. Otherwise, the key is returned. + * @param key the key to map to a TextAttribute field + * @return the TextAttribute for key if key + * is an AttributeKey; otherwise key is returned + */ + /*public*/ static Object mapKeyToAttribute(Object key) { + + try { + AttributeKey aKey = (AttributeKey) key; + if (aKey.fId < fgTextAttributes.length) { + return fgTextAttributes[aKey.fId]; + } + else { + return key; + } + } + catch(ClassCastException e) { + return key; + } + } + + /** + * If attribute is a known TextAttribute, return an AttributeKey + * for it. Otherwise the object is returned. + * @param attribute the attribute to map to an AttributeKey + * @return an AttributeKey for attribute + * if attribute is a known attribute; otherwise + * attribute is returned + */ + /*public*/ static Object mapAttributeToKey(Object attribute) { + + final int attrCount = fgTextAttributes.length; + + for (int index = 0; index < attrCount; index += 1) { + if (fgTextAttributes[index].equals(attribute)) { + return fgAttributeKeys[index]; + } + } + + return attribute; + } + + + private int fId; + + private AttributeKey(int id) { + + fId = id; + } + + public boolean equals(Object rhs) { + + try { + return ((AttributeKey)rhs).fId == fId; + } + catch(ClassCastException e) { + return false; + } + } + + public int hashCode() { + + return fId; + } +} \ No newline at end of file diff --git a/icu4j/src/com/ibm/textlayout/attributes/Map.java b/icu4j/src/com/ibm/textlayout/attributes/Map.java new file mode 100755 index 00000000000..da5b38db5bc --- /dev/null +++ b/icu4j/src/com/ibm/textlayout/attributes/Map.java @@ -0,0 +1,51 @@ +/* + * @(#)$RCSfile: Map.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:33:07 $ + * + * (C) Copyright IBM Corp. 1998-1999. All Rights Reserved. + * + * The program is provided "as is" without any warranty express or + * implied, including the warranty of non-infringement and the implied + * warranties of merchantibility and fitness for a particular purpose. + * IBM will not be liable for any damages suffered by you as a result + * of using the Program. In no event will IBM be liable for any + * special, indirect or consequential damages or lost profits even if + * IBM has been advised of the possibility of their occurrence. IBM + * will not be liable for any third party claims against you. + */ +// Requires Java2 +package com.ibm.textlayout.attributes; + +/** + * A Map is a collection of key-value pairs (or entries), where each + * key in the Map is unique. This interface is a subset of the + * JDK 1.2 Map interface. It is used by JDK 1.1-compatible code. + */ +public interface Map { + + static final String COPYRIGHT = + "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved"; + /** + * Return the number of entries in this Map. + * @return the number of entries in this Map + */ + public int size(); + + /** + * Return true if this Map has no entries. + * @return true if this Map has no entries + */ + public boolean isEmpty(); + + /** + * Return the value of the given key. + * @return the value of the given key. If the key does not have + * a value in this Map, null is returned. + */ + public Object get(Object key); + + /** + * Return true if this Map contains the given key. + * @return true if this Map contains the given key + */ + public boolean containsKey(Object key); +} \ No newline at end of file diff --git a/icu4j/src/com/ibm/textlayout/attributes/TextAttribute.java b/icu4j/src/com/ibm/textlayout/attributes/TextAttribute.java new file mode 100755 index 00000000000..6aeaf5eaaca --- /dev/null +++ b/icu4j/src/com/ibm/textlayout/attributes/TextAttribute.java @@ -0,0 +1,248 @@ +/* + * @(#)$RCSfile: TextAttribute.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:33:07 $ + * + * (C) Copyright IBM Corp. 1998-1999. All Rights Reserved. + * + * The program is provided "as is" without any warranty express or + * implied, including the warranty of non-infringement and the implied + * warranties of merchantibility and fitness for a particular purpose. + * IBM will not be liable for any damages suffered by you as a result + * of using the Program. In no event will IBM be liable for any + * special, indirect or consequential damages or lost profits even if + * IBM has been advised of the possibility of their occurrence. IBM + * will not be liable for any third party claims against you. + */ +// Requires Java2 +package com.ibm.textlayout.attributes; + +import java.util.Hashtable; +import java.text.AttributedCharacterIterator.Attribute; +import java.io.InvalidObjectException; + +/** + * This class contains a number of keys (or attributes) for specifying text styles + * in a Map. A text style is a collection of key-value pairs, where + * the key represents a certain characteristic (such as size) and + * the value tells how that characteristic is realized (ie what the + * size is). + *

+ * All of the attributes in TextAttribute specify a class for their value. + * Map does not enforce these class specifications; however, text rendering code + * will tend to fail with a ClassCastException if a key's value + * has an unexpected class. + *

+ * Some attributes are meaningful for entire paragraphs, not individual + * characters. These attributes are documented as applying to paragraphs. + * All other attributes apply to characters. + *

+ * Many of the field names and values in this class + * are taken from the JDK 1.2 + * java.awt.font.TextAttribute class. + * @see Map + */ +public class TextAttribute extends Attribute { + + private static final Hashtable instanceMap = new Hashtable(9); + + protected TextAttribute(String name) { + super(name); + if (this.getClass() == TextAttribute.class) { + instanceMap.put(name, this); + } + } + + protected Object readResolve() throws InvalidObjectException { + if (this.getClass() != TextAttribute.class) { + throw new InvalidObjectException("subclass didn't correctly implement readResolve"); + } + + TextAttribute instance = (TextAttribute) instanceMap.get(getName()); + if (instance != null) { + return instance; + } else { + throw new InvalidObjectException("unknown attribute name"); + }; + } + + public static final Object FONT = java.awt.font.TextAttribute.FONT; + + public static final Object FAMILY = java.awt.font.TextAttribute.FAMILY; + + public static final Object WEIGHT = java.awt.font.TextAttribute.WEIGHT; + public static final Float WEIGHT_BOLD = java.awt.font.TextAttribute.WEIGHT_BOLD; + + public static final Object POSTURE = java.awt.font.TextAttribute.POSTURE; + public static final Float POSTURE_OBLIQUE = java.awt.font.TextAttribute.POSTURE_OBLIQUE; + + public static final Object SIZE = java.awt.font.TextAttribute.SIZE; + + public static final Object SUPERSCRIPT = java.awt.font.TextAttribute.SUPERSCRIPT; + public static final Integer SUPERSCRIPT_SUPER = java.awt.font.TextAttribute.SUPERSCRIPT_SUPER; + public static final Integer SUPERSCRIPT_SUB = java.awt.font.TextAttribute.SUPERSCRIPT_SUB; + + /** + * Attribute key for the foreground and background color adornment. + */ + public static final Object FOREGROUND = java.awt.font.TextAttribute.FOREGROUND; + public static final Object BACKGROUND = java.awt.font.TextAttribute.BACKGROUND; + + public static final Object UNDERLINE = java.awt.font.TextAttribute.UNDERLINE; + public static final Integer UNDERLINE_ON = java.awt.font.TextAttribute.UNDERLINE_ON; + + public static final Object STRIKETHROUGH = java.awt.font.TextAttribute.STRIKETHROUGH; + public static final Boolean STRIKETHROUGH_ON = java.awt.font.TextAttribute.STRIKETHROUGH_ON; + + public static final Object OFFSET = new TextAttribute("offset"); + + public static final Object CHAR_REPLACEMENT = java.awt.font.TextAttribute.CHAR_REPLACEMENT; +// Paragraph Styles + +// values are Floats: + public static final Object EXTRA_LINE_SPACING = new TextAttribute("extra_line_spacing"); + public static final Object EXTRA_FIRST_LINE_SPACING = new TextAttribute("extra_first_line_spacing"); + + /** + * Amount beyond leading margin to indent the first line of a paragraph. + */ + public static final Object FIRST_LINE_INDENT = new TextAttribute("first_line_indent"); + + public static final Object MIN_LINE_SPACING = new TextAttribute("min_line_spacing"); + public static final Object MIN_FIRST_LINE_SPACING = new TextAttribute("min_first_line_spacing"); + + /** + * Flush lines left, right, or center. + */ + public static final Object LINE_FLUSH = new TextAttribute("line_flush"); + + public static final Integer FLUSH_LEADING = new Integer(0); + public static final Integer FLUSH_CENTER = new Integer(1); + public static final Integer FLUSH_TRAILING = new Integer(2); + /** + * Value of LINE_FLUSH for full justification. + */ + public static final Integer FULLY_JUSTIFIED = new Integer(3); + + /** + * Leading and trailing margin in paragraph. + */ + public static final Object LEADING_MARGIN = new TextAttribute("leading_margin"); + public static final Object TRAILING_MARGIN = new TextAttribute("trailing_margin"); + + /** + * Tab ruler. Values are MTabRuler instances. + */ + public static final Object TAB_RULER = new TextAttribute("tab_ruler"); + + /** + * Attribute key for the run direction of the line. + * + *

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *

Key

RUN_DIRECTION

Value

Boolean

Constants

RUN_DIRECTION_LTR = true, RUN_DIRECTION_RTL = false + *

Default

Use the default Unicode base direction from the BIDI + * algorithm.

Description

Specifies which base run direction to use when + * positioning mixed directional runs within a paragraph. If this value is + * RUN_DIRECTION_DEFAULT, TextLayout uses the default Unicode + * base direction from the BIDI algorithm.

+ *

This attribute should have the same value over the whole + * paragraph.

+ */ + public static final Object RUN_DIRECTION = java.awt.font.TextAttribute.RUN_DIRECTION; + + /** + * Left-to-right run direction. + * @see #RUN_DIRECTION + */ + public static final Boolean RUN_DIRECTION_LTR = java.awt.font.TextAttribute.RUN_DIRECTION_LTR; + + /** + * Right-to-left run direction. + * @see #RUN_DIRECTION + */ + public static final Boolean RUN_DIRECTION_RTL = java.awt.font.TextAttribute.RUN_DIRECTION_RTL; + + /** + * Attribute key for the embedding level for nested bidirectional runs. + * + *

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *

Key

BIDI_EMBEDDING

Value

Integer

Limits

Positive values 1 through 15 are embedding + * levels, negative values
through -15 are override levels + *

Default

Use standard BIDI to compute levels from formatting + * characters in the text.

Description

Specifies the bidi embedding level of the character. + * When this attribute is present anywhere in a paragraph, then the + * Unicode characters RLO, LRO, RLE, LRE, PDF are disregarded in the BIDI + * analysis of that paragraph. + * See the Unicode Standard v. 2.0, section 3-11. + *
+ */ + public static final Object BIDI_EMBEDDING = java.awt.font.TextAttribute.BIDI_EMBEDDING; + + /** + * Attribute key for the justification of a paragraph. + * + *

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *

Key

JUSTIFICATION

Value

Float

Limits

0.0 through1.0

Default

1.0

Description

Specifies which fraction of the extra space to use + * when justification is requested. For example, if the line is 50 points + * wide and the margins are 70 points apart, a value of 0.5 means that the + * line is padded to reach a width of 60 points.

+ *

This attribute should have the same value over the whole + * paragraph.

+ */ + public static final Object JUSTIFICATION = java.awt.font.TextAttribute.JUSTIFICATION; + + /** + * Justify the line to the full requested width. + * @see #JUSTIFICATION + */ + public static final Float JUSTIFICATION_FULL = java.awt.font.TextAttribute.JUSTIFICATION_FULL; + + /** + * Do not allow the line to be justified. + * @see #JUSTIFICATION + */ + public static final Float JUSTIFICATION_NONE = java.awt.font.TextAttribute.JUSTIFICATION_NONE; +}