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.

PageAttributesConverter.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.rtf;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.impl.SimpleLog;
  21. import org.apache.fop.datatypes.Length;
  22. import org.apache.fop.fo.Constants;
  23. import org.apache.fop.fo.expr.NumericOp;
  24. import org.apache.fop.fo.pagination.RegionBA;
  25. import org.apache.fop.fo.pagination.RegionBody;
  26. import org.apache.fop.fo.pagination.SimplePageMaster;
  27. import org.apache.fop.fo.properties.CommonMarginBlock;
  28. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAttributes;
  29. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfPage;
  30. /** Converts simple-page-master attributes into strings as defined in RtfPage.
  31. * @author Christopher Scott, scottc@westinghouse.com
  32. * @author Peter Herweg, pherweg@web.de
  33. */
  34. final class PageAttributesConverter {
  35. private static Log log = new SimpleLog("FOP/RTF");
  36. /**
  37. * Constructor is private, because it's just a utility class.
  38. */
  39. private PageAttributesConverter() {
  40. }
  41. /** convert xsl:fo attributes to RTF text attributes */
  42. static RtfAttributes convertPageAttributes(SimplePageMaster pagemaster) {
  43. FOPRtfAttributes attrib = new FOPRtfAttributes();
  44. try {
  45. RegionBA before = (RegionBA) pagemaster.getRegion(Constants.FO_REGION_BEFORE);
  46. RegionBody body = (RegionBody) pagemaster.getRegion(Constants.FO_REGION_BODY);
  47. RegionBA after = (RegionBA) pagemaster.getRegion(Constants.FO_REGION_AFTER);
  48. attrib.setTwips(RtfPage.PAGE_WIDTH, pagemaster.getPageWidth());
  49. attrib.setTwips(RtfPage.PAGE_HEIGHT, pagemaster.getPageHeight());
  50. //Sets the document level property
  51. attrib.set(RtfPage.ITAP, "0");
  52. Object widthRaw = attrib.getValue(RtfPage.PAGE_WIDTH);
  53. Object heightRaw = attrib.getValue(RtfPage.PAGE_HEIGHT);
  54. if ((widthRaw instanceof Integer) && (heightRaw instanceof Integer)
  55. && ((Integer) widthRaw).intValue() > ((Integer) heightRaw).intValue()) {
  56. attrib.set(RtfPage.LANDSCAPE);
  57. }
  58. Length pageTop = pagemaster.getCommonMarginBlock().marginTop;
  59. Length pageBottom = pagemaster.getCommonMarginBlock().marginBottom;
  60. Length pageLeft = pagemaster.getCommonMarginBlock().marginLeft;
  61. Length pageRight = pagemaster.getCommonMarginBlock().marginRight;
  62. Length bodyTop = pageTop;
  63. Length bodyBottom = pageBottom;
  64. Length bodyLeft = pageLeft;
  65. Length bodyRight = pageRight;
  66. if (body != null) {
  67. // Should perhaps be replaced by full reference-area handling.
  68. CommonMarginBlock bodyMargin = body.getCommonMarginBlock();
  69. bodyTop = (Length) NumericOp.addition(pageTop, bodyMargin.marginTop);
  70. bodyBottom = (Length) NumericOp.addition(pageBottom, bodyMargin.marginBottom);
  71. bodyLeft = (Length) NumericOp.addition(pageLeft, bodyMargin.marginLeft);
  72. bodyRight = (Length) NumericOp.addition(pageRight, bodyMargin.marginRight);
  73. }
  74. attrib.setTwips(RtfPage.MARGIN_TOP, bodyTop);
  75. attrib.setTwips(RtfPage.MARGIN_BOTTOM, bodyBottom);
  76. attrib.setTwips(RtfPage.MARGIN_LEFT, bodyLeft);
  77. attrib.setTwips(RtfPage.MARGIN_RIGHT, bodyRight);
  78. //region-before attributes
  79. Length beforeTop = pageTop;
  80. if (before != null) {
  81. beforeTop = (Length) NumericOp.addition(pageTop, before.getExtent());
  82. }
  83. attrib.setTwips(RtfPage.HEADERY, beforeTop);
  84. //region-after attributes
  85. Length afterBottom = pageBottom;
  86. if (after != null) {
  87. afterBottom = (Length) NumericOp.addition(pageBottom, after.getExtent());
  88. }
  89. attrib.setTwips(RtfPage.FOOTERY, afterBottom);
  90. } catch (Exception e) {
  91. log.error("Exception in convertPageAttributes: "
  92. + e.getMessage() + "- page attributes ignored");
  93. attrib = new FOPRtfAttributes();
  94. }
  95. return attrib;
  96. }
  97. }