aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/using_skia_and_harfbuzz.cpp
diff options
context:
space:
mode:
authorGravatar halcanary <halcanary@google.com>2016-07-01 08:48:12 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-07-01 08:48:12 -0700
commit627ad6db4c9caca7fcd3af64bba82b96ee38e49c (patch)
tree2399a23bd5a3412cc5eb589738de4c8f6045b46c /tools/using_skia_and_harfbuzz.cpp
parentb3058bdb1049ca75d526eb9f11e1a42a49e63585 (diff)
using_skia_and_harfbuzz: use default typeface
Also: add little script to generate utf-8 test text. BUG=skia:5434 TBR=bungeman@google.com Review-Url: https://codereview.chromium.org/2118833002
Diffstat (limited to 'tools/using_skia_and_harfbuzz.cpp')
-rw-r--r--tools/using_skia_and_harfbuzz.cpp34
1 files changed, 32 insertions, 2 deletions
diff --git a/tools/using_skia_and_harfbuzz.cpp b/tools/using_skia_and_harfbuzz.cpp
index 1694e962e0..01a5b02689 100644
--- a/tools/using_skia_and_harfbuzz.cpp
+++ b/tools/using_skia_and_harfbuzz.cpp
@@ -86,7 +86,7 @@ struct Config {
SkStringOption *subject = new SkStringOption("-k", "PDF subject", SkString("---"));
SkStringOption *keywords = new SkStringOption("-c", "PDF keywords", SkString("---"));
SkStringOption *creator = new SkStringOption("-t", "PDF creator", SkString("---"));
- StdStringOption *font_file = new StdStringOption("-f", ".ttf font file", "fonts/DejaVuSans.ttf");
+ StdStringOption *font_file = new StdStringOption("-f", ".ttf font file", "");
DoubleOption *font_size = new DoubleOption("-z", "Font size", 8.0f);
DoubleOption *left_margin = new DoubleOption("-m", "Left margin", 20.0f);
DoubleOption *line_spacing_ratio = new DoubleOption("-h", "Line spacing ratio", 1.5f);
@@ -141,6 +141,31 @@ struct Face {
std::unique_ptr<hb_face_t, HBFDel> fHarfBuzzFace;
sk_sp<SkTypeface> fSkiaTypeface;
+ Face(sk_sp<SkTypeface> skiaTypeface) : fSkiaTypeface(std::move(skiaTypeface)) {
+ int index;
+ std::unique_ptr<SkStreamAsset> asset(fSkiaTypeface->openStream(&index));
+ size_t size = asset->getLength();
+ // TODO(halcanary): avoid this malloc and copy.
+ char* buffer = (char*)malloc(size);
+ asset->read(buffer, size);
+ hb_blob_t* blob = hb_blob_create(buffer,
+ size,
+ HB_MEMORY_MODE_READONLY,
+ nullptr,
+ free);
+ assert(blob);
+ hb_blob_make_immutable(blob);
+ hb_face_t* face = hb_face_create(blob, (unsigned)index);
+ hb_blob_destroy(blob);
+ assert(face);
+ if (!face) {
+ fSkiaTypeface.reset();
+ return;
+ }
+ hb_face_set_index(face, (unsigned)index);
+ hb_face_set_upem(face, fSkiaTypeface->getUnitsPerEm());
+ fHarfBuzzFace.reset(face);
+ }
Face(const char* path, int index) {
// fairly portable mmap impl
auto data = SkData::MakeFromFileName(path);
@@ -175,7 +200,12 @@ struct Face {
class Placement {
public:
Placement(Config &_config, SkWStream* outputStream) : config(_config) {
- face = new Face(config.font_file->value.c_str(), 0 /* index */);
+ const std::string& font_file = config.font_file->value;
+ if (font_file.size() > 0) {
+ face = new Face(font_file.c_str(), 0 /* index */);
+ } else {
+ face = new Face(SkTypeface::MakeDefault());
+ }
hb_font = hb_font_create(face->fHarfBuzzFace.get());
hb_font_set_scale(hb_font,