001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.css.engine;
020:
021: import org.apache.batik.css.engine.value.Value;
022:
023: /**
024: * This class represents objects which contains property/value mappings.
025: *
026: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
027: * @version $Id: StyleMap.java 498515 2007-01-22 03:19:02Z cam $
028: */
029: public class StyleMap {
030:
031: //
032: // The masks, still have 2 free bits: 0x0800, & 0x1000, could also
033: // go to int if needed.
034: //
035: public static final short IMPORTANT_MASK = 0x0001;
036: public static final short COMPUTED_MASK = 0x0002;
037: public static final short NULL_CASCADED_MASK = 0x0004;
038: public static final short INHERITED_MASK = 0x0008;
039:
040: public static final short LINE_HEIGHT_RELATIVE_MASK = 0x0010;
041: public static final short FONT_SIZE_RELATIVE_MASK = 0x0020;
042: public static final short COLOR_RELATIVE_MASK = 0x0040;
043: public static final short PARENT_RELATIVE_MASK = 0x0080;
044: public static final short BLOCK_WIDTH_RELATIVE_MASK = 0x0100;
045: public static final short BLOCK_HEIGHT_RELATIVE_MASK = 0x0200;
046: public static final short BOX_RELATIVE_MASK = 0x0400;
047:
048: public static final short ORIGIN_MASK = (short) 0xE000; // 3 last bits
049:
050: //
051: // The origin values.
052: //
053: public static final short USER_AGENT_ORIGIN = 0;
054: public static final short USER_ORIGIN = 0x2000; // 0010
055: public static final short NON_CSS_ORIGIN = 0x4000; // 0100
056: public static final short AUTHOR_ORIGIN = 0x6000; // 0110
057: public static final short INLINE_AUTHOR_ORIGIN = (short) 0x8000; // 1000
058: public static final short OVERRIDE_ORIGIN = (short) 0xA000; // 1010
059:
060: /**
061: * The values.
062: */
063: protected Value[] values;
064:
065: /**
066: * To store the value masks.
067: */
068: protected short[] masks;
069:
070: /**
071: * Whether the values of this map cannot be re-cascaded.
072: */
073: protected boolean fixedCascadedValues;
074:
075: /**
076: * Creates a new StyleMap.
077: */
078: public StyleMap(int size) {
079: values = new Value[size];
080: masks = new short[size];
081: }
082:
083: /**
084: * Whether this map has fixed cascaded value.
085: */
086: public boolean hasFixedCascadedValues() {
087: return fixedCascadedValues;
088: }
089:
090: /**
091: * Sets the fixedCascadedValues property.
092: */
093: public void setFixedCascadedStyle(boolean b) {
094: fixedCascadedValues = b;
095: }
096:
097: /**
098: * Returns the value at the given index, null if unspecified.
099: */
100: public Value getValue(int i) {
101: return values[i];
102: }
103:
104: /**
105: * Returns the mask of the given property value.
106: */
107: public short getMask(int i) {
108: return masks[i];
109: }
110:
111: /**
112: * Tells whether the given property value is important.
113: */
114: public boolean isImportant(int i) {
115: return (masks[i] & IMPORTANT_MASK) != 0;
116: }
117:
118: /**
119: * Tells whether the given property value is computed.
120: */
121: public boolean isComputed(int i) {
122: return (masks[i] & COMPUTED_MASK) != 0;
123: }
124:
125: /**
126: * Tells whether the given cascaded property value is null.
127: */
128: public boolean isNullCascaded(int i) {
129: return (masks[i] & NULL_CASCADED_MASK) != 0;
130: }
131:
132: /**
133: * Tells whether the given cascaded property value was
134: * inherited from it's parent or set locally.
135: */
136: public boolean isInherited(int i) {
137: return (masks[i] & INHERITED_MASK) != 0;
138: }
139:
140: /**
141: * Returns the origin value.
142: */
143: public short getOrigin(int i) {
144: return (short) (masks[i] & ORIGIN_MASK);
145: }
146:
147: /**
148: * Tells whether the given property value is relative to 'color'.
149: */
150: public boolean isColorRelative(int i) {
151: return (masks[i] & COLOR_RELATIVE_MASK) != 0;
152: }
153:
154: /**
155: * Tells whether the given property value is relative to the parent's
156: * property value.
157: */
158: public boolean isParentRelative(int i) {
159: return (masks[i] & PARENT_RELATIVE_MASK) != 0;
160: }
161:
162: /**
163: * Tells whether the given property value is relative to 'line-height'.
164: */
165: public boolean isLineHeightRelative(int i) {
166: return (masks[i] & LINE_HEIGHT_RELATIVE_MASK) != 0;
167: }
168:
169: /**
170: * Tells whether the given property value is relative to 'font-size'.
171: */
172: public boolean isFontSizeRelative(int i) {
173: return (masks[i] & FONT_SIZE_RELATIVE_MASK) != 0;
174: }
175:
176: /**
177: * Tells whether the given property value is relative to the
178: * width of the containing block.
179: */
180: public boolean isBlockWidthRelative(int i) {
181: return (masks[i] & BLOCK_WIDTH_RELATIVE_MASK) != 0;
182: }
183:
184: /**
185: * Tells whether the given property value is relative to the
186: * height of the containing block.
187: */
188: public boolean isBlockHeightRelative(int i) {
189: return (masks[i] & BLOCK_HEIGHT_RELATIVE_MASK) != 0;
190: }
191:
192: /**
193: * Puts a property value, given the property index.
194: * @param i The property index.
195: * @param v The property value.
196: */
197: public void putValue(int i, Value v) {
198: values[i] = v;
199: }
200:
201: /**
202: * Puts a property mask, given the property index.
203: * @param i The property index.
204: * @param m The property mask.
205: */
206: public void putMask(int i, short m) {
207: masks[i] = m;
208: }
209:
210: /**
211: * Sets the priority of a property value.
212: */
213: public void putImportant(int i, boolean b) {
214: if (b)
215: masks[i] |= IMPORTANT_MASK;
216: else
217: masks[i] &= ~IMPORTANT_MASK;
218: }
219:
220: /**
221: * Sets the origin of the given value.
222: */
223: public void putOrigin(int i, short val) {
224: masks[i] &= ~ORIGIN_MASK;
225: masks[i] |= (short) (val & ORIGIN_MASK);
226: }
227:
228: /**
229: * Sets the computed flag of a property value.
230: */
231: public void putComputed(int i, boolean b) {
232: if (b)
233: masks[i] |= COMPUTED_MASK;
234: else
235: masks[i] &= ~COMPUTED_MASK;
236: }
237:
238: /**
239: * Sets the null-cascaded flag of a property value.
240: */
241: public void putNullCascaded(int i, boolean b) {
242: if (b)
243: masks[i] |= NULL_CASCADED_MASK;
244: else
245: masks[i] &= ~NULL_CASCADED_MASK;
246: }
247:
248: /**
249: * Sets the inherited flag of a property value.
250: * If true this computed value was inherited from it's parent.
251: */
252: public void putInherited(int i, boolean b) {
253: if (b)
254: masks[i] |= INHERITED_MASK;
255: else
256: masks[i] &= ~INHERITED_MASK;
257: }
258:
259: /**
260: * Sets the color-relative flag of a property value.
261: */
262: public void putColorRelative(int i, boolean b) {
263: if (b)
264: masks[i] |= COLOR_RELATIVE_MASK;
265: else
266: masks[i] &= ~COLOR_RELATIVE_MASK;
267: }
268:
269: /**
270: * Sets the parent-relative flag of a property value.
271: */
272: public void putParentRelative(int i, boolean b) {
273: if (b)
274: masks[i] |= PARENT_RELATIVE_MASK;
275: else
276: masks[i] &= ~PARENT_RELATIVE_MASK;
277: }
278:
279: /**
280: * Sets the line-height-relative flag of a property value.
281: */
282: public void putLineHeightRelative(int i, boolean b) {
283: if (b)
284: masks[i] |= LINE_HEIGHT_RELATIVE_MASK;
285: else
286: masks[i] &= ~LINE_HEIGHT_RELATIVE_MASK;
287: }
288:
289: /**
290: * Sets the font-size-relative flag of a property value.
291: */
292: public void putFontSizeRelative(int i, boolean b) {
293: if (b)
294: masks[i] |= FONT_SIZE_RELATIVE_MASK;
295: else
296: masks[i] &= ~FONT_SIZE_RELATIVE_MASK;
297: }
298:
299: /**
300: * Sets the block-width-relative flag of a property value.
301: */
302: public void putBlockWidthRelative(int i, boolean b) {
303: if (b)
304: masks[i] |= BLOCK_WIDTH_RELATIVE_MASK;
305: else
306: masks[i] &= ~BLOCK_WIDTH_RELATIVE_MASK;
307: }
308:
309: /**
310: * Sets the block-height-relative flag of a property value.
311: */
312: public void putBlockHeightRelative(int i, boolean b) {
313: if (b)
314: masks[i] |= BLOCK_HEIGHT_RELATIVE_MASK;
315: else
316: masks[i] &= ~BLOCK_HEIGHT_RELATIVE_MASK;
317: }
318:
319: /**
320: * Returns a printable representation of this style map.
321: */
322: public String toString(CSSEngine eng) {
323: // Note that values.length should always be equal to
324: // eng.getNumberOfProperties() for StyleMaps that were created
325: // by that CSSEngine.
326: int nSlots = values.length;
327: StringBuffer sb = new StringBuffer(nSlots * 8);
328: for (int i = 0; i < nSlots; i++) {
329: Value v = values[i];
330: if (v == null)
331: continue;
332:
333: sb.append(eng.getPropertyName(i));
334: sb.append(": ");
335: sb.append(v);
336: if (isImportant(i))
337: sb.append(" !important");
338: sb.append(";\n");
339: }
340: return sb.toString();
341: }
342: }
|