Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

FoUnitsConverter.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.render.rtf;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import org.apache.xmlgraphics.util.UnitConv;
  22. import org.apache.fop.apps.FOPException;
  23. import org.apache.fop.fo.properties.FixedLength;
  24. /**
  25. * <p>Converts XSL-FO units to RTF units.</p>
  26. *
  27. * <p>This work was originally developed by Bertrand Delacretaz (bdelacretaz@codeconsult.ch).</p>
  28. */
  29. final class FoUnitsConverter {
  30. private static final FoUnitsConverter INSTANCE = new FoUnitsConverter();
  31. /** points to twips: 1 twip is 1/20 of a point */
  32. public static final float POINT_TO_TWIPS = 20f;
  33. /** millimeters and centimeters to twips: , one point is 1/72 of an inch, one inch is 25.4 mm */
  34. public static final float IN_TO_TWIPS = UnitConv.IN2PT * POINT_TO_TWIPS;
  35. public static final float MM_TO_TWIPS = IN_TO_TWIPS / UnitConv.IN2MM;
  36. public static final float CM_TO_TWIPS = 10 * MM_TO_TWIPS;
  37. /** conversion factors keyed by xsl:fo units names */
  38. private static final Map TWIP_FACTORS = new HashMap();
  39. static {
  40. TWIP_FACTORS.put(FixedLength.MM, MM_TO_TWIPS);
  41. TWIP_FACTORS.put(FixedLength.CM, CM_TO_TWIPS);
  42. TWIP_FACTORS.put(FixedLength.POINT, POINT_TO_TWIPS);
  43. TWIP_FACTORS.put(FixedLength.INCH, IN_TO_TWIPS);
  44. }
  45. /** singleton pattern */
  46. private FoUnitsConverter() {
  47. }
  48. /** singleton pattern */
  49. static FoUnitsConverter getInstance() {
  50. return INSTANCE;
  51. }
  52. /** convert given value to RTF units
  53. * @param foValue a value like "12mm"
  54. * TODO: tested with "mm" units only, needs work to comply with FO spec
  55. * Why does it search for period instead of simply breaking last two
  56. * Characters into another units string? - Chris
  57. */
  58. float convertToTwips(String foValue)
  59. throws FOPException {
  60. foValue = foValue.trim();
  61. // break value into number and units
  62. final StringBuffer number = new StringBuffer();
  63. final StringBuffer units = new StringBuffer();
  64. for (int i = 0; i < foValue.length(); i++) {
  65. final char c = foValue.charAt(i);
  66. if (Character.isDigit(c) || c == '.') {
  67. number.append(c);
  68. } else {
  69. // found the end of the digits
  70. units.append(foValue.substring(i).trim());
  71. break;
  72. }
  73. }
  74. return numberToTwips(number.toString(), units.toString());
  75. }
  76. /** convert given value to twips according to given units */
  77. private float numberToTwips(String number, String units)
  78. throws FOPException {
  79. float result = 0;
  80. // convert number to integer
  81. try {
  82. if (number != null && number.trim().length() > 0) {
  83. result = Float.valueOf(number);
  84. }
  85. } catch (Exception e) {
  86. throw new FOPException("number format error: cannot convert '"
  87. + number + "' to float value");
  88. }
  89. // find conversion factor
  90. if (units != null && units.trim().length() > 0) {
  91. final Float factor = (Float)TWIP_FACTORS.get(units.toLowerCase());
  92. if (factor == null) {
  93. throw new FOPException("conversion factor not found for '" + units + "' units");
  94. }
  95. result *= factor;
  96. }
  97. return result;
  98. }
  99. /** convert a font size given in points like "12pt" */
  100. int convertFontSize(String size) throws FOPException {
  101. size = size.trim();
  102. final String sFONTSUFFIX = FixedLength.POINT;
  103. if (!size.endsWith(sFONTSUFFIX)) {
  104. throw new FOPException("Invalid font size '" + size + "', must end with '"
  105. + sFONTSUFFIX + "'");
  106. }
  107. float result = 0;
  108. size = size.substring(0, size.length() - sFONTSUFFIX.length());
  109. try {
  110. result = (Float.valueOf(size));
  111. } catch (Exception e) {
  112. throw new FOPException("Invalid font size value '" + size + "'");
  113. }
  114. // RTF font size units are in half-points
  115. return (int)(result * 2.0);
  116. }
  117. public float convertMptToTwips(int width) {
  118. return width * POINT_TO_TWIPS / 1000;
  119. }
  120. }