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

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

mercurial