Line data Source code
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 : #include "sfntly/font_factory.h"
18 :
19 : #include <string.h>
20 :
21 : #include "sfntly/tag.h"
22 :
23 : namespace sfntly {
24 :
25 0 : FontFactory::~FontFactory() {
26 0 : }
27 :
28 0 : CALLER_ATTACH FontFactory* FontFactory::GetInstance() {
29 0 : FontFactoryPtr instance = new FontFactory();
30 0 : return instance.Detach();
31 : }
32 :
33 0 : void FontFactory::FingerprintFont(bool fingerprint) {
34 0 : fingerprint_ = fingerprint;
35 0 : }
36 :
37 0 : bool FontFactory::FingerprintFont() {
38 0 : return fingerprint_;
39 : }
40 :
41 0 : void FontFactory::LoadFonts(InputStream* is, FontArray* output) {
42 0 : assert(output);
43 0 : PushbackInputStream* pbis = down_cast<PushbackInputStream*>(is);
44 0 : if (IsCollection(pbis)) {
45 0 : LoadCollection(pbis, output);
46 0 : return;
47 : }
48 0 : FontPtr font;
49 0 : font.Attach(LoadSingleOTF(pbis));
50 0 : if (font) {
51 0 : output->push_back(font);
52 : }
53 : }
54 :
55 0 : void FontFactory::LoadFonts(ByteVector* b, FontArray* output) {
56 0 : WritableFontDataPtr wfd;
57 0 : wfd.Attach(WritableFontData::CreateWritableFontData(b));
58 0 : if (IsCollection(wfd)) {
59 0 : LoadCollection(wfd, output);
60 0 : return;
61 : }
62 0 : FontPtr font;
63 0 : font.Attach(LoadSingleOTF(wfd));
64 0 : if (font) {
65 0 : output->push_back(font);
66 : }
67 : }
68 :
69 0 : void FontFactory::LoadFontsForBuilding(InputStream* is,
70 : FontBuilderArray* output) {
71 0 : PushbackInputStream* pbis = down_cast<PushbackInputStream*>(is);
72 0 : if (IsCollection(pbis)) {
73 0 : LoadCollectionForBuilding(pbis, output);
74 0 : return;
75 : }
76 0 : FontBuilderPtr builder;
77 0 : builder.Attach(LoadSingleOTFForBuilding(pbis));
78 0 : if (builder) {
79 0 : output->push_back(builder);
80 : }
81 : }
82 :
83 0 : void FontFactory::LoadFontsForBuilding(ByteVector* b,
84 : FontBuilderArray* output) {
85 0 : WritableFontDataPtr wfd;
86 0 : wfd.Attach(WritableFontData::CreateWritableFontData(b));
87 0 : if (IsCollection(wfd)) {
88 0 : LoadCollectionForBuilding(wfd, output);
89 0 : return;
90 : }
91 0 : FontBuilderPtr builder;
92 0 : builder.Attach(LoadSingleOTFForBuilding(wfd, 0));
93 0 : if (builder) {
94 0 : output->push_back(builder);
95 : }
96 : }
97 :
98 0 : void FontFactory::SerializeFont(Font* font, OutputStream* os) {
99 0 : font->Serialize(os, &table_ordering_);
100 0 : }
101 :
102 0 : void FontFactory::SetSerializationTableOrdering(
103 : const IntegerList& table_ordering) {
104 0 : table_ordering_ = table_ordering;
105 0 : }
106 :
107 0 : CALLER_ATTACH Font::Builder* FontFactory::NewFontBuilder() {
108 0 : return Font::Builder::GetOTFBuilder(this);
109 : }
110 :
111 0 : CALLER_ATTACH Font* FontFactory::LoadSingleOTF(InputStream* is) {
112 0 : FontBuilderPtr builder;
113 0 : builder.Attach(LoadSingleOTFForBuilding(is));
114 0 : return builder->Build();
115 : }
116 :
117 0 : CALLER_ATTACH Font* FontFactory::LoadSingleOTF(WritableFontData* wfd) {
118 0 : FontBuilderPtr builder;
119 0 : builder.Attach(LoadSingleOTFForBuilding(wfd, 0));
120 0 : return builder->Build();
121 : }
122 :
123 0 : void FontFactory::LoadCollection(InputStream* is, FontArray* output) {
124 0 : FontBuilderArray ba;
125 0 : LoadCollectionForBuilding(is, &ba);
126 0 : output->reserve(ba.size());
127 0 : for (FontBuilderArray::iterator builder = ba.begin(), builders_end = ba.end();
128 : builder != builders_end; ++builder) {
129 0 : FontPtr font;
130 0 : font.Attach((*builder)->Build());
131 0 : output->push_back(font);
132 : }
133 0 : }
134 :
135 0 : void FontFactory::LoadCollection(WritableFontData* wfd, FontArray* output) {
136 0 : FontBuilderArray builders;
137 0 : LoadCollectionForBuilding(wfd, &builders);
138 0 : output->reserve(builders.size());
139 0 : for (FontBuilderArray::iterator builder = builders.begin(),
140 0 : builders_end = builders.end();
141 : builder != builders_end; ++builder) {
142 0 : FontPtr font;
143 0 : font.Attach((*builder)->Build());
144 0 : output->push_back(font);
145 : }
146 0 : }
147 :
148 : CALLER_ATTACH
149 0 : Font::Builder* FontFactory::LoadSingleOTFForBuilding(InputStream* is) {
150 : // UNIMPLEMENTED: SHA-1 hash checking via Java DigestStream
151 0 : Font::Builder* builder = Font::Builder::GetOTFBuilder(this, is);
152 : // UNIMPLEMENTED: setDigest
153 0 : return builder;
154 : }
155 :
156 : CALLER_ATTACH Font::Builder*
157 0 : FontFactory::LoadSingleOTFForBuilding(WritableFontData* wfd,
158 : int32_t offset_to_offset_table) {
159 : // UNIMPLEMENTED: SHA-1 hash checking via Java DigestStream
160 : Font::Builder* builder =
161 0 : Font::Builder::GetOTFBuilder(this, wfd, offset_to_offset_table);
162 : // UNIMPLEMENTED: setDigest
163 0 : return builder;
164 : }
165 :
166 0 : void FontFactory::LoadCollectionForBuilding(InputStream* is,
167 : FontBuilderArray* builders) {
168 0 : assert(is);
169 0 : assert(builders);
170 0 : WritableFontDataPtr wfd;
171 0 : wfd.Attach(WritableFontData::CreateWritableFontData(is->Available()));
172 0 : wfd->CopyFrom(is);
173 0 : LoadCollectionForBuilding(wfd, builders);
174 0 : }
175 :
176 0 : void FontFactory::LoadCollectionForBuilding(WritableFontData* wfd,
177 : FontBuilderArray* builders) {
178 0 : int32_t ttc_tag = wfd->ReadULongAsInt(Offset::kTTCTag);
179 : UNREFERENCED_PARAMETER(ttc_tag);
180 0 : int32_t version = wfd->ReadFixed(Offset::kVersion);
181 : UNREFERENCED_PARAMETER(version);
182 0 : int32_t num_fonts = wfd->ReadULongAsInt(Offset::kNumFonts);
183 :
184 0 : builders->reserve(num_fonts);
185 0 : int32_t offset_table_offset = Offset::kOffsetTable;
186 0 : for (int32_t font_number = 0;
187 0 : font_number < num_fonts;
188 0 : font_number++, offset_table_offset += DataSize::kULONG) {
189 0 : int32_t offset = wfd->ReadULongAsInt(offset_table_offset);
190 0 : FontBuilderPtr builder;
191 0 : builder.Attach(LoadSingleOTFForBuilding(wfd, offset));
192 0 : builders->push_back(builder);
193 : }
194 0 : }
195 :
196 0 : bool FontFactory::IsCollection(PushbackInputStream* pbis) {
197 0 : ByteVector tag(4);
198 0 : pbis->Read(&tag);
199 0 : pbis->Unread(&tag);
200 0 : return Tag::ttcf == GenerateTag(tag[0], tag[1], tag[2], tag[3]);
201 : }
202 :
203 0 : bool FontFactory::IsCollection(ReadableFontData* rfd) {
204 0 : ByteVector tag(4);
205 0 : rfd->ReadBytes(0, &(tag[0]), 0, tag.size());
206 0 : return Tag::ttcf ==
207 0 : GenerateTag(tag[0], tag[1], tag[2], tag[3]);
208 : }
209 :
210 0 : FontFactory::FontFactory()
211 0 : : fingerprint_(false) {
212 0 : }
213 :
214 : } // namespace sfntly
|