OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
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 "SkAdvancedTypefaceMetrics.h" | 8 #include "SkAdvancedTypefaceMetrics.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 bool shouldSubpixelBitmap(const SkGlyph&, const SkMatrix&); | 227 bool shouldSubpixelBitmap(const SkGlyph&, const SkMatrix&); |
228 }; | 228 }; |
229 | 229 |
230 /////////////////////////////////////////////////////////////////////////// | 230 /////////////////////////////////////////////////////////////////////////// |
231 /////////////////////////////////////////////////////////////////////////// | 231 /////////////////////////////////////////////////////////////////////////// |
232 | 232 |
233 struct SkFaceRec { | 233 struct SkFaceRec { |
234 SkFaceRec* fNext; | 234 SkFaceRec* fNext; |
235 FT_Face fFace; | 235 FT_Face fFace; |
236 FT_StreamRec fFTStream; | 236 FT_StreamRec fFTStream; |
237 std::unique_ptr<SkStreamAsset> fSkStream; | 237 SkAutoTDelete<SkStreamAsset> fSkStream; |
238 uint32_t fRefCnt; | 238 uint32_t fRefCnt; |
239 uint32_t fFontID; | 239 uint32_t fFontID; |
240 | 240 |
241 SkFaceRec(std::unique_ptr<SkStreamAsset> stream, uint32_t fontID); | 241 // assumes ownership of the stream, will delete when its done |
| 242 SkFaceRec(SkStreamAsset* strm, uint32_t fontID); |
242 }; | 243 }; |
243 | 244 |
244 extern "C" { | 245 extern "C" { |
245 static unsigned long sk_ft_stream_io(FT_Stream ftStream, | 246 static unsigned long sk_ft_stream_io(FT_Stream ftStream, |
246 unsigned long offset, | 247 unsigned long offset, |
247 unsigned char* buffer, | 248 unsigned char* buffer, |
248 unsigned long count) | 249 unsigned long count) |
249 { | 250 { |
250 SkStreamAsset* stream = static_cast<SkStreamAsset*>(ftStream->descriptor
.pointer); | 251 SkStreamAsset* stream = static_cast<SkStreamAsset*>(ftStream->descriptor
.pointer); |
251 | 252 |
252 if (count) { | 253 if (count) { |
253 if (!stream->seek(offset)) { | 254 if (!stream->seek(offset)) { |
254 return 0; | 255 return 0; |
255 } | 256 } |
256 count = stream->read(buffer, count); | 257 count = stream->read(buffer, count); |
257 } | 258 } |
258 return count; | 259 return count; |
259 } | 260 } |
260 | 261 |
261 static void sk_ft_stream_close(FT_Stream) {} | 262 static void sk_ft_stream_close(FT_Stream) {} |
262 } | 263 } |
263 | 264 |
264 SkFaceRec::SkFaceRec(std::unique_ptr<SkStreamAsset> stream, uint32_t fontID) | 265 SkFaceRec::SkFaceRec(SkStreamAsset* stream, uint32_t fontID) |
265 : fNext(nullptr), fSkStream(std::move(stream)), fRefCnt(1), fFontID(font
ID) | 266 : fNext(nullptr), fSkStream(stream), fRefCnt(1), fFontID(fontID) |
266 { | 267 { |
267 sk_bzero(&fFTStream, sizeof(fFTStream)); | 268 sk_bzero(&fFTStream, sizeof(fFTStream)); |
268 fFTStream.size = fSkStream->getLength(); | 269 fFTStream.size = fSkStream->getLength(); |
269 fFTStream.descriptor.pointer = fSkStream.get(); | 270 fFTStream.descriptor.pointer = fSkStream; |
270 fFTStream.read = sk_ft_stream_io; | 271 fFTStream.read = sk_ft_stream_io; |
271 fFTStream.close = sk_ft_stream_close; | 272 fFTStream.close = sk_ft_stream_close; |
272 } | 273 } |
273 | 274 |
274 static void ft_face_setup_axes(FT_Face face, const SkFontData& data) { | 275 static void ft_face_setup_axes(FT_Face face, const SkFontData& data) { |
275 if (!(face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) { | 276 if (!(face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) { |
276 return; | 277 return; |
277 } | 278 } |
278 | 279 |
279 SkDEBUGCODE( | 280 SkDEBUGCODE( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 SkFaceRec* rec = gFaceRecHead; | 312 SkFaceRec* rec = gFaceRecHead; |
312 while (rec) { | 313 while (rec) { |
313 if (rec->fFontID == fontID) { | 314 if (rec->fFontID == fontID) { |
314 SkASSERT(rec->fFace); | 315 SkASSERT(rec->fFace); |
315 rec->fRefCnt += 1; | 316 rec->fRefCnt += 1; |
316 return rec->fFace; | 317 return rec->fFace; |
317 } | 318 } |
318 rec = rec->fNext; | 319 rec = rec->fNext; |
319 } | 320 } |
320 | 321 |
321 std::unique_ptr<SkFontData> data = typeface->makeFontData(); | 322 SkAutoTDelete<SkFontData> data(typeface->createFontData()); |
322 if (nullptr == data || !data->hasStream()) { | 323 if (nullptr == data || !data->hasStream()) { |
323 return nullptr; | 324 return nullptr; |
324 } | 325 } |
325 | 326 |
| 327 // this passes ownership of stream to the rec |
326 rec = new SkFaceRec(data->detachStream(), fontID); | 328 rec = new SkFaceRec(data->detachStream(), fontID); |
327 | 329 |
328 FT_Open_Args args; | 330 FT_Open_Args args; |
329 memset(&args, 0, sizeof(args)); | 331 memset(&args, 0, sizeof(args)); |
330 const void* memoryBase = rec->fSkStream->getMemoryBase(); | 332 const void* memoryBase = rec->fSkStream->getMemoryBase(); |
331 if (memoryBase) { | 333 if (memoryBase) { |
332 args.flags = FT_OPEN_MEMORY; | 334 args.flags = FT_OPEN_MEMORY; |
333 args.memory_base = (const FT_Byte*)memoryBase; | 335 args.memory_base = (const FT_Byte*)memoryBase; |
334 args.memory_size = rec->fSkStream->getLength(); | 336 args.memory_size = rec->fSkStream->getLength(); |
335 } else { | 337 } else { |
(...skipping 1219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1555 return; | 1557 return; |
1556 } | 1558 } |
1557 FT_Add_Default_Modules(fLibrary); | 1559 FT_Add_Default_Modules(fLibrary); |
1558 } | 1560 } |
1559 SkTypeface_FreeType::Scanner::~Scanner() { | 1561 SkTypeface_FreeType::Scanner::~Scanner() { |
1560 if (fLibrary) { | 1562 if (fLibrary) { |
1561 FT_Done_Library(fLibrary); | 1563 FT_Done_Library(fLibrary); |
1562 } | 1564 } |
1563 } | 1565 } |
1564 | 1566 |
1565 FT_Face SkTypeface_FreeType::Scanner::openFace(SkStreamAsset* stream, int ttcInd
ex, | 1567 FT_Face SkTypeface_FreeType::Scanner::openFace(SkStream* stream, int ttcIndex, |
1566 FT_Stream ftStream) const | 1568 FT_Stream ftStream) const |
1567 { | 1569 { |
1568 if (fLibrary == nullptr) { | 1570 if (fLibrary == nullptr) { |
1569 return nullptr; | 1571 return nullptr; |
1570 } | 1572 } |
1571 | 1573 |
1572 FT_Open_Args args; | 1574 FT_Open_Args args; |
1573 memset(&args, 0, sizeof(args)); | 1575 memset(&args, 0, sizeof(args)); |
1574 | 1576 |
1575 const void* memoryBase = stream->getMemoryBase(); | 1577 const void* memoryBase = stream->getMemoryBase(); |
(...skipping 13 matching lines...) Expand all Loading... |
1589 args.stream = ftStream; | 1591 args.stream = ftStream; |
1590 } | 1592 } |
1591 | 1593 |
1592 FT_Face face; | 1594 FT_Face face; |
1593 if (FT_Open_Face(fLibrary, &args, ttcIndex, &face)) { | 1595 if (FT_Open_Face(fLibrary, &args, ttcIndex, &face)) { |
1594 return nullptr; | 1596 return nullptr; |
1595 } | 1597 } |
1596 return face; | 1598 return face; |
1597 } | 1599 } |
1598 | 1600 |
1599 bool SkTypeface_FreeType::Scanner::recognizedFont(SkStreamAsset* stream, int* nu
mFaces) const { | 1601 bool SkTypeface_FreeType::Scanner::recognizedFont(SkStream* stream, int* numFace
s) const { |
1600 SkAutoMutexAcquire libraryLock(fLibraryMutex); | 1602 SkAutoMutexAcquire libraryLock(fLibraryMutex); |
1601 | 1603 |
1602 FT_StreamRec streamRec; | 1604 FT_StreamRec streamRec; |
1603 FT_Face face = this->openFace(stream, -1, &streamRec); | 1605 FT_Face face = this->openFace(stream, -1, &streamRec); |
1604 if (nullptr == face) { | 1606 if (nullptr == face) { |
1605 return false; | 1607 return false; |
1606 } | 1608 } |
1607 | 1609 |
1608 *numFaces = face->num_faces; | 1610 *numFaces = face->num_faces; |
1609 | 1611 |
1610 FT_Done_Face(face); | 1612 FT_Done_Face(face); |
1611 return true; | 1613 return true; |
1612 } | 1614 } |
1613 | 1615 |
1614 #include "SkTSearch.h" | 1616 #include "SkTSearch.h" |
1615 bool SkTypeface_FreeType::Scanner::scanFont( | 1617 bool SkTypeface_FreeType::Scanner::scanFont( |
1616 SkStreamAsset* stream, int ttcIndex, | 1618 SkStream* stream, int ttcIndex, |
1617 SkString* name, SkFontStyle* style, bool* isFixedPitch, AxisDefinitions* axe
s) const | 1619 SkString* name, SkFontStyle* style, bool* isFixedPitch, AxisDefinitions* axe
s) const |
1618 { | 1620 { |
1619 SkAutoMutexAcquire libraryLock(fLibraryMutex); | 1621 SkAutoMutexAcquire libraryLock(fLibraryMutex); |
1620 | 1622 |
1621 FT_StreamRec streamRec; | 1623 FT_StreamRec streamRec; |
1622 FT_Face face = this->openFace(stream, ttcIndex, &streamRec); | 1624 FT_Face face = this->openFace(stream, ttcIndex, &streamRec); |
1623 if (nullptr == face) { | 1625 if (nullptr == face) { |
1624 return false; | 1626 return false; |
1625 } | 1627 } |
1626 | 1628 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1767 SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\n", | 1769 SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\n", |
1768 name.c_str(), | 1770 name.c_str(), |
1769 (skTag >> 24) & 0xFF, | 1771 (skTag >> 24) & 0xFF, |
1770 (skTag >> 16) & 0xFF, | 1772 (skTag >> 16) & 0xFF, |
1771 (skTag >> 8) & 0xFF, | 1773 (skTag >> 8) & 0xFF, |
1772 (skTag) & 0xFF)); | 1774 (skTag) & 0xFF)); |
1773 } | 1775 } |
1774 } | 1776 } |
1775 ) | 1777 ) |
1776 } | 1778 } |
OLD | NEW |