2016-05-03 22:09:52 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
|
* found in the LICENSE file.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-05-04 16:34:25 +00:00
|
|
|
|
// This sample progam demonstrates how to use Skia and HarfBuzz to
|
|
|
|
|
// produce a PDF file from UTF-8 text in stdin.
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
|
|
|
|
#include <cassert>
|
2016-05-12 17:09:58 +00:00
|
|
|
|
#include <cstdlib>
|
2016-05-03 22:09:52 +00:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <map>
|
2016-05-12 17:09:58 +00:00
|
|
|
|
#include <sstream>
|
2016-07-11 21:30:39 +00:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2016-05-04 16:34:25 +00:00
|
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
2018-09-07 18:33:14 +00:00
|
|
|
|
#include "SkPDFDocument.h"
|
2016-07-11 21:30:39 +00:00
|
|
|
|
#include "SkShaper.h"
|
2016-05-04 16:34:25 +00:00
|
|
|
|
#include "SkStream.h"
|
|
|
|
|
#include "SkTextBlob.h"
|
|
|
|
|
#include "SkTypeface.h"
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
// Options /////////////////////////////////////////////////////////////////////
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
struct BaseOption {
|
|
|
|
|
std::string selector;
|
|
|
|
|
std::string description;
|
|
|
|
|
virtual void set(std::string _value) = 0;
|
|
|
|
|
virtual std::string valueToString() = 0;
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
BaseOption(std::string _selector, std::string _description)
|
|
|
|
|
: selector(_selector), description(_description) {}
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
virtual ~BaseOption() {}
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
static void Init(const std::vector<BaseOption*> &, int argc, char **argv);
|
2016-05-03 22:09:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
template <class T>
|
|
|
|
|
struct Option : BaseOption {
|
|
|
|
|
T value;
|
2018-11-30 16:24:47 +00:00
|
|
|
|
Option(std::string _selector, std::string _description, T defaultValue)
|
|
|
|
|
: BaseOption(_selector, _description), value(defaultValue) {}
|
2016-05-03 22:09:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
void BaseOption::Init(const std::vector<BaseOption*> &option_list,
|
|
|
|
|
int argc, char **argv) {
|
|
|
|
|
std::map<std::string, BaseOption *> options;
|
|
|
|
|
for (BaseOption *opt : option_list) {
|
|
|
|
|
options[opt->selector] = opt;
|
|
|
|
|
}
|
2016-05-03 22:09:52 +00:00
|
|
|
|
for (int i = 1; i < argc; i++) {
|
2016-07-11 21:30:39 +00:00
|
|
|
|
std::string option_selector(argv[i]);
|
|
|
|
|
auto it = options.find(option_selector);
|
|
|
|
|
if (it != options.end()) {
|
|
|
|
|
if (i >= argc) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
const char *option_value = argv[i + 1];
|
|
|
|
|
it->second->set(option_value);
|
|
|
|
|
i++;
|
|
|
|
|
} else {
|
|
|
|
|
printf("Ignoring unrecognized option: %s.\n", argv[i]);
|
|
|
|
|
printf("Usage: %s {option value}\n", argv[0]);
|
|
|
|
|
printf("\tTakes text from stdin and produces pdf file.\n");
|
|
|
|
|
printf("Supported options:\n");
|
|
|
|
|
for (BaseOption *opt : option_list) {
|
|
|
|
|
printf("\t%s\t%s (%s)\n", opt->selector.c_str(),
|
|
|
|
|
opt->description.c_str(), opt->valueToString().c_str());
|
|
|
|
|
}
|
|
|
|
|
exit(-1);
|
2016-05-03 22:09:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-11 21:30:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct DoubleOption : Option<double> {
|
|
|
|
|
virtual void set(std::string _value) { value = atof(_value.c_str()); }
|
|
|
|
|
virtual std::string valueToString() {
|
|
|
|
|
std::ostringstream stm;
|
|
|
|
|
stm << value;
|
|
|
|
|
return stm.str();
|
|
|
|
|
}
|
2018-11-30 16:24:47 +00:00
|
|
|
|
DoubleOption(std::string _selector,
|
|
|
|
|
std::string _description,
|
2016-07-11 21:30:39 +00:00
|
|
|
|
double defaultValue)
|
2018-11-30 16:24:47 +00:00
|
|
|
|
: Option<double>(_selector, _description, defaultValue) {}
|
2016-05-03 22:09:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
struct StringOption : Option<std::string> {
|
|
|
|
|
virtual void set(std::string _value) { value = _value; }
|
|
|
|
|
virtual std::string valueToString() { return value; }
|
2018-11-30 16:24:47 +00:00
|
|
|
|
StringOption(std::string _selector,
|
|
|
|
|
std::string _description,
|
2016-07-11 21:30:39 +00:00
|
|
|
|
std::string defaultValue)
|
2018-11-30 16:24:47 +00:00
|
|
|
|
: Option<std::string>(_selector, _description, defaultValue) {}
|
2016-07-11 21:30:39 +00:00
|
|
|
|
};
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
// Config //////////////////////////////////////////////////////////////////////
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
struct Config {
|
|
|
|
|
DoubleOption page_width = DoubleOption("-w", "Page width", 600.0f);
|
|
|
|
|
DoubleOption page_height = DoubleOption("-h", "Page height", 800.0f);
|
|
|
|
|
StringOption title = StringOption("-t", "PDF title", "---");
|
|
|
|
|
StringOption author = StringOption("-a", "PDF author", "---");
|
|
|
|
|
StringOption subject = StringOption("-k", "PDF subject", "---");
|
|
|
|
|
StringOption keywords = StringOption("-c", "PDF keywords", "---");
|
|
|
|
|
StringOption creator = StringOption("-t", "PDF creator", "---");
|
|
|
|
|
StringOption font_file = StringOption("-f", ".ttf font file", "");
|
|
|
|
|
DoubleOption font_size = DoubleOption("-z", "Font size", 8.0f);
|
|
|
|
|
DoubleOption left_margin = DoubleOption("-m", "Left margin", 20.0f);
|
|
|
|
|
DoubleOption line_spacing_ratio =
|
2018-01-25 19:37:17 +00:00
|
|
|
|
DoubleOption("-h", "Line spacing ratio", 0.25f);
|
2016-07-11 21:30:39 +00:00
|
|
|
|
StringOption output_file_name =
|
|
|
|
|
StringOption("-o", ".pdf output file name", "out-skiahf.pdf");
|
|
|
|
|
|
|
|
|
|
Config(int argc, char **argv) {
|
|
|
|
|
BaseOption::Init(std::vector<BaseOption*>{
|
|
|
|
|
&page_width, &page_height, &title, &author, &subject,
|
|
|
|
|
&keywords, &creator, &font_file, &font_size, &left_margin,
|
|
|
|
|
&line_spacing_ratio, &output_file_name}, argc, argv);
|
2016-05-03 22:09:52 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
// Placement ///////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2016-05-03 22:09:52 +00:00
|
|
|
|
class Placement {
|
2016-07-11 21:30:39 +00:00
|
|
|
|
public:
|
|
|
|
|
Placement(const Config* conf, SkDocument *doc)
|
|
|
|
|
: config(conf), document(doc), pageCanvas(nullptr) {
|
|
|
|
|
white_paint.setColor(SK_ColorWHITE);
|
|
|
|
|
glyph_paint.setColor(SK_ColorBLACK);
|
|
|
|
|
glyph_paint.setFlags(SkPaint::kAntiAlias_Flag |
|
|
|
|
|
SkPaint::kSubpixelText_Flag);
|
2018-11-26 22:23:24 +00:00
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
|
|
|
|
font.setSize(SkDoubleToScalar(config->font_size.value));
|
2016-07-01 15:48:12 +00:00
|
|
|
|
}
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
void WriteLine(const SkShaper& shaper, const char *text, size_t textBytes) {
|
2018-01-29 20:45:30 +00:00
|
|
|
|
SkTextBlobBuilder textBlobBuilder;
|
2018-11-26 22:23:24 +00:00
|
|
|
|
SkPoint endPoint = shaper.shape(&textBlobBuilder, font, text, textBytes, true,
|
2018-01-29 20:45:30 +00:00
|
|
|
|
SkPoint{0, 0},
|
|
|
|
|
config->page_width.value - 2*config->left_margin.value);
|
|
|
|
|
sk_sp<const SkTextBlob> blob = textBlobBuilder.make();
|
|
|
|
|
// If we don't have a page, or if we're not at the start of the page and the blob won't fit
|
|
|
|
|
if (!pageCanvas ||
|
|
|
|
|
(current_y > config->line_spacing_ratio.value * config->font_size.value &&
|
|
|
|
|
current_y + endPoint.y() > config->page_height.value)
|
|
|
|
|
) {
|
2016-07-11 21:30:39 +00:00
|
|
|
|
if (pageCanvas) {
|
|
|
|
|
document->endPage();
|
|
|
|
|
}
|
2016-08-03 17:43:55 +00:00
|
|
|
|
pageCanvas = document->beginPage(
|
|
|
|
|
SkDoubleToScalar(config->page_width.value),
|
|
|
|
|
SkDoubleToScalar(config->page_height.value));
|
2016-07-11 21:30:39 +00:00
|
|
|
|
pageCanvas->drawPaint(white_paint);
|
|
|
|
|
current_x = config->left_margin.value;
|
|
|
|
|
current_y = config->line_spacing_ratio.value * config->font_size.value;
|
|
|
|
|
}
|
2016-08-03 17:43:55 +00:00
|
|
|
|
pageCanvas->drawTextBlob(
|
|
|
|
|
blob.get(), SkDoubleToScalar(current_x),
|
|
|
|
|
SkDoubleToScalar(current_y), glyph_paint);
|
2016-07-11 21:30:39 +00:00
|
|
|
|
// Advance to the next line.
|
2018-01-25 19:37:17 +00:00
|
|
|
|
current_y += endPoint.y() + config->line_spacing_ratio.value * config->font_size.value;
|
2016-05-03 22:09:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2016-07-11 21:30:39 +00:00
|
|
|
|
const Config* config;
|
|
|
|
|
SkDocument *document;
|
|
|
|
|
SkCanvas *pageCanvas;
|
|
|
|
|
SkPaint white_paint;
|
|
|
|
|
SkPaint glyph_paint;
|
2018-11-26 22:23:24 +00:00
|
|
|
|
SkFont font;
|
2016-07-11 21:30:39 +00:00
|
|
|
|
double current_x;
|
|
|
|
|
double current_y;
|
|
|
|
|
};
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2017-12-14 18:25:04 +00:00
|
|
|
|
static sk_sp<SkDocument> MakePDFDocument(const Config &config, SkWStream *wStream) {
|
2018-09-07 18:33:14 +00:00
|
|
|
|
SkPDF::Metadata pdf_info;
|
2016-07-11 21:30:39 +00:00
|
|
|
|
pdf_info.fTitle = config.title.value.c_str();
|
|
|
|
|
pdf_info.fAuthor = config.author.value.c_str();
|
|
|
|
|
pdf_info.fSubject = config.subject.value.c_str();
|
|
|
|
|
pdf_info.fKeywords = config.keywords.value.c_str();
|
|
|
|
|
pdf_info.fCreator = config.creator.value.c_str();
|
|
|
|
|
#if 0
|
|
|
|
|
SkTime::DateTime now;
|
|
|
|
|
SkTime::GetDateTime(&now);
|
2018-09-07 18:33:14 +00:00
|
|
|
|
pdf_info.fCreation = now;
|
|
|
|
|
pdf_info.fModified = now;
|
2017-12-14 18:25:04 +00:00
|
|
|
|
pdf_info.fPDFA = true;
|
2016-07-11 21:30:39 +00:00
|
|
|
|
#endif
|
2018-09-07 18:33:14 +00:00
|
|
|
|
return SkPDF::MakeDocument(wStream, pdf_info);
|
2016-07-11 21:30:39 +00:00
|
|
|
|
}
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
int main(int argc, char **argv) {
|
2016-05-03 22:09:52 +00:00
|
|
|
|
Config config(argc, argv);
|
2016-07-11 21:30:39 +00:00
|
|
|
|
SkFILEWStream wStream(config.output_file_name.value.c_str());
|
|
|
|
|
sk_sp<SkDocument> doc = MakePDFDocument(config, &wStream);
|
|
|
|
|
assert(doc);
|
|
|
|
|
Placement placement(&config, doc.get());
|
2016-05-03 22:09:52 +00:00
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
const std::string &font_file = config.font_file.value;
|
|
|
|
|
sk_sp<SkTypeface> typeface;
|
|
|
|
|
if (font_file.size() > 0) {
|
|
|
|
|
typeface = SkTypeface::MakeFromFile(font_file.c_str(), 0 /* index */);
|
|
|
|
|
}
|
|
|
|
|
SkShaper shaper(typeface);
|
|
|
|
|
assert(shaper.good());
|
2017-08-30 17:56:19 +00:00
|
|
|
|
//SkString line("This is هذا هو الخط a line.");
|
2017-11-16 15:08:28 +00:00
|
|
|
|
//SkString line("This is a line هذا هو الخط.");
|
2016-05-03 22:09:52 +00:00
|
|
|
|
for (std::string line; std::getline(std::cin, line);) {
|
2016-07-11 21:30:39 +00:00
|
|
|
|
placement.WriteLine(shaper, line.c_str(), line.size());
|
2016-05-03 22:09:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 21:30:39 +00:00
|
|
|
|
doc->close();
|
2017-08-30 17:56:19 +00:00
|
|
|
|
wStream.flush();
|
2016-05-03 22:09:52 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|