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.

OfficeDrawingsImpl.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.hwpf.usermodel;
  16. import java.util.ArrayList;
  17. import java.util.Collection;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import org.apache.poi.ddf.DefaultEscherRecordFactory;
  21. import org.apache.poi.ddf.EscherBSERecord;
  22. import org.apache.poi.ddf.EscherBlipRecord;
  23. import org.apache.poi.ddf.EscherContainerRecord;
  24. import org.apache.poi.ddf.EscherOptRecord;
  25. import org.apache.poi.ddf.EscherPropertyTypes;
  26. import org.apache.poi.ddf.EscherRecord;
  27. import org.apache.poi.ddf.EscherRecordFactory;
  28. import org.apache.poi.ddf.EscherSimpleProperty;
  29. import org.apache.poi.ddf.EscherSpRecord;
  30. import org.apache.poi.ddf.EscherTertiaryOptRecord;
  31. import org.apache.poi.hwpf.model.EscherRecordHolder;
  32. import org.apache.poi.hwpf.model.FSPA;
  33. import org.apache.poi.hwpf.model.FSPATable;
  34. public class OfficeDrawingsImpl implements OfficeDrawings
  35. {
  36. private final EscherRecordHolder _escherRecordHolder;
  37. private final FSPATable _fspaTable;
  38. private final byte[] _mainStream;
  39. public OfficeDrawingsImpl( FSPATable fspaTable,
  40. EscherRecordHolder escherRecordHolder, byte[] mainStream )
  41. {
  42. this._fspaTable = fspaTable;
  43. this._escherRecordHolder = escherRecordHolder;
  44. this._mainStream = mainStream;
  45. }
  46. private EscherBlipRecord getBitmapRecord( int bitmapIndex )
  47. {
  48. List<? extends EscherContainerRecord> bContainers = _escherRecordHolder
  49. .getBStoreContainers();
  50. if ( bContainers == null || bContainers.size() != 1 )
  51. return null;
  52. EscherContainerRecord bContainer = bContainers.get( 0 );
  53. final List<EscherRecord> bitmapRecords = bContainer.getChildRecords();
  54. if ( bitmapRecords.size() < bitmapIndex )
  55. return null;
  56. EscherRecord imageRecord = bitmapRecords.get( bitmapIndex - 1 );
  57. if ( imageRecord instanceof EscherBlipRecord )
  58. {
  59. return (EscherBlipRecord) imageRecord;
  60. }
  61. if ( imageRecord instanceof EscherBSERecord )
  62. {
  63. EscherBSERecord bseRecord = (EscherBSERecord) imageRecord;
  64. EscherBlipRecord blip = bseRecord.getBlipRecord();
  65. if ( blip != null )
  66. {
  67. return blip;
  68. }
  69. if ( bseRecord.getOffset() > 0 )
  70. {
  71. /*
  72. * Blip stored in delay stream, which in a word doc, is the main
  73. * stream
  74. */
  75. EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
  76. EscherRecord record = recordFactory.createRecord( _mainStream,
  77. bseRecord.getOffset() );
  78. if ( record instanceof EscherBlipRecord )
  79. {
  80. record.fillFields( _mainStream, bseRecord.getOffset(),
  81. recordFactory );
  82. return (EscherBlipRecord) record;
  83. }
  84. }
  85. }
  86. return null;
  87. }
  88. private EscherContainerRecord getEscherShapeRecordContainer(
  89. final int shapeId )
  90. {
  91. for ( EscherContainerRecord spContainer : _escherRecordHolder
  92. .getSpContainers() )
  93. {
  94. EscherSpRecord escherSpRecord = spContainer
  95. .getChildById( (short) 0xF00A );
  96. if ( escherSpRecord != null
  97. && escherSpRecord.getShapeId() == shapeId )
  98. return spContainer;
  99. }
  100. return null;
  101. }
  102. private OfficeDrawing getOfficeDrawing( final FSPA fspa )
  103. {
  104. return new OfficeDrawing()
  105. {
  106. public HorizontalPositioning getHorizontalPositioning()
  107. {
  108. int value = getTertiaryPropertyValue(EscherPropertyTypes.GROUPSHAPE__POSH );
  109. switch ( value )
  110. {
  111. case 0:
  112. return HorizontalPositioning.ABSOLUTE;
  113. case 1:
  114. return HorizontalPositioning.LEFT;
  115. case 2:
  116. return HorizontalPositioning.CENTER;
  117. case 3:
  118. return HorizontalPositioning.RIGHT;
  119. case 4:
  120. return HorizontalPositioning.INSIDE;
  121. case 5:
  122. return HorizontalPositioning.OUTSIDE;
  123. }
  124. return HorizontalPositioning.ABSOLUTE;
  125. }
  126. public HorizontalRelativeElement getHorizontalRelative()
  127. {
  128. int value = getTertiaryPropertyValue( EscherPropertyTypes.GROUPSHAPE__POSRELH );
  129. switch ( value )
  130. {
  131. case 1:
  132. return HorizontalRelativeElement.MARGIN;
  133. case 2:
  134. return HorizontalRelativeElement.PAGE;
  135. case 3:
  136. return HorizontalRelativeElement.TEXT;
  137. case 4:
  138. return HorizontalRelativeElement.CHAR;
  139. }
  140. return HorizontalRelativeElement.TEXT;
  141. }
  142. public EscherContainerRecord getOfficeArtSpContainer()
  143. {
  144. return getEscherShapeRecordContainer( getShapeId() );
  145. }
  146. public byte[] getPictureData()
  147. {
  148. EscherContainerRecord shapeDescription = getEscherShapeRecordContainer( getShapeId() );
  149. if ( shapeDescription == null )
  150. return null;
  151. EscherOptRecord escherOptRecord = shapeDescription
  152. .getChildById( EscherOptRecord.RECORD_ID );
  153. if ( escherOptRecord == null )
  154. return null;
  155. EscherSimpleProperty escherProperty = escherOptRecord
  156. .lookup( EscherPropertyTypes.BLIP__BLIPTODISPLAY );
  157. if ( escherProperty == null )
  158. return null;
  159. int bitmapIndex = escherProperty.getPropertyValue();
  160. EscherBlipRecord escherBlipRecord = getBitmapRecord( bitmapIndex );
  161. if ( escherBlipRecord == null )
  162. return null;
  163. return escherBlipRecord.getPicturedata();
  164. }
  165. public int getRectangleBottom()
  166. {
  167. return fspa.getYaBottom();
  168. }
  169. public int getRectangleLeft()
  170. {
  171. return fspa.getXaLeft();
  172. }
  173. public int getRectangleRight()
  174. {
  175. return fspa.getXaRight();
  176. }
  177. public int getRectangleTop()
  178. {
  179. return fspa.getYaTop();
  180. }
  181. public int getShapeId()
  182. {
  183. return fspa.getSpid();
  184. }
  185. private int getTertiaryPropertyValue( EscherPropertyTypes type ) {
  186. EscherContainerRecord shapeDescription = getEscherShapeRecordContainer( getShapeId() );
  187. if ( shapeDescription == null ) {
  188. return -1;
  189. }
  190. EscherTertiaryOptRecord escherTertiaryOptRecord = shapeDescription
  191. .getChildById( EscherTertiaryOptRecord.RECORD_ID );
  192. if ( escherTertiaryOptRecord == null ) {
  193. return -1;
  194. }
  195. EscherSimpleProperty escherProperty = escherTertiaryOptRecord.lookup( type );
  196. return ( escherProperty == null ) ? -1 : escherProperty.getPropertyValue();
  197. }
  198. public VerticalPositioning getVerticalPositioning()
  199. {
  200. int value = getTertiaryPropertyValue( EscherPropertyTypes.GROUPSHAPE__POSV );
  201. switch ( value )
  202. {
  203. case 0:
  204. return VerticalPositioning.ABSOLUTE;
  205. case 1:
  206. return VerticalPositioning.TOP;
  207. case 2:
  208. return VerticalPositioning.CENTER;
  209. case 3:
  210. return VerticalPositioning.BOTTOM;
  211. case 4:
  212. return VerticalPositioning.INSIDE;
  213. case 5:
  214. return VerticalPositioning.OUTSIDE;
  215. }
  216. return VerticalPositioning.ABSOLUTE;
  217. }
  218. public VerticalRelativeElement getVerticalRelativeElement()
  219. {
  220. int value = getTertiaryPropertyValue( EscherPropertyTypes.GROUPSHAPE__POSV );
  221. switch ( value )
  222. {
  223. case 1:
  224. return VerticalRelativeElement.MARGIN;
  225. case 2:
  226. return VerticalRelativeElement.PAGE;
  227. case 3:
  228. return VerticalRelativeElement.TEXT;
  229. case 4:
  230. return VerticalRelativeElement.LINE;
  231. }
  232. return VerticalRelativeElement.TEXT;
  233. }
  234. @Override
  235. public String toString()
  236. {
  237. return "OfficeDrawingImpl: " + fspa;
  238. }
  239. };
  240. }
  241. public OfficeDrawing getOfficeDrawingAt( int characterPosition )
  242. {
  243. final FSPA fspa = _fspaTable.getFspaFromCp( characterPosition );
  244. if ( fspa == null )
  245. return null;
  246. return getOfficeDrawing( fspa );
  247. }
  248. public Collection<OfficeDrawing> getOfficeDrawings()
  249. {
  250. List<OfficeDrawing> result = new ArrayList<>();
  251. for ( FSPA fspa : _fspaTable.getShapes() )
  252. {
  253. result.add( getOfficeDrawing( fspa ) );
  254. }
  255. return Collections.unmodifiableList( result );
  256. }
  257. }