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 #include "sfntly/table/core/horizontal_device_metrics_table.h"
18 
19 namespace sfntly {
20 /******************************************************************************
21  * HorizontalDeviceMetricsTable class
22  ******************************************************************************/
~HorizontalDeviceMetricsTable()23 HorizontalDeviceMetricsTable:: ~HorizontalDeviceMetricsTable() {}
24 
Version()25 int32_t HorizontalDeviceMetricsTable::Version() {
26   return data_->ReadUShort(Offset::kVersion);
27 }
28 
NumRecords()29 int32_t HorizontalDeviceMetricsTable::NumRecords() {
30   return data_->ReadShort(Offset::kNumRecords);
31 }
32 
RecordSize()33 int32_t HorizontalDeviceMetricsTable::RecordSize() {
34   return data_->ReadLong(Offset::kSizeDeviceRecord);
35 }
36 
PixelSize(int32_t record_index)37 int32_t HorizontalDeviceMetricsTable::PixelSize(int32_t record_index) {
38   if (record_index < 0 || record_index >= NumRecords()) {
39 #if !defined (SFNTLY_NO_EXCEPTION)
40     throw IndexOutOfBoundsException();
41 #endif
42     return -1;
43   }
44   return data_->ReadUByte(Offset::kRecords + record_index * RecordSize() +
45                           Offset::kDeviceRecordPixelSize);
46 }
47 
MaxWidth(int32_t record_index)48 int32_t HorizontalDeviceMetricsTable::MaxWidth(int32_t record_index) {
49   if (record_index < 0 || record_index >= NumRecords()) {
50 #if !defined (SFNTLY_NO_EXCEPTION)
51     throw IndexOutOfBoundsException();
52 #endif
53     return -1;
54   }
55   return data_->ReadUByte(Offset::kRecords + record_index * RecordSize() +
56                           Offset::kDeviceRecordMaxWidth);
57 }
58 
Width(int32_t record_index,int32_t glyph_num)59 int32_t HorizontalDeviceMetricsTable::Width(int32_t record_index,
60                                             int32_t glyph_num) {
61   if (record_index < 0 || record_index >= NumRecords() ||
62       glyph_num < 0 || glyph_num >= num_glyphs_) {
63 #if !defined (SFNTLY_NO_EXCEPTION)
64     throw IndexOutOfBoundsException();
65 #endif
66     return -1;
67   }
68   return data_->ReadUByte(Offset::kRecords + record_index * RecordSize() +
69                           Offset::kDeviceRecordWidths + glyph_num);
70 }
71 
HorizontalDeviceMetricsTable(Header * header,ReadableFontData * data,int32_t num_glyphs)72 HorizontalDeviceMetricsTable::HorizontalDeviceMetricsTable(
73     Header* header,
74     ReadableFontData* data,
75     int32_t num_glyphs)
76     : Table(header, data), num_glyphs_(num_glyphs) {
77 }
78 
79 /******************************************************************************
80  * HorizontalDeviceMetricsTable::Builder class
81  ******************************************************************************/
Builder(Header * header,WritableFontData * data)82 HorizontalDeviceMetricsTable::Builder::Builder(Header* header,
83                                                WritableFontData* data)
84     : TableBasedTableBuilder(header, data), num_glyphs_(-1) {
85 }
86 
Builder(Header * header,ReadableFontData * data)87 HorizontalDeviceMetricsTable::Builder::Builder(Header* header,
88                                                ReadableFontData* data)
89     : TableBasedTableBuilder(header, data), num_glyphs_(-1) {
90 }
91 
~Builder()92 HorizontalDeviceMetricsTable::Builder::~Builder() {}
93 
94 CALLER_ATTACH FontDataTable*
SubBuildTable(ReadableFontData * data)95 HorizontalDeviceMetricsTable::Builder::SubBuildTable(ReadableFontData* data) {
96   FontDataTablePtr table = new HorizontalDeviceMetricsTable(header(), data,
97                                                             num_glyphs_);
98   return table.Detach();
99 }
100 
SetNumGlyphs(int32_t num_glyphs)101 void HorizontalDeviceMetricsTable::Builder::SetNumGlyphs(int32_t num_glyphs) {
102   if (num_glyphs < 0) {
103 #if !defined (SFNTLY_NO_EXCEPTION)
104     throw IllegalArgumentException("Number of glyphs can't be negative.");
105 #endif
106     return;
107   }
108   num_glyphs_ = num_glyphs;
109   HorizontalDeviceMetricsTable* table =
110       down_cast<HorizontalDeviceMetricsTable*>(GetTable());
111   if (table) {
112     table->num_glyphs_ = num_glyphs;
113   }
114 }
115 
116 CALLER_ATTACH HorizontalDeviceMetricsTable::Builder*
CreateBuilder(Header * header,WritableFontData * data)117 HorizontalDeviceMetricsTable::Builder::CreateBuilder(Header* header,
118                                                      WritableFontData* data) {
119   Ptr<HorizontalDeviceMetricsTable::Builder> builder;
120   builder = new HorizontalDeviceMetricsTable::Builder(header, data);
121   return builder.Detach();
122 }
123 
124 }  // namespace sfntly
125