Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support arbitrary fill patterns #1202

Open
artbot1 opened this issue Apr 1, 2021 · 2 comments
Open

support arbitrary fill patterns #1202

artbot1 opened this issue Apr 1, 2021 · 2 comments

Comments

@artbot1
Copy link

artbot1 commented Apr 1, 2021

module Prawn
  module Graphics
    module Patterns

can generate shading patterns

PatternType: 2

but there is no support for fill patterns yet

PatternType: 1

here an example from the PDF doc

15 0 obj
<< /Type /Pattern
/PatternType 1          % Tiling pattern
/PaintType 1
/TilingType 2
/BBox [ 0 0 100 100 ]
/XStep 100
/YStep 100
/Resources 16 0 R
/Matrix [ 0.4 0.0 0.0 0.4 0.0 0.0 ]
/Length 183
>>

it would be great if prawn would support this. think of (monochrome) bar charts with patterns etc.

@artbot1
Copy link
Author

artbot1 commented Apr 1, 2021

here some (very) quick an dirty sample code for this (just to see what's possible):

require 'prawn.rb'

module Prawn
  module Graphics
    module Patterns
      def create_art_pattern
        patterns = page.resources[:Pattern] ||= {}

        art_pat= ref!(
          #Type: '/Pattern',
          PatternType: 1,
          PaintType: 1,
          TilingType: 2,
          BBox: [ 0,0,20,20 ],
          XStep: 20,
          YStep: 20,
          Matrix: [ 1.0,0.0,0.0,1.0,0.0,0.0 ]
        )

        stream= PDF::Core::Stream.new
        stream << "5.0 5.0 10.0 10.0 re"
        stream << "\nf"

        art_pat.stream = stream

        registry_key= "dummytest123"
        patterns["SP#{registry_key}"] = art_pat

        type= :fill

        operator =
          case type
          when :fill
            'scn'
          when :stroke
            'SCN'
          else
            raise ArgumentError, "unknown type '#{type}'"
          end

        set_color_space type, :Pattern
        renderer.add_content "/SP#{registry_key} #{operator}"
      end

    end
  end
end

pdf= Prawn::Document.new(page_layout: :portrait, page_size: 'A4', margin: 0.0) do
  #stroke_axis(:step_length => 1.cm.to_i) #only shows up with margin > 0
  pdf= self
  #fill_gradient [50, 300], [150, 200], 'ff0000', '0000ff'

  create_art_pattern
  fill_rectangle [50, 300], 100, 100

  #fill_rectangle [155, 300], 100, 100


  render_file('/tmp/gradient.pdf')
end

@gettalong
Copy link
Member

Thanks - this sounds useful to have in Prawn but will probably be need to be implemented by the community. So pull requests for this feature are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants