Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Unified Diff: tools/using_skia_and_harfbuzz.cpp

Issue 2118833002: using_skia_and_harfbuzz: use default typeface (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « experimental/tools/generate-unicode-test-txt ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/using_skia_and_harfbuzz.cpp
diff --git a/tools/using_skia_and_harfbuzz.cpp b/tools/using_skia_and_harfbuzz.cpp
index 1694e962e0e438a3a86617fd5295ac0b9c2e3baf..01a5b026895e4f5c009b246d9db96c253ffc0537 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,
« no previous file with comments | « experimental/tools/generate-unicode-test-txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698