Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PDFImageHandlerRawJPEG.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.pdf;
  19. import java.awt.Point;
  20. import java.awt.Rectangle;
  21. import java.io.IOException;
  22. import org.apache.xmlgraphics.image.loader.Image;
  23. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  24. import org.apache.xmlgraphics.image.loader.impl.ImageRawJPEG;
  25. import org.apache.fop.pdf.PDFDocument;
  26. import org.apache.fop.pdf.PDFImage;
  27. import org.apache.fop.pdf.PDFResourceContext;
  28. import org.apache.fop.pdf.PDFXObject;
  29. import org.apache.fop.render.ImageHandler;
  30. import org.apache.fop.render.RendererContext;
  31. import org.apache.fop.render.RenderingContext;
  32. /**
  33. * Image handler implementation which handles raw JPEG images for PDF output.
  34. */
  35. public class PDFImageHandlerRawJPEG implements PDFImageHandler, ImageHandler {
  36. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
  37. ImageFlavor.RAW_JPEG,
  38. };
  39. /** {@inheritDoc} */
  40. public PDFXObject generateImage(RendererContext context, Image image,
  41. Point origin, Rectangle pos)
  42. throws IOException {
  43. PDFRenderer renderer = (PDFRenderer)context.getRenderer();
  44. ImageRawJPEG jpeg = (ImageRawJPEG)image;
  45. PDFDocument pdfDoc = (PDFDocument)context.getProperty(
  46. PDFRendererContextConstants.PDF_DOCUMENT);
  47. PDFResourceContext resContext = (PDFResourceContext)context.getProperty(
  48. PDFRendererContextConstants.PDF_CONTEXT);
  49. PDFImage pdfimage = new ImageRawJPEGAdapter(jpeg, image.getInfo().getOriginalURI());
  50. PDFXObject xobj = pdfDoc.addImage(resContext, pdfimage);
  51. float x = (float)pos.getX() / 1000f;
  52. float y = (float)pos.getY() / 1000f;
  53. float w = (float)pos.getWidth() / 1000f;
  54. float h = (float)pos.getHeight() / 1000f;
  55. renderer.placeImage(x, y, w, h, xobj);
  56. return xobj;
  57. }
  58. /** {@inheritDoc} */
  59. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  60. throws IOException {
  61. PDFRenderingContext pdfContext = (PDFRenderingContext)context;
  62. PDFContentGenerator generator = pdfContext.getGenerator();
  63. ImageRawJPEG imageJPEG = (ImageRawJPEG)image;
  64. PDFImage pdfimage = new ImageRawJPEGAdapter(imageJPEG, image.getInfo().getOriginalURI());
  65. PDFXObject xobj = generator.getDocument().addImage(
  66. generator.getResourceContext(), pdfimage);
  67. float x = (float)pos.getX() / 1000f;
  68. float y = (float)pos.getY() / 1000f;
  69. float w = (float)pos.getWidth() / 1000f;
  70. float h = (float)pos.getHeight() / 1000f;
  71. if (context.getUserAgent().isAccessibilityEnabled()) {
  72. String structElemType = pdfContext.getStructElemType();
  73. if (structElemType.length() > 0) {
  74. int sequenceNum = pdfContext.getSequenceNum();
  75. generator.placeImage(x, y, w, h, xobj, structElemType, sequenceNum);
  76. } else {
  77. generator.placeImage(x, y, w, h, xobj);
  78. }
  79. } else {
  80. generator.placeImage(x, y, w, h, xobj);
  81. }
  82. }
  83. /** {@inheritDoc} */
  84. public int getPriority() {
  85. return 100;
  86. }
  87. /** {@inheritDoc} */
  88. public Class getSupportedImageClass() {
  89. return ImageRawJPEG.class;
  90. }
  91. /** {@inheritDoc} */
  92. public ImageFlavor[] getSupportedImageFlavors() {
  93. return FLAVORS;
  94. }
  95. /** {@inheritDoc} */
  96. public boolean isCompatible(RenderingContext targetContext, Image image) {
  97. return (image == null || image instanceof ImageRawJPEG)
  98. && targetContext instanceof PDFRenderingContext;
  99. }
  100. }