• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.typography.font.sfntly.table;
18 
19 import com.google.typography.font.sfntly.data.ReadableFontData;
20 import com.google.typography.font.sfntly.data.WritableFontData;
21 
22 /**
23  * An abstract base class for subtables. Subtables are smaller tables nested
24  * within other tables and don't have an entry in the main font index. Examples
25  * of these are the CMap subtables within CMap table (cmap) or a glyph within
26  * the glyph table (glyf).
27  *
28  * @author Stuart Gill
29  *
30  */
31 public abstract class SubTable extends FontDataTable {
32   /**
33    * The data for the whole table in which this subtable is contained.
34    */
35   private final ReadableFontData masterData;
36 
37   private int padding = 0;
38 
39   /**
40    * Constructor.
41    *
42    * @param data the data representing the subtable
43    * @param masterData the data representing the full table containing this
44    *        subtable
45    */
SubTable(ReadableFontData data, ReadableFontData masterData)46   protected SubTable(ReadableFontData data, ReadableFontData masterData) {
47     super(data);
48     this.masterData = masterData;
49   }
50 
51   /**
52    * Constructor.
53    *
54    * @param data the data representing the subtable
55    */
SubTable(ReadableFontData data)56   protected SubTable(ReadableFontData data) {
57     this(data, null);
58   }
59 
60   /**
61    * Constructor.
62    *
63    * @param data the data object that contains the subtable
64    * @param offset the offset within the data where the subtable starts
65    * @param length the length of the subtable data within the data object
66    */
SubTable(ReadableFontData data, int offset, int length)67   protected SubTable(ReadableFontData data, int offset, int length) {
68     this(data.slice(offset, length));
69   }
70 
masterReadData()71   protected ReadableFontData masterReadData() {
72     return this.masterData;
73   }
74 
75   /**
76    * An abstract base class for subtable builders.
77    *
78    * @param <T> the type of the subtable
79    */
80   protected abstract static class Builder<T extends SubTable> extends FontDataTable.Builder<T> {
81     private ReadableFontData masterData;
82 
83     /**
84      * Constructor.
85      *
86      * @param data the data for the subtable being built
87      * @param masterData the data for the full table
88      */
Builder(WritableFontData data, ReadableFontData masterData)89     protected Builder(WritableFontData data, ReadableFontData masterData) {
90       super(data);
91       this.masterData = masterData;
92     }
93 
94     /**
95      * Constructor.
96      *
97      * @param data the data for the subtable being built
98      * @param masterData the data for the full table
99      */
Builder(ReadableFontData data, ReadableFontData masterData)100     protected Builder(ReadableFontData data, ReadableFontData masterData) {
101       super(data);
102       this.masterData = masterData;
103     }
104 
105     /**
106      * Constructor.
107      *
108      * @param data the data for the subtable being built
109      */
Builder(WritableFontData data)110     protected Builder(WritableFontData data) {
111       super(data);
112     }
113 
114     /**
115      * Constructor.
116      *
117      * @param data the data for the subtable being built
118      */
Builder(ReadableFontData data)119     protected Builder(ReadableFontData data) {
120       super(data);
121     }
122 
123     /**
124      * Constructor.
125      *
126      * Creates a new empty sub-table.
127      *
128      * @param dataSize the initial size for the data; if it is positive then the
129      *        size is fixed; if it is negative then it is variable sized
130      */
Builder(int dataSize)131     protected Builder(int dataSize) {
132       super(dataSize);
133     }
134 
masterReadData()135     protected ReadableFontData masterReadData() {
136       return this.masterData;
137     }
138   }
139 
140   /**
141    * Get the number of bytes of padding used in the table. The padding bytes are
142    * used to align the table length to a 4 byte boundary.
143    *
144    * @return the number of padding bytes
145    */
padding()146   public int padding() {
147     return this.padding;
148   }
149 
150   /**
151    * Sets the amount of padding that is part of the data being used by this
152    * subtable.
153    *
154    * @param padding
155    */
156   // TODO(stuartg): move to constructor
setPadding(int padding)157   protected void setPadding(int padding) {
158     this.padding = padding;
159   }
160 }