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 "subtly/merger.h"
18
19#include <stdio.h>
20
21#include "sfntly/font.h"
22#include "sfntly/font_factory.h"
23#include "subtly/character_predicate.h"
24#include "subtly/font_assembler.h"
25#include "subtly/font_info.h"
26#include "subtly/utils.h"
27
28namespace subtly {
29using namespace sfntly;
30
31/******************************************************************************
32 * Merger class
33 ******************************************************************************/
34Merger::Merger(FontArray* fonts) {
35  if (!fonts) {
36    return;
37  }
38  int32_t num_fonts = fonts->size();
39  for (int32_t i = 0; i < num_fonts; ++i) {
40    fonts_.insert(std::make_pair(i, fonts->at(i)));
41  }
42}
43
44CALLER_ATTACH Font* Merger::Merge() {
45  Ptr<FontInfo> merged_info;
46  merged_info.Attach(MergeFontInfos());
47  if (!merged_info) {
48#if defined (SUBTLY_DEBUG)
49    fprintf(stderr, "Could not create merged font info\n");
50#endif
51    return NULL;
52  }
53  Ptr<FontAssembler> font_assembler = new FontAssembler(merged_info);
54  return font_assembler->Assemble();
55}
56
57CALLER_ATTACH FontInfo* Merger::MergeFontInfos() {
58  Ptr<FontInfo> font_info = new FontInfo;
59  font_info->set_fonts(&fonts_);
60  for (FontIdMap::iterator it = fonts_.begin(),
61           e = fonts_.end(); it != e; ++it) {
62    Ptr<FontSourcedInfoBuilder> info_builder =
63        new FontSourcedInfoBuilder(it->second, it->first, NULL);
64    Ptr<FontInfo> current_font_info;
65    current_font_info.Attach(info_builder->GetFontInfo());
66    if (!current_font_info) {
67#if defined (SUBTLY_DEBUG)
68      fprintf(stderr, "Couldn't create font info. "
69              "No subset will be generated.\n");
70#endif
71      return NULL;
72    }
73    font_info->chars_to_glyph_ids()->insert(
74        current_font_info->chars_to_glyph_ids()->begin(),
75        current_font_info->chars_to_glyph_ids()->end());
76    font_info->resolved_glyph_ids()->insert(
77        current_font_info->resolved_glyph_ids()->begin(),
78        current_font_info->resolved_glyph_ids()->end());
79#if defined (SUBTLY_DEBUG)
80    fprintf(stderr, "Counts: chars_to_glyph_ids: %d; resoved_glyph_ids: %d\n",
81            font_info->chars_to_glyph_ids()->size(),
82            font_info->resolved_glyph_ids()->size());
83#endif
84  }
85  return font_info.Detach();
86}
87}
88