FileFace.cpp (3612B)
1 /* GRAPHITE2 LICENSING 2 3 Copyright 2012, SIL International 4 All rights reserved. 5 6 This library is free software; you can redistribute it and/or modify 7 it under the terms of the GNU Lesser General Public License as published 8 by the Free Software Foundation; either version 2.1 of License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should also have received a copy of the GNU Lesser General Public 17 License along with this library in the file named "LICENSE". 18 If not, write to the Free Software Foundation, 51 Franklin Street, 19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 20 internet at http://www.fsf.org/licenses/lgpl.html. 21 22 Alternatively, the contents of this file may be used under the terms of the 23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public 24 License, as published by the Free Software Foundation, either version 2 25 of the License or (at your option) any later version. 26 */ 27 #include <cstring> 28 #include "inc/FileFace.h" 29 30 31 #ifndef GRAPHITE2_NFILEFACE 32 33 using namespace graphite2; 34 35 FileFace::FileFace(const char *filename) 36 : _file(fopen(filename, "rb")), 37 _file_len(0), 38 _header_tbl(NULL), 39 _table_dir(NULL) 40 { 41 if (!_file) return; 42 43 if (fseek(_file, 0, SEEK_END)) return; 44 _file_len = ftell(_file); 45 if (fseek(_file, 0, SEEK_SET)) return; 46 47 size_t tbl_offset, tbl_len; 48 49 // Get the header. 50 if (!TtfUtil::GetHeaderInfo(tbl_offset, tbl_len)) return; 51 if (fseek(_file, long(tbl_offset), SEEK_SET)) return; 52 _header_tbl = (TtfUtil::Sfnt::OffsetSubTable*)gralloc<char>(tbl_len); 53 if (_header_tbl) 54 { 55 if (fread(_header_tbl, 1, tbl_len, _file) != tbl_len) return; 56 if (!TtfUtil::CheckHeader(_header_tbl)) return; 57 } 58 59 // Get the table directory 60 if (!TtfUtil::GetTableDirInfo(_header_tbl, tbl_offset, tbl_len)) return; 61 _table_dir = (TtfUtil::Sfnt::OffsetSubTable::Entry*)gralloc<char>(tbl_len); 62 if (fseek(_file, long(tbl_offset), SEEK_SET)) return; 63 if (_table_dir && fread(_table_dir, 1, tbl_len, _file) != tbl_len) 64 { 65 free(_table_dir); 66 _table_dir = NULL; 67 } 68 return; 69 } 70 71 FileFace::~FileFace() 72 { 73 free(_table_dir); 74 free(_header_tbl); 75 if (_file) 76 fclose(_file); 77 } 78 79 80 const void *FileFace::get_table_fn(const void* appFaceHandle, unsigned int name, size_t *len) 81 { 82 if (appFaceHandle == 0) return 0; 83 const FileFace & file_face = *static_cast<const FileFace *>(appFaceHandle); 84 85 void *tbl; 86 size_t tbl_offset, tbl_len; 87 if (!TtfUtil::GetTableInfo(name, file_face._header_tbl, file_face._table_dir, tbl_offset, tbl_len)) 88 return 0; 89 90 if (tbl_offset > file_face._file_len || tbl_len > file_face._file_len - tbl_offset 91 || fseek(file_face._file, long(tbl_offset), SEEK_SET) != 0) 92 return 0; 93 94 tbl = malloc(tbl_len); 95 if (!tbl || fread(tbl, 1, tbl_len, file_face._file) != tbl_len) 96 { 97 free(tbl); 98 return 0; 99 } 100 101 if (len) *len = tbl_len; 102 return tbl; 103 } 104 105 void FileFace::rel_table_fn(const void* appFaceHandle, const void *table_buffer) 106 { 107 if (appFaceHandle == 0) return; 108 109 free(const_cast<void *>(table_buffer)); 110 } 111 112 const gr_face_ops FileFace::ops = { sizeof FileFace::ops, &FileFace::get_table_fn, &FileFace::rel_table_fn }; 113 114 115 #endif //!GRAPHITE2_NFILEFACE