1464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com/*
2464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Copyright 2011 Google Inc. All Rights Reserved.
3464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
4464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Licensed under the Apache License, Version 2.0 (the "License");
5464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * you may not use this file except in compliance with the License.
6464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * You may obtain a copy of the License at
7464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
8464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *      http://www.apache.org/licenses/LICENSE-2.0
9464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
10464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Unless required by applicable law or agreed to in writing, software
11464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * distributed under the License is distributed on an "AS IS" BASIS,
12464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * See the License for the specific language governing permissions and
14464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * limitations under the License.
15464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com */
16464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
175af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#ifndef SFNTLY_CPP_SRC_SFNTLY_FONT_FACTORY_H_
185af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#define SFNTLY_CPP_SRC_SFNTLY_FONT_FACTORY_H_
19464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
20464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include <vector>
21464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
22464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/port/refcount.h"
23464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/port/type.h"
24464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/font.h"
25464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
26464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comnamespace sfntly {
27464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
28464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comclass FontFactory : public RefCounted<FontFactory> {
29246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com public:
30464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  virtual ~FontFactory();
31464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
32464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Factory method for the construction of a font factory.
33246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  static CALLER_ATTACH FontFactory* GetInstance();
34464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
35464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Toggle whether fonts that are loaded are fingerprinted with a SHA-1 hash.
36464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // If a font is fingerprinted then a SHA-1 hash is generated at load time and
37464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // stored in the font. This is useful for uniquely identifying fonts. By
38464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // default this is turned on.
39464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // @param fingerprint whether fingerprinting should be turned on or off
40464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // TODO(arthurhsu): IMPLEMENT: C++ port currently don't do any SHA-1
41246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void FingerprintFont(bool fingerprint);
42246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  bool FingerprintFont();
43464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
44464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Load the font(s) from the input stream. The current settings on the factory
45464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // are used during the loading process. One or more fonts are returned if the
46464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // stream contains valid font data. Some font container formats may have more
47464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // than one font and in this case multiple font objects will be returned. If
48464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // the data in the stream cannot be parsed or is invalid an array of size zero
49464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // will be returned.
50246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void LoadFonts(InputStream* is, FontArray* output);
51464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
52464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // ByteArray font loading
53464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Load the font(s) from the byte array. The current settings on the factory
54464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // are used during the loading process. One or more fonts are returned if the
55464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // stream contains valid font data. Some font container formats may have more
56464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // than one font and in this case multiple font objects will be returned. If
57464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // the data in the stream cannot be parsed or is invalid an array of size zero
58464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // will be returned.
59b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  void LoadFonts(ByteVector* b, FontArray* output);
60464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
61246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Load the font(s) from the input stream into font builders. The current
62464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // settings on the factory are used during the loading process. One or more
63464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // font builders are returned if the stream contains valid font data. Some
64464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // font container formats may have more than one font and in this case
65464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // multiple font builder objects will be returned. If the data in the stream
66464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // cannot be parsed or is invalid an array of size zero will be returned.
67246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void LoadFontsForBuilding(InputStream* is, FontBuilderArray* output);
68464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
69246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Load the font(s) from the byte array into font builders. The current
70246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // settings on the factory are used during the loading process. One or more
71246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // font builders are returned if the stream contains valid font data. Some
72246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // font container formats may have more than one font and in this case
73246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // multiple font builder objects will be returned. If the data in the stream
74246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // cannot be parsed or is invalid an array of size zero will be returned.
75b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  void LoadFontsForBuilding(ByteVector* b, FontBuilderArray* output);
76464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
77464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Font serialization
78464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Serialize the font to the output stream.
79464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // NOTE: in this port we attempted not to implement I/O stream because dealing
80464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  //       with cross-platform I/O stream itself is big enough as a project.
81464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  //       Byte buffer it is.
82246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void SerializeFont(Font* font, OutputStream* os);
83464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
84464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Set the table ordering to be used in serializing a font. The table ordering
85464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // is an ordered list of table ids and tables will be serialized in the order
86464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // given. Any tables whose id is not listed in the ordering will be placed in
87464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // an unspecified order following those listed.
88246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void SetSerializationTableOrdering(const IntegerList& table_ordering);
89464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
90464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Get an empty font builder for creating a new font from scratch.
91246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  CALLER_ATTACH Font::Builder* NewFontBuilder();
92464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
93464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com private:
94246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Offsets to specific elements in the underlying data. These offsets are
95246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // relative to the start of the table or the start of sub-blocks within the
96246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // table.
97246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  struct Offset {
98246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    enum {
99246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      // Offsets within the main directory.
100246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kTTCTag = 0,
101246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kVersion = 4,
102246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kNumFonts = 8,
103246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kOffsetTable = 12,
104246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
105246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      // TTC Version 2.0 extensions.
106246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      // Offsets from end of OffsetTable.
107246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kulDsigTag = 0,
108246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kulDsigLength = 4,
109246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kulDsigOffset = 8
110246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    };
111246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  };
112246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
113246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  FontFactory();
114246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
115246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  CALLER_ATTACH Font* LoadSingleOTF(InputStream* is);
116b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  CALLER_ATTACH Font* LoadSingleOTF(WritableFontData* wfd);
117246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
118246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void LoadCollection(InputStream* is, FontArray* output);
119b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  void LoadCollection(WritableFontData* wfd, FontArray* output);
120246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
121246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  CALLER_ATTACH Font::Builder* LoadSingleOTFForBuilding(InputStream* is);
122246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  CALLER_ATTACH Font::Builder*
123b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com      LoadSingleOTFForBuilding(WritableFontData* wfd,
124b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com                               int32_t offset_to_offset_table);
125246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
126246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void LoadCollectionForBuilding(InputStream* is, FontBuilderArray* builders);
127b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  void LoadCollectionForBuilding(WritableFontData* ba,
128b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com                                 FontBuilderArray* builders);
129246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
130246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  static bool IsCollection(PushbackInputStream* pbis);
131b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  static bool IsCollection(ReadableFontData* wfd);
132246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
133464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  bool fingerprint_;
134464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  IntegerList table_ordering_;
135464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
136464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comtypedef Ptr<FontFactory> FontFactoryPtr;
137464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
138464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}  // namespace sfntly
139464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
1405af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#endif  // SFNTLY_CPP_SRC_SFNTLY_FONT_FACTORY_H_
141