File: grid.rb

package info (click to toggle)
ruby-prawn 2.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,528 kB
  • sloc: ruby: 17,688; sh: 43; makefile: 20
file content (449 lines) | stat: -rw-r--r-- 10,637 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# frozen_string_literal: true

module Prawn
  class Document # rubocop: disable Style/Documentation
    # @group Experimental API

    # Defines the grid system for a particular document. Takes the number of
    # rows and columns and the width to use for the gutter as the
    # keys :rows, :columns, :gutter, :row_gutter, :column_gutter
    #
    # @note A completely new grid object is built each time `define_grid`
    #   is called. This means that all subsequent calls to grid() will use
    #   the newly defined Grid object -- grids are not nestable like
    #   bounding boxes are.
    #
    # @param options [Hash{Symbol => any}]
    # @option options :columns [Integer] Number of columns in the grid.
    # @option options :rows [Integer] Number of rows in the grid.
    # @option options :gutter [Number] Gutter size. `:row_gutter` and
    #   `:column_gutter` are ignored if specified.
    # @option options :row_gutter [Number] Row gutter size.
    # @option options :column_gutter [Number] Column gutter size.
    # @return [Grid]
    def define_grid(options = {})
      @boxes = nil
      @grid = Grid.new(self, options)
    end

    # A method that can either be used to access a particular grid on the page
    # or work with the grid system directly.
    #
    # @overload grid
    #   Get current grid.
    #
    #   @return [Grid]
    #
    # @overload grid(row, column)
    #   Get a grid box.
    #
    #   @param row [Integer]
    #   @param column [Integer]
    #   @return [GridBox]
    #
    # @overload grid(box1, box2)
    #   Get a grid multi-box.
    #
    #   @param box1 [Array(Integer, Integer)] Start box coordinates.
    #   @param box2 [Array(Integer, Integer)] End box coordinates.
    #   @return [MultiBox]
    def grid(*args)
      @boxes ||= {}
      @boxes[args] ||=
        if args.empty?
          @grid
        else
          g1, g2 = args

          if g1.is_a?(Array) && g2.is_a?(Array) &&
              g1.length == 2 && g2.length == 2
            multi_box(single_box(*g1), single_box(*g2))
          else
            single_box(g1, g2)
          end
        end
    end

    # A Grid represents the entire grid system of a Page and calculates
    # the column width and row height of the base box.
    #
    # @group Experimental API
    class Grid
      # @private
      # @return [Prawn::Document]
      attr_reader :pdf

      # Number of columns in the grid.
      # @return [Integer]
      attr_reader :columns

      # Number of rows in the grid.
      # @return [Integer]
      attr_reader :rows

      # Gutter size.
      # @return [Number]
      attr_reader :gutter

      # Row gutter size.
      # @return [Number]
      attr_reader :row_gutter

      # Column gutter size.
      # @return [Number]
      attr_reader :column_gutter

      # @param pdf [Prawn::Document]
      # @param options [Hash{Symbol => any}]
      # @option options :columns [Integer] Number of columns in the grid.
      # @option options :rows [Integer] Number of rows in the grid.
      # @option options :gutter [Number] Gutter size. `:row_gutter` and
      #   `:column_gutter` are ignored if specified.
      # @option options :row_gutter [Number] Row gutter size.
      # @option options :column_gutter [Number] Column gutter size.
      def initialize(pdf, options = {})
        valid_options = %i[columns rows gutter row_gutter column_gutter]
        Prawn.verify_options(valid_options, options)

        @pdf = pdf
        @columns = options[:columns]
        @rows = options[:rows]
        apply_gutter(options)
      end

      # Calculates the base width of boxes.
      #
      # @return [Float]
      def column_width
        @column_width ||= subdivide(pdf.bounds.width, columns, column_gutter)
      end

      # Calculates the base height of boxes.
      #
      # @return [Float]
      def row_height
        @row_height ||= subdivide(pdf.bounds.height, rows, row_gutter)
      end

      # Diagnostic tool to show all of the grid boxes.
      #
      # @param color [Color]
      # @return [void]
      def show_all(color = 'CCCCCC')
        rows.times do |row|
          columns.times do |column|
            pdf.grid(row, column).show(color)
          end
        end
      end

      private

      def subdivide(total, num, gutter)
        (Float(total) - (gutter * Float((num - 1)))) / Float(num)
      end

      def apply_gutter(options)
        if options.key?(:gutter)
          @gutter = Float(options[:gutter])
          @row_gutter = @gutter
          @column_gutter = @gutter
        else
          @row_gutter = Float(options[:row_gutter])
          @column_gutter = Float(options[:column_gutter])
          @gutter = 0
        end
      end
    end

    # A Box is a class that represents a bounded area of a page.
    # A Grid object has methods that allow easy access to the coordinates of
    # its corners, which can be plugged into most existing Prawn methods.
    #
    # @group Experimental API
    class GridBox
      # @private
      attr_reader :pdf

      def initialize(pdf, rows, columns)
        @pdf = pdf
        @rows = rows
        @columns = columns
      end

      # Mostly diagnostic method that outputs the name of a box as
      # col_num, row_num
      #
      # @return [String]
      def name
        "#{@rows},#{@columns}"
      end

      # @private
      def total_height
        Float(pdf.bounds.height)
      end

      # Width of a box.
      #
      # @return [Float]
      def width
        Float(grid.column_width)
      end

      # Height of a box.
      #
      # @return [Float]
      def height
        Float(grid.row_height)
      end

      # Width of the gutter.
      #
      # @return [Float]
      def gutter
        Float(grid.gutter)
      end

      # x-coordinate of left side.
      #
      # @return [Float]
      def left
        @left ||= (width + grid.column_gutter) * Float(@columns)
      end

      # x-coordinate of right side.
      #
      # @return [Float]
      def right
        @right ||= left + width
      end

      # y-coordinate of the top.
      #
      # @return [Float]
      def top
        @top ||= total_height - ((height + grid.row_gutter) * Float(@rows))
      end

      # y-coordinate of the bottom.
      #
      # @return [Float]
      def bottom
        @bottom ||= top - height
      end

      # x,y coordinates of top left corner.
      #
      # @return [Array(Float, Float)]
      def top_left
        [left, top]
      end

      # x,y coordinates of top right corner.
      #
      # @return [Array(Float, Float)]
      def top_right
        [right, top]
      end

      # x,y coordinates of bottom left corner.
      #
      # @return [Array(Float, Float)]
      def bottom_left
        [left, bottom]
      end

      # x,y coordinates of bottom right corner.
      #
      # @return [Array(Float, Float)]
      def bottom_right
        [right, bottom]
      end

      # Creates a standard bounding box based on the grid box.
      #
      # @yield
      # @return [void]
      def bounding_box(&blk)
        pdf.bounding_box(top_left, width: width, height: height, &blk)
      end

      # Drawn the box. Diagnostic method.
      #
      # @param grid_color [Color]
      # @return [void]
      def show(grid_color = 'CCCCCC')
        bounding_box do
          original_stroke_color = pdf.stroke_color

          pdf.stroke_color = grid_color
          pdf.text(name)
          pdf.stroke_bounds

          pdf.stroke_color = original_stroke_color
        end
      end

      private

      def grid
        pdf.grid
      end
    end

    # A MultiBox is specified by 2 Boxes and spans the areas between.
    #
    # @group Experimental API
    class MultiBox
      def initialize(pdf, box1, box2)
        @pdf = pdf
        @boxes = [box1, box2]
      end

      # @private
      attr_reader :pdf

      # Mostly diagnostic method that outputs the name of a box.
      #
      # @return [String]
      def name
        @boxes.map(&:name).join(':')
      end

      # @private
      def total_height
        @boxes[0].total_height
      end

      # Width of a box.
      #
      # @return [Float]
      def width
        right_box.right - left_box.left
      end

      # Height of a box.
      #
      # @return [Float]
      def height
        top_box.top - bottom_box.bottom
      end

      # Width of the gutter.
      #
      # @return [Float]
      def gutter
        @boxes[0].gutter
      end

      # x-coordinate of left side.
      #
      # @return [Float]
      def left
        left_box.left
      end

      # x-coordinate of right side.
      #
      # @return [Float]
      def right
        right_box.right
      end

      # y-coordinate of the top.
      #
      # @return [Float]
      def top
        top_box.top
      end

      # y-coordinate of the bottom.
      #
      # @return [Float]
      def bottom
        bottom_box.bottom
      end

      # x,y coordinates of top left corner.
      #
      # @return [Array(Float, Float)]
      def top_left
        [left, top]
      end

      # x,y coordinates of top right corner.
      #
      # @return [Array(Float, Float)]
      def top_right
        [right, top]
      end

      # x,y coordinates of bottom left corner.
      #
      # @return [Array(Float, Float)]
      def bottom_left
        [left, bottom]
      end

      # x,y coordinates of bottom right corner.
      #
      # @return [Array(Float, Float)]
      def bottom_right
        [right, bottom]
      end

      # Creates a standard bounding box based on the grid box.
      #
      # @yield
      # @return [void]
      def bounding_box(&blk)
        pdf.bounding_box(top_left, width: width, height: height, &blk)
      end

      # Drawn the box. Diagnostic method.
      #
      # @param grid_color [Color]
      # @return [void]
      def show(grid_color = 'CCCCCC')
        bounding_box do
          original_stroke_color = pdf.stroke_color

          pdf.stroke_color = grid_color
          pdf.text(name)
          pdf.stroke_bounds

          pdf.stroke_color = original_stroke_color
        end
      end

      private

      def left_box
        @left_box ||= @boxes.min_by(&:left)
      end

      def right_box
        @right_box ||= @boxes.max_by(&:right)
      end

      def top_box
        @top_box ||= @boxes.max_by(&:top)
      end

      def bottom_box
        @bottom_box ||= @boxes.min_by(&:bottom)
      end
    end

    private

    def single_box(rows, columns)
      GridBox.new(self, rows, columns)
    end

    def multi_box(box1, box2)
      MultiBox.new(self, box1, box2)
    end
  end
end