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.

HemfPicture.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.hwmf.usermodel.HwmfEmbedded;
  33. import org.apache.poi.util.Dimension2DDouble;
  34. import org.apache.poi.util.Internal;
  35. import org.apache.poi.util.LittleEndianInputStream;
  36. import org.apache.poi.util.Units;
  37. /**
  38. * Read-only EMF extractor. Lots remain
  39. */
  40. @Internal
  41. public class HemfPicture implements Iterable<HemfRecord> {
  42. private final LittleEndianInputStream stream;
  43. private final List<HemfRecord> records = new ArrayList<>();
  44. private boolean isParsed = false;
  45. public HemfPicture(InputStream is) throws IOException {
  46. this(new LittleEndianInputStream(is));
  47. }
  48. public HemfPicture(LittleEndianInputStream is) throws IOException {
  49. stream = is;
  50. }
  51. public HemfHeader getHeader() {
  52. return (HemfHeader)getRecords().get(0);
  53. }
  54. public List<HemfRecord> getRecords() {
  55. if (!isParsed) {
  56. // in case the (first) parsing throws an exception, we can provide the
  57. // records up to that point
  58. isParsed = true;
  59. HemfHeader[] header = new HemfHeader[1];
  60. new HemfRecordIterator(stream).forEachRemaining(r -> {
  61. if (r instanceof HemfHeader) {
  62. header[0] = (HemfHeader) r;
  63. }
  64. r.setHeader(header[0]);
  65. records.add(r);
  66. });
  67. }
  68. return records;
  69. }
  70. @Override
  71. public Iterator<HemfRecord> iterator() {
  72. return getRecords().iterator();
  73. }
  74. @Override
  75. public Spliterator<HemfRecord> spliterator() {
  76. return getRecords().spliterator();
  77. }
  78. @Override
  79. public void forEach(Consumer<? super HemfRecord> action) {
  80. getRecords().forEach(action);
  81. }
  82. /**
  83. * Returns the bounding box in device-independent units. Usually this is taken from the placeable header.
  84. *
  85. * @return the bounding box
  86. */
  87. public Rectangle2D getBounds() {
  88. HemfHeader header = (HemfHeader)getRecords().get(0);
  89. Rectangle2D dim = header.getFrameRectangle();
  90. double x = dim.getX(), y = dim.getY();
  91. double width = dim.getWidth(), height = dim.getHeight();
  92. if (dim.isEmpty() || Math.rint(width) == 0 || Math.rint(height) == 0) {
  93. for (HemfRecord r : getRecords()) {
  94. if (r instanceof HemfWindowing.EmfSetWindowExtEx) {
  95. HemfWindowing.EmfSetWindowExtEx extEx = (HemfWindowing.EmfSetWindowExtEx)r;
  96. Dimension2D d = extEx.getSize();
  97. width = d.getWidth();
  98. height = d.getHeight();
  99. // keep searching - sometimes there's another record
  100. }
  101. if (r instanceof HemfWindowing.EmfSetWindowOrgEx) {
  102. HemfWindowing.EmfSetWindowOrgEx orgEx = (HemfWindowing.EmfSetWindowOrgEx)r;
  103. x = orgEx.getX();
  104. y = orgEx.getY();
  105. }
  106. }
  107. }
  108. return new Rectangle2D.Double(x, y, width, height);
  109. }
  110. /**
  111. * Return the image size in points
  112. *
  113. * @return the image size in points
  114. */
  115. public Dimension2D getSize() {
  116. final Rectangle2D bounds = getBounds();
  117. if (bounds.isEmpty()) {
  118. return new Dimension2DDouble(100,100);
  119. }
  120. final double coeff = (double) Units.EMU_PER_CENTIMETER / Units.EMU_PER_POINT / 10.;
  121. double width = Math.abs(bounds.getWidth()*coeff);
  122. double height = Math.abs(bounds.getHeight()*coeff);
  123. return new Dimension2DDouble(width, height);
  124. }
  125. private static double minX(Rectangle2D bounds) {
  126. return Math.min(bounds.getMinX(), bounds.getMaxX());
  127. }
  128. private static double minY(Rectangle2D bounds) {
  129. return Math.min(bounds.getMinY(), bounds.getMaxY());
  130. }
  131. public void draw(Graphics2D ctx, Rectangle2D graphicsBounds) {
  132. HemfHeader header = (HemfHeader)getRecords().get(0);
  133. AffineTransform at = ctx.getTransform();
  134. try {
  135. Rectangle2D emfBounds = header.getBoundsRectangle();
  136. // scale output bounds to image bounds
  137. ctx.translate(minX(graphicsBounds), minY(graphicsBounds));
  138. ctx.scale(graphicsBounds.getWidth()/emfBounds.getWidth(), graphicsBounds.getHeight()/emfBounds.getHeight());
  139. ctx.translate(-minX(emfBounds), -minY(emfBounds));
  140. int idx = 0;
  141. HemfGraphics g = new HemfGraphics(ctx, emfBounds);
  142. for (HemfRecord r : getRecords()) {
  143. try {
  144. g.draw(r);
  145. } catch (RuntimeException ignored) {
  146. }
  147. idx++;
  148. }
  149. } finally {
  150. ctx.setTransform(at);
  151. }
  152. }
  153. public Iterable<HwmfEmbedded> getEmbeddings() {
  154. return () -> new HemfEmbeddedIterator(HemfPicture.this);
  155. }
  156. }