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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 org.apache.fop.area.Area;
  21. import org.apache.fop.area.Trait;
  22. import org.apache.fop.fonts.Font;
  23. import org.apache.fop.fonts.FontInfo;
  24. import org.apache.fop.fonts.FontResolver;
  25. import org.apache.fop.fonts.FontTriplet;
  26. import org.w3c.dom.Document;
  27. // Java
  28. import java.awt.Color;
  29. import java.awt.geom.Rectangle2D;
  30. import java.util.List;
  31. import java.util.Map;
  32. /** Abstract base class of "Print" type renderers. */
  33. public abstract class PrintRenderer extends AbstractRenderer {
  34. /** Font configuration */
  35. protected FontInfo fontInfo;
  36. /** Font resolver */
  37. protected FontResolver fontResolver = null;
  38. /** list of fonts */
  39. protected List/*<EmbedFontInfo>*/ embedFontInfoList = null;
  40. /**
  41. * Adds a font list to current list of fonts
  42. * @param fontList a font info list
  43. */
  44. public void addFontList(List/*<EmbedFontInfo>*/ fontList) {
  45. if (embedFontInfoList == null) {
  46. setFontList(fontList);
  47. } else {
  48. fontList.addAll(fontList);
  49. }
  50. }
  51. /**
  52. * @param embedFontInfoList list of available fonts
  53. */
  54. public void setFontList(List/*<EmbedFontInfo>*/ embedFontInfoList) {
  55. this.embedFontInfoList = embedFontInfoList;
  56. }
  57. /**
  58. * @return list of available embedded fonts
  59. */
  60. public List/*<EmbedFontInfo>*/ getFontList() {
  61. return this.embedFontInfoList;
  62. }
  63. /**
  64. * Set up the font info
  65. *
  66. * @param inFontInfo font info to set up
  67. */
  68. public void setupFontInfo(FontInfo inFontInfo) {
  69. this.fontInfo = inFontInfo;
  70. userAgent.getFactory().getFontManager().setupRenderer(this);
  71. }
  72. /**
  73. * Returns the internal font key for a font triplet coming from the area tree
  74. * @param area the area from which to retrieve the font triplet information
  75. * @return the internal font key (F1, F2 etc.) or null if not found
  76. */
  77. protected String getInternalFontNameForArea(Area area) {
  78. FontTriplet triplet = (FontTriplet)area.getTrait(Trait.FONT);
  79. return fontInfo.getInternalFontKey(triplet);
  80. }
  81. /**
  82. * Returns a Font object constructed based on the font traits in an area
  83. * @param area the area from which to retrieve the font triplet information
  84. * @return the requested Font instance or null if not found
  85. */
  86. protected Font getFontFromArea(Area area) {
  87. FontTriplet triplet = (FontTriplet)area.getTrait(Trait.FONT);
  88. int size = ((Integer)area.getTrait(Trait.FONT_SIZE)).intValue();
  89. return fontInfo.getFontInstance(triplet, size);
  90. }
  91. /**
  92. * Lightens up a color for groove, ridge, inset and outset border effects.
  93. * @param col the color to lighten up
  94. * @param factor factor by which to lighten up (negative values darken the color)
  95. * @return the modified color
  96. */
  97. public static Color lightenColor(Color col, float factor) {
  98. // TODO: This function converts the color into the sRGB namespace.
  99. // This should be avoided if possible.
  100. float[] cols = new float[4];
  101. cols = col.getRGBComponents(cols);
  102. if (factor > 0) {
  103. cols[0] += (1.0 - cols[0]) * factor;
  104. cols[1] += (1.0 - cols[1]) * factor;
  105. cols[2] += (1.0 - cols[2]) * factor;
  106. } else {
  107. cols[0] -= cols[0] * -factor;
  108. cols[1] -= cols[1] * -factor;
  109. cols[2] -= cols[2] * -factor;
  110. }
  111. return new Color(cols[0], cols[1], cols[2], cols[3]);
  112. }
  113. /**
  114. * Creates a RendererContext for an image.
  115. * @param x the x coordinate (in millipoints)
  116. * @param y the y coordinate (in millipoints)
  117. * @param width the width of the image (in millipoints)
  118. * @param height the height of the image (in millipoints)
  119. * @param foreignAttributes a Map or foreign attributes, may be null
  120. * @return the RendererContext
  121. */
  122. protected RendererContext createRendererContext(int x, int y, int width, int height,
  123. Map foreignAttributes) {
  124. RendererContext context;
  125. context = new RendererContext(this, getMimeType());
  126. context.setUserAgent(userAgent);
  127. context.setProperty(RendererContextConstants.WIDTH,
  128. new Integer(width));
  129. context.setProperty(RendererContextConstants.HEIGHT,
  130. new Integer(height));
  131. context.setProperty(RendererContextConstants.XPOS,
  132. new Integer(x));
  133. context.setProperty(RendererContextConstants.YPOS,
  134. new Integer(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. * Get FontResolver
  159. *
  160. * @return FontResolver
  161. */
  162. public FontResolver getFontResolver() {
  163. if (this.fontResolver == null) {
  164. this.fontResolver = new DefaultFontResolver(super.userAgent);
  165. }
  166. return this.fontResolver;
  167. }
  168. /**
  169. * @return the font info
  170. */
  171. public FontInfo getFontInfo() {
  172. return this.fontInfo;
  173. }
  174. }