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
19namespace sfntly {
20/******************************************************************************
21 * HorizontalDeviceMetricsTable class
22 ******************************************************************************/
23HorizontalDeviceMetricsTable:: ~HorizontalDeviceMetricsTable() {}
24
25int32_t HorizontalDeviceMetricsTable::Version() {
26  return data_->ReadUShort(Offset::kVersion);
27}
28
29int32_t HorizontalDeviceMetricsTable::NumRecords() {
30  return data_->ReadShort(Offset::kNumRecords);
31}
32
33int32_t HorizontalDeviceMetricsTable::RecordSize() {
34  return data_->ReadLong(Offset::kSizeDeviceRecord);
35}
36
37int32_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
48int32_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
59int32_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
72HorizontalDeviceMetricsTable::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 ******************************************************************************/
82HorizontalDeviceMetricsTable::Builder::Builder(Header* header,
83                                               WritableFontData* data)
84    : TableBasedTableBuilder(header, data), num_glyphs_(-1) {
85}
86
87HorizontalDeviceMetricsTable::Builder::Builder(Header* header,
88                                               ReadableFontData* data)
89    : TableBasedTableBuilder(header, data), num_glyphs_(-1) {
90}
91
92HorizontalDeviceMetricsTable::Builder::~Builder() {}
93
94CALLER_ATTACH FontDataTable*
95HorizontalDeviceMetricsTable::Builder::SubBuildTable(ReadableFontData* data) {
96  FontDataTablePtr table = new HorizontalDeviceMetricsTable(header(), data,
97                                                            num_glyphs_);
98  return table.Detach();
99}
100
101void 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
116CALLER_ATTACH HorizontalDeviceMetricsTable::Builder*
117HorizontalDeviceMetricsTable::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