michael@0: /* GRAPHITE2 LICENSING michael@0: michael@0: Copyright 2010, SIL International michael@0: All rights reserved. michael@0: michael@0: This library is free software; you can redistribute it and/or modify michael@0: it under the terms of the GNU Lesser General Public License as published michael@0: by the Free Software Foundation; either version 2.1 of License, or michael@0: (at your option) any later version. michael@0: michael@0: This program is distributed in the hope that it will be useful, michael@0: but WITHOUT ANY WARRANTY; without even the implied warranty of michael@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU michael@0: Lesser General Public License for more details. michael@0: michael@0: You should also have received a copy of the GNU Lesser General Public michael@0: License along with this library in the file named "LICENSE". michael@0: If not, write to the Free Software Foundation, 51 Franklin Street, michael@0: Suite 500, Boston, MA 02110-1335, USA or visit their web page on the michael@0: internet at http://www.fsf.org/licenses/lgpl.html. michael@0: michael@0: Alternatively, the contents of this file may be used under the terms of the michael@0: Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public michael@0: License, as published by the Free Software Foundation, either version 2 michael@0: of the License or (at your option) any later version. michael@0: */ michael@0: #include "graphite2/Font.h" michael@0: #include "inc/Face.h" michael@0: #include "inc/FileFace.h" michael@0: #include "inc/GlyphCache.h" michael@0: #include "inc/CachedFace.h" michael@0: #include "inc/CmapCache.h" michael@0: #include "inc/Silf.h" michael@0: #include "inc/json.h" michael@0: michael@0: using namespace graphite2; michael@0: michael@0: #if !defined GRAPHITE2_NTRACING michael@0: extern json *global_log; michael@0: #endif michael@0: michael@0: namespace michael@0: { michael@0: bool load_face(Face & face, unsigned int options) michael@0: { michael@0: #ifdef GRAPHITE2_TELEMETRY michael@0: telemetry::category _misc_cat(face.tele.misc); michael@0: #endif michael@0: Face::Table silf(face, Tag::Silf); michael@0: if (silf) options &= ~gr_face_dumbRendering; michael@0: else if (!(options & gr_face_dumbRendering)) michael@0: return false; michael@0: michael@0: if (!face.readGlyphs(options)) michael@0: return false; michael@0: michael@0: if (silf) michael@0: { michael@0: if (!face.readFeatures() || !face.readGraphite(silf)) michael@0: { michael@0: #if !defined GRAPHITE2_NTRACING michael@0: if (global_log) michael@0: { michael@0: *global_log << json::object michael@0: << "type" << "fontload" michael@0: << "failure" << face.error() michael@0: << "context" << face.error_context() michael@0: << json::close; michael@0: } michael@0: #endif michael@0: return false; michael@0: } michael@0: else michael@0: return true; michael@0: } michael@0: else michael@0: return options & gr_face_dumbRendering; michael@0: } michael@0: } michael@0: michael@0: extern "C" { michael@0: michael@0: gr_face* gr_make_face_with_ops(const void* appFaceHandle/*non-NULL*/, const gr_face_ops *ops, unsigned int faceOptions) michael@0: //the appFaceHandle must stay alive all the time when the gr_face is alive. When finished with the gr_face, call destroy_face michael@0: { michael@0: if (ops == 0) return 0; michael@0: michael@0: Face *res = new Face(appFaceHandle, *ops); michael@0: if (res && load_face(*res, faceOptions)) michael@0: return static_cast(res); michael@0: michael@0: delete res; michael@0: return 0; michael@0: } michael@0: michael@0: gr_face* gr_make_face(const void* appFaceHandle/*non-NULL*/, gr_get_table_fn tablefn, unsigned int faceOptions) michael@0: { michael@0: const gr_face_ops ops = {sizeof(gr_face_ops), tablefn, NULL}; michael@0: return gr_make_face_with_ops(appFaceHandle, &ops, faceOptions); michael@0: } michael@0: michael@0: #ifndef GRAPHITE2_NSEGCACHE michael@0: gr_face* gr_make_face_with_seg_cache_and_ops(const void* appFaceHandle/*non-NULL*/, const gr_face_ops *ops, unsigned int cacheSize, unsigned int faceOptions) michael@0: //the appFaceHandle must stay alive all the time when the GrFace is alive. When finished with the GrFace, call destroy_face michael@0: { michael@0: if (ops == 0) return 0; michael@0: michael@0: CachedFace *res = new CachedFace(appFaceHandle, *ops); michael@0: if (res && load_face(*res, faceOptions) michael@0: && res->setupCache(cacheSize)) michael@0: return static_cast(static_cast(res)); michael@0: michael@0: delete res; michael@0: return 0; michael@0: } michael@0: michael@0: gr_face* gr_make_face_with_seg_cache(const void* appFaceHandle/*non-NULL*/, gr_get_table_fn getTable, unsigned int cacheSize, unsigned int faceOptions) michael@0: { michael@0: const gr_face_ops ops = {sizeof(gr_face_ops), getTable, NULL}; michael@0: return gr_make_face_with_seg_cache_and_ops(appFaceHandle, &ops, cacheSize, faceOptions); michael@0: } michael@0: #endif michael@0: michael@0: gr_uint32 gr_str_to_tag(const char *str) michael@0: { michael@0: uint32 res = 0; michael@0: int i = strlen(str); michael@0: if (i > 4) i = 4; michael@0: while (--i >= 0) michael@0: res = (res >> 8) + (str[i] << 24); michael@0: return res; michael@0: } michael@0: michael@0: void gr_tag_to_str(gr_uint32 tag, char *str) michael@0: { michael@0: int i = 4; michael@0: while (--i >= 0) michael@0: { michael@0: str[i] = tag & 0xFF; michael@0: tag >>= 8; michael@0: } michael@0: } michael@0: michael@0: inline michael@0: uint32 zeropad(const uint32 x) michael@0: { michael@0: if (x == 0x20202020) return 0; michael@0: if ((x & 0x00FFFFFF) == 0x00202020) return x & 0xFF000000; michael@0: if ((x & 0x0000FFFF) == 0x00002020) return x & 0xFFFF0000; michael@0: if ((x & 0x000000FF) == 0x00000020) return x & 0xFFFFFF00; michael@0: return x; michael@0: } michael@0: michael@0: gr_feature_val* gr_face_featureval_for_lang(const gr_face* pFace, gr_uint32 langname/*0 means clone default*/) //clones the features. if none for language, clones the default michael@0: { michael@0: assert(pFace); michael@0: langname = zeropad(langname); michael@0: return static_cast(pFace->theSill().cloneFeatures(langname)); michael@0: } michael@0: michael@0: michael@0: const gr_feature_ref* gr_face_find_fref(const gr_face* pFace, gr_uint32 featId) //When finished with the FeatureRef, call destroy_FeatureRef michael@0: { michael@0: assert(pFace); michael@0: featId = zeropad(featId); michael@0: const FeatureRef* pRef = pFace->featureById(featId); michael@0: return static_cast(pRef); michael@0: } michael@0: michael@0: unsigned short gr_face_n_fref(const gr_face* pFace) michael@0: { michael@0: assert(pFace); michael@0: return pFace->numFeatures(); michael@0: } michael@0: michael@0: const gr_feature_ref* gr_face_fref(const gr_face* pFace, gr_uint16 i) //When finished with the FeatureRef, call destroy_FeatureRef michael@0: { michael@0: assert(pFace); michael@0: const FeatureRef* pRef = pFace->feature(i); michael@0: return static_cast(pRef); michael@0: } michael@0: michael@0: unsigned short gr_face_n_languages(const gr_face* pFace) michael@0: { michael@0: assert(pFace); michael@0: return pFace->theSill().numLanguages(); michael@0: } michael@0: michael@0: gr_uint32 gr_face_lang_by_index(const gr_face* pFace, gr_uint16 i) michael@0: { michael@0: assert(pFace); michael@0: return pFace->theSill().getLangName(i); michael@0: } michael@0: michael@0: michael@0: void gr_face_destroy(gr_face *face) michael@0: { michael@0: delete face; michael@0: } michael@0: michael@0: michael@0: gr_uint16 gr_face_name_lang_for_locale(gr_face *face, const char * locale) michael@0: { michael@0: if (face) michael@0: { michael@0: return face->languageForLocale(locale); michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: unsigned short gr_face_n_glyphs(const gr_face* pFace) michael@0: { michael@0: return pFace->glyphs().numGlyphs(); michael@0: } michael@0: michael@0: const gr_faceinfo *gr_face_info(const gr_face *pFace, gr_uint32 script) michael@0: { michael@0: if (!pFace) return 0; michael@0: const Silf *silf = pFace->chooseSilf(script); michael@0: if (silf) return silf->silfInfo(); michael@0: return 0; michael@0: } michael@0: michael@0: int gr_face_is_char_supported(const gr_face* pFace, gr_uint32 usv, gr_uint32 script) michael@0: { michael@0: const Cmap & cmap = pFace->cmap(); michael@0: gr_uint16 gid = cmap[usv]; michael@0: if (!gid) michael@0: { michael@0: const Silf * silf = pFace->chooseSilf(script); michael@0: gid = silf->findPseudo(usv); michael@0: } michael@0: return (gid != 0); michael@0: } michael@0: michael@0: #ifndef GRAPHITE2_NFILEFACE michael@0: gr_face* gr_make_file_face(const char *filename, unsigned int faceOptions) michael@0: { michael@0: FileFace* pFileFace = new FileFace(filename); michael@0: if (*pFileFace) michael@0: { michael@0: gr_face* pRes = gr_make_face_with_ops(pFileFace, &FileFace::ops, faceOptions); michael@0: if (pRes) michael@0: { michael@0: pRes->takeFileFace(pFileFace); //takes ownership michael@0: return pRes; michael@0: } michael@0: } michael@0: michael@0: //error when loading michael@0: michael@0: delete pFileFace; michael@0: return NULL; michael@0: } michael@0: michael@0: #ifndef GRAPHITE2_NSEGCACHE michael@0: gr_face* gr_make_file_face_with_seg_cache(const char* filename, unsigned int segCacheMaxSize, unsigned int faceOptions) //returns NULL on failure. //TBD better error handling michael@0: //when finished with, call destroy_face michael@0: { michael@0: FileFace* pFileFace = new FileFace(filename); michael@0: if (*pFileFace) michael@0: { michael@0: gr_face * pRes = gr_make_face_with_seg_cache_and_ops(pFileFace, &FileFace::ops, segCacheMaxSize, faceOptions); michael@0: if (pRes) michael@0: { michael@0: pRes->takeFileFace(pFileFace); //takes ownership michael@0: return pRes; michael@0: } michael@0: } michael@0: michael@0: //error when loading michael@0: michael@0: delete pFileFace; michael@0: return NULL; michael@0: } michael@0: #endif michael@0: #endif //!GRAPHITE2_NFILEFACE michael@0: michael@0: michael@0: } // extern "C" michael@0: michael@0: