Class: Pdfrb::Document::GraphicsState
- Inherits:
-
Object
- Object
- Pdfrb::Document::GraphicsState
- Defined in:
- lib/pdfrb/document/graphics_state.rb
Overview
Facade for managing ExtGState (graphics state) entries on pages. Each page can reference a /ExtGState sub-dict in /Resources that holds named graphics-state parameter sets (transparency, line width, overprint, etc.).
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Instance Method Summary collapse
- #count(page) ⇒ Object
-
#each(page, &block) ⇒ Object
Enumerate ExtGState entries on a page.
-
#initialize(document) ⇒ GraphicsState
constructor
A new instance of GraphicsState.
-
#register(page, **params) ⇒ Symbol
Register an ExtGState parameter dict on a page and return the resource name (e.g., :GS1).
-
#register_line_width(page, width:, line_cap: nil, line_join: nil) ⇒ Object
Convenience: register a line-width state.
-
#register_transparency(page, opacity:, blend_mode: nil) ⇒ Object
Convenience: register a transparency state.
Constructor Details
#initialize(document) ⇒ GraphicsState
Returns a new instance of GraphicsState.
12 13 14 15 |
# File 'lib/pdfrb/document/graphics_state.rb', line 12 def initialize(document) @document = document @next_gs_number = 1 end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
10 11 12 |
# File 'lib/pdfrb/document/graphics_state.rb', line 10 def document @document end |
Instance Method Details
#count(page) ⇒ Object
65 66 67 |
# File 'lib/pdfrb/document/graphics_state.rb', line 65 def count(page) each(page).count end |
#each(page, &block) ⇒ Object
Enumerate ExtGState entries on a page.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/pdfrb/document/graphics_state.rb', line 53 def each(page, &block) return enum_for(:each, page) unless block resources = page_resources(page) return unless resources ext = resources.value[:ExtGState] return unless ext ext.each(&block) end |
#register(page, **params) ⇒ Symbol
Register an ExtGState parameter dict on a page and return the resource name (e.g., :GS1).
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pdfrb/document/graphics_state.rb', line 24 def register(page, **params) gs = document.add({ Type: :ExtGState, **params }, type: Pdfrb::Model::Type::GraphicsStateParameter) ref = Pdfrb::Model::Reference.new(gs.oid, gs.gen) resources = ensure_resources(page) ext_gstate = resources.value[:ExtGState] || {} name = gs_name ext_gstate[name] = ref resources.value[:ExtGState] = ext_gstate name end |
#register_line_width(page, width:, line_cap: nil, line_join: nil) ⇒ Object
Convenience: register a line-width state.
45 46 47 48 49 50 |
# File 'lib/pdfrb/document/graphics_state.rb', line 45 def register_line_width(page, width:, line_cap: nil, line_join: nil) params = { LW: width } params[:LC] = line_cap if line_cap params[:LJ] = line_join if line_join register(page, **params) end |
#register_transparency(page, opacity:, blend_mode: nil) ⇒ Object
Convenience: register a transparency state.
38 39 40 41 42 |
# File 'lib/pdfrb/document/graphics_state.rb', line 38 def register_transparency(page, opacity:, blend_mode: nil) params = { ca: opacity, CA: opacity } params[:BM] = blend_mode if blend_mode register(page, **params) end |