Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PrintRenderer.java 6.8KB

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