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/utils.h"
18
19#include "sfntly/data/growable_memory_byte_array.h"
20#include "sfntly/data/memory_byte_array.h"
21#include "sfntly/font.h"
22#include "sfntly/font_factory.h"
23#include "sfntly/port/file_input_stream.h"
24#include "sfntly/port/memory_output_stream.h"
25
26namespace subtly {
27using namespace sfntly;
28
29CALLER_ATTACH Font* LoadFont(const char* font_path) {
30  Ptr<FontFactory> font_factory;
31  font_factory.Attach(FontFactory::GetInstance());
32  FontArray fonts;
33  LoadFonts(font_path, font_factory, &fonts);
34  return fonts[0].Detach();
35}
36
37CALLER_ATTACH Font::Builder* LoadFontBuilder(const char* font_path) {
38  FontFactoryPtr font_factory;
39  font_factory.Attach(FontFactory::GetInstance());
40  FontBuilderArray builders;
41  LoadFontBuilders(font_path, font_factory, &builders);
42  return builders[0].Detach();
43}
44
45void LoadFonts(const char* font_path, FontFactory* factory, FontArray* fonts) {
46  FileInputStream input_stream;
47  input_stream.Open(font_path);
48  factory->LoadFonts(&input_stream, fonts);
49  input_stream.Close();
50}
51
52void LoadFontBuilders(const char* font_path,
53                      FontFactory* factory,
54                      FontBuilderArray* builders) {
55  FileInputStream input_stream;
56  input_stream.Open(font_path);
57  factory->LoadFontsForBuilding(&input_stream, builders);
58  input_stream.Close();
59}
60
61bool SerializeFont(const char* font_path, Font* font) {
62  if (!font_path)
63    return false;
64  FontFactoryPtr font_factory;
65  font_factory.Attach(FontFactory::GetInstance());
66  return SerializeFont(font_path, font_factory, font);
67}
68
69bool SerializeFont(const char* font_path, FontFactory* factory, Font* font) {
70  if (!font_path || !factory || !font)
71    return false;
72  // Serializing the font to a stream.
73  MemoryOutputStream output_stream;
74  factory->SerializeFont(font, &output_stream);
75  // Serializing the stream to a file.
76  FILE* output_file = NULL;
77#if defined WIN32
78  fopen_s(&output_file, font_path, "wb");
79#else
80  output_file = fopen(font_path, "wb");
81#endif
82  if (output_file == reinterpret_cast<FILE*>(NULL))
83    return false;
84  for (size_t i = 0; i < output_stream.Size(); ++i) {
85    fwrite(&(output_stream.Get()[i]), 1, 1, output_file);
86  }
87  fflush(output_file);
88  fclose(output_file);
89  return true;
90}
91};
92