All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.poi.hslf.record.PPDrawing Maven / Gradle / Ivy

There is a newer version: 5.2.5
Show newest version
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.hslf.record;

import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogger;

import org.apache.poi.ddf.*;
import org.apache.poi.hslf.model.ShapeTypes;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Vector;
import java.util.Iterator;

/**
 * These are actually wrappers onto Escher drawings. Make use of
 *  the DDF classes to do useful things with them.
 * For now, creates a tree of the Escher records, and then creates any
 *  PowerPoint (hslf) records found within the EscherTextboxRecord
 *  (msofbtClientTextbox) records.
 * Also provides easy access to the EscherTextboxRecords, so that their
 *  text may be extracted and used in Sheets
 *
 * @author Nick Burch
 */

// For now, pretending to be an atom. Might not always be, but that
//  would require a wrapping class
public final class PPDrawing extends RecordAtom {
	private byte[] _header;
	private long _type;

	private EscherRecord[] childRecords;
	private EscherTextboxWrapper[] textboxWrappers;

	//cached EscherDgRecord
	private EscherDgRecord dg;

	/**
	 * Get access to the underlying Escher Records
	 */
	public EscherRecord[] getEscherRecords() { return childRecords; }

	/**
	 * Get access to the atoms inside Textboxes
	 */
	public EscherTextboxWrapper[] getTextboxWrappers() { return textboxWrappers; }


	/* ******************** record stuff follows ********************** */

	/**
	 * Sets everything up, groks the escher etc
	 */
	protected PPDrawing(byte[] source, int start, int len) {
		// Get the header
		_header = new byte[8];
		System.arraycopy(source,start,_header,0,8);

		// Get the type
		_type = LittleEndian.getUShort(_header,2);

		// Get the contents for now
		byte[] contents = new byte[len];
		System.arraycopy(source,start,contents,0,len);


		// Build up a tree of Escher records contained within
		DefaultEscherRecordFactory erf = new DefaultEscherRecordFactory();
		Vector escherChildren = new Vector();
		findEscherChildren(erf,contents,8,len-8,escherChildren);

		childRecords = new EscherRecord[escherChildren.size()];
		for(int i=0; i= 8) {
			findEscherChildren(erf, source, startPos, lenToGo, found);
		}
	}

	/**
	 * Look for EscherTextboxRecords
	 */
	private void findEscherTextboxRecord(EscherRecord[] toSearch, Vector found) {
		for(int i=0; i= 0; j--) {
					if(toSearch[j] instanceof EscherSpRecord){
						EscherSpRecord sp = (EscherSpRecord)toSearch[j];
						w.setShapeId(sp.getShapeId());
						break;
					}
				}
			} else {
				// If it has children, walk them
				if(toSearch[i].isContainerRecord()) {
					List childrenL = toSearch[i].getChildRecords();
					EscherRecord[] children = new EscherRecord[childrenL.size()];
					childrenL.toArray(children);
					findEscherTextboxRecord(children,found);
				}
			}
		}
	}

	/**
	 * We are type 1036
	 */
	public long getRecordType() { return _type; }

	/**
	 * We're pretending to be an atom, so return null
	 */
	public Record[] getChildRecords() { return null; }

	/**
	 * Write the contents of the record back, so it can be written
	 *  to disk
	 * Walks the escher layer to get the contents
	 */
	public void writeOut(OutputStream out) throws IOException {
		// Ensure the escher layer reflects the text changes
		for(int i=0; iPPDrawing.
	 */
	public void addTextboxWrapper(EscherTextboxWrapper txtbox){
		EscherTextboxWrapper[] tw = new EscherTextboxWrapper[textboxWrappers.length + 1];
		System.arraycopy(textboxWrappers, 0, tw, 0, textboxWrappers.length);

		tw[textboxWrappers.length] = txtbox;
		textboxWrappers = tw;
	}

	/**
	 * Return EscherDgRecord which keeps track of the number of shapes and shapeId in this drawing group
	 *
	 * @return EscherDgRecord
	 */
	public EscherDgRecord getEscherDgRecord(){
		if(dg == null){
			EscherContainerRecord dgContainer = (EscherContainerRecord)childRecords[0];
			for(Iterator it = dgContainer.getChildIterator(); it.hasNext();){
				EscherRecord r = it.next();
				if(r instanceof EscherDgRecord){
					dg = (EscherDgRecord)r;
					break;
				}
			}
		}
		return dg;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy