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 "SkFontDescriptor.h" | 8 #include "SkFontDescriptor.h" |
9 #include "SkFontHost_FreeType_common.h" | 9 #include "SkFontHost_FreeType_common.h" |
10 #include "SkFontMgr.h" | 10 #include "SkFontMgr.h" |
11 #include "SkFontMgr_custom.h" | 11 #include "SkFontMgr_custom.h" |
12 #include "SkFontStyle.h" | 12 #include "SkFontStyle.h" |
13 #include "SkMakeUnique.h" | |
14 #include "SkOSFile.h" | 13 #include "SkOSFile.h" |
15 #include "SkRefCnt.h" | 14 #include "SkRefCnt.h" |
16 #include "SkStream.h" | 15 #include "SkStream.h" |
17 #include "SkString.h" | 16 #include "SkString.h" |
18 #include "SkTArray.h" | 17 #include "SkTArray.h" |
19 #include "SkTemplates.h" | 18 #include "SkTemplates.h" |
20 #include "SkTypeface.h" | 19 #include "SkTypeface.h" |
21 #include "SkTypefaceCache.h" | 20 #include "SkTypefaceCache.h" |
22 #include "SkTypes.h" | 21 #include "SkTypes.h" |
23 | 22 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 SkTypeface_Stream(std::unique_ptr<SkFontData> fontData, | 77 SkTypeface_Stream(std::unique_ptr<SkFontData> fontData, |
79 const SkFontStyle& style, bool isFixedPitch, bool sysFont, | 78 const SkFontStyle& style, bool isFixedPitch, bool sysFont, |
80 const SkString familyName) | 79 const SkString familyName) |
81 : INHERITED(style, isFixedPitch, sysFont, familyName, fontData->getIndex
()) | 80 : INHERITED(style, isFixedPitch, sysFont, familyName, fontData->getIndex
()) |
82 , fData(std::move(fontData)) | 81 , fData(std::move(fontData)) |
83 { } | 82 { } |
84 | 83 |
85 protected: | 84 protected: |
86 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 85 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
87 *ttcIndex = fData->getIndex(); | 86 *ttcIndex = fData->getIndex(); |
88 return fData->getStream()->duplicate(); | 87 return fData->duplicateStream(); |
89 } | 88 } |
90 | 89 |
91 std::unique_ptr<SkFontData> onMakeFontData() const override { | 90 SkFontData* onCreateFontData() const override { |
92 return skstd::make_unique<SkFontData>(*fData); | 91 return new SkFontData(*fData.get()); |
93 } | 92 } |
94 | 93 |
95 private: | 94 private: |
96 const std::unique_ptr<const SkFontData> fData; | 95 std::unique_ptr<const SkFontData> fData; |
97 | 96 |
98 typedef SkTypeface_Custom INHERITED; | 97 typedef SkTypeface_Custom INHERITED; |
99 }; | 98 }; |
100 | 99 |
101 /** The file SkTypeface implementation for the custom font manager. */ | 100 /** The file SkTypeface implementation for the custom font manager. */ |
102 class SkTypeface_File : public SkTypeface_Custom { | 101 class SkTypeface_File : public SkTypeface_Custom { |
103 public: | 102 public: |
104 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont, | 103 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont, |
105 const SkString familyName, const char path[], int index) | 104 const SkString familyName, const char path[], int index) |
106 : INHERITED(style, isFixedPitch, sysFont, familyName, index) | 105 : INHERITED(style, isFixedPitch, sysFont, familyName, index) |
107 , fPath(path) | 106 , fPath(path) |
108 { } | 107 { } |
109 | 108 |
110 protected: | 109 protected: |
111 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 110 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
112 *ttcIndex = this->getIndex(); | 111 *ttcIndex = this->getIndex(); |
113 return SkStream::MakeFromFile(fPath.c_str()).release(); | 112 return SkStream::NewFromFile(fPath.c_str()); |
114 } | 113 } |
115 | 114 |
116 private: | 115 private: |
117 SkString fPath; | 116 SkString fPath; |
118 | 117 |
119 typedef SkTypeface_Custom INHERITED; | 118 typedef SkTypeface_Custom INHERITED; |
120 }; | 119 }; |
121 | 120 |
122 /////////////////////////////////////////////////////////////////////////////// | 121 /////////////////////////////////////////////////////////////////////////////// |
123 | 122 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { | 263 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { |
265 return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIn
dex); | 264 return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIn
dex); |
266 } | 265 } |
267 | 266 |
268 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { | 267 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { |
269 return this->createFromStream(bareStream, FontParameters().setCollection
Index(ttcIndex)); | 268 return this->createFromStream(bareStream, FontParameters().setCollection
Index(ttcIndex)); |
270 } | 269 } |
271 | 270 |
272 SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& param
s) const override { | 271 SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& param
s) const override { |
273 using Scanner = SkTypeface_FreeType::Scanner; | 272 using Scanner = SkTypeface_FreeType::Scanner; |
274 std::unique_ptr<SkStreamAsset> stream(s); | 273 SkAutoTDelete<SkStreamAsset> stream(s); |
275 bool isFixedPitch; | 274 bool isFixedPitch; |
276 SkFontStyle style; | 275 SkFontStyle style; |
277 SkString name; | 276 SkString name; |
278 Scanner::AxisDefinitions axisDefinitions; | 277 Scanner::AxisDefinitions axisDefinitions; |
279 if (!fScanner.scanFont(stream.get(), params.getCollectionIndex(), | 278 if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &styl
e, &isFixedPitch, |
280 &name, &style, &isFixedPitch, &axisDefinitions)) | 279 &axisDefinitions)) |
281 { | 280 { |
282 return nullptr; | 281 return nullptr; |
283 } | 282 } |
284 | 283 |
285 int paramAxisCount; | 284 int paramAxisCount; |
286 const FontParameters::Axis* paramAxes = params.getAxes(¶mAxisCount); | 285 const FontParameters::Axis* paramAxes = params.getAxes(¶mAxisCount); |
287 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); | 286 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); |
288 Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, a
xisValues, name); | 287 Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, a
xisValues, name); |
289 | 288 |
290 auto data = skstd::make_unique<SkFontData>(std::move(stream), params.get
CollectionIndex(), | 289 std::unique_ptr<SkFontData> data(new SkFontData(stream.release(), |
291 axisValues.get(), axisDefinit
ions.count()); | 290 params.getCollectionInde
x(), |
| 291 axisValues.get(), axisDe
finitions.count())); |
292 return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false
, name); | 292 return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false
, name); |
293 } | 293 } |
294 | 294 |
295 SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData> data) const ove
rride { | 295 SkTypeface* onCreateFromFontData(SkFontData* data) const override { |
296 bool isFixedPitch; | 296 bool isFixedPitch; |
297 SkFontStyle style; | 297 SkFontStyle style; |
298 SkString name; | 298 SkString name; |
299 if (!fScanner.scanFont(data->getStream(), data->getIndex(), | 299 if (!fScanner.scanFont(data->getStream(), data->getIndex(), |
300 &name, &style, &isFixedPitch, nullptr)) | 300 &name, &style, &isFixedPitch, nullptr)) |
301 { | 301 { |
302 return nullptr; | 302 return nullptr; |
303 } | 303 } |
304 return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false
, name); | 304 std::unique_ptr<SkFontData> unique_data(data); |
| 305 return new SkTypeface_Stream(std::move(unique_data), style, isFixedPitch
, false, name); |
305 } | 306 } |
306 | 307 |
307 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ | 308 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ |
308 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path); | 309 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); |
309 return stream.get() ? this->createFromStream(stream.release(), ttcIndex)
: nullptr; | 310 return stream.get() ? this->createFromStream(stream.release(), ttcIndex)
: nullptr; |
310 } | 311 } |
311 | 312 |
312 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle styl
e) const override { | 313 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle styl
e) const override { |
313 SkTypeface* tf = nullptr; | 314 SkTypeface* tf = nullptr; |
314 | 315 |
315 if (familyName) { | 316 if (familyName) { |
316 tf = this->onMatchFamilyStyle(familyName, style); | 317 tf = this->onMatchFamilyStyle(familyName, style); |
317 } | 318 } |
318 | 319 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 | 365 |
365 static void load_directory_fonts(const SkTypeface_FreeType::Scanner& scanner
, | 366 static void load_directory_fonts(const SkTypeface_FreeType::Scanner& scanner
, |
366 const SkString& directory, const char* suff
ix, | 367 const SkString& directory, const char* suff
ix, |
367 SkFontMgr_Custom::Families* families) | 368 SkFontMgr_Custom::Families* families) |
368 { | 369 { |
369 SkOSFile::Iter iter(directory.c_str(), suffix); | 370 SkOSFile::Iter iter(directory.c_str(), suffix); |
370 SkString name; | 371 SkString name; |
371 | 372 |
372 while (iter.next(&name, false)) { | 373 while (iter.next(&name, false)) { |
373 SkString filename(SkOSPath::Join(directory.c_str(), name.c_str())); | 374 SkString filename(SkOSPath::Join(directory.c_str(), name.c_str())); |
374 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(filen
ame.c_str()); | 375 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(filename.c_str(
))); |
375 if (!stream) { | 376 if (!stream.get()) { |
376 SkDebugf("---- failed to open <%s>\n", filename.c_str()); | 377 SkDebugf("---- failed to open <%s>\n", filename.c_str()); |
377 continue; | 378 continue; |
378 } | 379 } |
379 | 380 |
380 int numFaces; | 381 int numFaces; |
381 if (!scanner.recognizedFont(stream.get(), &numFaces)) { | 382 if (!scanner.recognizedFont(stream, &numFaces)) { |
382 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str(
)); | 383 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str(
)); |
383 continue; | 384 continue; |
384 } | 385 } |
385 | 386 |
386 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { | 387 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { |
387 bool isFixedPitch; | 388 bool isFixedPitch; |
388 SkString realname; | 389 SkString realname; |
389 SkFontStyle style = SkFontStyle(); // avoid uninitialized warnin
g | 390 SkFontStyle style = SkFontStyle(); // avoid uninitialized warnin
g |
390 if (!scanner.scanFont(stream.get(), faceIndex, | 391 if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isF
ixedPitch, nullptr)) { |
391 &realname, &style, &isFixedPitch, nullptr)
) | |
392 { | |
393 SkDebugf("---- failed to open <%s> <%d> as a font\n", | 392 SkDebugf("---- failed to open <%s> <%d> as a font\n", |
394 filename.c_str(), faceIndex); | 393 filename.c_str(), faceIndex); |
395 continue; | 394 continue; |
396 } | 395 } |
397 | 396 |
398 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c
_str()); | 397 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c
_str()); |
399 if (nullptr == addTo) { | 398 if (nullptr == addTo) { |
400 addTo = new SkFontStyleSet_Custom(realname); | 399 addTo = new SkFontStyleSet_Custom(realname); |
401 families->push_back().reset(addTo); | 400 families->push_back().reset(addTo); |
402 } | 401 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 return families[i].get(); | 455 return families[i].get(); |
457 } | 456 } |
458 } | 457 } |
459 return nullptr; | 458 return nullptr; |
460 } | 459 } |
461 | 460 |
462 static void load_embedded_font(const SkTypeface_FreeType::Scanner& scanner, | 461 static void load_embedded_font(const SkTypeface_FreeType::Scanner& scanner, |
463 const uint8_t* data, size_t size, int index, | 462 const uint8_t* data, size_t size, int index, |
464 SkFontMgr_Custom::Families* families) | 463 SkFontMgr_Custom::Families* families) |
465 { | 464 { |
466 auto stream = skstd::make_unique<SkMemoryStream>(data, size, false); | 465 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(data, size, fals
e)); |
467 | 466 |
468 int numFaces; | 467 int numFaces; |
469 if (!scanner.recognizedFont(stream.get(), &numFaces)) { | 468 if (!scanner.recognizedFont(stream, &numFaces)) { |
470 SkDebugf("---- failed to open <%d> as a font\n", index); | 469 SkDebugf("---- failed to open <%d> as a font\n", index); |
471 return; | 470 return; |
472 } | 471 } |
473 | 472 |
474 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { | 473 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { |
475 bool isFixedPitch; | 474 bool isFixedPitch; |
476 SkString realname; | 475 SkString realname; |
477 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning | 476 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning |
478 if (!scanner.scanFont(stream.get(), faceIndex, | 477 if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixed
Pitch, nullptr)) { |
479 &realname, &style, &isFixedPitch, nullptr)) | |
480 { | |
481 SkDebugf("---- failed to open <%d> <%d> as a font\n", index, fac
eIndex); | 478 SkDebugf("---- failed to open <%d> <%d> as a font\n", index, fac
eIndex); |
482 return; | 479 return; |
483 } | 480 } |
484 | 481 |
485 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str
()); | 482 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str
()); |
486 if (nullptr == addTo) { | 483 if (nullptr == addTo) { |
487 addTo = new SkFontStyleSet_Custom(realname); | 484 addTo = new SkFontStyleSet_Custom(realname); |
488 families->push_back().reset(addTo); | 485 families->push_back().reset(addTo); |
489 } | 486 } |
490 auto data = skstd::make_unique<SkFontData>(std::move(stream), faceIn
dex, nullptr, 0); | 487 std::unique_ptr<SkFontData> data( |
| 488 new SkFontData(stream.release(), faceIndex, nullptr, 0)); |
491 addTo->appendTypeface(sk_make_sp<SkTypeface_Stream>(std::move(data), | 489 addTo->appendTypeface(sk_make_sp<SkTypeface_Stream>(std::move(data), |
492 style, isFixedPi
tch, | 490 style, isFixedPi
tch, |
493 true, realname))
; | 491 true, realname))
; |
494 } | 492 } |
495 } | 493 } |
496 | 494 |
497 const SkEmbeddedResourceHeader* fHeader; | 495 const SkEmbeddedResourceHeader* fHeader; |
498 }; | 496 }; |
499 | 497 |
500 SkFontMgr* SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header)
{ | 498 SkFontMgr* SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header)
{ |
(...skipping 12 matching lines...) Expand all Loading... |
513 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()); | 511 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()); |
514 families->push_back().reset(family); | 512 families->push_back().reset(family); |
515 family->appendTypeface(sk_make_sp<SkTypeface_Empty>()); | 513 family->appendTypeface(sk_make_sp<SkTypeface_Empty>()); |
516 } | 514 } |
517 | 515 |
518 }; | 516 }; |
519 | 517 |
520 SK_API SkFontMgr* SkFontMgr_New_Custom_Empty() { | 518 SK_API SkFontMgr* SkFontMgr_New_Custom_Empty() { |
521 return new SkFontMgr_Custom(EmptyFontLoader()); | 519 return new SkFontMgr_Custom(EmptyFontLoader()); |
522 } | 520 } |
OLD | NEW |