Commit 95aa790c authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add support for the 'mm' unit

parent 361a944d
Loading
Loading
Loading
Loading
+10 −7
Original line number Diff line number Diff line
@@ -324,10 +324,13 @@ struct SVGDrawOp {
ReturnCode export_svg(
    const Layer* layer,
    std::string* buffer) {
  auto width = convert_unit(*layer, layer->width);
  auto height = convert_unit(*layer, layer->height);

  auto svg = std::make_shared<SVGData>();
  svg->width = layer->width;
  svg->height = layer->height;
  svg->proj = mul(translate2({0, layer->height}), scale2({1, -1}));
  svg->width = width;
  svg->height = height;
  svg->proj = mul(translate2({0, height}), scale2({1, -1}));

  for (const auto& cmd : layer->drawlist) {
    if (auto rc = svg_add_shape_elem(cmd, svg); !rc) {
@@ -343,12 +346,12 @@ ReturnCode export_svg(

    << "<svg"
      << svg_attr("xmlns", "http://www.w3.org/2000/svg")
      << svg_attr("width", layer->width)
      << svg_attr("height", layer->height)
      << svg_attr("width", width)
      << svg_attr("height", height)
      << ">\n"
    << "  <rect"
      << svg_attr("width", layer->width)
      << svg_attr("height", layer->height)
      << svg_attr("width", width)
      << svg_attr("height", height)
      << svg_attr("fill", layer->background_color.to_hex_str())
      << svg_attr("fill-opacity", layer->background_color.component(3))
      << "/>\n"
+30 −1
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
 * limitations under the License.
 */
#include "measure.h"
#include "layer.h"
#include <assert.h>
#include <iostream>

@@ -29,6 +30,10 @@ Measure from_unit(double v) {
  return Measure(Unit::UNIT, v);
}

Measure from_mm(double v) {
  return Measure(Unit::MM, v);
}

Measure from_px(double v) {
  return Measure(Unit::PX, v);
}
@@ -74,6 +79,11 @@ ReturnCode parse_measure(
  }

  auto unit = s.substr(unit_pos);
  if (unit == "mm") {
    *measure = from_mm(value);
    return OK;
  }

  if (unit == "px") {
    *measure = from_px(value);
    return OK;
@@ -101,7 +111,7 @@ ReturnCode parse_measure(

  return errorf(
      ERROR,
      "invalid unit: '{}', expected one of 'px', 'pt', 'em', 'rem', 'rel' or '%'",
      "invalid unit: '{}', expected one of 'mm', 'px', 'pt', 'em', 'rem', 'rel' or '%'",
      unit);
}

@@ -110,6 +120,8 @@ std::string measure_to_string(const Measure& m) {
    case Unit::UNIT:
    case Unit::USER:
      return fmt::format("{}", m.value);
    case Unit::MM:
      return fmt::format("{}mm", m.value);
    case Unit::PX:
      return fmt::format("{}px", m.value);
    case Unit::PT:
@@ -133,6 +145,19 @@ Measure measure_or(const Measure& primary, const Measure& fallback) {
  }
}

void convert_unit(
    const Layer& layer,
    Measure* measure) {
  convert_unit_typographic(layer.dpi, layer.font_size, measure);
}

Measure convert_unit(
    const Layer& layer,
    Measure measure) {
  convert_unit(layer, &measure);
  return measure;
}

void convert_units(
    const std::vector<UnitConverter>& converters,
    Measure* begin,
@@ -167,6 +192,10 @@ void convert_unit_typographic(
  }

  switch (measure->unit) {
    case Unit::MM:
      measure->value = (measure->value / 25.4) * dpi;
      measure->unit = Unit::UNIT;
      break;
    case Unit::PT:
      measure->value = (measure->value / 72.0) * dpi;
      measure->unit = Unit::UNIT;
+11 −0
Original line number Diff line number Diff line
@@ -18,10 +18,12 @@
#include <return_code.h>

namespace clip {
struct Layer;
struct Rectangle;

enum class Unit {
  UNIT,  // Screen units
  MM,    // Millimeters
  PX,    // Pixels
  PT,    // Typographic Points
  REM,   // Typographic "em" size
@@ -44,6 +46,7 @@ struct MeasureConv {
};

Measure from_unit(double v);
Measure from_mm(double v);
Measure from_px(double v);
Measure from_pt(double v, double dpi);
Measure from_pt(double v);
@@ -62,6 +65,14 @@ Measure measure_or(const Measure& primary, const Measure& fallback);

using UnitConverter = std::function<void (Measure*)>;

void convert_unit(
    const Layer& layer,
    Measure* measure);

Measure convert_unit(
    const Layer& layer,
    Measure measure);

void convert_unit(
    const std::vector<UnitConverter>& converters,
    Measure* measure);
+5 −1
Original line number Diff line number Diff line
@@ -137,7 +137,11 @@ Rectangle plot_get_clip(const PlotConfig* plot, const Layer* layer) {
    }

    return layout_margin_box(
        Rectangle(0, 0, layer->width, layer->height),
        Rectangle(
            0,
            0,
            convert_unit(*layer, layer->width),
            convert_unit(*layer, layer->height)),
        margins[0],
        margins[1],
        margins[2],
+1 −0
Original line number Diff line number Diff line
(layer/resize 200mm 300mm)
Loading