// overrides virtual SkStream* openStream() { SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str())); // check for failure if (stream->getLength() <= 0) { SkDELETE(stream); // maybe MMAP isn't supported. try FILE stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str())); if (stream->getLength() <= 0) { SkDELETE(stream); stream = NULL; } } return stream; }
static void delete_stream_proc(void* info, const void* addr, size_t size) { SkASSERT(info); SkStream* stream = (SkStream*)info; SkASSERT(stream->getMemoryBase() == addr); SkASSERT(stream->getLength() == size); SkDELETE(stream); }
void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { SkFontDescriptor descriptor; { SkAutoMutexAcquire ac(gFamilyHeadAndNameListMutex); descriptor.setFamilyName(find_family_name(face)); descriptor.setStyle(face->style()); descriptor.setFontFileName(((FamilyTypeface*)face)->getUniqueString()); } descriptor.serialize(stream); const bool isCustomFont = !((FamilyTypeface*)face)->isSysFont(); if (isCustomFont) { // store the entire font in the fontData SkStream* fontStream = ((FamilyTypeface*)face)->openStream(); const uint32_t length = fontStream->getLength(); stream->writePackedUInt(length); stream->writeStream(fontStream, length); fontStream->unref(); } else { stream->writePackedUInt(0); } }
SkStream* SkFontHost::OpenStream(uint32_t fontID) { FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID); SkStream* stream = tf ? tf->openStream() : NULL; if (stream && stream->getLength() == 0) { stream->unref(); stream = NULL; } return stream; }
static FTMacTypeface* create_from_path(const char path[]) { SkStream* stream = new SkMMAPStream(path); size_t size = stream->getLength(); SkASSERT(size); FTMacTypeface* tf = new FTMacTypeface(SkTypeface::kNormal, SkTypefaceCache::NewFontID(), stream); SkTypefaceCache::Add(tf, SkTypeface::kNormal); return tf; }
// Will return 0 on failure static SkFaceRec* ref_ft_face(uint32_t fontID) { SkFaceRec* rec = gFaceRecHead; while (rec) { if (rec->fFontID == fontID) { SkASSERT(rec->fFace); rec->fRefCnt += 1; return rec; } rec = rec->fNext; } SkStream* strm = SkFontHost::OpenStream(fontID); if (NULL == strm) { SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID)); return 0; } // this passes ownership of strm to the rec rec = SkNEW_ARGS(SkFaceRec, (strm, fontID)); FT_Open_Args args; memset(&args, 0, sizeof(args)); const void* memoryBase = strm->getMemoryBase(); if (NULL != memoryBase) { //printf("mmap(%s)\n", keyString.c_str()); args.flags = FT_OPEN_MEMORY; args.memory_base = (const FT_Byte*)memoryBase; args.memory_size = strm->getLength(); } else { //printf("fopen(%s)\n", keyString.c_str()); args.flags = FT_OPEN_STREAM; args.stream = &rec->fFTStream; } int face_index; int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index); FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0, &rec->fFace); if (err) { // bad filename, try the default font fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID); SkDELETE(rec); return 0; } else { SkASSERT(rec->fFace); //fprintf(stderr, "Opened font '%s'\n", filename.c_str()); rec->fNext = gFaceRecHead; gFaceRecHead = rec; rec->fRefCnt = 1; return rec; } }
SkStream* SkFontHost::OpenStream(uint32_t fontID) { SkAutoMutexAcquire ac(gFamilyHeadAndNameListMutex); FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID); SkStream* stream = tf ? tf->openStream() : NULL; if (stream && stream->getLength() == 0) { stream->unref(); stream = NULL; } return stream; }
void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { SkStream* fontStream = ((FamilyTypeface*)face)->openStream(); // store the length of the custom font uint32_t len = fontStream->getLength(); stream->write32(len); // store the entire font in the serialized stream void* fontData = malloc(len); fontStream->read(fontData, len); stream->write(fontData, len); fontStream->unref(); free(fontData); // sk_throw(); }
SkStream* FontConfigTypeface::onOpenStream(int* ttcIndex) const { SkStream* stream = this->getLocalStream(); if (stream) { // should have been provided by CreateFromStream() *ttcIndex = 0; SkAutoTUnref<SkStream> dupStream(stream->duplicate()); if (dupStream) { return dupStream.detach(); } // TODO: update interface use, remove the following code in this block. size_t length = stream->getLength(); const void* memory = stream->getMemoryBase(); if (NULL != memory) { return new SkMemoryStream(memory, length, true); } SkAutoTMalloc<uint8_t> allocMemory(length); stream->rewind(); if (length == stream->read(allocMemory.get(), length)) { SkAutoTUnref<SkMemoryStream> copyStream(new SkMemoryStream()); copyStream->setMemoryOwned(allocMemory.detach(), length); return copyStream.detach(); } stream->rewind(); stream->ref(); } else { SkAutoTUnref<SkFontConfigInterface> fci(RefFCI()); if (NULL == fci.get()) { return NULL; } stream = fci->openStream(this->getIdentity()); *ttcIndex = this->getIdentity().fTTCIndex; } return stream; }
void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { // lookup and record if the font is custom (i.e. not a system font) bool isCustomFont = !((FamilyTypeface*)face)->isSysFont(); stream->writeBool(isCustomFont); if (isCustomFont) { SkStream* fontStream = ((FamilyTypeface*)face)->openStream(); // store the length of the custom font uint32_t len = fontStream->getLength(); stream->write32(len); // 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 %d\n", face->style(), len); } else { const char* name = ((FamilyTypeface*)face)->getFilePath(); // 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()); } } }
static unsigned int file_getsize(FPDFEMB_FILE_ACCESS* file) { SkStream* stream = (SkStream*)file->user; return stream->getLength(); }