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_CORE_HORIZONTAL_METRICS_TABLE_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_TABLE_CORE_HORIZONTAL_METRICS_TABLE_H_
19 
20 #include "sfntly/table/table.h"
21 #include "sfntly/table/table_based_table_builder.h"
22 
23 namespace sfntly {
24 
25 // A Horizontal Metrics table - 'hmtx'.
26 class HorizontalMetricsTable : public Table,
27                                public RefCounted<HorizontalMetricsTable> {
28  public:
29   // Builder for a Horizontal Metrics Table - 'hmtx'.
30   class Builder : public TableBasedTableBuilder, public RefCounted<Builder> {
31    public:
32     // Constructor scope altered to public because C++ does not allow base
33     // class to instantiate derived class with protected constructors.
34     Builder(Header* header, WritableFontData* data);
35     Builder(Header* header, ReadableFontData* data);
36     virtual ~Builder();
37 
38     virtual CALLER_ATTACH FontDataTable* SubBuildTable(ReadableFontData* data);
39     static CALLER_ATTACH Builder* CreateBuilder(Header* header,
40                                                 WritableFontData* data);
41 
42     void SetNumberOfHMetrics(int32_t num_hmetrics);
43     void SetNumGlyphs(int32_t num_glyphs);
44 
45    private:
46     int32_t num_hmetrics_;
47     int32_t num_glyphs_;
48   };
49 
50   virtual ~HorizontalMetricsTable();
51   int32_t NumberOfHMetrics();
52   int32_t NumberOfLSBs();
53   int32_t HMetricAdvanceWidth(int32_t entry);
54   int32_t HMetricLSB(int32_t entry);
55   int32_t LsbTableEntry(int32_t entry);
56   int32_t AdvanceWidth(int32_t glyph_id);
57   int32_t LeftSideBearing(int32_t glyph_id);
58 
59  private:
60   struct Offset {
61     enum {
62       // hMetrics
63       kHMetricsStart = 0,
64       kHMetricsSize = 4,
65 
66       // Offset within an hMetric
67       kHMetricsAdvanceWidth = 0,
68       kHMetricsLeftSideBearing = 2,
69 
70       kLeftSideBearingSize = 2
71     };
72   };
73 
74   HorizontalMetricsTable(Header* header,
75                          ReadableFontData* data,
76                          int32_t num_hmetrics,
77                          int32_t num_glyphs);
78 
79   int32_t num_hmetrics_;
80   int32_t num_glyphs_;
81 };
82 typedef Ptr<HorizontalMetricsTable> HorizontalMetricsTablePtr;
83 typedef Ptr<HorizontalMetricsTable::Builder> HorizontalMetricsTableBuilderPtr;
84 
85 }  // namespace sfntly
86 
87 #endif  // SFNTLY_CPP_SRC_SFNTLY_TABLE_CORE_HORIZONTAL_METRICS_TABLE_H_
88