From 1c1d0fdf1ebffbddd7e6d6083010e55c19c482d9 Mon Sep 17 00:00:00 2001 From: Andreas Salzburger Date: Mon, 7 Oct 2024 18:36:33 +0200 Subject: [PATCH] with commit this time --- core/include/actsvg/core/svg.hpp | 8 +++ core/src/core/draw.cpp | 64 +++++++---------------- meta/include/actsvg/display/datamodel.hpp | 16 ++---- meta/include/actsvg/display/geometry.hpp | 28 +++------- meta/include/actsvg/display/sheets.hpp | 18 +++---- meta/src/display/geometry.cpp | 4 +- meta/src/display/grids.cpp | 4 +- meta/src/display/materials.cpp | 4 +- tests/common/playground.hpp | 4 +- tests/core/text.cpp | 31 +++++++++++ 10 files changed, 78 insertions(+), 103 deletions(-) diff --git a/core/include/actsvg/core/svg.hpp b/core/include/actsvg/core/svg.hpp index 32a977f..b18fd61 100644 --- a/core/include/actsvg/core/svg.hpp +++ b/core/include/actsvg/core/svg.hpp @@ -101,6 +101,14 @@ struct object { /// Summary object summary _summary = summary{}; + /// Static constructor for a group + static object create_group(const std::string &id_) { + object g; + g._tag = "g"; + g._id = id_; + return g; + } + /// An object is defined if a tag is set bool is_defined() const; diff --git a/core/src/core/draw.cpp b/core/src/core/draw.cpp index 811a5d1..2017873 100644 --- a/core/src/core/draw.cpp +++ b/core/src/core/draw.cpp @@ -169,9 +169,7 @@ svg::object bezier(const std::string &id_, const style::stroke &stroke_, const style::transform &transform_) { - svg::object c; - c._tag = "g"; - c._id = id_; + svg::object c = svg::object::create_group(id_); point2 lx = {0, 0}; point2 ld = {0, 0}; @@ -396,6 +394,7 @@ svg::object text(const std::string &id_, const point2 &p_, t._attribute_map["font-size"] = std::to_string(font_._size); t._field_span = font_._size * font_._line_spacing; + // Get the longest text line size_t l = 0; for (const auto &tl : text_) { l = l > tl.size() ? l : tl.size(); @@ -403,8 +402,8 @@ svg::object text(const std::string &id_, const point2 &p_, scalar fs = font_._size; - detail::adapt_range( - t, {{x, y - l}, {static_cast(x + 0.7 * fs * l), y + l}}); + detail::adapt_range(t, {{x, y - fs * text_.size()}, + {static_cast(x + 0.7 * fs * l), y + l}}); t._barycenter = utils::barycenter>( {{x, y - l}, {static_cast(x + 0.7 * fs * l), y + l}}); @@ -451,10 +450,7 @@ svg::object image_box(const std::string &id_, const std::string &href_, const svg::object &object_, const std::vector &highlight_, const std::string &onerror_) { - svg::object i; - - i._tag = "g"; - i._id = id_; + svg::object i = svg::object::create_group(id_); // Image box object svg::object imgb; @@ -506,9 +502,7 @@ svg::object connected_info_box( const style::font &text_font_, const style::stroke &stroke_, const svg::object &object_, const std::vector &highlight_) { - svg::object ib; - ib._tag = "g"; - ib._id = id_; + svg::object ib = svg::object::create_group(id_); size_t tew = 0; for (const auto &t : text_) { @@ -585,9 +579,7 @@ svg::object cartesian_grid(const std::string &id_, const style::stroke &stroke_, const style::transform &transform_) { // The grid group object - svg::object grid; - grid._tag = "g"; - grid._id = id_; + svg::object grid = svg::object::create_group(id_); scalar l0_min = l0_edges_[0]; scalar l0_max = l0_edges_[l0_edges_.size() - 1]; @@ -615,9 +607,7 @@ svg::object tiled_cartesian_grid(const std::string &id_, const style::stroke &stroke_, const style::transform &transform_) { // The grid group object - svg::object grid; - grid._tag = "g"; - grid._id = id_; + svg::object grid = svg::object::create_group(id_); // The list of grid sectors for (size_t il0 = 1; il0 < l0_edges_.size(); ++il0) { @@ -652,9 +642,7 @@ svg::object fan_grid(const std::string &id_, const style::stroke &stroke_, const style::transform &transform_) noexcept(false) { // The list of grid sectors - svg::object grid; - grid._tag = "g"; - grid._id = id_; + svg::object grid = svg::object::create_group(id_); if (x_low_edges_.size() != x_high_edges_.size()) { throw std::invalid_argument( @@ -698,9 +686,7 @@ svg::object tiled_fan_grid(const std::string &id_, const style::fill &fill_, const style::stroke &stroke_, const style::transform &transform_) noexcept(false) { - svg::object grid; - grid._tag = "g"; - grid._id = id_; + svg::object grid = svg::object::create_group(id_); // The list of grid sectors std::vector grid_tiles; @@ -761,9 +747,7 @@ svg::object polar_grid(const std::string &id_, const style::stroke &stroke_, const style::transform &transform_) { // The list of grid sectors - svg::object grid; - grid._tag = "g"; - grid._id = id_; + svg::object grid = svg::object::create_group(id_); scalar r_min = r_edges_[0]; scalar r_max = r_edges_[r_edges_.size() - 1]; @@ -802,9 +786,7 @@ svg::object tiled_polar_grid(const std::string &id_, const style::fill &fill_, const style::stroke &stroke_, const style::transform &transform_) { - svg::object grid; - grid._tag = "g"; - grid._id = id_; + svg::object grid = svg::object::create_group(id_); for (size_t ir = 1; ir < r_edges_.size(); ++ir) { // Grid svg object @@ -830,8 +812,7 @@ svg::object tiled_polar_grid(const std::string &id_, svg::object marker(const std::string &id_, const point2 &at_, const style::marker &marker_, scalar rot_) { - svg::object marker_group; - marker_group._tag = "g"; + svg::object marker_group = svg::object::create_group(id_); std::vector arrow_head; auto size = marker_._size; @@ -901,9 +882,7 @@ svg::object measure(const std::string &id_, const point2 &start_, const style::marker &end_marker_, const style::font &font_, const std::string &label_, const point2 &label_pos_) { // Measure group here we go - svg::object measure_group; - measure_group._tag = "g"; - measure_group._id = id_; + svg::object measure_group = svg::object::create_group(id_); auto mline = line(id_ + "_line", start_, end_, stroke_); measure_group.add_object(mline); @@ -934,9 +913,7 @@ svg::object arc_measure(const std::string &id_, scalar r_, const point2 &start_, const style::font &font_, const std::string &label_, const point2 &label_pos_) { // Measure group here we go - svg::object measure_group; - measure_group._tag = "g"; - measure_group._id = id_; + svg::object measure_group = svg::object::create_group(id_); measure_group.add_object( arc((id_ + "_arc"), r_, start_, end_, style::fill(), stroke_)); @@ -977,9 +954,7 @@ svg::object x_y_axes(const std::string &id_, const style::stroke &stroke_, const std::string &x_label_, const std::string &y_label_, const style::font &font_, const style::axis_markers<2u> &markers_) { - svg::object axes; - axes._tag = "g"; - axes._id = id_; + svg::object axes = svg::object::create_group(id_); auto x = line(id_ + "_x_axis", {x_range_[0], 0.}, {x_range_[1], 0.}, stroke_); @@ -1055,8 +1030,7 @@ svg::object gradient_box( const style::label &label_, const style::stroke &stroke_, const style::font &font_, const style::transform t_) { - svg::object g; - g._tag = "g"; + svg::object g = svg::object::create_group(id_); g._sterile = true; auto [w_, h_] = w_h_; @@ -1141,10 +1115,8 @@ svg::object from_template(const std::string &id_, const svg::object &ro_, const style::fill &f_, const style::stroke &s_, const style::transform t_) { // Create new svg object - svg::object nsvg; + svg::object nsvg = svg::object::create_group(id_); nsvg._sterile = true; - nsvg._id = id_; - nsvg._tag = "g"; // Refer to as the linker object svg::object use_obj; diff --git a/meta/include/actsvg/display/datamodel.hpp b/meta/include/actsvg/display/datamodel.hpp index 9043437..2b549ad 100644 --- a/meta/include/actsvg/display/datamodel.hpp +++ b/meta/include/actsvg/display/datamodel.hpp @@ -45,9 +45,7 @@ std::pair cluster( const style::fill& fill_m_ = style::fill{style::color{{0, 0, 255}}}, const std::array& expand_ = {2, 2}) { - svg::object cluster_group; - cluster_group._tag = "g"; - cluster_group._id = id_; + svg::object cluster_group = svg::object::create_group(id_); // Grid indices unsigned int i_min = std::numeric_limits::max(); @@ -293,9 +291,7 @@ std::pair cluster( } // Get the focussed grid - svg::object grid_area; - grid_area._tag = "g"; - grid_area._id = id_ + std::string("_focussed_grid"); + svg::object grid_area = svg::object::create_group(id_ + "_focussed_grid"); // Expand i_min = (i_min >= expand_[0]) ? i_min - expand_[0] : 0; @@ -331,9 +327,7 @@ template svg::object trajectory(const std::string& id, const trajectory_type& trajectory_, const view_type& view_, bool bezier_ = false) { - svg::object trajectory_group; - trajectory_group._tag = "g"; - trajectory_group._id = id; + svg::object trajectory_group = svg::object::create_group(id); std::vector points; points.reserve(trajectory_._path.size()); @@ -393,9 +387,7 @@ svg::object trajectory(const std::string& id, template svg::object seed(const std::string& id_, const seed_type& seed_, const view_type& view_) { - svg::object seed_group; - seed_group._tag = "g"; - seed_group._id = id_; + svg::object seed_group = svg::object::create_group(id_); // Draw the tranjectory if it exist if (seed_._trajectory.has_value()) { diff --git a/meta/include/actsvg/display/geometry.hpp b/meta/include/actsvg/display/geometry.hpp index 597ff5d..c8b1f94 100644 --- a/meta/include/actsvg/display/geometry.hpp +++ b/meta/include/actsvg/display/geometry.hpp @@ -154,9 +154,7 @@ svg::object surface(const std::string& id_, const surface_type& s_, if (o_._label_measures) { // make a copy & a group out of the object auto sc = s; - s = svg::object{}; - s._tag = "g"; - s._id = id_ + "_labeled_group"; + s = svg::object::create_group(id_ + "_labeled_group"); s.add_object(sc); // Draw min / max circles @@ -515,9 +513,7 @@ svg::object surface(const std::string& id_, const surface_type& s_, template svg::object oriented_polygon(const std::string& id_, const surface_type& s_, const view_type& v_) { - svg::object s; - s._tag = "g"; - s._id = id_; + svg::object s = svg::object::create_group(id_); s._fill._sterile = true; s._stroke._sterile = true; @@ -574,9 +570,7 @@ svg::object portal_link(const std::string& id_, [[maybe_unused]] const portal_type& p_, const typename portal_type::link& link_, const view_type& v_) { - svg::object l; - l._tag = "g"; - l._id = id_; + svg::object l = svg::object::create_group(id_); scalar d_z = static_cast(link_._end[2u] - link_._start[2u]); // View activation / deactivation @@ -593,9 +587,7 @@ svg::object portal_link(const std::string& id_, if (std::is_same_v and d_r <= std::numeric_limits::epsilon()) { - svg::object arr_xy; - arr_xy._tag = "g"; - arr_xy._id = id_ + "_arrow"; + svg::object arr_xy = svg::object::create_group(id_ + "_arrow"); arr_xy.add_object(draw::circle(id_ + "_arrow_top", {static_cast(link_._start[0u]), static_cast(link_._start[1u])}, @@ -653,9 +645,7 @@ svg::object portal_link(const std::string& id_, template svg::object portal(const std::string& id_, const portal_type& p_, const view_type& v_) { - svg::object p; - p._tag = "g"; - p._id = id_; + svg::object p = svg::object::create_group(id_); p._fill._sterile = true; p._stroke._sterile = true; @@ -681,9 +671,7 @@ svg::object portal(const std::string& id_, const portal_type& p_, template svg::object volume(const std::string& id_, const volume_type& dv_, const view_type& v_, bool p_ = true, bool s_ = true) { - svg::object v; - v._tag = "g"; - v._id = id_; + svg::object v = svg::object::create_group(id_); v._fill._sterile = true; v._stroke._sterile = true; @@ -751,9 +739,7 @@ svg::object volume(const std::string& id_, const volume_type& dv_, template svg::object detector(const std::string& id_, const detector_type& d_, const view_type& v_) { - svg::object d; - d._tag = "g"; - d._id = id_; + svg::object d = svg::object::create_group(id_); d._fill._sterile = true; d._stroke._sterile = true; diff --git a/meta/include/actsvg/display/sheets.hpp b/meta/include/actsvg/display/sheets.hpp index 01af0d1..7eb8439 100644 --- a/meta/include/actsvg/display/sheets.hpp +++ b/meta/include/actsvg/display/sheets.hpp @@ -48,9 +48,7 @@ svg::object surface_sheet_xy(const std::string& id_, const proto::surface& s_, const std::array& sh_ = {400., 400.}, bool fs_ = false) noexcept(false) { - svg::object so; - so._tag = "g"; - so._id = id_; + svg::object so = svg::object::create_group(id_); views::x_y x_y_view; @@ -550,9 +548,7 @@ svg::object sheet(const std::string& id_, sheet_type t_ = e_module_info, const std::array& s_sh_ = {600., 600.}) { - svg::object o_sheet; - o_sheet._tag = "g"; - o_sheet._id = id_; + svg::object o_sheet = svg::object::create_group(id_); view_type view; @@ -598,9 +594,8 @@ svg::object sheet(const std::string& id_, s_fill._fc._opacity = s._fill._fc._opacity; s._fill = s_fill; // The template sheet - svg::object s_sheet; - s_sheet._id = s._name + "_surface_sheet"; - s_sheet._tag = "g"; + svg::object s_sheet = + svg::object::create_group(s._name + "_surface_sheet"); // Background panel auto bg_panel = draw::rectangle(s._name + "_surface_sheet_bg", {0, 0}, @@ -667,9 +662,8 @@ svg::object sheet(const std::string& id_, // Add the modules & eventual extra objects for (auto [ib, module_batch] : utils::enumerate(modules)) { - svg::object sub_sheet; - sub_sheet._tag = "g"; - sub_sheet._id = id_ + "_sub_sheet_" + std::to_string(ib); + svg::object sub_sheet = + svg::object::create_group(id_ + "_sub_sheet" + std::to_string(ib)); sub_sheet.add_objects(module_batch); // Add the axes on top @todo add autmated font size adaption auto axis_font = __a_font; diff --git a/meta/src/display/geometry.cpp b/meta/src/display/geometry.cpp index d70adb4..227cf12 100644 --- a/meta/src/display/geometry.cpp +++ b/meta/src/display/geometry.cpp @@ -28,9 +28,7 @@ svg::object eta_lines( style::font>>& els_, const style::transform& tr_) { - svg::object e; - e._tag = "g"; - e._id = id_; + svg::object e = svg::object::create_group(id_); e._transform = tr_; auto theta_from_eta = [](scalar eta) -> scalar { diff --git a/meta/src/display/grids.cpp b/meta/src/display/grids.cpp index 6ada6c6..ef4da67 100644 --- a/meta/src/display/grids.cpp +++ b/meta/src/display/grids.cpp @@ -25,9 +25,7 @@ namespace display { svg::object grid(const std::string& id_, const proto::grid& g_) { // The grid object - svg::object g; - g._id = id_; - g._tag = "g"; + svg::object g = svg::object::create_group(id_); // Get the tiles std::vector grid_tiles; diff --git a/meta/src/display/materials.cpp b/meta/src/display/materials.cpp index 5e3ee49..bbe8be5 100644 --- a/meta/src/display/materials.cpp +++ b/meta/src/display/materials.cpp @@ -22,9 +22,7 @@ namespace display { svg::object surface_material(const std::string& id_, const proto::surface_material& m_) { - svg::object m; - m._tag = "g"; - m._id = id_; + svg::object m = svg::object::create_group(id_); m._sterile = true; // Create the grid object diff --git a/tests/common/playground.hpp b/tests/common/playground.hpp index ae5801f..eb109e7 100644 --- a/tests/common/playground.hpp +++ b/tests/common/playground.hpp @@ -29,12 +29,10 @@ static inline svg::object playground( const std::vector& ticks_x_ = {50., 100.}, const std::vector& ticks_y_ = {50., 100.}) { - svg::object pg; + svg::object pg = svg::object::create_group("playground"); pg._x_range = {llc_[0], ruc_[0]}; pg._y_range = {-ruc_[1], -llc_[1]}; - pg._tag = "g"; - /// Add a playground scalar half = 0.5; scalar cx = half * (llc_[0] + ruc_[0]); diff --git a/tests/core/text.cpp b/tests/core/text.cpp index 80456ac..b7ef3be 100644 --- a/tests/core/text.cpp +++ b/tests/core/text.cpp @@ -86,4 +86,35 @@ TEST(text, multiline_text) { fo << ftemplate._svg_tail; fo << ftemplate._html_tail; fo.close(); +} + +TEST(text, multiline_text_outside_playground) { + + svg::file ftemplate; + + svg::object pgo = svg::object::create_group("playground"); + + // Set a playground + auto pg = test::playground({-400, -400}, {400, 400}); + pgo.add_object(pg); + + style::color red{{255, 0, 0}}; + style::font fs; + fs._family = "Arial"; + fs._fc = red; + + // Write out the file, it should adapt the range for the outside text + std::ofstream fo; + fo.open("test_core_text_multiline_range.svg"); + + // Add the text + auto t0 = draw::text("t0", {500, 500}, {"line 0", "line 1", "line 2"}, fs); + auto t1 = draw::text("t1", {-500, -500}, + {"other line 0", "other line 1", "other line 2"}, fs); + pgo.add_object(t0); + pgo.add_object(t1); + + // Add to the file anf write out + ftemplate.add_object(pgo); + fo << ftemplate; } \ No newline at end of file