選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PrintRenderer.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.FontSetup;
  26. import org.apache.fop.fonts.FontTriplet;
  27. import org.w3c.dom.Document;
  28. // Java
  29. import java.awt.Color;
  30. import java.awt.geom.Rectangle2D;
  31. import java.util.List;
  32. import java.util.Map;
  33. /** Abstract base class of "Print" type renderers. */
  34. public abstract class PrintRenderer extends AbstractRenderer {
  35. /** Font configuration */
  36. protected FontInfo fontInfo;
  37. /** Font resolver */
  38. protected FontResolver fontResolver = null;
  39. /** list of fonts */
  40. protected List fontList = null;
  41. /**
  42. * adds a font list to current list of fonts
  43. * @param fontInfoList font list
  44. */
  45. public void addFontList(List fontInfoList) {
  46. if (this.fontList == null) {
  47. setFontList(fontInfoList);
  48. } else {
  49. this.fontList.addAll(fontInfoList);
  50. }
  51. }
  52. /**
  53. * @param fontList list of available fonts
  54. */
  55. public void setFontList(List fontList) {
  56. this.fontList = fontList;
  57. }
  58. /**
  59. * Set up the font info
  60. *
  61. * @param inFontInfo font info to set up
  62. */
  63. public void setupFontInfo(FontInfo inFontInfo) {
  64. this.fontInfo = inFontInfo;
  65. FontSetup.setup(fontInfo, fontList, fontResolver,
  66. userAgent.getFactory().isBase14KerningEnabled());
  67. }
  68. /**
  69. * Returns the internal font key fot a font triplet coming from the area tree
  70. * @param area the area from which to retrieve the font triplet information
  71. * @return the internal font key (F1, F2 etc.) or null if not found
  72. */
  73. protected String getInternalFontNameForArea(Area area) {
  74. FontTriplet triplet = (FontTriplet)area.getTrait(Trait.FONT);
  75. return fontInfo.getInternalFontKey(triplet);
  76. }
  77. /**
  78. * Returns a Font object constructed based on the font traits in an area
  79. * @param area the area from which to retrieve the font triplet information
  80. * @return the requested Font instance or null if not found
  81. */
  82. protected Font getFontFromArea(Area area) {
  83. FontTriplet triplet = (FontTriplet)area.getTrait(Trait.FONT);
  84. int size = ((Integer)area.getTrait(Trait.FONT_SIZE)).intValue();
  85. return fontInfo.getFontInstance(triplet, size);
  86. }
  87. /**
  88. * Lightens up a color for groove, ridge, inset and outset border effects.
  89. * @param col the color to lighten up
  90. * @param factor factor by which to lighten up (negative values darken the color)
  91. * @return the modified color
  92. */
  93. public static Color lightenColor(Color col, float factor) {
  94. // TODO: This function converts the color into the sRGB namespace.
  95. // This should be avoided if possible.
  96. float[] cols = new float[4];
  97. cols = col.getRGBComponents(cols);
  98. if (factor > 0) {
  99. cols[0] += (1.0 - cols[0]) * factor;
  100. cols[1] += (1.0 - cols[1]) * factor;
  101. cols[2] += (1.0 - cols[2]) * factor;
  102. } else {
  103. cols[0] -= cols[0] * -factor;
  104. cols[1] -= cols[1] * -factor;
  105. cols[2] -= cols[2] * -factor;
  106. }
  107. return new Color(cols[0], cols[1], cols[2], cols[3]);
  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;
  121. context = new RendererContext(this, getMimeType());
  122. context.setUserAgent(userAgent);
  123. context.setProperty(RendererContextConstants.WIDTH,
  124. new Integer(width));
  125. context.setProperty(RendererContextConstants.HEIGHT,
  126. new Integer(height));
  127. context.setProperty(RendererContextConstants.XPOS,
  128. new Integer(x));
  129. context.setProperty(RendererContextConstants.YPOS,
  130. new Integer(y));
  131. context.setProperty(RendererContextConstants.PAGE_VIEWPORT,
  132. getCurrentPageViewport());
  133. if (foreignAttributes != null) {
  134. context.setProperty(RendererContextConstants.FOREIGN_ATTRIBUTES, foreignAttributes);
  135. }
  136. return context;
  137. }
  138. /**
  139. * Renders an XML document (SVG for example).
  140. * @param doc the DOM Document containing the XML document to be rendered
  141. * @param ns the namespace URI for the XML document
  142. * @param pos the position for the generated graphic/image
  143. * @param foreignAttributes the foreign attributes containing rendering hints, or null
  144. */
  145. public void renderDocument(Document doc, String ns, Rectangle2D pos, Map foreignAttributes) {
  146. int x = currentIPPosition + (int) pos.getX();
  147. int y = currentBPPosition + (int) pos.getY();
  148. int width = (int)pos.getWidth();
  149. int height = (int)pos.getHeight();
  150. RendererContext context = createRendererContext(x, y, width, height, foreignAttributes);
  151. renderXML(context, doc, ns);
  152. }
  153. /**
  154. * Get FontResolver
  155. *
  156. * @return FontResolver
  157. */
  158. public FontResolver getFontResolver() {
  159. if (this.fontResolver == null) {
  160. this.fontResolver = new DefaultFontResolver(super.userAgent);
  161. }
  162. return this.fontResolver;
  163. }
  164. }