other-licenses/skia-npapi/ANPTypeface.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright 2008, The Android Open Source Project
michael@0 3 *
michael@0 4 * Redistribution and use in source and binary forms, with or without
michael@0 5 * modification, are permitted provided that the following conditions
michael@0 6 * are met:
michael@0 7 * * Redistributions of source code must retain the above copyright
michael@0 8 * notice, this list of conditions and the following disclaimer.
michael@0 9 * * Redistributions in binary form must reproduce the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer in the
michael@0 11 * documentation and/or other materials provided with the distribution.
michael@0 12 *
michael@0 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
michael@0 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
michael@0 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
michael@0 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
michael@0 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 24 */
michael@0 25
michael@0 26 // must include config.h first for webkit to fiddle with new/delete
michael@0 27 #include "SkANP.h"
michael@0 28 #include "SkFontHost.h"
michael@0 29 #include "SkStream.h"
michael@0 30
michael@0 31 static ANPTypeface* anp_createFromName(const char name[], ANPTypefaceStyle s) {
michael@0 32 SkTypeface* tf = SkTypeface::CreateFromName(name,
michael@0 33 static_cast<SkTypeface::Style>(s));
michael@0 34 return reinterpret_cast<ANPTypeface*>(tf);
michael@0 35 }
michael@0 36
michael@0 37 static ANPTypeface* anp_createFromTypeface(const ANPTypeface* family,
michael@0 38 ANPTypefaceStyle s) {
michael@0 39 SkTypeface* tf = SkTypeface::CreateFromTypeface(family,
michael@0 40 static_cast<SkTypeface::Style>(s));
michael@0 41 return reinterpret_cast<ANPTypeface*>(tf);
michael@0 42 }
michael@0 43
michael@0 44 static int32_t anp_getRefCount(const ANPTypeface* tf) {
michael@0 45 return tf ? tf->getRefCnt() : 0;
michael@0 46 }
michael@0 47
michael@0 48 static void anp_ref(ANPTypeface* tf) {
michael@0 49 SkSafeRef(tf);
michael@0 50 }
michael@0 51
michael@0 52 static void anp_unref(ANPTypeface* tf) {
michael@0 53 SkSafeUnref(tf);
michael@0 54 }
michael@0 55
michael@0 56 static ANPTypefaceStyle anp_getStyle(const ANPTypeface* tf) {
michael@0 57 SkTypeface::Style s = tf ? tf->style() : SkTypeface::kNormal;
michael@0 58 return static_cast<ANPTypefaceStyle>(s);
michael@0 59 }
michael@0 60
michael@0 61 static int32_t anp_getFontPath(const ANPTypeface* tf, char fileName[],
michael@0 62 int32_t length, int32_t* index) {
michael@0 63 SkStream* stream = tf->openStream(index);
michael@0 64
michael@0 65 return 0;
michael@0 66 /*
michael@0 67 if (stream->getFileName()) {
michael@0 68 strcpy(fileName, stream->getFileName());
michael@0 69 } else {
michael@0 70 return 0;
michael@0 71 }
michael@0 72
michael@0 73 return strlen(fileName);
michael@0 74 */
michael@0 75 }
michael@0 76
michael@0 77 static const char* gFontDir;
michael@0 78 #define FONT_DIR_SUFFIX "/fonts/"
michael@0 79
michael@0 80 static const char* anp_getFontDirectoryPath() {
michael@0 81 if (NULL == gFontDir) {
michael@0 82 const char* root = getenv("ANDROID_ROOT");
michael@0 83 size_t len = strlen(root);
michael@0 84 char* storage = (char*)malloc(len + sizeof(FONT_DIR_SUFFIX));
michael@0 85 if (NULL == storage) {
michael@0 86 return NULL;
michael@0 87 }
michael@0 88 memcpy(storage, root, len);
michael@0 89 memcpy(storage + len, FONT_DIR_SUFFIX, sizeof(FONT_DIR_SUFFIX));
michael@0 90 // save this assignment for last, so that if multiple threads call us
michael@0 91 // (which should never happen), we never return an incomplete global.
michael@0 92 // At worst, we would allocate storage for the path twice.
michael@0 93 gFontDir = storage;
michael@0 94 }
michael@0 95 return gFontDir;
michael@0 96 }
michael@0 97
michael@0 98 ///////////////////////////////////////////////////////////////////////////////
michael@0 99
michael@0 100 #define ASSIGN(obj, name) (obj)->name = anp_##name
michael@0 101
michael@0 102 void InitTypeFaceInterface(ANPTypefaceInterfaceV0* i) {
michael@0 103 ASSIGN(i, createFromName);
michael@0 104 ASSIGN(i, createFromTypeface);
michael@0 105 ASSIGN(i, getRefCount);
michael@0 106 ASSIGN(i, ref);
michael@0 107 ASSIGN(i, unref);
michael@0 108 ASSIGN(i, getStyle);
michael@0 109 ASSIGN(i, getFontPath);
michael@0 110 ASSIGN(i, getFontDirectoryPath);
michael@0 111 }

mercurial