summaryrefslogtreecommitdiffstats
path: root/gfx/sfntly/cpp/src/test/otf_basic_editing_test.cc
blob: 04c8778c2516fa9dc950bd3df5802b8e8c8eea6d (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
/*
 * 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 "gtest/gtest.h"
#include "sfntly/font.h"
#include "sfntly/font_factory.h"
#include "sfntly/table/core/font_header_table.h"
#include "sfntly/tag.h"
#include "sfntly/data/memory_byte_array.h"
#include "sfntly/port/endian.h"
#include "sfntly/port/file_input_stream.h"
#include "sfntly/port/memory_output_stream.h"
#include "test/test_data.h"
#include "test/test_font_utils.h"

namespace sfntly {

bool TestOTFBasicEditing() {
  FontFactoryPtr factory;
  factory.Attach(FontFactory::GetInstance());
  FontBuilderArray font_builder_array;
  BuilderForFontFile(SAMPLE_TTF_FILE, factory, &font_builder_array);
  if (font_builder_array.size() != 1) {
    EXPECT_TRUE(false);
    return false;
  }
  FontBuilderPtr font_builder = font_builder_array[0];

  // ensure the builder is not bogus
  if (!font_builder) {
    EXPECT_TRUE(false);
    return false;
  }
  TableBuilderMap* builder_map = font_builder->table_builders();
  if (!builder_map) {
    EXPECT_TRUE(false);
    return false;
  }
  IntegerSet builder_tags;
  for (TableBuilderMap::iterator i = builder_map->begin(),
                                 e = builder_map->end(); i != e; ++i) {
    if (!i->second) {
      EXPECT_TRUE(false);
      char tag[5] = {0};
      int32_t value = ToBE32(i->first);
      memcpy(tag, &value, 4);
      fprintf(stderr, "tag %s does not have valid builder\n", tag);
      continue;
    }
    builder_tags.insert(i->first);
  }

  FontHeaderTableBuilderPtr header_builder =
      down_cast<FontHeaderTable::Builder*>(
          font_builder->GetTableBuilder(Tag::head));
  int64_t mod_date = header_builder->Modified();
  EXPECT_EQ(3397043097, mod_date);
  header_builder->SetModified(mod_date + 1);
  FontPtr font;
  font.Attach(font_builder->Build());

  // ensure every table had a builder
  const TableMap* table_map = font->GetTableMap();
  for (TableMap::const_iterator i = table_map->begin(), e = table_map->end();
                                i != e; ++i) {
    TablePtr table = i->second;
    HeaderPtr header = table->header();
    size_t erased = builder_tags.erase(header->tag());
    EXPECT_EQ(1U, erased);
  }
  EXPECT_TRUE(builder_tags.empty());

  FontHeaderTablePtr header =
      down_cast<FontHeaderTable*>(font->GetTable(Tag::head));
  int64_t after_mod_date = header->Modified();
  EXPECT_EQ(mod_date + 1, after_mod_date);

  // Checksum correctness of builder.
  TablePtr post = font->GetTable(Tag::post);
  EXPECT_EQ(TTF_CHECKSUM[SAMPLE_TTF_POST], post->CalculatedChecksum());
  return true;
}

}  // namespace sfntly

TEST(OTFBasicEditing, All) {
  ASSERT_TRUE(sfntly::TestOTFBasicEditing());
}