Home | History | Annotate | Download | only in test
      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 "gtest/gtest.h"
     20 #include "sfntly/data/memory_byte_array.h"
     21 #include "sfntly/data/growable_memory_byte_array.h"
     22 #include "sfntly/port/file_input_stream.h"
     23 #include "test/test_font_utils.h"
     24 
     25 namespace sfntly {
     26 
     27 void BuilderForFontFile(const char* font_path, FontFactory* factory,
     28                         FontBuilderArray* builders) {
     29   assert(factory);
     30   FileInputStream is;
     31   is.Open(font_path);
     32   factory->LoadFontsForBuilding(&is, builders);
     33   EXPECT_GT(builders->size(), static_cast<size_t>(0));
     34 }
     35 
     36 void SerializeFont(const char* font_path, FontFactory* factory, Font* font) {
     37   assert(font_path);
     38   assert(factory);
     39   assert(font);
     40   MemoryOutputStream output_stream;
     41   factory->SerializeFont(font, &output_stream);
     42   SerializeToFile(&output_stream, font_path);
     43 }
     44 
     45 void LoadFont(const char* font_path, FontFactory* factory, FontArray* fonts) {
     46   FileInputStream is;
     47   is.Open(font_path);
     48   factory->LoadFonts(&is, fonts);
     49   is.Close();
     50 }
     51 
     52 void LoadFontUsingByteVector(const char* font_path,
     53                             bool fingerprint,
     54                             FontArray* fonts) {
     55   ByteVector bv;
     56   LoadFile(font_path, &bv);
     57   FontFactoryPtr factory;
     58   factory.Attach(FontFactory::GetInstance());
     59   factory->FingerprintFont(fingerprint);
     60   factory->LoadFonts(&bv, fonts);
     61 }
     62 
     63 void LoadFile(const char* input_file_path, ByteVector* input_buffer) {
     64   assert(input_file_path);
     65   assert(input_buffer);
     66 
     67   FILE* input_file = NULL;
     68 #if defined WIN32
     69   fopen_s(&input_file, input_file_path, "rb");
     70 #else
     71   input_file = fopen(input_file_path, "rb");
     72 #endif
     73   EXPECT_NE(input_file, reinterpret_cast<FILE*>(NULL));
     74   fseek(input_file, 0, SEEK_END);
     75   size_t file_size = ftell(input_file);
     76   fseek(input_file, 0, SEEK_SET);
     77   input_buffer->resize(file_size);
     78   size_t bytes_read = fread(&((*input_buffer)[0]), 1, file_size, input_file);
     79   EXPECT_EQ(bytes_read, file_size);
     80   fclose(input_file);
     81 }
     82 
     83 void SerializeToFile(MemoryOutputStream* output_stream, const char* file_path) {
     84   assert(file_path);
     85   assert(output_stream);
     86 
     87   FILE* output_file = NULL;
     88 #if defined WIN32
     89   fopen_s(&output_file, file_path, "wb");
     90 #else
     91   output_file = fopen(file_path, "wb");
     92 #endif
     93   EXPECT_NE(output_file, reinterpret_cast<FILE*>(NULL));
     94   fwrite(output_stream->Get(), 1, output_stream->Size(), output_file);
     95   fflush(output_file);
     96   fclose(output_file);
     97 }
     98 
     99 void HexDump(const unsigned char* byte_data, size_t length) {
    100   if (byte_data == NULL || length == 0) {
    101     fprintf(stderr, "<NULL>\n");
    102     return;
    103   }
    104 
    105   fprintf(stderr, "data length = %ld (%lx)\n", length, length);
    106   for (size_t i = 0; i < length; ++i) {
    107     fprintf(stderr, "%02x ", byte_data[i]);
    108     if ((i & 0xf) == 0xf) {
    109       fprintf(stderr, "\n");
    110     }
    111   }
    112   fprintf(stderr, "\n");
    113 }
    114 
    115 }  // namespace sfntly
    116