2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2012-09-25 20:16:46 +00:00
|
|
|
|
2010-04-12 14:27:37 +00:00
|
|
|
#include "SkCanvas.h"
|
2013-03-21 19:43:15 +00:00
|
|
|
#include "SkCommandLineFlags.h"
|
2013-05-20 16:33:41 +00:00
|
|
|
#include "SkData.h"
|
2013-06-07 20:30:16 +00:00
|
|
|
#include "SkDocument.h"
|
2010-04-12 14:27:37 +00:00
|
|
|
#include "SkGraphics.h"
|
2013-05-20 16:33:41 +00:00
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkStream.h"
|
2010-04-12 14:27:37 +00:00
|
|
|
#include "SkString.h"
|
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
DEFINE_string2(outFile, o, "skhello", "The filename to write the image.");
|
2013-04-09 21:25:46 +00:00
|
|
|
DEFINE_string2(text, t, "Hello", "The string to write.");
|
2010-04-12 14:27:37 +00:00
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
static void doDraw(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
|
2017-01-23 16:39:45 +00:00
|
|
|
SkRect bounds = canvas->getLocalClipBounds();
|
2013-06-07 20:30:16 +00:00
|
|
|
|
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
canvas->drawText(text, strlen(text),
|
|
|
|
bounds.centerX(), bounds.centerY(),
|
|
|
|
paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool do_surface(int w, int h, const char path[], const char text[],
|
|
|
|
const SkPaint& paint) {
|
2015-06-23 13:26:08 +00:00
|
|
|
SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
|
2016-03-24 01:59:25 +00:00
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(w, h, &props));
|
2013-06-07 20:30:16 +00:00
|
|
|
doDraw(surface->getCanvas(), paint, text);
|
2013-06-08 07:01:13 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(surface->makeImageSnapshot());
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(image->encode());
|
|
|
|
if (!data) {
|
2013-06-07 20:30:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SkFILEWStream stream(path);
|
|
|
|
return stream.write(data->data(), data->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool do_document(int w, int h, const char path[], const char text[],
|
|
|
|
const SkPaint& paint) {
|
2019-01-07 15:00:48 +00:00
|
|
|
auto doc = SkPDF::MakeDocument(path);
|
2013-06-07 20:30:16 +00:00
|
|
|
if (doc.get()) {
|
|
|
|
SkScalar width = SkIntToScalar(w);
|
|
|
|
SkScalar height = SkIntToScalar(h);
|
2015-08-27 14:41:13 +00:00
|
|
|
doDraw(doc->beginPage(width, height, nullptr), paint, text);
|
2013-06-07 20:30:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-06 17:46:20 +00:00
|
|
|
int main(int argc, char** argv) {
|
2013-03-21 19:43:15 +00:00
|
|
|
SkCommandLineFlags::SetUsage("");
|
|
|
|
SkCommandLineFlags::Parse(argc, argv);
|
2013-03-04 20:58:01 +00:00
|
|
|
|
2010-04-12 14:27:37 +00:00
|
|
|
SkAutoGraphics ag;
|
2013-06-07 20:30:16 +00:00
|
|
|
SkString path("skhello");
|
2010-04-12 14:27:37 +00:00
|
|
|
SkString text("Hello");
|
|
|
|
|
2013-04-09 21:25:46 +00:00
|
|
|
if (!FLAGS_outFile.isEmpty()) {
|
|
|
|
path.set(FLAGS_outFile[0]);
|
2013-03-04 20:58:01 +00:00
|
|
|
}
|
2013-04-09 21:25:46 +00:00
|
|
|
if (!FLAGS_text.isEmpty()) {
|
|
|
|
text.set(FLAGS_text[0]);
|
2010-04-12 14:27:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextSize(SkIntToScalar(30));
|
2013-05-20 16:33:41 +00:00
|
|
|
paint.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
|
2010-04-12 14:27:37 +00:00
|
|
|
SkScalar width = paint.measureText(text.c_str(), text.size());
|
|
|
|
SkScalar spacing = paint.getFontSpacing();
|
|
|
|
|
2013-12-17 19:22:07 +00:00
|
|
|
int w = SkScalarRoundToInt(width) + 30;
|
|
|
|
int h = SkScalarRoundToInt(spacing) + 30;
|
2010-04-12 14:27:37 +00:00
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
static const struct {
|
|
|
|
bool (*fProc)(int w, int h, const char path[], const char text[],
|
|
|
|
const SkPaint&);
|
|
|
|
const char* fSuffix;
|
|
|
|
} gRec[] = {
|
|
|
|
{ do_surface, ".png" },
|
|
|
|
{ do_document, ".pdf" },
|
2013-05-20 16:33:41 +00:00
|
|
|
};
|
2013-06-08 07:01:13 +00:00
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
|
|
|
|
SkString file;
|
|
|
|
file.printf("%s%s", path.c_str(), gRec[i].fSuffix);
|
|
|
|
if (!gRec[i].fProc(w, h, file.c_str(), text.c_str(), paint)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-31 14:00:10 +00:00
|
|
|
}
|
2013-06-07 20:30:16 +00:00
|
|
|
return 0;
|
2010-04-12 14:27:37 +00:00
|
|
|
}
|