krupuk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b03ae8d4085ac85218fbf8a267d21c4183419660cd34fcacd77edddd1b005b9f
4
+ data.tar.gz: c90e1d77608c146d3e048df84479bd3a73be45c78febaab4e7724acc3f7bcf19
5
+ SHA512:
6
+ metadata.gz: 18342c57ea70a6929c81055da497ddf2b1c2d91883a203e247c340e41fe89ee1bc3ab0f68824ffa807b8c6eeecb1fb918f8a14d2646d39dc296407fd618dfed8
7
+ data.tar.gz: 8eedf875679626d9ae5e86efd52f66041dfb760ac68c92d2859cec832d0d969e57028e4bf7349edd2bfa96b433eada8667de86479cf07f0c4872efd37505c819
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Krupuk
4
+ class BackMirror
5
+ def initialize(columns:)
6
+ @columns = columns
7
+ end
8
+
9
+ def back_x(front_col:, card_width:)
10
+ mirrored_col = @columns - 1 - front_col
11
+ mirrored_col * card_width
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Krupuk
4
+ class Configuration
5
+ attr_accessor :card_width, :card_height, :margin, :columns, :rows,
6
+ :page_layout, :page_size, :cut_marks
7
+
8
+ def initialize
9
+ @card_width = 180
10
+ @card_height = 252
11
+ @margin = 18
12
+ @columns = 4
13
+ @rows = 2
14
+ @page_layout = :landscape
15
+ @page_size = "LETTER"
16
+ @cut_marks = true
17
+ end
18
+
19
+ def cards_per_sheet
20
+ columns * rows
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Krupuk
4
+ class CutMarks
5
+ TICK = 18
6
+
7
+ def self.draw(pdf, x, y, config)
8
+ w = config.card_width
9
+ h = config.card_height
10
+
11
+ pdf.stroke_color("000000")
12
+ pdf.line_width(1)
13
+ pdf.transparent(0.3) do
14
+ pdf.stroke_line([x, y + h], [x + TICK, y + h])
15
+ pdf.stroke_line([x, y + h], [x, y + h - TICK])
16
+
17
+ pdf.stroke_line([x + w, y + h], [x + w - TICK, y + h])
18
+ pdf.stroke_line([x + w, y + h], [x + w, y + h - TICK])
19
+
20
+ pdf.stroke_line([x, y], [x + TICK, y])
21
+ pdf.stroke_line([x, y], [x, y + TICK])
22
+
23
+ pdf.stroke_line([x + w, y], [x + w - TICK, y])
24
+ pdf.stroke_line([x + w, y], [x + w, y + TICK])
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Krupuk
4
+ class Layout
5
+ def initialize(pack)
6
+ @pack = pack
7
+ @config = Krupuk.configuration
8
+ @pdf = create_pdf
9
+ end
10
+
11
+ def render
12
+ @pack.cards
13
+ .each_slice(@config.cards_per_sheet)
14
+ .each_with_index do |sheet_cards, i|
15
+ Sheet.new(sheet_cards, pdf: @pdf, config: @config, first_sheet: i.zero?).render
16
+ end
17
+ @pdf
18
+ end
19
+
20
+ private
21
+
22
+ def create_pdf
23
+ pdf = Prawn::Document.new(page_layout: @config.page_layout, page_size: @config.page_size)
24
+ pdf.font_families.update(@pack.fonts) if @pack.respond_to?(:fonts) && @pack.fonts
25
+ pdf
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Krupuk
4
+ class Sheet
5
+ def initialize(cards, pdf:, config:, first_sheet:)
6
+ @cards = cards
7
+ @pdf = pdf
8
+ @config = config
9
+ @first_sheet = first_sheet
10
+ @mirror = BackMirror.new(columns: config.columns)
11
+ end
12
+
13
+ def render
14
+ render_fronts_page
15
+ render_backs_page
16
+ end
17
+
18
+ private
19
+
20
+ def render_fronts_page
21
+ @pdf.start_new_page unless @first_sheet
22
+ @cards.each_with_index do |card, i|
23
+ x, y = front_position(i)
24
+ CutMarks.draw(@pdf, x, y, @config) if @config.cut_marks
25
+ card.draw_front(@pdf, x, y)
26
+ end
27
+ end
28
+
29
+ def render_backs_page
30
+ @pdf.start_new_page
31
+ @cards.each_with_index do |card, i|
32
+ x, y = back_position(i)
33
+ card.draw_back(@pdf, x, y)
34
+ end
35
+ end
36
+
37
+ def front_position(i)
38
+ col = i % @config.columns
39
+ row = i / @config.columns
40
+ x = col * @config.card_width
41
+ y = @config.margin + (@config.rows - 1 - row) * @config.card_height
42
+ [x, y]
43
+ end
44
+
45
+ def back_position(i)
46
+ col = i % @config.columns
47
+ row = i / @config.columns
48
+ x = @mirror.back_x(front_col: col, card_width: @config.card_width)
49
+ y = @config.margin + (@config.rows - 1 - row) * @config.card_height
50
+ [x, y]
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Krupuk
4
+ VERSION = "0.1.0"
5
+ end
data/lib/krupuk.rb ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prawn"
4
+ require_relative "krupuk/version"
5
+ require_relative "krupuk/configuration"
6
+ require_relative "krupuk/back_mirror"
7
+ require_relative "krupuk/cut_marks"
8
+ require_relative "krupuk/sheet"
9
+ require_relative "krupuk/layout"
10
+
11
+ module Krupuk
12
+ def self.configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def self.configure
17
+ yield configuration
18
+ end
19
+
20
+ def self.render(pack)
21
+ Layout.new(pack).render
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: krupuk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sal Espinosa
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: prawn
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '2.4'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '2.4'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '3'
32
+ description: Given a pack of cards (each with a front and a back), Krupuk lays them
33
+ out on printable sheets with mirrored backs for duplex printing.
34
+ email:
35
+ - sespinos@gmail.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - lib/krupuk.rb
41
+ - lib/krupuk/back_mirror.rb
42
+ - lib/krupuk/configuration.rb
43
+ - lib/krupuk/cut_marks.rb
44
+ - lib/krupuk/layout.rb
45
+ - lib/krupuk/sheet.rb
46
+ - lib/krupuk/version.rb
47
+ homepage: https://github.com/sespinos/krupuk
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://github.com/sespinos/krupuk
52
+ source_code_uri: https://github.com/sespinos/krupuk/tree/main
53
+ changelog_uri: https://github.com/sespinos/krupuk/blob/main/CHANGELOG.md
54
+ rubygems_mfa_required: 'true'
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.6.9
70
+ specification_version: 4
71
+ summary: Prawn-based layout engine for printing double-sided card sheets
72
+ test_files: []