summaryrefslogtreecommitdiff
path: root/sfntly/table/truetype/glyph_table.cc
blob: f38fac5c5c4f9f8294abfd8ce7de813c39f64577 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
 * 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/truetype/glyph_table.h"

#include <stdlib.h>

#include "sfntly/port/exception_type.h"

namespace sfntly {
/******************************************************************************
 * Constants
 ******************************************************************************/
const int32_t GlyphTable::SimpleGlyph::kFLAG_ONCURVE = 1;
const int32_t GlyphTable::SimpleGlyph::kFLAG_XSHORT = 1 << 1;
const int32_t GlyphTable::SimpleGlyph::kFLAG_YSHORT = 1 << 2;
const int32_t GlyphTable::SimpleGlyph::kFLAG_REPEAT = 1 << 3;
const int32_t GlyphTable::SimpleGlyph::kFLAG_XREPEATSIGN = 1 << 4;
const int32_t GlyphTable::SimpleGlyph::kFLAG_YREPEATSIGN = 1 << 5;

const int32_t GlyphTable::CompositeGlyph::kFLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
const int32_t GlyphTable::CompositeGlyph::kFLAG_ARGS_ARE_XY_VALUES = 1 << 1;
const int32_t GlyphTable::CompositeGlyph::kFLAG_ROUND_XY_TO_GRID = 1 << 2;
const int32_t GlyphTable::CompositeGlyph::kFLAG_WE_HAVE_A_SCALE = 1 << 3;
const int32_t GlyphTable::CompositeGlyph::kFLAG_RESERVED = 1 << 4;
const int32_t GlyphTable::CompositeGlyph::kFLAG_MORE_COMPONENTS = 1 << 5;
const int32_t GlyphTable::CompositeGlyph::kFLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6;
const int32_t GlyphTable::CompositeGlyph::kFLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7;
const int32_t GlyphTable::CompositeGlyph::kFLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
const int32_t GlyphTable::CompositeGlyph::kFLAG_USE_MY_METRICS = 1 << 9;
const int32_t GlyphTable::CompositeGlyph::kFLAG_OVERLAP_COMPOUND = 1 << 10;
const int32_t GlyphTable::CompositeGlyph::kFLAG_SCALED_COMPONENT_OFFSET = 1 << 11;
const int32_t GlyphTable::CompositeGlyph::kFLAG_UNSCALED_COMPONENT_OFFSET = 1 << 12;

/******************************************************************************
 * GlyphTable class
 ******************************************************************************/
GlyphTable::~GlyphTable() {
}

GlyphTable::Glyph* GlyphTable::GetGlyph(int32_t offset, int32_t length) {
  return GlyphTable::Glyph::GetGlyph(this, this->data_, offset, length);
}

GlyphTable::GlyphTable(Header* header, ReadableFontData* data)
    : SubTableContainerTable(header, data) {
}

/******************************************************************************
 * GlyphTable::Builder class
 ******************************************************************************/
GlyphTable::Builder::Builder(Header* header, ReadableFontData* data)
    : SubTableContainerTable::Builder(header, data) {
}

GlyphTable::Builder::~Builder() {
}

void GlyphTable::Builder::SetLoca(const IntegerList& loca) {
  loca_ = loca;
  set_model_changed(false);
  glyph_builders_.clear();
}

void GlyphTable::Builder::GenerateLocaList(IntegerList* locas) {
  assert(locas);
  GlyphBuilderList* glyph_builders = GetGlyphBuilders();
  locas->push_back(0);
  if (glyph_builders->size() == 0) {
    locas->push_back(0);
  } else {
    int32_t total = 0;
    for (GlyphBuilderList::iterator b = glyph_builders->begin(),
                                    b_end = glyph_builders->end();
                                    b != b_end; ++b) {
      int32_t size = (*b)->SubDataSizeToSerialize();
      locas->push_back(total + size);
      total += size;
    }
  }
}

CALLER_ATTACH GlyphTable::Builder*
    GlyphTable::Builder::CreateBuilder(Header* header, WritableFontData* data) {
  Ptr<GlyphTable::Builder> builder;
  builder = new GlyphTable::Builder(header, data);
  return builder.Detach();
}

GlyphTable::GlyphBuilderList* GlyphTable::Builder::GlyphBuilders() {
  return GetGlyphBuilders();
}

void GlyphTable::Builder::SetGlyphBuilders(GlyphBuilderList* glyph_builders) {
  glyph_builders_ = *glyph_builders;
  set_model_changed();
}

CALLER_ATTACH GlyphTable::Glyph::Builder*
    GlyphTable::Builder::GlyphBuilder(ReadableFontData* data) {
  return Glyph::Builder::GetBuilder(this, data);
}

CALLER_ATTACH FontDataTable*
    GlyphTable::Builder::SubBuildTable(ReadableFontData* data) {
  FontDataTablePtr table = new GlyphTable(header(), data);
  return table.Detach();
}

void GlyphTable::Builder::SubDataSet() {
  glyph_builders_.clear();
  set_model_changed(false);
}

int32_t GlyphTable::Builder::SubDataSizeToSerialize() {
  if (glyph_builders_.empty())
    return 0;

  bool variable = false;
  int32_t size = 0;

  // Calculate size of each table.
  for (GlyphBuilderList::iterator b = glyph_builders_.begin(),
                                  end = glyph_builders_.end(); b != end; ++b) {
      int32_t glyph_size = (*b)->SubDataSizeToSerialize();
      size += abs(glyph_size);
      variable |= glyph_size <= 0;
  }
  return variable ? -size : size;
}

bool GlyphTable::Builder::SubReadyToSerialize() {
  return !glyph_builders_.empty();
}

int32_t GlyphTable::Builder::SubSerialize(WritableFontData* new_data) {
  int32_t size = 0;
  for (GlyphBuilderList::iterator b = glyph_builders_.begin(),
                                  end = glyph_builders_.end(); b != end; ++b) {
    FontDataPtr data;
    data.Attach(new_data->Slice(size));
    size += (*b)->SubSerialize(down_cast<WritableFontData*>(data.p_));
  }
  return size;
}

void GlyphTable::Builder::Initialize(ReadableFontData* data,
                                     const IntegerList& loca) {
  if (data != NULL) {
    if (loca_.empty()) {
      return;
    }
    int32_t loca_value;
    int32_t last_loca_value = loca[0];
    for (size_t i = 1; i < loca.size(); ++i) {
      loca_value = loca[i];
      GlyphBuilderPtr builder;
      builder.Attach(
        Glyph::Builder::GetBuilder(this,
                                   data,
                                   last_loca_value /*offset*/,
                                   loca_value - last_loca_value /*length*/));
      glyph_builders_.push_back(builder);
      last_loca_value = loca_value;
    }
  }
}

GlyphTable::GlyphBuilderList* GlyphTable::Builder::GetGlyphBuilders() {
  if (glyph_builders_.empty()) {
    if (InternalReadData() && !loca_.empty()) {
#if !defined (SFNTLY_NO_EXCEPTION)
      throw IllegalStateException(
          "Loca values not set - unable to parse glyph data.");
#endif
      return NULL;
    }
    Initialize(InternalReadData(), loca_);
    set_model_changed();
  }
  return &glyph_builders_;
}

void GlyphTable::Builder::Revert() {
  glyph_builders_.clear();
  set_model_changed(false);
}

/******************************************************************************
 * GlyphTable::Glyph class
 ******************************************************************************/
GlyphTable::Glyph::~Glyph() {}

CALLER_ATTACH GlyphTable::Glyph*
    GlyphTable::Glyph::GetGlyph(GlyphTable* table,
                                ReadableFontData* data,
                                int32_t offset,
                                int32_t length) {
  UNREFERENCED_PARAMETER(table);
  int32_t type = GlyphType(data, offset, length);
  GlyphPtr glyph;

  ReadableFontDataPtr sliced_data;
  sliced_data.Attach(down_cast<ReadableFontData*>(data->Slice(offset, length)));
  if (type == GlyphType::kSimple) {
    glyph = new SimpleGlyph(sliced_data);
  } else {
    glyph = new CompositeGlyph(sliced_data);
  }
  return glyph.Detach();
}

int32_t GlyphTable::Glyph::Padding() {
  Initialize();
  return SubTable::Padding();
}

int32_t GlyphTable::Glyph::GlyphType() {
  return glyph_type_;
}

int32_t GlyphTable::Glyph::NumberOfContours() {
  return number_of_contours_;
}

int32_t GlyphTable::Glyph::XMin() {
  return data_->ReadShort(Offset::kXMin);
}

int32_t GlyphTable::Glyph::XMax() {
  return data_->ReadShort(Offset::kXMax);
}

int32_t GlyphTable::Glyph::YMin() {
  return data_->ReadShort(Offset::kYMin);
}

int32_t GlyphTable::Glyph::YMax() {
  return data_->ReadShort(Offset::kYMax);
}

GlyphTable::Glyph::Glyph(ReadableFontData* data, int32_t glyph_type)
    : SubTable(data),
      glyph_type_(glyph_type) {
  if (data_->Length() == 0) {
    number_of_contours_ = 0;
  } else {
    // -1 if composite
    number_of_contours_ = data_->ReadShort(Offset::kNumberOfContours);
  }
}

int32_t GlyphTable::Glyph::GlyphType(ReadableFontData* data,
                                     int32_t offset,
                                     int32_t length) {
  if (length == 0) {
    return GlyphType::kSimple;
  }
  int32_t number_of_contours = data->ReadShort(offset);
  if (number_of_contours >= 0) {
    return GlyphType::kSimple;
  }
  return GlyphType::kComposite;
}

/******************************************************************************
 * GlyphTable::Glyph::Builder class
 ******************************************************************************/
GlyphTable::Glyph::Builder::~Builder() {
}

GlyphTable::Glyph::Builder::Builder(WritableFontData* data)
    : SubTable::Builder(data) {
}

GlyphTable::Glyph::Builder::Builder(ReadableFontData* data)
    : SubTable::Builder(data) {
}

CALLER_ATTACH GlyphTable::Glyph::Builder*
    GlyphTable::Glyph::Builder::GetBuilder(
        GlyphTable::Builder* table_builder,
        ReadableFontData* data) {
  return GetBuilder(table_builder, data, 0, data->Length());
}

CALLER_ATTACH GlyphTable::Glyph::Builder*
    GlyphTable::Glyph::Builder::GetBuilder(
        GlyphTable::Builder* table_builder,
        ReadableFontData* data,
        int32_t offset,
        int32_t length) {
  UNREFERENCED_PARAMETER(table_builder);
  int32_t type = Glyph::GlyphType(data, offset, length);
  GlyphBuilderPtr builder;
  ReadableFontDataPtr sliced_data;
  sliced_data.Attach(down_cast<ReadableFontData*>(data->Slice(offset, length)));
  if (type == GlyphType::kSimple) {
    builder = new SimpleGlyph::SimpleGlyphBuilder(sliced_data);
  } else {
    builder = new CompositeGlyph::CompositeGlyphBuilder(sliced_data);
  }
  return builder.Detach();
}

void GlyphTable::Glyph::Builder::SubDataSet() {
  // NOP
}

int32_t GlyphTable::Glyph::Builder::SubDataSizeToSerialize() {
  return InternalReadData()->Length();
}

bool GlyphTable::Glyph::Builder::SubReadyToSerialize() {
  return true;
}

int32_t GlyphTable::Glyph::Builder::SubSerialize(WritableFontData* new_data) {
  return InternalReadData()->CopyTo(new_data);
}

/******************************************************************************
 * GlyphTable::SimpleGlyph
 ******************************************************************************/
GlyphTable::SimpleGlyph::SimpleGlyph(ReadableFontData* data)
    : GlyphTable::Glyph(data, GlyphType::kSimple), initialized_(false) {
}

GlyphTable::SimpleGlyph::~SimpleGlyph() {
}

int32_t GlyphTable::SimpleGlyph::InstructionSize() {
  Initialize();
  return instruction_size_;
}

CALLER_ATTACH ReadableFontData* GlyphTable::SimpleGlyph::Instructions() {
  Initialize();
  return down_cast<ReadableFontData*>(
             data_->Slice(instructions_offset_, InstructionSize()));
}

int32_t GlyphTable::SimpleGlyph::NumberOfPoints(int32_t contour) {
  Initialize();
  if (contour >= NumberOfContours()) {
    return 0;
  }
  return contour_index_[contour + 1] - contour_index_[contour];
}

int32_t GlyphTable::SimpleGlyph::XCoordinate(int32_t contour, int32_t point) {
  Initialize();
  return x_coordinates_[contour_index_[contour] + point];
}

int32_t GlyphTable::SimpleGlyph::YCoordinate(int32_t contour, int32_t point) {
  Initialize();
  return y_coordinates_[contour_index_[contour] + point];
}

bool GlyphTable::SimpleGlyph::OnCurve(int32_t contour, int32_t point) {
  Initialize();
  return on_curve_[contour_index_[contour] + point];
}

void GlyphTable::SimpleGlyph::Initialize() {
  AutoLock lock(initialization_lock_);
  if (initialized_) {
    return;
  }

  if (ReadFontData()->Length() == 0) {
    instruction_size_ = 0;
    number_of_points_ = 0;
    instructions_offset_ = 0;
    flags_offset_ = 0;
    x_coordinates_offset_ = 0;
    y_coordinates_offset_ = 0;
    return;
  }

  instruction_size_ = data_->ReadUShort(Offset::kSimpleEndPtsOfCountours +
      NumberOfContours() * DataSize::kUSHORT);
  instructions_offset_ = Offset::kSimpleEndPtsOfCountours +
      (NumberOfContours() + 1) * DataSize::kUSHORT;
  flags_offset_ = instructions_offset_ + instruction_size_ * DataSize::kBYTE;
  number_of_points_ = ContourEndPoint(NumberOfContours() - 1) + 1;
  x_coordinates_.resize(number_of_points_);
  y_coordinates_.resize(number_of_points_);
  on_curve_.resize(number_of_points_);
  ParseData(false);
  x_coordinates_offset_ = flags_offset_ + flag_byte_count_ * DataSize::kBYTE;
  y_coordinates_offset_ = x_coordinates_offset_ + x_byte_count_ *
      DataSize::kBYTE;
  contour_index_.resize(NumberOfContours() + 1);
  contour_index_[0] = 0;
  for (uint32_t contour = 0; contour < contour_index_.size() - 1; ++contour) {
    contour_index_[contour + 1] = ContourEndPoint(contour) + 1;
  }
  ParseData(true);
  int32_t non_padded_data_length =
    5 * DataSize::kSHORT +
    (NumberOfContours() * DataSize::kUSHORT) +
    DataSize::kUSHORT +
    (instruction_size_ * DataSize::kBYTE) +
    (flag_byte_count_ * DataSize::kBYTE) +
    (x_byte_count_ * DataSize::kBYTE) +
    (y_byte_count_ * DataSize::kBYTE);
  set_padding(DataLength() - non_padded_data_length);
  initialized_ = true;
}

void GlyphTable::SimpleGlyph::ParseData(bool fill_arrays) {
  int32_t flag = 0;
  int32_t flag_repeat = 0;
  int32_t flag_index = 0;
  int32_t x_byte_index = 0;
  int32_t y_byte_index = 0;

  for (int32_t point_index = 0; point_index < number_of_points_;
       ++point_index) {
    // get the flag for the current point
    if (flag_repeat == 0) {
      flag = FlagAsInt(flag_index++);
      if ((flag & kFLAG_REPEAT) == kFLAG_REPEAT) {
        flag_repeat = FlagAsInt(flag_index++);
      }
    } else {
      flag_repeat--;
    }

    // on the curve?
    if (fill_arrays) {
      on_curve_[point_index] = ((flag & kFLAG_ONCURVE) == kFLAG_ONCURVE);
    }
    // get the x coordinate
    if ((flag & kFLAG_XSHORT) == kFLAG_XSHORT) {
      // single byte x coord value
      if (fill_arrays) {
        x_coordinates_[point_index] =
            data_->ReadUByte(x_coordinates_offset_ + x_byte_index);
        x_coordinates_[point_index] *=
            ((flag & kFLAG_XREPEATSIGN) == kFLAG_XREPEATSIGN) ? 1 : -1;
      }
      x_byte_index++;
    } else {
      // double byte coord value
      if (!((flag & kFLAG_XREPEATSIGN) == kFLAG_XREPEATSIGN)) {
        if (fill_arrays) {
          x_coordinates_[point_index] =
            data_->ReadShort(x_coordinates_offset_ + x_byte_index);
        }
        x_byte_index += 2;
      }
    }
    if (fill_arrays && point_index > 0) {
      x_coordinates_[point_index] += x_coordinates_[point_index - 1];
    }

    // get the y coordinate
    if ((flag & kFLAG_YSHORT) == kFLAG_YSHORT) {
      if (fill_arrays) {
        y_coordinates_[point_index] =
          data_->ReadUByte(y_coordinates_offset_ + y_byte_index);
        y_coordinates_[point_index] *=
          ((flag & kFLAG_YREPEATSIGN) == kFLAG_YREPEATSIGN) ? 1 : -1;
      }
      y_byte_index++;
    } else {
      if (!((flag & kFLAG_YREPEATSIGN) == kFLAG_YREPEATSIGN)) {
        if (fill_arrays) {
          y_coordinates_[point_index] =
            data_->ReadShort(y_coordinates_offset_ + y_byte_index);
        }
        y_byte_index += 2;
      }
    }
    if (fill_arrays && point_index > 0) {
      y_coordinates_[point_index] += y_coordinates_[point_index - 1];
    }
  }
  flag_byte_count_ = flag_index;
  x_byte_count_ = x_byte_index;
  y_byte_count_ = y_byte_index;
}

int32_t GlyphTable::SimpleGlyph::FlagAsInt(int32_t index) {
  return data_->ReadUByte(flags_offset_ + index * DataSize::kBYTE);
}

int32_t GlyphTable::SimpleGlyph::ContourEndPoint(int32_t contour) {
  return data_->ReadUShort(contour * DataSize::kUSHORT +
                           Offset::kSimpleEndPtsOfCountours);
}

/******************************************************************************
 * GlyphTable::SimpleGlyph::Builder
 ******************************************************************************/
GlyphTable::SimpleGlyph::SimpleGlyphBuilder::~SimpleGlyphBuilder() {
}

GlyphTable::SimpleGlyph::SimpleGlyphBuilder::SimpleGlyphBuilder(
    WritableFontData* data)
    : Glyph::Builder(data) {
}

GlyphTable::SimpleGlyph::SimpleGlyphBuilder::SimpleGlyphBuilder(
    ReadableFontData* data)
    : Glyph::Builder(data) {
}

CALLER_ATTACH FontDataTable*
    GlyphTable::SimpleGlyph::SimpleGlyphBuilder::SubBuildTable(
        ReadableFontData* data) {
  FontDataTablePtr table = new SimpleGlyph(data);
  return table.Detach();
}

/******************************************************************************
 * GlyphTable::CompositeGlyph
 ******************************************************************************/
GlyphTable::CompositeGlyph::CompositeGlyph(ReadableFontData* data)
    : GlyphTable::Glyph(data, GlyphType::kComposite),
      instruction_size_(0),
      instructions_offset_(0),
      initialized_(false) {
  Initialize();
}

GlyphTable::CompositeGlyph::~CompositeGlyph() {
}

int32_t GlyphTable::CompositeGlyph::Flags(int32_t contour) {
  return data_->ReadUShort(contour_index_[contour]);
}

int32_t GlyphTable::CompositeGlyph::NumGlyphs() {
  return contour_index_.size();
}

int32_t GlyphTable::CompositeGlyph::GlyphIndex(int32_t contour) {
  return data_->ReadUShort(DataSize::kUSHORT + contour_index_[contour]);
}

int32_t GlyphTable::CompositeGlyph::Argument1(int32_t contour) {
  int32_t index = 2 * DataSize::kUSHORT + contour_index_[contour];
  int32_t contour_flags = Flags(contour);
  if ((contour_flags & kFLAG_ARG_1_AND_2_ARE_WORDS) ==
                       kFLAG_ARG_1_AND_2_ARE_WORDS) {
    return data_->ReadUShort(index);
  }
  return data_->ReadByte(index);
}

int32_t GlyphTable::CompositeGlyph::Argument2(int32_t contour) {
  int32_t index = 2 * DataSize::kUSHORT + contour_index_[contour];
  int32_t contour_flags = Flags(contour);
  if ((contour_flags & kFLAG_ARG_1_AND_2_ARE_WORDS) ==
                       kFLAG_ARG_1_AND_2_ARE_WORDS) {
    return data_->ReadUShort(index + DataSize::kUSHORT);
  }
  return data_->ReadByte(index + DataSize::kUSHORT);
}

int32_t GlyphTable::CompositeGlyph::TransformationSize(int32_t contour) {
  int32_t contour_flags = Flags(contour);
  if ((contour_flags & kFLAG_WE_HAVE_A_SCALE) == kFLAG_WE_HAVE_A_SCALE) {
      return DataSize::kF2DOT14;
    } else if ((contour_flags & kFLAG_WE_HAVE_AN_X_AND_Y_SCALE) ==
                                kFLAG_WE_HAVE_AN_X_AND_Y_SCALE) {
      return 2 * DataSize::kF2DOT14;
    } else if ((contour_flags & kFLAG_WE_HAVE_A_TWO_BY_TWO) ==
                                kFLAG_WE_HAVE_A_TWO_BY_TWO) {
      return 4 * DataSize::kF2DOT14;
    }
    return 0;
}

void GlyphTable::CompositeGlyph::Transformation(int32_t contour,
                                                ByteVector* transformation) {
  int32_t contour_flags = Flags(contour);
  int32_t index = contour_index_[contour] + 2 * DataSize::kUSHORT;
  if ((contour_flags & kFLAG_ARG_1_AND_2_ARE_WORDS) ==
                       kFLAG_ARG_1_AND_2_ARE_WORDS) {
    index += 2 * DataSize::kSHORT;
  } else {
    index += 2 * DataSize::kBYTE;
  }
  int32_t tsize = TransformationSize(contour);
  transformation->resize(tsize);
  data_->ReadBytes(index, &((*transformation)[0]), 0, tsize);
}

int32_t GlyphTable::CompositeGlyph::InstructionSize() {
  return instruction_size_;
}

CALLER_ATTACH ReadableFontData* GlyphTable::CompositeGlyph::Instructions() {
  return down_cast<ReadableFontData*>(
             data_->Slice(instructions_offset_, InstructionSize()));
}

void GlyphTable::CompositeGlyph::Initialize() {
  AutoLock lock(initialization_lock_);
  if (initialized_) {
    return;
  }

  int32_t index = 5 * DataSize::kUSHORT;
  int32_t flags = kFLAG_MORE_COMPONENTS;

  while ((flags & kFLAG_MORE_COMPONENTS) == kFLAG_MORE_COMPONENTS) {
    contour_index_.push_back(index);
    flags = data_->ReadUShort(index);
    index += 2 * DataSize::kUSHORT;  // flags and glyphIndex
    if ((flags & kFLAG_ARG_1_AND_2_ARE_WORDS) == kFLAG_ARG_1_AND_2_ARE_WORDS) {
      index += 2 * DataSize::kSHORT;
    } else {
      index += 2 * DataSize::kBYTE;
    }
    if ((flags & kFLAG_WE_HAVE_A_SCALE) == kFLAG_WE_HAVE_A_SCALE) {
      index += DataSize::kF2DOT14;
    } else if ((flags & kFLAG_WE_HAVE_AN_X_AND_Y_SCALE) ==
                        kFLAG_WE_HAVE_AN_X_AND_Y_SCALE) {
      index += 2 * DataSize::kF2DOT14;
    } else if ((flags & kFLAG_WE_HAVE_A_TWO_BY_TWO) ==
                        kFLAG_WE_HAVE_A_TWO_BY_TWO) {
      index += 4 * DataSize::kF2DOT14;
    }
    int32_t non_padded_data_length = index;
    if ((flags & kFLAG_WE_HAVE_INSTRUCTIONS) == kFLAG_WE_HAVE_INSTRUCTIONS) {
      instruction_size_ = data_->ReadUShort(index);
      index += DataSize::kUSHORT;
      instructions_offset_ = index;
      non_padded_data_length = index + (instruction_size_ * DataSize::kBYTE);
    }
    set_padding(DataLength() - non_padded_data_length);
  }

  initialized_ = true;
}

/******************************************************************************
 * GlyphTable::CompositeGlyph::Builder
 ******************************************************************************/
GlyphTable::CompositeGlyph::CompositeGlyphBuilder::~CompositeGlyphBuilder() {
}

GlyphTable::CompositeGlyph::CompositeGlyphBuilder::CompositeGlyphBuilder(
    WritableFontData* data)
    : Glyph::Builder(data) {
}

GlyphTable::CompositeGlyph::CompositeGlyphBuilder::CompositeGlyphBuilder(
    ReadableFontData* data)
    : Glyph::Builder(data) {
}

CALLER_ATTACH FontDataTable*
    GlyphTable::CompositeGlyph::CompositeGlyphBuilder::SubBuildTable(
        ReadableFontData* data) {
  FontDataTablePtr table = new CompositeGlyph(data);
  return table.Detach();
}

}  // namespace sfntly