Commit 1b72664d authored by Paul Asmuth's avatar Paul Asmuth
Browse files

simplify the draw command list

Convert the DrawCommandList into a simple, flat vector of draw
command structs. A mechanism for piping semantic text information
through to the output file will be reimplemented later.

This change also removes the dependency on std::variant, which is
a C++17 feature, but is not supported on macOS 10.13.
parent e4e6b679
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -235,7 +235,7 @@ ReturnCode plot_legend_borders(
    const Rectangle& bbox) {
  /* draw top border  */
  if (borders[0].line_width > 0) {
    draw_cmd::Shape line;
    DrawCommand line;
    line.stroke_style.line_width = borders[0].line_width;
    line.stroke_style.color = borders[0].color;

@@ -249,7 +249,7 @@ ReturnCode plot_legend_borders(

  /* draw right border  */
  if (borders[1].line_width > 0) {
    draw_cmd::Shape line;
    DrawCommand line;
    line.stroke_style.line_width = borders[1].line_width;
    line.stroke_style.color = borders[1].color;

@@ -263,7 +263,7 @@ ReturnCode plot_legend_borders(

  /* draw top border  */
  if (borders[2].line_width > 0) {
    draw_cmd::Shape line;
    DrawCommand line;
    line.stroke_style.line_width = borders[2].line_width;
    line.stroke_style.color = borders[2].color;

@@ -277,7 +277,7 @@ ReturnCode plot_legend_borders(

  /* draw left border  */
  if (borders[3].line_width > 0) {
    draw_cmd::Shape line;
    DrawCommand line;
    line.stroke_style.line_width = borders[3].line_width;
    line.stroke_style.color = borders[3].color;

@@ -376,7 +376,7 @@ ReturnCode plot_legend(

  /* draw background */
  if (config->background) {
    draw_cmd::Shape shape;
    DrawCommand shape;
    shape.fill_style.color = *config->background;
    path_add_rectangle(&shape.path, border_box);
    draw_shape(ctx, shape);
+2 −2
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@ void draw_polygon(
      layer_get_font_size(ctx),
      &stroke_style.line_width);

  draw_cmd::Polygon elem;
  elem.poly = poly;
  DrawCommand elem;
  elem.path = path_from_poly2(poly);
  elem.stroke_style = stroke_style;
  elem.fill_style = fill_style;
  layer_get(ctx)->drawlist.push_back(elem);
+2 −10
Original line number Diff line number Diff line
@@ -22,14 +22,6 @@
namespace clip {
struct Context;

/**
 * The draw command list represents a list of abstract 2D vector graphics
 * operations, such as rendering text and drawing polygons. Note that the "list"
 * is not necessarily a flat list, but may be a tree.
 */
using DrawCommand = std::variant<draw_cmd::Text, draw_cmd::Shape, draw_cmd::Polygon>;
using DrawCommandList = std::vector<DrawCommand>;

/**
 * The page structure stores a number of global drawing properties
 */
@@ -46,7 +38,7 @@ struct Page {

void draw_shape(
    Context* ctx,
    draw_cmd::Shape elem);
    DrawCommand elem);

void draw_path(
    Context* ctx,
@@ -74,7 +66,7 @@ void draw_line(

void draw_text(
    Context* ctx,
    draw_cmd::Text elem);
    const TextInfo& elem);

ReturnCode draw_text(
    Context* ctx,
+33 −6
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@

namespace clip {

void draw_shape(Context* ctx, draw_cmd::Shape elem) {
void draw_shape(Context* ctx, DrawCommand elem) {
  layer_get(ctx)->drawlist.emplace_back(std::move(elem));
}

@@ -29,7 +29,7 @@ void draw_path(
    const Path& path,
    StrokeStyle stroke_style,
    FillStyle fill_style) {
  draw_cmd::Shape shape;
  DrawCommand shape;
  shape.path = path;
  shape.stroke_style = stroke_style;
  shape.fill_style = fill_style;
@@ -41,15 +41,42 @@ void draw_line(
    vec2 from,
    vec2 to,
    StrokeStyle stroke_style) {
  draw_cmd::Shape shape;
  DrawCommand shape;
  shape.path.moveTo(from.x, from.y);
  shape.path.lineTo(to.x, to.y);
  shape.stroke_style = stroke_style;
  draw_shape(ctx, shape);
}

void draw_text(Context* ctx, draw_cmd::Text elem) {
  layer_get(ctx)->drawlist.emplace_back(std::move(elem));
void draw_text(Context* ctx, const TextInfo& elem) {
  const auto& style = elem.style;

  for (const auto& gg : elem.glyphs) {
    for (const auto& g : gg.glyphs) {
      auto gt = translate2({g.x, g.y});
      if (elem.transform) {
        gt = mul(*elem.transform, gt);
      }

      Path gp;
      auto rc = font_get_glyph_path(
          g.font,
          elem.style.font_size,
          layer_get_dpi(ctx),
          g.codepoint,
          &gp);

      if (!rc) {
        return;
      }

      DrawCommand shape;
      shape.path = path_transform(gp, gt);
      shape.fill_style.color = style.color;
      draw_shape(ctx, shape);
    }
  }

}

ReturnCode draw_text(
@@ -104,7 +131,7 @@ ReturnCode draw_text(
    }
  }

  draw_cmd::Text op;
  TextInfo op;
  op.text = text;
  op.glyphs = std::move(glyphs);

+17 −18
Original line number Diff line number Diff line
@@ -14,7 +14,6 @@
#pragma once
#include <stdlib.h>
#include <string>
#include <variant>
#include <vector>

#include "color.h"
@@ -24,9 +23,23 @@
#include "font_lookup.h"
#include "style.h"

namespace clip::draw_cmd {
namespace clip {

struct Text {
struct DrawCommand {
  Path path;
  StrokeStyle stroke_style;
  FillStyle fill_style;
  Option<AntialiasingMode> antialiasing_mode;
};

/**
 * The draw command list represents a list of abstract 2D vector graphics
 * operations, such as rendering text and drawing polygons. Note that the "list"
 * is not necessarily a flat list, but may be a tree.
 */
using DrawCommandList = std::vector<DrawCommand>;

struct TextInfo {
  std::string text;
  std::vector<text::GlyphPlacementGroup> glyphs;

@@ -37,19 +50,5 @@ struct Text {
  Option<mat3> transform;
};

struct Shape {
  Path path;
  StrokeStyle stroke_style;
  FillStyle fill_style;
  Option<AntialiasingMode> antialiasing_mode;
};

struct Polygon {
  Poly2 poly;
  StrokeStyle stroke_style;
  FillStyle fill_style;
  Option<AntialiasingMode> antialiasing_mode;
};

} // namespace clip::draw_cmd
} // namespace clip
Loading