add more names to fontdescriptor

Review URL: https://codereview.appspot.com/6249074

git-svn-id: http://skia.googlecode.com/svn/trunk@4110 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2012-05-31 21:16:48 +00:00
parent 14882947f3
commit 3b700f6929
2 changed files with 60 additions and 42 deletions

View File

@ -9,27 +9,31 @@
#include "SkStream.h"
enum {
// these must match the sfnt 'name' enums
kFontFamilyName = 0x01,
kFontStyle = 0x02,
kFontFileName = 0xFD,
kSentinel = 0xFE,
kFullName = 0x04,
kPostscriptName = 0x06,
// These count backwards from 0xFF, so as not to collide with the SFNT
// defines for names in its 'name' table.
kFontFileName = 0xFE,
kSentinel = 0xFF,
};
SkFontDescriptor::SkFontDescriptor() {
fFontStyle = SkTypeface::kNormal;
SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) {
fStyle = style;
}
static const char* read_string(SkStream* stream) {
SkString string;
static void read_string(SkStream* stream, SkString* string) {
const uint32_t length = stream->readPackedUInt();
if (length > 0) {
string.resize(length);
stream->read(string.writable_str(), length);
string->resize(length);
stream->read(string->writable_str(), length);
}
return string.c_str();
}
static void write_string(SkWStream* stream, SkString string, uint32_t id) {
static void write_string(SkWStream* stream, const SkString& string,
uint32_t id) {
if (!string.isEmpty()) {
stream->writePackedUInt(id);
stream->writePackedUInt(string.size());
@ -38,36 +42,38 @@ static void write_string(SkWStream* stream, SkString string, uint32_t id) {
}
SkFontDescriptor::SkFontDescriptor(SkStream* stream) {
fFontStyle = SkTypeface::kNormal;
fStyle = (SkTypeface::Style)stream->readPackedUInt();
uint32_t id = stream->readPackedUInt();
while (id != kSentinel) {
switch (id) {
case kFontFamilyName: {
fFontFamilyName.set(read_string(stream));
} break;
case kFontStyle: {
fFontStyle = (SkTypeface::Style)stream->readPackedUInt();
} break;
case kFontFileName: {
fFontFileName.set(read_string(stream));
} break;
for (;;) {
switch (stream->readPackedUInt()) {
case kFontFamilyName:
read_string(stream, &fFamilyName);
break;
case kFullName:
read_string(stream, &fFullName);
break;
case kPostscriptName:
read_string(stream, &fPostscriptName);
break;
case kFontFileName:
read_string(stream, &fFontFileName);
break;
case kSentinel:
return;
default:
SkDEBUGFAIL("Unknown id used by a font descriptor");
return;
}
int prevId = id;
id = stream->readPackedUInt();
}
}
void SkFontDescriptor::serialize(SkWStream* stream) {
write_string(stream, fFontFamilyName, kFontFamilyName);
write_string(stream, fFontFileName, kFontFileName);
stream->writePackedUInt(fStyle);
if (fFontStyle != SkTypeface::kNormal) {
stream->writePackedUInt(kFontStyle);
stream->writePackedUInt((uint32_t)fFontStyle);
}
write_string(stream, fFamilyName, kFontFamilyName);
write_string(stream, fFullName, kFullName);
write_string(stream, fPostscriptName, kPostscriptName);
write_string(stream, fFontFileName, kFontFileName);
stream->writePackedUInt(kSentinel);
}

View File

@ -5,8 +5,6 @@
* found in the LICENSE file.
*/
///////////////////////////////////////////////////////////////////////////////
#ifndef SkFontDescriptor_DEFINED
#define SkFontDescriptor_DEFINED
@ -18,23 +16,37 @@ class SkWStream;
class SkFontDescriptor {
public:
SkFontDescriptor();
SkFontDescriptor(SkTypeface::Style = SkTypeface::kNormal);
SkFontDescriptor(SkStream*);
void serialize(SkWStream*);
void setFontFamilyName(const char* name) { fFontFamilyName.set(name); }
void setFontStyle(SkTypeface::Style style) { fFontStyle = style; }
SkTypeface::Style getStyle() { return fStyle; }
void setStyle(SkTypeface::Style style) { fStyle = style; }
const char* getFamilyName() { return fFamilyName.c_str(); }
const char* getFullName() { return fFullName.c_str(); }
const char* getPostscriptName() { return fPostscriptName.c_str(); }
const char* getFontFileName() { return fFontFileName.c_str(); }
void setFamilyName(const char* name) { fFamilyName.set(name); }
void setFullName(const char* name) { fFullName.set(name); }
void setPostscriptName(const char* name) { fPostscriptName.set(name); }
void setFontFileName(const char* name) { fFontFileName.set(name); }
const char* getFontFamilyName() { return fFontFamilyName.c_str(); }
SkTypeface::Style getFontStyle() { return fFontStyle; }
const char* getFontFileName() { return fFontFileName.c_str(); }
// legacy naming
SkTypeface::Style getFontStyle() { return this->getStyle(); }
void setFontStyle(SkTypeface::Style style) { this->setStyle(style); }
const char* getFontFamilyName() { return this->getFamilyName(); }
void setFontFamilyName(const char* name) { this->setFamilyName(name); }
private:
SkString fFontFamilyName;
SkTypeface::Style fFontStyle;
SkString fFamilyName;
SkString fFullName;
SkString fPostscriptName;
SkString fFontFileName;
SkTypeface::Style fStyle;
};
#endif // SkFontDescriptor_DEFINED