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  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: PDFTTFStream.java 1305467 2012-03-26 17:39:20Z vhennebert $ */
19 
20 package org.apache.fop.pdf;
21 
22 import java.io.IOException;
23 import java.io.OutputStream;
24 
25 /**
26  * Special PDFStream for embeddable TrueType fonts.
27  */
28 public class PDFTTFStream extends AbstractPDFFontStream {
29 
30     private int origLength;
31     private byte[] ttfData;
32 
33     /**
34      * Main constructor
35      * @param len original length
36      */
PDFTTFStream(int len)37     public PDFTTFStream(int len) {
38         super();
39         origLength = len;
40     }
41 
42     /** {@inheritDoc} */
getSizeHint()43     protected int getSizeHint() throws IOException {
44         if (this.ttfData != null) {
45             return ttfData.length;
46         } else {
47             return 0; //no hint available
48         }
49     }
50 
51     /**
52      * Overload the base object method so we don't have to copy
53      * byte arrays around so much
54      * {@inheritDoc}
55      */
output(java.io.OutputStream stream)56     public int output(java.io.OutputStream stream)
57             throws java.io.IOException {
58         if (log.isDebugEnabled()) {
59             log.debug("Writing " + origLength + " bytes of TTF font data");
60         }
61 
62         int length = super.output(stream);
63         log.debug("Embedded TrueType/OpenType font");
64         return length;
65     }
66 
67     /** {@inheritDoc} */
outputRawStreamData(OutputStream out)68     protected void outputRawStreamData(OutputStream out) throws IOException {
69         out.write(this.ttfData);
70     }
71 
72     /** {@inheritDoc} */
populateStreamDict(Object lengthEntry)73     protected void populateStreamDict(Object lengthEntry) {
74         put("Length1", origLength);
75         super.populateStreamDict(lengthEntry);
76     }
77 
78     /**
79      * Sets the TrueType font data.
80      * @param data the font payload
81      * @param size size of the payload
82      * @throws IOException in case of an I/O problem
83      */
setData(byte[] data, int size)84     public void setData(byte[] data, int size) throws IOException {
85         this.ttfData = new byte[size];
86         System.arraycopy(data, 0, this.ttfData, 0, size);
87     }
88 
89 }
90