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.

HemfPicture.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hemf.usermodel;
  16. import java.awt.Graphics2D;
  17. import java.awt.geom.AffineTransform;
  18. import java.awt.geom.Dimension2D;
  19. import java.awt.geom.Rectangle2D;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.util.ArrayList;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Spliterator;
  26. import java.util.function.Consumer;
  27. import org.apache.poi.hemf.draw.HemfGraphics;
  28. import org.apache.poi.hemf.record.emf.HemfHeader;
  29. import org.apache.poi.hemf.record.emf.HemfRecord;
  30. import org.apache.poi.hemf.record.emf.HemfRecordIterator;
  31. import org.apache.poi.hemf.record.emf.HemfWindowing;
  32. import org.apache.poi.util.Dimension2DDouble;
  33. import org.apache.poi.util.Internal;
  34. import org.apache.poi.util.LittleEndianInputStream;
  35. import org.apache.poi.util.Units;
  36. /**
  37. * Read-only EMF extractor. Lots remain
  38. */
  39. @Internal
  40. public class HemfPicture implements Iterable<HemfRecord> {
  41. private final LittleEndianInputStream stream;
  42. private final List<HemfRecord> records = new ArrayList<>();
  43. private boolean isParsed = false;
  44. public HemfPicture(InputStream is) throws IOException {
  45. this(new LittleEndianInputStream(is));
  46. }
  47. public HemfPicture(LittleEndianInputStream is) throws IOException {
  48. stream = is;
  49. }
  50. public HemfHeader getHeader() {
  51. return (HemfHeader)getRecords().get(0);
  52. }
  53. public List<HemfRecord> getRecords() {
  54. if (!isParsed) {
  55. // in case the (first) parsing throws an exception, we can provide the
  56. // records up to that point
  57. isParsed = true;
  58. HemfHeader[] header = new HemfHeader[1];
  59. new HemfRecordIterator(stream).forEachRemaining(r -> {
  60. if (r instanceof HemfHeader) {
  61. header[0] = (HemfHeader) r;
  62. }
  63. r.setHeader(header[0]);
  64. records.add(r);
  65. });
  66. }
  67. return records;
  68. }
  69. @Override
  70. public Iterator<HemfRecord> iterator() {
  71. return getRecords().iterator();
  72. }
  73. @Override
  74. public Spliterator<HemfRecord> spliterator() {
  75. return getRecords().spliterator();
  76. }
  77. @Override
  78. public void forEach(Consumer<? super HemfRecord> action) {
  79. getRecords().forEach(action);
  80. }
  81. /**
  82. * Return the image size in points
  83. *
  84. * @return the image size in points
  85. */
  86. public Dimension2D getSize() {
  87. HemfHeader header = (HemfHeader)getRecords().get(0);
  88. final double coeff = (double) Units.EMU_PER_CENTIMETER / Units.EMU_PER_POINT / 10.;
  89. Rectangle2D dim = header.getFrameRectangle();
  90. double width = dim.getWidth(), height = dim.getHeight();
  91. if (dim.isEmpty() || Math.rint(width*coeff) == 0 || Math.rint(height*coeff) == 0) {
  92. for (HemfRecord r : getRecords()) {
  93. if (r instanceof HemfWindowing.EmfSetWindowExtEx) {
  94. Dimension2D d = ((HemfWindowing.EmfSetWindowExtEx)r).getSize();
  95. width = d.getWidth();
  96. height = d.getHeight();
  97. // keep searching - sometimes there's another record
  98. }
  99. }
  100. }
  101. if (Math.rint(width*coeff) == 0 || Math.rint(height*coeff) == 0) {
  102. width = 100;
  103. height = 100;
  104. }
  105. return new Dimension2DDouble(Math.abs(width*coeff), Math.abs(height*coeff));
  106. }
  107. private static double minX(Rectangle2D bounds) {
  108. return Math.min(bounds.getMinX(), bounds.getMaxX());
  109. }
  110. private static double minY(Rectangle2D bounds) {
  111. return Math.min(bounds.getMinY(), bounds.getMaxY());
  112. }
  113. public void draw(Graphics2D ctx, Rectangle2D graphicsBounds) {
  114. HemfHeader header = (HemfHeader)getRecords().get(0);
  115. AffineTransform at = ctx.getTransform();
  116. try {
  117. Rectangle2D emfBounds = header.getBoundsRectangle();
  118. // scale output bounds to image bounds
  119. ctx.translate(minX(graphicsBounds), minY(graphicsBounds));
  120. ctx.scale(graphicsBounds.getWidth()/emfBounds.getWidth(), graphicsBounds.getHeight()/emfBounds.getHeight());
  121. ctx.translate(-minX(emfBounds), -minY(emfBounds));
  122. int idx = 0;
  123. HemfGraphics g = new HemfGraphics(ctx, emfBounds);
  124. for (HemfRecord r : getRecords()) {
  125. try {
  126. g.draw(r);
  127. } catch (RuntimeException ignored) {
  128. }
  129. idx++;
  130. }
  131. } finally {
  132. ctx.setTransform(at);
  133. }
  134. }
  135. }