1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/*
* Copyright 2009, The Android Open Source Project
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "EmojiFactory.h"
#include "EmojiFont.h"
#include "SkCanvas.h"
#include "SkImageDecoder.h"
#include "SkPaint.h"
#include "SkTSearch.h"
#include "SkUtils.h"
#include "gmoji_pua_table.h"
namespace android {
// lazily allocate the factory
static EmojiFactory* get_emoji_factory() {
static EmojiFactory* gEmojiFactory;
if (NULL == gEmojiFactory) {
gEmojiFactory = EmojiFactory::GetAvailableImplementation();
// we may still be NULL, if there is no impl.
}
return gEmojiFactory;
}
#define UNINITIALIZED_ENCODE_SIZE 0 // our array is initialzed with 0s
#define NOT_AVAILABLE_ENCODE_SIZE -1 // never a legal length for data
struct EncodeDataRec {
SkBitmap* fBitmap;
const void* fData;
int fSize;
};
static EncodeDataRec gGmojiEncodeData[GMOJI_PUA_COUNT] = {};
/* Given a local index, return (initialized if needed) a rec containing the
encoded data and length. The bitmap field is initialized to 0, and is not
filled in by this routine per-se.
*/
static EncodeDataRec* get_encoderec(int index) {
if ((unsigned)index >= GMOJI_PUA_COUNT) {
SkDebugf("bad index passed to EncodeDataRec& get_encode_data %d\n",
index);
return NULL;
}
// lazily fill in the data
EncodeDataRec* rec = &gGmojiEncodeData[index];
if (NOT_AVAILABLE_ENCODE_SIZE == rec->fSize) {
return NULL;
}
if (UNINITIALIZED_ENCODE_SIZE == rec->fSize) {
EmojiFactory* fact = get_emoji_factory();
if (NULL == fact) {
return NULL;
}
int32_t pua = GMOJI_PUA_MIN + gGmojiPUA[index];
rec->fData = fact->GetImageBinaryFromAndroidPua(pua, &rec->fSize);
if (NULL == rec->fData) {
// flag this entry is not available, so we won't ask again
rec->fSize = NOT_AVAILABLE_ENCODE_SIZE;
return NULL;
}
}
return rec;
}
/* Return the bitmap associated with the local index, or NULL if none is
available. Note that this will try to cache the bitmap the first time it
creates it.
*/
static const SkBitmap* get_bitmap(int index) {
EncodeDataRec* rec = get_encoderec(index);
SkBitmap* bitmap = NULL;
if (rec) {
bitmap = rec->fBitmap;
if (NULL == bitmap) {
bitmap = new SkBitmap;
if (!SkImageDecoder::DecodeMemory(rec->fData, rec->fSize, bitmap)) {
delete bitmap;
// we failed, so mark us to not try again
rec->fSize = NOT_AVAILABLE_ENCODE_SIZE;
return NULL;
}
// cache the answer
rec->fBitmap = bitmap;
// todo: we never know if/when to let go of this cached bitmap
// tho, since the pixels are managed separately, and are purged,
// the "leak" may not be too important
}
}
return bitmap;
}
///////////////////////////////////////////////////////////////////////////////
bool EmojiFont::IsAvailable() {
return get_emoji_factory() != NULL;
}
uint16_t EmojiFont::UnicharToGlyph(int32_t unichar) {
// do a quick range check before calling the search routine
if (unichar >= GMOJI_PUA_MIN && unichar <= GMOJI_PUA_MAX) {
// our table is stored relative to GMOJI_PUA_MIN to save space (16bits)
uint16_t relative = unichar - GMOJI_PUA_MIN;
int index = SkTSearch<uint16_t>(gGmojiPUA, GMOJI_PUA_COUNT, relative,
sizeof(uint16_t));
// a negative value means it was not found
if (index >= 0) {
return index + kGlyphBase;
}
// fall through to return 0
}
// not a supported emoji pua
return 0;
}
SkScalar EmojiFont::GetAdvanceWidth(uint16_t glyphID) {
if (glyphID < kGlyphBase) {
SkDebugf("-------- bad glyph passed to EmojiFont::GetAdvanceWidth %d\n",
glyphID);
return 0;
}
const SkBitmap* bitmap = get_bitmap(glyphID - kGlyphBase);
if (NULL == bitmap) {
return 0;
}
// add one for 1-pixel right-side-bearing
return SkIntToScalar(bitmap->width() + 1);
}
void EmojiFont::Draw(SkCanvas* canvas, uint16_t glyphID,
SkScalar x, SkScalar y, const SkPaint* paint) {
if (glyphID < kGlyphBase) {
SkDebugf("-------- bad glyph passed to EmojiFont::Draw %d\n", glyphID);
}
const SkBitmap* bitmap = get_bitmap(glyphID - kGlyphBase);
if (bitmap) {
canvas->drawBitmap(*bitmap, x, y - SkIntToScalar(bitmap->height()),
paint);
}
}
}
|