001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.font.truetype;
031:
032: import java.io.IOException;
033:
034: import de.intarsys.tools.randomaccess.IRandomAccess;
035: import de.intarsys.tools.randomaccess.RandomAccessByteArray;
036: import de.intarsys.tools.randomaccess.RandomAccessViewport;
037: import de.intarsys.tools.stream.StreamTools;
038:
039: /**
040: *
041: */
042: public class TTTable {
043: private byte[] name;
044:
045: private int checksum = 0;
046:
047: private long offset;
048:
049: private long length;
050:
051: private TTFont font;
052:
053: protected TTTable(TTFont font, long offset, long length) {
054: super ();
055: this .font = font;
056: this .offset = offset;
057: this .length = length;
058: }
059:
060: public int getChecksum() {
061: if (checksum == 0) {
062: try {
063: checksum = createChecksum();
064: } catch (IOException e) {
065: //
066: }
067: }
068: return checksum;
069: }
070:
071: public void setName(byte[] string) {
072: name = string;
073: }
074:
075: public byte[] getName() {
076: return name;
077: }
078:
079: protected void setChecksum(int i) {
080: checksum = i;
081: }
082:
083: private byte[] bytes;
084:
085: protected void setBytes(byte[] pBytes) {
086: bytes = pBytes;
087: checksum = 0;
088: length = pBytes.length;
089: offset = 0;
090: }
091:
092: protected IRandomAccess getRandomAccess() throws IOException {
093: if (bytes != null) {
094: return new RandomAccessByteArray(bytes);
095: }
096: IRandomAccess random = getFont().getLocator().getRandomAccess();
097: IRandomAccess viewport = new RandomAccessViewport(random,
098: getOffset(), getLength());
099: return viewport;
100: }
101:
102: public TTFont getFont() {
103: return font;
104: }
105:
106: public long getLength() {
107: return length;
108: }
109:
110: public long getOffset() {
111: return offset;
112: }
113:
114: protected int createChecksum() throws IOException {
115: IRandomAccess random = getRandomAccess();
116: try {
117: random.seek(0);
118: long len = random.getLength() / 4;
119: int v0 = 0;
120: int v1 = 0;
121: int v2 = 0;
122: int v3 = 0;
123: for (int i = 0; i < len; i++) {
124: v3 += random.read();
125: v2 += random.read();
126: v1 += random.read();
127: v0 += random.read();
128: }
129: return v0 + (v1 << 8) + (v2 << 16) + (v3 << 24);
130: } finally {
131: StreamTools.close(random);
132: }
133: }
134:
135: public String toString() {
136: return new String(getName());
137: }
138: }
|