summaryrefslogtreecommitdiff
path: root/test/autogenerated/cmap_basic_test.cc
blob: aae35e1c139f118afa6914c1646180fb425716d2 (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
/*
 * 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.
 */

// type.h needs to be included first because of building issues on Windows
// Type aliases we delcare are defined in other headers and make the build
// fail otherwise.
#include "sfntly/port/type.h"
#include <assert.h>
#include <stdio.h>
#include <unicode/ucnv.h>

#include <iostream>
#include <string>

#include "gtest/gtest.h"
#include "sfntly/data/memory_byte_array.h"
#include "sfntly/font.h"
#include "sfntly/font_factory.h"
#include "sfntly/table/core/cmap_table.h"
#include "sfntly/table/core/font_header_table.h"
#include "sfntly/tag.h"
#include "test/autogenerated/cmap_test_data.h"
#include "test/test_font_utils.h"
#include "test/test_utils.h"
#include "test/test_xml_utils.h"

namespace sfntly {

#if GTEST_HAS_PARAM_TEST

using ::testing::TestWithParam;
using ::testing::Values;

class CMapBasicTests : public :: testing::TestWithParam<const char*> {
 public:
  CMapBasicTests() {}
  virtual void SetUp();
  virtual void TearDown() {}

  Ptr<CMapTable> cmap_table_;
  TiXmlDocument document_;
};

void CMapBasicTests::SetUp() {
  // Loading the font
  Ptr<FontFactory> font_factory;
  font_factory.Attach(FontFactory::GetInstance());
  FontArray font_array;
  std::string font_name = "../../";
#if defined (WIN32)
  font_name += "../";
#endif
  font_name += std::string(GetParam());
  LoadFont(font_name.c_str(), font_factory, &font_array);
  ASSERT_FALSE(font_array.empty());
  Ptr<Font> font = font_array.at(0);
  ASSERT_NE(font, reinterpret_cast<Font*>(NULL));
  cmap_table_ = down_cast<CMapTable*>(font->GetTable(Tag::cmap));
  if (!cmap_table_)
    fprintf(stderr, "No CMap: %s\n", font_name.c_str());
  ASSERT_NE(cmap_table_, reinterpret_cast<CMapTable*>(NULL));

  // Loading the XML file
  document_ = TiXmlDocument((font_name + ".xml").c_str());
  ASSERT_TRUE(document_.LoadFile());
}

TEST_P(CMapBasicTests, BasicTest) {
  TiXmlNodeVector* cmap_table = GetNodesWithName(&document_, "cmap_table");
  // A font can only have one CMap table
  ASSERT_EQ(cmap_table->size(), (size_t)1);
  TiXmlNodeVector* cmaps = GetNodesWithName(cmap_table->at(0), "cmap");
  const TiXmlAttribute* num_cmaps_attr = GetAttribute(cmap_table->at(0),
                                                      "num_cmaps");
  ASSERT_NE(num_cmaps_attr, reinterpret_cast<TiXmlAttribute*>(NULL));
  // But there may be more than one CMap in this table
  ASSERT_LE(cmaps->size(), (size_t)num_cmaps_attr->IntValue());
  for (TiXmlNodeVector::iterator it = cmaps->begin();
       it != cmaps->end(); ++it) {
    int32_t platform_id = GetAttribute(*it, "platform_id")->IntValue();
    int32_t encoding_id = GetAttribute(*it, "encoding_id")->IntValue();
    Ptr<CMapTable::CMap> cmap;
    cmap.Attach(cmap_table_->GetCMap(platform_id, encoding_id));
    if (!cmap) {
      fprintf(stderr, "Cannot test unsupported CMapFormat%d\n",
              GetAttribute(*it, "format")->IntValue());
      continue;
    }
    ASSERT_EQ(cmap->platform_id(), platform_id);
    ASSERT_EQ(cmap->encoding_id(), encoding_id);
    TiXmlNodeVector* maps = GetNodesWithName(*it, "map");
    for (TiXmlNodeVector::iterator jt = maps->begin();
         jt != maps->end(); ++jt) {
      int32_t character;
#if defined (WIN32)
      sscanf_s(GetAttribute(*jt, "char")->Value(), "%x", &character);
#else
      sscanf(GetAttribute(*jt, "char")->Value(), "%x", &character);
#endif
      int32_t glyph_id = GetAttribute(*jt, "gid")->IntValue();
      ASSERT_EQ(cmap->GlyphId(character), glyph_id);
    }
    delete maps;
  }
  delete cmaps;
  delete cmap_table;
}

INSTANTIATE_TEST_CASE_P(CMapBasicTests,
                        CMapBasicTests,
                        ::testing::ValuesIn(cmap_test_data::kAllTests));

#else

TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {}

#endif  // GTEST_HAS_PARAM
}