907 lines · 33.6 KB · Java
RawDownload
/* * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * * (C) Copyright IBM Corp. 2005 - All Rights Reserved * * The original version of this source code and documentation is * copyrighted and owned by IBM. These materials are provided * under terms of a License Agreement between IBM and Sun. * This technology is protected by multiple US and International * patents. This notice and attribution to IBM may not be removed. */ package sun.font; import static sun.font.EAttribute.*;import static java.lang.Math.*; import java.awt.Font;import java.awt.Paint;import java.awt.Toolkit;import java.awt.font.GraphicAttribute;import java.awt.font.NumericShaper;import java.awt.font.TextAttribute;import java.awt.font.TransformAttribute;import java.awt.geom.AffineTransform;import java.awt.geom.NoninvertibleTransformException;import java.awt.geom.Point2D;import java.awt.im.InputMethodHighlight;import java.io.Serializable;import java.text.Annotation;import java.text.AttributedCharacterIterator.Attribute;import java.util.Map;import java.util.HashMap;import java.util.Hashtable; public final class AttributeValues implements Cloneable {    private int defined;    private int nondefault;     private String family = "Default";    private float weight = 1f;    private float width = 1f;    private float posture; // 0f    private float size = 12f;    private float tracking; // 0f    private NumericShaper numericShaping; // null    private AffineTransform transform; // null == identity    private GraphicAttribute charReplacement; // null    private Paint foreground; // null    private Paint background; // null    private float justification = 1f;    private Object imHighlight; // null    // (can be either Attribute wrapping IMH, or IMH itself    private Font font; // here for completeness, don't actually use    private byte imUnderline = -1; // same default as underline    private byte superscript; // 0    private byte underline = -1; // arrgh, value for ON is 0    private byte runDirection = -2; // BIDI.DIRECTION_DEFAULT_LEFT_TO_RIGHT    private byte bidiEmbedding; // 0    private byte kerning; // 0    private byte ligatures; // 0    private boolean strikethrough; // false    private boolean swapColors; // false     private AffineTransform baselineTransform; // derived from transform    private AffineTransform charTransform; // derived from transform     private static final AttributeValues DEFAULT = new AttributeValues();     // type-specific API    public String getFamily() { return family; }    public void setFamily(String f) { this.family = f; update(EFAMILY); }     public float getWeight() { return weight; }    public void setWeight(float f) { this.weight = f; update(EWEIGHT); }     public float getWidth() { return width; }    public void setWidth(float f) { this.width = f; update(EWIDTH); }     public float getPosture() { return posture; }    public void setPosture(float f) { this.posture = f; update(EPOSTURE); }     public float getSize() { return size; }    public void setSize(float f) { this.size = f; update(ESIZE); }     public AffineTransform getTransform() { return transform; }    public void setTransform(AffineTransform f) {        this.transform = (f == null || f.isIdentity())            ? DEFAULT.transform            : new AffineTransform(f);        updateDerivedTransforms();        update(ETRANSFORM);    }    public void setTransform(TransformAttribute f) {        this.transform = (f == null || f.isIdentity())            ? DEFAULT.transform            : f.getTransform();        updateDerivedTransforms();        update(ETRANSFORM);    }     public int getSuperscript() { return superscript; }    public void setSuperscript(int f) {      this.superscript = (byte)f; update(ESUPERSCRIPT); }     public Font getFont() { return font; }    public void setFont(Font f) { this.font = f; update(EFONT); }     public GraphicAttribute getCharReplacement() { return charReplacement; }    public void setCharReplacement(GraphicAttribute f) {      this.charReplacement = f; update(ECHAR_REPLACEMENT); }     public Paint getForeground() { return foreground; }    public void setForeground(Paint f) {      this.foreground = f; update(EFOREGROUND); }     public Paint getBackground() { return background; }    public void setBackground(Paint f) {      this.background = f; update(EBACKGROUND); }     public int getUnderline() { return underline; }    public void setUnderline(int f) {      this.underline = (byte)f; update(EUNDERLINE); }     public boolean getStrikethrough() { return strikethrough; }    public void setStrikethrough(boolean f) {      this.strikethrough = f; update(ESTRIKETHROUGH); }     public int getRunDirection() { return runDirection; }    public void setRunDirection(int f) {      this.runDirection = (byte)f; update(ERUN_DIRECTION); }     public int getBidiEmbedding() { return bidiEmbedding; }    public void setBidiEmbedding(int f) {      this.bidiEmbedding = (byte)f; update(EBIDI_EMBEDDING); }     public float getJustification() { return justification; }    public void setJustification(float f) {      this.justification = f; update(EJUSTIFICATION); }     public Object getInputMethodHighlight() { return imHighlight; }    public void setInputMethodHighlight(Annotation f) {      this.imHighlight = f; update(EINPUT_METHOD_HIGHLIGHT); }    public void setInputMethodHighlight(InputMethodHighlight f) {      this.imHighlight = f; update(EINPUT_METHOD_HIGHLIGHT); }     public int getInputMethodUnderline() { return imUnderline; }    public void setInputMethodUnderline(int f) {      this.imUnderline = (byte)f; update(EINPUT_METHOD_UNDERLINE); }     public boolean getSwapColors() { return swapColors; }    public void setSwapColors(boolean f) {      this.swapColors = f; update(ESWAP_COLORS); }     public NumericShaper getNumericShaping() { return numericShaping; }    public void setNumericShaping(NumericShaper f) {      this.numericShaping = f; update(ENUMERIC_SHAPING); }     public int getKerning() { return kerning; }    public void setKerning(int f) {      this.kerning = (byte)f; update(EKERNING); }     public float getTracking() { return tracking; }    public void setTracking(float f) {      this.tracking = (byte)f; update(ETRACKING); }     public int getLigatures() { return ligatures; }    public void setLigatures(int f) {      this.ligatures = (byte)f; update(ELIGATURES); }      public AffineTransform getBaselineTransform() { return baselineTransform; }    public AffineTransform getCharTransform() { return charTransform; }     // mask api     public static int getMask(EAttribute att) {        return att.mask;    }     public static int getMask(EAttribute ... atts) {        int mask = 0;        for (EAttribute a: atts) {            mask |= a.mask;        }        return mask;    }     public static final int MASK_ALL =        getMask(EAttribute.class.getEnumConstants());     public void unsetDefault() {        defined &= nondefault;    }     public void defineAll(int mask) {        defined |= mask;        if ((defined & EBASELINE_TRANSFORM.mask) != 0) {            throw new InternalError("can't define derived attribute");        }    }     public boolean allDefined(int mask) {        return (defined & mask) == mask;    }     public boolean anyDefined(int mask) {        return (defined & mask) != 0;    }     public boolean anyNonDefault(int mask) {        return (nondefault & mask) != 0;    }     // generic EAttribute API     public boolean isDefined(EAttribute a) {        return (defined & a.mask) != 0;    }     public boolean isNonDefault(EAttribute a) {        return (nondefault & a.mask) != 0;    }     public void setDefault(EAttribute a) {        if (a.att == null) {            throw new InternalError("can't set default derived attribute: " + a);        }        i_set(a, DEFAULT);        defined |= a.mask;        nondefault &= ~a.mask;    }     public void unset(EAttribute a) {        if (a.att == null) {            throw new InternalError("can't unset derived attribute: " + a);        }        i_set(a, DEFAULT);        defined &= ~a.mask;        nondefault &= ~a.mask;    }     public void set(EAttribute a, AttributeValues src) {        if (a.att == null) {            throw new InternalError("can't set derived attribute: " + a);        }        if (src == null || src == DEFAULT) {            setDefault(a);        } else {            if ((src.defined & a.mask) != 0) {                i_set(a, src);                update(a);            }        }    }     public void set(EAttribute a, Object o) {        if (a.att == null) {            throw new InternalError("can't set derived attribute: " + a);        }        if (o != null) {            try {                i_set(a, o);                update(a);                return;            } catch (Exception e) {            }        }        setDefault(a);    }     public Object get(EAttribute a) {        if (a.att == null) {            throw new InternalError("can't get derived attribute: " + a);        }        if ((nondefault & a.mask) != 0) {            return i_get(a);        }        return null;    }     // merging     public AttributeValues merge(Map<? extends Attribute, ?>map) {        return merge(map, MASK_ALL);    }     public AttributeValues merge(Map<? extends Attribute, ?>map,                                 int mask) {        if (map instanceof AttributeMap &&            ((AttributeMap) map).getValues() != null) {            merge(((AttributeMap)map).getValues(), mask);        } else if (map != null && !map.isEmpty()) {            for (Map.Entry<? extends Attribute, ?> e: map.entrySet()) {                try {                    EAttribute ea = EAttribute.forAttribute(e.getKey());                    if (ea!= null && (mask & ea.mask) != 0) {                        set(ea, e.getValue());                    }                } catch (ClassCastException cce) {                    // IGNORED                }            }        }        return this;    }     public AttributeValues merge(AttributeValues src) {        return merge(src, MASK_ALL);    }     public AttributeValues merge(AttributeValues src, int mask) {        int m = mask & src.defined;        for (EAttribute ea: EAttribute.atts) {            if (m == 0) {                break;            }            if ((m & ea.mask) != 0) {                m &= ~ea.mask;                i_set(ea, src);                update(ea);            }        }        return this;    }     // creation API     public static AttributeValues fromMap(Map<? extends Attribute, ?> map) {        return fromMap(map, MASK_ALL);    }     public static AttributeValues fromMap(Map<? extends Attribute, ?> map,                                          int mask) {        return new AttributeValues().merge(map, mask);    }     public Map<TextAttribute, Object> toMap(Map<TextAttribute, Object> fill) {        if (fill == null) {            fill = new HashMap<TextAttribute, Object>();        }         for (int m = defined, i = 0; m != 0; ++i) {            EAttribute ea = EAttribute.atts[i];            if ((m & ea.mask) != 0) {                m &= ~ea.mask;                fill.put(ea.att, get(ea));            }        }         return fill;    }     // key must be serializable, so use String, not Object    private static final String DEFINED_KEY =        "sun.font.attributevalues.defined_key";     public static boolean is16Hashtable(Hashtable<Object, Object> ht) {        return ht.containsKey(DEFINED_KEY);    }     public static AttributeValues    fromSerializableHashtable(Hashtable<Object, Object> ht)    {        AttributeValues result = new AttributeValues();        if (ht != null && !ht.isEmpty()) {            for (Map.Entry<Object, Object> e: ht.entrySet()) {                Object key = e.getKey();                Object val = e.getValue();                if (key.equals(DEFINED_KEY)) {                    result.defineAll(((Integer)val).intValue());                } else {                    try {                        EAttribute ea =                            EAttribute.forAttribute((Attribute)key);                        if (ea != null) {                            result.set(ea, val);                        }                    }                    catch (ClassCastException ex) {                    }                }            }        }        return result;    }     public Hashtable<Object, Object> toSerializableHashtable() {        Hashtable ht = new Hashtable();        int hashkey = defined;        for (int m = defined, i = 0; m != 0; ++i) {            EAttribute ea = EAttribute.atts[i];            if ((m & ea.mask) != 0) {                m &= ~ea.mask;                Object o = get(ea);                if (o == null) {                    // hashkey will handle it                } else if (o instanceof Serializable) { // check all...                    ht.put(ea.att, o);                } else {                    hashkey &= ~ea.mask;                }            }        }        ht.put(DEFINED_KEY, Integer.valueOf(hashkey));         return ht;    }     // boilerplate    public int hashCode() {        return defined << 8 ^ nondefault;    }     public boolean equals(Object rhs) {        try {            return equals((AttributeValues)rhs);        }        catch (ClassCastException e) {        }        return false;    }     public boolean equals(AttributeValues rhs) {        // test in order of most likely to differ and easiest to compare        // also assumes we're generally calling this only if family,        // size, weight, posture are the same         if (rhs == null) return false;        if (rhs == this) return true;         return defined == rhs.defined            && nondefault == rhs.nondefault            && underline == rhs.underline            && strikethrough == rhs.strikethrough            && superscript == rhs.superscript            && width == rhs.width            && kerning == rhs.kerning            && tracking == rhs.tracking            && ligatures == rhs.ligatures            && runDirection == rhs.runDirection            && bidiEmbedding == rhs.bidiEmbedding            && swapColors == rhs.swapColors            && equals(transform, rhs.transform)            && equals(foreground, rhs.foreground)            && equals(background, rhs.background)            && equals(numericShaping, rhs.numericShaping)            && equals(justification, rhs.justification)            && equals(charReplacement, rhs.charReplacement)            && size == rhs.size            && weight == rhs.weight            && posture == rhs.posture            && equals(family, rhs.family)            && equals(font, rhs.font)            && imUnderline == rhs.imUnderline            && equals(imHighlight, rhs.imHighlight);    }     public AttributeValues clone() {        try {            AttributeValues result = (AttributeValues)super.clone();            if (transform != null) { // AffineTransform is mutable                result.transform = new AffineTransform(transform);                result.updateDerivedTransforms();            }            // if transform is null, derived transforms are null            // so there's nothing to do            return result;        }        catch (CloneNotSupportedException e) {            // never happens            return null;        }    }     public String toString() {

Lines 1–500 of 907.