Sha256: e3852c3d5ccd0a0d59ae22924095f89184bbd8096bf0d698e22d4f7e97a63d02

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require_relative 'stack_view_row_renderer'

module Thinreports
  module SectionReport
    module Renderer
      class StackViewRenderer
        def initialize(pdf)
          @pdf = pdf
          @row_renderer = Renderer::StackViewRowRenderer.new(pdf)
        end

        RowLayout = Struct.new(:row, :height, :top)

        def section_height(shape)
          row_layouts = build_row_layouts(shape.rows)

          total_row_height = row_layouts.sum(0, &:height)
          float_content_bottom = row_layouts
            .map { |l| row_renderer.calc_float_content_bottom(l.row) + l.top }
            .max.to_f

          [total_row_height, float_content_bottom].max
        end

        def render(shape)
          doc = pdf.pdf

          x, y, w = shape.format.attributes.values_at('x', 'y', 'width')
          doc.bounding_box([x, doc.bounds.height - y], width: w, height: section_height(shape)) do
            shape.rows.each do |row|
              row_renderer.render(row)
            end
          end
        end

        private

        attr_reader :pdf, :row_renderer

        def build_row_layouts(rows)
          row_layouts = rows.map { |row| RowLayout.new(row, row_renderer.section_height(row)) }

          row_layouts.inject(0) do |top, row_layout|
            row_layout.top = top
            top + row_layout.height
          end

          row_layouts
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
thinreports-0.14.2 lib/thinreports/section_report/pdf/renderer/stack_view_renderer.rb
thinreports-0.14.1 lib/thinreports/section_report/pdf/renderer/stack_view_renderer.rb
thinreports-0.14.0 lib/thinreports/section_report/pdf/renderer/stack_view_renderer.rb
thinreports-0.13.1 lib/thinreports/section_report/pdf/renderer/stack_view_renderer.rb
thinreports-0.13.0 lib/thinreports/section_report/pdf/renderer/stack_view_renderer.rb
thinreports-0.12.1 lib/thinreports/section_report/pdf/renderer/stack_view_renderer.rb
thinreports-0.12.0 lib/thinreports/section_report/pdf/renderer/stack_view_renderer.rb