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

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