1 /*
2  * $Id: HheaTable.java,v 1.4 2009-01-16 01:47:59 tomoke Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 package com.sun.pdfview.font.ttf;
23 
24 import java.nio.ByteBuffer;
25 
26 /**
27  *
28  * @author  jkaplan
29  */
30 public class HheaTable extends TrueTypeTable {
31 
32     /** Holds value of property version. */
33     private int version;
34 
35     /** Holds value of property ascent. */
36     private short ascent;
37 
38     /** Holds value of property descent. */
39     private short descent;
40 
41     /** Holds value of property lineGap. */
42     private short lineGap;
43 
44     /** Holds value of property advanceWidthMax. */
45     private short advanceWidthMax;
46 
47     /** Holds value of property minLeftSideBearing. */
48     private short minLeftSideBearing;
49 
50     /** Holds value of property minRightSideBearing. */
51     private short minRightSideBearing;
52 
53     /** Holds value of property xMaxExtent. */
54     private short xMaxExtent;
55 
56     /** Holds value of property caretSlopeRise. */
57     private short caretSlopeRise;
58 
59     /** Holds value of property caretSlopeRun. */
60     private short caretSlopeRun;
61 
62     /** Holds value of property caretOffset. */
63     private short caretOffset;
64 
65     /** Holds value of property metricDataFormat. */
66     private short metricDataFormat;
67 
68     /** Holds value of property numOfLongHorMetrics. */
69     private short numOfLongHorMetrics;
70 
71     /** Creates a new instance of HeadTable
72      * Makes up reasonable(?) defaults for all values
73      */
HheaTable()74     protected HheaTable() {
75         super(TrueTypeTable.HEAD_TABLE);
76 
77         setVersion(0x10000);
78     }
79 
80     /**
81      * Parse the data before it is set
82      */
setData(ByteBuffer data)83     public void setData(ByteBuffer data) {
84         if (data.remaining() != 36) {
85             throw new IllegalArgumentException("Bad Head table size");
86         }
87         setVersion(data.getInt());
88         setAscent(data.getShort());
89         setDescent(data.getShort());
90         setLineGap(data.getShort());
91         setAdvanceWidthMax(data.getShort());
92         setMinLeftSideBearing(data.getShort());
93         setMinRightSideBearing(data.getShort());
94         setXMaxExtent(data.getShort());
95         setCaretSlopeRise(data.getShort());
96         setCaretSlopeRun(data.getShort());
97         setCaretOffset(data.getShort());
98 
99         // padding
100         data.getShort();
101         data.getShort();
102         data.getShort();
103         data.getShort();
104 
105         setMetricDataFormat(data.getShort());
106         setNumOfLongHorMetrics(data.getShort());
107     }
108 
109     /**
110      * Get the data we have stored
111      */
getData()112     public ByteBuffer getData() {
113         ByteBuffer buf = ByteBuffer.allocate(getLength());
114 
115         buf.putInt(getVersion());
116         buf.putShort(getAscent());
117         buf.putShort(getDescent());
118         buf.putShort(getLineGap());
119         buf.putShort(getAdvanceWidthMax());
120         buf.putShort(getMinLeftSideBearing());
121         buf.putShort(getMinRightSideBearing());
122         buf.putShort(getXMaxExtent());
123         buf.putShort(getCaretSlopeRise());
124         buf.putShort(getCaretSlopeRun());
125         buf.putShort(getCaretOffset());
126 
127         // padding
128         buf.putShort((short) 0);
129         buf.putShort((short) 0);
130         buf.putShort((short) 0);
131         buf.putShort((short) 0);
132 
133         buf.putShort(getMetricDataFormat());
134         buf.putShort((short) getNumOfLongHorMetrics());
135 
136         // reset the position to the start of the buffer
137         buf.flip();
138 
139         return buf;
140     }
141 
142     /**
143      * Get the length of this table
144      */
getLength()145     public int getLength() {
146         return 36;
147     }
148 
149     /** Getter for property version.
150      * @return Value of property version.
151      *
152      */
getVersion()153     public int getVersion() {
154         return version;
155     }
156 
157     /** Setter for property version.
158      * @param version New value of property version.
159      *
160      */
setVersion(int version)161     public void setVersion(int version) {
162         this.version = version;
163     }
164 
165     /**
166      * Create a pretty string
167      */
toString()168     public String toString() {
169         StringBuffer buf = new StringBuffer();
170         String indent = "    ";
171 
172         buf.append(indent + "Version             : " + Integer.toHexString(getVersion()) + "\n");
173         buf.append(indent + "Ascent              : " + getAscent() + "\n");
174         buf.append(indent + "Descent             : " + getDescent() + "\n");
175         buf.append(indent + "LineGap             : " + getLineGap() + "\n");
176         buf.append(indent + "AdvanceWidthMax     : " + getAdvanceWidthMax() + "\n");
177         buf.append(indent + "MinLSB              : " + getMinLeftSideBearing() + "\n");
178         buf.append(indent + "MinRSB              : " + getMinRightSideBearing() + "\n");
179         buf.append(indent + "MaxExtent           : " + getXMaxExtent() + "\n");
180         buf.append(indent + "CaretSlopeRise      : " + getCaretSlopeRise() + "\n");
181         buf.append(indent + "CaretSlopeRun       : " + getCaretSlopeRun() + "\n");
182         buf.append(indent + "CaretOffset         : " + getCaretOffset() + "\n");
183         buf.append(indent + "MetricDataFormat    : " + getMetricDataFormat() + "\n");
184         buf.append(indent + "NumOfLongHorMetrics : " + getNumOfLongHorMetrics() + "\n");
185         return buf.toString();
186     }
187 
188     /** Getter for property ascent.
189      * @return Value of property ascent.
190      *
191      */
getAscent()192     public short getAscent() {
193         return this.ascent;
194     }
195 
196     /** Setter for property ascent.
197      * @param ascent New value of property ascent.
198      *
199      */
setAscent(short ascent)200     public void setAscent(short ascent) {
201         this.ascent = ascent;
202     }
203 
204     /** Getter for property descent.
205      * @return Value of property descent.
206      *
207      */
getDescent()208     public short getDescent() {
209         return this.descent;
210     }
211 
212     /** Setter for property descent.
213      * @param descent New value of property descent.
214      *
215      */
setDescent(short descent)216     public void setDescent(short descent) {
217         this.descent = descent;
218     }
219 
220     /** Getter for property lineGap.
221      * @return Value of property lineGap.
222      *
223      */
getLineGap()224     public short getLineGap() {
225         return this.lineGap;
226     }
227 
228     /** Setter for property lineGap.
229      * @param lineGap New value of property lineGap.
230      *
231      */
setLineGap(short lineGap)232     public void setLineGap(short lineGap) {
233         this.lineGap = lineGap;
234     }
235 
236     /** Getter for property advanceWidthMax.
237      * @return Value of property advanceWidthMax.
238      *
239      */
getAdvanceWidthMax()240     public short getAdvanceWidthMax() {
241         return this.advanceWidthMax;
242     }
243 
244     /** Setter for property advanceWidthMax.
245      * @param advanceWidthMax New value of property advanceWidthMax.
246      *
247      */
setAdvanceWidthMax(short advanceWidthMax)248     public void setAdvanceWidthMax(short advanceWidthMax) {
249         this.advanceWidthMax = advanceWidthMax;
250     }
251 
252     /** Getter for property minLeftSideBearing.
253      * @return Value of property minLeftSideBearing.
254      *
255      */
getMinLeftSideBearing()256     public short getMinLeftSideBearing() {
257         return this.minLeftSideBearing;
258     }
259 
260     /** Setter for property minLeftSideBearing.
261      * @param minLeftSideBearing New value of property minLeftSideBearing.
262      *
263      */
setMinLeftSideBearing(short minLeftSideBearing)264     public void setMinLeftSideBearing(short minLeftSideBearing) {
265         this.minLeftSideBearing = minLeftSideBearing;
266     }
267 
268     /** Getter for property minRIghtSideBearing.
269      * @return Value of property minRIghtSideBearing.
270      *
271      */
getMinRightSideBearing()272     public short getMinRightSideBearing() {
273         return this.minRightSideBearing;
274     }
275 
276     /** Setter for property minRIghtSideBearing.
277      * @param minRightSideBearing New value of property minRIghtSideBearing.
278      *
279      */
setMinRightSideBearing(short minRightSideBearing)280     public void setMinRightSideBearing(short minRightSideBearing) {
281         this.minRightSideBearing = minRightSideBearing;
282     }
283 
284     /** Getter for property xMaxExtent.
285      * @return Value of property xMaxExtent.
286      *
287      */
getXMaxExtent()288     public short getXMaxExtent() {
289         return this.xMaxExtent;
290     }
291 
292     /** Setter for property xMaxExtent.
293      * @param xMaxExtent New value of property xMaxExtent.
294      *
295      */
setXMaxExtent(short xMaxExtent)296     public void setXMaxExtent(short xMaxExtent) {
297         this.xMaxExtent = xMaxExtent;
298     }
299 
300     /** Getter for property caretSlopeRise.
301      * @return Value of property caretSlopeRise.
302      *
303      */
getCaretSlopeRise()304     public short getCaretSlopeRise() {
305         return this.caretSlopeRise;
306     }
307 
308     /** Setter for property caretSlopeRise.
309      * @param caretSlopeRise New value of property caretSlopeRise.
310      *
311      */
setCaretSlopeRise(short caretSlopeRise)312     public void setCaretSlopeRise(short caretSlopeRise) {
313         this.caretSlopeRise = caretSlopeRise;
314     }
315 
316     /** Getter for property caretSlopeRun.
317      * @return Value of property caretSlopeRun.
318      *
319      */
getCaretSlopeRun()320     public short getCaretSlopeRun() {
321         return this.caretSlopeRun;
322     }
323 
324     /** Setter for property caretSlopeRun.
325      * @param caretSlopeRun New value of property caretSlopeRun.
326      *
327      */
setCaretSlopeRun(short caretSlopeRun)328     public void setCaretSlopeRun(short caretSlopeRun) {
329         this.caretSlopeRun = caretSlopeRun;
330     }
331 
332     /** Getter for property caretOffset.
333      * @return Value of property caretOffset.
334      *
335      */
getCaretOffset()336     public short getCaretOffset() {
337         return this.caretOffset;
338     }
339 
340     /** Setter for property caretOffset.
341      * @param caretOffset New value of property caretOffset.
342      *
343      */
setCaretOffset(short caretOffset)344     public void setCaretOffset(short caretOffset) {
345         this.caretOffset = caretOffset;
346     }
347 
348     /** Getter for property metricDataFormat.
349      * @return Value of property metricDataFormat.
350      *
351      */
getMetricDataFormat()352     public short getMetricDataFormat() {
353         return this.metricDataFormat;
354     }
355 
356     /** Setter for property metricDataFormat.
357      * @param metricDataFormat New value of property metricDataFormat.
358      *
359      */
setMetricDataFormat(short metricDataFormat)360     public void setMetricDataFormat(short metricDataFormat) {
361         this.metricDataFormat = metricDataFormat;
362     }
363 
364     /** Getter for property numOfLongHorMetrics.
365      * @return Value of property numOfLongHorMetrics.
366      *
367      */
getNumOfLongHorMetrics()368     public int getNumOfLongHorMetrics() {
369         return this.numOfLongHorMetrics & 0xFFFF;
370     }
371 
372     /** Setter for property numOfLongHorMetrics.
373      * @param numOfLongHorMetrics New value of property numOfLongHorMetrics.
374      *
375      */
setNumOfLongHorMetrics(short numOfLongHorMetrics)376     public void setNumOfLongHorMetrics(short numOfLongHorMetrics) {
377         this.numOfLongHorMetrics = numOfLongHorMetrics;
378     }
379 
380 }
381