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 "config.h"
28#include "SkANP.h"
29#include "SkFontHost.h"
30
31static 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}
36
37static 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}
43
44static int32_t anp_getRefCount(const ANPTypeface* tf) {
45    return tf ? tf->getRefCnt() : 0;
46}
47
48static void anp_ref(ANPTypeface* tf) {
49    SkSafeRef(tf);
50}
51
52static void anp_unref(ANPTypeface* tf) {
53    SkSafeUnref(tf);
54}
55
56static ANPTypefaceStyle anp_getStyle(const ANPTypeface* tf) {
57    SkTypeface::Style s = tf ? tf->style() : SkTypeface::kNormal;
58    return static_cast<ANPTypefaceStyle>(s);
59}
60
61static int32_t anp_getFontPath(const ANPTypeface* tf, char fileName[],
62                               int32_t length, int32_t* index) {
63    size_t size = SkFontHost::GetFileName(SkTypeface::UniqueID(tf), fileName,
64                                          length, index);
65    return static_cast<int32_t>(size);
66}
67
68static const char* gFontDir;
69#define FONT_DIR_SUFFIX     "/fonts/"
70
71static const char* anp_getFontDirectoryPath() {
72    if (NULL == gFontDir) {
73        const char* root = getenv("ANDROID_ROOT");
74        size_t len = strlen(root);
75        char* storage = (char*)malloc(len + sizeof(FONT_DIR_SUFFIX));
76        if (NULL == storage) {
77            return NULL;
78        }
79        memcpy(storage, root, len);
80        memcpy(storage + len, FONT_DIR_SUFFIX, sizeof(FONT_DIR_SUFFIX));
81        // save this assignment for last, so that if multiple threads call us
82        // (which should never happen), we never return an incomplete global.
83        // At worst, we would allocate storage for the path twice.
84        gFontDir = storage;
85    }
86    return gFontDir;
87}
88
89///////////////////////////////////////////////////////////////////////////////
90
91#define ASSIGN(obj, name)   (obj)->name = anp_##name
92
93void ANPTypefaceInterfaceV0_Init(ANPInterface* v) {
94    ANPTypefaceInterfaceV0* i = reinterpret_cast<ANPTypefaceInterfaceV0*>(v);
95
96    ASSIGN(i, createFromName);
97    ASSIGN(i, createFromTypeface);
98    ASSIGN(i, getRefCount);
99    ASSIGN(i, ref);
100    ASSIGN(i, unref);
101    ASSIGN(i, getStyle);
102    ASSIGN(i, getFontPath);
103    ASSIGN(i, getFontDirectoryPath);
104}
105