Source Code Cross Referenced for TTFontParser.java in  » PDF » jPod » de » intarsys » font » truetype » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » PDF » jPod » de.intarsys.font.truetype 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        import java.util.HashMap;
034:        import java.util.Map;
035:
036:        import de.intarsys.tools.randomaccess.IRandomAccess;
037:        import de.intarsys.tools.stream.StreamTools;
038:        import de.intarsys.tools.string.StringTools;
039:
040:        /**
041:         * Apple TT specification
042:         * http://developer.apple.com/fonts/TTRefMan/RM06/Chap6.html
043:         */
044:        public class TTFontParser {
045:            // todo support indexed fonts
046:
047:            /** the parsed table directory information */
048:            private TTTable[] tables;
049:
050:            public TTFontParser() {
051:            }
052:
053:            public TTTable[] parseTables(TTFont font) throws IOException {
054:                IRandomAccess random = null;
055:                try {
056:                    random = font.getLocator().getRandomAccess();
057:                    readTables(font, random);
058:                    font.setTables(tables);
059:                } finally {
060:                    StreamTools.close(random);
061:                }
062:                return tables;
063:            }
064:
065:            public Map parseTable_cmap(TTTable data) throws IOException {
066:                Map result = new HashMap();
067:                IRandomAccess random = data.getRandomAccess();
068:                try {
069:                    random.seek(0);
070:                    // skip version
071:                    random.seekBy(2);
072:                    int count = readUShort(random);
073:                    for (int i = 0; i < count; i++) {
074:                        int platformID = readUShort(random);
075:                        int platformSpecificID = readUShort(random);
076:                        int offset = readInt(random);
077:                        TTTable subtable = new TTTable(data.getFont(), data
078:                                .getOffset()
079:                                + offset, -1);
080:                        String key = StringTools.EMPTY + platformID
081:                                + ":" + platformSpecificID; //$NON-NLS-1$
082:                        result.put(key, subtable);
083:                    }
084:                    return result;
085:                } finally {
086:                    StreamTools.close(random);
087:                }
088:            }
089:
090:            public Map parseTable_cmap_subtable(TTTable data)
091:                    throws IOException {
092:                Map result = null;
093:                IRandomAccess random = data.getRandomAccess();
094:                try {
095:                    random.seek(0);
096:                    int format = readUShort(random);
097:                    switch (format) {
098:                    case 0:
099:                        result = readCMapFormat0(random);
100:                        break;
101:                    case 4:
102:                        result = readCMapFormat4(random);
103:                        break;
104:                    case 6:
105:                        result = readCMapFormat6(random);
106:                        break;
107:                    }
108:                    return result;
109:                } finally {
110:                    StreamTools.close(random);
111:                }
112:            }
113:
114:            /**
115:             * This table gives global information about the font.
116:             * 
117:             * <pre>
118:             *  Type        Name        Description
119:             *  FIXED        Table version number        0x00010000 for version 1.0.
120:             *  FIXED        fontRevision        Set by font manufacturer.
121:             *  ULONG        checkSumAdjustment        To compute:  set it to 0, sum the entire font as ULONG, then store 0xB1B0AFBA - sum.
122:             *  ULONG        magicNumber        Set to 0x5F0F3CF5.
123:             *  USHORT        flags        Bit 0 - baseline for font at y=0;
124:             *                                         Bit 1 - left sidebearing at x=0;
125:             *                                         Bit 2 - instructions may depend on point size;
126:             *                                         Bit 3 - force ppem to integer values for all internal scaler math; may use fractional ppem sizes if this bit is clear;
127:             *                                         Bit 4 - instructions may alter advance width (the advance widths might not scale linearly);
128:             *                                         Note: All other bits must be zero.
129:             *  USHORT        unitsPerEm        Valid range is from 16 to 16384
130:             *  longDateTime        created        International date (8-byte field).
131:             *  longDateTime        modified        International date (8-byte field).
132:             *  FWORD        xMin        For all glyph bounding boxes.
133:             *  FWORD        yMin        For all glyph bounding boxes.
134:             *  FWORD        xMax        For all glyph bounding boxes.
135:             *  FWORD        yMax        For all glyph bounding boxes.
136:             *  USHORT        macStyle        Bit 0 bold (if set to 1); Bit 1 italic (if set to 1)Bits 2-15 reserved (set to 0).
137:             *  USHORT        lowestRecPPEM        Smallest readable size in pixels.
138:             *  SHORT        fontDirectionHint         0   Fully mixed directional glyphs; 1   Only strongly left to right; 2   Like 1 but also contains neutrals ;-1   Only strongly right to left;-2   Like -1 but also contains neutrals.
139:             *  SHORT        indexToLocFormat        0 for short offsets, 1 for long.
140:             *  SHORT        glyphDataFormat        0 for current format.
141:             * </pre>
142:             * 
143:             * @param data
144:             *            docme
145:             * 
146:             * @return docme
147:             * 
148:             * @throws IOException
149:             *             docme
150:             */
151:            public TTFontHeader parseTable_head(TTTable data)
152:                    throws IOException {
153:                TTFontHeader result = new TTFontHeader();
154:                IRandomAccess random = data.getRandomAccess();
155:                try {
156:                    random.seek(0);
157:                    // skip version
158:                    // skip font Revision
159:                    // skip checksum
160:                    // skip magic number
161:                    random.seekBy(16);
162:                    result.setFlags(readUShort(random));
163:                    result.setUnitsPerEm(readUShort(random));
164:                    // skip created
165:                    // skip modified
166:                    random.seekBy(16);
167:                    result.setXMin(readShort(random));
168:                    result.setYMin(readShort(random));
169:                    result.setXMax(readShort(random));
170:                    result.setYMax(readShort(random));
171:                    result.setMacStyle(readUShort(random));
172:                    // skip smallest size in pixels
173:                    // skip font direction hint
174:                    random.seekBy(4);
175:                    result.setShortLocationFormat(readShort(random) == 0);
176:                    return result;
177:                } finally {
178:                    StreamTools.close(random);
179:                }
180:            }
181:
182:            /**
183:             * <pre>
184:             *  FIXED        Table version number        0x00010000 for version 1.0.
185:             *  FWORD        Ascender        Typographic ascent.
186:             *  FWORD        Descender        Typographic descent.
187:             *  FWORD        LineGap        Typographic line gap. Negative LineGap values are treated as zero in Windows 3.1, System 6, and System 7.
188:             *  UFWORD        advanceWidthMax        Maximum advance width value in ‘hmtx’ table.
189:             *  FWORD        minLeftSideBearing        Minimum left sidebearing value in ‘hmtx’ table.
190:             *  FWORD        minRightSideBearing        Minimum right sidebearing value; calculated as Min(aw - lsb - (xMax - xMin)).
191:             *  FWORD        xMaxExtent        Max(lsb + (xMax - xMin)).
192:             *  SHORT        caretSlopeRise        Used to calculate the slope of the cursor (rise/run); 1 for vertical.
193:             *  SHORT        caretSlopeRun        0 for vertical.
194:             *  SHORT        (reserved)        set to 0
195:             *  SHORT        (reserved)        set to 0
196:             *  SHORT        (reserved)        set to 0
197:             *  SHORT        (reserved)        set to 0
198:             *  SHORT        (reserved)        set to 0
199:             *  SHORT        metricDataFormat        0 for current format.
200:             *  USHORT        numberOfHMetrics        Number of hMetric entries in  ‘hmtx’ table; may be smaller than the total number of glyphs in the font.
201:             * </pre>
202:             * 
203:             * @param data
204:             *            docme
205:             * 
206:             * @return docme
207:             * 
208:             * @throws IOException
209:             *             docme
210:             */
211:            public TTHorizontalHeader parseTable_hhea(TTTable data)
212:                    throws IOException {
213:                TTHorizontalHeader result = new TTHorizontalHeader();
214:                IRandomAccess random = data.getRandomAccess();
215:                try {
216:                    random.seek(0);
217:                    // skip version
218:                    random.seekBy(4);
219:                    result.setAscender(readShort(random));
220:                    result.setDescender(readShort(random));
221:                    result.setLineGap(readShort(random));
222:                    result.setAdvanceWidthMax(readUShort(random));
223:                    result.setMinLeftSideBearing(readShort(random));
224:                    result.setMinRightSideBearing(readShort(random));
225:                    result.setXMaxExtent(readShort(random));
226:                    result.setCaretSlopeRise(readShort(random));
227:                    result.setCaretSlopeRun(readShort(random));
228:                    // skip
229:                    random.seekBy(12);
230:                    result.setNumberOfHMetrics(readUShort(random));
231:                    // skip rest
232:                    return result;
233:                } finally {
234:                    StreamTools.close(random);
235:                }
236:            }
237:
238:            /**
239:             * <pre>
240:             * The type longHorMetric is defined as an array where each element has
241:             * two parts: the advance width, which is of type USHORT, and the left side
242:             * bearing, which is of type SHORT. These fields are in font design units.
243:             *  typedef struct         _longHorMetric {
244:             *                 USHORT        advanceWidth;
245:             *                 SHORT                lsb;
246:             *  }  longHorMetric;
247:             * 
248:             *  Field Type Description
249:             *  hMetrics longHorMetric [numberOfHMetrics] Paired advance width and
250:             *                                 left side bearing values for each glyph. The value
251:             *                                 numOfHMetrics comes from the 'hhea' table. If the font is
252:             *                                 monospaced, only one entry need be in the array, but that
253:             *                                 entry is required. The last entry applies to all subsequent
254:             *                                 glyphs.
255:             *  leftSideBearing SHORT[ ] Here the advanceWidth is assumed to be the
256:             *                                 same as the advanceWidth for the last entry above. The
257:             *                                 number of entries in this array is derived from numGlyphs
258:             *                                 (from 'maxp' table) minus numberOfHMetrics. This generally
259:             *                                 is used with a run of monospaced glyphs (e.g., Kanji fonts
260:             *                                 or Courier fonts). Only one run is allowed and it must be
261:             *                                 at the end. This allows a monospaced font to vary the left
262:             *                                 side bearing values for each glyph.
263:             * </pre>
264:             * 
265:             * @param data
266:             *            docme
267:             * @param count
268:             *            docme
269:             * 
270:             * @return docme
271:             * 
272:             * @throws IOException
273:             *             docme
274:             */
275:            public int[] parseTable_hmtx(TTTable data, int count)
276:                    throws IOException {
277:                int[] widths = new int[count];
278:                IRandomAccess random = data.getRandomAccess();
279:                try {
280:                    random.seek(0);
281:                    for (int i = 0; i < count; i++) {
282:                        widths[i] = readUShort(random);
283:                        readShort(random);
284:                    }
285:                    return widths;
286:                } finally {
287:                    StreamTools.close(random);
288:                }
289:            }
290:
291:            public int[] parseTable_loca(TTTable data,
292:                    boolean shortLocationFormat) throws IOException {
293:                int count = 0;
294:                int[] locations;
295:                IRandomAccess random = data.getRandomAccess();
296:                try {
297:                    random.seek(0);
298:                    if (shortLocationFormat) {
299:                        count = (int) random.getLength() / 2;
300:                        locations = new int[count];
301:
302:                        for (int i = 0; i < count; i++) {
303:                            locations[i] = readUShort(random) << 1;
304:                        }
305:                    } else {
306:                        count = (int) random.getLength() / 4;
307:                        locations = new int[count];
308:
309:                        for (int i = 0; i < count; i++) {
310:                            locations[i] = readInt(random);
311:                        }
312:                    }
313:                    return locations;
314:                } finally {
315:                    StreamTools.close(random);
316:                }
317:            }
318:
319:            public TTNaming parseTable_name(TTTable data) throws IOException {
320:                TTNaming result = new TTNaming();
321:                IRandomAccess random = data.getRandomAccess();
322:                try {
323:                    random.seek(0);
324:                    // skip format
325:                    random.seekBy(2);
326:                    // USHORT count Number of name records.
327:                    int count = readUShort(random);
328:                    // USHORT stringOffset Offset to start of string storage (from start
329:                    // of
330:                    // table).
331:                    int stringTableOffset = readUShort(random);
332:                    // Namerecords follow
333:                    for (int i = 0; i < count; i++) {
334:                        TTNameRecord record = readNameRecord(random,
335:                                stringTableOffset);
336:                        // filter for Microsoft-Plattform and Language=American
337:                        if ((record.getPlatformID() == 3)
338:                                && (record.getLanguageID() == 1033)) {
339:                            result.add(record.getNameID(), record);
340:                        }
341:                    }
342:                    return result;
343:                } finally {
344:                    StreamTools.close(random);
345:                }
346:            }
347:
348:            /**
349:             * <pre>
350:             *  USHORT        version        0x0001
351:             *  SHORT        xAvgCharWidth;
352:             *  USHORT        usWeightClass;
353:             *  USHORT        usWidthClass;
354:             *  SHORT        fsType;
355:             *  SHORT        ySubscriptXSize;
356:             *  SHORT        ySubscriptYSize;
357:             *  SHORT        ySubscriptXOffset;
358:             *  SHORT        ySubscriptYOffset;
359:             *  SHORT        ySuperscriptXSize;
360:             *  SHORT        ySuperscriptYSize;
361:             *  SHORT        ySuperscriptXOffset;
362:             *  SHORT        ySuperscriptYOffset;
363:             *  SHORT        yStrikeoutSize;
364:             *  SHORT        yStrikeoutPosition;
365:             *  SHORT        sFamilyClass;
366:             *  PANOSE        panose;
367:             *  ULONG        ulUnicodeRange1        Bits 0–31
368:             *  ULONG        ulUnicodeRange2        Bits 32–63
369:             *  ULONG        ulUnicodeRange3        Bits 64–95
370:             *  ULONG        ulUnicodeRange4        Bits 96–127
371:             *  CHAR        achVendID[4];
372:             *  USHORT        fsSelection;
373:             *  USHORT        usFirstCharIndex
374:             *  USHORT        usLastCharIndex
375:             *  USHORT        sTypoAscender
376:             *  USHORT        sTypoDescender
377:             *  USHORT        sTypoLineGap
378:             *  USHORT        usWinAscent
379:             *  USHORT        usWinDescent
380:             *  ULONG        ulCodePageRange1        Bits 0-31
381:             *  ULONG        ulCodePageRange2        Bits 32-63
382:             *  SHORT                  sxHeight
383:             *         SHORT                 sCapHeight
384:             *         USHORT                 usDefaultChar
385:             *         USHORT                 usBreakChar
386:             *         USHORT                 usMaxContext
387:             * </pre>
388:             * 
389:             * @param data
390:             *            docme
391:             * 
392:             * @return docme
393:             * 
394:             * @throws IOException
395:             *             docme
396:             */
397:            public TTMetrics parseTable_os2(TTTable data) throws IOException {
398:                TTMetrics result = new TTMetrics();
399:                IRandomAccess random = data.getRandomAccess();
400:                try {
401:                    random.seek(0);
402:                    // skip version
403:                    random.seekBy(2);
404:                    result.setXAvgCharWidth(readShort(random));
405:                    result.setUsWeightClass(readUShort(random));
406:                    result.setUsWidthClass(readUShort(random));
407:                    result.setFsType(readUShort(random));
408:                    result.setYSubscriptXSize(readShort(random));
409:                    result.setYSubscriptYSize(readShort(random));
410:                    result.setYSubscriptXOffset(readShort(random));
411:                    result.setYSubscriptYOffset(readShort(random));
412:                    result.setYSuperscriptXSize(readShort(random));
413:                    result.setYSuperscriptYSize(readShort(random));
414:                    result.setYSuperscriptXOffset(readShort(random));
415:                    result.setYSuperscriptYOffset(readShort(random));
416:                    result.setYStrikeoutSize(readShort(random));
417:                    result.setYStrikeoutPosition(readShort(random));
418:                    result.setSFamilyClass(readShort(random));
419:                    result.setPanose(readBytes(random, 10));
420:                    // skip unicode ranges
421:                    // skip vendor id
422:                    random.seekBy(20);
423:                    result.setFsSelection(readUShort(random));
424:                    result.setUsFirstCharIndex(readUShort(random));
425:                    result.setUsLastCharIndex(readUShort(random));
426:                    result.setSTypoAscender(readShort(random));
427:                    result.setSTypoDescender(readShort(random));
428:                    result.setSTypoLineGap(readShort(random));
429:                    result.setUsWinAscent(readUShort(random));
430:                    result.setUsWinDescent(readUShort(random));
431:                    result.setSxHeight(readShort(random));
432:                    result.setSCapHeight(readShort(random));
433:                    result.setUsDefaultChar(readUShort(random));
434:                    result.setUsMaxContext(readUShort(random));
435:                    // skip rest
436:                    return result;
437:                } finally {
438:                    StreamTools.close(random);
439:                }
440:            }
441:
442:            /**
443:             * <pre>
444:             * Type         Name                 Description
445:             * Fixed         Version         0x00010000 for version 1.0
446:             *                                         0x00020000 for version 2.0
447:             *                                         0x00025000 for version 2.5 (deprecated)
448:             *                                         0x00030000 for version 3.0
449:             * Fixed         italicAngle         Italic angle in counter-clockwise degrees from the vertical. Zero for upright text, negative for text that leans to the right (forward).
450:             * FWord         underlinePosition         This is the suggested distance of the top of the underline from the baseline (negative values indicate below baseline).
451:             *                                                 The PostScript definition of this FontInfo dictionary key (the y coordinate of the center of the stroke) is not used for historical reasons. The value of the PostScript key may be calculated by subtracting half the underlineThickness from the value of this field.
452:             * FWord         underlineThickness         Suggested values for the underline thickness.
453:             * ULONG         isFixedPitch         Set to 0 if the font is proportionally spaced, non-zero if the font is not proportionally spaced (i.e. monospaced).
454:             * ULONG         minMemType42         Minimum memory usage when an OpenType font is downloaded.
455:             * ULONG         maxMemType42         Maximum memory usage when an OpenType font is downloaded.
456:             * ULONG         minMemType1         Minimum memory usage when an OpenType font is downloaded as a Type 1 font.
457:             * ULONG         maxMemType1         Maximum memory usage when an OpenType font is downloaded as a Type 1 font.
458:             * </pre>
459:             * 
460:             * @param data
461:             *            docme
462:             * 
463:             * @return docme
464:             * 
465:             * @throws IOException
466:             *             docme
467:             */
468:            public TTPostScriptInformation parseTable_post(TTTable data)
469:                    throws IOException {
470:                TTPostScriptInformation result = new TTPostScriptInformation();
471:                IRandomAccess random = data.getRandomAccess();
472:                try {
473:                    random.seek(0);
474:                    result.setVersion(readFixed(random));
475:                    result.setItalicAngle(readFixed(random));
476:                    result.setUnderlinePosition(readShort(random));
477:                    result.setUnderlineThickness(readShort(random));
478:                    return result;
479:                } finally {
480:                    StreamTools.close(random);
481:                }
482:            }
483:
484:            protected byte[] readBytes(IRandomAccess random, int count)
485:                    throws IOException {
486:                byte[] bytes = new byte[count];
487:                int bytesRead = random.read(bytes);
488:                if (bytesRead < bytes.length) {
489:                    return null;
490:                }
491:                return bytes;
492:            }
493:
494:            protected Map readCMapFormat0(IRandomAccess random)
495:                    throws IOException {
496:                Map result = new HashMap();
497:
498:                // skip length
499:                // skip language
500:                random.seekBy(4);
501:
502:                for (int i = 0; i < 256; i++) {
503:                    int glyphCode = random.read();
504:                    result.put(new Integer(i), new Integer(glyphCode));
505:                }
506:
507:                return result;
508:            }
509:
510:            protected Map readCMapFormat4(IRandomAccess random)
511:                    throws IOException {
512:                Map result = new HashMap();
513:                int length = readUShort(random);
514:
515:                // skip language
516:                random.seekBy(2);
517:
518:                int segCount = readUShort(random) / 2;
519:
520:                // skip hints
521:                random.seekBy(6);
522:
523:                int[] endIndices = new int[segCount];
524:
525:                for (int i = 0; i < segCount; i++) {
526:                    endIndices[i] = readUShort(random);
527:                }
528:
529:                // skip reserved
530:                random.seekBy(2);
531:
532:                int[] startIndices = new int[segCount];
533:
534:                for (int i = 0; i < segCount; i++) {
535:                    startIndices[i] = readUShort(random);
536:                }
537:
538:                int[] deltas = new int[segCount];
539:
540:                for (int i = 0; i < segCount; i++) {
541:                    deltas[i] = readUShort(random);
542:                }
543:
544:                int[] rangeOffsets = new int[segCount];
545:
546:                for (int i = 0; i < segCount; i++) {
547:                    rangeOffsets[i] = readUShort(random);
548:                }
549:
550:                int glyphIndexCount = (length / 2) - 8 - (segCount * 4);
551:                int[] glyphIndices = new int[glyphIndexCount];
552:
553:                for (int i = 0; i < glyphIndexCount; i++) {
554:                    glyphIndices[i] = readUShort(random);
555:                }
556:
557:                // now construct map
558:                int glyphCode;
559:
560:                for (int i = 0; i < segCount; i++) {
561:                    for (int charCode = startIndices[i]; (charCode <= endIndices[i])
562:                            && (charCode != 0xffff); charCode++) {
563:                        if (rangeOffsets[i] == 0) {
564:                            glyphCode = (deltas[i] + charCode) & 0xffff;
565:                        } else {
566:                            // index as described in open font specification
567:                            int index = (charCode - startIndices[i])
568:                                    + (rangeOffsets[i] / 2);
569:
570:                            // try emulate c pointer arithmetic..
571:                            index = index - segCount + i;
572:                            glyphCode = glyphIndices[index];
573:
574:                            if (glyphCode != 0) {
575:                                glyphCode = (deltas[i] + glyphCode) & 0xffff;
576:                            }
577:                        }
578:
579:                        result.put(new Integer(charCode),
580:                                new Integer(glyphCode));
581:                    }
582:                }
583:
584:                return result;
585:            }
586:
587:            protected Map readCMapFormat6(IRandomAccess random)
588:                    throws IOException {
589:                Map result = new HashMap();
590:
591:                // skip length
592:                // skip language
593:                random.seekBy(4);
594:
595:                int firstIndex = readUShort(random);
596:                int count = readUShort(random);
597:                int lastIndex = firstIndex + count;
598:
599:                for (int i = firstIndex; i < lastIndex; i++) {
600:                    int glyphCode = readUShort(random);
601:                    result.put(new Integer(i), new Integer(glyphCode));
602:                }
603:
604:                return result;
605:            }
606:
607:            protected float readFixed(IRandomAccess random) throws IOException {
608:                int i = readInt(random);
609:                boolean negative = false;
610:
611:                if (i < 0) {
612:                    negative = true;
613:                    i *= -1;
614:                }
615:
616:                float hi = (i >> 16);
617:                float low = (i & 0x0000FFFF);
618:
619:                while (low >= 1) {
620:                    low = low / 10;
621:                }
622:
623:                if (negative) {
624:                    return -1 * (hi + low);
625:                }
626:
627:                return hi + low;
628:            }
629:
630:            protected int readInt(IRandomAccess random) throws IOException {
631:                int b1 = random.read();
632:                int b2 = random.read();
633:                int b3 = random.read();
634:                int b4 = random.read();
635:
636:                if ((b1 | b2 | b3 | b4) < 0) {
637:                    throw new IOException("unexpected end of stream"); //$NON-NLS-1$
638:                }
639:
640:                return ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
641:            }
642:
643:            protected TTNameRecord readNameRecord(IRandomAccess random,
644:                    int stringTableOffset) throws IOException {
645:                TTNameRecord record = new TTNameRecord();
646:
647:                // USHORT platformID Platform ID.
648:                record.setPlatformID(readUShort(random));
649:
650:                // USHORT encodingID Platform-specific encoding ID.
651:                record.setEncodingID(readUShort(random));
652:
653:                // USHORT languageID Language ID.
654:                record.setLanguageID(readUShort(random));
655:
656:                // USHORT nameID Name ID.
657:                record.setNameID(readUShort(random));
658:
659:                // USHORT length String length (in bytes).
660:                record.setLength(readUShort(random));
661:
662:                // USHORT offset String offset from start of storage area (in bytes).
663:                int nameOffset = readUShort(random);
664:                byte[] value = new byte[record.getLength()];
665:
666:                random.mark();
667:                random.seek(stringTableOffset + nameOffset);
668:                random.read(value, 0, record.getLength());
669:                record.setValue(new String(value, "UTF-16BE")); //$NON-NLS-1$
670:                random.reset();
671:
672:                return record;
673:            }
674:
675:            protected short readShort(IRandomAccess random) throws IOException {
676:                int b1 = random.read();
677:                int b2 = random.read();
678:
679:                if ((b1 | b2) < 0) {
680:                    throw new IOException("unexpected end of stream"); //$NON-NLS-1$
681:                }
682:
683:                return (short) ((b1 << 8) + b2);
684:            }
685:
686:            protected TTTable readTable(TTFont font, IRandomAccess random)
687:                    throws IOException {
688:                byte[] name = readBytes(random, 4);
689:                int checksum = readInt(random);
690:                int offset = readInt(random);
691:                int length = readInt(random);
692:
693:                TTTable result = new TTTable(font, offset, length);
694:                result.setName(name);
695:                result.setChecksum(checksum);
696:                return result;
697:            }
698:
699:            protected void readTables(TTFont font, IRandomAccess random)
700:                    throws IOException {
701:                // this is not true for all valid font files, skip version check
702:                // if (readInt(is) != 0x00010000) {
703:                // throw new IOException("not a valid TTF file.");
704:                // }
705:                readInt(random);
706:
707:                int tableCount = readUShort(random);
708:
709:                // skip search range
710:                // skip entry selector
711:                // skip range shift
712:                random.seekBy(6);
713:                tables = new TTTable[tableCount];
714:
715:                for (int i = 0; i < tableCount; i++) {
716:                    tables[i] = readTable(font, random);
717:                }
718:            }
719:
720:            protected long readUInt(IRandomAccess random) throws IOException {
721:                int b1 = random.read();
722:                int b2 = random.read();
723:                int b3 = random.read();
724:                int b4 = random.read();
725:
726:                if ((b1 | b2 | b3 | b4) < 0) {
727:                    throw new IOException("unexpected end of stream"); //$NON-NLS-1$
728:                }
729:
730:                return (long) (((long) b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
731:            }
732:
733:            protected int readUShort(IRandomAccess random) throws IOException {
734:                int b1 = random.read();
735:                int b2 = random.read();
736:
737:                if ((b1 | b2) < 0) {
738:                    throw new IOException("unexpected end of stream"); //$NON-NLS-1$
739:                }
740:
741:                return (b1 << 8) + b2;
742:            }
743:
744:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.