Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

AbstractPDFImageHandler.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.Rectangle;
  20. import java.io.IOException;
  21. import org.apache.xmlgraphics.image.loader.Image;
  22. import org.apache.fop.pdf.PDFImage;
  23. import org.apache.fop.pdf.PDFXObject;
  24. import org.apache.fop.render.ImageHandler;
  25. import org.apache.fop.render.RenderingContext;
  26. import org.apache.fop.render.pdf.PDFLogicalStructureHandler.MarkedContentInfo;
  27. /**
  28. * A partial implementation of a PDF-specific image handler, containing the code that is
  29. * common between image flavors.
  30. */
  31. abstract class AbstractPDFImageHandler implements ImageHandler {
  32. /** {@inheritDoc} */
  33. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  34. throws IOException {
  35. assert context instanceof PDFRenderingContext;
  36. PDFRenderingContext pdfContext = (PDFRenderingContext)context;
  37. PDFContentGenerator generator = pdfContext.getGenerator();
  38. PDFImage pdfimage = createPDFImage(image, image.getInfo().getOriginalURI());
  39. PDFXObject xobj = generator.getDocument().addImage(
  40. generator.getResourceContext(), pdfimage);
  41. float x = (float)pos.getX() / 1000f;
  42. float y = (float)pos.getY() / 1000f;
  43. float w = (float)pos.getWidth() / 1000f;
  44. float h = (float)pos.getHeight() / 1000f;
  45. if (context.getUserAgent().isAccessibilityEnabled()) {
  46. MarkedContentInfo mci = pdfContext.getMarkedContentInfo();
  47. generator.placeImage(x, y, w, h, xobj, mci.tag, mci.mcid);
  48. } else {
  49. generator.placeImage(x, y, w, h, xobj);
  50. }
  51. }
  52. /**
  53. * Creates a PDF image object out of the given image.
  54. *
  55. * @param image an image
  56. * @param xobjectKey a key for retrieval of the image from the document's XObject collection
  57. * @return a suitable {@link PDFImage} implementation that can handle the flavour of
  58. * the given image
  59. */
  60. abstract PDFImage createPDFImage(Image image, String xobjectKey);
  61. }