Commit 328fdff5 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved svg layer, default font size

parent a7f30e72
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -44,8 +44,8 @@ Document::Document() :
    width({Unit::PX, 1200}),
    height({Unit::PX, 600}),
    background_colour(Colour::fromRGB(1,1,1)),
    text_colour(Colour::fromRGB(.3,.3,.3)),
    border_colour(Colour::fromRGB(.45,.45,.45)) {}
    text_colour(Colour::fromRGB(.4,.4,.4)),
    border_colour(Colour::fromRGB(.66,.66,.66)) {}

ReturnCode document_setup_defaults(Document* doc) {
  if (auto path = getenv("PLOTFX_FONT_PATH"); path) {
+28 −9
Original line number Diff line number Diff line
@@ -42,10 +42,25 @@ struct SVGData {

using SVGDataRef = std::shared_ptr<SVGData>;

Status svg_text_span(const layer_ops::TextSpanOp& op, SVGDataRef svg) {
Status svg_text_span(
    const layer_ops::TextSpanOp& op,
    const MeasureTable& measures,
    SVGDataRef svg) {
  const auto& style = op.style;

  std::string anchor = "middle";
  std::string anchor;
  switch (style.halign) {
    case TextHAlign::CENTER:
      anchor = "middle";
      break;
    case TextHAlign::LEFT:
      anchor = "start";
      break;
    case TextHAlign::RIGHT:
      anchor = "end";
      break;
  }

  std::string baseline = "middle";

  svg->buffer
@@ -54,7 +69,8 @@ Status svg_text_span(const layer_ops::TextSpanOp& op, SVGDataRef svg) {
    << "x='" << op.x << "' "
    << "y='" << op.y << "' "
    << "fill='" << style.colour.to_hex_str() << "' "
    << "font-size='" << style.font_size << "' "
    << "font-size='" << to_px(measures, style.font_size).value << "' "
    << "font-family='" << "\"Helvetica Neue\", Helvetica, Arial, sans-serif" << "' "
    << "text-anchor='" << anchor << "' "
    << "dominant-baseline='" << baseline << "' "
    << ">"
@@ -64,7 +80,10 @@ Status svg_text_span(const layer_ops::TextSpanOp& op, SVGDataRef svg) {
  return OK;
}

Status svg_stroke_path(const layer_ops::BrushStrokeOp& op, SVGDataRef svg) {
Status svg_stroke_path(
    const layer_ops::BrushStrokeOp& op,
    const MeasureTable& measures,
    SVGDataRef svg) {
  const auto& clip = op.clip;
  const auto& path = op.path;
  const auto& style = op.style;
@@ -75,7 +94,7 @@ Status svg_stroke_path(const layer_ops::BrushStrokeOp& op, SVGDataRef svg) {

  svg->buffer << StringUtil::format(
      "  <path stroke-width='$0' stroke='$1' fill='none' d=\"",
      style.line_width.value, // FIXME
      to_px(measures, style.line_width).value,
      style.colour.to_hex_str());

  for (const auto& cmd : path) {
@@ -116,13 +135,13 @@ ReturnCode layer_bind_svg(
    .height = svg->height = height,
    .measures = measures,
    .text_shaper = std::make_shared<text::TextShaper>(),
    .apply = [svg, submit] (const auto& op) {
      return std::visit([svg, submit] (auto&& op) {
    .apply = [svg, submit, measures] (const auto& op) {
      return std::visit([svg, submit, measures] (auto&& op) {
        using T = std::decay_t<decltype(op)>;
        if constexpr (std::is_same_v<T, layer_ops::BrushStrokeOp>)
          return svg_stroke_path(op, svg);
          return svg_stroke_path(op, measures, svg);
        if constexpr (std::is_same_v<T, layer_ops::TextSpanOp>)
          return svg_text_span(op, svg);
          return svg_text_span(op, measures, svg);
        if constexpr (std::is_same_v<T, layer_ops::SubmitOp>)
          return submit(svg->to_svg());
        else
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ enum class Unit {
};

struct MeasureTable {
  MeasureTable() : dpi(96), rem(12) {}
  MeasureTable() : dpi(96), rem(11) {}
  double dpi;
  double rem;
};
+1 −1
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ TextStyle::TextStyle() :
    direction(TextDirection::LTR),
    halign(TextHAlign::LEFT),
    valign(TextVAlign::BASELINE),
    font_size(12) {}
    font_size(from_pt(11)) {}

Status drawText(
    const std::string& text,
+2 −1
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@
#include "plotfx.h"
#include "path.h"
#include "colour.h"
#include "measure.h"

namespace plotfx {
class Layer;
@@ -57,7 +58,7 @@ struct TextStyle {
  TextHAlign halign;
  TextVAlign valign;
  FontInfo font;
  double font_size;
  Measure font_size;
  Colour colour;
};

Loading