15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright 2011 Google Inc. All Rights Reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * you may not use this file except in compliance with the License.
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * You may obtain a copy of the License at
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
902772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch *
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
1202772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * See the License for the specific language governing permissions and
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * limitations under the License.
1502772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch */
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef SFNTLY_CPP_SRC_SFNTLY_FONT_FACTORY_H_
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define SFNTLY_CPP_SRC_SFNTLY_FONT_FACTORY_H_
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <vector>
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "sfntly/port/refcount.h"
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "sfntly/port/type.h"
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "sfntly/font.h"
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace sfntly {
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class FontFactory : public RefCounted<FontFactory> {
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) public:
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  virtual ~FontFactory();
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // Factory method for the construction of a font factory.
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  static CALLER_ATTACH FontFactory* GetInstance();
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // Toggle whether fonts that are loaded are fingerprinted with a SHA-1 hash.
365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // If a font is fingerprinted then a SHA-1 hash is generated at load time and
375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // stored in the font. This is useful for uniquely identifying fonts. By
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // default this is turned on.
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // @param fingerprint whether fingerprinting should be turned on or off
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // TODO(arthurhsu): IMPLEMENT: C++ port currently don't do any SHA-1
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  void FingerprintFont(bool fingerprint);
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  bool FingerprintFont();
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // Load the font(s) from the input stream. The current settings on the factory
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // are used during the loading process. One or more fonts are returned if the
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // stream contains valid font data. Some font container formats may have more
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // than one font and in this case multiple font objects will be returned. If
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // the data in the stream cannot be parsed or is invalid an array of size zero
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // will be returned.
50  void LoadFonts(InputStream* is, FontArray* output);
51
52  // ByteArray font loading
53  // Load the font(s) from the byte array. The current settings on the factory
54  // are used during the loading process. One or more fonts are returned if the
55  // stream contains valid font data. Some font container formats may have more
56  // than one font and in this case multiple font objects will be returned. If
57  // the data in the stream cannot be parsed or is invalid an array of size zero
58  // will be returned.
59  void LoadFonts(ByteVector* b, FontArray* output);
60
61  // Load the font(s) from the input stream into font builders. The current
62  // settings on the factory are used during the loading process. One or more
63  // font builders are returned if the stream contains valid font data. Some
64  // font container formats may have more than one font and in this case
65  // multiple font builder objects will be returned. If the data in the stream
66  // cannot be parsed or is invalid an array of size zero will be returned.
67  void LoadFontsForBuilding(InputStream* is, FontBuilderArray* output);
68
69  // Load the font(s) from the byte array into font builders. The current
70  // settings on the factory are used during the loading process. One or more
71  // font builders are returned if the stream contains valid font data. Some
72  // font container formats may have more than one font and in this case
73  // multiple font builder objects will be returned. If the data in the stream
74  // cannot be parsed or is invalid an array of size zero will be returned.
75  void LoadFontsForBuilding(ByteVector* b, FontBuilderArray* output);
76
77  // Font serialization
78  // Serialize the font to the output stream.
79  // NOTE: in this port we attempted not to implement I/O stream because dealing
80  //       with cross-platform I/O stream itself is big enough as a project.
81  //       Byte buffer it is.
82  void SerializeFont(Font* font, OutputStream* os);
83
84  // Set the table ordering to be used in serializing a font. The table ordering
85  // is an ordered list of table ids and tables will be serialized in the order
86  // given. Any tables whose id is not listed in the ordering will be placed in
87  // an unspecified order following those listed.
88  void SetSerializationTableOrdering(const IntegerList& table_ordering);
89
90  // Get an empty font builder for creating a new font from scratch.
91  CALLER_ATTACH Font::Builder* NewFontBuilder();
92
93 private:
94  // Offsets to specific elements in the underlying data. These offsets are
95  // relative to the start of the table or the start of sub-blocks within the
96  // table.
97  struct Offset {
98    enum {
99      // Offsets within the main directory.
100      kTTCTag = 0,
101      kVersion = 4,
102      kNumFonts = 8,
103      kOffsetTable = 12,
104
105      // TTC Version 2.0 extensions.
106      // Offsets from end of OffsetTable.
107      kulDsigTag = 0,
108      kulDsigLength = 4,
109      kulDsigOffset = 8
110    };
111  };
112
113  FontFactory();
114
115  CALLER_ATTACH Font* LoadSingleOTF(InputStream* is);
116  CALLER_ATTACH Font* LoadSingleOTF(WritableFontData* wfd);
117
118  void LoadCollection(InputStream* is, FontArray* output);
119  void LoadCollection(WritableFontData* wfd, FontArray* output);
120
121  CALLER_ATTACH Font::Builder* LoadSingleOTFForBuilding(InputStream* is);
122  CALLER_ATTACH Font::Builder*
123      LoadSingleOTFForBuilding(WritableFontData* wfd,
124                               int32_t offset_to_offset_table);
125
126  void LoadCollectionForBuilding(InputStream* is, FontBuilderArray* builders);
127  void LoadCollectionForBuilding(WritableFontData* ba,
128                                 FontBuilderArray* builders);
129
130  static bool IsCollection(PushbackInputStream* pbis);
131  static bool IsCollection(ReadableFontData* wfd);
132
133  bool fingerprint_;
134  IntegerList table_ordering_;
135};
136typedef Ptr<FontFactory> FontFactoryPtr;
137
138}  // namespace sfntly
139
140#endif  // SFNTLY_CPP_SRC_SFNTLY_FONT_FACTORY_H_
141