michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkFontDescriptor.h" michael@0: #include "SkStream.h" michael@0: michael@0: enum { michael@0: // these must match the sfnt 'name' enums michael@0: kFontFamilyName = 0x01, michael@0: kFullName = 0x04, michael@0: kPostscriptName = 0x06, michael@0: michael@0: // These count backwards from 0xFF, so as not to collide with the SFNT michael@0: // defines for names in its 'name' table. michael@0: kFontFileName = 0xFE, michael@0: kSentinel = 0xFF, michael@0: }; michael@0: michael@0: SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) { michael@0: fStyle = style; michael@0: } michael@0: michael@0: static void read_string(SkStream* stream, SkString* string) { michael@0: const uint32_t length = SkToU32(stream->readPackedUInt()); michael@0: if (length > 0) { michael@0: string->resize(length); michael@0: stream->read(string->writable_str(), length); michael@0: } michael@0: } michael@0: michael@0: static void write_string(SkWStream* stream, const SkString& string, michael@0: uint32_t id) { michael@0: if (!string.isEmpty()) { michael@0: stream->writePackedUInt(id); michael@0: stream->writePackedUInt(string.size()); michael@0: stream->write(string.c_str(), string.size()); michael@0: } michael@0: } michael@0: michael@0: SkFontDescriptor::SkFontDescriptor(SkStream* stream) { michael@0: fStyle = (SkTypeface::Style)stream->readPackedUInt(); michael@0: michael@0: for (;;) { michael@0: switch (stream->readPackedUInt()) { michael@0: case kFontFamilyName: michael@0: read_string(stream, &fFamilyName); michael@0: break; michael@0: case kFullName: michael@0: read_string(stream, &fFullName); michael@0: break; michael@0: case kPostscriptName: michael@0: read_string(stream, &fPostscriptName); michael@0: break; michael@0: case kFontFileName: michael@0: read_string(stream, &fFontFileName); michael@0: break; michael@0: case kSentinel: michael@0: return; michael@0: default: michael@0: SkDEBUGFAIL("Unknown id used by a font descriptor"); michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkFontDescriptor::serialize(SkWStream* stream) { michael@0: stream->writePackedUInt(fStyle); michael@0: michael@0: write_string(stream, fFamilyName, kFontFamilyName); michael@0: write_string(stream, fFullName, kFullName); michael@0: write_string(stream, fPostscriptName, kPostscriptName); michael@0: write_string(stream, fFontFileName, kFontFileName); michael@0: michael@0: stream->writePackedUInt(kSentinel); michael@0: }