Skip to content

Commit cd6f574

Browse files
committed
Export full-size breadboard print templates
1 parent 245b628 commit cd6f574

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ bkrender circuit.bk.rb -o circuit.svg --theme dark --show-nets --legend
6464
bkrender circuit.bk.rb -o circuit.png --scale 3
6565
bkrender circuit.bk.rb -o circuit.webp --quality 85
6666
bkrender circuit.bk.rb -o circuit.pdf
67+
bkrender circuit.bk.rb --print-template -o board-template.pdf
6768
bkrender circuit.bk.rb -o circuit.html --theme dark
6869
bkrender --diff before.bk.rb after.bk.rb -o changes.html
6970
bkrender circuit.bk.rb --format svg > circuit.svg
@@ -98,6 +99,9 @@ PNG, JPEG, WebP, and PDF need a conversion backend; SVG works without one.
9899
- `--diff OLD NEW` creates a two-panel HTML viewer. Removed wires are red in
99100
the old view and added wires are green in the new view; matching wires use
100101
their normal colors.
102+
- `--print-template` makes a PDF with 2.54 mm hole spacing, the complete board,
103+
and the print palette. Print at 100% scale; the option fixes the output scale
104+
to 1 and requires `rsvg-convert`.
101105

102106
The demo was generated with:
103107

@@ -128,6 +132,7 @@ bkrender ../breadkit/examples/05_sensor_demo.bk.rb \
128132
| `--backend NAME` | `auto` | Use `rsvg`, `vips`, or `magick` for raster output. |
129133
| `--background COLOR` | white | Set the JPEG background with a CSS name, `#RGB`, or `#RRGGBB`; PNG and SVG reject it. |
130134
| `--quality N` | `90` | JPEG and WebP quality. |
135+
| `--print-template` | off | Export a full-board PDF with physical hole spacing for 100% printing. |
131136
| `--static` | off | Omit SVG layer controls and scripts. |
132137
| `--force` | off | Draw resolved elements even when input has layout errors. |
133138
| `--render-timeout SECONDS` | 60 | Limit external raster conversion time. |

‎lib/breadkit/render/cli.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def run(argv)
3030
opts.on("--background COLOR") { |value| options[:background] = value }
3131
opts.on("--static") { options[:static] = true }
3232
opts.on("--quality N", Integer) { |value| options[:quality] = value }
33+
opts.on("--print-template") { options[:print_template] = true }
3334
opts.on("--render-timeout SECONDS", Float) { |value| options[:render_timeout] = value }
3435
opts.on("--force") { options[:force] = true }
3536
opts.on("-v", "--version") { puts "bkrender #{VERSION}"; return 0 }
@@ -42,6 +43,10 @@ def run(argv)
4243
raise ArgumentError, "--diff requires OLD and NEW inputs" if options[:diff] && !second_input
4344
raise ArgumentError, "unexpected arguments: #{argv.join(' ')}" unless argv.empty?
4445
format = output_format(options)
46+
if options[:print_template]
47+
raise ArgumentError, "--print-template requires PDF output" unless format == "pdf"
48+
options.merge!(theme: "print", crop: "none", scale: 1.0, static: true)
49+
end
4550
raise ArgumentError, "--background is only supported for JPEG" if options[:background] && format != "jpeg"
4651
raise ArgumentError, "cannot write binary image data to a terminal; use -o PATH" if !%w[svg html].include?(format) && !options[:output] && $stdout.tty?
4752
return render_diff(input, second_input, options, format) if options[:diff]
@@ -61,6 +66,7 @@ def run(argv)
6166
state: state, active_layer: options[:layer], focus: options[:focus],
6267
highlight_net: options[:highlight_net] }
6368
svg = SvgRenderer.new.render(circuit, **render_options)
69+
svg = print_dimensions(svg) if options[:print_template]
6470
output = case format
6571
when "svg" then svg
6672
when "html" then html_viewer(svg, theme: options[:theme], circuit: circuit, render_options: render_options)
@@ -81,6 +87,12 @@ def run(argv)
8187

8288
private
8389

90+
def print_dimensions(svg)
91+
svg.sub(/(<svg\b[^>]*?\bwidth=")([\d.]+)(" height=")([\d.]+)(")/) do
92+
"#{$1}#{format('%.2f', $2.to_f * 0.254)}mm#{$3}#{format('%.2f', $4.to_f * 0.254)}mm#{$5}"
93+
end
94+
end
95+
8496
def output_format(options)
8597
extension = File.extname(options[:output].to_s).downcase
8698
from_path = { ".svg" => "svg", ".html" => "html", ".png" => "png", ".jpg" => "jpeg", ".jpeg" => "jpeg",

‎spec/breadkit/render_spec.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,18 @@
591591
skip e.message
592592
end
593593

594+
it "writes a full-board PDF with physical hole spacing for printing" do
595+
input = File.expand_path("../../../breadkit/examples/01_led_button.bk.rb", __dir__)
596+
Dir.mktmpdir do |directory|
597+
pdf = File.join(directory, "board.pdf")
598+
expect(described_class.new.run([input, "--print-template", "-o", pdf])).to eq(0)
599+
expect(File.binread(pdf)).to start_with("%PDF-".b)
600+
expect(described_class.new.run([input, "--print-template", "-o", File.join(directory, "board.png")])).to eq(2)
601+
end
602+
expect(described_class.new.send(:print_dimensions, '<svg width="100.00" height="200.00" viewBox="0 0 100 200">'))
603+
.to include('width="25.40mm" height="50.80mm"')
604+
end
605+
594606
it "rasterizes to PNG and JPEG when optional system backends are installed" do
595607
svg = Breadkit::Render::SvgRenderer.new.render(Breadkit.load(File.expand_path("../../../breadkit/examples/01_led_button.bk.rb", __dir__)))
596608
rasterizer = Breadkit::Render::Rasterizer.new

0 commit comments

Comments
 (0)