選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Trait.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. String dest = null;
  398. boolean newWindow = false;
  399. String[] values = traitValue.split(",");
  400. for (int i = 0, c = values.length; i < c; i++) {
  401. String v = values[i];
  402. if (v.startsWith("dest=")) {
  403. dest = v.substring(5);
  404. } else if (v.startsWith("newWindow=")) {
  405. newWindow = Boolean.valueOf(v.substring(10)).booleanValue();
  406. } else {
  407. throw new IllegalArgumentException(
  408. "Malformed trait value for Trait.ExternalLink: " + traitValue);
  409. }
  410. }
  411. return new ExternalLink(dest, newWindow);
  412. }
  413. /**
  414. * Get the target/destination of the link
  415. * @return the destination of the link
  416. */
  417. public String getDestination() {
  418. return this.destination;
  419. }
  420. /**
  421. * Check if the target has to be displayed in a new window
  422. * @return <code>true</code> if the target has to be displayed in a new window
  423. */
  424. public boolean newWindow() {
  425. return this.newWindow;
  426. }
  427. /**
  428. * Return a String representation of the object.
  429. * @return a <code>String</code> of the form
  430. * "org.apache.fop.area.Trait.ExternalLink[dest=someURL,newWindow=false]"
  431. */
  432. public String toString() {
  433. StringBuffer sb = new StringBuffer(64);
  434. sb.append("newWindow=").append(newWindow);
  435. sb.append(",dest=").append(this.destination);
  436. return sb.toString();
  437. }
  438. }
  439. /**
  440. * Background trait structure.
  441. * Used for storing back trait information which are related.
  442. */
  443. public static class Background implements Serializable {
  444. /** The background color if any. */
  445. private Color color = null;
  446. /** The background image url if any. */
  447. private String url = null;
  448. /** The background image if any. */
  449. private ImageInfo imageInfo = null;
  450. /** Background repeat enum for images. */
  451. private int repeat;
  452. /** Background horizontal offset for images. */
  453. private int horiz;
  454. /** Background vertical offset for images. */
  455. private int vertical;
  456. /**
  457. * Returns the background color.
  458. * @return background color, null if n/a
  459. */
  460. public Color getColor() {
  461. return color;
  462. }
  463. /**
  464. * Returns the horizontal offset for images.
  465. * @return the horizontal offset
  466. */
  467. public int getHoriz() {
  468. return horiz;
  469. }
  470. /**
  471. * Returns the image repetition behaviour for images.
  472. * @return the image repetition behaviour
  473. */
  474. public int getRepeat() {
  475. return repeat;
  476. }
  477. /**
  478. * Returns the URL to the background image
  479. * @return URL to the background image, null if n/a
  480. */
  481. public String getURL() {
  482. return url;
  483. }
  484. /**
  485. * Returns the ImageInfo object representing the background image
  486. * @return the background image, null if n/a
  487. */
  488. public ImageInfo getImageInfo() {
  489. return imageInfo;
  490. }
  491. /**
  492. * Returns the vertical offset for images.
  493. * @return the vertical offset
  494. */
  495. public int getVertical() {
  496. return vertical;
  497. }
  498. /**
  499. * Sets the color.
  500. * @param color The color to set
  501. */
  502. public void setColor(Color color) {
  503. this.color = color;
  504. }
  505. /**
  506. * Sets the horizontal offset.
  507. * @param horiz The horizontal offset to set
  508. */
  509. public void setHoriz(int horiz) {
  510. this.horiz = horiz;
  511. }
  512. /**
  513. * Sets the image repetition behaviour for images.
  514. * @param repeat The image repetition behaviour to set
  515. */
  516. public void setRepeat(int repeat) {
  517. this.repeat = repeat;
  518. }
  519. /**
  520. * Sets the image repetition behaviour for images.
  521. * @param repeat The image repetition behaviour to set
  522. */
  523. public void setRepeat(String repeat) {
  524. setRepeat(getConstantForRepeat(repeat));
  525. }
  526. /**
  527. * Sets the URL to the background image.
  528. * @param url The URL to set
  529. */
  530. public void setURL(String url) {
  531. this.url = url;
  532. }
  533. /**
  534. * Sets the ImageInfo of the image to use as the background image.
  535. * @param info The background image's info object
  536. */
  537. public void setImageInfo(ImageInfo info) {
  538. this.imageInfo = info;
  539. }
  540. /**
  541. * Sets the vertical offset for images.
  542. * @param vertical The vertical offset to set
  543. */
  544. public void setVertical(int vertical) {
  545. this.vertical = vertical;
  546. }
  547. private String getRepeatString() {
  548. switch (getRepeat()) {
  549. case Constants.EN_REPEAT: return "repeat";
  550. case Constants.EN_REPEATX: return "repeat-x";
  551. case Constants.EN_REPEATY: return "repeat-y";
  552. case Constants.EN_NOREPEAT: return "no-repeat";
  553. default: throw new IllegalStateException("Illegal repeat style: " + getRepeat());
  554. }
  555. }
  556. private static int getConstantForRepeat(String repeat) {
  557. if ("repeat".equalsIgnoreCase(repeat)) {
  558. return Constants.EN_REPEAT;
  559. } else if ("repeat-x".equalsIgnoreCase(repeat)) {
  560. return Constants.EN_REPEATX;
  561. } else if ("repeat-y".equalsIgnoreCase(repeat)) {
  562. return Constants.EN_REPEATY;
  563. } else if ("no-repeat".equalsIgnoreCase(repeat)) {
  564. return Constants.EN_NOREPEAT;
  565. } else {
  566. throw new IllegalStateException("Illegal repeat style: " + repeat);
  567. }
  568. }
  569. /**
  570. * Return the string for debugging.
  571. * {@inheritDoc}
  572. */
  573. public String toString() {
  574. StringBuffer sb = new StringBuffer();
  575. if (color != null) {
  576. sb.append("color=").append(ColorUtil.colorToString(color));
  577. }
  578. if (url != null) {
  579. if (color != null) {
  580. sb.append(",");
  581. }
  582. sb.append("url=").append(url);
  583. sb.append(",repeat=").append(getRepeatString());
  584. sb.append(",horiz=").append(horiz);
  585. sb.append(",vertical=").append(vertical);
  586. }
  587. return sb.toString();
  588. }
  589. }
  590. }