gfx/skia/trunk/src/utils/ios/SkFontHost_iOS.mm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #import <UIKit/UIKit.h>
michael@0 2
michael@0 3 #include "SkStream_NSData.h"
michael@0 4 #include "SkTypeface.h"
michael@0 5 #include "SkFontHost.h"
michael@0 6 #include "SkThread.h"
michael@0 7 #include "SkTemplates.h"
michael@0 8
michael@0 9 enum FontDesign {
michael@0 10 kUnknown_Design,
michael@0 11 kSans_FontDesign,
michael@0 12 kSerif_FontDesign,
michael@0 13
michael@0 14 kIllegal_FontDesign, // never use with a real font
michael@0 15 };
michael@0 16
michael@0 17 // returns kIllegal_FontDesign if not found
michael@0 18 static FontDesign find_design_from_name(const char name[]) {
michael@0 19 static const struct {
michael@0 20 const char* fName;
michael@0 21 FontDesign fDesign;
michael@0 22 } gRec[] = {
michael@0 23 { "sans-serif", kSans_FontDesign },
michael@0 24 { "serif", kSerif_FontDesign },
michael@0 25 };
michael@0 26
michael@0 27 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
michael@0 28 if (!strcasecmp(name, gRec[i].fName)) {
michael@0 29 return gRec[i].fDesign;
michael@0 30 }
michael@0 31 }
michael@0 32 return kIllegal_FontDesign;
michael@0 33 }
michael@0 34
michael@0 35 struct FontRes {
michael@0 36 const char* fName;
michael@0 37 SkTypeface::Style fStyle;
michael@0 38 FontDesign fDesign;
michael@0 39 };
michael@0 40
michael@0 41 static const FontRes gFontRes[] = {
michael@0 42 { "DroidSans", SkTypeface::kNormal, kSans_FontDesign },
michael@0 43 { "DroidSans", SkTypeface::kBold, kSans_FontDesign },
michael@0 44 { "DroidSerif-Regular", SkTypeface::kNormal, kSerif_FontDesign },
michael@0 45 { "DroidSerif-Bold", SkTypeface::kBold, kSerif_FontDesign },
michael@0 46 // { "PescaderoPro", SkTypeface::kNormal, kSerif_FontDesign },
michael@0 47 // { "PescaderoPro-Bold", SkTypeface::kBold, kSerif_FontDesign },
michael@0 48 };
michael@0 49 #define FONTRES_COUNT SK_ARRAY_COUNT(gFontRes)
michael@0 50
michael@0 51 #define DEFAULT_INDEX_REGULAR 1
michael@0 52 #define DEFAULT_INDEX_BOLD 2
michael@0 53
michael@0 54 ///////////////////////////////////////////////////////////////////////////////
michael@0 55
michael@0 56 class SkTypeface_Stream : public SkTypeface {
michael@0 57 public:
michael@0 58 SkTypeface_Stream(SkStream* stream, Style style);
michael@0 59 virtual ~SkTypeface_Stream();
michael@0 60
michael@0 61 SkStream* refStream() {
michael@0 62 fStream->ref();
michael@0 63 return fStream;
michael@0 64 }
michael@0 65
michael@0 66 private:
michael@0 67 SkStream* fStream;
michael@0 68 };
michael@0 69
michael@0 70 static int32_t gUniqueFontID;
michael@0 71
michael@0 72 SkTypeface_Stream::SkTypeface_Stream(SkStream* stream, Style style)
michael@0 73 : SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1) {
michael@0 74 fStream = stream;
michael@0 75 fStream->ref();
michael@0 76 }
michael@0 77
michael@0 78 SkTypeface_Stream::~SkTypeface_Stream() {
michael@0 79 fStream->unref();
michael@0 80 }
michael@0 81
michael@0 82 static SkTypeface_Stream* create_from_fontres(const FontRes& res) {
michael@0 83 SkStream* stream = SkStream_NSData::CreateFromResource(res.fName, "ttf");
michael@0 84 SkAutoUnref aur(stream);
michael@0 85
michael@0 86 return SkNEW_ARGS(SkTypeface_Stream, (stream, res.fStyle));
michael@0 87 }
michael@0 88
michael@0 89 ///////////////////////////////////////////////////////////////////////////////
michael@0 90
michael@0 91 static int compute_style_distance(SkTypeface::Style a, SkTypeface::Style b) {
michael@0 92 int dist = 0;
michael@0 93 int diff = a ^ b;
michael@0 94 if (diff & SkTypeface::kBold) {
michael@0 95 dist += 2;
michael@0 96 }
michael@0 97 if (diff & SkTypeface::kItalic) {
michael@0 98 dist += 1;
michael@0 99 }
michael@0 100 return dist;
michael@0 101 }
michael@0 102
michael@0 103 static SkTypeface_Stream* gFonts[FONTRES_COUNT];
michael@0 104
michael@0 105 static void assure_init_fonts() {
michael@0 106 static bool gOnce;
michael@0 107 if (!gOnce) {
michael@0 108 for (size_t i = 0; i < FONTRES_COUNT; i++) {
michael@0 109 gFonts[i] = create_from_fontres(gFontRes[i]);
michael@0 110 gOnce = true;
michael@0 111 }
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 static SkTypeface_Stream* get_default_font(SkTypeface::Style style) {
michael@0 116 assure_init_fonts();
michael@0 117
michael@0 118 if (style & SkTypeface::kBold) {
michael@0 119 return gFonts[DEFAULT_INDEX_BOLD];
michael@0 120 } else {
michael@0 121 return gFonts[DEFAULT_INDEX_REGULAR];
michael@0 122 }
michael@0 123 }
michael@0 124
michael@0 125 static SkTypeface_Stream* find_by_id(SkFontID fontID) {
michael@0 126 assure_init_fonts();
michael@0 127
michael@0 128 for (size_t i = 0; i < FONTRES_COUNT; i++) {
michael@0 129 if (gFonts[i]->uniqueID() == fontID) {
michael@0 130 return gFonts[i];
michael@0 131 }
michael@0 132 }
michael@0 133 return NULL;
michael@0 134 }
michael@0 135
michael@0 136 ///////////////////////////////////////////////////////////////////////////////
michael@0 137
michael@0 138 template <typename T> T* ref_and_return(T* obj) {
michael@0 139 obj->ref();
michael@0 140 return obj;
michael@0 141 }
michael@0 142
michael@0 143 SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
michael@0 144 const char familyName[],
michael@0 145 const void* data, size_t bytelength,
michael@0 146 SkTypeface::Style style) {
michael@0 147 assure_init_fonts();
michael@0 148
michael@0 149 if (familyName) {
michael@0 150 FontDesign design = find_design_from_name(familyName);
michael@0 151 if (kIllegal_FontDesign != design) {
michael@0 152 familyName = "$#@*&%*#$@ never match any name";
michael@0 153 }
michael@0 154
michael@0 155 int bestDistance = 999;
michael@0 156 int bestIndex = -1;
michael@0 157 for (size_t i = 0; i < FONTRES_COUNT; i++) {
michael@0 158 if (design == gFontRes[i].fDesign || !strcmp(gFontRes[i].fName, familyName)) {
michael@0 159 int dist = compute_style_distance(style, gFontRes[i].fStyle);
michael@0 160 if (dist < bestDistance) {
michael@0 161 bestDistance = dist;
michael@0 162 bestIndex = i;
michael@0 163 }
michael@0 164 }
michael@0 165 }
michael@0 166 if (bestIndex >= 0) {
michael@0 167 return ref_and_return(gFonts[bestIndex]);
michael@0 168 }
michael@0 169 }
michael@0 170
michael@0 171 return ref_and_return(get_default_font(style));
michael@0 172 }
michael@0 173
michael@0 174 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
michael@0 175 SkDEBUGFAIL("SkFontHost::CreateTypeface unimplemented");
michael@0 176 return NULL;
michael@0 177 }
michael@0 178
michael@0 179 SkTypeface* SkFontHost::CreateTypefaceFromFile(char const*) {
michael@0 180 // SkDEBUGFAIL("SkFontHost::CreateTypefaceFromFile unimplemented");
michael@0 181 return NULL;
michael@0 182 }
michael@0 183
michael@0 184 ///////////////////////////////////////////////////////////////////////////////
michael@0 185
michael@0 186 SkStream* SkFontHost::OpenStream(uint32_t uniqueID) {
michael@0 187 SkTypeface_Stream* tf = find_by_id(uniqueID);
michael@0 188 SkASSERT(tf);
michael@0 189 return tf->refStream();
michael@0 190 }
michael@0 191
michael@0 192 size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length,
michael@0 193 int32_t* index) {
michael@0 194 SkDebugf("SkFontHost::GetFileName unimplemented\n");
michael@0 195 return 0;
michael@0 196 }
michael@0 197
michael@0 198 ///////////////////////////////////////////////////////////////////////////////
michael@0 199
michael@0 200 void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
michael@0 201 SkDEBUGFAIL("SkFontHost::Serialize unimplemented");
michael@0 202 }
michael@0 203
michael@0 204 SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
michael@0 205 int style = stream->readU8();
michael@0 206 int len = stream->readPackedUInt();
michael@0 207 const char* name = NULL;
michael@0 208 if (len > 0) {
michael@0 209 SkString str;
michael@0 210 str.resize(len);
michael@0 211 stream->read(str.writable_str(), len);
michael@0 212
michael@0 213 if (str.startsWith("DroidSans")) {
michael@0 214 name = "sans-serif";
michael@0 215 } else if (str.startsWith("DroidSerif")) {
michael@0 216 name = "serif";
michael@0 217 }
michael@0 218 SkDebugf("---- deserialize typeface <%s> %d %s\n", str.c_str(), style, name);
michael@0 219 }
michael@0 220 // name = NULL; style = 0;
michael@0 221 return SkFontHost::CreateTypeface(NULL, name, NULL, NULL,
michael@0 222 (SkTypeface::Style)style);
michael@0 223 }
michael@0 224
michael@0 225 SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) {
michael@0 226 return 0;
michael@0 227 }
michael@0 228
michael@0 229 #define FONT_CACHE_MEMORY_BUDGET 1 * 1024 * 1024
michael@0 230
michael@0 231 size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) {
michael@0 232 if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET)
michael@0 233 return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET;
michael@0 234 else
michael@0 235 return 0; // nothing to do
michael@0 236 }
michael@0 237
michael@0 238 ///////////////////////////////////////////////////////////////////////////////
michael@0 239 int SkFontHost::ComputeGammaFlag(const SkPaint& paint) {
michael@0 240 return 0;
michael@0 241 }
michael@0 242
michael@0 243 void SkFontHost::GetGammaTables(const uint8_t* tables[2]) {
michael@0 244 tables[0] = NULL; // black gamma (e.g. exp=1.4)
michael@0 245 tables[1] = NULL; // white gamma (e.g. exp= 1/1.4)
michael@0 246 }
michael@0 247
michael@0 248 // static
michael@0 249 SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
michael@0 250 uint32_t fontID,
michael@0 251 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo) {
michael@0 252 SkDEBUGFAIL("SkFontHost::GetAdvancedTypefaceMetrics unimplemented");
michael@0 253 return NULL;
michael@0 254 }
michael@0 255
michael@0 256 void SkFontHost::FilterRec(SkScalerContext::Rec* rec, SkTypeface*) {
michael@0 257 }
michael@0 258
michael@0 259 SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
michael@0 260 SkDEBUGFAIL("SkFontHost::CreateScalarContext unimplemented");
michael@0 261 return NULL;
michael@0 262 }

mercurial