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$ */
19 package org.apache.fop.fonts.truetype;
20 
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 
24 public class OTFSubSetWriter extends OTFFile {
25     protected int currentPos;
26     protected ByteArrayOutputStream output = new ByteArrayOutputStream();
27 
OTFSubSetWriter()28     public OTFSubSetWriter() throws IOException {
29         super();
30     }
31 
concatArray(byte[] a, byte[] b)32     public static byte[] concatArray(byte[] a, byte[] b) {
33         int aLen = a.length;
34         int bLen = b.length;
35         byte[] c = new byte[aLen + bLen];
36         System.arraycopy(a, 0, c, 0, aLen);
37         System.arraycopy(b, 0, c, aLen, bLen);
38         return c;
39     }
40 
41     /**
42      * Appends a byte to the output array,
43      * updates currentPost but not realSize
44      */
writeByte(int b)45     protected void writeByte(int b) {
46         output.write(b);
47         currentPos++;
48     }
49 
50     /**
51      * Appends a USHORT to the output array,
52      * updates currentPost but not realSize
53      */
writeCard16(int s)54     protected void writeCard16(int s) {
55         byte b1 = (byte)((s >> 8) & 0xff);
56         byte b2 = (byte)(s & 0xff);
57         writeByte(b1);
58         writeByte(b2);
59     }
60 
writeThreeByteNumber(int s)61     protected void writeThreeByteNumber(int s) {
62         byte b1 = (byte)((s >> 16) & 0xFF);
63         byte b2 = (byte)((s >> 8) & 0xFF);
64         byte b3 = (byte)(s & 0xFF);
65         writeByte(b1);
66         writeByte(b2);
67         writeByte(b3);
68     }
69 
70     /**
71      * Appends a ULONG to the output array,
72      * at the given position
73      */
writeULong(int s)74     protected void writeULong(int s) {
75         byte b1 = (byte)((s >> 24) & 0xff);
76         byte b2 = (byte)((s >> 16) & 0xff);
77         byte b3 = (byte)((s >> 8) & 0xff);
78         byte b4 = (byte)(s & 0xff);
79         writeByte(b1);
80         writeByte(b2);
81         writeByte(b3);
82         writeByte(b4);
83     }
84 
85 
writeBytes(byte[] out)86     protected void writeBytes(byte[] out) {
87         for (byte anOut : out) {
88             writeByte(anOut);
89         }
90     }
91 
92     /**
93      * Returns a subset of the fonts (readFont() MUST be called first in order to create the
94      * subset).
95      * @return byte array
96      */
getFontSubset()97     public byte[] getFontSubset() {
98         return output.toByteArray();
99     }
100 }
101