Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Created January 10, 2019 02:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HalCanary/1a870f75e5a6cf2b64e10f2756bc0f4b to your computer and use it in GitHub Desktop.
Save HalCanary/1a870f75e5a6cf2b64e10f2756bc0f4b to your computer and use it in GitHub Desktop.
// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "skia.h"
#include "SkShaper.h"
#include <iostream>
#include <string>
int main() {
constexpr int kWidth = 1080;
constexpr int kXMargin = 40;
constexpr int kYMargin = 40;
constexpr char kFileName[] = "text_to_img.png";
constexpr char kFamilyName[] = "DejaVuSerif";
constexpr int kFontWeight = SkFontStyle::kNormal_Weight;
constexpr int kFontWidth = SkFontStyle::kNormal_Width;
constexpr SkFontStyle::Slant kFontSlant = SkFontStyle::kUpright_Slant;
constexpr float kTextSize = 64;
SkFont font(SkTypeface::MakeFromName(kFamilyName,
SkFontStyle(kFontWeight, kFontWidth, kFontSlant)), kTextSize);
SkShaper shaper(font.refTypeface());
SkTextBlobBuilderLineHandler handler;
SkPoint point{0, 0};
for (std::string line; std::getline(std::cin, line);) {
line += ' ';
point = shaper.shape(&handler, font, line.c_str(), line.size(),
true, point, kWidth - 2 * kXMargin);
}
SkBitmap bitmap;
bitmap.allocN32Pixels(kWidth, point.y() + 2 * kYMargin);
bitmap.eraseColor(SK_ColorWHITE);
SkCanvas(bitmap).drawTextBlob(handler.makeBlob(), kXMargin, kYMargin, SkPaint());
SkFILEWStream dst(kFileName);
SkEncodeImage(&dst, bitmap, SkEncodedImageFormat::kPNG, 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment