diff options
author | Derek Sollenberger <djsollen@google.com> | 2011-10-12 11:54:49 -0400 |
---|---|---|
committer | Derek Sollenberger <djsollen@google.com> | 2011-10-12 11:58:47 -0400 |
commit | 40646971fb336f93951a6fc616f83c00e4de7add (patch) | |
tree | cbadd36e59a0866e2623becb94535a2d895a14a3 | |
parent | d6cd36570b5f18182c711532172cf233a41e643f (diff) | |
download | skia-40646971fb336f93951a6fc616f83c00e4de7add.tar.gz |
Fix font issue for some sites using offline reading mode.
Custom fonts (e.g. WebFonts) were not properly serialized and
resulted in unexpected behavior when a picture was saved for
offline reading. This CL serializes the custom font so that we
can use it later when playing the picture back.
bug: 5388379
Change-Id: Ic29e9bf10ac70c46a45f3902ea93558b235fd2dd
-rw-r--r-- | src/ports/SkFontHost_android.cpp | 95 |
1 files changed, 70 insertions, 25 deletions
diff --git a/src/ports/SkFontHost_android.cpp b/src/ports/SkFontHost_android.cpp index 76d9961f2f..766c617a2f 100644 --- a/src/ports/SkFontHost_android.cpp +++ b/src/ports/SkFontHost_android.cpp @@ -545,40 +545,85 @@ static void load_system_fonts() { /////////////////////////////////////////////////////////////////////////////// void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { - const char* name = ((FamilyTypeface*)face)->getUniqueString(); + // lookup and record if the font is custom (i.e. not a system font) + bool isCustomFont = !((FamilyTypeface*)face)->isSysFont(); + stream->writeBool(isCustomFont); - stream->write8((uint8_t)face->style()); + if (isCustomFont) { + SkStream* fontStream = ((FamilyTypeface*)face)->openStream(); - if (NULL == name || 0 == *name) { - stream->writePackedUInt(0); -// SkDebugf("--- fonthost serialize null\n"); - } else { - uint32_t len = strlen(name); + // store the length of the custom font + uint32_t len = fontStream->getLength(); stream->writePackedUInt(len); - stream->write(name, len); -// SkDebugf("--- fonthost serialize <%s> %d\n", name, face->style()); + + // store the entire font in the serialized stream + void* fontData = malloc(len); + + fontStream->read(fontData, len); + stream->write(fontData, len); + + fontStream->unref(); + free(fontData); +// SkDebugf("--- fonthost custom serialize %d\n", face->style()); + + } else { + const char* name = ((FamilyTypeface*)face)->getUniqueString(); + + stream->write8((uint8_t)face->style()); + + if (NULL == name || 0 == *name) { + stream->writePackedUInt(0); +// SkDebugf("--- fonthost serialize null\n"); + } else { + uint32_t len = strlen(name); + stream->writePackedUInt(len); + stream->write(name, len); +// SkDebugf("--- fonthost serialize <%s> %d\n", name, face->style()); + } } } SkTypeface* SkFontHost::Deserialize(SkStream* stream) { load_system_fonts(); - int style = stream->readU8(); - - int len = stream->readPackedUInt(); - if (len > 0) { - SkString str; - str.resize(len); - stream->read(str.writable_str(), len); - - const FontInitRec* rec = gSystemFonts; - for (size_t i = 0; i < gNumSystemFonts; i++) { - if (strcmp(rec[i].fFileName, str.c_str()) == 0) { - // backup until we hit the fNames - for (int j = i; j >= 0; --j) { - if (rec[j].fNames != NULL) { - return SkFontHost::CreateTypeface(NULL, - rec[j].fNames[0], NULL, 0, (SkTypeface::Style)style); + // check if the font is a custom or system font + bool isCustomFont = stream->readBool(); + + if (isCustomFont) { + + // read the length of the custom font from the stream + int len = stream->readPackedUInt(); + + // generate a new stream to store the custom typeface + SkMemoryStream* fontStream = new SkMemoryStream(len); + stream->read((void*)fontStream->getMemoryBase(), len); + + SkTypeface* face = CreateTypefaceFromStream(fontStream); + + fontStream->unref(); + +// SkDebugf("--- fonthost custom deserialize %d\n", face->style()); + return face; + + } else { + int style = stream->readU8(); + + int len = stream->readPackedUInt(); + if (len > 0) { + SkString str; + str.resize(len); + stream->read(str.writable_str(), len); + + const FontInitRec* rec = gSystemFonts; + for (size_t i = 0; i < gNumSystemFonts; i++) { + if (strcmp(rec[i].fFileName, str.c_str()) == 0) { + // backup until we hit the fNames + for (int j = i; j >= 0; --j) { + if (rec[j].fNames != NULL) { + return SkFontHost::CreateTypeface(NULL, + rec[j].fNames[0], NULL, 0, + (SkTypeface::Style)style); + } } } } |