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.

Length.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package org.apache.xml.fop.datatypes;
  2. import org.apache.xml.fop.fo.Property;
  3. /**
  4. * a length quantity in XSL
  5. */
  6. public class Length {
  7. protected int millipoints = 0;
  8. protected double fontsize = 12;
  9. /**
  10. * set the length given a particular String specifying length and units
  11. */
  12. public Length (String len) {
  13. convert(len);
  14. }
  15. /**
  16. * set the length given a particular String specifying length and units,
  17. * and the font-size (necessary for an em)
  18. */
  19. public Length (String len, int fontsize) {
  20. this.fontsize = fontsize;
  21. convert(len);
  22. }
  23. /**
  24. * set the length given a particular multiplier and a length property
  25. */
  26. public Length (double multiplier, Property property) {
  27. this.millipoints = (int)(multiplier * property.getLength().mvalue());
  28. }
  29. protected void convert(String len) {
  30. /* convert the given length to a dimensionless integer representing
  31. points. */
  32. int assumed_resolution = 1; // points/pixel
  33. int l = len.length();
  34. if (l == 0) {
  35. System.err.println("WARNING: empty length");
  36. this.millipoints = 0;
  37. } else {
  38. String unit = len.substring(l-2);
  39. double dvalue =
  40. Double.valueOf(len.substring(0,(l-2))).doubleValue();
  41. if (unit.equals("in"))
  42. dvalue = dvalue * 72;
  43. else if (unit.equals("cm"))
  44. dvalue = dvalue * 28.35;
  45. else if (unit.equals("mm"))
  46. dvalue = dvalue * 2.84;
  47. else if (unit.equals("pt"))
  48. dvalue = dvalue;
  49. else if (unit.equals("pc"))
  50. dvalue = dvalue * 12;
  51. else if (unit.equals("em"))
  52. dvalue = dvalue * fontsize;
  53. else if (unit.equals("px"))
  54. dvalue = dvalue * assumed_resolution;
  55. else {
  56. dvalue = 0;
  57. System.err.println("ERROR: unknown length units in "
  58. + len);
  59. }
  60. this.millipoints = (int) (dvalue * 1000);
  61. }
  62. }
  63. /**
  64. * return the length in 1/1000ths of a point
  65. */
  66. public int mvalue() {
  67. return millipoints;
  68. }
  69. public String toString() {
  70. String s = millipoints + "mpt";
  71. return s;
  72. }
  73. }