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

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