Home | History | Annotate | Download | only in core
      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 {
     12     // these must match the sfnt 'name' enums
     13     kFontFamilyName = 0x01,
     14     kFullName       = 0x04,
     15     kPostscriptName = 0x06,
     16 
     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,
     21 };
     22 
     23 SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) {
     24     fStyle = style;
     25 }
     26 
     27 static void read_string(SkStream* stream, SkString* string) {
     28     const uint32_t length = stream->readPackedUInt();
     29     if (length > 0) {
     30         string->resize(length);
     31         stream->read(string->writable_str(), length);
     32     }
     33 }
     34 
     35 static void write_string(SkWStream* stream, const SkString& string,
     36                          uint32_t id) {
     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) {
     45     fStyle = (SkTypeface::Style)stream->readPackedUInt();
     46 
     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;
     63             default:
     64                 SkDEBUGFAIL("Unknown id used by a font descriptor");
     65                 return;
     66         }
     67     }
     68 }
     69 
     70 void SkFontDescriptor::serialize(SkWStream* stream) {
     71     stream->writePackedUInt(fStyle);
     72 
     73     write_string(stream, fFamilyName, kFontFamilyName);
     74     write_string(stream, fFullName, kFullName);
     75     write_string(stream, fPostscriptName, kPostscriptName);
     76     write_string(stream, fFontFileName, kFontFileName);
     77 
     78     stream->writePackedUInt(kSentinel);
     79 }
     80