summaryrefslogtreecommitdiff
path: root/sfntly/table/bitmap/index_sub_table_format2.cc
blob: ce73e9beec81fbffe89799c901c0466786ced37b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sfntly/table/bitmap/index_sub_table_format2.h"

#include "sfntly/table/bitmap/eblc_table.h"

namespace sfntly {
/******************************************************************************
 * IndexSubTableFormat2 class
 ******************************************************************************/
IndexSubTableFormat2::~IndexSubTableFormat2() {
}

int32_t IndexSubTableFormat2::ImageSize() {
  return data_->ReadULongAsInt(EblcTable::Offset::kIndexSubTable2_imageSize);
}

CALLER_ATTACH BigGlyphMetrics* IndexSubTableFormat2::BigMetrics() {
  ReadableFontDataPtr slice;
  slice.Attach(down_cast<ReadableFontData*>(
      data_->Slice(EblcTable::Offset::kIndexSubTable2_bigGlyphMetrics,
                   BigGlyphMetrics::Offset::kMetricsLength)));
  BigGlyphMetricsPtr output = new BigGlyphMetrics(slice);
  return output.Detach();
}

int32_t IndexSubTableFormat2::NumGlyphs() {
  return last_glyph_index() - first_glyph_index() + 1;
}

int32_t IndexSubTableFormat2::GlyphStartOffset(int32_t glyph_id) {
  int32_t loca = CheckGlyphRange(glyph_id);
  if (loca == -1) {
    return -1;
  }
  return loca * image_size_;
}

int32_t IndexSubTableFormat2::GlyphLength(int32_t glyph_id) {
  if (CheckGlyphRange(glyph_id) == -1) {
    return 0;
  }
  return image_size_;
}

IndexSubTableFormat2::IndexSubTableFormat2(ReadableFontData* data,
                                           int32_t first,
                                           int32_t last)
    : IndexSubTable(data, first, last) {
  image_size_ =
      data_->ReadULongAsInt(EblcTable::Offset::kIndexSubTable2_imageSize);
}

/******************************************************************************
 * IndexSubTableFormat2::Builder class
 ******************************************************************************/
IndexSubTableFormat2::Builder::~Builder() {
}

int32_t IndexSubTableFormat2::Builder::NumGlyphs() {
  return last_glyph_index() - first_glyph_index() + 1;
}

int32_t IndexSubTableFormat2::Builder::GlyphStartOffset(int32_t glyph_id) {
  int32_t loca = CheckGlyphRange(glyph_id);
  if (loca == -1) {
    return -1;
  }
  return loca * ImageSize();
}

int32_t IndexSubTableFormat2::Builder::GlyphLength(int32_t glyph_id) {
  int32_t loca = CheckGlyphRange(glyph_id);
  if (loca == -1) {
    return 0;
  }
  return ImageSize();
}

CALLER_ATTACH IndexSubTableFormat2::Builder::BitmapGlyphInfoIterator*
    IndexSubTableFormat2::Builder::GetIterator() {
  Ptr<IndexSubTableFormat2::Builder::BitmapGlyphInfoIterator> it =
      new IndexSubTableFormat2::Builder::BitmapGlyphInfoIterator(this);
  return it.Detach();
}

int32_t IndexSubTableFormat2::Builder::ImageSize() {
  return InternalReadData()->ReadULongAsInt(
      EblcTable::Offset::kIndexSubTable2_imageSize);
}

void IndexSubTableFormat2::Builder::SetImageSize(int32_t image_size) {
  InternalWriteData()->WriteULong(EblcTable::Offset::kIndexSubTable2_imageSize,
                                  image_size);
}

BigGlyphMetrics::Builder* IndexSubTableFormat2::Builder::BigMetrics() {
  if (metrics_ == NULL) {
    WritableFontDataPtr data;
    data.Attach(down_cast<WritableFontData*>(InternalWriteData()->Slice(
        EblcTable::Offset::kIndexSubTable2_bigGlyphMetrics,
        BigGlyphMetrics::Offset::kMetricsLength)));
    metrics_ = new BigGlyphMetrics::Builder(data);
  }
  return metrics_;
}

// static
CALLER_ATTACH IndexSubTableFormat2::Builder*
IndexSubTableFormat2::Builder::CreateBuilder() {
  IndexSubTableFormat2BuilderPtr output = new IndexSubTableFormat2::Builder();
  return output.Detach();
}

// static
CALLER_ATTACH IndexSubTableFormat2::Builder*
IndexSubTableFormat2::Builder::CreateBuilder(ReadableFontData* data,
                                             int32_t index_sub_table_offset,
                                             int32_t first_glyph_index,
                                             int32_t last_glyph_index) {
  int32_t length = Builder::DataLength(data,
                                       index_sub_table_offset,
                                       first_glyph_index,
                                       last_glyph_index);
  ReadableFontDataPtr new_data;
  new_data.Attach(down_cast<ReadableFontData*>(
      data->Slice(index_sub_table_offset, length)));
  if (new_data == NULL) {
    return NULL;
  }
  IndexSubTableFormat2BuilderPtr output =
      new IndexSubTableFormat2::Builder(new_data,
                                        first_glyph_index,
                                        last_glyph_index);
  return output.Detach();
}

// static
CALLER_ATTACH IndexSubTableFormat2::Builder*
IndexSubTableFormat2::Builder::CreateBuilder(WritableFontData* data,
                                             int32_t index_sub_table_offset,
                                             int32_t first_glyph_index,
                                             int32_t last_glyph_index) {
  int32_t length = Builder::DataLength(data,
                                       index_sub_table_offset,
                                       first_glyph_index,
                                       last_glyph_index);
  WritableFontDataPtr new_data;
  new_data.Attach(down_cast<WritableFontData*>(
      data->Slice(index_sub_table_offset, length)));
  IndexSubTableFormat2BuilderPtr output =
      new IndexSubTableFormat2::Builder(new_data,
                                        first_glyph_index,
                                        last_glyph_index);
  return output.Detach();
}

CALLER_ATTACH FontDataTable* IndexSubTableFormat2::Builder::SubBuildTable(
    ReadableFontData* data) {
  IndexSubTableFormat2Ptr output = new IndexSubTableFormat2(
      data, first_glyph_index(), last_glyph_index());
  return output.Detach();
}

void IndexSubTableFormat2::Builder::SubDataSet() {
  Revert();
}

int32_t IndexSubTableFormat2::Builder::SubDataSizeToSerialize() {
  return EblcTable::Offset::kIndexSubTable2Length;
}

bool IndexSubTableFormat2::Builder::SubReadyToSerialize() {
  return true;
}

int32_t IndexSubTableFormat2::Builder::SubSerialize(
    WritableFontData* new_data) {
  int32_t size = SerializeIndexSubHeader(new_data);
  if (metrics_ == NULL) {
    ReadableFontDataPtr source;
    WritableFontDataPtr target;
    source.Attach(down_cast<ReadableFontData*>(
        InternalReadData()->Slice(size)));
    target.Attach(down_cast<WritableFontData*>(new_data->Slice(size)));
    size += source->CopyTo(target);
  } else {
    WritableFontDataPtr slice;
    size += new_data->WriteLong(EblcTable::Offset::kIndexSubTable2_imageSize,
                                ImageSize());
    slice.Attach(down_cast<WritableFontData*>(new_data->Slice(size)));
    size += metrics_->SubSerialize(slice);
  }
  return size;
}

IndexSubTableFormat2::Builder::Builder()
    : IndexSubTable::Builder(EblcTable::Offset::kIndexSubTable3_builderDataSize,
                             IndexSubTable::Format::FORMAT_2) {
  metrics_.Attach(BigGlyphMetrics::Builder::CreateBuilder());
}

IndexSubTableFormat2::Builder::Builder(WritableFontData* data,
                                       int32_t first_glyph_index,
                                       int32_t last_glyph_index)
    : IndexSubTable::Builder(data, first_glyph_index, last_glyph_index) {
}

IndexSubTableFormat2::Builder::Builder(ReadableFontData* data,
                                       int32_t first_glyph_index,
                                       int32_t last_glyph_index)
    : IndexSubTable::Builder(data, first_glyph_index, last_glyph_index) {
}

// static
int32_t IndexSubTableFormat2::Builder::DataLength(
    ReadableFontData* data,
    int32_t index_sub_table_offset,
    int32_t first_glyph_index,
    int32_t last_glyph_index) {
  UNREFERENCED_PARAMETER(data);
  UNREFERENCED_PARAMETER(index_sub_table_offset);
  UNREFERENCED_PARAMETER(first_glyph_index);
  UNREFERENCED_PARAMETER(last_glyph_index);
  return EblcTable::Offset::kIndexSubTable2Length;
}

/******************************************************************************
 * IndexSubTableFormat2::Builder::BitmapGlyphInfoIterator class
 ******************************************************************************/
IndexSubTableFormat2::Builder::BitmapGlyphInfoIterator::BitmapGlyphInfoIterator(
    IndexSubTableFormat2::Builder* container)
    : RefIterator<BitmapGlyphInfo, IndexSubTableFormat2::Builder,
                  IndexSubTable::Builder>(container) {
  glyph_id_ = container->first_glyph_index();
}

bool IndexSubTableFormat2::Builder::BitmapGlyphInfoIterator::HasNext() {
  if (glyph_id_ <= container()->last_glyph_index()) {
    return true;
  }
  return false;
}

CALLER_ATTACH BitmapGlyphInfo*
IndexSubTableFormat2::Builder::BitmapGlyphInfoIterator::Next() {
  BitmapGlyphInfoPtr output;
  if (!HasNext()) {
    // Note: In C++, we do not throw exception when there's no element.
    return NULL;
  }
  output = new BitmapGlyphInfo(glyph_id_,
                               container()->image_data_offset(),
                               container()->GlyphStartOffset(glyph_id_),
                               container()->GlyphLength(glyph_id_),
                               container()->image_format());
  glyph_id_++;
  return output.Detach();
}

}  // namespace sfntly