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.

InstreamForeignObject.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.flow;
  19. import java.awt.geom.Point2D;
  20. import org.xml.sax.Locator;
  21. import org.apache.xmlgraphics.util.QName;
  22. import org.apache.fop.ResourceEventProducer;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.datatypes.Length;
  25. import org.apache.fop.fo.FONode;
  26. import org.apache.fop.fo.ValidationException;
  27. import org.apache.fop.fo.XMLObj;
  28. /**
  29. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_instream-foreign-object">
  30. * <code>fo:instream-foreign-object</code></a> object.
  31. * This is an atomic inline object that contains XML data.
  32. */
  33. public class InstreamForeignObject extends AbstractGraphics {
  34. // The value of properties relevant for fo:instream-foreign-object.
  35. // All property values contained in AbstractGraphics
  36. // End of property values
  37. //Additional value
  38. private Point2D intrinsicDimensions;
  39. private boolean instrisicSizeDetermined;
  40. private Length intrinsicAlignmentAdjust;
  41. /**
  42. * Constructs an instream-foreign-object object
  43. * (called by {@link org.apache.fop.fo.ElementMapping.Maker}).
  44. *
  45. * @param parent the parent {@link FONode}
  46. */
  47. public InstreamForeignObject(FONode parent) {
  48. super(parent);
  49. }
  50. /**
  51. * Make sure content model satisfied, if so then tell the
  52. * {@link org.apache.fop.fo.FOEventHandler} that we are at
  53. * the end of the instream-foreign-object.
  54. * {@inheritDoc}
  55. */
  56. protected void endOfNode() throws FOPException {
  57. if (firstChild == null) {
  58. missingChildElementError("one (1) non-XSL namespace child");
  59. }
  60. getFOEventHandler().foreignObject(this);
  61. }
  62. /**
  63. * {@inheritDoc}
  64. * <br>XSL Content Model: one (1) non-XSL namespace child
  65. */
  66. protected void validateChildNode(Locator loc, String nsURI, String localName)
  67. throws ValidationException {
  68. if (FO_URI.equals(nsURI)) {
  69. invalidChildError(loc, nsURI, localName);
  70. } else if (firstChild != null) {
  71. tooManyNodesError(loc, new QName(nsURI, null, localName));
  72. }
  73. }
  74. /** {@inheritDoc} */
  75. public String getLocalName() {
  76. return "instream-foreign-object";
  77. }
  78. /**
  79. * {@inheritDoc}
  80. * @return {@link org.apache.fop.fo.Constants#FO_INSTREAM_FOREIGN_OBJECT}
  81. */
  82. public int getNameId() {
  83. return FO_INSTREAM_FOREIGN_OBJECT;
  84. }
  85. /** Preloads the image so the intrinsic size is available. */
  86. private void prepareIntrinsicSize() {
  87. if (!this.instrisicSizeDetermined) {
  88. XMLObj child = (XMLObj) firstChild;
  89. Point2D csize = new Point2D.Float(-1, -1);
  90. intrinsicDimensions = child.getDimension(csize);
  91. if (intrinsicDimensions == null) {
  92. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  93. getUserAgent().getEventBroadcaster());
  94. eventProducer.ifoNoIntrinsicSize(this, getLocator());
  95. }
  96. intrinsicAlignmentAdjust = child.getIntrinsicAlignmentAdjust();
  97. this.instrisicSizeDetermined = true;
  98. }
  99. }
  100. /** {@inheritDoc} */
  101. public int getIntrinsicWidth() {
  102. prepareIntrinsicSize();
  103. if (intrinsicDimensions != null) {
  104. return (int)(intrinsicDimensions.getX() * 1000);
  105. } else {
  106. return 0;
  107. }
  108. }
  109. /** {@inheritDoc} */
  110. public int getIntrinsicHeight() {
  111. prepareIntrinsicSize();
  112. if (intrinsicDimensions != null) {
  113. return (int)(intrinsicDimensions.getY() * 1000);
  114. } else {
  115. return 0;
  116. }
  117. }
  118. /** {@inheritDoc} */
  119. public Length getIntrinsicAlignmentAdjust() {
  120. prepareIntrinsicSize();
  121. return intrinsicAlignmentAdjust;
  122. }
  123. /** {@inheritDoc} */
  124. protected void addChildNode(FONode child) throws FOPException {
  125. super.addChildNode(child);
  126. }
  127. /** @return the {@link XMLObj} child node of the instream-foreign-object. */
  128. public XMLObj getChildXMLObj() {
  129. return (XMLObj) firstChild;
  130. }
  131. }