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 package org.apache.pdfbox.pdmodel.interactive.measurement; 18 19 import org.apache.pdfbox.cos.COSBase; 20 import org.apache.pdfbox.cos.COSDictionary; 21 import org.apache.pdfbox.cos.COSName; 22 import org.apache.pdfbox.pdmodel.common.COSObjectable; 23 24 /** 25 * This class represents a number format dictionary. 26 * 27 * @version $Revision: 1.0$ 28 * 29 */ 30 public class PDNumberFormatDictionary implements COSObjectable 31 { 32 33 /** 34 * The type of the dictionary. 35 */ 36 public static final String TYPE = "NumberFormat"; 37 38 /** 39 * Constant indicating that the label specified by U is a suffix to the value. 40 */ 41 public static final String LABEL_SUFFIX_TO_VALUE = "S"; 42 /** 43 * Constant indicating that the label specified by U is a postfix to the value. 44 */ 45 public static final String LABEL_PREFIX_TO_VALUE = "P"; 46 47 /** 48 * Constant for showing a fractional value as decimal to the precision specified by the D entry. 49 */ 50 public static final String FRACTIONAL_DISPLAY_DECIMAL = "D"; 51 /** 52 * Constant for showing a fractional value as a fraction with denominator specified by the D entry. 53 */ 54 public static final String FRACTIONAL_DISPLAY_FRACTION = "F"; 55 /** 56 * Constant for showing a fractional value without fractional part; round to the nearest whole unit. 57 */ 58 public static final String FRACTIONAL_DISPLAY_ROUND = "R"; 59 /** 60 * Constant for showing a fractional value without fractional part; truncate to achieve whole units. 61 */ 62 public static final String FRACTIONAL_DISPLAY_TRUNCATE = "T"; 63 64 private COSDictionary numberFormatDictionary; 65 66 /** 67 * Constructor. 68 */ 69 public PDNumberFormatDictionary() 70 { 71 this.numberFormatDictionary = new COSDictionary(); 72 this.numberFormatDictionary.setName(COSName.TYPE, TYPE); 73 } 74 75 /** 76 * Constructor. 77 * 78 * @param dictionary the corresponding dictionary 79 */ 80 public PDNumberFormatDictionary(COSDictionary dictionary) 81 { 82 this.numberFormatDictionary = dictionary; 83 } 84 85 /** 86 * {@inheritDoc} 87 */ 88 public COSBase getCOSObject() 89 { 90 return this.numberFormatDictionary; 91 } 92 93 /** 94 * This will return the dictionary. 95 * 96 * @return the number format dictionary 97 */ 98 public COSDictionary getDictionary() 99 { 100 return this.numberFormatDictionary; 101 } 102 103 /** 104 * This will return the type of the number format dictionary. 105 * It must be "NumberFormat" 106 * 107 * @return the type 108 */ 109 public String getType() 110 { 111 return TYPE; 112 } 113 114 /** 115 * This will return the label for the units. 116 * 117 * @return the label for the units 118 */ 119 public String getUnits() 120 { 121 return this.getDictionary().getString("U"); 122 } 123 124 /** 125 * This will set the label for the units. 126 * 127 * @param units the label for the units 128 */ 129 public void setUnits(String units) 130 { 131 this.getDictionary().setString("U", units); 132 } 133 134 /** 135 * This will return the conversion factor. 136 * 137 * @return the conversion factor 138 */ 139 public float getConversionFactor() 140 { 141 return this.getDictionary().getFloat("C"); 142 } 143 144 /** 145 * This will set the conversion factor. 146 * 147 * @param conversionFactor the conversion factor 148 */ 149 public void setConversionFactor(float conversionFactor) 150 { 151 this.getDictionary().setFloat("C", conversionFactor); 152 } 153 154 /** 155 * This will return the value for the manner to display a fractional value. 156 * 157 * @return the manner to display a fractional value 158 */ 159 public String getFractionalDisplay() 160 { 161 return this.getDictionary().getString("F", FRACTIONAL_DISPLAY_DECIMAL); 162 } 163 164 /** 165 * This will set the value for the manner to display a fractional value. 166 * Allowed values are "D", "F", "R" and "T" 167 * @param fractionalDisplay the manner to display a fractional value 168 */ 169 public void setFractionalDisplay(String fractionalDisplay) 170 { 171 if ((fractionalDisplay == null) 172 || FRACTIONAL_DISPLAY_DECIMAL.equals(fractionalDisplay) 173 || FRACTIONAL_DISPLAY_FRACTION.equals(fractionalDisplay) 174 || FRACTIONAL_DISPLAY_ROUND.equals(fractionalDisplay) 175 || FRACTIONAL_DISPLAY_TRUNCATE.equals(fractionalDisplay)) 176 { 177 this.getDictionary().setString("F", fractionalDisplay); 178 } 179 else 180 { 181 throw new IllegalArgumentException("Value must be \"D\", \"F\", \"R\", or \"T\", (or null)."); 182 } 183 } 184 185 /** 186 * This will return the precision or denominator of a fractional amount. 187 * 188 * @return the precision or denominator 189 */ 190 public int getDenominator() 191 { 192 return this.getDictionary().getInt("D"); 193 } 194 195 /** 196 * This will set the precision or denominator of a fractional amount. 197 * 198 * @param denominator the precision or denominator 199 */ 200 public void setDenominator(int denominator) 201 { 202 this.getDictionary().setInt("D", denominator); 203 } 204 205 /** 206 * This will return the value indication if the denominator of the fractional value is reduced/truncated . 207 * 208 * @return fd 209 */ 210 public boolean isFD() 211 { 212 return this.getDictionary().getBoolean("FD", false); 213 } 214 215 /** 216 * This will set the value indication if the denominator of the fractional value is reduced/truncated . 217 * The denominator may not be reduced/truncated if true 218 * @param fd fd 219 */ 220 public void setFD(boolean fd) 221 { 222 this.getDictionary().setBoolean("FD", fd); 223 } 224 225 /** 226 * This will return the text to be used between orders of thousands in display of numerical values. 227 * 228 * @return thousands separator 229 */ 230 public String getThousandsSeparator() 231 { 232 return this.getDictionary().getString("RT", ","); 233 } 234 235 /** 236 * This will set the text to be used between orders of thousands in display of numerical values. 237 * 238 * @param thousandsSeparator thousands separator 239 */ 240 public void setThousandsSeparator(String thousandsSeparator) 241 { 242 this.getDictionary().setString("RT", thousandsSeparator); 243 } 244 245 /** 246 * This will return the text to be used as the decimal point in displaying numerical values. 247 * 248 * @return decimal separator 249 */ 250 public String getDecimalSeparator() 251 { 252 return this.getDictionary().getString("RD", "."); 253 } 254 255 /** 256 * This will set the text to be used as the decimal point in displaying numerical values. 257 * 258 * @param decimalSeparator decimal separator 259 */ 260 public void setDecimalSeparator(String decimalSeparator) 261 { 262 this.getDictionary().setString("RD", decimalSeparator); 263 } 264 265 /** 266 * This will return the text to be concatenated to the left of the label specified by U. 267 * @return label prefix 268 */ 269 public String getLabelPrefixString() 270 { 271 return this.getDictionary().getString("PS", " "); 272 } 273 274 /** 275 * This will set the text to be concatenated to the left of the label specified by U. 276 * @param labelPrefixString label prefix 277 */ 278 public void setLabelPrefixString(String labelPrefixString) 279 { 280 this.getDictionary().setString("PS", labelPrefixString); 281 } 282 283 /** 284 * This will return the text to be concatenated after the label specified by U. 285 * 286 * @return label suffix 287 */ 288 public String getLabelSuffixString() 289 { 290 return this.getDictionary().getString("SS", " "); 291 } 292 293 /** 294 * This will set the text to be concatenated after the label specified by U. 295 * 296 * @param labelSuffixString label suffix 297 */ 298 public void setLabelSuffixString(String labelSuffixString) 299 { 300 this.getDictionary().setString("SS", labelSuffixString); 301 } 302 303 /** 304 * This will return a value indicating the ordering of the label specified by U to the calculated unit value. 305 * 306 * @return label position 307 */ 308 public String getLabelPositionToValue() 309 { 310 return this.getDictionary().getString("O", LABEL_SUFFIX_TO_VALUE); 311 } 312 313 /** 314 * This will set the value indicating the ordering of the label specified by U to the calculated unit value. 315 * Possible values are "S" and "P" 316 * 317 * @param labelPositionToValue label position 318 */ 319 public void setLabelPositionToValue(String labelPositionToValue) 320 { 321 if ((labelPositionToValue == null) 322 || LABEL_PREFIX_TO_VALUE.equals(labelPositionToValue) 323 || LABEL_SUFFIX_TO_VALUE.equals(labelPositionToValue)) 324 { 325 this.getDictionary().setString("O", labelPositionToValue); 326 } 327 else 328 { 329 throw new IllegalArgumentException("Value must be \"S\", or \"P\" (or null)."); 330 } 331 } 332 333 }