Commit 382dca29 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add support for named international paper sizes

parent 95aa790c
Loading
Loading
Loading
Loading
+41 −1
Original line number Diff line number Diff line
@@ -5,14 +5,54 @@ desc: |
  The `layer/resize` command is used to change the size of the current layer.

syntax: |
  (layer/resize &lt;<a href="width">width</a>&gt; &lt;<a href="width">height</a>&gt;)
  (layer/resize &lt;<a href="#option-width">width</a>&gt; &lt;<a href="#option-height">height</a>&gt;)
  (layer/resize &lt;<a href="#option-paper-size">paper-size</a>&gt;)

arguments:

  - name: width
    type: measure
    desc: The new layer width
    examples: |
      ;; set the layer size to 1920x1080
      (layer/resize 1920px 1080px)

      ;; set the layer size to 200mmx300mm
      (layer/resize 200mm 300mm)

  - name: height
    type: measure
    desc: The new layer height
    examples: |
      ;; set the layer size to 1920x1080
      (layer/resize 1920px 1080px)

      ;; set the layer size to 200mmx300mm
      (layer/resize 200mm 300mm)

  - name: paper-size
    type: paper-size
    desc: |
      Set the layer size to a standard paper size. Valid values are:

      - `A0` – 841mm x 1189mm
      - `A0*` – 1189mm x 841mm
      - `A1` – 594mm x 841mm
      - `A1*` – 841mm x 594mm
      - `A2` – 420mm x 594mm
      - `A2*` – 594mm x 420mm
      - `A3` – 297mm x 420mm
      - `A3*` – 420mm x 297mm
      - `A4` – 210mm x 297mm
      - `A4*` – 297mm x 210mm
      - `A5` – 148mm x 210mm
      - `A5*` – 210mm x 148mm
      - `A6` – 105mm x 148mm
      - `A6*` – 148mm x 105mm

    examples: |
      ;; set the layer size to A6
      (layer/resize A6)

      ;; set the layer size to A4 landscape
      (layer/resize A4*)
+58 −14
Original line number Diff line number Diff line
@@ -53,22 +53,62 @@ ReturnCode layer_create(
  return OK;
}

void layer_resize(
ReturnCode layer_resize(
    Layer* layer,
    Measure width,
    Measure height) {
  layer->width = width;
  layer->height = height;
  return OK;
}

ReturnCode layer_resize(
    Layer* layer,
    const std::string& size) {
  static const std::unordered_map<std::string, std::tuple<Measure, Measure>> sizes = {
    {"A0", {from_mm(841), from_mm(1189)}},
    {"A0*", {from_mm(1189), from_mm(841)}},
    {"A1", {from_mm(594), from_mm(841)}},
    {"A1*", {from_mm(841), from_mm(594)}},
    {"A2", {from_mm(420), from_mm(594)}},
    {"A2*", {from_mm(594), from_mm(420)}},
    {"A3", {from_mm(297), from_mm(420)}},
    {"A3*", {from_mm(420), from_mm(297)}},
    {"A4", {from_mm(210), from_mm(297)}},
    {"A4*", {from_mm(297), from_mm(210)}},
    {"A5", {from_mm(148), from_mm(210)}},
    {"A5*", {from_mm(210), from_mm(148)}},
    {"A6", {from_mm(105), from_mm(148)}},
    {"A6*", {from_mm(148), from_mm(105)}},
  };

  const auto& size_iter = sizes.find(size);
  if (size_iter == sizes.end()) {
    std::vector<std::string> size_names;
    for (const auto& s : sizes) {
      size_names.push_back(s.first);
    }

    return err_invalid_value(size, size_names);
  }

  layer->width = std::get<0>(size_iter->second);
  layer->height = std::get<1>(size_iter->second);

  return OK;
}

ReturnCode layer_resize(
    Context* ctx,
    const Expr* expr) {
  auto args = expr_collect(expr);
  if (args.size() != 2) {
    return err_invalid_nargs(args.size(), 2);

  switch (args.size()) {
    case 1: {
      return layer_resize(layer_get(ctx), expr_get_value(args[0]));
    }

    case 2: {
      Measure width;
      if (auto rc = measure_read(args[0], &width); !rc) {
        return rc;
@@ -79,8 +119,12 @@ ReturnCode layer_resize(
        return rc;
      }

  layer_resize(layer_get(ctx), width, height);
  return OK;
      return layer_resize(layer_get(ctx), width, height);
    }

    default:
      return err_invalid_nargs(args.size(), 2);
  }
}

Layer* layer_get(Context* ctx) {
+5 −1
Original line number Diff line number Diff line
@@ -46,11 +46,15 @@ ReturnCode layer_create(
    Context* ctx,
    std::unique_ptr<Layer>* layer_storage);

void layer_resize(
ReturnCode layer_resize(
    Layer* layer,
    Measure width,
    Measure height);

ReturnCode layer_resize(
    Layer* layer,
    const std::string& size);

ReturnCode layer_resize(
    Context* ctx,
    const Expr* expr);
+1 −0
Original line number Diff line number Diff line
(layer/resize A4)
+5 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Generated by clip v0.7.0 (clip-lang.org) -->
<svg xmlns="http://www.w3.org/2000/svg" width="793.700787" height="1122.519685">
  <rect width="793.700787" height="1122.519685" fill="#ffffff" fill-opacity="1.000000"/>
</svg>
 No newline at end of file
Loading