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 <stdio.h>
18
19#include "sfntly/font.h"
20#include "sfntly/table/table.h"
21#include "sfntly/tag.h"
22#include "subtly/stats.h"
23
24namespace subtly {
25using namespace sfntly;
26
27int32_t TotalFontSize(Font* font) {
28  int32_t size = 0;
29  const TableMap* table_map = font->GetTableMap();
30  for (TableMap::const_iterator it = table_map->begin(),
31           e = table_map->end(); it != e; ++it) {
32    size += it->second->DataLength();
33  }
34  return size;
35}
36
37double TableSizePercent(Font* font, int32_t tag) {
38  TablePtr table = font->GetTable(tag);
39  return static_cast<double>(table->DataLength()) / TotalFontSize(font) * 100;
40}
41
42void PrintComparison(FILE* out, Font* font, Font* new_font) {
43  fprintf(out, "====== Table Comparison (original v. subset) ======\n");
44  const TableMap* tables = font->GetTableMap();
45  for (TableMap::const_iterator it = tables->begin(),
46           e = tables->end(); it != e; ++it) {
47    char *name = TagToString(it->first);
48    int32_t size = it->second->DataLength();
49    fprintf(out, "-- %s: %d (%lf%%) ", name, size,
50            TableSizePercent(font, it->first));
51    delete[] name;
52
53    Ptr<FontDataTable> new_table = new_font->GetTable(it->first);
54    int32_t new_size = 0;
55    double size_percent = 0;
56    if (new_table) {
57      new_size = new_table->DataLength();
58      size_percent = subtly::TableSizePercent(new_font, it->first);
59    }
60
61    if (new_size == size) {
62      fprintf(out, "| same size\n");
63    } else {
64      fprintf(out, "-> %d (%lf%%) | %lf%% of original\n", new_size,
65              size_percent, static_cast<double>(new_size) / size * 100);
66    }
67  }
68}
69
70void PrintStats(FILE* out, Font* font) {
71  fprintf(out, "====== Table Stats ======\n");
72  const TableMap* tables = font->GetTableMap();
73  for (TableMap::const_iterator it = tables->begin(),
74           e = tables->end(); it != e; ++it) {
75    char *name = TagToString(it->first);
76    int32_t size = it->second->DataLength();
77    fprintf(out, "-- %s: %d (%lf%%)\n", name, size,
78            TableSizePercent(font, it->first));
79    delete[] name;
80  }
81}
82}
83