diff options
author | halcanary <halcanary@google.com> | 2016-07-12 09:17:43 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-07-12 09:17:43 -0700 |
commit | f119796f95f336d5b29f06f369b15b48a6a0df45 (patch) | |
tree | 6be08da85f0ccfd7407710fd9727a6e9559d9cc9 | |
parent | 7f2c85e66bc45ef5fc204e301ed726491160a615 (diff) |
tools/SkShaper: SkStream->blob
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2142023002
Review-Url: https://codereview.chromium.org/2142023002
-rwxr-xr-x | experimental/tools/generate-unicode-test-txt | 2 | ||||
-rw-r--r-- | tools/SkShaper.cpp | 44 |
2 files changed, 32 insertions, 14 deletions
diff --git a/experimental/tools/generate-unicode-test-txt b/experimental/tools/generate-unicode-test-txt index f105254705..76de70ec25 100755 --- a/experimental/tools/generate-unicode-test-txt +++ b/experimental/tools/generate-unicode-test-txt @@ -41,7 +41,7 @@ valid_codepoint_data = ''' ''' if sys.version_info[0] < 3: def to_unicode(i): - return unichr(i).encode('UTF-8') + return ('\U%08x' % i).decode('unicode-escape').encode('UTF-8') else: def to_unicode(i): return chr(i) diff --git a/tools/SkShaper.cpp b/tools/SkShaper.cpp index 377f07c109..44dd8fc7f1 100644 --- a/tools/SkShaper.cpp +++ b/tools/SkShaper.cpp @@ -14,6 +14,32 @@ static const int FONT_SIZE_SCALE = 512; +namespace { +struct HBFBlobDel { + void operator()(hb_blob_t* b) { hb_blob_destroy(b); } +}; + +std::unique_ptr<hb_blob_t, HBFBlobDel> stream_to_blob(std::unique_ptr<SkStreamAsset> asset) { + size_t size = asset->getLength(); + std::unique_ptr<hb_blob_t, HBFBlobDel> blob; + if (const void* base = asset->getMemoryBase()) { + blob.reset(hb_blob_create((char*)base, size, + HB_MEMORY_MODE_READONLY, asset.release(), + [](void* p) { delete (SkStreamAsset*)p; })); + } else { + // SkDebugf("Extra SkStreamAsset copy\n"); + SkAutoMalloc autoMalloc(size); + asset->read(autoMalloc.get(), size); + void* ptr = autoMalloc.get(); + blob.reset(hb_blob_create((char*)autoMalloc.release(), size, + HB_MEMORY_MODE_READONLY, ptr, sk_free)); + } + SkASSERT(blob); + hb_blob_make_immutable(blob.get()); + return blob; +} +} // namespace + struct SkShaper::Impl { struct HBFontDel { void operator()(hb_font_t* f) { hb_font_destroy(f); } @@ -29,22 +55,14 @@ struct SkShaper::Impl { SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) { fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault(); int index; - std::unique_ptr<SkStreamAsset> asset(fImpl->fTypeface->openStream(&index)); - size_t size = asset->getLength(); - SkAutoMalloc autoMalloc(size); // TODO(halcanary): Avoid this malloc+copy. - asset->read(autoMalloc.get(), size); - asset = nullptr; - void* ptr = autoMalloc.get(); - hb_blob_t* blob = hb_blob_create((char*)autoMalloc.release(), size, - HB_MEMORY_MODE_READONLY, ptr, sk_free); - SkASSERT(blob); - hb_blob_make_immutable(blob); - + std::unique_ptr<hb_blob_t, HBFBlobDel> blob( + stream_to_blob(std::unique_ptr<SkStreamAsset>( + fImpl->fTypeface->openStream(&index)))); struct HBFaceDel { void operator()(hb_face_t* f) { hb_face_destroy(f); } }; - std::unique_ptr<hb_face_t, HBFaceDel> face(hb_face_create(blob, (unsigned)index)); - hb_blob_destroy(blob); + std::unique_ptr<hb_face_t, HBFaceDel> face( + hb_face_create(blob.get(), (unsigned)index)); SkASSERT(face); if (!face) { return; |