Commit 326ba5b1 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

rasterizer improvements, allow retrieval of raw pixel data

parent a635999d
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -32,6 +32,44 @@

namespace plotfx {

ReturnCode layer_bind_img(
    double width,
    double height,
    double dpi,
    Measure font_size,
    const Color& background_color,
    std::function<Status (const unsigned char* data, size_t len)> submit,
    LayerRef* layer) {
  auto text_shaper = std::make_shared<text::TextShaper>();
  auto raster = std::make_shared<Rasterizer>(width, height, dpi, text_shaper);
  raster->clear(background_color);

  layer->reset(new Layer {
    .width = width,
    .height = height,
    .dpi = dpi,
    .font_size = font_size,
    .text_shaper = text_shaper,
    .apply = [submit, raster] (auto op) {
      return std::visit([submit, raster] (auto&& op) {
        using T = std::decay_t<decltype(op)>;
        if constexpr (std::is_same_v<T, layer_ops::BrushStrokeOp>)
          return raster->strokePath(op);
        if constexpr (std::is_same_v<T, layer_ops::BrushFillOp>)
          return raster->fillPath(op);
        if constexpr (std::is_same_v<T, layer_ops::TextSpanOp>)
          return raster->drawText(op);
        if constexpr (std::is_same_v<T, layer_ops::SubmitOp>)
          return submit(raster->data(), raster->size());
        else
          return ERROR;
      }, op);
    },
  });

  return OK;
}

ReturnCode layer_bind_png(
    double width,
    double height,
+9 −0
Original line number Diff line number Diff line
@@ -33,6 +33,15 @@
namespace plotfx {
class Rasterizer;

ReturnCode layer_bind_img(
    double width,
    double height,
    double dpi,
    Measure font_size,
    const Color& background_color,
    std::function<Status (const unsigned char* data, size_t len)> submit,
    LayerRef* layer);

ReturnCode layer_bind_png(
    double width,
    double height,
+13 −3
Original line number Diff line number Diff line
@@ -35,10 +35,12 @@
namespace plotfx {

Rasterizer::Rasterizer(
    uint32_t width,
    uint32_t height,
    uint32_t width_,
    uint32_t height_,
    double dpi_,
    std::shared_ptr<text::TextShaper> text_shaper_) :
    width(width_),
    height(height_),
    dpi(dpi_),
    text_shaper(text_shaper_),
    ft_ready(false) {
@@ -198,7 +200,7 @@ Status Rasterizer::drawTextGlyphs(

  auto cairo_face = cairo_ft_font_face_create_for_ft_face(ft_font, 0);
  cairo_set_font_face(cr_ctx, cairo_face);
  cairo_set_font_size(cr_ctx, (style.font_size / 72.0) * dpi);
  cairo_set_font_size(cr_ctx, style.font_size);

  auto cairo_glyphs = cairo_glyph_allocate(glyph_count);
  for (int i = 0; i < glyph_count; ++i) {
@@ -244,6 +246,14 @@ void Rasterizer::clear(const Color& c) {
  cairo_paint(cr_ctx);
}

const unsigned char* Rasterizer::data() const {
  return cairo_image_surface_get_data(cr_surface);
}

size_t Rasterizer::size() const {
  return width * height * 4;
}

cairo_status_t cr_copy(
    void* closure,
    const unsigned char* data,
+4 −0
Original line number Diff line number Diff line
@@ -72,7 +72,11 @@ public:
  Status writeToFile(const std::string& path);

  std::string to_png() const;
  const unsigned char* data() const;
  size_t size() const;

  uint32_t width;
  uint32_t height;
  double dpi;
  std::shared_ptr<text::TextShaper> text_shaper;
  FT_Library ft;