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.

HemfComment.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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.record.emf;
  16. import java.awt.geom.Rectangle2D;
  17. import java.io.IOException;
  18. import java.nio.charset.StandardCharsets;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.function.Supplier;
  24. import org.apache.poi.hemf.draw.HemfGraphics;
  25. import org.apache.poi.hemf.record.emfplus.HemfPlusRecord;
  26. import org.apache.poi.hemf.record.emfplus.HemfPlusRecordIterator;
  27. import org.apache.poi.hwmf.usermodel.HwmfPicture;
  28. import org.apache.poi.util.IOUtils;
  29. import org.apache.poi.util.Internal;
  30. import org.apache.poi.util.LittleEndianConsts;
  31. import org.apache.poi.util.LittleEndianInputStream;
  32. import org.apache.poi.util.LocaleUtil;
  33. import org.apache.poi.util.RecordFormatException;
  34. /**
  35. * Contains arbitrary data
  36. */
  37. @Internal
  38. public class HemfComment {
  39. private static final int MAX_RECORD_LENGTH = HwmfPicture.MAX_RECORD_LENGTH;
  40. public enum HemfCommentRecordType {
  41. emfGeneric(-1, EmfCommentDataGeneric::new, false),
  42. emfSpool(0x00000000, EmfCommentDataGeneric::new, false),
  43. emfPlus(0x2B464D45, EmfCommentDataPlus::new, false),
  44. emfPublic(0x43494447, null, false),
  45. emfBeginGroup(0x00000002, EmfCommentDataBeginGroup::new, true),
  46. emfEndGroup(0x00000003, EmfCommentDataEndGroup::new, true),
  47. emfMultiFormats(0x40000004, EmfCommentDataMultiformats::new, true),
  48. emfWMF(0x80000001, EmfCommentDataWMF::new, true),
  49. emfUnicodeString(0x00000040, EmfCommentDataUnicode::new, true),
  50. emfUnicodeEnd(0x00000080, EmfCommentDataUnicode::new, true)
  51. ;
  52. public final long id;
  53. public final Supplier<? extends EmfCommentData> constructor;
  54. public final boolean isEmfPublic;
  55. HemfCommentRecordType(long id, Supplier<? extends EmfCommentData> constructor, boolean isEmfPublic) {
  56. this.id = id;
  57. this.constructor = constructor;
  58. this.isEmfPublic = isEmfPublic;
  59. }
  60. public static HemfCommentRecordType getById(long id, boolean isEmfPublic) {
  61. for (HemfCommentRecordType wrt : values()) {
  62. if (wrt.id == id && wrt.isEmfPublic == isEmfPublic) {
  63. return wrt;
  64. }
  65. }
  66. return emfGeneric;
  67. }
  68. }
  69. public interface EmfCommentData {
  70. HemfCommentRecordType getCommentRecordType();
  71. long init(LittleEndianInputStream leis, long dataSize) throws IOException;
  72. }
  73. public static class EmfComment implements HemfRecord {
  74. private EmfCommentData data;
  75. @Override
  76. public HemfRecordType getEmfRecordType() {
  77. return HemfRecordType.comment;
  78. }
  79. @Override
  80. public long init(LittleEndianInputStream leis, long recordSize, long recordId) throws IOException {
  81. int startIdx = leis.getReadIndex();
  82. data = new EmfCommentDataIterator(leis, (int)recordSize, true).next();
  83. return leis.getReadIndex()-startIdx;
  84. }
  85. public EmfCommentData getCommentData() {
  86. return data;
  87. }
  88. @Override
  89. public void draw(HemfGraphics ctx) {
  90. if (data instanceof EmfCommentDataPlus) {
  91. if (ctx.getRenderState() == HemfGraphics.EmfRenderState.INITIAL) {
  92. ctx.setRenderState(HemfGraphics.EmfRenderState.EMFPLUS_ONLY);
  93. }
  94. ((EmfCommentDataPlus)data).draw(ctx);
  95. }
  96. }
  97. @Override
  98. public String toString() {
  99. return "{ data: "+data+" }";
  100. }
  101. }
  102. public static class EmfCommentDataIterator implements Iterator<EmfCommentData> {
  103. private final LittleEndianInputStream leis;
  104. private final int startIdx;
  105. private final int limit;
  106. private EmfCommentData currentRecord;
  107. /** is the caller the EmfComment */
  108. private final boolean emfParent;
  109. public EmfCommentDataIterator(LittleEndianInputStream leis, int limit, boolean emfParent) {
  110. this.leis = leis;
  111. this.limit = limit;
  112. this.emfParent = emfParent;
  113. startIdx = leis.getReadIndex();
  114. //queue the first non-header record
  115. currentRecord = _next();
  116. }
  117. @Override
  118. public boolean hasNext() {
  119. return currentRecord != null;
  120. }
  121. @Override
  122. public EmfCommentData next() {
  123. EmfCommentData toReturn = currentRecord;
  124. final boolean isEOF = (limit == -1 || leis.getReadIndex() >= startIdx+limit);
  125. // (currentRecord instanceof HemfPlusMisc.EmfEof)
  126. currentRecord = isEOF ? null : _next();
  127. return toReturn;
  128. }
  129. private EmfCommentData _next() {
  130. long type, recordSize;
  131. if (currentRecord == null && emfParent) {
  132. type = HemfRecordType.comment.id;
  133. recordSize = limit;
  134. } else {
  135. // A 32-bit unsigned integer from the RecordType enumeration that identifies this record
  136. // as a comment record. This value MUST be 0x00000046.
  137. try {
  138. type = leis.readUInt();
  139. } catch (RuntimeException e) {
  140. // EOF
  141. return null;
  142. }
  143. assert(type == HemfRecordType.comment.id);
  144. // A 32-bit unsigned integer that specifies the size in bytes of this record in the
  145. // metafile. This value MUST be a multiple of 4 bytes.
  146. recordSize = leis.readUInt();
  147. }
  148. // A 32-bit unsigned integer that specifies the size, in bytes, of the CommentIdentifier and
  149. // CommentRecordParm fields in the RecordBuffer field that follows.
  150. // It MUST NOT include the size of itself or the size of the AlignmentPadding field, if present.
  151. long dataSize = leis.readUInt();
  152. try {
  153. leis.mark(2*LittleEndianConsts.INT_SIZE);
  154. // An optional, 32-bit unsigned integer that identifies the type of comment record.
  155. // See the preceding table for descriptions of these record types.
  156. // Valid comment identifier values are listed in the following table.
  157. //
  158. // If this field contains any other value, the comment record MUST be an EMR_COMMENT record
  159. final int commentIdentifier = (int)leis.readUInt();
  160. // A 32-bit unsigned integer that identifies the type of public comment record.
  161. final int publicCommentIdentifier = (int)leis.readUInt();
  162. final boolean isEmfPublic = (commentIdentifier == HemfCommentRecordType.emfPublic.id);
  163. leis.reset();
  164. final HemfCommentRecordType commentType = HemfCommentRecordType.getById
  165. (isEmfPublic ? publicCommentIdentifier : commentIdentifier, isEmfPublic);
  166. assert(commentType != null);
  167. final EmfCommentData record = commentType.constructor.get();
  168. long readBytes = record.init(leis, dataSize);
  169. final int skipBytes = (int)(recordSize-4-readBytes);
  170. assert (skipBytes >= 0);
  171. leis.skipFully(skipBytes);
  172. return record;
  173. } catch (IOException e) {
  174. throw new RecordFormatException(e);
  175. }
  176. }
  177. @Override
  178. public void remove() {
  179. throw new UnsupportedOperationException("Remove not supported");
  180. }
  181. }
  182. /**
  183. * Private data is unknown to EMF; it is meaningful only to applications that know the format of the
  184. * data and how to use it. EMR_COMMENT private data records MAY be ignored.
  185. */
  186. public static class EmfCommentDataGeneric implements EmfCommentData {
  187. private byte[] privateData;
  188. @Override
  189. public HemfCommentRecordType getCommentRecordType() {
  190. return HemfCommentRecordType.emfGeneric;
  191. }
  192. @Override
  193. public long init(LittleEndianInputStream leis, long dataSize) throws IOException {
  194. privateData = IOUtils.safelyAllocate(dataSize, MAX_RECORD_LENGTH);
  195. leis.readFully(privateData);
  196. return privateData.length;
  197. }
  198. public byte[] getPrivateData() {
  199. return privateData;
  200. }
  201. @Override
  202. public String toString() {
  203. return "\""+new String(privateData, LocaleUtil.CHARSET_1252).replaceAll("\\p{Cntrl}", ".")+"\"";
  204. }
  205. }
  206. /** The EMR_COMMENT_EMFPLUS record contains embedded EMF+ records. */
  207. public static class EmfCommentDataPlus implements EmfCommentData {
  208. private final List<HemfPlusRecord> records = new ArrayList<>();
  209. @Override
  210. public HemfCommentRecordType getCommentRecordType() {
  211. return HemfCommentRecordType.emfPlus;
  212. }
  213. @Override
  214. public long init(final LittleEndianInputStream leis, final long dataSize)
  215. throws IOException {
  216. long startIdx = leis.getReadIndex();
  217. int commentIdentifier = leis.readInt();
  218. assert (commentIdentifier == HemfCommentRecordType.emfPlus.id);
  219. new HemfPlusRecordIterator(leis, (int)dataSize-LittleEndianConsts.INT_SIZE).forEachRemaining(records::add);
  220. return leis.getReadIndex()-startIdx;
  221. }
  222. public List<HemfPlusRecord> getRecords() {
  223. return Collections.unmodifiableList(records);
  224. }
  225. public void draw(HemfGraphics ctx) {
  226. records.forEach(ctx::draw);
  227. }
  228. }
  229. public static class EmfCommentDataBeginGroup implements EmfCommentData {
  230. private final Rectangle2D bounds = new Rectangle2D.Double();
  231. private String description;
  232. @Override
  233. public HemfCommentRecordType getCommentRecordType() {
  234. return HemfCommentRecordType.emfBeginGroup;
  235. }
  236. @Override
  237. public long init(final LittleEndianInputStream leis, final long dataSize)
  238. throws IOException {
  239. final int startIdx = leis.getReadIndex();
  240. final int commentIdentifier = (int)leis.readUInt();
  241. assert(commentIdentifier == HemfCommentRecordType.emfPublic.id);
  242. final int publicCommentIdentifier = (int)leis.readUInt();
  243. assert(publicCommentIdentifier == HemfCommentRecordType.emfBeginGroup.id);
  244. HemfDraw.readRectL(leis, bounds);
  245. // The number of Unicode characters in the optional description string that follows.
  246. int nDescription = (int)leis.readUInt();
  247. byte[] buf = IOUtils.safelyAllocate(nDescription * 2L, MAX_RECORD_LENGTH);
  248. leis.readFully(buf);
  249. description = new String(buf, StandardCharsets.UTF_16LE);
  250. return leis.getReadIndex()-startIdx;
  251. }
  252. }
  253. public static class EmfCommentDataEndGroup implements EmfCommentData {
  254. @Override
  255. public HemfCommentRecordType getCommentRecordType() {
  256. return HemfCommentRecordType.emfEndGroup;
  257. }
  258. @Override
  259. public long init(final LittleEndianInputStream leis, final long dataSize)
  260. throws IOException {
  261. final int startIdx = leis.getReadIndex();
  262. final int commentIdentifier = (int)leis.readUInt();
  263. assert(commentIdentifier == HemfCommentRecordType.emfPublic.id);
  264. final int publicCommentIdentifier = (int)leis.readUInt();
  265. assert(publicCommentIdentifier == HemfCommentRecordType.emfEndGroup.id);
  266. return leis.getReadIndex()-startIdx;
  267. }
  268. }
  269. public static class EmfCommentDataMultiformats implements EmfCommentData {
  270. private final Rectangle2D bounds = new Rectangle2D.Double();
  271. private final List<EmfCommentDataFormat> formats = new ArrayList<>();
  272. @Override
  273. public HemfCommentRecordType getCommentRecordType() {
  274. return HemfCommentRecordType.emfMultiFormats;
  275. }
  276. @Override
  277. public long init(final LittleEndianInputStream leis, final long dataSize)
  278. throws IOException {
  279. final int startIdx = leis.getReadIndex();
  280. final int commentIdentifier = (int)leis.readUInt();
  281. assert(commentIdentifier == HemfCommentRecordType.emfPublic.id);
  282. final int publicCommentIdentifier = (int)leis.readUInt();
  283. assert(publicCommentIdentifier == HemfCommentRecordType.emfMultiFormats.id);
  284. HemfDraw.readRectL(leis, bounds);
  285. // A 32-bit unsigned integer that specifies the number of graphics formats contained in this record.
  286. int countFormats = (int)leis.readUInt();
  287. for (int i=0; i<countFormats; i++) {
  288. EmfCommentDataFormat fmt = new EmfCommentDataFormat();
  289. long readBytes = fmt.init(leis, dataSize, startIdx);
  290. formats.add(fmt);
  291. if (readBytes == 0) {
  292. // binary data is appended without DataFormat header
  293. break;
  294. }
  295. }
  296. for (EmfCommentDataFormat fmt : formats) {
  297. int skip = fmt.offData-(leis.getReadIndex()-startIdx);
  298. leis.skipFully(skip);
  299. fmt.rawData = IOUtils.safelyAllocate(fmt.sizeData, MAX_RECORD_LENGTH);
  300. int readBytes = leis.read(fmt.rawData);
  301. if (readBytes < fmt.sizeData) {
  302. // EOF
  303. break;
  304. }
  305. }
  306. return leis.getReadIndex()-startIdx;
  307. }
  308. public List<EmfCommentDataFormat> getFormats() {
  309. return Collections.unmodifiableList(formats);
  310. }
  311. }
  312. public enum EmfFormatSignature {
  313. /**
  314. * The value of this member is the sequence of ASCII characters "FME ",
  315. * which happens to be the reverse of the string "EMF", and it denotes EMF record data.
  316. */
  317. ENHMETA_SIGNATURE(0x464D4520),
  318. /**
  319. * The value of this member is the sequence of ASCII characters "FSPE", which happens to be the reverse
  320. * of the string "EPSF", and it denotes encapsulated PostScript (EPS) format data.
  321. */
  322. EPS_SIGNATURE(0x46535045);
  323. int id;
  324. EmfFormatSignature(int id) {
  325. this.id = id;
  326. }
  327. public static EmfFormatSignature getById(int id) {
  328. for (EmfFormatSignature wrt : values()) {
  329. if (wrt.id == id) {
  330. return wrt;
  331. }
  332. }
  333. return null;
  334. }
  335. }
  336. public static class EmfCommentDataFormat {
  337. private EmfFormatSignature signature;
  338. private int version;
  339. private int sizeData;
  340. private int offData;
  341. private byte[] rawData;
  342. public long init(final LittleEndianInputStream leis, final long dataSize, long startIdx) throws IOException {
  343. // A 32-bit unsigned integer that specifies the format of the image data.
  344. signature = EmfFormatSignature.getById(leis.readInt());
  345. // A 32-bit unsigned integer that specifies the format version number.
  346. // If the Signature field specifies encapsulated PostScript (EPS), this value MUST be 0x00000001;
  347. // otherwise, this value MUST be ignored.
  348. version = leis.readInt();
  349. // A 32-bit unsigned integer that specifies the size of the data in bytes.
  350. sizeData = leis.readInt();
  351. // A 32-bit unsigned integer that specifies the offset to the data from the start
  352. // of the identifier field in an EMR_COMMENT_PUBLIC record. The offset MUST be 32-bit aligned.
  353. offData = leis.readInt();
  354. if (sizeData < 0) {
  355. throw new RecordFormatException("size for emrformat must be > 0");
  356. }
  357. if (offData < 0) {
  358. throw new RecordFormatException("offset for emrformat must be > 0");
  359. }
  360. return 4*LittleEndianConsts.INT_SIZE;
  361. }
  362. public byte[] getRawData() {
  363. return rawData;
  364. }
  365. public EmfFormatSignature getSignature() {
  366. return signature;
  367. }
  368. }
  369. public static class EmfCommentDataWMF implements EmfCommentData {
  370. private final Rectangle2D bounds = new Rectangle2D.Double();
  371. private final List<EmfCommentDataFormat> formats = new ArrayList<>();
  372. private byte[] wmfData;
  373. @Override
  374. public HemfCommentRecordType getCommentRecordType() {
  375. return HemfCommentRecordType.emfWMF;
  376. }
  377. @Override
  378. public long init(final LittleEndianInputStream leis, final long dataSize) throws IOException {
  379. final int startIdx = leis.getReadIndex();
  380. final int commentIdentifier = (int)leis.readUInt();
  381. assert(commentIdentifier == HemfCommentRecordType.emfPublic.id);
  382. final int publicCommentIdentifier = (int)leis.readUInt();
  383. assert(publicCommentIdentifier == HemfCommentRecordType.emfWMF.id);
  384. // A 16-bit unsigned integer that specifies the WMF metafile version in terms
  385. //of support for device-independent bitmaps (DIBs)
  386. int version = leis.readUShort();
  387. // A 16-bit value that MUST be 0x0000 and MUST be ignored.
  388. leis.skipFully(LittleEndianConsts.SHORT_SIZE);
  389. // A 32-bit unsigned integer that specifies the checksum for this record.
  390. int checksum = leis.readInt();
  391. // A 32-bit value that MUST be 0x00000000 and MUST be ignored.
  392. int flags = leis.readInt();
  393. // A 32-bit unsigned integer that specifies the size, in bytes, of the
  394. // WMF metafile in the WinMetafile field.
  395. int winMetafileSize = (int)leis.readUInt();
  396. wmfData = IOUtils.safelyAllocate(winMetafileSize, MAX_RECORD_LENGTH);
  397. // some emf comments are truncated, so we don't use readFully here
  398. leis.read(wmfData);
  399. return leis.getReadIndex()-startIdx;
  400. }
  401. public byte[] getWMFData() {
  402. return wmfData;
  403. }
  404. }
  405. public static class EmfCommentDataUnicode implements EmfCommentData {
  406. private final Rectangle2D bounds = new Rectangle2D.Double();
  407. private final List<EmfCommentDataFormat> formats = new ArrayList<>();
  408. @Override
  409. public HemfCommentRecordType getCommentRecordType() {
  410. return HemfCommentRecordType.emfUnicodeString;
  411. }
  412. @Override
  413. public long init(final LittleEndianInputStream leis, final long dataSize)
  414. throws IOException {
  415. throw new RecordFormatException("UNICODE_STRING/UNICODE_END values are reserved in CommentPublic records");
  416. }
  417. }
  418. }