Skip to content

Commit

Permalink
add support for the 'mm' unit
Browse files Browse the repository at this point in the history
  • Loading branch information
asmuth committed May 14, 2020
1 parent 361a944 commit 95aa790
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 9 deletions.
17 changes: 10 additions & 7 deletions src/graphics/export_svg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"
Expand Down
31 changes: 30 additions & 1 deletion src/graphics/measure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* limitations under the License.
*/
#include "measure.h"
#include "layer.h"
#include <assert.h>
#include <iostream>

Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/graphics/measure.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/plot/plotgen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
1 change: 1 addition & 0 deletions test/layer/resize_mm.clp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(layer/resize 200mm 300mm)
5 changes: 5 additions & 0 deletions test/layer/resize_mm.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/layer/resize_mm_dpi.clp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(layer/resize 200mm 300mm)
(layer/set-dpi 240)
5 changes: 5 additions & 0 deletions test/layer/resize_mm_dpi.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/layer/resize_px.clp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(layer/resize 1920px 1080px)
5 changes: 5 additions & 0 deletions test/layer/resize_px.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/layer/resize_px_dpi.clp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(layer/resize 1920px 1080px)
(layer/set-dpi 240)
5 changes: 5 additions & 0 deletions test/layer/resize_px_dpi.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 95aa790

Please sign in to comment.