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.

DrawingSelectionRecord.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.hssf.record;
  16. import java.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.common.usermodel.GenericRecord;
  19. import org.apache.poi.util.GenericRecordUtil;
  20. import org.apache.poi.util.LittleEndianInput;
  21. import org.apache.poi.util.LittleEndianOutput;
  22. /**
  23. * MsoDrawingSelection (0x00ED)<p>
  24. * Reference:
  25. * [MS-OGRAPH].pdf sec 2.4.69
  26. */
  27. public final class DrawingSelectionRecord extends StandardRecord {
  28. public static final short sid = 0x00ED;
  29. /**
  30. * From [MS-ODRAW].pdf sec 2.2.1<p>
  31. * TODO - make EscherRecordHeader {@link LittleEndianInput} aware and refactor with this
  32. */
  33. private static final class OfficeArtRecordHeader implements GenericRecord {
  34. public static final int ENCODED_SIZE = 8;
  35. /**
  36. * lower 4 bits is 'version' usually 0x01 or 0x0F (for containers)
  37. * upper 12 bits is 'instance'
  38. */
  39. private final int _verAndInstance;
  40. /** value should be between 0xF000 and 0xFFFF */
  41. private final int _type;
  42. private final int _length;
  43. public OfficeArtRecordHeader(LittleEndianInput in) {
  44. _verAndInstance = in.readUShort();
  45. _type = in.readUShort();
  46. _length = in.readInt();
  47. }
  48. public void serialize(LittleEndianOutput out) {
  49. out.writeShort(_verAndInstance);
  50. out.writeShort(_type);
  51. out.writeInt(_length);
  52. }
  53. @Override
  54. public Map<String, Supplier<?>> getGenericProperties() {
  55. return GenericRecordUtil.getGenericProperties(
  56. "verAndInstance", () -> _verAndInstance,
  57. "type", () -> _type,
  58. "length", () -> _length
  59. );
  60. }
  61. }
  62. // [MS-OGRAPH].pdf says that the data of this record is an OfficeArtFDGSL structure
  63. // as described in[MS-ODRAW].pdf sec 2.2.33
  64. private OfficeArtRecordHeader _header;
  65. private int _cpsp;
  66. /** a MSODGSLK enum value for the current selection mode */
  67. private int _dgslk;
  68. private int _spidFocus;
  69. /** selected shape IDs (e.g. from EscherSpRecord.ShapeId) */
  70. private int[] _shapeIds;
  71. public DrawingSelectionRecord(RecordInputStream in) {
  72. _header = new OfficeArtRecordHeader(in);
  73. _cpsp = in.readInt();
  74. _dgslk = in.readInt();
  75. _spidFocus = in.readInt();
  76. int nShapes = in.available() / 4;
  77. int[] shapeIds = new int[nShapes];
  78. for (int i = 0; i < nShapes; i++) {
  79. shapeIds[i] = in.readInt();
  80. }
  81. _shapeIds = shapeIds;
  82. }
  83. public short getSid() {
  84. return sid;
  85. }
  86. protected int getDataSize() {
  87. return OfficeArtRecordHeader.ENCODED_SIZE
  88. + 12 // 3 int fields
  89. + _shapeIds.length * 4;
  90. }
  91. public void serialize(LittleEndianOutput out) {
  92. _header.serialize(out);
  93. out.writeInt(_cpsp);
  94. out.writeInt(_dgslk);
  95. out.writeInt(_spidFocus);
  96. for (int shapeId : _shapeIds) {
  97. out.writeInt(shapeId);
  98. }
  99. }
  100. @Override
  101. public DrawingSelectionRecord copy() {
  102. // currently immutable
  103. return this;
  104. }
  105. @Override
  106. public HSSFRecordTypes getGenericRecordType() {
  107. return HSSFRecordTypes.DRAWING_SELECTION;
  108. }
  109. @Override
  110. public Map<String, Supplier<?>> getGenericProperties() {
  111. return GenericRecordUtil.getGenericProperties(
  112. "rh", () -> _header,
  113. "cpsp", () -> _cpsp,
  114. "dgslk", () -> _dgslk,
  115. "spidFocus", () -> _spidFocus,
  116. "shapeIds", () -> _shapeIds
  117. );
  118. }
  119. }