View Javadoc

1   // Copyright 2001, FreeHEP.
2   package org.freehep.graphicsio.swf;
3   
4   import java.io.IOException;
5   
6   /**
7    * DefineFontInfo TAG.
8    * 
9    * @author Mark Donszelmann
10   * @author Charles Loomis
11   * @version $Id: DefineFontInfo.java 8584 2006-08-10 23:06:37Z duns $
12   */
13  public class DefineFontInfo extends DefinitionTag {
14  
15      protected int fontID;
16  
17      protected String name;
18  
19      protected boolean shiftJIS, ansi;
20  
21      protected boolean italic, bold;
22  
23      protected boolean wideCodes;
24  
25      protected int[] codes;
26  
27      public DefineFontInfo(int fontID, String name, boolean shiftJIS,
28              boolean ansi, boolean italic, boolean bold, boolean wideCodes,
29              int[] codes) {
30          this();
31          this.fontID = fontID;
32          this.name = name;
33          this.shiftJIS = shiftJIS;
34          this.ansi = ansi;
35          this.italic = italic;
36          this.bold = bold;
37          this.wideCodes = wideCodes;
38          this.codes = codes;
39      }
40  
41      public DefineFontInfo() {
42          super(13, 1);
43      }
44  
45      protected DefineFontInfo(int code, int version) {
46          super(code, version);
47      }
48  
49      public SWFTag read(int tagID, SWFInputStream swf, int len)
50              throws IOException {
51  
52          DefineFontInfo tag = new DefineFontInfo();
53          tag.fontID = swf.readUnsignedShort();
54  
55          // associate this fontinfo with font
56          DefineFont font = (DefineFont) swf.getDictionary().get(tag.fontID);
57          int numGlyphs = font.getGlyphCount();
58          font.setFontInfo(tag);
59  
60          int nameLength = swf.readUnsignedByte();
61          if (swf.getVersion() >= 6) {
62              tag.name = swf.readUTF();
63          } else {
64              tag.name = new String(swf.readByte(nameLength));
65          }
66          /* int reserved = (int) */ swf.readUBits(3);
67          tag.shiftJIS = swf.readBitFlag();
68          tag.ansi = swf.readBitFlag();
69          tag.italic = swf.readBitFlag();
70          tag.bold = swf.readBitFlag();
71          tag.wideCodes = swf.readBitFlag();
72  
73          tag.codes = (tag.wideCodes) ? swf.readUnsignedShort(numGlyphs) : swf
74                  .readUnsignedByte(numGlyphs);
75          return tag;
76      }
77  
78      public void write(int tagID, SWFOutputStream swf) throws IOException {
79          swf.writeUnsignedShort(fontID);
80          swf.writeUnsignedByte(name.length());
81          if (swf.getVersion() >= 6) {
82              swf.writeUTF(name);
83          } else {
84              swf.writeByte(name.getBytes());
85          }
86          swf.writeUBits(0, 3);
87          swf.writeBitFlag(shiftJIS);
88          swf.writeBitFlag(ansi);
89          swf.writeBitFlag(italic);
90          swf.writeBitFlag(bold);
91          swf.writeBitFlag(wideCodes);
92          if (wideCodes) {
93              swf.writeUnsignedShort(codes);
94          } else {
95              swf.writeUnsignedByte(codes);
96          }
97      }
98  
99      public String toString() {
100         StringBuffer s = new StringBuffer();
101         s.append(super.toString() + "\n");
102         s.append("  name:      " + name + "\n");
103         s.append("  numGlyphs: " + codes.length + "\n");
104         s.append("    ");
105         for (int i = 0; i < codes.length; i++) {
106             s.append("[" + codes[i] + ",'" + ((char) codes[i]) + "'] ");
107         }
108         s.append("\n");
109         return s.toString();
110     }
111 }