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.

BackgroundPositionShorthand.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.fo.properties;
  19. import java.util.List;
  20. import org.apache.fop.datatypes.PercentBase;
  21. import org.apache.fop.datatypes.PercentBaseContext;
  22. import org.apache.fop.fo.Constants;
  23. import org.apache.fop.fo.FObj;
  24. import org.apache.fop.fo.PropertyList;
  25. import org.apache.fop.fo.expr.PropertyException;
  26. /**
  27. * Class encapsulating functionality for the <a href="http://www.w3.org/TR/xsl/#background-position">
  28. * <code>background-position</code></a> shorthand.
  29. */
  30. public class BackgroundPositionShorthand extends ListProperty {
  31. /**
  32. * Inner class for creating instances of this property
  33. *
  34. */
  35. public static class Maker extends ListProperty.Maker {
  36. /**
  37. * Construct an instance of a Maker for the given property.
  38. *
  39. * @param propId The Constant ID of the property to be made.
  40. */
  41. public Maker(int propId) {
  42. super(propId);
  43. }
  44. /**
  45. * {@inheritDoc}
  46. * If only <code>background-position-horizontal</code> is
  47. * specified, <code>background-position-vertical</code> is set
  48. * to "50%".
  49. */
  50. public Property make(PropertyList propertyList, String value, FObj fo)
  51. throws PropertyException {
  52. Property p = super.make(propertyList, value, fo);
  53. if (p.getList().size() == 1) {
  54. /* only background-position-horizontal specified
  55. * through the shorthand, as a length or percentage:
  56. * background-position-vertical=50% (see: XSL-FO 1.1 -- 7.31.2)
  57. */
  58. PropertyMaker m = FObj.getPropertyMakerFor(
  59. Constants.PR_BACKGROUND_POSITION_VERTICAL);
  60. p.getList().add(1, m.make(propertyList, "50%", fo));
  61. }
  62. return p;
  63. }
  64. private static final class Dimension1PercentBase implements PercentBase {
  65. /** {@inheritDoc} */
  66. public int getBaseLength(PercentBaseContext context) throws PropertyException {
  67. return 0;
  68. }
  69. /** {@inheritDoc} */
  70. public double getBaseValue() {
  71. return 0;
  72. }
  73. /** {@inheritDoc} */
  74. public int getDimension() {
  75. return 1;
  76. }
  77. }
  78. private static final Dimension1PercentBase DIMENSION_1_PERCENT_BASE
  79. = new Dimension1PercentBase();
  80. /**
  81. * {@inheritDoc}
  82. * Returns a {@link org.apache.fop.datatypes.PercentBase} whose
  83. * <code>getDimension()</code> returns 1.
  84. */
  85. public PercentBase getPercentBase(PropertyList pl) {
  86. return DIMENSION_1_PERCENT_BASE;
  87. }
  88. }
  89. /**
  90. * Inner class to provide shorthand parsing capabilities
  91. *
  92. */
  93. public static class Parser extends GenericShorthandParser {
  94. /** {@inheritDoc} */
  95. public Property getValueForProperty(int propId,
  96. Property property,
  97. PropertyMaker maker,
  98. PropertyList propertyList)
  99. throws PropertyException {
  100. int index = -1;
  101. List propList = property.getList();
  102. if (propId == Constants.PR_BACKGROUND_POSITION_HORIZONTAL) {
  103. index = 0;
  104. } else if (propId == Constants.PR_BACKGROUND_POSITION_VERTICAL) {
  105. index = 1;
  106. }
  107. if (index >= 0) {
  108. return maker.convertProperty(
  109. (Property) propList.get(index),
  110. propertyList,
  111. propertyList.getFObj());
  112. } // else: invalid index? shouldn't happen...
  113. return null;
  114. }
  115. }
  116. }