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[]) {
|
|
|
|
SkRect bounds;
|
|
|
|
canvas->getClipBounds(&bounds);
|
|
|
|
|
|
|
|
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) {
|
2013-09-20 19:33:52 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(w, h));
|
2013-06-07 20:30:16 +00:00
|
|
|
doDraw(surface->getCanvas(), paint, text);
|
2013-06-08 07:01:13 +00:00
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
|
|
|
|
SkAutoDataUnref data(image->encode());
|
|
|
|
if (NULL == data.get()) {
|
|
|
|
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) {
|
|
|
|
SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path));
|
|
|
|
if (doc.get()) {
|
|
|
|
SkScalar width = SkIntToScalar(w);
|
|
|
|
SkScalar height = SkIntToScalar(h);
|
|
|
|
doDraw(doc->beginPage(width, height, NULL), paint, text);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-02 18:33:14 +00:00
|
|
|
int tool_main(int argc, char** argv);
|
|
|
|
int tool_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();
|
|
|
|
|
|
|
|
int w = SkScalarRound(width) + 30;
|
|
|
|
int h = SkScalarRound(spacing) + 30;
|
|
|
|
|
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
|
|
|
}
|
2012-10-02 18:33:14 +00:00
|
|
|
|
|
|
|
#if !defined SK_BUILD_FOR_IOS
|
|
|
|
int main(int argc, char * const argv[]) {
|
|
|
|
return tool_main(argc, (char**) argv);
|
|
|
|
}
|
|
|
|
#endif
|