djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkFontDescriptor.h" |
| 9 | #include "SkStream.h" |
| 10 | |
| 11 | enum { |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 12 | // these must match the sfnt 'name' enums |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 13 | kFontFamilyName = 0x01, |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 14 | kFullName = 0x04, |
| 15 | kPostscriptName = 0x06, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 16 | |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 17 | // These count backwards from 0xFF, so as not to collide with the SFNT |
| 18 | // defines for names in its 'name' table. |
| 19 | kFontFileName = 0xFE, |
| 20 | kSentinel = 0xFF, |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 23 | SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) { |
| 24 | fStyle = style; |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 25 | } |
| 26 | |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 27 | static void read_string(SkStream* stream, SkString* string) { |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 28 | const uint32_t length = stream->readPackedUInt(); |
| 29 | if (length > 0) { |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 30 | string->resize(length); |
| 31 | stream->read(string->writable_str(), length); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 32 | } |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 33 | } |
| 34 | |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 35 | static void write_string(SkWStream* stream, const SkString& string, |
| 36 | uint32_t id) { |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 37 | if (!string.isEmpty()) { |
| 38 | stream->writePackedUInt(id); |
| 39 | stream->writePackedUInt(string.size()); |
| 40 | stream->write(string.c_str(), string.size()); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | SkFontDescriptor::SkFontDescriptor(SkStream* stream) { |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 45 | fStyle = (SkTypeface::Style)stream->readPackedUInt(); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 46 | |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 47 | for (;;) { |
| 48 | switch (stream->readPackedUInt()) { |
| 49 | case kFontFamilyName: |
| 50 | read_string(stream, &fFamilyName); |
| 51 | break; |
| 52 | case kFullName: |
| 53 | read_string(stream, &fFullName); |
| 54 | break; |
| 55 | case kPostscriptName: |
| 56 | read_string(stream, &fPostscriptName); |
| 57 | break; |
| 58 | case kFontFileName: |
| 59 | read_string(stream, &fFontFileName); |
| 60 | break; |
| 61 | case kSentinel: |
| 62 | return; |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 63 | default: |
| 64 | SkDEBUGFAIL("Unknown id used by a font descriptor"); |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 65 | return; |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 66 | } |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | void SkFontDescriptor::serialize(SkWStream* stream) { |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 71 | stream->writePackedUInt(fStyle); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 72 | |
reed@google.com | 3b700f6 | 2012-05-31 21:16:48 +0000 | [diff] [blame] | 73 | write_string(stream, fFamilyName, kFontFamilyName); |
| 74 | write_string(stream, fFullName, kFullName); |
| 75 | write_string(stream, fPostscriptName, kPostscriptName); |
| 76 | write_string(stream, fFontFileName, kFontFileName); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 77 | |
| 78 | stream->writePackedUInt(kSentinel); |
| 79 | } |