1a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com/*
2a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com * Copyright 2011 Google Inc. All Rights Reserved.
3a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com *
4a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com * Licensed under the Apache License, Version 2.0 (the "License");
5a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com * you may not use this file except in compliance with the License.
6a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com * You may obtain a copy of the License at
7a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com *
8a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com *      http://www.apache.org/licenses/LICENSE-2.0
9a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com *
10a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com * Unless required by applicable law or agreed to in writing, software
11a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com * distributed under the License is distributed on an "AS IS" BASIS,
12a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com * See the License for the specific language governing permissions and
14a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com * limitations under the License.
15a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com */
16a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
1779fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com// type.h needs to be included first because of building issues on Windows
1879fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com// Type aliases we delcare are defined in other headers and make the build
1979fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com// fail otherwise.
20ba91573e890c0871c6949e480a365bc85d408e77dfilimon@google.com#include "sfntly/port/type.h"
21a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include <assert.h>
22a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include <stdio.h>
23a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include <unicode/ucnv.h>
24a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
25a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include <iostream>
26a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include <string>
27a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
28a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include "gtest/gtest.h"
29a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include "sfntly/data/memory_byte_array.h"
30a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include "sfntly/font.h"
31a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include "sfntly/font_factory.h"
32a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include "sfntly/table/core/cmap_table.h"
33a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include "sfntly/table/core/font_header_table.h"
34a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include "sfntly/tag.h"
35a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include "test/autogenerated/cmap_test_data.h"
36a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include "test/test_font_utils.h"
37a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#include "test/test_utils.h"
380c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com#include "test/test_xml_utils.h"
39a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
40a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.comnamespace sfntly {
41a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
42a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#if GTEST_HAS_PARAM_TEST
43a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
44a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.comusing ::testing::TestWithParam;
45a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.comusing ::testing::Values;
46a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
470c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.comclass CMapBasicTests : public :: testing::TestWithParam<const char*> {
48a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com public:
49a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  CMapBasicTests() {}
50a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  virtual void SetUp();
51a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  virtual void TearDown() {}
52a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
53a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  Ptr<CMapTable> cmap_table_;
540c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  TiXmlDocument document_;
55a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com};
56a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
57a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.comvoid CMapBasicTests::SetUp() {
580c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  // Loading the font
59a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  Ptr<FontFactory> font_factory;
60a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  font_factory.Attach(FontFactory::GetInstance());
61a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  FontArray font_array;
620c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  std::string font_name = "../../";
63a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#if defined (WIN32)
64a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  font_name += "../";
65a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#endif
660c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  font_name += std::string(GetParam());
67a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  LoadFont(font_name.c_str(), font_factory, &font_array);
68a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  ASSERT_FALSE(font_array.empty());
69a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  Ptr<Font> font = font_array.at(0);
70a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  ASSERT_NE(font, reinterpret_cast<Font*>(NULL));
71a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  cmap_table_ = down_cast<CMapTable*>(font->GetTable(Tag::cmap));
72a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  if (!cmap_table_)
73a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com    fprintf(stderr, "No CMap: %s\n", font_name.c_str());
74a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com  ASSERT_NE(cmap_table_, reinterpret_cast<CMapTable*>(NULL));
75a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
760c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  // Loading the XML file
770c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  document_ = TiXmlDocument((font_name + ".xml").c_str());
780c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  ASSERT_TRUE(document_.LoadFile());
79a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com}
80a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
81a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.comTEST_P(CMapBasicTests, BasicTest) {
820c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  TiXmlNodeVector* cmap_table = GetNodesWithName(&document_, "cmap_table");
830c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  // A font can only have one CMap table
848dd54cd13ca1dfe7ef6245056d4a322b43a7fae5dfilimon@google.com  ASSERT_EQ(cmap_table->size(), (size_t)1);
850c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  TiXmlNodeVector* cmaps = GetNodesWithName(cmap_table->at(0), "cmap");
860c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  const TiXmlAttribute* num_cmaps_attr = GetAttribute(cmap_table->at(0),
870c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com                                                      "num_cmaps");
880c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  ASSERT_NE(num_cmaps_attr, reinterpret_cast<TiXmlAttribute*>(NULL));
890c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  // But there may be more than one CMap in this table
90f247d4b0f53526631581f74a6862a2f2ca952f75dfilimon@google.com  ASSERT_LE(cmaps->size(), (size_t)num_cmaps_attr->IntValue());
910c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  for (TiXmlNodeVector::iterator it = cmaps->begin();
920c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com       it != cmaps->end(); ++it) {
930c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    int32_t platform_id = GetAttribute(*it, "platform_id")->IntValue();
940c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    int32_t encoding_id = GetAttribute(*it, "encoding_id")->IntValue();
950c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    Ptr<CMapTable::CMap> cmap;
960c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    cmap.Attach(cmap_table_->GetCMap(platform_id, encoding_id));
970c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    if (!cmap) {
980c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com      fprintf(stderr, "Cannot test unsupported CMapFormat%d\n",
990c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com              GetAttribute(*it, "format")->IntValue());
1000c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com      continue;
1010c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    }
1020c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    ASSERT_EQ(cmap->platform_id(), platform_id);
1030c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    ASSERT_EQ(cmap->encoding_id(), encoding_id);
1040c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    TiXmlNodeVector* maps = GetNodesWithName(*it, "map");
1050c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    for (TiXmlNodeVector::iterator jt = maps->begin();
1060c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com         jt != maps->end(); ++jt) {
1070c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com      int32_t character;
10814783268d0367737c31150c0ea03bba7f9e4514edfilimon@google.com#if defined (WIN32)
10914783268d0367737c31150c0ea03bba7f9e4514edfilimon@google.com      sscanf_s(GetAttribute(*jt, "char")->Value(), "%x", &character);
11014783268d0367737c31150c0ea03bba7f9e4514edfilimon@google.com#else
1110c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com      sscanf(GetAttribute(*jt, "char")->Value(), "%x", &character);
11214783268d0367737c31150c0ea03bba7f9e4514edfilimon@google.com#endif
1130c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com      int32_t glyph_id = GetAttribute(*jt, "gid")->IntValue();
1140c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com      ASSERT_EQ(cmap->GlyphId(character), glyph_id);
1150c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    }
1160c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com    delete maps;
1170c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  }
1180c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  delete cmaps;
1190c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com  delete cmap_table;
120a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com}
121a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
122a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.comINSTANTIATE_TEST_CASE_P(CMapBasicTests,
123a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com                        CMapBasicTests,
1240c18481adaebdef466bb1b5b0965f9f8db5e2081dfilimon@google.com                        ::testing::ValuesIn(cmap_test_data::kAllTests));
125a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
126a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#else
127a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
128a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.comTEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {}
129a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com
130a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com#endif  // GTEST_HAS_PARAM
131a2725711c28771ab66670264bdf0caa797ebb22adfilimon@google.com}
132