Commit 6ea79256 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved measure conversion flow

switch to converting all measures to internal units as soon as
they are configured to simplify the internal layouting logic
parent 2d6fba0b
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -108,8 +108,10 @@ ReturnCode configure_series(
  return ERROR_INVALID_ARGUMENT;
}

ReturnCode parseMeasureProp(
ReturnCode configure_measure_rel(
    const plist::Property& prop,
    double dpi,
    double font_size,
    Measure* value) {
  if (!plist::is_value(prop)) {
    return ReturnCode::errorf(
@@ -118,7 +120,7 @@ ReturnCode parseMeasureProp(
        prop.size());
  }

  return parse_measure(prop, value);
  return parse_measure_rel(prop, dpi, font_size, value);
}

ParserFn configure_multiprop(const std::vector<ParserFn>& parsers) {
+3 −1
Original line number Diff line number Diff line
@@ -91,8 +91,10 @@ ReturnCode configure_series(
    const plist::Property& prop,
    Series* data);

ReturnCode parseMeasureProp(
ReturnCode configure_measure_rel(
    const plist::Property& prop,
    double dpi,
    double font_size,
    Measure* value);

ReturnCode configure_colour(
+19 −13
Original line number Diff line number Diff line
@@ -41,11 +41,13 @@
namespace plotfx {

Document::Document() :
    width({Unit::PX, 1200}),
    height({Unit::PX, 600}),
    width(1200),
    height(600),
    background_colour(Colour::fromRGB(1,1,1)),
    text_colour(Colour::fromRGB(.2,.2,.2)),
    border_colour(Colour::fromRGB(.66,.66,.66)) {}
    border_colour(Colour::fromRGB(.66,.66,.66)),
    dpi(96),
    font_size(from_pt(11, dpi)) {}

ReturnCode document_setup_defaults(Document* doc) {
  if (!font_load(DefaultFont::HELVETICA_REGULAR, &doc->font_sans)) {
@@ -64,9 +66,11 @@ ReturnCode document_load(
    return rc;
  }

  // IMPORTANT: parse dpi + font size first

  static const ParserDefinitions pdefs = {
    {"width", std::bind(&parseMeasureProp, std::placeholders::_1, &doc->width)},
    {"height", std::bind(&parseMeasureProp, std::placeholders::_1, &doc->height)},
    {"width", std::bind(&configure_measure_rel, std::placeholders::_1, doc->dpi, doc->font_size, &doc->width)},
    {"height", std::bind(&configure_measure_rel, std::placeholders::_1, doc->dpi, doc->font_size, &doc->height)},
    {"background-colour", std::bind(&configure_colour, std::placeholders::_1, &doc->background_colour)},
    {
      "foreground-colour",
@@ -92,7 +96,7 @@ ReturnCode document_load(

    const auto& elem_config = plist[i].next.get();

    std::unique_ptr<Element> elem;
    ElementRef elem;
    if (auto rc = buildElement(*doc, elem_name, *elem_config, &elem); !rc.isSuccess()) {
      return rc;
    }
@@ -124,7 +128,7 @@ ReturnCode document_render_to(
  Rectangle clip(0, 0, layer->width, layer->height);

  for (const auto& e : tree.roots) {
    if (auto rc = e->draw(clip, layer); !rc.isSuccess()) {
    if (auto rc = e->draw(tree, clip, layer); !rc.isSuccess()) {
      return rc;
    }
  }
@@ -154,9 +158,10 @@ ReturnCode document_render_svg(
  LayerRef layer;

  auto rc = layer_bind_svg(
      to_px(doc.measures, doc.width).value,
      to_px(doc.measures, doc.height).value,
      doc.measures,
      doc.width,
      doc.height,
      doc.dpi,
      doc.font_size,
      doc.background_colour,
      [filename] (auto svg) {
        FileUtil::write(filename, Buffer(svg.data(), svg.size()));
@@ -181,9 +186,10 @@ ReturnCode document_render_png(
  LayerRef layer;

  auto rc = layer_bind_png(
      to_px(doc.measures, doc.width).value,
      to_px(doc.measures, doc.height).value,
      doc.measures,
      doc.width,
      doc.height,
      doc.dpi,
      doc.font_size,
      doc.background_colour,
      [filename] (auto png) {
        FileUtil::write(filename, Buffer(png.data(), png.size()));
+2 −1
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ class Layer;

struct Document {
  Document();
  MeasureTable measures;
  Measure width;
  Measure height;
  ColourScheme colour_scheme;
@@ -51,6 +50,8 @@ struct Document {
  FontInfo font_sans;
  FontInfo font_mono;
  std::vector<ElementRef> roots;
  double dpi;
  Measure font_size;
};

ReturnCode document_load(
+5 −4
Original line number Diff line number Diff line
@@ -36,16 +36,17 @@
#include "utils/return_code.h"

namespace plotfx {
class Layer;
class Rectangle;
struct Layer;
struct Rectangle;
struct Document;

using plist::PropertyList;

struct Element {
  std::function<ReturnCode (const Rectangle&, Layer*)> draw;
  std::function<ReturnCode (const Document&, const Rectangle&, Layer*)> draw;
};

using ElementRef = std::unique_ptr<Element>;
using ElementRef = std::shared_ptr<Element>;


} // namespace plotfx
Loading