001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.svggen.font.table;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.IOException;
023: import java.io.RandomAccessFile;
024:
025: /**
026: * @version $Id: HmtxTable.java 489226 2006-12-21 00:05:36Z cam $
027: * @author <a href="mailto:david@steadystate.co.uk">David Schweinsberg</a>
028: */
029: public class HmtxTable implements Table {
030:
031: private byte[] buf = null;
032: private int[] hMetrics = null;
033: private short[] leftSideBearing = null;
034:
035: protected HmtxTable(DirectoryEntry de, RandomAccessFile raf)
036: throws IOException {
037: raf.seek(de.getOffset());
038: buf = new byte[de.getLength()];
039: raf.read(buf);
040: /*
041: TableMaxp t_maxp = (TableMaxp) td.getEntryByTag(maxp).getTable();
042: TableHhea t_hhea = (TableHhea) td.getEntryByTag(hhea).getTable();
043: int lsbCount = t_maxp.getNumGlyphs() - t_hhea.getNumberOfHMetrics();
044: hMetrics = new int[t_hhea.getNumberOfHMetrics()];
045: for (int i = 0; i < t_hhea.getNumberOfHMetrics(); i++) {
046: hMetrics[i] = raf.readInt();
047: }
048: if (lsbCount > 0) {
049: leftSideBearing = new short[lsbCount];
050: for (int i = 0; i < lsbCount; i++) {
051: leftSideBearing[i] = raf.readShort();
052: }
053: }
054: */
055: }
056:
057: public void init(int numberOfHMetrics, int lsbCount) {
058: if (buf == null) {
059: return;
060: }
061: hMetrics = new int[numberOfHMetrics];
062: ByteArrayInputStream bais = new ByteArrayInputStream(buf);
063: for (int i = 0; i < numberOfHMetrics; i++) {
064: // pack 4 bytes from bais into an int and store in hMetrics[]
065: // bais.read() returns an int 0..255, so no need to worry for sign-extension here
066: hMetrics[i] = (bais.read() << 24 | bais.read() << 16
067: | bais.read() << 8 | bais.read());
068: }
069: if (lsbCount > 0) {
070: leftSideBearing = new short[lsbCount];
071: for (int i = 0; i < lsbCount; i++) {
072: leftSideBearing[i] = (short) (bais.read() << 8 | bais
073: .read());
074: }
075: }
076: buf = null;
077: }
078:
079: public int getAdvanceWidth(int i) {
080: if (hMetrics == null) {
081: return 0;
082: }
083: if (i < hMetrics.length) {
084: return hMetrics[i] >> 16;
085: } else {
086: return hMetrics[hMetrics.length - 1] >> 16;
087: }
088: }
089:
090: public short getLeftSideBearing(int i) {
091: if (hMetrics == null) {
092: return 0;
093: }
094: if (i < hMetrics.length) {
095: return (short) (hMetrics[i] & 0xffff);
096: } else {
097: return leftSideBearing[i - hMetrics.length];
098: }
099: }
100:
101: public int getType() {
102: return hmtx;
103: }
104: }
|