001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hslf.record;
019:
020: import org.apache.poi.util.LittleEndian;
021: import org.apache.poi.util.POILogger;
022:
023: import java.io.*;
024: import java.util.*;
025:
026: /**
027: * <code>FontCollection</code> ia a container that holds information
028: * about all the fonts in the presentation.
029: *
030: * @author Yegor Kozlov
031: */
032:
033: public class FontCollection extends RecordContainer {
034: private List fonts;
035: private byte[] _header;
036:
037: protected FontCollection(byte[] source, int start, int len) {
038: // Grab the header
039: _header = new byte[8];
040: System.arraycopy(source, start, _header, 0, 8);
041:
042: _children = Record.findChildRecords(source, start + 8, len - 8);
043:
044: // Save font names into <code>List</code>
045: fonts = new ArrayList();
046: for (int i = 0; i < _children.length; i++) {
047: if (_children[i] instanceof FontEntityAtom) {
048: FontEntityAtom atom = (FontEntityAtom) _children[i];
049: fonts.add(atom.getFontName());
050: } else {
051: logger.log(POILogger.WARN,
052: "Warning: FontCollection child wasn't a FontEntityAtom, was "
053: + _children[i]);
054: }
055: }
056: }
057:
058: /**
059: * Return the type, which is 2005
060: */
061: public long getRecordType() {
062: return RecordTypes.FontCollection.typeID;
063: }
064:
065: /**
066: * Write the contents of the record back, so it can be written
067: * to disk
068: */
069: public void writeOut(OutputStream out) throws IOException {
070: writeOut(_header[0], _header[1], getRecordType(), _children,
071: out);
072: }
073:
074: /**
075: * Add font with the specified name to the font collection.
076: * If the font is already present return its index.
077: * @param name of the font
078: * @return zero based index of the font in the collection
079: */
080: public int addFont(String name) {
081: for (int i = 0; i < fonts.size(); i++) {
082: if (fonts.get(i).equals(name)) {
083: //if the font is already present return its index
084: return i;
085: }
086: }
087:
088: FontEntityAtom fnt = new FontEntityAtom();
089: fnt.setFontIndex(fonts.size() << 4);
090: fnt.setFontName(name);
091: fonts.add(name);
092:
093: // Append new child to the end
094: appendChildRecord(fnt);
095:
096: return fonts.size() - 1; //the added font is the last in the list
097: }
098:
099: /**
100: * Get the name of the font at the given ID, or null if there is
101: * no font at that ID.
102: * @param id
103: */
104: public String getFontWithId(int id) {
105: if (id >= fonts.size()) {
106: // No font with that id
107: return null;
108: }
109: return (String) fonts.get(id);
110: }
111: }
|