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 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. * Shadow offset.
  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. /**
  80. * The border end.
  81. */
  82. public static final Integer BORDER_END = new Integer(16);
  83. /**
  84. * The border before.
  85. */
  86. public static final Integer BORDER_BEFORE = new Integer(17);
  87. /**
  88. * The border after.
  89. */
  90. public static final Integer BORDER_AFTER = new Integer(18);
  91. /**
  92. * The padding start.
  93. */
  94. public static final Integer PADDING_START = new Integer(19);
  95. /**
  96. * The padding end.
  97. */
  98. public static final Integer PADDING_END = new Integer(20);
  99. /**
  100. * The padding before.
  101. */
  102. public static final Integer PADDING_BEFORE = new Integer(21);
  103. /**
  104. * The padding after.
  105. */
  106. public static final Integer PADDING_AFTER = new Integer(22);
  107. private static final Map shmTraitInfo = new HashMap();
  108. private static class TraitInfo {
  109. String sName;
  110. Class sClass; // Class of trait data
  111. TraitInfo(String sName, Class sClass) {
  112. this.sName = sName;
  113. this.sClass = sClass;
  114. }
  115. }
  116. static {
  117. // Create a hashmap mapping trait code to name for external representation
  118. shmTraitInfo.put(ID_LINK, new TraitInfo("id-link", String.class));
  119. shmTraitInfo.put(INTERNAL_LINK,
  120. new TraitInfo("internal-link", String.class));
  121. shmTraitInfo.put(EXTERNAL_LINK,
  122. new TraitInfo("external-link", String.class));
  123. shmTraitInfo.put(FONT_NAME,
  124. new TraitInfo("font-family", String.class));
  125. shmTraitInfo.put(FONT_SIZE,
  126. new TraitInfo("font-size", Integer.class));
  127. shmTraitInfo.put(COLOR, new TraitInfo("color", String.class));
  128. shmTraitInfo.put(ID_AREA, new TraitInfo("id-area", String.class));
  129. shmTraitInfo.put(BACKGROUND,
  130. new TraitInfo("background", Background.class));
  131. shmTraitInfo.put(UNDERLINE,
  132. new TraitInfo("underline", Boolean.class));
  133. shmTraitInfo.put(OVERLINE,
  134. new TraitInfo("overline", Boolean.class));
  135. shmTraitInfo.put(LINETHROUGH,
  136. new TraitInfo("linethrough", Boolean.class));
  137. shmTraitInfo.put(OFFSET, new TraitInfo("offset", Integer.class));
  138. shmTraitInfo.put(SHADOW, new TraitInfo("shadow", Integer.class));
  139. shmTraitInfo.put(BORDER_START,
  140. new TraitInfo("border-start", BorderProps.class));
  141. shmTraitInfo.put(BORDER_END,
  142. new TraitInfo("border-end", BorderProps.class));
  143. shmTraitInfo.put(BORDER_BEFORE,
  144. new TraitInfo("border-before", BorderProps.class));
  145. shmTraitInfo.put(BORDER_AFTER,
  146. new TraitInfo("border-after", BorderProps.class));
  147. shmTraitInfo.put(PADDING_START,
  148. new TraitInfo("padding-start", Integer.class));
  149. shmTraitInfo.put(PADDING_END,
  150. new TraitInfo("padding-end", Integer.class));
  151. shmTraitInfo.put(PADDING_BEFORE,
  152. new TraitInfo("padding-before", Integer.class));
  153. shmTraitInfo.put(PADDING_AFTER,
  154. new TraitInfo("padding-after", Integer.class));
  155. }
  156. /**
  157. * Get the trait name for a trait code.
  158. *
  159. * @param traitCode the trait code to get the name for
  160. * @return the trait name
  161. */
  162. public static String getTraitName(Object traitCode) {
  163. Object obj = shmTraitInfo.get(traitCode);
  164. if (obj != null) {
  165. return ((TraitInfo) obj).sName;
  166. } else {
  167. return "unknown-trait-" + traitCode.toString();
  168. }
  169. }
  170. /**
  171. * Get the trait code for a trait name.
  172. *
  173. * @param sTraitName the name of the trait to find
  174. * @return the trait code object
  175. */
  176. public static Object getTraitCode(String sTraitName) {
  177. Iterator iter = shmTraitInfo.entrySet().iterator();
  178. while (iter.hasNext()) {
  179. Map.Entry entry = (Map.Entry) iter.next();
  180. TraitInfo ti = (TraitInfo) entry.getValue();
  181. if (ti != null && ti.sName.equals(sTraitName)) {
  182. return entry.getKey();
  183. }
  184. }
  185. return null;
  186. }
  187. /**
  188. * Get the data storage class for the trait.
  189. *
  190. * @param oTraitCode the trait code to lookup
  191. * @return the class type for the trait
  192. */
  193. private static Class getTraitClass(Object oTraitCode) {
  194. TraitInfo ti = (TraitInfo) shmTraitInfo.get(oTraitCode);
  195. return (ti != null ? ti.sClass : null);
  196. }
  197. /**
  198. * The type of trait for an area.
  199. */
  200. public Object propType;
  201. /**
  202. * The data value of the trait.
  203. */
  204. public Object data;
  205. /**
  206. * Create a new emty trait.
  207. */
  208. public Trait() {
  209. this.propType = null;
  210. this.data = null;
  211. }
  212. /**
  213. * Create a trait with the value and type.
  214. *
  215. * @param propType the type of trait
  216. * @param data the data value
  217. */
  218. public Trait(Object propType, Object data) {
  219. this.propType = propType;
  220. this.data = data;
  221. }
  222. /**
  223. * Return the string for debugging.
  224. *
  225. * @param the string from the data value
  226. */
  227. public String toString() {
  228. return data.toString();
  229. }
  230. /**
  231. * Make a trait value.
  232. *
  233. * @param oCode
  234. */
  235. public static Object makeTraitValue(Object oCode, String sTraitValue) {
  236. // Get the code from the name
  237. // See what type of object it is
  238. // Convert string value to an object of that type
  239. Class tclass = getTraitClass(oCode);
  240. if (tclass == null)
  241. return null;
  242. if (tclass.equals(String.class)) {
  243. return sTraitValue;
  244. }
  245. if (tclass.equals(Integer.class)) {
  246. return new Integer(sTraitValue);
  247. }
  248. // See if the class has a constructor from string or can read from a string
  249. try {
  250. Object o = tclass.newInstance();
  251. //return o.fromString(sTraitValue);
  252. } catch (IllegalAccessException e1) {
  253. System.err.println("Can't create instance of " +
  254. tclass.getName());
  255. return null;
  256. }
  257. catch (InstantiationException e2) {
  258. System.err.println("Can't create instance of " +
  259. tclass.getName());
  260. return null;
  261. }
  262. return null;
  263. }
  264. /**
  265. * Background trait structure.
  266. * Used for storing back trait information which are related.
  267. */
  268. public static class Background implements Serializable {
  269. /**
  270. * The background color if any.
  271. */
  272. public ColorType color = null;
  273. /**
  274. * The background image url if any.
  275. */
  276. public String url = null;
  277. /**
  278. * Background repeat enum for images.
  279. */
  280. public int repeat;
  281. /**
  282. * Background horizontal offset for images.
  283. */
  284. public int horiz;
  285. /**
  286. * Background vertical offset for images.
  287. */
  288. public int vertical;
  289. }
  290. }