1/*
2 * Copyright 2011 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "gtest/gtest.h"
18#include "sfntly/font.h"
19#include "sfntly/table/core/horizontal_metrics_table.h"
20#include "test/serialization_test.h"
21
22namespace sfntly {
23
24const int32_t HMTX_ENTRIES_COUNT = 1499;
25const int32_t HMTX_LSB_COUNT = 3;
26
27struct HmtxEntry {
28  int32_t advance_width_;
29  int32_t lsb_;
30
31  HmtxEntry(int32_t advance_width, int32_t lsb)
32      : advance_width_(advance_width), lsb_(lsb) {}
33};
34
35const HmtxEntry HMTX_ENTRIES[] = {
36    HmtxEntry(748, 68),  // 0
37    HmtxEntry(0, 0),  // 1
38    HmtxEntry(682, 0),  // 2
39    HmtxEntry(616, 0),  // 3
40    HmtxEntry(421, 103),  // 4
41    HmtxEntry(690, 129),  // 5
42    HmtxEntry(1589, 129),  // 6
43    HmtxEntry(1017, 25),  // 7
44    HmtxEntry(1402, 104),  // 8
45    HmtxEntry(1241, 100),  // 9
46};
47const int32_t NUM_HMTX_ENTRIES = 10;
48
49static bool VerifyHMTX(Table* table) {
50  HorizontalMetricsTablePtr hmtx = down_cast<HorizontalMetricsTable*>(table);
51  if (hmtx == NULL) {
52    return false;
53  }
54
55  EXPECT_EQ(hmtx->NumberOfHMetrics(), HMTX_ENTRIES_COUNT);
56  EXPECT_EQ(hmtx->NumberOfLSBs(), HMTX_LSB_COUNT);
57
58  for (int32_t i = 0; i < NUM_HMTX_ENTRIES; ++i) {
59    EXPECT_EQ(hmtx->AdvanceWidth(i), HMTX_ENTRIES[i].advance_width_);
60    EXPECT_EQ(hmtx->LeftSideBearing(i), HMTX_ENTRIES[i].lsb_);
61  }
62
63  // No such element case.
64  EXPECT_EQ(hmtx->AdvanceWidth(HMTX_ENTRIES_COUNT),
65            HMTX_ENTRIES[0].advance_width_);
66  EXPECT_EQ(hmtx->LeftSideBearing(HMTX_ENTRIES_COUNT), HMTX_ENTRIES[0].lsb_);
67  return true;
68}
69
70bool VerifyHMTX(Table* original, Table* target) {
71  EXPECT_TRUE(VerifyHMTX(original));
72  EXPECT_TRUE(VerifyHMTX(target));
73  return true;
74}
75
76}  // namespace sfntly
77