• 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 #ifndef SFNTLY_CPP_SRC_SFNTLY_FONT_FACTORY_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_FONT_FACTORY_H_
19 
20 #include <vector>
21 
22 #include "sfntly/port/refcount.h"
23 #include "sfntly/port/type.h"
24 #include "sfntly/font.h"
25 
26 namespace sfntly {
27 
28 class FontFactory : public RefCounted<FontFactory> {
29  public:
30   virtual ~FontFactory();
31 
32   // Factory method for the construction of a font factory.
33   static CALLER_ATTACH FontFactory* GetInstance();
34 
35   // Toggle whether fonts that are loaded are fingerprinted with a SHA-1 hash.
36   // If a font is fingerprinted then a SHA-1 hash is generated at load time and
37   // stored in the font. This is useful for uniquely identifying fonts. By
38   // default this is turned on.
39   // @param fingerprint whether fingerprinting should be turned on or off
40   // TODO(arthurhsu): IMPLEMENT: C++ port currently don't do any SHA-1
41   void FingerprintFont(bool fingerprint);
42   bool FingerprintFont();
43 
44   // Load the font(s) from the input stream. The current settings on the factory
45   // are used during the loading process. One or more fonts are returned if the
46   // stream contains valid font data. Some font container formats may have more
47   // than one font and in this case multiple font objects will be returned. If
48   // the data in the stream cannot be parsed or is invalid an array of size zero
49   // 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(std::vector<uint8_t>* 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(std::vector<uint8_t>* 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   // Get an empty font builder for creating a new font from scratch.
85   CALLER_ATTACH Font::Builder* NewFontBuilder();
86 
87  private:
88   // Offsets to specific elements in the underlying data. These offsets are
89   // relative to the start of the table or the start of sub-blocks within the
90   // table.
91   struct Offset {
92     enum {
93       // Offsets within the main directory.
94       kTTCTag = 0,
95       kVersion = 4,
96       kNumFonts = 8,
97       kOffsetTable = 12,
98 
99       // TTC Version 2.0 extensions.
100       // Offsets from end of OffsetTable.
101       kulDsigTag = 0,
102       kulDsigLength = 4,
103       kulDsigOffset = 8
104     };
105   };
106 
107   FontFactory();
108 
109   CALLER_ATTACH Font* LoadSingleOTF(InputStream* is);
110   CALLER_ATTACH Font* LoadSingleOTF(WritableFontData* wfd);
111 
112   void LoadCollection(InputStream* is, FontArray* output);
113   void LoadCollection(WritableFontData* wfd, FontArray* output);
114 
115   CALLER_ATTACH Font::Builder* LoadSingleOTFForBuilding(InputStream* is);
116   CALLER_ATTACH Font::Builder*
117       LoadSingleOTFForBuilding(WritableFontData* wfd,
118                                int32_t offset_to_offset_table);
119 
120   void LoadCollectionForBuilding(InputStream* is, FontBuilderArray* builders);
121   void LoadCollectionForBuilding(WritableFontData* ba,
122                                  FontBuilderArray* builders);
123 
124   static bool IsCollection(PushbackInputStream* pbis);
125   static bool IsCollection(ReadableFontData* wfd);
126 
127   bool fingerprint_;
128 };
129 typedef Ptr<FontFactory> FontFactoryPtr;
130 
131 }  // namespace sfntly
132 
133 #endif  // SFNTLY_CPP_SRC_SFNTLY_FONT_FACTORY_H_
134