1 18 package org.apache.batik.css.engine.value; 19 20 import org.apache.batik.css.engine.CSSContext; 21 import org.apache.batik.css.engine.CSSEngine; 22 import org.apache.batik.css.engine.CSSStylableElement; 23 import org.apache.batik.css.engine.StyleMap; 24 import org.w3c.css.sac.LexicalUnit; 25 import org.w3c.dom.DOMException ; 26 import org.w3c.dom.css.CSSPrimitiveValue; 27 import org.w3c.dom.css.CSSValue; 28 29 36 public abstract class LengthManager extends AbstractValueManager { 37 38 41 public Value createValue(LexicalUnit lu, CSSEngine engine) 42 throws DOMException { 43 switch (lu.getLexicalUnitType()) { 44 case LexicalUnit.SAC_EM: 45 return new FloatValue(CSSPrimitiveValue.CSS_EMS, 46 lu.getFloatValue()); 47 48 case LexicalUnit.SAC_EX: 49 return new FloatValue(CSSPrimitiveValue.CSS_EXS, 50 lu.getFloatValue()); 51 52 case LexicalUnit.SAC_PIXEL: 53 return new FloatValue(CSSPrimitiveValue.CSS_PX, 54 lu.getFloatValue()); 55 56 case LexicalUnit.SAC_CENTIMETER: 57 return new FloatValue(CSSPrimitiveValue.CSS_CM, 58 lu.getFloatValue()); 59 60 case LexicalUnit.SAC_MILLIMETER: 61 return new FloatValue(CSSPrimitiveValue.CSS_MM, 62 lu.getFloatValue()); 63 64 case LexicalUnit.SAC_INCH: 65 return new FloatValue(CSSPrimitiveValue.CSS_IN, 66 lu.getFloatValue()); 67 68 case LexicalUnit.SAC_POINT: 69 return new FloatValue(CSSPrimitiveValue.CSS_PT, 70 lu.getFloatValue()); 71 72 case LexicalUnit.SAC_PICA: 73 return new FloatValue(CSSPrimitiveValue.CSS_PC, 74 lu.getFloatValue()); 75 76 case LexicalUnit.SAC_INTEGER: 77 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 78 lu.getIntegerValue()); 79 80 case LexicalUnit.SAC_REAL: 81 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 82 lu.getFloatValue()); 83 84 case LexicalUnit.SAC_PERCENTAGE: 85 return new FloatValue(CSSPrimitiveValue.CSS_PERCENTAGE, 86 lu.getFloatValue()); 87 } 88 throw createInvalidLexicalUnitDOMException(lu.getLexicalUnitType()); 89 } 90 91 94 public Value createFloatValue(short type, float floatValue) 95 throws DOMException { 96 switch (type) { 97 case CSSPrimitiveValue.CSS_PERCENTAGE: 98 case CSSPrimitiveValue.CSS_EMS: 99 case CSSPrimitiveValue.CSS_EXS: 100 case CSSPrimitiveValue.CSS_PX: 101 case CSSPrimitiveValue.CSS_CM: 102 case CSSPrimitiveValue.CSS_MM: 103 case CSSPrimitiveValue.CSS_IN: 104 case CSSPrimitiveValue.CSS_PT: 105 case CSSPrimitiveValue.CSS_PC: 106 case CSSPrimitiveValue.CSS_NUMBER: 107 return new FloatValue(type, floatValue); 108 } 109 throw createInvalidFloatTypeDOMException(type); 110 } 111 112 116 public Value computeValue(CSSStylableElement elt, 117 String pseudo, 118 CSSEngine engine, 119 int idx, 120 StyleMap sm, 121 Value value) { 122 if (value.getCssValueType() != CSSValue.CSS_PRIMITIVE_VALUE) { 123 return value; 124 } 125 126 switch (value.getPrimitiveType()) { 127 case CSSPrimitiveValue.CSS_NUMBER: 128 case CSSPrimitiveValue.CSS_PX: 129 return value; 130 131 case CSSPrimitiveValue.CSS_MM: 132 CSSContext ctx = engine.getCSSContext(); 133 float v = value.getFloatValue(); 134 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 135 v / ctx.getPixelUnitToMillimeter()); 136 137 case CSSPrimitiveValue.CSS_CM: 138 ctx = engine.getCSSContext(); 139 v = value.getFloatValue(); 140 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 141 v * 10f / ctx.getPixelUnitToMillimeter()); 142 143 case CSSPrimitiveValue.CSS_IN: 144 ctx = engine.getCSSContext(); 145 v = value.getFloatValue(); 146 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 147 v * 25.4f / ctx.getPixelUnitToMillimeter()); 148 149 case CSSPrimitiveValue.CSS_PT: 150 ctx = engine.getCSSContext(); 151 v = value.getFloatValue(); 152 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 153 v * 25.4f / 154 (72f * ctx.getPixelUnitToMillimeter())); 155 156 case CSSPrimitiveValue.CSS_PC: 157 ctx = engine.getCSSContext(); 158 v = value.getFloatValue(); 159 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 160 (v * 25.4f / 161 (6f * ctx.getPixelUnitToMillimeter()))); 162 163 case CSSPrimitiveValue.CSS_EMS: 164 sm.putFontSizeRelative(idx, true); 165 166 v = value.getFloatValue(); 167 int fsidx = engine.getFontSizeIndex(); 168 float fs; 169 fs = engine.getComputedStyle(elt, pseudo, fsidx).getFloatValue(); 170 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * fs); 171 172 173 case CSSPrimitiveValue.CSS_EXS: 174 sm.putFontSizeRelative(idx, true); 175 176 v = value.getFloatValue(); 177 fsidx = engine.getFontSizeIndex(); 178 fs = engine.getComputedStyle(elt, pseudo, fsidx).getFloatValue(); 179 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * fs * 0.5f); 180 181 case CSSPrimitiveValue.CSS_PERCENTAGE: 182 ctx = engine.getCSSContext(); 183 switch (getOrientation()) { 184 case HORIZONTAL_ORIENTATION: 185 sm.putBlockWidthRelative(idx, true); 186 fs = value.getFloatValue() * ctx.getBlockWidth(elt) / 100f; 187 break; 188 case VERTICAL_ORIENTATION: 189 sm.putBlockHeightRelative(idx, true); 190 fs = value.getFloatValue() * ctx.getBlockHeight(elt) / 100f; 191 break; 192 default: sm.putBlockWidthRelative(idx, true); 194 sm.putBlockHeightRelative(idx, true); 195 double w = ctx.getBlockWidth(elt); 196 double h = ctx.getBlockHeight(elt); 197 fs = (float)(value.getFloatValue() * 198 (Math.sqrt(w * w + h * h) / Math.sqrt(2)) / 100.0); 199 } 200 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, fs); 201 } 202 return value; 203 } 204 205 protected final static int HORIZONTAL_ORIENTATION = 0; 209 protected final static int VERTICAL_ORIENTATION = 1; 210 protected final static int BOTH_ORIENTATION = 2; 211 212 216 protected abstract int getOrientation(); 217 } 218 | Popular Tags |