1 17 18 19 20 package org.apache.fop.render.rtf; 21 22 import java.util.Map ; 23 import java.util.HashMap ; 24 25 import org.apache.fop.apps.FOPException; 27 28 29 38 39 final class FoUnitsConverter { 40 private static final FoUnitsConverter INSTANCE = new FoUnitsConverter(); 41 42 43 public static final float POINT_TO_TWIPS = 20f; 44 45 46 public static final float IN_TO_TWIPS = 72f * POINT_TO_TWIPS; 47 public static final float MM_TO_TWIPS = IN_TO_TWIPS / 25.4f; 48 public static final float CM_TO_TWIPS = 10 * MM_TO_TWIPS; 49 50 51 52 private static final Map TWIP_FACTORS = new HashMap (); 53 static { 54 TWIP_FACTORS.put("mm", new Float (MM_TO_TWIPS)); 55 TWIP_FACTORS.put("cm", new Float (CM_TO_TWIPS)); 56 TWIP_FACTORS.put("pt", new Float (POINT_TO_TWIPS)); 57 TWIP_FACTORS.put("in", new Float (IN_TO_TWIPS)); 58 } 59 60 61 private FoUnitsConverter() { 62 } 63 64 65 static FoUnitsConverter getInstance() { 66 return INSTANCE; 67 } 68 69 75 float convertToTwips(String foValue) 76 throws FOPException { 77 foValue = foValue.trim(); 78 79 final StringBuffer number = new StringBuffer (); 81 final StringBuffer units = new StringBuffer (); 82 83 for (int i = 0; i < foValue.length(); i++) { 84 final char c = foValue.charAt(i); 85 if (Character.isDigit(c) || c == '.') { 86 number.append(c); 87 } else { 88 units.append(foValue.substring(i).trim()); 90 break; 91 } 92 } 93 94 return numberToTwips(number.toString(), units.toString()); 95 } 96 97 98 99 private float numberToTwips(String number, String units) 100 throws FOPException { 101 float result = 0; 102 103 try { 105 if (number != null && number.trim().length() > 0) { 106 result = Float.valueOf(number).floatValue(); 107 } 108 } catch (Exception e) { 109 throw new FOPException("number format error: cannot convert '" 110 + number + "' to float value"); 111 } 112 113 if (units != null && units.trim().length() > 0) { 115 final Float factor = (Float )TWIP_FACTORS.get(units.toLowerCase()); 116 if (factor == null) { 117 throw new FOPException("conversion factor not found for '" + units + "' units"); 118 } 119 result *= factor.floatValue(); 120 } 121 122 return result; 123 } 124 125 126 int convertFontSize(String size) throws FOPException { 127 size = size.trim(); 128 final String sFONTSUFFIX = "pt"; 129 if (!size.endsWith(sFONTSUFFIX)) { 130 throw new FOPException("Invalid font size '" + size + "', must end with '" 131 + sFONTSUFFIX + "'"); 132 } 133 134 float result = 0; 135 size = size.substring(0, size.length() - sFONTSUFFIX.length()); 136 try { 137 result = (Float.valueOf(size).floatValue()); 138 } catch (Exception e) { 139 throw new FOPException("Invalid font size value '" + size + "'"); 140 } 141 142 return (int)(result * 2.0); 144 } 145 } 146 | Popular Tags |