Path: blob/master/thirdparty/graphite/src/FileFace.cpp
9903 views
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later1// Copyright 2012, SIL International, All rights reserved.23#include <cstring>4#include "inc/FileFace.h"567#ifndef GRAPHITE2_NFILEFACE89using namespace graphite2;1011FileFace::FileFace(const char *filename)12: _file(fopen(filename, "rb")),13_file_len(0),14_header_tbl(NULL),15_table_dir(NULL)16{17if (!_file) return;1819if (fseek(_file, 0, SEEK_END)) return;20_file_len = ftell(_file);21if (fseek(_file, 0, SEEK_SET)) return;2223size_t tbl_offset, tbl_len;2425// Get the header.26if (!TtfUtil::GetHeaderInfo(tbl_offset, tbl_len)) return;27if (fseek(_file, long(tbl_offset), SEEK_SET)) return;28_header_tbl = (TtfUtil::Sfnt::OffsetSubTable*)gralloc<char>(tbl_len);29if (_header_tbl)30{31if (fread(_header_tbl, 1, tbl_len, _file) != tbl_len) return;32if (!TtfUtil::CheckHeader(_header_tbl)) return;33}3435// Get the table directory36if (!TtfUtil::GetTableDirInfo(_header_tbl, tbl_offset, tbl_len)) return;37_table_dir = (TtfUtil::Sfnt::OffsetSubTable::Entry*)gralloc<char>(tbl_len);38if (fseek(_file, long(tbl_offset), SEEK_SET)) return;39if (_table_dir && fread(_table_dir, 1, tbl_len, _file) != tbl_len)40{41free(_table_dir);42_table_dir = NULL;43}44return;45}4647FileFace::~FileFace()48{49free(_table_dir);50free(_header_tbl);51if (_file)52fclose(_file);53}545556const void *FileFace::get_table_fn(const void* appFaceHandle, unsigned int name, size_t *len)57{58if (appFaceHandle == 0) return 0;59const FileFace & file_face = *static_cast<const FileFace *>(appFaceHandle);6061void *tbl;62size_t tbl_offset, tbl_len;63if (!TtfUtil::GetTableInfo(name, file_face._header_tbl, file_face._table_dir, tbl_offset, tbl_len))64return 0;6566if (tbl_offset > file_face._file_len || tbl_len > file_face._file_len - tbl_offset67|| fseek(file_face._file, long(tbl_offset), SEEK_SET) != 0)68return 0;6970tbl = malloc(tbl_len);71if (!tbl || fread(tbl, 1, tbl_len, file_face._file) != tbl_len)72{73free(tbl);74return 0;75}7677if (len) *len = tbl_len;78return tbl;79}8081void FileFace::rel_table_fn(const void* appFaceHandle, const void *table_buffer)82{83if (appFaceHandle == 0) return;8485free(const_cast<void *>(table_buffer));86}8788const gr_face_ops FileFace::ops = { sizeof FileFace::ops, &FileFace::get_table_fn, &FileFace::rel_table_fn };899091#endif //!GRAPHITE2_NFILEFACE929394