gfx/graphite2/src/FileFace.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/graphite2/src/FileFace.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*  GRAPHITE2 LICENSING
     1.5 +
     1.6 +    Copyright 2012, SIL International
     1.7 +    All rights reserved.
     1.8 +
     1.9 +    This library is free software; you can redistribute it and/or modify
    1.10 +    it under the terms of the GNU Lesser General Public License as published
    1.11 +    by the Free Software Foundation; either version 2.1 of License, or
    1.12 +    (at your option) any later version.
    1.13 +
    1.14 +    This program is distributed in the hope that it will be useful,
    1.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 +    Lesser General Public License for more details.
    1.18 +
    1.19 +    You should also have received a copy of the GNU Lesser General Public
    1.20 +    License along with this library in the file named "LICENSE".
    1.21 +    If not, write to the Free Software Foundation, 51 Franklin Street, 
    1.22 +    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 
    1.23 +    internet at http://www.fsf.org/licenses/lgpl.html.
    1.24 +
    1.25 +Alternatively, the contents of this file may be used under the terms of the
    1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    1.27 +License, as published by the Free Software Foundation, either version 2
    1.28 +of the License or (at your option) any later version.
    1.29 +*/
    1.30 +#include <cstring>
    1.31 +#include "inc/FileFace.h"
    1.32 +
    1.33 +
    1.34 +#ifndef GRAPHITE2_NFILEFACE
    1.35 +
    1.36 +using namespace graphite2;
    1.37 +
    1.38 +FileFace::FileFace(const char *filename)
    1.39 +: _file(fopen(filename, "rb")),
    1.40 +  _file_len(0),
    1.41 +  _header_tbl(NULL),
    1.42 +  _table_dir(NULL)
    1.43 +{
    1.44 +    if (!_file) return;
    1.45 +
    1.46 +    if (fseek(_file, 0, SEEK_END)) return;
    1.47 +    _file_len = ftell(_file);
    1.48 +    if (fseek(_file, 0, SEEK_SET)) return;
    1.49 +
    1.50 +    size_t tbl_offset, tbl_len;
    1.51 +
    1.52 +    // Get the header.
    1.53 +    if (!TtfUtil::GetHeaderInfo(tbl_offset, tbl_len)) return;
    1.54 +    if (fseek(_file, tbl_offset, SEEK_SET)) return;
    1.55 +    _header_tbl = (TtfUtil::Sfnt::OffsetSubTable*)gralloc<char>(tbl_len);
    1.56 +    if (_header_tbl)
    1.57 +    {
    1.58 +        if (fread(_header_tbl, 1, tbl_len, _file) != tbl_len) return;
    1.59 +        if (!TtfUtil::CheckHeader(_header_tbl)) return;
    1.60 +    }
    1.61 +
    1.62 +    // Get the table directory
    1.63 +    if (!TtfUtil::GetTableDirInfo(_header_tbl, tbl_offset, tbl_len)) return;
    1.64 +    _table_dir = (TtfUtil::Sfnt::OffsetSubTable::Entry*)gralloc<char>(tbl_len);
    1.65 +    if (fseek(_file, tbl_offset, SEEK_SET)) return;
    1.66 +    if (_table_dir)
    1.67 +        if (fread(_table_dir, 1, tbl_len, _file) != tbl_len) return;
    1.68 +}
    1.69 +
    1.70 +FileFace::~FileFace()
    1.71 +{
    1.72 +    free(_table_dir);
    1.73 +    free(_header_tbl);
    1.74 +    if (_file)
    1.75 +        fclose(_file);
    1.76 +}
    1.77 +
    1.78 +
    1.79 +const void *FileFace::get_table_fn(const void* appFaceHandle, unsigned int name, size_t *len)
    1.80 +{
    1.81 +    if (appFaceHandle == 0)     return 0;
    1.82 +    const FileFace & file_face = *static_cast<const FileFace *>(appFaceHandle);
    1.83 +
    1.84 +    void *tbl;
    1.85 +    size_t tbl_offset, tbl_len;
    1.86 +    if (!TtfUtil::GetTableInfo(name, file_face._header_tbl, file_face._table_dir, tbl_offset, tbl_len))
    1.87 +        return 0;
    1.88 +
    1.89 +    if (tbl_offset + tbl_len > file_face._file_len
    1.90 +            || fseek(file_face._file, tbl_offset, SEEK_SET) != 0)
    1.91 +        return 0;
    1.92 +
    1.93 +    tbl = malloc(tbl_len);
    1.94 +    if (fread(tbl, 1, tbl_len, file_face._file) != tbl_len)
    1.95 +    {
    1.96 +        free(tbl);
    1.97 +        return 0;
    1.98 +    }
    1.99 +
   1.100 +    if (len) *len = tbl_len;
   1.101 +    return tbl;
   1.102 +}
   1.103 +
   1.104 +void FileFace::rel_table_fn(const void* appFaceHandle, const void *table_buffer)
   1.105 +{
   1.106 +    if (appFaceHandle == 0)     return;
   1.107 +
   1.108 +    free(const_cast<void *>(table_buffer));
   1.109 +}
   1.110 +
   1.111 +const gr_face_ops FileFace::ops = { sizeof FileFace::ops, &FileFace::get_table_fn, &FileFace::rel_table_fn };
   1.112 +
   1.113 +
   1.114 +#endif                  //!GRAPHITE2_NFILEFACE

mercurial