Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FoUnitsConverter.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.render.rtf;
  52. import java.util.Map;
  53. import java.util.HashMap;
  54. //FOP
  55. import org.apache.fop.apps.FOPException;
  56. /** Converts XSL-FO units to RTF units
  57. *
  58. * @author Bertrand Delacretaz <bdelacretaz@codeconsult.ch>
  59. * @author putzi
  60. * @author Peter Herweg <pherweg@web.de>
  61. *
  62. * This class was originally developed by Bertrand Delacretaz bdelacretaz@codeconsult.ch
  63. * for the JFOR project and is now integrated into FOP.
  64. */
  65. class FoUnitsConverter {
  66. private static final FoUnitsConverter m_instance = new FoUnitsConverter();
  67. /** points to twips: 1 twip is 1/20 of a point */
  68. public static final float POINT_TO_TWIPS = 20f;
  69. /** millimeters and centimeters to twips: , one point is 1/72 of an inch, one inch is 25.4 mm */
  70. public static final float IN_TO_TWIPS = 72f * POINT_TO_TWIPS;
  71. public static final float MM_TO_TWIPS = IN_TO_TWIPS / 25.4f;
  72. public static final float CM_TO_TWIPS = 10 * MM_TO_TWIPS;
  73. /** conversion factors keyed by xsl:fo units names */
  74. private static final Map m_twipFactors = new HashMap();
  75. static {
  76. m_twipFactors.put("mm", new Float(MM_TO_TWIPS));
  77. m_twipFactors.put("cm", new Float(CM_TO_TWIPS));
  78. m_twipFactors.put("pt", new Float(POINT_TO_TWIPS));
  79. m_twipFactors.put("in", new Float(IN_TO_TWIPS));
  80. }
  81. /** singleton pattern */
  82. private FoUnitsConverter() {
  83. }
  84. /** singleton pattern */
  85. static FoUnitsConverter getInstance() {
  86. return m_instance;
  87. }
  88. /** convert given value to RTF units
  89. * @param foValue a value like "12mm"
  90. * TODO: tested with "mm" units only, needs work to comply with FO spec
  91. * Why does it search for period instead of simply breaking last two
  92. * Characters into another units string? - Chris
  93. */
  94. float convertToTwips(String foValue)
  95. throws FOPException {
  96. foValue = foValue.trim();
  97. // break value into number and units
  98. final StringBuffer number = new StringBuffer();
  99. final StringBuffer units = new StringBuffer();
  100. for (int i = 0; i < foValue.length(); i++) {
  101. final char c = foValue.charAt(i);
  102. if (Character.isDigit(c) || c == '.') {
  103. number.append(c);
  104. } else {
  105. // found the end of the digits
  106. units.append(foValue.substring(i).trim());
  107. break;
  108. }
  109. }
  110. return numberToTwips(number.toString(), units.toString());
  111. }
  112. /** convert given value to twips according to given units */
  113. private float numberToTwips(String number, String units)
  114. throws FOPException {
  115. float result = 0;
  116. // convert number to integer
  117. try {
  118. if (number != null && number.trim().length() > 0) {
  119. result = Float.valueOf(number).floatValue();
  120. }
  121. } catch (Exception e) {
  122. throw new FOPException("number format error: cannot convert '"
  123. + number + "' to float value");
  124. }
  125. // find conversion factor
  126. if (units != null && units.trim().length() > 0) {
  127. final Float factor = (Float)m_twipFactors.get(units.toLowerCase());
  128. if (factor == null) {
  129. throw new FOPException("conversion factor not found for '" + units + "' units");
  130. }
  131. result *= factor.floatValue();
  132. }
  133. return result;
  134. }
  135. /** convert a font size given in points like "12pt" */
  136. int convertFontSize(String size) throws FOPException {
  137. size = size.trim();
  138. final String FONT_SUFFIX = "pt";
  139. if (!size.endsWith(FONT_SUFFIX)) {
  140. throw new FOPException("Invalid font size '" + size + "', must end with '"
  141. + FONT_SUFFIX + "'");
  142. }
  143. float result = 0;
  144. size = size.substring(0, size.length() - FONT_SUFFIX.length());
  145. try {
  146. result = (Float.valueOf(size).floatValue());
  147. } catch (Exception e) {
  148. throw new FOPException("Invalid font size value '" + size + "'");
  149. }
  150. // RTF font size units are in half-points
  151. return (int)(result * 2.0);
  152. }
  153. }