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.8KB

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