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  */
25 
26 // must include config.h first for webkit to fiddle with new/delete
27 #include "SkANP.h"
28 #include "SkStream.h"
29 #include <stdlib.h>
30 
anp_createFromName(const char name[],ANPTypefaceStyle s)31 static ANPTypeface* anp_createFromName(const char name[], ANPTypefaceStyle s) {
32     sk_sp<SkTypeface> tf = SkTypeface::MakeFromName(name,
33                                         SkFontStyle::FromOldStyle(s));
34     return reinterpret_cast<ANPTypeface*>(tf.release());
35 }
36 
anp_createFromTypeface(const ANPTypeface * family,ANPTypefaceStyle s)37 static ANPTypeface* anp_createFromTypeface(const ANPTypeface* family,
38                                            ANPTypefaceStyle s) {
39     sk_sp<SkTypeface> tf = SkTypeface::MakeFromTypeface(const_cast<ANPTypeface*>(family),
40                                           static_cast<SkTypeface::Style>(s));
41     return reinterpret_cast<ANPTypeface*>(tf.release());
42 }
43 
anp_getRefCount(const ANPTypeface * tf)44 static int32_t anp_getRefCount(const ANPTypeface* tf) {
45     return tf ? tf->getRefCnt() : 0;
46 }
47 
anp_ref(ANPTypeface * tf)48 static void anp_ref(ANPTypeface* tf) {
49     SkSafeRef(tf);
50 }
51 
anp_unref(ANPTypeface * tf)52 static void anp_unref(ANPTypeface* tf) {
53     SkSafeUnref(tf);
54 }
55 
anp_getStyle(const ANPTypeface * tf)56 static ANPTypefaceStyle anp_getStyle(const ANPTypeface* tf) {
57     SkTypeface::Style s = tf ? tf->style() : SkTypeface::kNormal;
58     return static_cast<ANPTypefaceStyle>(s);
59 }
60 
anp_getFontPath(const ANPTypeface * tf,char fileName[],int32_t length,int32_t * index)61 static int32_t anp_getFontPath(const ANPTypeface* tf, char fileName[],
62                                int32_t length, int32_t* index) {
63     return 0;
64     /*
65     SkStream* stream = tf->openStream(index);
66     if (stream->getFileName()) {
67       strcpy(fileName, stream->getFileName());
68     } else {
69       return 0;
70     }
71 
72     return strlen(fileName);
73     */
74 }
75 
76 static const char* gFontDir;
77 #define FONT_DIR_SUFFIX     "/fonts/"
78 
anp_getFontDirectoryPath()79 static const char* anp_getFontDirectoryPath() {
80     if (NULL == gFontDir) {
81         const char* root = getenv("ANDROID_ROOT");
82         size_t len = strlen(root);
83         char* storage = (char*)malloc(len + sizeof(FONT_DIR_SUFFIX));
84         if (NULL == storage) {
85             return NULL;
86         }
87         memcpy(storage, root, len);
88         memcpy(storage + len, FONT_DIR_SUFFIX, sizeof(FONT_DIR_SUFFIX));
89         // save this assignment for last, so that if multiple threads call us
90         // (which should never happen), we never return an incomplete global.
91         // At worst, we would allocate storage for the path twice.
92         gFontDir = storage;
93     }
94     return gFontDir;
95 }
96 
97 ///////////////////////////////////////////////////////////////////////////////
98 
99 #define ASSIGN(obj, name)   (obj)->name = anp_##name
100 
InitTypeFaceInterface(ANPTypefaceInterfaceV0 * i)101 void InitTypeFaceInterface(ANPTypefaceInterfaceV0* i) {
102     ASSIGN(i, createFromName);
103     ASSIGN(i, createFromTypeface);
104     ASSIGN(i, getRefCount);
105     ASSIGN(i, ref);
106     ASSIGN(i, unref);
107     ASSIGN(i, getStyle);
108     ASSIGN(i, getFontPath);
109     ASSIGN(i, getFontDirectoryPath);
110 }
111