[8084] | 1 | /*
|
---|
| 2 | * SVG Salamander
|
---|
| 3 | * Copyright (c) 2004, Mark McKay
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or
|
---|
| 7 | * without modification, are permitted provided that the following
|
---|
| 8 | * conditions are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above
|
---|
| 11 | * copyright notice, this list of conditions and the following
|
---|
| 12 | * disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above
|
---|
| 14 | * copyright notice, this list of conditions and the following
|
---|
| 15 | * disclaimer in the documentation and/or other materials
|
---|
| 16 | * provided with the distribution.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
| 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
---|
| 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
---|
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 30 | *
|
---|
| 31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
| 32 | * projects can be found at http://www.kitfox.com
|
---|
| 33 | *
|
---|
| 34 | * Created on February 18, 2004, 2:43 PM
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | package com.kitfox.svg.xml;
|
---|
| 38 |
|
---|
| 39 | import java.io.Serializable;
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @author Mark McKay
|
---|
| 43 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
| 44 | */
|
---|
| 45 | public class NumberWithUnits implements Serializable
|
---|
| 46 | {
|
---|
| 47 | public static final long serialVersionUID = 0;
|
---|
| 48 |
|
---|
| 49 | public static final int UT_UNITLESS = 0;
|
---|
| 50 | public static final int UT_PX = 1; //Pixels
|
---|
| 51 | public static final int UT_CM = 2; //Centimeters
|
---|
| 52 | public static final int UT_MM = 3; //Millimeters
|
---|
| 53 | public static final int UT_IN = 4; //Inches
|
---|
| 54 | public static final int UT_EM = 5; //Default font height
|
---|
| 55 | public static final int UT_EX = 6; //Height of character 'x' in default font
|
---|
| 56 | public static final int UT_PT = 7; //Points - 1/72 of an inch
|
---|
| 57 | public static final int UT_PC = 8; //Picas - 1/6 of an inch
|
---|
| 58 | public static final int UT_PERCENT = 9; //Percent - relative width
|
---|
| 59 |
|
---|
| 60 | float value = 0f;
|
---|
| 61 | int unitType = UT_UNITLESS;
|
---|
| 62 |
|
---|
| 63 | /** Creates a new instance of NumberWithUnits */
|
---|
| 64 | public NumberWithUnits()
|
---|
| 65 | {
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public NumberWithUnits(String value)
|
---|
| 69 | {
|
---|
| 70 | set(value);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public NumberWithUnits(float value, int unitType)
|
---|
| 74 | {
|
---|
| 75 | this.value = value;
|
---|
| 76 | this.unitType = unitType;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public float getValue() { return value; }
|
---|
| 80 | public int getUnits() { return unitType; }
|
---|
| 81 |
|
---|
| 82 | public void set(String value)
|
---|
| 83 | {
|
---|
| 84 | this.value = XMLParseUtil.findFloat(value);
|
---|
| 85 | unitType = UT_UNITLESS;
|
---|
| 86 |
|
---|
| 87 | if (value.indexOf("px") != -1) { unitType = UT_PX; return; }
|
---|
| 88 | if (value.indexOf("cm") != -1) { unitType = UT_CM; return; }
|
---|
| 89 | if (value.indexOf("mm") != -1) { unitType = UT_MM; return; }
|
---|
| 90 | if (value.indexOf("in") != -1) { unitType = UT_IN; return; }
|
---|
| 91 | if (value.indexOf("em") != -1) { unitType = UT_EM; return; }
|
---|
| 92 | if (value.indexOf("ex") != -1) { unitType = UT_EX; return; }
|
---|
| 93 | if (value.indexOf("pt") != -1) { unitType = UT_PT; return; }
|
---|
| 94 | if (value.indexOf("pc") != -1) { unitType = UT_PC; return; }
|
---|
| 95 | if (value.indexOf("%") != -1) { unitType = UT_PERCENT; return; }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public static String unitsAsString(int unitIdx)
|
---|
| 99 | {
|
---|
| 100 | switch (unitIdx)
|
---|
| 101 | {
|
---|
| 102 | default:
|
---|
| 103 | return "";
|
---|
| 104 | case UT_PX:
|
---|
| 105 | return "px";
|
---|
| 106 | case UT_CM:
|
---|
| 107 | return "cm";
|
---|
| 108 | case UT_MM:
|
---|
| 109 | return "mm";
|
---|
| 110 | case UT_IN:
|
---|
| 111 | return "in";
|
---|
| 112 | case UT_EM:
|
---|
| 113 | return "em";
|
---|
| 114 | case UT_EX:
|
---|
| 115 | return "ex";
|
---|
| 116 | case UT_PT:
|
---|
| 117 | return "pt";
|
---|
| 118 | case UT_PC:
|
---|
| 119 | return "pc";
|
---|
| 120 | case UT_PERCENT:
|
---|
| 121 | return "%";
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[11525] | 125 | @Override
|
---|
[8084] | 126 | public String toString()
|
---|
| 127 | {
|
---|
| 128 | return "" + value + unitsAsString(unitType);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[11525] | 131 | @Override
|
---|
[8084] | 132 | public boolean equals(Object obj)
|
---|
| 133 | {
|
---|
| 134 | if (obj == null) {
|
---|
| 135 | return false;
|
---|
| 136 | }
|
---|
| 137 | if (getClass() != obj.getClass()) {
|
---|
| 138 | return false;
|
---|
| 139 | }
|
---|
| 140 | final NumberWithUnits other = (NumberWithUnits) obj;
|
---|
| 141 | if (Float.floatToIntBits(this.value) != Float.floatToIntBits(other.value)) {
|
---|
| 142 | return false;
|
---|
| 143 | }
|
---|
| 144 | if (this.unitType != other.unitType) {
|
---|
| 145 | return false;
|
---|
| 146 | }
|
---|
| 147 | return true;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[11525] | 150 | @Override
|
---|
[8084] | 151 | public int hashCode()
|
---|
| 152 | {
|
---|
| 153 | int hash = 5;
|
---|
| 154 | hash = 37 * hash + Float.floatToIntBits(this.value);
|
---|
| 155 | hash = 37 * hash + this.unitType;
|
---|
| 156 | return hash;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | }
|
---|