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.

AbstractGraphicsLayoutManager.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.layoutmgr.inline;
  19. import java.awt.Dimension;
  20. import java.awt.Rectangle;
  21. import java.util.List;
  22. import org.apache.fop.area.Area;
  23. import org.apache.fop.area.inline.InlineViewport;
  24. import org.apache.fop.datatypes.LengthBase;
  25. import org.apache.fop.fo.FObj;
  26. import org.apache.fop.fo.flow.AbstractGraphics;
  27. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  28. import org.apache.fop.layoutmgr.LayoutContext;
  29. import org.apache.fop.layoutmgr.TraitSetter;
  30. /**
  31. * LayoutManager handling the common tasks for the fo:instream-foreign-object
  32. * and fo:external-graphics formatting objects
  33. */
  34. public abstract class AbstractGraphicsLayoutManager extends LeafNodeLayoutManager {
  35. /**
  36. * Constructor.
  37. *
  38. * @param node
  39. * the formatting object that creates this area
  40. */
  41. public AbstractGraphicsLayoutManager(AbstractGraphics node) {
  42. super(node);
  43. }
  44. /**
  45. * Get the inline area created by this element.
  46. *
  47. * @return the viewport inline area
  48. */
  49. private InlineViewport getInlineArea() {
  50. final AbstractGraphics fobj = (AbstractGraphics)this.fobj;
  51. Dimension intrinsicSize = new Dimension(
  52. fobj.getIntrinsicWidth(),
  53. fobj.getIntrinsicHeight());
  54. //TODO Investigate if the line-height property has to be taken into the calculation
  55. //somehow. There was some code here that hints in this direction but it was disabled.
  56. ImageLayout imageLayout = new ImageLayout(fobj, this, intrinsicSize);
  57. Rectangle placement = imageLayout.getPlacement();
  58. CommonBorderPaddingBackground borderProps = fobj.getCommonBorderPaddingBackground();
  59. //Determine extra BPD from borders and padding
  60. int beforeBPD = borderProps.getPadding(CommonBorderPaddingBackground.BEFORE, false, this);
  61. beforeBPD += borderProps.getBorderWidth(CommonBorderPaddingBackground.BEFORE, false);
  62. placement.y += beforeBPD;
  63. //Determine extra IPD from borders and padding
  64. int startIPD = borderProps.getPadding(CommonBorderPaddingBackground.START, false, this);
  65. startIPD += borderProps.getBorderWidth(CommonBorderPaddingBackground.START, false);
  66. placement.x += startIPD;
  67. Area viewportArea = getChildArea();
  68. TraitSetter.setProducerID(viewportArea, fobj.getId());
  69. transferForeignAttributes(viewportArea);
  70. InlineViewport vp = new InlineViewport(viewportArea);
  71. TraitSetter.addStructureTreeElement(vp, fobj.getStructureTreeElement());
  72. TraitSetter.setProducerID(vp, fobj.getId());
  73. vp.setIPD(imageLayout.getViewportSize().width);
  74. vp.setBPD(imageLayout.getViewportSize().height);
  75. vp.setContentPosition(placement);
  76. vp.setClip(imageLayout.isClipped());
  77. vp.setOffset(0);
  78. // Common Border, Padding, and Background Properties
  79. TraitSetter.addBorders(vp, fobj.getCommonBorderPaddingBackground()
  80. , false, false, false, false, this);
  81. TraitSetter.addPadding(vp, fobj.getCommonBorderPaddingBackground()
  82. , false, false, false, false, this);
  83. TraitSetter.addBackground(vp, fobj.getCommonBorderPaddingBackground(), this);
  84. return vp;
  85. }
  86. /** {@inheritDoc} */
  87. public List getNextKnuthElements(LayoutContext context,
  88. int alignment) {
  89. InlineViewport areaCurrent = getInlineArea();
  90. setCurrentArea(areaCurrent);
  91. return super.getNextKnuthElements(context, alignment);
  92. }
  93. /** {@inheritDoc} */
  94. protected AlignmentContext makeAlignmentContext(LayoutContext context) {
  95. final AbstractGraphics fobj = (AbstractGraphics)this.fobj;
  96. return new AlignmentContext(
  97. get(context).getAllocBPD()
  98. , fobj.getAlignmentAdjust()
  99. , fobj.getAlignmentBaseline()
  100. , fobj.getBaselineShift()
  101. , fobj.getDominantBaseline()
  102. , context.getAlignmentContext()
  103. );
  104. }
  105. /**
  106. * Returns the image of foreign object area to be put into
  107. * the viewport.
  108. * @return the appropriate area
  109. */
  110. protected abstract Area getChildArea();
  111. // --------- Property Resolution related functions --------- //
  112. /**
  113. * {@inheritDoc}
  114. */
  115. public int getBaseLength(int lengthBase, FObj fobj) {
  116. switch (lengthBase) {
  117. case LengthBase.IMAGE_INTRINSIC_WIDTH:
  118. return ((AbstractGraphics)fobj).getIntrinsicWidth();
  119. case LengthBase.IMAGE_INTRINSIC_HEIGHT:
  120. return ((AbstractGraphics)fobj).getIntrinsicHeight();
  121. case LengthBase.ALIGNMENT_ADJUST:
  122. return get(null).getBPD();
  123. default: // Delegate to super class
  124. return super.getBaseLength(lengthBase, fobj);
  125. }
  126. }
  127. }