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

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