OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkEndian.h" |
9 #include "SkErrorInternals.h" | 10 #include "SkErrorInternals.h" |
10 #include "SkImage.h" | 11 #include "SkImage.h" |
11 #include "SkImageGenerator.h" | 12 #include "SkImageGenerator.h" |
12 #include "SkReadBuffer.h" | 13 #include "SkReadBuffer.h" |
13 #include "SkStream.h" | 14 #include "SkStream.h" |
14 #include "SkTypeface.h" | 15 #include "SkTypeface.h" |
15 | 16 |
16 static uint32_t default_flags() { | 17 static uint32_t default_flags() { |
17 uint32_t flags = 0; | 18 uint32_t flags = 0; |
18 flags |= SkReadBuffer::kScalarIsFloat_Flag; | 19 flags |= SkReadBuffer::kScalarIsFloat_Flag; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 } | 100 } |
100 | 101 |
101 uint32_t SkReadBuffer::readUInt() { | 102 uint32_t SkReadBuffer::readUInt() { |
102 return fReader.readU32(); | 103 return fReader.readU32(); |
103 } | 104 } |
104 | 105 |
105 int32_t SkReadBuffer::read32() { | 106 int32_t SkReadBuffer::read32() { |
106 return fReader.readInt(); | 107 return fReader.readInt(); |
107 } | 108 } |
108 | 109 |
| 110 uint8_t SkReadBuffer::peekByte() { |
| 111 SkASSERT(fReader.available() > 0); |
| 112 return *((uint8_t*) fReader.peek()); |
| 113 } |
| 114 |
109 void SkReadBuffer::readString(SkString* string) { | 115 void SkReadBuffer::readString(SkString* string) { |
110 size_t len; | 116 size_t len; |
111 const char* strContents = fReader.readString(&len); | 117 const char* strContents = fReader.readString(&len); |
112 string->set(strContents, len); | 118 string->set(strContents, len); |
113 } | 119 } |
114 | 120 |
115 void* SkReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncoding enco
ding) { | 121 void* SkReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncoding enco
ding) { |
116 SkDEBUGCODE(int32_t encodingType = ) fReader.readInt(); | 122 SkDEBUGCODE(int32_t encodingType = ) fReader.readInt(); |
117 SkASSERT(encodingType == encoding); | 123 SkASSERT(encodingType == encoding); |
118 *length = fReader.readInt(); | 124 *length = fReader.readInt(); |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 if (0 == index) { | 343 if (0 == index) { |
338 return nullptr; // writer failed to give us the flattenable | 344 return nullptr; // writer failed to give us the flattenable |
339 } | 345 } |
340 index -= 1; // we stored the index-base-1 | 346 index -= 1; // we stored the index-base-1 |
341 if ((unsigned)index >= (unsigned)fFactoryCount) { | 347 if ((unsigned)index >= (unsigned)fFactoryCount) { |
342 this->validate(false); | 348 this->validate(false); |
343 return nullptr; | 349 return nullptr; |
344 } | 350 } |
345 factory = fFactoryArray[index]; | 351 factory = fFactoryArray[index]; |
346 } else { | 352 } else { |
347 factory = (SkFlattenable::Factory)readFunctionPtr(); | 353 SkString name; |
348 if (nullptr == factory) { | 354 if (this->peekByte()) { |
349 return nullptr; // writer failed to give us the flattenable | 355 // If the first byte is non-zero, the flattenable is specified by a
string. |
| 356 this->readString(&name); |
| 357 |
| 358 // Add the string to the dictionary. |
| 359 fFlattenableDict.set(fFlattenableDict.count() + 1, name); |
| 360 } else { |
| 361 // Read the index. We are guaranteed that the first and last bytes |
| 362 // are zeroed, so we must shift down a byte. |
| 363 uint32_t index = fReader.readU32() >> 8; |
| 364 if (0 == index) { |
| 365 return nullptr; // writer failed to give us the flattenable |
| 366 } |
| 367 |
| 368 SkString* namePtr = fFlattenableDict.find(index); |
| 369 SkASSERT(namePtr); |
| 370 name = *namePtr; |
| 371 } |
| 372 |
| 373 // Check if a custom Factory has been specified for this flattenable. |
| 374 if (!(factory = this->getCustomFactory(name))) { |
| 375 // If there is no custom Factory, check for a default. |
| 376 if (!(factory = SkFlattenable::NameToFactory(name.c_str()))) { |
| 377 return nullptr; // writer failed to give us the flattenable |
| 378 } |
350 } | 379 } |
351 } | 380 } |
352 | 381 |
353 // if we get here, factory may still be null, but if that is the case, the | 382 // if we get here, factory may still be null, but if that is the case, the |
354 // failure was ours, not the writer. | 383 // failure was ours, not the writer. |
355 sk_sp<SkFlattenable> obj; | 384 sk_sp<SkFlattenable> obj; |
356 uint32_t sizeRecorded = fReader.readU32(); | 385 uint32_t sizeRecorded = fReader.readU32(); |
357 if (factory) { | 386 if (factory) { |
358 size_t offset = fReader.offset(); | 387 size_t offset = fReader.offset(); |
359 obj = (*factory)(*this); | 388 obj = (*factory)(*this); |
(...skipping 20 matching lines...) Expand all Loading... |
380 return; | 409 return; |
381 } | 410 } |
382 } else { | 411 } else { |
383 if (nullptr == this->readFunctionPtr()) { | 412 if (nullptr == this->readFunctionPtr()) { |
384 return; | 413 return; |
385 } | 414 } |
386 } | 415 } |
387 uint32_t sizeRecorded = fReader.readU32(); | 416 uint32_t sizeRecorded = fReader.readU32(); |
388 fReader.skip(sizeRecorded); | 417 fReader.skip(sizeRecorded); |
389 } | 418 } |
OLD | NEW |