1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
/*
* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.fonts;
import java.io.*;
import java.util.Iterator;
import java.util.HashMap;
import java.util.ArrayList;
/**
* Reads a TrueType file and generates a subset
* That can be used to embed a TrueType CID font
* TrueType tables needed for embedded CID fonts are:
* "head", "hhea", "loca", "maxp", "cvt ", "prep", "glyf", "hmtx" and "fpgm"
* The TrueType spec can be found at the Microsoft
* Typography site: http://www.microsoft.com/truetype/
*/
public class TTFSubSetFile extends TTFFile {
byte[] output = null;
int realSize = 0;
int currentPos = 0;
/*
* Offsets in name table to be filled out by table.
* The offsets are to the checkSum field
*/
int cvtDirOffset = 0;
int fpgmDirOffset = 0;
int glyfDirOffset = 0;
int headDirOffset = 0;
int hheaDirOffset = 0;
int hmtxDirOffset = 0;
int locaDirOffset = 0;
int maxpDirOffset = 0;
int prepDirOffset = 0;
int checkSumAdjustmentOffset = 0;
int locaOffset = 0;
/**
* Initalize the output array
*/
private void init(int size) {
output = new byte[size];
realSize = 0;
currentPos = 0;
// createDirectory()
}
/**
* Create the directory table
*/
private void createDirectory() {
int numTables = 9;
// Create the TrueType header
writeByte((byte)0);
writeByte((byte)1);
writeByte((byte)0);
writeByte((byte)0);
realSize += 4;
writeUShort(numTables);
realSize += 2;
// Create searchRange, entrySelector and rangeShift
int maxPow = maxPow2(numTables);
int searchRange = maxPow * 16;
writeUShort(searchRange);
realSize += 2;
writeUShort(maxPow);
realSize += 2;
writeUShort((numTables * 16) - searchRange);
realSize += 2;
// Create space for the table entries
writeString("cvt ");
cvtDirOffset = currentPos;
currentPos += 12;
realSize += 16;
writeString("fpgm");
fpgmDirOffset = currentPos;
currentPos += 12;
realSize += 16;
writeString("glyf");
glyfDirOffset = currentPos;
currentPos += 12;
realSize += 16;
writeString("head");
headDirOffset = currentPos;
currentPos += 12;
realSize += 16;
writeString("hhea");
hheaDirOffset = currentPos;
currentPos += 12;
realSize += 16;
writeString("hmtx");
hmtxDirOffset = currentPos;
currentPos += 12;
realSize += 16;
writeString("loca");
locaDirOffset = currentPos;
currentPos += 12;
realSize += 16;
writeString("maxp");
maxpDirOffset = currentPos;
currentPos += 12;
realSize += 16;
writeString("prep");
prepDirOffset = currentPos;
currentPos += 12;
realSize += 16;
}
/**
* Copy the cvt table as is from original font to subset font
*/
private void createCvt(FontFileReader in) throws IOException {
TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("cvt ");
if (entry != null) {
pad4();
seek_tab(in, "cvt ", 0);
System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
0, output, currentPos, (int)entry.length);
int checksum = getCheckSum(currentPos, (int)entry.length);
writeULong(cvtDirOffset, checksum);
writeULong(cvtDirOffset + 4, currentPos);
writeULong(cvtDirOffset + 8, (int)entry.length);
currentPos += (int)entry.length;
realSize += (int)entry.length;
} else {
throw new IOException("Can't find cvt table");
}
}
/**
* Copy the fpgm table as is from original font to subset font
*/
private void createFpgm(FontFileReader in) throws IOException {
TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("fpgm");
if (entry != null) {
pad4();
seek_tab(in, "fpgm", 0);
System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
0, output, currentPos, (int)entry.length);
int checksum = getCheckSum(currentPos, (int)entry.length);
writeULong(fpgmDirOffset, checksum);
writeULong(fpgmDirOffset + 4, currentPos);
writeULong(fpgmDirOffset + 8, (int)entry.length);
currentPos += (int)entry.length;
realSize += (int)entry.length;
} else {
throw new IOException("Can't find fpgm table");
}
}
/**
* Create an empty loca table without updating checksum
*/
private void createLoca(int size) throws IOException {
pad4();
locaOffset = currentPos;
writeULong(locaDirOffset + 4, currentPos);
writeULong(locaDirOffset + 8, size * 4 + 4);
currentPos += size * 4 + 4;
realSize += size * 4 + 4;
}
/**
* Copy the maxp table as is from original font to subset font
* and set num glyphs to size
*/
private void createMaxp(FontFileReader in, int size) throws IOException {
TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("maxp");
if (entry != null) {
pad4();
seek_tab(in, "maxp", 0);
System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
0, output, currentPos, (int)entry.length);
writeUShort(currentPos + 4, size);
int checksum = getCheckSum(currentPos, (int)entry.length);
writeULong(maxpDirOffset, checksum);
writeULong(maxpDirOffset + 4, currentPos);
writeULong(maxpDirOffset + 8, (int)entry.length);
currentPos += (int)entry.length;
realSize += (int)entry.length;
} else {
throw new IOException("Can't find maxp table");
}
}
/**
* Copy the prep table as is from original font to subset font
*/
private void createPrep(FontFileReader in) throws IOException {
TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("prep");
if (entry != null) {
pad4();
seek_tab(in, "prep", 0);
System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
0, output, currentPos, (int)entry.length);
int checksum = getCheckSum(currentPos, (int)entry.length);
writeULong(prepDirOffset, checksum);
writeULong(prepDirOffset + 4, currentPos);
writeULong(prepDirOffset + 8, (int)entry.length);
currentPos += (int)entry.length;
realSize += (int)entry.length;
} else {
throw new IOException("Can't find prep table");
}
}
/**
* Copy the hhea table as is from original font to subset font
* and fill in size of hmtx table
*/
private void createHhea(FontFileReader in, int size) throws IOException {
TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("hhea");
if (entry != null) {
pad4();
seek_tab(in, "hhea", 0);
System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
0, output, currentPos, (int)entry.length);
writeUShort((int)entry.length + currentPos - 2, size);
int checksum = getCheckSum(currentPos, (int)entry.length);
writeULong(hheaDirOffset, checksum);
writeULong(hheaDirOffset + 4, currentPos);
writeULong(hheaDirOffset + 8, (int)entry.length);
currentPos += (int)entry.length;
realSize += (int)entry.length;
} else {
throw new IOException("Can't find hhea table");
}
}
/**
* Copy the head table as is from original font to subset font
* and set indexToLocaFormat to long and set
* checkSumAdjustment to 0, store offset to checkSumAdjustment
* in checkSumAdjustmentOffset
*/
private void createHead(FontFileReader in) throws IOException {
TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("head");
if (entry != null) {
pad4();
seek_tab(in, "head", 0);
System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
0, output, currentPos, (int)entry.length);
checkSumAdjustmentOffset = currentPos + 8;
output[currentPos + 8] = 0; // Set checkSumAdjustment to 0
output[currentPos + 9] = 0;
output[currentPos + 10] = 0;
output[currentPos + 11] = 0;
output[currentPos + 50] = 0; // long locaformat
output[currentPos + 51] = 1; // long locaformat
int checksum = getCheckSum(currentPos, (int)entry.length);
writeULong(headDirOffset, checksum);
writeULong(headDirOffset + 4, currentPos);
writeULong(headDirOffset + 8, (int)entry.length);
currentPos += (int)entry.length;
realSize += (int)entry.length;
} else {
throw new IOException("Can't find head table");
}
}
/**
* Create the glyf table and fill in loca table
*/
private void createGlyf(FontFileReader in,
HashMap glyphs) throws IOException {
TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("glyf");
int size = 0;
int start = 0;
int endOffset = 0; // Store this as the last loca
if (entry != null) {
pad4();
start = currentPos;
for (Iterator e = glyphs.keySet().iterator(); e.hasNext(); ) {
int glyphLength = 0;
Integer origIndex = (Integer)e.next();
Integer subsetIndex = (Integer)glyphs.get(origIndex);
int nextOffset = 0;
if (origIndex.intValue() >= (mtx_tab.length - 1))
nextOffset = (int)lastLoca;
else
nextOffset =
(int)mtx_tab[origIndex.intValue() + 1].offset;
glyphLength = nextOffset
- (int)mtx_tab[origIndex.intValue()].offset;
// Copy glyph
System.arraycopy(in.getBytes((int)entry.offset + (int)mtx_tab[origIndex.intValue()].offset, glyphLength),
0, output, currentPos, glyphLength);
// Update loca table
writeULong(locaOffset + subsetIndex.intValue() * 4,
currentPos - start);
if ((currentPos - start + glyphLength) > endOffset)
endOffset = (currentPos - start + glyphLength);
currentPos += glyphLength;
realSize += glyphLength;
}
size = currentPos - start;
int checksum = getCheckSum(start, size);
writeULong(glyfDirOffset, checksum);
writeULong(glyfDirOffset + 4, start);
writeULong(glyfDirOffset + 8, size);
currentPos += 12;
realSize += 12;
// Update loca checksum and last loca index
writeULong(locaOffset + glyphs.size() * 4, endOffset);
checksum = getCheckSum(locaOffset, glyphs.size() * 4 + 4);
writeULong(locaDirOffset, checksum);
} else {
throw new IOException("Can't find glyf table");
}
}
/**
* Create the hmtx table by copying metrics from original
* font to subset font. The glyphs hashtable contains an
* Integer key and Integer value that maps the original
* metric (key) to the subset metric (value)
*/
private void createHmtx(FontFileReader in,
HashMap glyphs) throws IOException {
TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("hmtx");
int longHorMetricSize = glyphs.size() * 2;
int leftSideBearingSize = glyphs.size() * 2;
int hmtxSize = longHorMetricSize + leftSideBearingSize;
if (entry != null) {
pad4();
int offset = (int)entry.offset;
for (Iterator e = glyphs.keySet().iterator(); e.hasNext(); ) {
Integer origIndex = (Integer)e.next();
Integer subsetIndex = (Integer)glyphs.get(origIndex);
writeUShort(currentPos + subsetIndex.intValue() * 4,
mtx_tab[origIndex.intValue()].wx);
writeUShort(currentPos + subsetIndex.intValue() * 4 + 2,
mtx_tab[origIndex.intValue()].lsb);
}
int checksum = getCheckSum(currentPos, hmtxSize);
writeULong(hmtxDirOffset, checksum);
writeULong(hmtxDirOffset + 4, currentPos);
writeULong(hmtxDirOffset + 8, hmtxSize);
currentPos += hmtxSize;
realSize += hmtxSize;
} else {
throw new IOException("Can't find hmtx table");
}
}
/**
* Returns a List containing the glyph itself plus all glyphs
* that this composite glyph uses
*/
private ArrayList getIncludedGlyphs(FontFileReader in, int glyphOffset,
Integer glyphIdx) throws IOException {
ArrayList ret = new ArrayList();
ret.add(glyphIdx);
int offset = glyphOffset + (int)mtx_tab[glyphIdx.intValue()].offset
+ 10;
Integer compositeIdx = null;
int flags = 0;
boolean moreComposites = true;
while (moreComposites) {
flags = in.readTTFUShort(offset);
compositeIdx = new Integer(in.readTTFUShort(offset + 2));
ret.add(compositeIdx);
offset += 4;
if ((flags & 1) > 0) {
// ARG_1_AND_ARG_2_ARE_WORDS
offset += 4;
} else {
offset += 2;
}
if ((flags & 8) > 0)
offset += 2; // WE_HAVE_A_SCALE
else if ((flags & 64) > 0)
offset += 4; // WE_HAVE_AN_X_AND_Y_SCALE
else if ((flags & 128) > 0)
offset += 8; // WE_HAVE_A_TWO_BY_TWO
if ((flags & 32) > 0)
moreComposites = true;
else
moreComposites = false;
}
return ret;
}
/**
* Rewrite all compositepointers in glyphindex glyphIdx
*
*/
private void remapComposite(FontFileReader in, HashMap glyphs,
int glyphOffset,
Integer glyphIdx) throws IOException {
int offset = glyphOffset + (int)mtx_tab[glyphIdx.intValue()].offset
+ 10;
Integer compositeIdx = null;
int flags = 0;
boolean moreComposites = true;
while (moreComposites) {
flags = in.readTTFUShort(offset);
compositeIdx = new Integer(in.readTTFUShort(offset + 2));
Integer newIdx = (Integer)glyphs.get(compositeIdx);
if (newIdx == null) {
// This errormessage would look much better
// if the fontname was printed to
//log.error("An embedded font "
// + "contains bad glyph data. "
// + "Characters might not display "
// + "correctly.");
moreComposites = false;
continue;
}
in.writeTTFUShort(offset + 2, newIdx.intValue());
offset += 4;
if ((flags & 1) > 0) {
// ARG_1_AND_ARG_2_ARE_WORDS
offset += 4;
} else {
offset += 2;
}
if ((flags & 8) > 0) {
offset += 2; // WE_HAVE_A_SCALE
} else if ((flags & 64) > 0) {
offset += 4; // WE_HAVE_AN_X_AND_Y_SCALE
} else if ((flags & 128) > 0) {
offset += 8; // WE_HAVE_A_TWO_BY_TWO
}
if ((flags & 32) > 0)
moreComposites = true;
else
moreComposites = false;
}
}
/**
* Scan all the original glyphs for composite glyphs and add those glyphs
* to the glyphmapping also rewrite the composite glyph pointers to the new
* mapping
*/
private void scanGlyphs(FontFileReader in,
HashMap glyphs) throws IOException {
TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("glyf");
HashMap newComposites = null;
HashMap allComposites = new HashMap();
int newIndex = glyphs.size();
if (entry != null) {
while (newComposites == null || newComposites.size() > 0) {
// Inefficient to iterate through all glyphs
newComposites = new HashMap();
for (Iterator e = glyphs.keySet().iterator(); e.hasNext(); ) {
Integer origIndex = (Integer)e.next();
if (in.readTTFShort(entry.offset
+ mtx_tab[origIndex.intValue()].offset) < 0) {
// origIndex is a composite glyph
allComposites.put(origIndex, glyphs.get(origIndex));
ArrayList composites =
getIncludedGlyphs(in, (int)entry.offset,
origIndex);
// Iterate through all composites pointed to
// by this composite and check if they exists
// in the glyphs map, add them if not.
for (Iterator cps = composites.iterator();
cps.hasNext(); ) {
Integer cIdx = (Integer)cps.next();
if (glyphs.get(cIdx) == null
&& newComposites.get(cIdx) == null) {
newComposites.put(cIdx,
new Integer(newIndex));
newIndex++;
}
}
}
}
// Add composites to glyphs
for (Iterator m = newComposites.keySet().iterator();
m.hasNext(); ) {
Integer im = (Integer)m.next();
glyphs.put(im, newComposites.get(im));
}
}
// Iterate through all composites to remap their composite index
for (Iterator ce = allComposites.keySet().iterator();
ce.hasNext(); ) {
remapComposite(in, glyphs, (int)entry.offset,
(Integer)ce.next());
}
} else {
throw new IOException("Can't find glyf table");
}
}
/**
* glyphs has old index as (Integer) key and new index
* as (Integer) value
*/
public byte[] readFont(FontFileReader in, String name,
HashMap glyphs) throws IOException {
/*
* Check if TrueType collection, and that the name
* exists in the collection
*/
if (!checkTTC(in, name))
throw new IOException("Failed to read font");
output = new byte[in.getFileSize()];
readDirTabs(in);
readFontHeader(in);
getNumGlyphs(in);
readHorizontalHeader(in);
readHorizontalMetrics(in);
readIndexToLocation(in);
scanGlyphs(in, glyphs);
createDirectory(); // Create the TrueType header and directory
createHead(in);
createHhea(in, glyphs.size()); // Create the hhea table
createHmtx(in, glyphs); // Create hmtx table
createMaxp(in, glyphs.size()); // copy the maxp table
try {
createCvt(in); // copy the cvt table
} catch (IOException ex) {
// Cvt is optional (only required for OpenType (MS) fonts)
//log.error("TrueType warning: " + ex.getMessage());
}
try {
createFpgm(in); // copy fpgm table
} catch (IOException ex) {
// Fpgm is optional (only required for OpenType (MS) fonts)
//log.error("TrueType warning: " + ex.getMessage());
}
try {
createPrep(in); // copy prep table
} catch (IOException ex) {
// Prep is optional (only required for OpenType (MS) fonts)
//log.error("TrueType warning: " + ex.getMessage());
}
try {
createLoca(glyphs.size()); // create empty loca table
} catch (IOException ex) {
// Loca is optional (only required for OpenType (MS) fonts)
//log.error("TrueType warning: " + ex.getMessage());
}
try {
createGlyf(in, glyphs);
} catch (IOException ex) {
// Glyf is optional (only required for OpenType (MS) fonts)
//log.error("TrueType warning: " + ex.getMessage());
}
pad4();
createCheckSumAdjustment();
byte[] ret = new byte[realSize];
System.arraycopy(output, 0, ret, 0, realSize);
return ret;
}
/**
* writes a ISO-8859-1 string at the currentPosition
* updates currentPosition but not realSize
* @return number of bytes written
*/
private int writeString(String str) {
int length = 0;
try {
byte[] buf = str.getBytes("ISO-8859-1");
System.arraycopy(buf, 0, output, currentPos, buf.length);
length = buf.length;
currentPos += length;
} catch (Exception e) {
// This should never happen!
}
return length;
}
/**
* Appends a byte to the output array,
* updates currentPost but not realSize
*/
private void writeByte(byte b) {
output[currentPos++] = b;
}
/**
* Appends a USHORT to the output array,
* updates currentPost but not realSize
*/
private void writeUShort(int s) {
byte b1 = (byte)((s >> 8) & 0xff);
byte b2 = (byte)(s & 0xff);
writeByte(b1);
writeByte(b2);
}
/**
* Appends a USHORT to the output array,
* at the given position without changing currentPos
*/
private void writeUShort(int pos, int s) {
byte b1 = (byte)((s >> 8) & 0xff);
byte b2 = (byte)(s & 0xff);
output[pos] = b1;
output[pos + 1] = b2;
}
/**
* Appends a ULONG to the output array,
* updates currentPos but not realSize
*/
private void writeULong(int s) {
byte b1 = (byte)((s >> 24) & 0xff);
byte b2 = (byte)((s >> 16) & 0xff);
byte b3 = (byte)((s >> 8) & 0xff);
byte b4 = (byte)(s & 0xff);
writeByte(b1);
writeByte(b2);
writeByte(b3);
writeByte(b4);
}
/**
* Appends a ULONG to the output array,
* at the given position without changing currentPos
*/
private void writeULong(int pos, int s) {
byte b1 = (byte)((s >> 24) & 0xff);
byte b2 = (byte)((s >> 16) & 0xff);
byte b3 = (byte)((s >> 8) & 0xff);
byte b4 = (byte)(s & 0xff);
output[pos] = b1;
output[pos + 1] = b2;
output[pos + 2] = b3;
output[pos + 3] = b4;
}
/**
* Read a signed short value at given position
*/
private short readShort(int pos) {
int ret = readUShort(pos);
return (short)ret;
}
/**
* Read a unsigned short value at given position
*/
private int readUShort(int pos) {
int ret = (int)output[pos];
if (ret < 0)
ret += 256;
ret = ret << 8;
if ((int)output[pos + 1] < 0) {
ret |= (int)output[pos + 1] + 256;
} else
ret |= (int)output[pos + 1];
return ret;
}
/**
* Create a padding in the fontfile to align
* on a 4-byte boundary
*/
private void pad4() {
int padSize = currentPos % 4;
for (int i = 0; i < padSize; i++) {
output[currentPos++] = 0;
realSize++;
}
}
/**
* Returns the maximum power of 2 <= max
*/
private int maxPow2(int max) {
int i = 0;
while (Math.pow(2, (double)i) < max)
i++;
return (i - 1);
}
private int log2(int num) {
return (int)(Math.log((double)num) / Math.log(2));
}
private int getCheckSum(int start, int size) {
return (int)getLongCheckSum(start, size);
}
private long getLongCheckSum(int start, int size) {
// All the tables here are aligned on four byte boundaries
// Add remainder to size if it's not a multiple of 4
int remainder = size % 4;
if (remainder != 0)
size += remainder;
long sum = 0;
for (int i = 0; i < size; i += 4) {
int l = (int)(output[start + i] << 24);
l += (int)(output[start + i + 1] << 16);
l += (int)(output[start + i + 2] << 16);
l += (int)(output[start + i + 3] << 16);
sum += l;
if (sum > 0xffffffff)
sum = sum - 0xffffffff;
}
return sum;
}
private void createCheckSumAdjustment() {
long sum = getLongCheckSum(0, realSize);
int checksum = (int)(0xb1b0afba - sum);
writeULong(checkSumAdjustmentOffset, checksum);
}
}
|