12aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com/*
22aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * Copyright 2011 Google Inc. All Rights Reserved.
32aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com *
42aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * Licensed under the Apache License, Version 2.0 (the "License");
52aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * you may not use this file except in compliance with the License.
62aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * You may obtain a copy of the License at
72aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com *
82aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com *      http://www.apache.org/licenses/LICENSE-2.0
92aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com *
102aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * Unless required by applicable law or agreed to in writing, software
112aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * distributed under the License is distributed on an "AS IS" BASIS,
122aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * See the License for the specific language governing permissions and
142aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * limitations under the License.
152aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com */
162aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
172aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#include <stdio.h>
182aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
192aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#include "gtest/gtest.h"
202aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#include "sfntly/data/memory_byte_array.h"
21f8dcae69a9ab5b147c115b1ab04132c876c2f35cdfilimon@google.com#include "sfntly/data/growable_memory_byte_array.h"
222aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#include "sfntly/port/file_input_stream.h"
232aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#include "test/test_font_utils.h"
242aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
252aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.comnamespace sfntly {
262aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
27246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comvoid BuilderForFontFile(const char* font_path, FontFactory* factory,
282aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com                        FontBuilderArray* builders) {
292aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  assert(factory);
302aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  FileInputStream is;
31246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  is.Open(font_path);
32246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  factory->LoadFontsForBuilding(&is, builders);
332aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  EXPECT_GT(builders->size(), static_cast<size_t>(0));
342aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com}
352aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
36246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comvoid SerializeFont(const char* font_path, FontFactory* factory, Font* font) {
372aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  assert(font_path);
382aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  assert(factory);
392aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  assert(font);
402aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  MemoryOutputStream output_stream;
41246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  factory->SerializeFont(font, &output_stream);
42246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  SerializeToFile(&output_stream, font_path);
432aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com}
442aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
45246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comvoid LoadFont(const char* font_path, FontFactory* factory, FontArray* fonts) {
462aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  FileInputStream is;
47246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  is.Open(font_path);
48246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  factory->LoadFonts(&is, fonts);
49246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  is.Close();
502aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com}
512aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
52b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.comvoid LoadFontUsingByteVector(const char* font_path,
53f8dcae69a9ab5b147c115b1ab04132c876c2f35cdfilimon@google.com                            bool fingerprint,
54f8dcae69a9ab5b147c115b1ab04132c876c2f35cdfilimon@google.com                            FontArray* fonts) {
55f8dcae69a9ab5b147c115b1ab04132c876c2f35cdfilimon@google.com  ByteVector bv;
56f8dcae69a9ab5b147c115b1ab04132c876c2f35cdfilimon@google.com  LoadFile(font_path, &bv);
57f8dcae69a9ab5b147c115b1ab04132c876c2f35cdfilimon@google.com  FontFactoryPtr factory;
58f8dcae69a9ab5b147c115b1ab04132c876c2f35cdfilimon@google.com  factory.Attach(FontFactory::GetInstance());
59f8dcae69a9ab5b147c115b1ab04132c876c2f35cdfilimon@google.com  factory->FingerprintFont(fingerprint);
60b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  factory->LoadFonts(&bv, fonts);
61f8dcae69a9ab5b147c115b1ab04132c876c2f35cdfilimon@google.com}
62f8dcae69a9ab5b147c115b1ab04132c876c2f35cdfilimon@google.com
63246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comvoid LoadFile(const char* input_file_path, ByteVector* input_buffer) {
642aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  assert(input_file_path);
652aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  assert(input_buffer);
662aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
672aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  FILE* input_file = NULL;
682aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#if defined WIN32
692aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  fopen_s(&input_file, input_file_path, "rb");
702aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#else
712aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  input_file = fopen(input_file_path, "rb");
722aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#endif
732aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  EXPECT_NE(input_file, reinterpret_cast<FILE*>(NULL));
742aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  fseek(input_file, 0, SEEK_END);
752aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  size_t file_size = ftell(input_file);
762aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  fseek(input_file, 0, SEEK_SET);
772aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  input_buffer->resize(file_size);
7835a9bf28a889295528bfe46ab51902a460be5407arthurhsu@google.com  size_t bytes_read = fread(&((*input_buffer)[0]), 1, file_size, input_file);
7935a9bf28a889295528bfe46ab51902a460be5407arthurhsu@google.com  EXPECT_EQ(bytes_read, file_size);
802aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  fclose(input_file);
812aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com}
822aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
83246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comvoid SerializeToFile(MemoryOutputStream* output_stream, const char* file_path) {
842aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  assert(file_path);
852aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  assert(output_stream);
862aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
872aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  FILE* output_file = NULL;
882aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#if defined WIN32
892aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  fopen_s(&output_file, file_path, "wb");
902aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#else
912aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  output_file = fopen(file_path, "wb");
922aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#endif
932aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  EXPECT_NE(output_file, reinterpret_cast<FILE*>(NULL));
94246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  fwrite(output_stream->Get(), 1, output_stream->Size(), output_file);
952aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  fflush(output_file);
962aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com  fclose(output_file);
972aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com}
982aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com
99246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comvoid HexDump(const unsigned char* byte_data, size_t length) {
100ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com  if (byte_data == NULL || length == 0) {
101ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com    fprintf(stderr, "<NULL>\n");
102ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com    return;
103ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com  }
104ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com
105ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com  fprintf(stderr, "data length = %ld (%lx)\n", length, length);
106ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com  for (size_t i = 0; i < length; ++i) {
107ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com    fprintf(stderr, "%02x ", byte_data[i]);
108ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com    if ((i & 0xf) == 0xf) {
109ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com      fprintf(stderr, "\n");
110ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com    }
111ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com  }
112ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com  fprintf(stderr, "\n");
113ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com}
114ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com
1152aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com}  // namespace sfntly
116