Horizon
canvas.hpp
1 #pragma once
2 #include "common/common.hpp"
3 #include "common/text.hpp"
4 #include "layer_display.hpp"
5 #include "selectables.hpp"
6 #include "target.hpp"
7 #include "triangle.hpp"
8 #include "object_ref.hpp"
9 #include "fragment_cache.hpp"
10 #include "util/placement.hpp"
11 #include "util/text_data.hpp"
12 #include "color_palette.hpp"
13 #include <array>
14 #include <set>
15 #include <unordered_map>
16 #include <deque>
17 #include <list>
18 #include "picture.hpp"
19 #include "pool/unit.hpp"
20 #include "util/vector_pair.hpp"
21 #include "util/text_data.hpp"
22 
23 namespace horizon {
24 class Canvas {
25  friend Selectables;
26  friend class SelectionFilter;
27  friend class CanvasAnnotation;
28 
29 public:
30  Canvas();
31  virtual ~Canvas()
32  {
33  }
34  virtual void clear();
35  void update(const class Symbol &sym, const Placement &transform = Placement(), bool edit = true);
36  void update(const class Sheet &sheet);
37  void update(const class Padstack &padstack, bool edit = true);
38  void update(const class Package &pkg, bool edit = true);
39  enum class PanelMode { INCLUDE, SKIP };
40  void update(const class Board &brd, PanelMode mode = PanelMode::INCLUDE);
41  void update(const class Frame &fr, bool edit = true);
42  void update(const class Decal &dec, bool edit = true);
43  void update(const class BlockSymbol &sym, bool edit = true);
44 
45  ObjectRef add_line(const std::deque<Coordi> &pts, int64_t width, ColorP color, int layer);
46  void remove_obj(const ObjectRef &r);
47  void hide_obj(const ObjectRef &r);
48  void show_obj(const ObjectRef &r);
49  void set_flags(const ObjectRef &r, uint8_t mask_set, uint8_t mask_clear);
50  void set_flags_all(uint8_t mask_set, uint8_t mask_clear);
51 
52  void reset_color2();
53  void set_color2(const ObjectRef &r, uint8_t color);
54 
55  void show_all_obj();
56 
57  virtual void update_markers()
58  {
59  }
60 
61  const LayerDisplay &get_layer_display(int index) const;
62  void set_layer_display(int index, const LayerDisplay &ld);
63  void set_layer_color(int layer, const Color &color);
64 
65  bool layer_is_visible(int layer) const;
66  bool layer_is_visible(LayerRange layer) const;
67 
68  bool show_all_junctions_in_schematic = false;
69  bool show_text_in_tracks = false;
70  bool show_text_in_vias = false;
71 
72  virtual bool get_flip_view() const
73  {
74  return false;
75  };
76 
77  virtual float get_view_angle() const
78  {
79  return 0;
80  }
81 
82  std::pair<Coordf, Coordf> get_bbox(bool visible_only = true) const;
83 
84  static const int first_overlay_layer = 30000;
85 
86 protected:
87  std::map<int, vector_pair<Triangle, TriangleInfo>> triangles;
88  std::list<CanvasPicture> pictures;
89  void add_triangle(int layer, const Coordf &p0, const Coordf &p1, const Coordf &p2, ColorP co, uint8_t flg = 0,
90  uint8_t color2 = 0);
91 
92  using ObjectRefIdx = std::map<int, std::pair<size_t, size_t>>;
93  std::unordered_map<ObjectRef, ObjectRefIdx> object_refs;
94  void begin_group(int layer);
95  void end_group();
96  std::vector<ObjectRef> object_refs_current;
97  std::vector<ObjectRefIdx *> object_ref_idx;
98  void object_ref_push(const ObjectRef &ref);
99  template <typename... Args> void object_ref_push(Args... args)
100  {
101  object_ref_push(ObjectRef(args...));
102  }
103  void object_ref_pop();
104 
105  void render(const class Symbol &sym, bool on_sheet = false, bool smashed = false, ColorP co = ColorP::FROM_LAYER);
106  void render(const class Junction &junc, bool interactive = true, ObjectType mode = ObjectType::INVALID);
107  void render(const class SchematicJunction &junc);
108  void render(const class Line &line, bool interactive = true, ColorP co = ColorP::FROM_LAYER);
109  void render(const class SymbolPin &pin, bool interactive = true, ColorP co = ColorP::FROM_LAYER);
110  void render(const class Arc &arc, bool interactive = true, ColorP co = ColorP::FROM_LAYER);
111  void render(const class Sheet &sheet);
112  void render(const class SchematicSymbol &sym);
113  void render(const class LineNet &line);
114  void render(const class NetLabel &label);
115  void render(const class BusLabel &label);
116  void render(const class Warning &warn);
117  void render(const class PowerSymbol &sym);
118  void render(const class BusRipper &ripper);
119  void render(const class Text &text, bool interactive = true, ColorP co = ColorP::FROM_LAYER);
120  void render(const class Padstack &padstack, bool interactive = true);
121  void render(const class Polygon &polygon, bool interactive = true, ColorP co = ColorP::FROM_LAYER);
122  void render(const class Shape &shape, bool interactive = true);
123  void render(const class Hole &hole, bool interactive = true);
124  void render(const class Package &package, bool interactive = true, bool smashed = false,
125  bool omit_silkscreen = false, bool omit_outline = false, bool on_panel = false);
126  void render_pad_overlay(const class Pad &pad, bool interactive);
127  void render(const class Pad &pad);
128  enum class OutlineMode { INCLUDE, OMIT };
129  void render(const class Board &brd, bool interactive = true, PanelMode mode = PanelMode::INCLUDE,
130  OutlineMode outline_mode = OutlineMode::INCLUDE);
131  void render(const class BoardPackage &pkg, bool interactive = true);
132  void render(const class BoardHole &hole, bool interactive = true);
133  void render(const class Track &track, bool interactive = true);
134  void render(const class Via &via, bool interactive = true);
135  void render(const class Dimension &dim);
136  void render(const class Frame &frame, bool on_sheet = false);
137  void render(const class ConnectionLine &line);
138  void render(const class BoardPanel &panel);
139  void render(const class Picture &pic, bool interactive = true);
140  void render(const class Decal &decal, bool interactive = true);
141  void render(const class BoardDecal &decal);
142  void render(const class BlockSymbol &sym, bool on_sheet = false);
143  void render(const class BlockSymbolPort &port, bool interactive = true);
144  void render(const class SchematicBlockSymbol &sym);
145 
146  bool needs_push = true;
147  virtual void request_push() = 0;
148  virtual void push() = 0;
149 
150  void set_lod_size(float size);
151 
152  void draw_line(const Coord<float> &a, const Coord<float> &b, ColorP color = ColorP::FROM_LAYER, int layer = 10000,
153  bool tr = true, uint64_t width = 0);
154  void draw_cross(const Coord<float> &o, float size, ColorP color = ColorP::FROM_LAYER, int layer = 10000,
155  bool tr = true, uint64_t width = 0);
156  void draw_plus(const Coord<float> &o, float size, ColorP color = ColorP::FROM_LAYER, int layer = 10000,
157  bool tr = true, uint64_t width = 0);
158  void draw_box(const Coord<float> &o, float size, ColorP color = ColorP::FROM_LAYER, int layer = 10000,
159  bool tr = true, uint64_t width = 0);
160  void draw_arc(const Coord<float> &center, float radius, float a0, float a1, ColorP color = ColorP::FROM_LAYER,
161  int layer = 10000);
162  void draw_circle(const Coord<float> &center, float radius, ColorP color = ColorP::FROM_LAYER, int layer = 10000);
163  std::pair<Coordf, Coordf> draw_arc2(const Coord<float> &center, float radius0, float a0, float a1, ColorP color,
164  int layer, uint64_t width);
165  void draw_arc0(const Coord<float> &center, float radius0, float a0, float a1, ColorP color, int layer,
166  uint64_t width);
167 
168  struct TextOptions {
169  uint64_t width = 0;
170  bool flip = false;
171  bool mirror = false;
172  bool draw = true;
173  TextData::Font font = TextData::Font::SIMPLEX;
174  bool center = false;
175  bool allow_upside_down = false;
176  };
177  std::pair<Coordf, Coordf> draw_text(const Coordf &p, float size, const std::string &rtext, int angle,
178  TextOrigin origin, ColorP color, int layer, const TextOptions &opts);
179 
180  virtual void draw_bitmap_text(const Coordf &p, float scale, const std::string &rtext, int angle, ColorP color,
181  int layer)
182  {
183  }
184 
185  virtual std::pair<Coordf, Coordf> measure_bitmap_text(const std::string &text) const
186  {
187  return {{0, 0}, {0, 0}};
188  }
189 
190  enum class TextBoxMode { FULL, LOWER, UPPER };
191 
192  virtual void draw_bitmap_text_box(const Placement &q, float width, float height, const std::string &s, ColorP color,
193  int layer, TextBoxMode mode)
194  {
195  }
196 
197  void draw_error(const Coordf &center, float scale, const std::string &text, bool tr = true);
198  std::tuple<Coordf, Coordf, Coordi> draw_flag(const Coordf &position, const std::string &txt, int64_t size,
199  Orientation orientation, ColorP color = ColorP::FROM_LAYER);
200  void draw_lock(const Coordf &center, float size, ColorP color = ColorP::FROM_LAYER, int layer = 10000,
201  bool tr = true);
202 
203  virtual bool img_layer_is_visible(int layer) const
204  {
205  return true;
206  }
207  virtual void img_net(const class Net *net)
208  {
209  }
210  virtual void img_polygon(const Polygon &poly, bool tr = true)
211  {
212  }
213  virtual void img_padstack(const Padstack &ps)
214  {
215  }
216  virtual void img_set_padstack(bool v)
217  {
218  }
219  virtual void img_line(const Coordi &p0, const Coordi &p1, const uint64_t width, int layer = 10000, bool tr = true);
220  virtual void img_hole(const Hole &hole)
221  {
222  }
223  virtual void img_patch_type(PatchType type)
224  {
225  }
226  virtual void img_draw_text(const Coordf &p, float size, const std::string &rtext, int angle, bool flip,
227  TextOrigin origin, int layer = 10000, uint64_t width = 0,
228  TextData::Font font = TextData::Font::SIMPLEX, bool center = false, bool mirror = false)
229  {
230  }
231  bool img_mode = false;
232  bool img_auto_line = false;
233 
234  Placement transform;
235  void transform_save();
236  void transform_restore();
237  std::vector<Placement> transforms;
238 
239  Selectables selectables;
240  std::vector<Target> targets;
241  Target target_current;
242 
243  const class LayerProvider *layer_provider = nullptr;
244  std::map<int, Color> layer_colors;
245  Color get_layer_color(int layer) const;
246  int work_layer = 0;
247  std::map<int, LayerDisplay> layer_display;
248 
249  TriangleInfo::Type triangle_type_current = TriangleInfo::Type::NONE;
250 
251  std::map<std::pair<LayerRange, bool>, int> overlay_layers; // layer, ignore_flip -> overlay layer
252  int overlay_layer_current = first_overlay_layer;
253  int get_overlay_layer(const LayerRange &layer, bool ignore_flip = false);
254  bool is_overlay_layer(int overlay_layer, int layer) const;
255 
256  FragmentCache fragment_cache;
257 
258 private:
259  uint8_t lod_current = 0;
260 
261  int group_layer = 0;
262  vector_pair<Triangle, TriangleInfo> *group_tris = nullptr;
263  size_t group_size = 0;
264 
265  void draw_direction(Pin::Direction dir, ColorP color);
266 
267  TextData::Buffer text_data_buffer;
268 };
269 } // namespace horizon
Graphical arc.
Definition: arc.hpp:20
Definition: block_symbol.hpp:20
Definition: block_symbol.hpp:51
Definition: board_decal.hpp:11
Definition: board_hole.hpp:12
Definition: board_package.hpp:15
Definition: board_panel.hpp:7
Definition: board.hpp:43
Makes a Bus available on the schematic.
Definition: bus_label.hpp:16
Make a Bus member's Net available on the schematic.
Definition: bus_ripper.hpp:19
Definition: annotation.hpp:7
Definition: canvas.hpp:24
Definition: common.hpp:267
Definition: connection_line.hpp:15
Definition: decal.hpp:16
Definition: dimension.hpp:12
Definition: frame.hpp:21
A hole with diameter and position, that's it.
Definition: hole.hpp:18
A Junction is a point in 2D-Space.
Definition: junction.hpp:20
Definition: layer_display.hpp:5
Definition: layer_range.hpp:7
LineNet is similar to Line, except it denotes electrical connection.
Definition: line_net.hpp:17
Graphical line.
Definition: line.hpp:19
Displays the junction's Net name it is attached to.
Definition: net_label.hpp:20
Definition: object_ref.hpp:7
Definition: package.hpp:30
Definition: pad.hpp:15
Definition: padstack.hpp:21
Definition: picture.hpp:10
Definition: placement.hpp:8
Polygon used in Padstack, Package and Board for specifying filled Regions.
Definition: polygon.hpp:27
GND symbols and the like.
Definition: power_symbol.hpp:16
Definition: schematic_block_symbol.hpp:12
Definition: schematic_junction.hpp:6
Definition: schematic_symbol.hpp:17
Definition: selectables.hpp:66
Definition: selection_filter.hpp:6
For commonly used Pad shapes.
Definition: shape.hpp:18
Definition: sheet.hpp:40
Definition: symbol.hpp:20
Definition: symbol.hpp:72
Used wherever a user-editable text is needed.
Definition: text.hpp:18
Definition: track.hpp:16
Definition: via.hpp:16
Definition: warning.hpp:6
zip_uint64_t uint64_t
zip_uint64_t_t typedef.
Definition: zip.hpp:108
zip_int64_t int64_t
zip_int64_t typedef.
Definition: zip.hpp:103
zip_uint8_t uint8_t
zip_uint8_t typedef.
Definition: zip.hpp:78
Definition: canvas.hpp:168