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