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.

ImageHeaderEMF.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.sl.image;
  16. import java.awt.Dimension;
  17. import java.awt.Rectangle;
  18. import org.apache.logging.log4j.LogManager;
  19. import org.apache.logging.log4j.Logger;
  20. import org.apache.poi.util.Internal;
  21. import org.apache.poi.util.LittleEndian;
  22. import org.apache.poi.util.LocaleUtil;
  23. @Internal
  24. public class ImageHeaderEMF {
  25. private static final Logger LOG = LogManager.getLogger(ImageHeaderEMF.class);
  26. private static final String EMF_SIGNATURE = " EMF"; // 0x464D4520 (LE)
  27. // rectangular inclusive-inclusive bounds, in device units, of the smallest
  28. // rectangle that can be drawn around the image stored in the metafile.
  29. private final Rectangle deviceBounds;
  30. public ImageHeaderEMF(final byte[] data, final int off) {
  31. int offset = off;
  32. int type = (int)LittleEndian.getUInt(data, offset); offset += 4;
  33. if (type != 1) {
  34. LOG.atWarn().log("Invalid EMF picture - invalid type");
  35. deviceBounds = new Rectangle(0,0,200,200);
  36. return;
  37. }
  38. // ignore header size
  39. offset += 4;
  40. int left = LittleEndian.getInt(data, offset); offset += 4;
  41. int top = LittleEndian.getInt(data, offset); offset += 4;
  42. int right = LittleEndian.getInt(data, offset); offset += 4;
  43. int bottom = LittleEndian.getInt(data, offset); offset += 4;
  44. deviceBounds = new Rectangle(left, top, right-left, bottom-top);
  45. // ignore frame bounds
  46. offset += 16;
  47. String signature = new String(data, offset, EMF_SIGNATURE.length(), LocaleUtil.CHARSET_1252);
  48. if (!EMF_SIGNATURE.equals(signature)) {
  49. LOG.atWarn().log("Invalid EMF picture - invalid signature");
  50. }
  51. }
  52. public Dimension getSize() {
  53. return deviceBounds.getSize();
  54. }
  55. public Rectangle getBounds() {
  56. return deviceBounds;
  57. }
  58. }