18c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com/*
28c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com * Copyright 2011 Google Inc. All Rights Reserved.
38c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com *
48c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com * Licensed under the Apache License, Version 2.0 (the "License");
58c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com * you may not use this file except in compliance with the License.
68c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com * You may obtain a copy of the License at
78c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com *
88c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com *      http://www.apache.org/licenses/LICENSE-2.0
98c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com *
108c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com * Unless required by applicable law or agreed to in writing, software
118c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com * distributed under the License is distributed on an "AS IS" BASIS,
128c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com * See the License for the specific language governing permissions and
148c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com * limitations under the License.
158c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com */
168c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com
178c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com#include <stdio.h>
188c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com
198c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com#include "sfntly/font.h"
208c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com#include "sfntly/table/table.h"
218c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com#include "sfntly/tag.h"
228c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com#include "subtly/stats.h"
238c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com
248c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.comnamespace subtly {
258c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.comusing namespace sfntly;
268c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com
278c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.comint32_t TotalFontSize(Font* font) {
288c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  int32_t size = 0;
298c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  const TableMap* table_map = font->GetTableMap();
308c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  for (TableMap::const_iterator it = table_map->begin(),
318c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com           e = table_map->end(); it != e; ++it) {
328c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    size += it->second->DataLength();
338c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  }
348c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  return size;
358c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com}
368c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com
378c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.comdouble TableSizePercent(Font* font, int32_t tag) {
388c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  TablePtr table = font->GetTable(tag);
398c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  return static_cast<double>(table->DataLength()) / TotalFontSize(font) * 100;
408c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com}
418c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com
428c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.comvoid PrintComparison(FILE* out, Font* font, Font* new_font) {
438c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  fprintf(out, "====== Table Comparison (original v. subset) ======\n");
448c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  const TableMap* tables = font->GetTableMap();
458c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  for (TableMap::const_iterator it = tables->begin(),
468c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com           e = tables->end(); it != e; ++it) {
478c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    char *name = TagToString(it->first);
488c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    int32_t size = it->second->DataLength();
498c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    fprintf(out, "-- %s: %d (%lf%%) ", name, size,
508c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com            TableSizePercent(font, it->first));
518c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    delete[] name;
528c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com
538c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    Ptr<FontDataTable> new_table = new_font->GetTable(it->first);
548c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    int32_t new_size = 0;
558c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    double size_percent = 0;
568c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    if (new_table) {
578c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com      new_size = new_table->DataLength();
588c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com      size_percent = subtly::TableSizePercent(new_font, it->first);
598c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    }
608c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com
618c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    if (new_size == size) {
628c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com      fprintf(out, "| same size\n");
638c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    } else {
648c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com      fprintf(out, "-> %d (%lf%%) | %lf%% of original\n", new_size,
658c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com              size_percent, static_cast<double>(new_size) / size * 100);
668c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    }
678c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  }
688c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com}
698c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com
708c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.comvoid PrintStats(FILE* out, Font* font) {
718c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  fprintf(out, "====== Table Stats ======\n");
728c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  const TableMap* tables = font->GetTableMap();
738c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  for (TableMap::const_iterator it = tables->begin(),
748c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com           e = tables->end(); it != e; ++it) {
758c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    char *name = TagToString(it->first);
768c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    int32_t size = it->second->DataLength();
778c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    fprintf(out, "-- %s: %d (%lf%%)\n", name, size,
788c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com            TableSizePercent(font, it->first));
798c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com    delete[] name;
808c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com  }
818c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com}
828c433a9f5819ad995a14c8476c266487c8a82f53dfilimon@google.com}
83