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_TABLE_TABLE_H_
18#define SFNTLY_CPP_SRC_SFNTLY_TABLE_TABLE_H_
19
20#include <set>
21#include <map>
22#include <vector>
23#include <utility>
24
25#include "sfntly/port/type.h"
26#include "sfntly/table/font_data_table.h"
27#include "sfntly/table/header.h"
28
29namespace sfntly {
30class Font;
31
32// A concrete implementation of a root level table in the font. This is the base
33// class used for all specific table implementations and is used as the generic
34// table for all tables which have no specific implementations.
35class Table : public FontDataTable {
36 public:
37  // Note: original version is Builder<T extends Table>
38  //       C++ template is not designed that way so plain old inheritance is
39  //       chosen.
40  class Builder : public FontDataTable::Builder {
41   public:
42    virtual ~Builder();
43    virtual Header* header() { return header_; }
44    virtual void NotifyPostTableBuild(FontDataTable* table);
45
46    // Get a builder for the table type specified by the data in the header.
47    // @param header the header for the table
48    // @param tableData the data to be used to build the table from
49    // @return builder for the table specified
50    static CALLER_ATTACH Builder* GetBuilder(Header* header,
51                                             WritableFontData* table_data);
52
53    // UNIMPLEMENTED: toString()
54
55   protected:
56    Builder(Header* header, WritableFontData* data);
57    Builder(Header* header, ReadableFontData* data);
58    Builder(Header* header);
59
60   private:
61    Ptr<Header> header_;
62  };
63
64  // Note: GenericTableBuilder moved to table_based_table_builder.h to avoid
65  //       circular inclusion.
66
67  virtual ~Table();
68
69  // Get the calculated checksum for the data in the table.
70  virtual int64_t CalculatedChecksum();
71
72  // Get the header for the table.
73  virtual Header* header()          { return header_; }
74
75  // Get the tag for the table from the record header.
76  virtual int32_t header_tag()      { return header_->tag(); }
77
78  // Get the offset for the table from the record header.
79  virtual int32_t header_offset()   { return header_->offset(); }
80
81  // Get the length of the table from the record header.
82  virtual int32_t header_length()   { return header_->length(); }
83
84  // Get the checksum for the table from the record header.
85  virtual int64_t header_checksum() { return header_->checksum(); }
86
87  // UNIMPLEMENTED: toString()
88
89  virtual void SetFont(Font* font);
90
91 protected:
92  Table(Header* header, ReadableFontData* data);
93
94 private:
95  Ptr<Header> header_;
96  Ptr<Font> font_;
97};
98
99// C++ port only
100class GenericTable : public Table, public RefCounted<GenericTable> {
101 public:
102  GenericTable(Header* header, ReadableFontData* data) : Table(header, data) {}
103  virtual ~GenericTable() {}
104};
105
106typedef Ptr<Table> TablePtr;
107typedef std::vector<HeaderPtr> TableHeaderList;
108typedef Ptr<Table::Builder> TableBuilderPtr;
109typedef std::map<int32_t, TablePtr> TableMap;
110typedef std::pair<int32_t, TablePtr> TableMapEntry;
111
112typedef std::map<HeaderPtr, WritableFontDataPtr> DataBlockMap;
113typedef std::pair<HeaderPtr, WritableFontDataPtr> DataBlockEntry;
114typedef std::map<int32_t, TableBuilderPtr> TableBuilderMap;
115typedef std::pair<int32_t, TableBuilderPtr> TableBuilderEntry;
116
117}  // namespace sfntly
118
119#endif  // SFNTLY_CPP_SRC_SFNTLY_TABLE_TABLE_H_
120