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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. /** Maximum value used by trait keys */
  159. public static final int MAX_TRAIT_KEY = 36;
  160. private static final TraitInfo[] TRAIT_INFO = new TraitInfo[MAX_TRAIT_KEY + 1];
  161. private static class TraitInfo {
  162. private String name;
  163. private Class clazz; // Class of trait data
  164. public TraitInfo(String name, Class clazz) {
  165. this.name = name;
  166. this.clazz = clazz;
  167. }
  168. public String getName() {
  169. return this.name;
  170. }
  171. public Class getClazz() {
  172. return this.clazz;
  173. }
  174. }
  175. private static void put(Integer key, TraitInfo info) {
  176. TRAIT_INFO[key.intValue()] = info;
  177. }
  178. static {
  179. // Create a hashmap mapping trait code to name for external representation
  180. //put(ID_LINK, new TraitInfo("id-link", String.class));
  181. put(INTERNAL_LINK, new TraitInfo("internal-link", InternalLink.class));
  182. put(EXTERNAL_LINK, new TraitInfo("external-link", ExternalLink.class));
  183. put(FONT, new TraitInfo("font", FontTriplet.class));
  184. put(FONT_SIZE, new TraitInfo("font-size", Integer.class));
  185. put(COLOR, new TraitInfo("color", Color.class));
  186. put(PROD_ID, new TraitInfo("prod-id", String.class));
  187. put(BACKGROUND, new TraitInfo("background", Background.class));
  188. put(UNDERLINE, new TraitInfo("underline-score", Boolean.class));
  189. put(UNDERLINE_COLOR, new TraitInfo("underline-score-color", Color.class));
  190. put(OVERLINE, new TraitInfo("overline-score", Boolean.class));
  191. put(OVERLINE_COLOR, new TraitInfo("overline-score-color", Color.class));
  192. put(LINETHROUGH, new TraitInfo("through-score", Boolean.class));
  193. put(LINETHROUGH_COLOR, new TraitInfo("through-score-color", Color.class));
  194. put(BLINK, new TraitInfo("blink", Boolean.class));
  195. //put(OFFSET, new TraitInfo("offset", Integer.class));
  196. //put(SHADOW, new TraitInfo("shadow", Integer.class));
  197. put(BORDER_START,
  198. new TraitInfo("border-start", BorderProps.class));
  199. put(BORDER_END,
  200. new TraitInfo("border-end", BorderProps.class));
  201. put(BORDER_BEFORE,
  202. new TraitInfo("border-before", BorderProps.class));
  203. put(BORDER_AFTER,
  204. new TraitInfo("border-after", BorderProps.class));
  205. put(PADDING_START,
  206. new TraitInfo("padding-start", Integer.class));
  207. put(PADDING_END,
  208. new TraitInfo("padding-end", Integer.class));
  209. put(PADDING_BEFORE,
  210. new TraitInfo("padding-before", Integer.class));
  211. put(PADDING_AFTER,
  212. new TraitInfo("padding-after", Integer.class));
  213. put(SPACE_START,
  214. new TraitInfo("space-start", Integer.class));
  215. put(SPACE_END,
  216. new TraitInfo("space-end", Integer.class));
  217. //put(BREAK_BEFORE,
  218. // new TraitInfo("break-before", Integer.class));
  219. //put(BREAK_AFTER,
  220. // new TraitInfo("break-after", Integer.class));
  221. put(START_INDENT,
  222. new TraitInfo("start-indent", Integer.class));
  223. put(END_INDENT,
  224. new TraitInfo("end-indent", Integer.class));
  225. put(SPACE_BEFORE,
  226. new TraitInfo("space-before", Integer.class));
  227. put(SPACE_AFTER,
  228. new TraitInfo("space-after", Integer.class));
  229. put(IS_REFERENCE_AREA,
  230. new TraitInfo("is-reference-area", Boolean.class));
  231. put(IS_VIEWPORT_AREA,
  232. new TraitInfo("is-viewport-area", Boolean.class));
  233. }
  234. /**
  235. * Get the trait name for a trait code.
  236. *
  237. * @param traitCode the trait code to get the name for
  238. * @return the trait name
  239. */
  240. public static String getTraitName(Object traitCode) {
  241. return TRAIT_INFO[((Integer)traitCode).intValue()].getName();
  242. }
  243. /**
  244. * Get the data storage class for the trait.
  245. *
  246. * @param traitCode the trait code to lookup
  247. * @return the class type for the trait
  248. */
  249. public static Class getTraitClass(Object traitCode) {
  250. return TRAIT_INFO[((Integer)traitCode).intValue()].getClazz();
  251. }
  252. /**
  253. * Class for internal link traits.
  254. * Stores PageViewport key and producer ID
  255. */
  256. public static class InternalLink implements Serializable {
  257. /** The unique key of the PageViewport. */
  258. private String pvKey;
  259. /** The PROD_ID of the link target */
  260. private String idRef;
  261. /**
  262. * Create an InternalLink to the given PageViewport and target ID
  263. *
  264. * @param pvKey the PageViewport key
  265. * @param idRef the target ID
  266. */
  267. public InternalLink(String pvKey, String idRef) {
  268. setPVKey(pvKey);
  269. setIDRef(idRef);
  270. }
  271. /**
  272. * Create an InternalLink based on the given XML attribute value.
  273. * This is typically called when data are read from an XML area tree.
  274. *
  275. * @param attrValue attribute value to be parsed by InternalLink.parseXMLAttribute
  276. */
  277. public InternalLink(String attrValue) {
  278. String[] values = parseXMLAttribute(attrValue);
  279. setPVKey(values[0]);
  280. setIDRef(values[1]);
  281. }
  282. /**
  283. * Sets the key of the targeted PageViewport.
  284. *
  285. * @param pvKey the PageViewport key
  286. */
  287. public void setPVKey(String pvKey) {
  288. this.pvKey = pvKey;
  289. }
  290. /**
  291. * Returns the key of the targeted PageViewport.
  292. *
  293. * @return the PageViewport key
  294. */
  295. public String getPVKey() {
  296. return pvKey;
  297. }
  298. /**
  299. * Sets the target ID.
  300. *
  301. * @param idRef the target ID
  302. */
  303. public void setIDRef(String idRef) {
  304. this.idRef = idRef;
  305. }
  306. /**
  307. * Returns the target ID.
  308. *
  309. * @return the target ID
  310. */
  311. public String getIDRef() {
  312. return idRef;
  313. }
  314. /**
  315. * Returns the attribute value for this object as
  316. * used in the area tree XML.
  317. *
  318. * @return a string of the type "(thisPVKey,thisIDRef)"
  319. */
  320. public String xmlAttribute() {
  321. return makeXMLAttribute(pvKey, idRef);
  322. }
  323. /**
  324. * Returns the XML attribute value for the given PV key and ID ref.
  325. * This value is used in the area tree XML.
  326. *
  327. * @param pvKey the PageViewport key of the link target
  328. * @param idRef the ID of the link target
  329. * @return a string of the type "(thisPVKey,thisIDRef)"
  330. */
  331. public static String makeXMLAttribute(String pvKey, String idRef) {
  332. return "(" + (pvKey == null ? "" : pvKey) + ","
  333. + (idRef == null ? "" : idRef) + ")";
  334. }
  335. /**
  336. * Parses XML attribute value from the area tree into
  337. * PageViewport key + IDRef strings. If the attribute value is
  338. * formatted like "(s1,s2)", then s1 and s2 are considered to be
  339. * the PV key and the IDRef, respectively.
  340. * Otherwise, the entire string is the PV key and the IDRef is null.
  341. *
  342. * @param attrValue the atribute value (PV key and possibly IDRef)
  343. * @return a 2-String array containing the PV key and the IDRef.
  344. * Both may be null.
  345. */
  346. public static String[] parseXMLAttribute(String attrValue) {
  347. String[] result = {null, null};
  348. if (attrValue != null) {
  349. int len = attrValue.length();
  350. if (len >= 2 && attrValue.charAt(0) == '(' && attrValue.charAt(len - 1) == ')'
  351. && attrValue.indexOf(',') != -1) {
  352. String value = attrValue.substring(1, len - 1); // remove brackets
  353. int delimIndex = value.indexOf(',');
  354. result[0] = value.substring(0, delimIndex).trim(); // PV key
  355. result[1] = value.substring(delimIndex + 1, value.length()).trim(); // IDRef
  356. } else {
  357. // PV key only, e.g. from old area tree XML:
  358. result[0] = attrValue;
  359. }
  360. }
  361. return result;
  362. }
  363. /**
  364. * Return the human-friendly string for debugging.
  365. * {@inheritDoc}
  366. */
  367. public String toString() {
  368. StringBuffer sb = new StringBuffer();
  369. sb.append("pvKey=").append(pvKey);
  370. sb.append(",idRef=").append(idRef);
  371. return sb.toString();
  372. }
  373. }
  374. /**
  375. * External Link trait structure
  376. */
  377. public static class ExternalLink implements Serializable {
  378. private String destination;
  379. private boolean newWindow;
  380. /**
  381. * Constructs an ExternalLink object with the given destination
  382. *
  383. * @param destination target of the link
  384. * @param newWindow true if the target should be opened in a new window
  385. */
  386. public ExternalLink(String destination, boolean newWindow) {
  387. this.destination = destination;
  388. this.newWindow = newWindow;
  389. }
  390. /**
  391. * Create an <code>ExternalLink</code> from a trait value/attribute value in the
  392. * area tree
  393. * @param traitValue the value to use (should match the result of {@link #toString()}
  394. * @return an <code>ExternalLink</code> instance corresponding to the given value
  395. */
  396. protected static ExternalLink makeFromTraitValue(String traitValue) {
  397. if (traitValue.indexOf(ExternalLink.class.getName()) == -1
  398. || traitValue.indexOf("dest=") == -1) {
  399. throw new IllegalArgumentException(
  400. "Malformed trait value for Trait.ExternalLink: " + traitValue);
  401. }
  402. int startIndex = traitValue.indexOf("dest=") + 5;
  403. int endIndex = traitValue.indexOf(',', startIndex);
  404. if (endIndex == -1) {
  405. endIndex = traitValue.indexOf(']');
  406. }
  407. String dest = traitValue.substring(startIndex, endIndex);
  408. startIndex = traitValue.indexOf("newWindow=", endIndex) + 10;
  409. endIndex = traitValue.indexOf(']', startIndex);
  410. boolean newWindow = Boolean.parseBoolean(
  411. traitValue.substring(startIndex, endIndex));
  412. return new ExternalLink(dest, newWindow);
  413. }
  414. /**
  415. * Get the target/destination of the link
  416. * @return the destination of the link
  417. */
  418. public String getDestination() {
  419. return this.destination;
  420. }
  421. /**
  422. * Check if the target has to be displayed in a new window
  423. * @return <code>true</code> if the target has to be displayed in a new window
  424. */
  425. public boolean newWindow() {
  426. return this.newWindow;
  427. }
  428. /**
  429. * Return a String representation of the object.
  430. * @return a <code>String</code> of the form
  431. * "org.apache.fop.area.Trait.ExternalLink[dest=someURL,newWindow=false]"
  432. */
  433. public String toString() {
  434. StringBuffer sb = new StringBuffer(64);
  435. sb.append(super.toString());
  436. sb.append("[dest=").append(this.destination);
  437. sb.append(",newWindow=").append(newWindow).append("]");
  438. return sb.toString();
  439. }
  440. }
  441. /**
  442. * Background trait structure.
  443. * Used for storing back trait information which are related.
  444. */
  445. public static class Background implements Serializable {
  446. /** The background color if any. */
  447. private Color color = null;
  448. /** The background image url if any. */
  449. private String url = null;
  450. /** The background image if any. */
  451. private ImageInfo imageInfo = null;
  452. /** Background repeat enum for images. */
  453. private int repeat;
  454. /** Background horizontal offset for images. */
  455. private int horiz;
  456. /** Background vertical offset for images. */
  457. private int vertical;
  458. /**
  459. * Returns the background color.
  460. * @return background color, null if n/a
  461. */
  462. public Color getColor() {
  463. return color;
  464. }
  465. /**
  466. * Returns the horizontal offset for images.
  467. * @return the horizontal offset
  468. */
  469. public int getHoriz() {
  470. return horiz;
  471. }
  472. /**
  473. * Returns the image repetition behaviour for images.
  474. * @return the image repetition behaviour
  475. */
  476. public int getRepeat() {
  477. return repeat;
  478. }
  479. /**
  480. * Returns the URL to the background image
  481. * @return URL to the background image, null if n/a
  482. */
  483. public String getURL() {
  484. return url;
  485. }
  486. /**
  487. * Returns the ImageInfo object representing the background image
  488. * @return the background image, null if n/a
  489. */
  490. public ImageInfo getImageInfo() {
  491. return imageInfo;
  492. }
  493. /**
  494. * Returns the vertical offset for images.
  495. * @return the vertical offset
  496. */
  497. public int getVertical() {
  498. return vertical;
  499. }
  500. /**
  501. * Sets the color.
  502. * @param color The color to set
  503. */
  504. public void setColor(Color color) {
  505. this.color = color;
  506. }
  507. /**
  508. * Sets the horizontal offset.
  509. * @param horiz The horizontal offset to set
  510. */
  511. public void setHoriz(int horiz) {
  512. this.horiz = horiz;
  513. }
  514. /**
  515. * Sets the image repetition behaviour for images.
  516. * @param repeat The image repetition behaviour to set
  517. */
  518. public void setRepeat(int repeat) {
  519. this.repeat = repeat;
  520. }
  521. /**
  522. * Sets the image repetition behaviour for images.
  523. * @param repeat The image repetition behaviour to set
  524. */
  525. public void setRepeat(String repeat) {
  526. setRepeat(getConstantForRepeat(repeat));
  527. }
  528. /**
  529. * Sets the URL to the background image.
  530. * @param url The URL to set
  531. */
  532. public void setURL(String url) {
  533. this.url = url;
  534. }
  535. /**
  536. * Sets the ImageInfo of the image to use as the background image.
  537. * @param info The background image's info object
  538. */
  539. public void setImageInfo(ImageInfo info) {
  540. this.imageInfo = info;
  541. }
  542. /**
  543. * Sets the vertical offset for images.
  544. * @param vertical The vertical offset to set
  545. */
  546. public void setVertical(int vertical) {
  547. this.vertical = vertical;
  548. }
  549. private String getRepeatString() {
  550. switch (getRepeat()) {
  551. case Constants.EN_REPEAT: return "repeat";
  552. case Constants.EN_REPEATX: return "repeat-x";
  553. case Constants.EN_REPEATY: return "repeat-y";
  554. case Constants.EN_NOREPEAT: return "no-repeat";
  555. default: throw new IllegalStateException("Illegal repeat style: " + getRepeat());
  556. }
  557. }
  558. private static int getConstantForRepeat(String repeat) {
  559. if ("repeat".equalsIgnoreCase(repeat)) {
  560. return Constants.EN_REPEAT;
  561. } else if ("repeat-x".equalsIgnoreCase(repeat)) {
  562. return Constants.EN_REPEATX;
  563. } else if ("repeat-y".equalsIgnoreCase(repeat)) {
  564. return Constants.EN_REPEATY;
  565. } else if ("no-repeat".equalsIgnoreCase(repeat)) {
  566. return Constants.EN_NOREPEAT;
  567. } else {
  568. throw new IllegalStateException("Illegal repeat style: " + repeat);
  569. }
  570. }
  571. /**
  572. * Return the string for debugging.
  573. * {@inheritDoc}
  574. */
  575. public String toString() {
  576. StringBuffer sb = new StringBuffer();
  577. if (color != null) {
  578. sb.append("color=").append(ColorUtil.colorToString(color));
  579. }
  580. if (url != null) {
  581. if (color != null) {
  582. sb.append(",");
  583. }
  584. sb.append("url=").append(url);
  585. sb.append(",repeat=").append(getRepeatString());
  586. sb.append(",horiz=").append(horiz);
  587. sb.append(",vertical=").append(vertical);
  588. }
  589. return sb.toString();
  590. }
  591. }
  592. }