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

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