You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Trait.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.area;
  8. import org.apache.fop.datatypes.ColorType;
  9. import org.apache.fop.traits.BorderProps;
  10. import org.apache.fop.layout.FontState;
  11. import java.io.Serializable;
  12. import java.util.Map;
  13. import java.util.HashMap;
  14. import java.util.Iterator;
  15. // properties should be serialized by the holder
  16. /**
  17. * Area traits used for rendering.
  18. * This class represents an area trait that specifies a value for rendering.
  19. */
  20. public class Trait implements Serializable {
  21. /**
  22. * Id reference line, not resolved.
  23. * not sure if this is needed.
  24. */
  25. public static final Integer ID_LINK = new Integer(0);
  26. /**
  27. * Internal link trait.
  28. * This is resolved and provides a link to an internal area.
  29. */
  30. public static final Integer INTERNAL_LINK = new Integer(1); //resolved
  31. /**
  32. * External link. A URL link to an external resource.
  33. */
  34. public static final Integer EXTERNAL_LINK = new Integer(2);
  35. /**
  36. * The font name from the font setup.
  37. */
  38. public static final Integer FONT_NAME = new Integer(3);
  39. /**
  40. * Font size for the current font.
  41. */
  42. public static final Integer FONT_SIZE = new Integer(4);
  43. /**
  44. * The current colour.
  45. */
  46. public static final Integer COLOR = new Integer(7);
  47. /**
  48. * Don't think this is necessary.
  49. */
  50. public static final Integer ID_AREA = new Integer(8);
  51. /**
  52. * Background trait for an area.
  53. */
  54. public static final Integer BACKGROUND = new Integer(9);
  55. /**
  56. * Underline trait used when rendering inline parent.
  57. */
  58. public static final Integer UNDERLINE = new Integer(10);
  59. /**
  60. * Overline trait used when rendering inline parent.
  61. */
  62. public static final Integer OVERLINE = new Integer(11);
  63. /**
  64. * Linethrough trait used when rendering inline parent.
  65. */
  66. public static final Integer LINETHROUGH = new Integer(12);
  67. /**
  68. *
  69. */
  70. public static final Integer OFFSET = new Integer(13);
  71. /**
  72. * The shadow for text.
  73. */
  74. public static final Integer SHADOW = new Integer(14);
  75. /**
  76. * The border start.
  77. */
  78. public static final Integer BORDER_START = new Integer(15);
  79. public static final Integer BORDER_END = new Integer(16);
  80. public static final Integer BORDER_BEFORE = new Integer(17);
  81. public static final Integer BORDER_AFTER = new Integer(18);
  82. public static final Integer PADDING_START = new Integer(19);
  83. public static final Integer PADDING_END = new Integer(20);
  84. public static final Integer PADDING_BEFORE = new Integer(21);
  85. public static final Integer PADDING_AFTER = new Integer(22);
  86. static HashMap s_hmTraitInfo;
  87. private static class TraitInfo {
  88. String sName;
  89. Class sClass; // Class of trait data
  90. TraitInfo(String sName, Class sClass) {
  91. this.sName = sName;
  92. this.sClass = sClass;
  93. }
  94. }
  95. static {
  96. // Create a hashmap mapping trait code to name for external representation
  97. s_hmTraitInfo = new HashMap();
  98. s_hmTraitInfo.put(ID_LINK, new TraitInfo("id-link", String.class));
  99. s_hmTraitInfo.put(INTERNAL_LINK,
  100. new TraitInfo("internal-link", PageViewport.class));
  101. s_hmTraitInfo.put(EXTERNAL_LINK,
  102. new TraitInfo("external-link", String.class));
  103. s_hmTraitInfo.put(FONT_NAME,
  104. new TraitInfo("font-family", String.class));
  105. s_hmTraitInfo.put(FONT_SIZE,
  106. new TraitInfo("font-size", Integer.class));
  107. s_hmTraitInfo.put(COLOR, new TraitInfo("color", String.class));
  108. s_hmTraitInfo.put(ID_AREA, new TraitInfo("id-area", String.class));
  109. s_hmTraitInfo.put(BACKGROUND,
  110. new TraitInfo("background", Background.class));
  111. s_hmTraitInfo.put(UNDERLINE,
  112. new TraitInfo("underline", Integer.class));
  113. s_hmTraitInfo.put(OVERLINE,
  114. new TraitInfo("overline", Integer.class));
  115. s_hmTraitInfo.put(LINETHROUGH,
  116. new TraitInfo("linethrough", Integer.class));
  117. s_hmTraitInfo.put(OFFSET, new TraitInfo("offset", Integer.class));
  118. s_hmTraitInfo.put(SHADOW, new TraitInfo("shadow", Integer.class));
  119. s_hmTraitInfo.put(BORDER_START,
  120. new TraitInfo("border-start", BorderProps.class));
  121. s_hmTraitInfo.put(BORDER_END,
  122. new TraitInfo("border-end", BorderProps.class));
  123. s_hmTraitInfo.put(BORDER_BEFORE,
  124. new TraitInfo("border-before", BorderProps.class));
  125. s_hmTraitInfo.put(BORDER_AFTER,
  126. new TraitInfo("border-after", BorderProps.class));
  127. s_hmTraitInfo.put(PADDING_START,
  128. new TraitInfo("padding-start", Integer.class));
  129. s_hmTraitInfo.put(PADDING_END,
  130. new TraitInfo("padding-end", Integer.class));
  131. s_hmTraitInfo.put(PADDING_BEFORE,
  132. new TraitInfo("padding-before", Integer.class));
  133. s_hmTraitInfo.put(PADDING_AFTER,
  134. new TraitInfo("padding-after", Integer.class));
  135. }
  136. public static String getTraitName(Object traitCode) {
  137. Object obj = s_hmTraitInfo.get(traitCode);
  138. if (obj != null) {
  139. return ((TraitInfo) obj).sName;
  140. } else {
  141. return "unknown-trait-" + traitCode.toString();
  142. }
  143. }
  144. public static Object getTraitCode(String sTraitName) {
  145. Iterator iter = s_hmTraitInfo.entrySet().iterator();
  146. while (iter.hasNext()) {
  147. Map.Entry entry = (Map.Entry) iter.next();
  148. TraitInfo ti = (TraitInfo) entry.getValue();
  149. if (ti != null && ti.sName.equals(sTraitName)) {
  150. return entry.getKey();
  151. }
  152. }
  153. return null;
  154. }
  155. private static Class getTraitClass(Object oTraitCode) {
  156. TraitInfo ti = (TraitInfo) s_hmTraitInfo.get(oTraitCode);
  157. return (ti != null ? ti.sClass : null);
  158. }
  159. public Object propType;
  160. public Object data;
  161. public Trait() {
  162. this.propType = null;
  163. this.data = null;
  164. }
  165. public Trait(Object propType, Object data) {
  166. this.propType = propType;
  167. this.data = data;
  168. }
  169. public String toString() {
  170. return data.toString();
  171. }
  172. public static Object makeTraitValue(Object oCode, String sTraitValue) {
  173. // Get the code from the name
  174. // See what type of object it is
  175. // Convert string value to an object of that type
  176. Class tclass = getTraitClass(oCode);
  177. if (tclass == null)
  178. return null;
  179. if (tclass.equals(String.class)) {
  180. return sTraitValue;
  181. }
  182. if (tclass.equals(Integer.class)) {
  183. return new Integer(sTraitValue);
  184. }
  185. // See if the class has a constructor from string or can read from a string
  186. try {
  187. Object o = tclass.newInstance();
  188. //return o.fromString(sTraitValue);
  189. } catch (IllegalAccessException e1) {
  190. System.err.println("Can't create instance of " +
  191. tclass.getName());
  192. return null;
  193. }
  194. catch (InstantiationException e2) {
  195. System.err.println("Can't create instance of " +
  196. tclass.getName());
  197. return null;
  198. }
  199. return null;
  200. }
  201. public static class Background {
  202. ColorType color;
  203. String url;
  204. int repeat;
  205. int horiz;
  206. int vertical;
  207. }
  208. }