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.

InlineContainerLayoutManager.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.util.ArrayList;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import org.apache.fop.area.Area;
  23. import org.apache.fop.area.Trait;
  24. import org.apache.fop.area.inline.Container;
  25. import org.apache.fop.area.inline.InlineViewport;
  26. import org.apache.fop.fo.flow.InlineContainer;
  27. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  28. import org.apache.fop.fo.properties.LengthRangeProperty;
  29. import org.apache.fop.fo.properties.Property;
  30. import org.apache.fop.fonts.Font;
  31. import org.apache.fop.fonts.FontInfo;
  32. import org.apache.fop.fonts.FontTriplet;
  33. import org.apache.fop.layoutmgr.AbstractLayoutManager;
  34. import org.apache.fop.layoutmgr.BlockLevelEventProducer;
  35. import org.apache.fop.layoutmgr.ElementListUtils;
  36. import org.apache.fop.layoutmgr.InlineKnuthSequence;
  37. import org.apache.fop.layoutmgr.KnuthPossPosIter;
  38. import org.apache.fop.layoutmgr.KnuthSequence;
  39. import org.apache.fop.layoutmgr.LayoutContext;
  40. import org.apache.fop.layoutmgr.LayoutManager;
  41. import org.apache.fop.layoutmgr.ListElement;
  42. import org.apache.fop.layoutmgr.Position;
  43. import org.apache.fop.layoutmgr.PositionIterator;
  44. import org.apache.fop.layoutmgr.SpaceResolver;
  45. import org.apache.fop.layoutmgr.TraitSetter;
  46. /**
  47. * This creates a single inline container area after
  48. * laying out the child block areas. All footnotes, floats
  49. * and id areas are maintained for later retrieval.
  50. */
  51. public class InlineContainerLayoutManager extends AbstractLayoutManager implements InlineLevelLayoutManager {
  52. private CommonBorderPaddingBackground borderProps;
  53. private int alignmentBaseline = EN_BASELINE;
  54. private int contentAreaIPD;
  55. private int contentAreaBPD;
  56. private List<ListElement> childElements;
  57. private InlineViewport currentViewport;
  58. private Container referenceArea;
  59. public InlineContainerLayoutManager(InlineContainer node) {
  60. super(node);
  61. }
  62. @Override
  63. public void initialize() {
  64. InlineContainer node = (InlineContainer) fobj;
  65. borderProps = node.getCommonBorderPaddingBackground();
  66. }
  67. @Override
  68. public List<KnuthSequence> getNextKnuthElements(LayoutContext context, int alignment) {
  69. determineIPD(context);
  70. LayoutContext childLC = LayoutContext.offspringOf(context); // TODO copyOf?
  71. childLC.setRefIPD(contentAreaIPD);
  72. childElements = getChildKnuthElements(childLC, alignment); // TODO which alignment?
  73. determineBPD();
  74. AlignmentContext alignmentContext = makeAlignmentContext(context); // TODO correct?
  75. Position position = new Position(this, 0);
  76. KnuthSequence knuthSequence = new InlineKnuthSequence();
  77. knuthSequence.add(new KnuthInlineBox(contentAreaIPD, alignmentContext, position, false));
  78. List<KnuthSequence> knuthElements = new ArrayList<KnuthSequence>(1);
  79. knuthElements.add(knuthSequence);
  80. setFinished(true);
  81. return knuthElements;
  82. }
  83. private void determineIPD(LayoutContext layoutContext) {
  84. LengthRangeProperty ipd = ((InlineContainer) fobj).getInlineProgressionDimension();
  85. Property optimum = ipd.getOptimum(this); // TODO percent base context
  86. if (optimum.isAuto()) {
  87. contentAreaIPD = layoutContext.getRefIPD();
  88. InlineLevelEventProducer eventProducer = InlineLevelEventProducer.Provider.get(
  89. fobj.getUserAgent().getEventBroadcaster());
  90. eventProducer.inlineContainerAutoIPDNotSupported(this, contentAreaIPD / 1000f);
  91. } else {
  92. contentAreaIPD = optimum.getLength().getValue(this); // TODO percent base context
  93. }
  94. }
  95. private void determineBPD() {
  96. LengthRangeProperty bpd = ((InlineContainer) fobj).getBlockProgressionDimension();
  97. Property optimum = bpd.getOptimum(this); // TODO percent base context
  98. int actualBPD = ElementListUtils.calcContentLength(childElements);
  99. if (optimum.isAuto()) {
  100. contentAreaBPD = actualBPD;
  101. } else {
  102. contentAreaBPD = optimum.getLength().getValue(this); // TODO percent base context
  103. if (contentAreaBPD < actualBPD) {
  104. BlockLevelEventProducer eventProducer = BlockLevelEventProducer.Provider.get(
  105. fobj.getUserAgent().getEventBroadcaster());
  106. boolean canRecover = (((InlineContainer) fobj).getOverflow() != EN_ERROR_IF_OVERFLOW);
  107. eventProducer.viewportBPDOverflow(this, fobj.getName(),
  108. actualBPD - contentAreaBPD, needClip(), canRecover,
  109. fobj.getLocator());
  110. }
  111. }
  112. }
  113. private List<ListElement> getChildKnuthElements(LayoutContext layoutContext, int alignment) {
  114. List<ListElement> allChildElements = new LinkedList<ListElement>();
  115. LayoutManager childLM;
  116. while ((childLM = getChildLM()) != null) {
  117. LayoutContext childLC = LayoutContext.offspringOf(layoutContext); // TODO copyOf? newInstance?
  118. childLC.setRefIPD(layoutContext.getRefIPD());
  119. @SuppressWarnings("unchecked")
  120. List<ListElement> childElements = childLM.getNextKnuthElements(childLC, alignmentBaseline);
  121. allChildElements.addAll(childElements);
  122. // TODO breaks, keeps, empty content
  123. }
  124. SpaceResolver.resolveElementList(allChildElements);
  125. // TODO break-before, break-after
  126. return allChildElements;
  127. }
  128. @Override
  129. public void addAreas(PositionIterator posIter, LayoutContext context) {
  130. Position inlineContainerPosition = null;
  131. while (posIter.hasNext()) {
  132. Position pos = posIter.next();
  133. if (pos.getLM() == this) {
  134. inlineContainerPosition = pos;
  135. }
  136. }
  137. addId();
  138. // addMarkersToPage(
  139. // true,
  140. // true,
  141. // lastPos == null || isLast(lastPos));
  142. if (inlineContainerPosition != null) {
  143. LayoutManager childLM;
  144. KnuthPossPosIter childPosIter = new KnuthPossPosIter(childElements);
  145. while ((childLM = childPosIter.getNextChildLM()) != null) {
  146. LayoutContext childLC = LayoutContext.copyOf(context); // TODO correct?
  147. childLM.addAreas(childPosIter, childLC);
  148. }
  149. }
  150. // addMarkersToPage(
  151. // false,
  152. // true,
  153. // lastPos == null || isLast(lastPos));
  154. // boolean isLast = (context.isLastArea() && prevLM == lastChildLM);
  155. // context.setFlags(LayoutContext.LAST_AREA, isLast);
  156. }
  157. @Override
  158. public Area getParentArea(Area childArea) {
  159. if (referenceArea == null) {
  160. referenceArea = new Container();
  161. referenceArea.addTrait(Trait.IS_REFERENCE_AREA, Boolean.TRUE);
  162. TraitSetter.setProducerID(referenceArea, fobj.getId());
  163. referenceArea.setIPD(contentAreaIPD);
  164. currentViewport = new InlineViewport(referenceArea);
  165. currentViewport.addTrait(Trait.IS_VIEWPORT_AREA, Boolean.TRUE);
  166. currentViewport.setIPD(getContentAreaIPD());
  167. currentViewport.setBPD(getContentAreaBPD());
  168. TraitSetter.setProducerID(currentViewport, fobj.getId());
  169. TraitSetter.addBorders(currentViewport,
  170. borderProps,
  171. false, false, false, false, this);
  172. TraitSetter.addPadding(currentViewport,
  173. borderProps,
  174. false, false, false, false, this);
  175. TraitSetter.addBackground(currentViewport,
  176. borderProps,
  177. this);
  178. currentViewport.setClip(needClip());
  179. currentViewport.setContentPosition(
  180. new java.awt.geom.Rectangle2D.Float(0, 0, getContentAreaIPD(), getContentAreaBPD()));
  181. getParent().addChildArea(currentViewport);
  182. }
  183. return referenceArea;
  184. }
  185. @Override
  186. public int getContentAreaIPD() {
  187. return contentAreaIPD;
  188. }
  189. @Override
  190. public int getContentAreaBPD() {
  191. return contentAreaBPD;
  192. }
  193. @Override
  194. public void addChildArea(Area childArea) {
  195. referenceArea.addChildArea(childArea);
  196. }
  197. private boolean needClip() {
  198. int overflow = ((InlineContainer) fobj).getOverflow();
  199. return (overflow == EN_HIDDEN || overflow == EN_ERROR_IF_OVERFLOW);
  200. }
  201. protected AlignmentContext makeAlignmentContext(LayoutContext context) {
  202. InlineContainer ic = (InlineContainer) fobj;
  203. FontInfo fi = fobj.getFOEventHandler().getFontInfo();
  204. FontTriplet[] fontkeys = ic.getCommonFont().getFontState(fi);
  205. Font fs = fi.getFontInstance(fontkeys[0], ic.getCommonFont().fontSize.getValue(this));
  206. return new AlignmentContext(fs, ic.getLineHeight().getOptimum(this).getLength().getValue(this), // TODO
  207. context.getWritingMode());
  208. }
  209. public List addALetterSpaceTo(List oldList) {
  210. throw new UnsupportedOperationException("Not implemented");
  211. }
  212. public List addALetterSpaceTo(List oldList, int depth) {
  213. throw new UnsupportedOperationException("Not implemented");
  214. }
  215. public String getWordChars(Position pos) {
  216. throw new UnsupportedOperationException("Not implemented");
  217. }
  218. public void hyphenate(Position pos, HyphContext hyphContext) {
  219. throw new UnsupportedOperationException("Not implemented");
  220. }
  221. public boolean applyChanges(List oldList) {
  222. throw new UnsupportedOperationException("Not implemented");
  223. }
  224. public boolean applyChanges(List oldList, int depth) {
  225. throw new UnsupportedOperationException("Not implemented");
  226. }
  227. public List getChangedKnuthElements(List oldList, int alignment, int depth) {
  228. throw new UnsupportedOperationException("Not implemented");
  229. }
  230. }