halcanary | 00d44e0 | 2016-05-03 15:09:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
halcanary | 85e38bc | 2016-05-04 09:34:25 -0700 | [diff] [blame^] | 8 | // This sample progam demonstrates how to use Skia and HarfBuzz to |
| 9 | // produce a PDF file from UTF-8 text in stdin. |
halcanary | 00d44e0 | 2016-05-03 15:09:52 -0700 | [diff] [blame] | 10 | |
| 11 | #include <cassert> |
| 12 | #include <iostream> |
| 13 | #include <map> |
| 14 | #include <string> |
| 15 | |
halcanary | 85e38bc | 2016-05-04 09:34:25 -0700 | [diff] [blame^] | 16 | #include <hb-ot.h> |
| 17 | |
| 18 | #include "SkCanvas.h" |
| 19 | #include "SkDocument.h" |
| 20 | #include "SkStream.h" |
| 21 | #include "SkTextBlob.h" |
| 22 | #include "SkTypeface.h" |
halcanary | 00d44e0 | 2016-05-03 15:09:52 -0700 | [diff] [blame] | 23 | |
| 24 | struct BaseOption { |
| 25 | std::string selector; |
| 26 | std::string description; |
| 27 | virtual void set(std::string _value) = 0; |
| 28 | virtual std::string valueToString() = 0; |
| 29 | |
| 30 | BaseOption(std::string _selector, std::string _description) : |
| 31 | selector(_selector), |
| 32 | description(_description) {} |
| 33 | |
| 34 | virtual ~BaseOption() {} |
| 35 | }; |
| 36 | |
| 37 | template <class T> struct Option : BaseOption { |
| 38 | T value; |
| 39 | Option(std::string selector, std::string description, T defaultValue) : |
| 40 | BaseOption(selector, description), |
| 41 | value(defaultValue) {} |
| 42 | }; |
| 43 | |
| 44 | struct DoubleOption : Option<double> { |
| 45 | virtual void set(std::string _value) { |
| 46 | value = atof(_value.c_str()); |
| 47 | } |
| 48 | virtual std::string valueToString() { |
| 49 | return std::to_string(value); |
| 50 | } |
| 51 | DoubleOption(std::string selector, std::string description, double defaultValue) : |
| 52 | Option<double>(selector, description, defaultValue) {} |
| 53 | }; |
| 54 | |
| 55 | struct SkStringOption : Option<SkString> { |
| 56 | virtual void set(std::string _value) { |
| 57 | value = _value.c_str(); |
| 58 | } |
| 59 | virtual std::string valueToString() { |
| 60 | return value.c_str(); |
| 61 | } |
| 62 | SkStringOption(std::string selector, std::string description, SkString defaultValue) : |
| 63 | Option<SkString>(selector, description, defaultValue) {} |
| 64 | }; |
| 65 | |
| 66 | struct StdStringOption : Option<std::string> { |
| 67 | virtual void set(std::string _value) { |
| 68 | value = _value; |
| 69 | } |
| 70 | virtual std::string valueToString() { |
| 71 | return value; |
| 72 | } |
| 73 | StdStringOption(std::string selector, std::string description, std::string defaultValue) : |
| 74 | Option<std::string>(selector, description, defaultValue) {} |
| 75 | }; |
| 76 | |
| 77 | struct Config { |
| 78 | DoubleOption *page_width = new DoubleOption("-w", "Page width", 600.0f); |
| 79 | DoubleOption *page_height = new DoubleOption("-h", "Page height", 800.0f); |
| 80 | SkStringOption *title = new SkStringOption("-t", "PDF title", SkString("---")); |
| 81 | SkStringOption *author = new SkStringOption("-a", "PDF author", SkString("---")); |
| 82 | SkStringOption *subject = new SkStringOption("-k", "PDF subject", SkString("---")); |
| 83 | SkStringOption *keywords = new SkStringOption("-c", "PDF keywords", SkString("---")); |
| 84 | SkStringOption *creator = new SkStringOption("-t", "PDF creator", SkString("---")); |
| 85 | StdStringOption *font_file = new StdStringOption("-f", ".ttf font file", "fonts/DejaVuSans.ttf"); |
| 86 | DoubleOption *font_size = new DoubleOption("-z", "Font size", 8.0f); |
| 87 | DoubleOption *left_margin = new DoubleOption("-m", "Left margin", 20.0f); |
| 88 | DoubleOption *line_spacing_ratio = new DoubleOption("-h", "Line spacing ratio", 1.5f); |
| 89 | StdStringOption *output_file_name = new StdStringOption("-o", ".pdf output file name", "out-skiahf.pdf"); |
| 90 | |
| 91 | std::map<std::string, BaseOption*> options = { |
| 92 | { page_width->selector, page_width }, |
| 93 | { page_height->selector, page_height }, |
| 94 | { title->selector, title }, |
| 95 | { author->selector, author }, |
| 96 | { subject->selector, subject }, |
| 97 | { keywords->selector, keywords }, |
| 98 | { creator->selector, creator }, |
| 99 | { font_file->selector, font_file }, |
| 100 | { font_size->selector, font_size }, |
| 101 | { left_margin->selector, left_margin }, |
| 102 | { line_spacing_ratio->selector, line_spacing_ratio }, |
| 103 | { output_file_name->selector, output_file_name }, |
| 104 | }; |
| 105 | |
| 106 | Config(int argc, char **argv) { |
| 107 | for (int i = 1; i < argc; i++) { |
| 108 | std::string option_selector(argv[i]); |
| 109 | auto it = options.find(option_selector); |
| 110 | if (it != options.end()) { |
| 111 | if (i >= argc) { |
| 112 | break; |
| 113 | } |
| 114 | const char *option_value = argv[i + 1]; |
| 115 | it->second->set(option_value); |
| 116 | i++; |
| 117 | } else { |
| 118 | printf("Ignoring unrecognized option: %s.\n", argv[i]); |
| 119 | printf("Usage: %s {option value}\n", argv[0]); |
| 120 | printf("\tTakes text from stdin and produces pdf file.\n"); |
| 121 | printf("Supported options:\n"); |
| 122 | for (auto it = options.begin(); it != options.end(); ++it) { |
| 123 | printf("\t%s\t%s (%s)\n", it->first.c_str(), |
| 124 | it->second->description.c_str(), |
| 125 | it->second->valueToString().c_str()); |
| 126 | } |
| 127 | exit(-1); |
| 128 | } |
| 129 | } |
| 130 | } // end of Config::Config |
| 131 | }; |
| 132 | |
| 133 | const double FONT_SIZE_SCALE = 64.0f; |
| 134 | |
| 135 | struct Face { |
| 136 | struct HBFDel { void operator()(hb_face_t* f) { hb_face_destroy(f); } }; |
| 137 | std::unique_ptr<hb_face_t, HBFDel> fHarfBuzzFace; |
| 138 | sk_sp<SkTypeface> fSkiaTypeface; |
| 139 | |
| 140 | Face(const char* path, int index) { |
| 141 | // fairly portable mmap impl |
| 142 | auto data = SkData::MakeFromFileName(path); |
| 143 | assert(data); |
| 144 | if (!data) { return; } |
| 145 | fSkiaTypeface.reset( |
| 146 | SkTypeface::CreateFromStream( |
| 147 | new SkMemoryStream(data), index)); |
| 148 | assert(fSkiaTypeface); |
| 149 | if (!fSkiaTypeface) { return; } |
| 150 | auto destroy = [](void *d) { static_cast<SkData*>(d)->unref(); }; |
| 151 | const char* bytes = (const char*)data->data(); |
| 152 | unsigned int size = (unsigned int)data->size(); |
| 153 | hb_blob_t* blob = hb_blob_create(bytes, |
| 154 | size, |
| 155 | HB_MEMORY_MODE_READONLY, |
| 156 | data.release(), |
| 157 | destroy); |
| 158 | assert(blob); |
| 159 | hb_blob_make_immutable(blob); |
| 160 | hb_face_t* face = hb_face_create(blob, (unsigned)index); |
| 161 | hb_blob_destroy(blob); |
| 162 | assert(face); |
| 163 | if (!face) { |
| 164 | fSkiaTypeface.reset(); |
| 165 | return; |
| 166 | } |
| 167 | hb_face_set_index(face, (unsigned)index); |
| 168 | hb_face_set_upem(face, fSkiaTypeface->getUnitsPerEm()); |
| 169 | fHarfBuzzFace.reset(face); |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | class Placement { |
| 174 | public: |
| 175 | Placement(Config &_config, SkWStream* outputStream) : config(_config) { |
| 176 | face = new Face(config.font_file->value.c_str(), 0 /* index */); |
| 177 | hb_font = hb_font_create(face->fHarfBuzzFace.get()); |
| 178 | |
| 179 | hb_font_set_scale(hb_font, |
| 180 | FONT_SIZE_SCALE * config.font_size->value, |
| 181 | FONT_SIZE_SCALE * config.font_size->value); |
| 182 | hb_ot_font_set_funcs(hb_font); |
| 183 | |
| 184 | SkDocument::PDFMetadata pdf_info; |
| 185 | pdf_info.fTitle = config.title->value; |
| 186 | pdf_info.fAuthor = config.author->value; |
| 187 | pdf_info.fSubject = config.subject->value; |
| 188 | pdf_info.fKeywords = config.keywords->value; |
| 189 | pdf_info.fCreator = config.creator->value; |
| 190 | SkTime::DateTime now; |
| 191 | SkTime::GetDateTime(&now); |
| 192 | pdf_info.fCreation.fEnabled = true; |
| 193 | pdf_info.fCreation.fDateTime = now; |
| 194 | pdf_info.fModified.fEnabled = true; |
| 195 | pdf_info.fModified.fDateTime = now; |
| 196 | pdfDocument = SkDocument::MakePDF(outputStream, SK_ScalarDefaultRasterDPI, |
| 197 | pdf_info, nullptr, true); |
| 198 | assert(pdfDocument); |
| 199 | |
| 200 | white_paint.setColor(SK_ColorWHITE); |
| 201 | |
| 202 | glyph_paint.setFlags( |
| 203 | SkPaint::kAntiAlias_Flag | |
| 204 | SkPaint::kSubpixelText_Flag); // ... avoid waggly text when rotating. |
| 205 | glyph_paint.setColor(SK_ColorBLACK); |
| 206 | glyph_paint.setTextSize(config.font_size->value); |
halcanary | 00d44e0 | 2016-05-03 15:09:52 -0700 | [diff] [blame] | 207 | glyph_paint.setTypeface(face->fSkiaTypeface); |
| 208 | glyph_paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 209 | |
| 210 | NewPage(); |
| 211 | } // end of Placement |
| 212 | |
| 213 | ~Placement() { |
| 214 | delete face; |
| 215 | hb_font_destroy (hb_font); |
| 216 | } |
| 217 | |
| 218 | void WriteLine(const char *text) { |
| 219 | /* Create hb-buffer and populate. */ |
| 220 | hb_buffer_t *hb_buffer = hb_buffer_create (); |
| 221 | hb_buffer_add_utf8 (hb_buffer, text, -1, 0, -1); |
| 222 | hb_buffer_guess_segment_properties (hb_buffer); |
| 223 | |
| 224 | /* Shape it! */ |
| 225 | hb_shape (hb_font, hb_buffer, NULL, 0); |
| 226 | |
| 227 | DrawGlyphs(hb_buffer); |
| 228 | |
| 229 | hb_buffer_destroy (hb_buffer); |
| 230 | |
| 231 | // Advance to the next line. |
| 232 | current_y += config.line_spacing_ratio->value * config.font_size->value; |
| 233 | if (current_y > config.page_height->value) { |
| 234 | pdfDocument->endPage(); |
| 235 | NewPage(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | bool Close() { |
| 240 | return pdfDocument->close(); |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | Config config; |
| 245 | |
| 246 | Face *face; |
| 247 | |
| 248 | hb_font_t *hb_font; |
| 249 | |
| 250 | sk_sp<SkDocument> pdfDocument; |
| 251 | |
| 252 | SkCanvas* pageCanvas; |
| 253 | |
| 254 | SkPaint white_paint; |
| 255 | SkPaint glyph_paint; |
| 256 | |
| 257 | double current_x; |
| 258 | double current_y; |
| 259 | |
| 260 | void NewPage() { |
| 261 | pageCanvas = pdfDocument->beginPage(config.page_width->value, config.page_height->value); |
| 262 | |
| 263 | pageCanvas->drawPaint(white_paint); |
| 264 | |
| 265 | current_x = config.left_margin->value; |
| 266 | current_y = config.line_spacing_ratio->value * config.font_size->value; |
| 267 | } |
| 268 | |
| 269 | bool DrawGlyphs(hb_buffer_t *hb_buffer) { |
| 270 | SkTextBlobBuilder textBlobBuilder; |
| 271 | unsigned len = hb_buffer_get_length (hb_buffer); |
| 272 | if (len == 0) { |
| 273 | return true; |
| 274 | } |
| 275 | hb_glyph_info_t *info = hb_buffer_get_glyph_infos (hb_buffer, NULL); |
| 276 | hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (hb_buffer, NULL); |
| 277 | auto runBuffer = textBlobBuilder.allocRunPos(glyph_paint, len); |
| 278 | |
| 279 | double x = 0; |
| 280 | double y = 0; |
| 281 | for (unsigned int i = 0; i < len; i++) |
| 282 | { |
| 283 | runBuffer.glyphs[i] = info[i].codepoint; |
| 284 | reinterpret_cast<SkPoint*>(runBuffer.pos)[i] = SkPoint::Make( |
| 285 | x + pos[i].x_offset / FONT_SIZE_SCALE, |
| 286 | y - pos[i].y_offset / FONT_SIZE_SCALE); |
| 287 | x += pos[i].x_advance / FONT_SIZE_SCALE; |
| 288 | y += pos[i].y_advance / FONT_SIZE_SCALE; |
| 289 | } |
| 290 | |
| 291 | pageCanvas->drawTextBlob(textBlobBuilder.build(), current_x, current_y, glyph_paint); |
| 292 | return true; |
| 293 | } // end of DrawGlyphs |
| 294 | }; // end of Placement class |
| 295 | |
| 296 | int main(int argc, char** argv) { |
| 297 | Config config(argc, argv); |
| 298 | |
| 299 | Placement placement(config, new SkFILEWStream(config.output_file_name->value.c_str())); |
| 300 | for (std::string line; std::getline(std::cin, line);) { |
| 301 | placement.WriteLine(line.c_str()); |
| 302 | } |
| 303 | placement.Close(); |
| 304 | |
| 305 | return 0; |
| 306 | } |