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.

PrintRenderer.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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;
  19. // FOP
  20. import java.awt.geom.Rectangle2D;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.w3c.dom.Document;
  24. import org.apache.fop.apps.FOPException;
  25. import org.apache.fop.apps.FOUserAgent;
  26. import org.apache.fop.area.Area;
  27. import org.apache.fop.area.Trait;
  28. import org.apache.fop.fonts.CustomFontCollection;
  29. import org.apache.fop.fonts.EmbedFontInfo;
  30. import org.apache.fop.fonts.Font;
  31. import org.apache.fop.fonts.FontCollection;
  32. import org.apache.fop.fonts.FontInfo;
  33. import org.apache.fop.fonts.FontManager;
  34. import org.apache.fop.fonts.FontTriplet;
  35. import org.apache.fop.fonts.base14.Base14FontCollection;
  36. /** Abstract base class of "Print" type renderers. */
  37. public abstract class PrintRenderer extends AbstractRenderer {
  38. /**
  39. * @param userAgent the user agent that contains configuration details. This cannot be null.
  40. */
  41. public PrintRenderer(FOUserAgent userAgent) {
  42. super(userAgent);
  43. }
  44. /** Font configuration */
  45. protected FontInfo fontInfo;
  46. /** list of fonts */
  47. protected List<EmbedFontInfo> embedFontInfoList;
  48. /**
  49. * Adds a font list to current list of fonts
  50. * @param fontList a font info list
  51. */
  52. public void addFontList(List<EmbedFontInfo> fontList) {
  53. if (embedFontInfoList == null) {
  54. setFontList(fontList);
  55. } else {
  56. embedFontInfoList.addAll(fontList);
  57. }
  58. }
  59. /**
  60. * @param embedFontInfoList list of available fonts
  61. */
  62. public void setFontList(List<EmbedFontInfo> embedFontInfoList) {
  63. this.embedFontInfoList = embedFontInfoList;
  64. }
  65. /**
  66. * @return list of available embedded fonts
  67. */
  68. public List<EmbedFontInfo> getFontList() {
  69. return this.embedFontInfoList;
  70. }
  71. /** {@inheritDoc} */
  72. public void setupFontInfo(FontInfo inFontInfo) throws FOPException {
  73. this.fontInfo = inFontInfo;
  74. FontManager fontManager = userAgent.getFontManager();
  75. FontCollection[] fontCollections = new FontCollection[] {
  76. new Base14FontCollection(fontManager.isBase14KerningEnabled()),
  77. new CustomFontCollection(fontManager.getResourceResolver(), getFontList(),
  78. userAgent.isComplexScriptFeaturesEnabled())
  79. };
  80. fontManager.setup(getFontInfo(), fontCollections);
  81. }
  82. /**
  83. * Returns the internal font key for a font triplet coming from the area tree
  84. * @param area the area from which to retrieve the font triplet information
  85. * @return the internal font key (F1, F2 etc.) or null if not found
  86. */
  87. protected String getInternalFontNameForArea(Area area) {
  88. FontTriplet triplet = (FontTriplet)area.getTrait(Trait.FONT);
  89. String key = fontInfo.getInternalFontKey(triplet);
  90. if (key == null) {
  91. //Find a default fallback font as last resort
  92. triplet = FontTriplet.DEFAULT_FONT_TRIPLET;
  93. key = fontInfo.getInternalFontKey(triplet);
  94. }
  95. return key;
  96. }
  97. /**
  98. * Returns a Font object constructed based on the font traits in an area
  99. * @param area the area from which to retrieve the font triplet information
  100. * @return the requested Font instance or null if not found
  101. */
  102. protected Font getFontFromArea(Area area) {
  103. FontTriplet triplet = (FontTriplet)area.getTrait(Trait.FONT);
  104. int size = (Integer) area.getTrait(Trait.FONT_SIZE);
  105. return fontInfo.getFontInstance(triplet, size);
  106. }
  107. /**
  108. * Instantiates a RendererContext for an image
  109. * @return a newly created RendererContext.
  110. */
  111. protected RendererContext instantiateRendererContext() {
  112. return new RendererContext(this, getMimeType());
  113. }
  114. /**
  115. * Creates a RendererContext for an image.
  116. * @param x the x coordinate (in millipoints)
  117. * @param y the y coordinate (in millipoints)
  118. * @param width the width of the image (in millipoints)
  119. * @param height the height of the image (in millipoints)
  120. * @param foreignAttributes a Map or foreign attributes, may be null
  121. * @return the RendererContext
  122. */
  123. protected RendererContext createRendererContext(int x, int y, int width, int height,
  124. Map foreignAttributes) {
  125. RendererContext context = instantiateRendererContext();
  126. context.setUserAgent(userAgent);
  127. context.setProperty(RendererContextConstants.WIDTH,
  128. width);
  129. context.setProperty(RendererContextConstants.HEIGHT,
  130. height);
  131. context.setProperty(RendererContextConstants.XPOS,
  132. x);
  133. context.setProperty(RendererContextConstants.YPOS,
  134. y);
  135. context.setProperty(RendererContextConstants.PAGE_VIEWPORT,
  136. getCurrentPageViewport());
  137. if (foreignAttributes != null) {
  138. context.setProperty(RendererContextConstants.FOREIGN_ATTRIBUTES, foreignAttributes);
  139. }
  140. return context;
  141. }
  142. /**
  143. * Renders an XML document (SVG for example).
  144. * @param doc the DOM Document containing the XML document to be rendered
  145. * @param ns the namespace URI for the XML document
  146. * @param pos the position for the generated graphic/image
  147. * @param foreignAttributes the foreign attributes containing rendering hints, or null
  148. */
  149. public void renderDocument(Document doc, String ns, Rectangle2D pos, Map foreignAttributes) {
  150. int x = currentIPPosition + (int) pos.getX();
  151. int y = currentBPPosition + (int) pos.getY();
  152. int width = (int)pos.getWidth();
  153. int height = (int)pos.getHeight();
  154. RendererContext context = createRendererContext(x, y, width, height, foreignAttributes);
  155. renderXML(context, doc, ns);
  156. }
  157. /**
  158. * @return the font info
  159. */
  160. public FontInfo getFontInfo() {
  161. return this.fontInfo;
  162. }
  163. }