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_H_
185af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#define SFNTLY_CPP_SRC_SFNTLY_FONT_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/port/endian.h"
25464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/data/font_input_stream.h"
26464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/data/font_output_stream.h"
27464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/data/writable_font_data.h"
285af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#include "sfntly/table/table.h"
29464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
30464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comnamespace sfntly {
31464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
32464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// Note: following constants are embedded in Font class in Java.  They are
33464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//       extracted out for easier reference from other classes.  Offset is the
34464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//       one that is kept within class.
35464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// Platform ids. These are used in a number of places within the font whenever
36464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// the platform needs to be specified.
37464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comstruct PlatformId {
3844bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com  enum {
3944bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnknown = -1,
4044bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnicode = 0,
4144bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kMacintosh = 1,
4244bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kISO = 2,
4344bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kWindows = 3,
4444bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kCustom = 4
4544bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com  };
46464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
47464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
48464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// Unicode encoding ids. These are used in a number of places within the font
49464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// whenever character encodings need to be specified.
50464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comstruct UnicodeEncodingId {
5144bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com  enum {
5244bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnknown = -1,
5344bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnicode1_0 = 0,
5444bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnicode1_1 = 1,
5544bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kISO10646 = 2,
5644bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnicode2_0_BMP = 3,
5744bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnicode2_0 = 4,
5844bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnicodeVariationSequences = 5
5944bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com  };
60464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
61464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
62464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// Windows encoding ids. These are used in a number of places within the font
63464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// whenever character encodings need to be specified.
64464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comstruct WindowsEncodingId {
6544bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com  enum {
6644bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnknown = 0xffffffff,
6744bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kSymbol = 0,
6844bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnicodeUCS2 = 1,
6944bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kShiftJIS = 2,
7044bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kPRC = 3,
7144bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kBig5 = 4,
7244bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kWansung = 5,
7344bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kJohab = 6,
7444bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnicodeUCS4 = 10
7544bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com  };
76464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
77464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
78464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// Macintosh encoding ids. These are used in a number of places within the
79464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// font whenever character encodings need to be specified.
80464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comstruct MacintoshEncodingId {
81464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Macintosh Platform Encodings
8244bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com  enum {
8344bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUnknown = -1,
8444bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kRoman = 0,
8544bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kJapanese = 1,
8644bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kChineseTraditional = 2,
8744bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kKorean = 3,
8844bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kArabic = 4,
8944bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kHebrew = 5,
9044bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kGreek = 6,
9144bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kRussian = 7,
9244bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kRSymbol = 8,
9344bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kDevanagari = 9,
9444bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kGurmukhi = 10,
9544bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kGujarati = 11,
9644bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kOriya = 12,
9744bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kBengali = 13,
9844bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kTamil = 14,
9944bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kTelugu = 15,
10044bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kKannada = 16,
10144bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kMalayalam = 17,
10244bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kSinhalese = 18,
10344bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kBurmese = 19,
10444bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kKhmer = 20,
10544bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kThai = 21,
10644bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kLaotian = 22,
10744bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kGeorgian = 23,
10844bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kArmenian = 24,
10944bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kChineseSimplified = 25,
11044bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kTibetan = 26,
11144bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kMongolian = 27,
11244bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kGeez = 28,
11344bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kSlavic = 29,
11444bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kVietnamese = 30,
11544bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kSindhi = 31,
11644bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com    kUninterpreted = 32
11744bcb4a1475aeb0c4bce1cba0c30f5ee01eb50d9arthurhsu@google.com  };
118464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
119464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
120464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comclass FontFactory;
121b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
122b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com// An sfnt container font object. This object is immutable and thread safe. To
123b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com// construct one use an instance of Font::Builder.
124464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comclass Font : public RefCounted<Font> {
125464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com public:
126b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // A builder for a font object. The builder allows the for the creation of
127b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // immutable Font objects. The builder is a one use non-thread safe object and
128b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // once the Font object has been created it is no longer usable. To create a
129b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // further Font object new builder will be required.
130b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  class Builder : public RefCounted<Builder> {
131464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com   public:
132464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    virtual ~Builder();
133464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
134464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    static CALLER_ATTACH Builder*
135246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com        GetOTFBuilder(FontFactory* factory, InputStream* is);
136464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    static CALLER_ATTACH Builder*
137246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com        GetOTFBuilder(FontFactory* factory,
138b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com                      WritableFontData* ba,
139464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                      int32_t offset_to_offset_table);
140246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    static CALLER_ATTACH Builder* GetOTFBuilder(FontFactory* factory);
141b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
142b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Get the font factory that created this font builder.
143b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    FontFactory* GetFontFactory() { return factory_; }
144b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
145b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Is the font ready to build?
146b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    bool ReadyToBuild();
147b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
148b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Build the Font. After this call this builder will no longer be usable.
149b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    CALLER_ATTACH Font* Build();
150b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
151b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Set a unique fingerprint for the font object.
152b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    void SetDigest(ByteVector* digest);
153b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
154b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Clear all table builders.
155b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    void ClearTableBuilders();
156b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
157b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Does this font builder have the specified table builder.
158b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    bool HasTableBuilder(int32_t tag);
159b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
160b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Get the table builder for the given tag. If there is no builder for that
161b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // tag then return a null.
162b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    Table::Builder* GetTableBuilder(int32_t tag);
163464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
164464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // Creates a new table builder for the table type given by the table id tag.
165464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // This new table has been added to the font and will replace any existing
166464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // builder for that table.
167464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // @return new empty table of the type specified by tag; if tag is not known
168464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    //         then a generic OpenTypeTable is returned
1696c4f92bcc799598f6fcba4b3c7d4d549da9a8491dfilimon@google.com    virtual Table::Builder* NewTableBuilder(int32_t tag);
170b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
171b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Creates a new table builder for the table type given by the table id tag.
172b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // It makes a copy of the data provided and uses that copy for the table.
173b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // This new table has been added to the font and will replace any existing
174b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // builder for that table.
1756c4f92bcc799598f6fcba4b3c7d4d549da9a8491dfilimon@google.com    virtual Table::Builder* NewTableBuilder(int32_t tag,
1766c4f92bcc799598f6fcba4b3c7d4d549da9a8491dfilimon@google.com                                            ReadableFontData* src_data);
177b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
178b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Get a map of the table builders in this font builder accessed by table
179b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // tag.
180246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual TableBuilderMap* table_builders() { return &table_builders_; }
181b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
182b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Remove the specified table builder from the font builder.
183464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // Note: different from Java: we don't return object in removeTableBuilder
184246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual void RemoveTableBuilder(int32_t tag);
185b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
186b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    // Get the number of table builders in the font builder.
187246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual int32_t number_of_table_builders() {
188246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      return (int32_t)table_builders_.size();
189246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    }
190464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
191464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com   private:
192246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    explicit Builder(FontFactory* factory);
193246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual void LoadFont(InputStream* is);
194b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    virtual void LoadFont(WritableFontData* wfd,
195b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com                          int32_t offset_to_offset_table);
196246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    int32_t SfntWrapperSize();
197246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    void BuildAllTableBuilders(DataBlockMap* table_data,
198464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                               TableBuilderMap* builder_map);
199464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    CALLER_ATTACH Table::Builder*
2006b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com        GetTableBuilder(Header* header, WritableFontData* data);
201b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    void BuildTablesFromBuilders(Font* font,
202b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com                                 TableBuilderMap* builder_map,
203464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                                 TableMap* tables);
204246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    static void InterRelateBuilders(TableBuilderMap* builder_map);
205b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
206b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    void ReadHeader(FontInputStream* is,
207b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com                    HeaderOffsetSortedSet* records);
208b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
209246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    void ReadHeader(ReadableFontData* fd,
210246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                    int32_t offset,
211b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com                    HeaderOffsetSortedSet* records);
212b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
213b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    void LoadTableData(HeaderOffsetSortedSet* headers,
214246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                       FontInputStream* is,
215246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                       DataBlockMap* table_data);
216b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
217b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    void LoadTableData(HeaderOffsetSortedSet* headers,
218246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                       WritableFontData* fd,
219464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                       DataBlockMap* table_data);
220464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
221464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    TableBuilderMap table_builders_;
222464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    FontFactory* factory_;  // dumb pointer, avoid circular refcounting
223464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    int32_t sfnt_version_;
224464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    int32_t num_tables_;
225464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    int32_t search_range_;
226464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    int32_t entry_selector_;
227464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    int32_t range_shift_;
228464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    DataBlockMap data_blocks_;
229464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    ByteVector digest_;
230464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  };
231464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
232246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual ~Font();
233246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
234246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Gets the sfnt version set in the sfnt wrapper of the font.
235b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  int32_t sfnt_version() { return sfnt_version_; }
236246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
237246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Gets a copy of the fonts digest that was created when the font was read. If
238246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // no digest was set at creation time then the return result will be null.
239246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  ByteVector* digest() { return &digest_; }
240246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
241246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Get the checksum for this font.
242246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  int64_t checksum() { return checksum_; }
243246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
244246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Get the number of tables in this font.
245246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  int32_t num_tables() { return (int32_t)tables_.size(); }
246246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
247246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Whether the font has a particular table.
248246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  bool HasTable(int32_t tag);
249246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
250246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // UNIMPLEMENTED: public Iterator<? extends Table> iterator
251246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
252246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Get the table in this font with the specified id.
253246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // @param tag the identifier of the table
254246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // @return the table specified if it exists; null otherwise
255246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // C++ port: rename table() to GetTable()
256246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  Table* GetTable(int32_t tag);
257246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
258246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Get a map of the tables in this font accessed by table tag.
259246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // @return an unmodifiable view of the tables in this font
260b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // Note: renamed tableMap() to GetTableMap()
261b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  const TableMap* GetTableMap();
262b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
263b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // UNIMPLEMENTED: toString()
264246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
265246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Serialize the font to the output stream.
266246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // @param os the destination for the font serialization
267246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // @param tableOrdering the table ordering to apply
268246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void Serialize(OutputStream* os, IntegerList* table_ordering);
269246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
270464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com private:
271246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Offsets to specific elements in the underlying data. These offsets are
272246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // relative to the start of the table or the start of sub-blocks within the
273246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // table.
274246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  struct Offset {
275246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    enum {
276246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      // Offsets within the main directory
277246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kSfntVersion = 0,
278246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kNumTables = 4,
279246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kSearchRange = 6,
280246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kEntrySelector = 8,
281246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kRangeShift = 10,
282246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kTableRecordBegin = 12,
283246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kSfntHeaderSize = 12,
284246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
285246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      // Offsets within a specific table record
286246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kTableTag = 0,
287246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kTableCheckSum = 4,
288246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kTableOffset = 8,
289246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kTableLength = 12,
290246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      kTableRecordSize = 16
291246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    };
292246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  };
293246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
294246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Note: the two constants are moved to tag.h to avoid VC++ bug.
295246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com//  static const int32_t CFF_TABLE_ORDERING[];
296246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com//  static const int32_t TRUE_TYPE_TABLE_ORDERING[];
297246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
298b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // Constructor.
299b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @param sfntVersion the sfnt version
300b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @param digest the computed digest for the font; null if digest was not
301b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  //        computed
302b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // Note: Current C++ port does not support SHA digest validation.
303b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  Font(int32_t sfnt_version, ByteVector* digest);
304b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
305b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // Build the table headers to be used for serialization. These headers will be
306b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // filled out with the data required for serialization. The headers will be
307b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // sorted in the order specified and only those specified will have headers
308b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // generated.
309b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @param tableOrdering the tables to generate headers for and the order to
310b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  //        sort them
311b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @return a list of table headers ready for serialization
312246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void BuildTableHeadersForSerialization(IntegerList* table_ordering,
313246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                                         TableHeaderList* table_headers);
314b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
315b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // Searialize the headers.
316b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @param fos the destination stream for the headers
317b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @param tableHeaders the headers to serialize
318b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @throws IOException
319246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void SerializeHeader(FontOutputStream* fos, TableHeaderList* table_headers);
320b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
321b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // Serialize the tables.
322b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @param fos the destination stream for the headers
323b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @param tableHeaders the headers for the tables to serialize
324b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @throws IOException
325246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void SerializeTables(FontOutputStream* fos, TableHeaderList* table_headers);
326b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
327b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // Generate the full table ordering to used for serialization. The full
328b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // ordering uses the partial ordering as a seed and then adds all remaining
329b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // tables in the font in an undefined order.
330b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @param defaultTableOrdering the partial ordering to be used as a seed for
331b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  //        the full ordering
332b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @param (out) table_ordering the full ordering for serialization
333b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  void GenerateTableOrdering(IntegerList* default_table_ordering,
334b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com                             IntegerList* table_ordering);
335b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
336b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // Get the default table ordering based on the type of the font.
337b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // @param (out) default_table_ordering the default table ordering
338246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void DefaultTableOrdering(IntegerList* default_table_ordering);
339246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
340464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  int32_t sfnt_version_;
341464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  ByteVector digest_;
342464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  int64_t checksum_;
343464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  TableMap tables_;
344464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
345464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comtypedef Ptr<Font> FontPtr;
346464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comtypedef std::vector<FontPtr> FontArray;
347464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comtypedef Ptr<Font::Builder> FontBuilderPtr;
348464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comtypedef std::vector<FontBuilderPtr> FontBuilderArray;
349464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
350464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}  // namespace sfntly
351464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
3525af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#endif  // SFNTLY_CPP_SRC_SFNTLY_FONT_H_
353