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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.area;
  19. import java.awt.Color;
  20. import java.io.Serializable;
  21. import org.apache.xmlgraphics.image.loader.ImageInfo;
  22. import org.apache.fop.fonts.FontTriplet;
  23. import org.apache.fop.traits.BorderProps;
  24. import org.apache.fop.util.ColorUtil;
  25. import static org.apache.fop.fo.Constants.EN_REPEAT;
  26. import static org.apache.fop.fo.Constants.EN_REPEATX;
  27. import static org.apache.fop.fo.Constants.EN_REPEATY;
  28. import static org.apache.fop.fo.Constants.EN_NOREPEAT;
  29. // properties should be serialized by the holder
  30. /**
  31. * Area traits used for rendering.
  32. * This class represents an area trait that specifies a value for rendering.
  33. */
  34. public final class Trait implements Serializable {
  35. private static final long serialVersionUID = 3234280285391611437L;
  36. private Trait() {
  37. }
  38. /** Id reference line, not resolved. (not sure if this is needed.) */
  39. //public static final Integer ID_LINK = new Integer(0);
  40. /**
  41. * Internal link trait.
  42. * Contains the PageViewport key and the PROD_ID of the target area
  43. */
  44. public static final Integer INTERNAL_LINK = 1;
  45. /** * External link. A URL link to an external resource. */
  46. public static final Integer EXTERNAL_LINK = 2;
  47. /** The font triplet for the current font. */
  48. public static final Integer FONT = 3;
  49. /** Font size for the current font. */
  50. public static final Integer FONT_SIZE = 4;
  51. /** The current color. */
  52. public static final Integer COLOR = 7;
  53. /** The ID of the FO that produced an area. */
  54. public static final Integer PROD_ID = 8;
  55. /** Background trait for an area. */
  56. public static final Integer BACKGROUND = 9;
  57. /** Underline trait used when rendering inline parent. */
  58. public static final Integer UNDERLINE = 10;
  59. /** Overline trait used when rendering inline parent. */
  60. public static final Integer OVERLINE = 11;
  61. /** Linethrough trait used when rendering inline parent. */
  62. public static final Integer LINETHROUGH = 12;
  63. /** Shadow offset. */
  64. //public static final Integer OFFSET = new Integer(13);
  65. /** The shadow for text. */
  66. //public static final Integer SHADOW = new Integer(14);
  67. /** The border start. */
  68. public static final Integer BORDER_START = 15;
  69. /** The border end. */
  70. public static final Integer BORDER_END = 16;
  71. /** The border before. */
  72. public static final Integer BORDER_BEFORE = 17;
  73. /** The border after. */
  74. public static final Integer BORDER_AFTER = 18;
  75. /** The padding start. */
  76. public static final Integer PADDING_START = 19;
  77. /** The padding end. */
  78. public static final Integer PADDING_END = 20;
  79. /** The padding before. */
  80. public static final Integer PADDING_BEFORE = 21;
  81. /** The padding after. */
  82. public static final Integer PADDING_AFTER = 22;
  83. /** The space start. */
  84. public static final Integer SPACE_START = 23;
  85. /** The space end. */
  86. public static final Integer SPACE_END = 24;
  87. /** break before */
  88. //public static final Integer BREAK_BEFORE = new Integer(25);
  89. /** break after */
  90. //public static final Integer BREAK_AFTER = new Integer(26);
  91. /** The start-indent trait. */
  92. public static final Integer START_INDENT = 27;
  93. /** The end-indent trait. */
  94. public static final Integer END_INDENT = 28;
  95. /** The space-before trait. */
  96. public static final Integer SPACE_BEFORE = 29;
  97. /** The space-after trait. */
  98. public static final Integer SPACE_AFTER = 30;
  99. /** The is-reference-area trait. */
  100. public static final Integer IS_REFERENCE_AREA = 31;
  101. /** The is-viewport-area trait. */
  102. public static final Integer IS_VIEWPORT_AREA = 32;
  103. /** Blinking trait used when rendering inline parent. */
  104. public static final Integer BLINK = 33;
  105. /** Trait for color of underline decorations when rendering inline parent. */
  106. public static final Integer UNDERLINE_COLOR = 34;
  107. /** Trait for color of overline decorations when rendering inline parent. */
  108. public static final Integer OVERLINE_COLOR = 35;
  109. /** Trait for color of linethrough decorations when rendering inline parent. */
  110. public static final Integer LINETHROUGH_COLOR = 36;
  111. /** For navigation in the document structure. */
  112. public static final Integer STRUCTURE_TREE_ELEMENT = 37;
  113. /** Maximum value used by trait keys */
  114. public static final int MAX_TRAIT_KEY = 37;
  115. private static final TraitInfo[] TRAIT_INFO = new TraitInfo[MAX_TRAIT_KEY + 1];
  116. private static class TraitInfo {
  117. private String name;
  118. private Class clazz; // Class of trait data
  119. public TraitInfo(String name, Class clazz) {
  120. this.name = name;
  121. this.clazz = clazz;
  122. }
  123. public String getName() {
  124. return this.name;
  125. }
  126. public Class getClazz() {
  127. return this.clazz;
  128. }
  129. }
  130. private static void put(Integer key, TraitInfo info) {
  131. TRAIT_INFO[key] = info;
  132. }
  133. static {
  134. // Create a hashmap mapping trait code to name for external representation
  135. //put(ID_LINK, new TraitInfo("id-link", String.class));
  136. put(STRUCTURE_TREE_ELEMENT, new TraitInfo("structure-tree-element", String.class));
  137. put(INTERNAL_LINK, new TraitInfo("internal-link", InternalLink.class));
  138. put(EXTERNAL_LINK, new TraitInfo("external-link", ExternalLink.class));
  139. put(FONT, new TraitInfo("font", FontTriplet.class));
  140. put(FONT_SIZE, new TraitInfo("font-size", Integer.class));
  141. put(COLOR, new TraitInfo("color", Color.class));
  142. put(PROD_ID, new TraitInfo("prod-id", String.class));
  143. put(BACKGROUND, new TraitInfo("background", Background.class));
  144. put(UNDERLINE, new TraitInfo("underline-score", Boolean.class));
  145. put(UNDERLINE_COLOR, new TraitInfo("underline-score-color", Color.class));
  146. put(OVERLINE, new TraitInfo("overline-score", Boolean.class));
  147. put(OVERLINE_COLOR, new TraitInfo("overline-score-color", Color.class));
  148. put(LINETHROUGH, new TraitInfo("through-score", Boolean.class));
  149. put(LINETHROUGH_COLOR, new TraitInfo("through-score-color", Color.class));
  150. put(BLINK, new TraitInfo("blink", Boolean.class));
  151. //put(OFFSET, new TraitInfo("offset", Integer.class));
  152. //put(SHADOW, new TraitInfo("shadow", Integer.class));
  153. put(BORDER_START, new TraitInfo("border-start", BorderProps.class));
  154. put(BORDER_END, new TraitInfo("border-end", BorderProps.class));
  155. put(BORDER_BEFORE, new TraitInfo("border-before", BorderProps.class));
  156. put(BORDER_AFTER, new TraitInfo("border-after", BorderProps.class));
  157. put(PADDING_START, new TraitInfo("padding-start", Integer.class));
  158. put(PADDING_END, new TraitInfo("padding-end", Integer.class));
  159. put(PADDING_BEFORE, new TraitInfo("padding-before", Integer.class));
  160. put(PADDING_AFTER, new TraitInfo("padding-after", Integer.class));
  161. put(SPACE_START, new TraitInfo("space-start", Integer.class));
  162. put(SPACE_END, new TraitInfo("space-end", Integer.class));
  163. //put(BREAK_BEFORE, new TraitInfo("break-before", Integer.class));
  164. //put(BREAK_AFTER, new TraitInfo("break-after", Integer.class));
  165. put(START_INDENT, new TraitInfo("start-indent", Integer.class));
  166. put(END_INDENT, new TraitInfo("end-indent", Integer.class));
  167. put(SPACE_BEFORE, new TraitInfo("space-before", Integer.class));
  168. put(SPACE_AFTER, new TraitInfo("space-after", Integer.class));
  169. put(IS_REFERENCE_AREA, new TraitInfo("is-reference-area", Boolean.class));
  170. put(IS_VIEWPORT_AREA, new TraitInfo("is-viewport-area", Boolean.class));
  171. }
  172. /**
  173. * Get the trait name for a trait code.
  174. *
  175. * @param traitCode the trait code to get the name for
  176. * @return the trait name
  177. */
  178. public static String getTraitName(Object traitCode) {
  179. return TRAIT_INFO[(Integer)traitCode].getName();
  180. }
  181. /**
  182. * Get the data storage class for the trait.
  183. *
  184. * @param traitCode the trait code to lookup
  185. * @return the class type for the trait
  186. */
  187. public static Class getTraitClass(Object traitCode) {
  188. return TRAIT_INFO[(Integer)traitCode].getClazz();
  189. }
  190. /**
  191. * Class for internal link traits.
  192. * Stores PageViewport key and producer ID
  193. */
  194. public static class InternalLink implements Serializable {
  195. private static final long serialVersionUID = -8993505060996723039L;
  196. /** The unique key of the PageViewport. */
  197. private String pvKey;
  198. /** The PROD_ID of the link target */
  199. private String idRef;
  200. /**
  201. * Create an InternalLink to the given PageViewport and target ID
  202. *
  203. * @param pvKey the PageViewport key
  204. * @param idRef the target ID
  205. */
  206. public InternalLink(String pvKey, String idRef) {
  207. setPVKey(pvKey);
  208. setIDRef(idRef);
  209. }
  210. /**
  211. * Create an InternalLink based on the given XML attribute value.
  212. * This is typically called when data are read from an XML area tree.
  213. *
  214. * @param attrValue attribute value to be parsed by InternalLink.parseXMLAttribute
  215. */
  216. public InternalLink(String attrValue) {
  217. String[] values = parseXMLAttribute(attrValue);
  218. setPVKey(values[0]);
  219. setIDRef(values[1]);
  220. }
  221. /**
  222. * Sets the key of the targeted PageViewport.
  223. *
  224. * @param pvKey the PageViewport key
  225. */
  226. public void setPVKey(String pvKey) {
  227. this.pvKey = pvKey;
  228. }
  229. /**
  230. * Returns the key of the targeted PageViewport.
  231. *
  232. * @return the PageViewport key
  233. */
  234. public String getPVKey() {
  235. return pvKey;
  236. }
  237. /**
  238. * Sets the target ID.
  239. *
  240. * @param idRef the target ID
  241. */
  242. public void setIDRef(String idRef) {
  243. this.idRef = idRef;
  244. }
  245. /**
  246. * Returns the target ID.
  247. *
  248. * @return the target ID
  249. */
  250. public String getIDRef() {
  251. return idRef;
  252. }
  253. /**
  254. * Returns the attribute value for this object as
  255. * used in the area tree XML.
  256. *
  257. * @return a string of the type "(thisPVKey,thisIDRef)"
  258. */
  259. public String xmlAttribute() {
  260. return makeXMLAttribute(pvKey, idRef);
  261. }
  262. /**
  263. * Returns the XML attribute value for the given PV key and ID ref.
  264. * This value is used in the area tree XML.
  265. *
  266. * @param pvKey the PageViewport key of the link target
  267. * @param idRef the ID of the link target
  268. * @return a string of the type "(thisPVKey,thisIDRef)"
  269. */
  270. public static String makeXMLAttribute(String pvKey, String idRef) {
  271. return "(" + (pvKey == null ? "" : pvKey) + ","
  272. + (idRef == null ? "" : idRef) + ")";
  273. }
  274. /**
  275. * Parses XML attribute value from the area tree into
  276. * PageViewport key + IDRef strings. If the attribute value is
  277. * formatted like "(s1,s2)", then s1 and s2 are considered to be
  278. * the PV key and the IDRef, respectively.
  279. * Otherwise, the entire string is the PV key and the IDRef is null.
  280. *
  281. * @param attrValue the atribute value (PV key and possibly IDRef)
  282. * @return a 2-String array containing the PV key and the IDRef.
  283. * Both may be null.
  284. */
  285. public static String[] parseXMLAttribute(String attrValue) {
  286. String[] result = {null, null};
  287. if (attrValue != null) {
  288. int len = attrValue.length();
  289. if (len >= 2 && attrValue.charAt(0) == '(' && attrValue.charAt(len - 1) == ')'
  290. && attrValue.indexOf(',') != -1) {
  291. String value = attrValue.substring(1, len - 1); // remove brackets
  292. int delimIndex = value.indexOf(',');
  293. result[0] = value.substring(0, delimIndex).trim(); // PV key
  294. result[1] = value.substring(delimIndex + 1, value.length()).trim(); // IDRef
  295. } else {
  296. // PV key only, e.g. from old area tree XML:
  297. result[0] = attrValue;
  298. }
  299. }
  300. return result;
  301. }
  302. /**
  303. * Return the human-friendly string for debugging.
  304. * {@inheritDoc}
  305. */
  306. @Override
  307. public String toString() {
  308. StringBuffer sb = new StringBuffer();
  309. sb.append("pvKey=").append(pvKey);
  310. sb.append(",idRef=").append(idRef);
  311. return sb.toString();
  312. }
  313. }
  314. /**
  315. * External Link trait structure
  316. */
  317. public static class ExternalLink implements Serializable {
  318. private static final long serialVersionUID = -3720707599232620946L;
  319. private String destination;
  320. private boolean newWindow;
  321. /**
  322. * Constructs an ExternalLink object with the given destination
  323. *
  324. * @param destination target of the link
  325. * @param newWindow true if the target should be opened in a new window
  326. */
  327. public ExternalLink(String destination, boolean newWindow) {
  328. this.destination = destination;
  329. this.newWindow = newWindow;
  330. }
  331. /**
  332. * Create an <code>ExternalLink</code> from a trait value/attribute value in the
  333. * area tree
  334. * @param traitValue the value to use (should match the result of {@link #toString()}
  335. * @return an <code>ExternalLink</code> instance corresponding to the given value
  336. */
  337. protected static ExternalLink makeFromTraitValue(String traitValue) {
  338. String dest = null;
  339. boolean newWindow = false;
  340. String[] values = traitValue.split(",");
  341. for (int i = 0, c = values.length; i < c; i++) {
  342. String v = values[i];
  343. if (v.startsWith("dest=")) {
  344. dest = v.substring(5);
  345. } else if (v.startsWith("newWindow=")) {
  346. newWindow = Boolean.valueOf(v.substring(10));
  347. } else {
  348. throw new IllegalArgumentException(
  349. "Malformed trait value for Trait.ExternalLink: " + traitValue);
  350. }
  351. }
  352. return new ExternalLink(dest, newWindow);
  353. }
  354. /**
  355. * Get the target/destination of the link
  356. * @return the destination of the link
  357. */
  358. public String getDestination() {
  359. return this.destination;
  360. }
  361. /**
  362. * Check if the target has to be displayed in a new window
  363. * @return <code>true</code> if the target has to be displayed in a new window
  364. */
  365. public boolean newWindow() {
  366. return this.newWindow;
  367. }
  368. /**
  369. * Return a String representation of the object.
  370. * @return a <code>String</code> of the form
  371. * "org.apache.fop.area.Trait.ExternalLink[dest=someURL,newWindow=false]"
  372. */
  373. @Override
  374. public String toString() {
  375. StringBuffer sb = new StringBuffer(64);
  376. sb.append("newWindow=").append(newWindow);
  377. sb.append(",dest=").append(this.destination);
  378. return sb.toString();
  379. }
  380. }
  381. /**
  382. * Background trait structure.
  383. * Used for storing back trait information which are related.
  384. */
  385. public static class Background implements Serializable {
  386. private static final long serialVersionUID = 8452078676273242870L;
  387. /** The background color if any. */
  388. private Color color = null;
  389. /** The background image url if any. */
  390. private String url = null;
  391. /** The background image if any. */
  392. private ImageInfo imageInfo = null;
  393. /** Background repeat enum for images. */
  394. private int repeat;
  395. /** Background horizontal offset for images. */
  396. private int horiz;
  397. /** Background vertical offset for images. */
  398. private int vertical;
  399. /**
  400. * Returns the background color.
  401. * @return background color, null if n/a
  402. */
  403. public Color getColor() {
  404. return color;
  405. }
  406. /**
  407. * Returns the horizontal offset for images.
  408. * @return the horizontal offset
  409. */
  410. public int getHoriz() {
  411. return horiz;
  412. }
  413. /**
  414. * Returns the image repetition behaviour for images.
  415. * @return the image repetition behaviour
  416. */
  417. public int getRepeat() {
  418. return repeat;
  419. }
  420. /**
  421. * Returns the URL to the background image
  422. * @return URL to the background image, null if n/a
  423. */
  424. public String getURL() {
  425. return url;
  426. }
  427. /**
  428. * Returns the ImageInfo object representing the background image
  429. * @return the background image, null if n/a
  430. */
  431. public ImageInfo getImageInfo() {
  432. return imageInfo;
  433. }
  434. /**
  435. * Returns the vertical offset for images.
  436. * @return the vertical offset
  437. */
  438. public int getVertical() {
  439. return vertical;
  440. }
  441. /**
  442. * Sets the color.
  443. * @param color The color to set
  444. */
  445. public void setColor(Color color) {
  446. this.color = color;
  447. }
  448. /**
  449. * Sets the horizontal offset.
  450. * @param horiz The horizontal offset to set
  451. */
  452. public void setHoriz(int horiz) {
  453. this.horiz = horiz;
  454. }
  455. /**
  456. * Sets the image repetition behaviour for images.
  457. * @param repeat The image repetition behaviour to set
  458. */
  459. public void setRepeat(int repeat) {
  460. this.repeat = repeat;
  461. }
  462. /**
  463. * Sets the image repetition behaviour for images.
  464. * @param repeat The image repetition behaviour to set
  465. */
  466. public void setRepeat(String repeat) {
  467. setRepeat(getConstantForRepeat(repeat));
  468. }
  469. /**
  470. * Sets the URL to the background image.
  471. * @param url The URL to set
  472. */
  473. public void setURL(String url) {
  474. this.url = url;
  475. }
  476. /**
  477. * Sets the ImageInfo of the image to use as the background image.
  478. * @param info The background image's info object
  479. */
  480. public void setImageInfo(ImageInfo info) {
  481. this.imageInfo = info;
  482. }
  483. /**
  484. * Sets the vertical offset for images.
  485. * @param vertical The vertical offset to set
  486. */
  487. public void setVertical(int vertical) {
  488. this.vertical = vertical;
  489. }
  490. private String getRepeatString() {
  491. switch (getRepeat()) {
  492. case EN_REPEAT: return "repeat";
  493. case EN_REPEATX: return "repeat-x";
  494. case EN_REPEATY: return "repeat-y";
  495. case EN_NOREPEAT: return "no-repeat";
  496. default: throw new IllegalStateException("Illegal repeat style: " + getRepeat());
  497. }
  498. }
  499. private static int getConstantForRepeat(String repeat) {
  500. if ("repeat".equalsIgnoreCase(repeat)) {
  501. return EN_REPEAT;
  502. } else if ("repeat-x".equalsIgnoreCase(repeat)) {
  503. return EN_REPEATX;
  504. } else if ("repeat-y".equalsIgnoreCase(repeat)) {
  505. return EN_REPEATY;
  506. } else if ("no-repeat".equalsIgnoreCase(repeat)) {
  507. return EN_NOREPEAT;
  508. } else {
  509. throw new IllegalStateException("Illegal repeat style: " + repeat);
  510. }
  511. }
  512. /**
  513. * Return the string for debugging.
  514. * {@inheritDoc}
  515. */
  516. @Override
  517. public String toString() {
  518. StringBuffer sb = new StringBuffer();
  519. if (color != null) {
  520. sb.append("color=").append(ColorUtil.colorToString(color));
  521. }
  522. if (url != null) {
  523. if (color != null) {
  524. sb.append(",");
  525. }
  526. sb.append("url=").append(url);
  527. sb.append(",repeat=").append(getRepeatString());
  528. sb.append(",horiz=").append(horiz);
  529. sb.append(",vertical=").append(vertical);
  530. }
  531. return sb.toString();
  532. }
  533. }
  534. }