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.

PageScale.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.extensions.prepress;
  19. import java.awt.geom.Point2D;
  20. import java.text.MessageFormat;
  21. import java.util.regex.Pattern;
  22. import org.apache.xmlgraphics.util.QName;
  23. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  24. /**
  25. * This class provides utility methods to parse the 'fox:scale' extension attribute.
  26. */
  27. public final class PageScale {
  28. /**
  29. * The extension 'scale' attribute for the simple-page-master element.
  30. */
  31. public static final QName EXT_PAGE_SCALE
  32. = new QName(ExtensionElementMapping.URI, null, "scale");
  33. private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
  34. /**
  35. * Utility classes should not have a public or default constructor
  36. */
  37. private PageScale() {
  38. }
  39. /**
  40. * Compute scale parameters from given fox:scale attribute which has the format: scaleX [scaleY]
  41. * If scaleY is not defined, it equals scaleX.
  42. * @param scale scale attribute, input format: scaleX [scaleY]
  43. * @return the pair of (sx, sy) values
  44. */
  45. public static Point2D getScale(String scale) {
  46. // TODO throw appropriate exceptions that can be caught by the event
  47. // notification mechanism
  48. final String err = "Extension 'scale' attribute has incorrect value(s): {0}";
  49. if (scale == null || scale.equals("")) {
  50. return null;
  51. }
  52. String[] scales = WHITESPACE_PATTERN.split(scale);
  53. double scaleX;
  54. try {
  55. scaleX = Double.parseDouble(scales[0]);
  56. } catch (NumberFormatException nfe) {
  57. throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale}));
  58. }
  59. double scaleY;
  60. switch (scales.length) {
  61. case 1:
  62. scaleY = scaleX;
  63. break;
  64. case 2:
  65. try {
  66. scaleY = Double.parseDouble(scales[1]);
  67. } catch (NumberFormatException nfe) {
  68. throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale}));
  69. }
  70. break;
  71. default:
  72. throw new IllegalArgumentException("Too many arguments");
  73. }
  74. if (scaleX <= 0 || scaleY <= 0) {
  75. throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale}));
  76. }
  77. return new Point2D.Double(scaleX, scaleY);
  78. }
  79. }