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.

HemfPalette.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.io.IOException;
  17. import org.apache.poi.hemf.draw.HemfGraphics;
  18. import org.apache.poi.hwmf.record.HwmfPalette;
  19. import org.apache.poi.util.LittleEndianConsts;
  20. import org.apache.poi.util.LittleEndianInputStream;
  21. public class HemfPalette {
  22. /** The EMR_SELECTPALETTE record specifies a logical palette for the playback device context. */
  23. public static class EmfSelectPalette extends HwmfPalette.WmfSelectPalette implements HemfRecord {
  24. @Override
  25. public HemfRecordType getEmfRecordType() {
  26. return HemfRecordType.selectPalette;
  27. }
  28. @Override
  29. public long init(LittleEndianInputStream leis, long recordSize, long recordId) throws IOException {
  30. /*
  31. * A 32-bit unsigned integer that specifies either the index of a LogPalette object
  32. * in the EMF Object Table or the value DEFAULT_PALETTE, which is the index
  33. * of a stock object palette from the StockObject enumeration
  34. */
  35. paletteIndex = (int)leis.readUInt();
  36. return LittleEndianConsts.INT_SIZE;
  37. }
  38. }
  39. /** The EMR_CREATEPALETTE record defines a logical palette for graphics operations. */
  40. public static class EmfCreatePalette extends HwmfPalette.WmfCreatePalette implements HemfRecord {
  41. protected int paletteIndex;
  42. @Override
  43. public HemfRecordType getEmfRecordType() {
  44. return HemfRecordType.createPalette;
  45. }
  46. @Override
  47. public long init(LittleEndianInputStream leis, long recordSize, long recordId) throws IOException {
  48. start = 0x0300;
  49. /* A 32-bit unsigned integer that specifies the index of the logical palette object
  50. * in the EMF Object Table. This index MUST be saved so that this object can be
  51. * reused or modified.
  52. */
  53. paletteIndex = (int)leis.readUInt();
  54. /* A 16-bit unsigned integer that specifies the version number of the system. This MUST be 0x0300. */
  55. int version = leis.readUShort();
  56. assert(version == 0x0300);
  57. int size = readPaletteEntries(leis, -1);
  58. return size + LittleEndianConsts.INT_SIZE + LittleEndianConsts.SHORT_SIZE;
  59. }
  60. @Override
  61. public void draw(HemfGraphics ctx) {
  62. ctx.addObjectTableEntry(this, paletteIndex);
  63. }
  64. }
  65. /**
  66. * The EMR_SETPALETTEENTRIES record defines RGB color values in a range of entries for an existing
  67. * LogPalette object.
  68. */
  69. public static class EmfSetPaletteEntries extends HwmfPalette.WmfSetPaletteEntries implements HemfRecord {
  70. /** specifies the palette EMF Object Table index. */
  71. int paletteIndex;
  72. @Override
  73. public HemfRecordType getEmfRecordType() {
  74. return HemfRecordType.setPaletteEntries;
  75. }
  76. @Override
  77. public long init(LittleEndianInputStream leis, long recordSize, long recordId) throws IOException {
  78. // A 32-bit unsigned integer that specifies the palette EMF Object Table index.
  79. paletteIndex = (int)leis.readUInt();
  80. // A 32-bit unsigned integer that specifies the index of the first entry to set.
  81. start = (int)leis.readUInt();
  82. // A 32-bit unsigned integer that specifies the number of entries.
  83. int nbrOfEntries = (int)leis.readUInt();
  84. int size = readPaletteEntries(leis, nbrOfEntries);
  85. return size + 3*LittleEndianConsts.INT_SIZE;
  86. }
  87. @Override
  88. public void draw(HemfGraphics ctx) {
  89. ctx.addObjectTableEntry(this, paletteIndex);
  90. }
  91. }
  92. /**
  93. * The EMR_RESIZEPALETTE record increases or decreases the size of an existing LogPalette object
  94. */
  95. public static class EmfResizePalette extends HwmfPalette.WmfResizePalette implements HemfRecord {
  96. /** specifies the palette EMF Object Table index. */
  97. int paletteIndex;
  98. @Override
  99. public HemfRecordType getEmfRecordType() {
  100. return HemfRecordType.resizePalette;
  101. }
  102. @Override
  103. public long init(LittleEndianInputStream leis, long recordSize, long recordId) throws IOException {
  104. // A 32-bit unsigned integer that specifies the index of the palette object in the EMF Object Table
  105. paletteIndex = (int)leis.readUInt();
  106. // A 32-bit unsigned integer that specifies the number of entries in the palette after resizing.
  107. // The value MUST be less than or equal to 0x00000400 and greater than 0x00000000.
  108. numberOfEntries = (int)leis.readUInt();
  109. return 2*LittleEndianConsts.INT_SIZE;
  110. }
  111. @Override
  112. public void draw(HemfGraphics ctx) {
  113. ctx.addObjectTableEntry(this, paletteIndex);
  114. }
  115. }
  116. /**
  117. * This record maps palette entries from the current LogPalette object to the system_palette.
  118. * This EMF record specifies no parameters.
  119. */
  120. public static class EmfRealizePalette extends HwmfPalette.WmfRealizePalette implements HemfRecord {
  121. @Override
  122. public HemfRecordType getEmfRecordType() {
  123. return HemfRecordType.realizePalette;
  124. }
  125. @Override
  126. public long init(LittleEndianInputStream leis, long recordSize, long recordId) throws IOException {
  127. return 0;
  128. }
  129. }
  130. }