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.StringUtil;
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.io.UnsupportedEncodingException;
025:
026: /**
027: * This atom corresponds exactly to a Windows Logical Font (LOGFONT) structure.
028: * It keeps all the information needed to define the attributes of a font,
029: * such as height, width, etc. For more information, consult the
030: * Windows API Programmer's reference.
031: *
032: * @author Yegor Kozlov
033: */
034:
035: public class FontEntityAtom extends RecordAtom {
036: /**
037: * record header
038: */
039: private byte[] _header;
040:
041: /**
042: * record data
043: */
044: private byte[] _recdata;
045:
046: /**
047: * Build an instance of <code>FontEntityAtom</code> from on-disk data
048: */
049: protected FontEntityAtom(byte[] source, int start, int len) {
050: // Get the header
051: _header = new byte[8];
052: System.arraycopy(source, start, _header, 0, 8);
053:
054: // Grab the record data
055: _recdata = new byte[len - 8];
056: System.arraycopy(source, start + 8, _recdata, 0, len - 8);
057: }
058:
059: /**
060: * Create a new instance of <code>FontEntityAtom</code>
061: */
062: protected FontEntityAtom() {
063: _recdata = new byte[68];
064:
065: _header = new byte[8];
066: LittleEndian.putShort(_header, 2, (short) getRecordType());
067: LittleEndian.putInt(_header, 4, _recdata.length);
068: }
069:
070: public long getRecordType() {
071: return RecordTypes.FontEntityAtom.typeID;
072: }
073:
074: /**
075: * A null-terminated string that specifies the typeface name of the font.
076: * The length of this string must not exceed 32 characters
077: * including the null terminator.
078: * @return font name
079: */
080: public String getFontName() {
081: String name = null;
082: try {
083: int i = 0;
084: while (i < 64) {
085: //loop until find null-terminated end of the font name
086: if (_recdata[i] == 0 && _recdata[i + 1] == 0) {
087: name = new String(_recdata, 0, i, "UTF-16LE");
088: break;
089: }
090: i += 2;
091: }
092: } catch (UnsupportedEncodingException e) {
093: throw new RuntimeException(e.getMessage(), e);
094: }
095: return name;
096: }
097:
098: /**
099: * Set the name of the font.
100: * The length of this string must not exceed 32 characters
101: * including the null terminator.
102: * Will be converted to null-terminated if not already
103: * @param name of the font
104: */
105: public void setFontName(String name) {
106: // Add a null termination if required
107: if (!name.endsWith("\000")) {
108: name = name + "\000";
109: }
110:
111: // Ensure it's not now too long
112: if (name.length() > 32) {
113: throw new RuntimeException(
114: "The length of the font name, including null termination, must not exceed 32 characters");
115: }
116:
117: // Everything's happy, so save the name
118: try {
119: byte[] bytes = name.getBytes("UTF-16LE");
120: System.arraycopy(bytes, 0, _recdata, 0, bytes.length);
121: } catch (UnsupportedEncodingException e) {
122: throw new RuntimeException(e.getMessage(), e);
123: }
124: }
125:
126: protected void setFontIndex(int idx) {
127: LittleEndian.putShort(_header, 0, (short) idx);
128: }
129:
130: protected int getFontIndex() {
131: return LittleEndian.getShort(_header, 0);
132: }
133:
134: /**
135: * Write the contents of the record back, so it can be written to disk
136: */
137: public void writeOut(OutputStream out) throws IOException {
138: out.write(_header);
139: out.write(_recdata);
140: }
141: }
|