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.

PageAttributesConverter.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * ============================================================================
  3. * The Apache Software License, Version 1.1
  4. * ============================================================================
  5. *
  6. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without modifica-
  9. * tion, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. *
  18. * 3. The end-user documentation included with the redistribution, if any, must
  19. * include the following acknowledgment: "This product includes software
  20. * developed by the Apache Software Foundation (http://www.apache.org/)."
  21. * Alternately, this acknowledgment may appear in the software itself, if
  22. * and wherever such third-party acknowledgments normally appear.
  23. *
  24. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  25. * endorse or promote products derived from this software without prior
  26. * written permission. For written permission, please contact
  27. * apache@apache.org.
  28. *
  29. * 5. Products derived from this software may not be called "Apache", nor may
  30. * "Apache" appear in their name, without prior written permission of the
  31. * Apache Software Foundation.
  32. *
  33. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  35. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  36. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  37. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  38. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  39. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  40. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  41. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  42. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. * ============================================================================
  44. *
  45. * This software consists of voluntary contributions made by many individuals
  46. * on behalf of the Apache Software Foundation and was originally created by
  47. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  48. * Software Foundation, please see <http://www.apache.org/>.
  49. */
  50. package org.apache.fop.render.rtf;
  51. import org.apache.avalon.framework.logger.Logger;
  52. import org.apache.avalon.framework.logger.ConsoleLogger;
  53. //FOP
  54. import org.apache.fop.apps.FOPException;
  55. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAttributes;
  56. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfPage;
  57. import org.apache.fop.fo.PropertyList;
  58. import org.apache.fop.fo.Property;
  59. /** Converts simple-page-master attributes into strings as defined in RtfPage.
  60. * @author Christopher Scott, scottc@westinghouse.com
  61. * @author Peter Herweg, pherweg@web.de
  62. */
  63. class PageAttributesConverter {
  64. private static Logger log = new ConsoleLogger();
  65. /** convert xsl:fo attributes to RTF text attributes */
  66. static RtfAttributes convertPageAttributes(PropertyList props, PropertyList defProps) {
  67. RtfAttributes attrib = null;
  68. try {
  69. Property p;
  70. if (defProps != null) {
  71. attrib = convertPageAttributes(defProps, null);
  72. } else {
  73. attrib = new RtfAttributes();
  74. }
  75. if ((p = props.get("page-width")) != null) {
  76. Float f = new Float(p.getLength().getValue() / 1000f);
  77. attrib.set(RtfPage.PAGE_WIDTH,
  78. (int)FoUnitsConverter.getInstance().convertToTwips(f.toString() + "pt"));
  79. }
  80. if ((p = props.get("page-height")) != null) {
  81. Float f = new Float(p.getLength().getValue() / 1000f);
  82. attrib.set(RtfPage.PAGE_HEIGHT,
  83. (int)FoUnitsConverter.getInstance().convertToTwips(f.toString() + "pt"));
  84. }
  85. if ((p = props.get("margin-top")) != null) {
  86. Float f = new Float(p.getLength().getValue() / 1000f);
  87. attrib.set(RtfPage.MARGIN_TOP,
  88. (int)FoUnitsConverter.getInstance().convertToTwips(f.toString() + "pt"));
  89. }
  90. if ((p = props.get("margin-bottom")) != null) {
  91. Float f = new Float(p.getLength().getValue() / 1000f);
  92. attrib.set(RtfPage.MARGIN_BOTTOM,
  93. (int)FoUnitsConverter.getInstance().convertToTwips(f.toString() + "pt"));
  94. }
  95. if ((p = props.get("margin-left")) != null) {
  96. Float f = new Float(p.getLength().getValue() / 1000f);
  97. attrib.set(RtfPage.MARGIN_LEFT,
  98. (int)FoUnitsConverter.getInstance().convertToTwips(f.toString() + "pt"));
  99. }
  100. if ((p = props.get("margin-right")) != null) {
  101. Float f = new Float(p.getLength().getValue() / 1000f);
  102. attrib.set(RtfPage.MARGIN_RIGHT,
  103. (int)FoUnitsConverter.getInstance().convertToTwips(f.toString() + "pt"));
  104. }
  105. } catch (FOPException e) {
  106. log.error("Exception in convertPageAttributes: " + e.getMessage() + "- page attributes ignored");
  107. attrib=new RtfAttributes();
  108. }
  109. return attrib;
  110. }
  111. }