Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | From 0d730a94e9f6676d5cde45f955fe025a4549817e Mon Sep 17 00:00:00 2001 |
michael@0 | 2 | From: George Wright <gw@gwright.org.uk> |
michael@0 | 3 | Date: Thu, 23 Aug 2012 16:45:38 -0400 |
michael@0 | 4 | Subject: [PATCH 4/9] Bug 777614 - Re-apply bug 719872 - Fix crash on Android |
michael@0 | 5 | by reverting to older FontHost r=nrc |
michael@0 | 6 | |
michael@0 | 7 | --- |
michael@0 | 8 | gfx/skia/src/ports/SkFontHost_android_old.cpp | 664 ++++++++++++++++++++++++++ |
michael@0 | 9 | 1 file changed, 664 insertions(+) |
michael@0 | 10 | create mode 100644 gfx/skia/src/ports/SkFontHost_android_old.cpp |
michael@0 | 11 | |
michael@0 | 12 | diff --git a/gfx/skia/src/ports/SkFontHost_android_old.cpp b/gfx/skia/src/ports/SkFontHost_android_old.cpp |
michael@0 | 13 | new file mode 100644 |
michael@0 | 14 | index 0000000..b5c4f3c |
michael@0 | 15 | --- /dev/null |
michael@0 | 16 | +++ b/gfx/skia/src/ports/SkFontHost_android_old.cpp |
michael@0 | 17 | @@ -0,0 +1,664 @@ |
michael@0 | 18 | + |
michael@0 | 19 | +/* |
michael@0 | 20 | + * Copyright 2006 The Android Open Source Project |
michael@0 | 21 | + * |
michael@0 | 22 | + * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 23 | + * found in the LICENSE file. |
michael@0 | 24 | + */ |
michael@0 | 25 | + |
michael@0 | 26 | + |
michael@0 | 27 | +#include "SkFontHost.h" |
michael@0 | 28 | +#include "SkDescriptor.h" |
michael@0 | 29 | +#include "SkMMapStream.h" |
michael@0 | 30 | +#include "SkPaint.h" |
michael@0 | 31 | +#include "SkString.h" |
michael@0 | 32 | +#include "SkStream.h" |
michael@0 | 33 | +#include "SkThread.h" |
michael@0 | 34 | +#include "SkTSearch.h" |
michael@0 | 35 | +#include <stdio.h> |
michael@0 | 36 | + |
michael@0 | 37 | +#define FONT_CACHE_MEMORY_BUDGET (768 * 1024) |
michael@0 | 38 | + |
michael@0 | 39 | +#ifndef SK_FONT_FILE_PREFIX |
michael@0 | 40 | + #define SK_FONT_FILE_PREFIX "/fonts/" |
michael@0 | 41 | +#endif |
michael@0 | 42 | + |
michael@0 | 43 | +bool find_name_and_attributes(SkStream* stream, SkString* name, SkTypeface::Style* style, |
michael@0 | 44 | + bool* isFixedWidth); |
michael@0 | 45 | + |
michael@0 | 46 | +static void GetFullPathForSysFonts(SkString* full, const char name[]) { |
michael@0 | 47 | + full->set(getenv("ANDROID_ROOT")); |
michael@0 | 48 | + full->append(SK_FONT_FILE_PREFIX); |
michael@0 | 49 | + full->append(name); |
michael@0 | 50 | +} |
michael@0 | 51 | + |
michael@0 | 52 | +/////////////////////////////////////////////////////////////////////////////// |
michael@0 | 53 | + |
michael@0 | 54 | +struct FamilyRec; |
michael@0 | 55 | + |
michael@0 | 56 | +/* This guy holds a mapping of a name -> family, used for looking up fonts. |
michael@0 | 57 | + Since it is stored in a stretchy array that doesn't preserve object |
michael@0 | 58 | + semantics, we don't use constructor/destructors, but just have explicit |
michael@0 | 59 | + helpers to manage our internal bookkeeping. |
michael@0 | 60 | +*/ |
michael@0 | 61 | +struct NameFamilyPair { |
michael@0 | 62 | + const char* fName; // we own this |
michael@0 | 63 | + FamilyRec* fFamily; // we don't own this, we just reference it |
michael@0 | 64 | + |
michael@0 | 65 | + void construct(const char name[], FamilyRec* family) { |
michael@0 | 66 | + fName = strdup(name); |
michael@0 | 67 | + fFamily = family; // we don't own this, so just record the referene |
michael@0 | 68 | + } |
michael@0 | 69 | + |
michael@0 | 70 | + void destruct() { |
michael@0 | 71 | + free((char*)fName); |
michael@0 | 72 | + // we don't own family, so just ignore our reference |
michael@0 | 73 | + } |
michael@0 | 74 | +}; |
michael@0 | 75 | + |
michael@0 | 76 | +// we use atomic_inc to grow this for each typeface we create |
michael@0 | 77 | +static int32_t gUniqueFontID; |
michael@0 | 78 | + |
michael@0 | 79 | +// this is the mutex that protects these globals |
michael@0 | 80 | +static SkMutex gFamilyMutex; |
michael@0 | 81 | +static FamilyRec* gFamilyHead; |
michael@0 | 82 | +static SkTDArray<NameFamilyPair> gNameList; |
michael@0 | 83 | + |
michael@0 | 84 | +struct FamilyRec { |
michael@0 | 85 | + FamilyRec* fNext; |
michael@0 | 86 | + SkTypeface* fFaces[4]; |
michael@0 | 87 | + |
michael@0 | 88 | + FamilyRec() |
michael@0 | 89 | + { |
michael@0 | 90 | + fNext = gFamilyHead; |
michael@0 | 91 | + memset(fFaces, 0, sizeof(fFaces)); |
michael@0 | 92 | + gFamilyHead = this; |
michael@0 | 93 | + } |
michael@0 | 94 | +}; |
michael@0 | 95 | + |
michael@0 | 96 | +static SkTypeface* find_best_face(const FamilyRec* family, |
michael@0 | 97 | + SkTypeface::Style style) { |
michael@0 | 98 | + SkTypeface* const* faces = family->fFaces; |
michael@0 | 99 | + |
michael@0 | 100 | + if (faces[style] != NULL) { // exact match |
michael@0 | 101 | + return faces[style]; |
michael@0 | 102 | + } |
michael@0 | 103 | + // look for a matching bold |
michael@0 | 104 | + style = (SkTypeface::Style)(style ^ SkTypeface::kItalic); |
michael@0 | 105 | + if (faces[style] != NULL) { |
michael@0 | 106 | + return faces[style]; |
michael@0 | 107 | + } |
michael@0 | 108 | + // look for the plain |
michael@0 | 109 | + if (faces[SkTypeface::kNormal] != NULL) { |
michael@0 | 110 | + return faces[SkTypeface::kNormal]; |
michael@0 | 111 | + } |
michael@0 | 112 | + // look for anything |
michael@0 | 113 | + for (int i = 0; i < 4; i++) { |
michael@0 | 114 | + if (faces[i] != NULL) { |
michael@0 | 115 | + return faces[i]; |
michael@0 | 116 | + } |
michael@0 | 117 | + } |
michael@0 | 118 | + // should never get here, since the faces list should not be empty |
michael@0 | 119 | + SkASSERT(!"faces list is empty"); |
michael@0 | 120 | + return NULL; |
michael@0 | 121 | +} |
michael@0 | 122 | + |
michael@0 | 123 | +static FamilyRec* find_family(const SkTypeface* member) { |
michael@0 | 124 | + FamilyRec* curr = gFamilyHead; |
michael@0 | 125 | + while (curr != NULL) { |
michael@0 | 126 | + for (int i = 0; i < 4; i++) { |
michael@0 | 127 | + if (curr->fFaces[i] == member) { |
michael@0 | 128 | + return curr; |
michael@0 | 129 | + } |
michael@0 | 130 | + } |
michael@0 | 131 | + curr = curr->fNext; |
michael@0 | 132 | + } |
michael@0 | 133 | + return NULL; |
michael@0 | 134 | +} |
michael@0 | 135 | + |
michael@0 | 136 | +/* Returns the matching typeface, or NULL. If a typeface is found, its refcnt |
michael@0 | 137 | + is not modified. |
michael@0 | 138 | + */ |
michael@0 | 139 | +static SkTypeface* find_from_uniqueID(uint32_t uniqueID) { |
michael@0 | 140 | + FamilyRec* curr = gFamilyHead; |
michael@0 | 141 | + while (curr != NULL) { |
michael@0 | 142 | + for (int i = 0; i < 4; i++) { |
michael@0 | 143 | + SkTypeface* face = curr->fFaces[i]; |
michael@0 | 144 | + if (face != NULL && face->uniqueID() == uniqueID) { |
michael@0 | 145 | + return face; |
michael@0 | 146 | + } |
michael@0 | 147 | + } |
michael@0 | 148 | + curr = curr->fNext; |
michael@0 | 149 | + } |
michael@0 | 150 | + return NULL; |
michael@0 | 151 | +} |
michael@0 | 152 | + |
michael@0 | 153 | +/* Remove reference to this face from its family. If the resulting family |
michael@0 | 154 | + is empty (has no faces), return that family, otherwise return NULL |
michael@0 | 155 | +*/ |
michael@0 | 156 | +static FamilyRec* remove_from_family(const SkTypeface* face) { |
michael@0 | 157 | + FamilyRec* family = find_family(face); |
michael@0 | 158 | + SkASSERT(family->fFaces[face->style()] == face); |
michael@0 | 159 | + family->fFaces[face->style()] = NULL; |
michael@0 | 160 | + |
michael@0 | 161 | + for (int i = 0; i < 4; i++) { |
michael@0 | 162 | + if (family->fFaces[i] != NULL) { // family is non-empty |
michael@0 | 163 | + return NULL; |
michael@0 | 164 | + } |
michael@0 | 165 | + } |
michael@0 | 166 | + return family; // return the empty family |
michael@0 | 167 | +} |
michael@0 | 168 | + |
michael@0 | 169 | +// maybe we should make FamilyRec be doubly-linked |
michael@0 | 170 | +static void detach_and_delete_family(FamilyRec* family) { |
michael@0 | 171 | + FamilyRec* curr = gFamilyHead; |
michael@0 | 172 | + FamilyRec* prev = NULL; |
michael@0 | 173 | + |
michael@0 | 174 | + while (curr != NULL) { |
michael@0 | 175 | + FamilyRec* next = curr->fNext; |
michael@0 | 176 | + if (curr == family) { |
michael@0 | 177 | + if (prev == NULL) { |
michael@0 | 178 | + gFamilyHead = next; |
michael@0 | 179 | + } else { |
michael@0 | 180 | + prev->fNext = next; |
michael@0 | 181 | + } |
michael@0 | 182 | + SkDELETE(family); |
michael@0 | 183 | + return; |
michael@0 | 184 | + } |
michael@0 | 185 | + prev = curr; |
michael@0 | 186 | + curr = next; |
michael@0 | 187 | + } |
michael@0 | 188 | + SkASSERT(!"Yikes, couldn't find family in our list to remove/delete"); |
michael@0 | 189 | +} |
michael@0 | 190 | + |
michael@0 | 191 | +static SkTypeface* find_typeface(const char name[], SkTypeface::Style style) { |
michael@0 | 192 | + NameFamilyPair* list = gNameList.begin(); |
michael@0 | 193 | + int count = gNameList.count(); |
michael@0 | 194 | + |
michael@0 | 195 | + int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0])); |
michael@0 | 196 | + |
michael@0 | 197 | + if (index >= 0) { |
michael@0 | 198 | + return find_best_face(list[index].fFamily, style); |
michael@0 | 199 | + } |
michael@0 | 200 | + return NULL; |
michael@0 | 201 | +} |
michael@0 | 202 | + |
michael@0 | 203 | +static SkTypeface* find_typeface(const SkTypeface* familyMember, |
michael@0 | 204 | + SkTypeface::Style style) { |
michael@0 | 205 | + const FamilyRec* family = find_family(familyMember); |
michael@0 | 206 | + return family ? find_best_face(family, style) : NULL; |
michael@0 | 207 | +} |
michael@0 | 208 | + |
michael@0 | 209 | +static void add_name(const char name[], FamilyRec* family) { |
michael@0 | 210 | + SkAutoAsciiToLC tolc(name); |
michael@0 | 211 | + name = tolc.lc(); |
michael@0 | 212 | + |
michael@0 | 213 | + NameFamilyPair* list = gNameList.begin(); |
michael@0 | 214 | + int count = gNameList.count(); |
michael@0 | 215 | + |
michael@0 | 216 | + int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0])); |
michael@0 | 217 | + |
michael@0 | 218 | + if (index < 0) { |
michael@0 | 219 | + list = gNameList.insert(~index); |
michael@0 | 220 | + list->construct(name, family); |
michael@0 | 221 | + } |
michael@0 | 222 | +} |
michael@0 | 223 | + |
michael@0 | 224 | +static void remove_from_names(FamilyRec* emptyFamily) |
michael@0 | 225 | +{ |
michael@0 | 226 | +#ifdef SK_DEBUG |
michael@0 | 227 | + for (int i = 0; i < 4; i++) { |
michael@0 | 228 | + SkASSERT(emptyFamily->fFaces[i] == NULL); |
michael@0 | 229 | + } |
michael@0 | 230 | +#endif |
michael@0 | 231 | + |
michael@0 | 232 | + SkTDArray<NameFamilyPair>& list = gNameList; |
michael@0 | 233 | + |
michael@0 | 234 | + // must go backwards when removing |
michael@0 | 235 | + for (int i = list.count() - 1; i >= 0; --i) { |
michael@0 | 236 | + NameFamilyPair* pair = &list[i]; |
michael@0 | 237 | + if (pair->fFamily == emptyFamily) { |
michael@0 | 238 | + pair->destruct(); |
michael@0 | 239 | + list.remove(i); |
michael@0 | 240 | + } |
michael@0 | 241 | + } |
michael@0 | 242 | +} |
michael@0 | 243 | + |
michael@0 | 244 | +/////////////////////////////////////////////////////////////////////////////// |
michael@0 | 245 | + |
michael@0 | 246 | +class FamilyTypeface : public SkTypeface { |
michael@0 | 247 | +public: |
michael@0 | 248 | + FamilyTypeface(Style style, bool sysFont, SkTypeface* familyMember, |
michael@0 | 249 | + bool isFixedWidth) |
michael@0 | 250 | + : SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1, isFixedWidth) { |
michael@0 | 251 | + fIsSysFont = sysFont; |
michael@0 | 252 | + |
michael@0 | 253 | + SkAutoMutexAcquire ac(gFamilyMutex); |
michael@0 | 254 | + |
michael@0 | 255 | + FamilyRec* rec = NULL; |
michael@0 | 256 | + if (familyMember) { |
michael@0 | 257 | + rec = find_family(familyMember); |
michael@0 | 258 | + SkASSERT(rec); |
michael@0 | 259 | + } else { |
michael@0 | 260 | + rec = SkNEW(FamilyRec); |
michael@0 | 261 | + } |
michael@0 | 262 | + rec->fFaces[style] = this; |
michael@0 | 263 | + } |
michael@0 | 264 | + |
michael@0 | 265 | + virtual ~FamilyTypeface() { |
michael@0 | 266 | + SkAutoMutexAcquire ac(gFamilyMutex); |
michael@0 | 267 | + |
michael@0 | 268 | + // remove us from our family. If the family is now empty, we return |
michael@0 | 269 | + // that and then remove that family from the name list |
michael@0 | 270 | + FamilyRec* family = remove_from_family(this); |
michael@0 | 271 | + if (NULL != family) { |
michael@0 | 272 | + remove_from_names(family); |
michael@0 | 273 | + detach_and_delete_family(family); |
michael@0 | 274 | + } |
michael@0 | 275 | + } |
michael@0 | 276 | + |
michael@0 | 277 | + bool isSysFont() const { return fIsSysFont; } |
michael@0 | 278 | + |
michael@0 | 279 | + virtual SkStream* openStream() = 0; |
michael@0 | 280 | + virtual const char* getUniqueString() const = 0; |
michael@0 | 281 | + virtual const char* getFilePath() const = 0; |
michael@0 | 282 | + |
michael@0 | 283 | +private: |
michael@0 | 284 | + bool fIsSysFont; |
michael@0 | 285 | + |
michael@0 | 286 | + typedef SkTypeface INHERITED; |
michael@0 | 287 | +}; |
michael@0 | 288 | + |
michael@0 | 289 | +/////////////////////////////////////////////////////////////////////////////// |
michael@0 | 290 | + |
michael@0 | 291 | +class StreamTypeface : public FamilyTypeface { |
michael@0 | 292 | +public: |
michael@0 | 293 | + StreamTypeface(Style style, bool sysFont, SkTypeface* familyMember, |
michael@0 | 294 | + SkStream* stream, bool isFixedWidth) |
michael@0 | 295 | + : INHERITED(style, sysFont, familyMember, isFixedWidth) { |
michael@0 | 296 | + SkASSERT(stream); |
michael@0 | 297 | + stream->ref(); |
michael@0 | 298 | + fStream = stream; |
michael@0 | 299 | + } |
michael@0 | 300 | + virtual ~StreamTypeface() { |
michael@0 | 301 | + fStream->unref(); |
michael@0 | 302 | + } |
michael@0 | 303 | + |
michael@0 | 304 | + // overrides |
michael@0 | 305 | + virtual SkStream* openStream() { |
michael@0 | 306 | + // we just ref our existing stream, since the caller will call unref() |
michael@0 | 307 | + // when they are through |
michael@0 | 308 | + fStream->ref(); |
michael@0 | 309 | + // must rewind each time, since the caller assumes a "new" stream |
michael@0 | 310 | + fStream->rewind(); |
michael@0 | 311 | + return fStream; |
michael@0 | 312 | + } |
michael@0 | 313 | + virtual const char* getUniqueString() const { return NULL; } |
michael@0 | 314 | + virtual const char* getFilePath() const { return NULL; } |
michael@0 | 315 | + |
michael@0 | 316 | +private: |
michael@0 | 317 | + SkStream* fStream; |
michael@0 | 318 | + |
michael@0 | 319 | + typedef FamilyTypeface INHERITED; |
michael@0 | 320 | +}; |
michael@0 | 321 | + |
michael@0 | 322 | +class FileTypeface : public FamilyTypeface { |
michael@0 | 323 | +public: |
michael@0 | 324 | + FileTypeface(Style style, bool sysFont, SkTypeface* familyMember, |
michael@0 | 325 | + const char path[], bool isFixedWidth) |
michael@0 | 326 | + : INHERITED(style, sysFont, familyMember, isFixedWidth) { |
michael@0 | 327 | + SkString fullpath; |
michael@0 | 328 | + |
michael@0 | 329 | + if (sysFont) { |
michael@0 | 330 | + GetFullPathForSysFonts(&fullpath, path); |
michael@0 | 331 | + path = fullpath.c_str(); |
michael@0 | 332 | + } |
michael@0 | 333 | + fPath.set(path); |
michael@0 | 334 | + } |
michael@0 | 335 | + |
michael@0 | 336 | + // overrides |
michael@0 | 337 | + virtual SkStream* openStream() { |
michael@0 | 338 | + SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str())); |
michael@0 | 339 | + |
michael@0 | 340 | + // check for failure |
michael@0 | 341 | + if (stream->getLength() <= 0) { |
michael@0 | 342 | + SkDELETE(stream); |
michael@0 | 343 | + // maybe MMAP isn't supported. try FILE |
michael@0 | 344 | + stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str())); |
michael@0 | 345 | + if (stream->getLength() <= 0) { |
michael@0 | 346 | + SkDELETE(stream); |
michael@0 | 347 | + stream = NULL; |
michael@0 | 348 | + } |
michael@0 | 349 | + } |
michael@0 | 350 | + return stream; |
michael@0 | 351 | + } |
michael@0 | 352 | + virtual const char* getUniqueString() const { |
michael@0 | 353 | + const char* str = strrchr(fPath.c_str(), '/'); |
michael@0 | 354 | + if (str) { |
michael@0 | 355 | + str += 1; // skip the '/' |
michael@0 | 356 | + } |
michael@0 | 357 | + return str; |
michael@0 | 358 | + } |
michael@0 | 359 | + virtual const char* getFilePath() const { |
michael@0 | 360 | + return fPath.c_str(); |
michael@0 | 361 | + } |
michael@0 | 362 | + |
michael@0 | 363 | +private: |
michael@0 | 364 | + SkString fPath; |
michael@0 | 365 | + |
michael@0 | 366 | + typedef FamilyTypeface INHERITED; |
michael@0 | 367 | +}; |
michael@0 | 368 | + |
michael@0 | 369 | +/////////////////////////////////////////////////////////////////////////////// |
michael@0 | 370 | +/////////////////////////////////////////////////////////////////////////////// |
michael@0 | 371 | + |
michael@0 | 372 | +static bool get_name_and_style(const char path[], SkString* name, |
michael@0 | 373 | + SkTypeface::Style* style, |
michael@0 | 374 | + bool* isFixedWidth, bool isExpected) { |
michael@0 | 375 | + SkString fullpath; |
michael@0 | 376 | + GetFullPathForSysFonts(&fullpath, path); |
michael@0 | 377 | + |
michael@0 | 378 | + SkMMAPStream stream(fullpath.c_str()); |
michael@0 | 379 | + if (stream.getLength() > 0) { |
michael@0 | 380 | + find_name_and_attributes(&stream, name, style, isFixedWidth); |
michael@0 | 381 | + return true; |
michael@0 | 382 | + } |
michael@0 | 383 | + else { |
michael@0 | 384 | + SkFILEStream stream(fullpath.c_str()); |
michael@0 | 385 | + if (stream.getLength() > 0) { |
michael@0 | 386 | + find_name_and_attributes(&stream, name, style, isFixedWidth); |
michael@0 | 387 | + return true; |
michael@0 | 388 | + } |
michael@0 | 389 | + } |
michael@0 | 390 | + |
michael@0 | 391 | + if (isExpected) { |
michael@0 | 392 | + SkDebugf("---- failed to open <%s> as a font\n", fullpath.c_str()); |
michael@0 | 393 | + } |
michael@0 | 394 | + return false; |
michael@0 | 395 | +} |
michael@0 | 396 | + |
michael@0 | 397 | +// used to record our notion of the pre-existing fonts |
michael@0 | 398 | +struct FontInitRec { |
michael@0 | 399 | + const char* fFileName; |
michael@0 | 400 | + const char* const* fNames; // null-terminated list |
michael@0 | 401 | +}; |
michael@0 | 402 | + |
michael@0 | 403 | +static const char* gSansNames[] = { |
michael@0 | 404 | + "sans-serif", "arial", "helvetica", "tahoma", "verdana", NULL |
michael@0 | 405 | +}; |
michael@0 | 406 | + |
michael@0 | 407 | +static const char* gSerifNames[] = { |
michael@0 | 408 | + "serif", "times", "times new roman", "palatino", "georgia", "baskerville", |
michael@0 | 409 | + "goudy", "fantasy", "cursive", "ITC Stone Serif", NULL |
michael@0 | 410 | +}; |
michael@0 | 411 | + |
michael@0 | 412 | +static const char* gMonoNames[] = { |
michael@0 | 413 | + "monospace", "courier", "courier new", "monaco", NULL |
michael@0 | 414 | +}; |
michael@0 | 415 | + |
michael@0 | 416 | +// deliberately empty, but we use the address to identify fallback fonts |
michael@0 | 417 | +static const char* gFBNames[] = { NULL }; |
michael@0 | 418 | + |
michael@0 | 419 | +/* Fonts must be grouped by family, with the first font in a family having the |
michael@0 | 420 | + list of names (even if that list is empty), and the following members having |
michael@0 | 421 | + null for the list. The names list must be NULL-terminated |
michael@0 | 422 | +*/ |
michael@0 | 423 | +static const FontInitRec gSystemFonts[] = { |
michael@0 | 424 | + { "DroidSans.ttf", gSansNames }, |
michael@0 | 425 | + { "DroidSans-Bold.ttf", NULL }, |
michael@0 | 426 | + { "DroidSerif-Regular.ttf", gSerifNames }, |
michael@0 | 427 | + { "DroidSerif-Bold.ttf", NULL }, |
michael@0 | 428 | + { "DroidSerif-Italic.ttf", NULL }, |
michael@0 | 429 | + { "DroidSerif-BoldItalic.ttf", NULL }, |
michael@0 | 430 | + { "DroidSansMono.ttf", gMonoNames }, |
michael@0 | 431 | + /* These are optional, and can be ignored if not found in the file system. |
michael@0 | 432 | + These are appended to gFallbackFonts[] as they are seen, so we list |
michael@0 | 433 | + them in the order we want them to be accessed by NextLogicalFont(). |
michael@0 | 434 | + */ |
michael@0 | 435 | + { "DroidSansArabic.ttf", gFBNames }, |
michael@0 | 436 | + { "DroidSansHebrew.ttf", gFBNames }, |
michael@0 | 437 | + { "DroidSansThai.ttf", gFBNames }, |
michael@0 | 438 | + { "MTLmr3m.ttf", gFBNames }, // Motoya Japanese Font |
michael@0 | 439 | + { "MTLc3m.ttf", gFBNames }, // Motoya Japanese Font |
michael@0 | 440 | + { "DroidSansJapanese.ttf", gFBNames }, |
michael@0 | 441 | + { "DroidSansFallback.ttf", gFBNames } |
michael@0 | 442 | +}; |
michael@0 | 443 | + |
michael@0 | 444 | +#define DEFAULT_NAMES gSansNames |
michael@0 | 445 | + |
michael@0 | 446 | +// these globals are assigned (once) by load_system_fonts() |
michael@0 | 447 | +static FamilyRec* gDefaultFamily; |
michael@0 | 448 | +static SkTypeface* gDefaultNormal; |
michael@0 | 449 | + |
michael@0 | 450 | +/* This is sized conservatively, assuming that it will never be a size issue. |
michael@0 | 451 | + It will be initialized in load_system_fonts(), and will be filled with the |
michael@0 | 452 | + fontIDs that can be used for fallback consideration, in sorted order (sorted |
michael@0 | 453 | + meaning element[0] should be used first, then element[1], etc. When we hit |
michael@0 | 454 | + a fontID==0 in the array, the list is done, hence our allocation size is |
michael@0 | 455 | + +1 the total number of possible system fonts. Also see NextLogicalFont(). |
michael@0 | 456 | + */ |
michael@0 | 457 | +static uint32_t gFallbackFonts[SK_ARRAY_COUNT(gSystemFonts)+1]; |
michael@0 | 458 | + |
michael@0 | 459 | +/* Called once (ensured by the sentinel check at the beginning of our body). |
michael@0 | 460 | + Initializes all the globals, and register the system fonts. |
michael@0 | 461 | + */ |
michael@0 | 462 | +static void load_system_fonts() { |
michael@0 | 463 | + // check if we've already be called |
michael@0 | 464 | + if (NULL != gDefaultNormal) { |
michael@0 | 465 | + return; |
michael@0 | 466 | + } |
michael@0 | 467 | + |
michael@0 | 468 | + const FontInitRec* rec = gSystemFonts; |
michael@0 | 469 | + SkTypeface* firstInFamily = NULL; |
michael@0 | 470 | + int fallbackCount = 0; |
michael@0 | 471 | + |
michael@0 | 472 | + for (size_t i = 0; i < SK_ARRAY_COUNT(gSystemFonts); i++) { |
michael@0 | 473 | + // if we're the first in a new family, clear firstInFamily |
michael@0 | 474 | + if (rec[i].fNames != NULL) { |
michael@0 | 475 | + firstInFamily = NULL; |
michael@0 | 476 | + } |
michael@0 | 477 | + |
michael@0 | 478 | + bool isFixedWidth; |
michael@0 | 479 | + SkString name; |
michael@0 | 480 | + SkTypeface::Style style; |
michael@0 | 481 | + |
michael@0 | 482 | + // we expect all the fonts, except the "fallback" fonts |
michael@0 | 483 | + bool isExpected = (rec[i].fNames != gFBNames); |
michael@0 | 484 | + if (!get_name_and_style(rec[i].fFileName, &name, &style, |
michael@0 | 485 | + &isFixedWidth, isExpected)) { |
michael@0 | 486 | + continue; |
michael@0 | 487 | + } |
michael@0 | 488 | + |
michael@0 | 489 | + SkTypeface* tf = SkNEW_ARGS(FileTypeface, |
michael@0 | 490 | + (style, |
michael@0 | 491 | + true, // system-font (cannot delete) |
michael@0 | 492 | + firstInFamily, // what family to join |
michael@0 | 493 | + rec[i].fFileName, |
michael@0 | 494 | + isFixedWidth) // filename |
michael@0 | 495 | + ); |
michael@0 | 496 | + |
michael@0 | 497 | + if (rec[i].fNames != NULL) { |
michael@0 | 498 | + // see if this is one of our fallback fonts |
michael@0 | 499 | + if (rec[i].fNames == gFBNames) { |
michael@0 | 500 | + // SkDebugf("---- adding %s as fallback[%d] fontID %d\n", |
michael@0 | 501 | + // rec[i].fFileName, fallbackCount, tf->uniqueID()); |
michael@0 | 502 | + gFallbackFonts[fallbackCount++] = tf->uniqueID(); |
michael@0 | 503 | + } |
michael@0 | 504 | + |
michael@0 | 505 | + firstInFamily = tf; |
michael@0 | 506 | + FamilyRec* family = find_family(tf); |
michael@0 | 507 | + const char* const* names = rec[i].fNames; |
michael@0 | 508 | + |
michael@0 | 509 | + // record the default family if this is it |
michael@0 | 510 | + if (names == DEFAULT_NAMES) { |
michael@0 | 511 | + gDefaultFamily = family; |
michael@0 | 512 | + } |
michael@0 | 513 | + // add the names to map to this family |
michael@0 | 514 | + while (*names) { |
michael@0 | 515 | + add_name(*names, family); |
michael@0 | 516 | + names += 1; |
michael@0 | 517 | + } |
michael@0 | 518 | + } |
michael@0 | 519 | + } |
michael@0 | 520 | + |
michael@0 | 521 | + // do this after all fonts are loaded. This is our default font, and it |
michael@0 | 522 | + // acts as a sentinel so we only execute load_system_fonts() once |
michael@0 | 523 | + gDefaultNormal = find_best_face(gDefaultFamily, SkTypeface::kNormal); |
michael@0 | 524 | + // now terminate our fallback list with the sentinel value |
michael@0 | 525 | + gFallbackFonts[fallbackCount] = 0; |
michael@0 | 526 | +} |
michael@0 | 527 | + |
michael@0 | 528 | +/////////////////////////////////////////////////////////////////////////////// |
michael@0 | 529 | + |
michael@0 | 530 | +void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { |
michael@0 | 531 | + const char* name = ((FamilyTypeface*)face)->getUniqueString(); |
michael@0 | 532 | + |
michael@0 | 533 | + stream->write8((uint8_t)face->style()); |
michael@0 | 534 | + |
michael@0 | 535 | + if (NULL == name || 0 == *name) { |
michael@0 | 536 | + stream->writePackedUInt(0); |
michael@0 | 537 | +// SkDebugf("--- fonthost serialize null\n"); |
michael@0 | 538 | + } else { |
michael@0 | 539 | + uint32_t len = strlen(name); |
michael@0 | 540 | + stream->writePackedUInt(len); |
michael@0 | 541 | + stream->write(name, len); |
michael@0 | 542 | +// SkDebugf("--- fonthost serialize <%s> %d\n", name, face->style()); |
michael@0 | 543 | + } |
michael@0 | 544 | +} |
michael@0 | 545 | + |
michael@0 | 546 | +SkTypeface* SkFontHost::Deserialize(SkStream* stream) { |
michael@0 | 547 | + load_system_fonts(); |
michael@0 | 548 | + |
michael@0 | 549 | + int style = stream->readU8(); |
michael@0 | 550 | + |
michael@0 | 551 | + int len = stream->readPackedUInt(); |
michael@0 | 552 | + if (len > 0) { |
michael@0 | 553 | + SkString str; |
michael@0 | 554 | + str.resize(len); |
michael@0 | 555 | + stream->read(str.writable_str(), len); |
michael@0 | 556 | + |
michael@0 | 557 | + const FontInitRec* rec = gSystemFonts; |
michael@0 | 558 | + for (size_t i = 0; i < SK_ARRAY_COUNT(gSystemFonts); i++) { |
michael@0 | 559 | + if (strcmp(rec[i].fFileName, str.c_str()) == 0) { |
michael@0 | 560 | + // backup until we hit the fNames |
michael@0 | 561 | + for (int j = i; j >= 0; --j) { |
michael@0 | 562 | + if (rec[j].fNames != NULL) { |
michael@0 | 563 | + return SkFontHost::CreateTypeface(NULL, |
michael@0 | 564 | + rec[j].fNames[0], (SkTypeface::Style)style); |
michael@0 | 565 | + } |
michael@0 | 566 | + } |
michael@0 | 567 | + } |
michael@0 | 568 | + } |
michael@0 | 569 | + } |
michael@0 | 570 | + return NULL; |
michael@0 | 571 | +} |
michael@0 | 572 | + |
michael@0 | 573 | +/////////////////////////////////////////////////////////////////////////////// |
michael@0 | 574 | + |
michael@0 | 575 | +SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, |
michael@0 | 576 | + const char familyName[], |
michael@0 | 577 | + SkTypeface::Style style) { |
michael@0 | 578 | + load_system_fonts(); |
michael@0 | 579 | + |
michael@0 | 580 | + SkAutoMutexAcquire ac(gFamilyMutex); |
michael@0 | 581 | + |
michael@0 | 582 | + // clip to legal style bits |
michael@0 | 583 | + style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic); |
michael@0 | 584 | + |
michael@0 | 585 | + SkTypeface* tf = NULL; |
michael@0 | 586 | + |
michael@0 | 587 | + if (NULL != familyFace) { |
michael@0 | 588 | + tf = find_typeface(familyFace, style); |
michael@0 | 589 | + } else if (NULL != familyName) { |
michael@0 | 590 | +// SkDebugf("======= familyName <%s>\n", familyName); |
michael@0 | 591 | + tf = find_typeface(familyName, style); |
michael@0 | 592 | + } |
michael@0 | 593 | + |
michael@0 | 594 | + if (NULL == tf) { |
michael@0 | 595 | + tf = find_best_face(gDefaultFamily, style); |
michael@0 | 596 | + } |
michael@0 | 597 | + |
michael@0 | 598 | + // we ref(), since the symantic is to return a new instance |
michael@0 | 599 | + tf->ref(); |
michael@0 | 600 | + return tf; |
michael@0 | 601 | +} |
michael@0 | 602 | + |
michael@0 | 603 | +SkStream* SkFontHost::OpenStream(uint32_t fontID) { |
michael@0 | 604 | + SkAutoMutexAcquire ac(gFamilyMutex); |
michael@0 | 605 | + |
michael@0 | 606 | + FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID); |
michael@0 | 607 | + SkStream* stream = tf ? tf->openStream() : NULL; |
michael@0 | 608 | + |
michael@0 | 609 | + if (stream && stream->getLength() == 0) { |
michael@0 | 610 | + stream->unref(); |
michael@0 | 611 | + stream = NULL; |
michael@0 | 612 | + } |
michael@0 | 613 | + return stream; |
michael@0 | 614 | +} |
michael@0 | 615 | + |
michael@0 | 616 | +size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length, |
michael@0 | 617 | + int32_t* index) { |
michael@0 | 618 | + SkAutoMutexAcquire ac(gFamilyMutex); |
michael@0 | 619 | + |
michael@0 | 620 | + FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID); |
michael@0 | 621 | + const char* src = tf ? tf->getFilePath() : NULL; |
michael@0 | 622 | + |
michael@0 | 623 | + if (src) { |
michael@0 | 624 | + size_t size = strlen(src); |
michael@0 | 625 | + if (path) { |
michael@0 | 626 | + memcpy(path, src, SkMin32(size, length)); |
michael@0 | 627 | + } |
michael@0 | 628 | + if (index) { |
michael@0 | 629 | + *index = 0; // we don't have collections (yet) |
michael@0 | 630 | + } |
michael@0 | 631 | + return size; |
michael@0 | 632 | + } else { |
michael@0 | 633 | + return 0; |
michael@0 | 634 | + } |
michael@0 | 635 | +} |
michael@0 | 636 | + |
michael@0 | 637 | +SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) { |
michael@0 | 638 | + load_system_fonts(); |
michael@0 | 639 | + |
michael@0 | 640 | + /* First see if fontID is already one of our fallbacks. If so, return |
michael@0 | 641 | + its successor. If fontID is not in our list, then return the first one |
michael@0 | 642 | + in our list. Note: list is zero-terminated, and returning zero means |
michael@0 | 643 | + we have no more fonts to use for fallbacks. |
michael@0 | 644 | + */ |
michael@0 | 645 | + const uint32_t* list = gFallbackFonts; |
michael@0 | 646 | + for (int i = 0; list[i] != 0; i++) { |
michael@0 | 647 | + if (list[i] == currFontID) { |
michael@0 | 648 | + return list[i+1]; |
michael@0 | 649 | + } |
michael@0 | 650 | + } |
michael@0 | 651 | + return list[0]; |
michael@0 | 652 | +} |
michael@0 | 653 | + |
michael@0 | 654 | +/////////////////////////////////////////////////////////////////////////////// |
michael@0 | 655 | + |
michael@0 | 656 | +SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { |
michael@0 | 657 | + if (NULL == stream || stream->getLength() <= 0) { |
michael@0 | 658 | + return NULL; |
michael@0 | 659 | + } |
michael@0 | 660 | + |
michael@0 | 661 | + bool isFixedWidth; |
michael@0 | 662 | + SkString name; |
michael@0 | 663 | + SkTypeface::Style style; |
michael@0 | 664 | + find_name_and_attributes(stream, &name, &style, &isFixedWidth); |
michael@0 | 665 | + |
michael@0 | 666 | + if (!name.isEmpty()) { |
michael@0 | 667 | + return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedWidth)); |
michael@0 | 668 | + } else { |
michael@0 | 669 | + return NULL; |
michael@0 | 670 | + } |
michael@0 | 671 | +} |
michael@0 | 672 | + |
michael@0 | 673 | +SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
michael@0 | 674 | + SkStream* stream = SkNEW_ARGS(SkMMAPStream, (path)); |
michael@0 | 675 | + SkTypeface* face = SkFontHost::CreateTypefaceFromStream(stream); |
michael@0 | 676 | + // since we created the stream, we let go of our ref() here |
michael@0 | 677 | + stream->unref(); |
michael@0 | 678 | + return face; |
michael@0 | 679 | +} |
michael@0 | 680 | + |
michael@0 | 681 | +/////////////////////////////////////////////////////////////////////////////// |
michael@0 | 682 | -- |
michael@0 | 683 | 1.7.11.4 |
michael@0 | 684 |