• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.typography.font.sfntly.table.bitmap;
18 
19 import com.google.typography.font.sfntly.data.FontData;
20 import com.google.typography.font.sfntly.data.ReadableFontData;
21 import com.google.typography.font.sfntly.data.WritableFontData;
22 import com.google.typography.font.sfntly.table.SubTable;
23 
24 /**
25  * @author Stuart Gill
26  *
27  */
28 public abstract class BitmapGlyph extends SubTable {
29 
30   protected enum Offset {
31     // header
32     version(0),
33 
34     smallGlyphMetricsLength(5),
35     bigGlyphMetricsLength(8),
36     // format 1
37     glyphFormat1_imageData(smallGlyphMetricsLength.offset),
38 
39     // format 2
40     glyphFormat2_imageData(smallGlyphMetricsLength.offset),
41 
42     // format 3
43 
44     // format 4
45 
46     // format 5
47     glyphFormat5_imageData(0),
48 
49     // format 6
50     glyphFormat6_imageData(bigGlyphMetricsLength.offset),
51 
52     // format 7
53     glyphFormat7_imageData(bigGlyphMetricsLength.offset),
54 
55     // format 8
56     glyphFormat8_numComponents(Offset.smallGlyphMetricsLength.offset + 1),
57     glyphFormat8_componentArray(glyphFormat8_numComponents.offset
58         + FontData.DataSize.USHORT.size()),
59 
60     // format 9
61     glyphFormat9_numComponents(Offset.bigGlyphMetricsLength.offset),
62     glyphFormat9_componentArray(glyphFormat9_numComponents.offset
63         + FontData.DataSize.USHORT.size()),
64 
65 
66     // ebdtComponent
67     ebdtComponentLength(FontData.DataSize.USHORT.size() + 2 * FontData.DataSize.CHAR.size()),
68     ebdtComponent_glyphCode(0),
69     ebdtComponent_xOffset(2),
70     ebdtComponent_yOffset(3);
71 
72     protected final int offset;
73 
Offset(int offset)74     private Offset(int offset) {
75       this.offset = offset;
76     }
77   }
78 
79   private int format;
80 
createGlyph(ReadableFontData data, int format)81   public static BitmapGlyph createGlyph(ReadableFontData data, int format) {
82     BitmapGlyph glyph = null;
83     BitmapGlyph.Builder<? extends BitmapGlyph> builder = Builder.createGlyphBuilder(data, format);
84     if (builder != null) {
85       glyph = builder.build();
86     }
87     return glyph;
88   }
89 
BitmapGlyph(ReadableFontData data, int format)90   protected BitmapGlyph(ReadableFontData data, int format) {
91     super(data);
92     this.format = format;
93   }
94 
BitmapGlyph(ReadableFontData data, int offset, int length, int format)95   protected BitmapGlyph(ReadableFontData data, int offset, int length, int format) {
96     super(data, offset, length);
97     this.format = format;
98   }
99 
format()100   public int format() {
101     return this.format;
102   }
103 
104   public static abstract class Builder<T extends BitmapGlyph> extends SubTable.Builder<T> {
105 
106     private final int format;
107 
createGlyphBuilder( ReadableFontData data, int format)108     public static Builder<? extends BitmapGlyph> createGlyphBuilder(
109         ReadableFontData data, int format) {
110       switch (format) {
111         case 1:
112         case 2:
113         case 3:
114         case 4:
115         case 5:
116         case 6:
117         case 7:
118           return new SimpleBitmapGlyph.Builder(data, format);
119         case 8:
120         case 9:
121           return new CompositeBitmapGlyph.Builder(data, format);
122       }
123       return null;
124     }
125 
Builder(WritableFontData data, int format)126     protected Builder(WritableFontData data, int format) {
127       super(data);
128       this.format = format;
129     }
130 
Builder(ReadableFontData data, int format)131     protected Builder(ReadableFontData data, int format) {
132       super(data);
133       this.format = format;
134     }
135 
format()136     public int format() {
137       return this.format;
138     }
139 
140     @Override
subDataSet()141     protected void subDataSet() {
142       // NOP
143     }
144 
145     @Override
subDataSizeToSerialize()146     protected int subDataSizeToSerialize() {
147       return this.internalReadData().length();
148     }
149 
150     @Override
subReadyToSerialize()151     protected boolean subReadyToSerialize() {
152       return true;
153     }
154 
155     @Override
subSerialize(WritableFontData newData)156     protected int subSerialize(WritableFontData newData) {
157       return this.internalReadData().copyTo(newData);
158     }
159   }
160 
161   @Override
toString()162   public String toString() {
163     return "BitmapGlyph [format=" + format + ", data = " + super.toString() + "]";
164   }
165 }
166