Class: JotPDF::Document::PageContext

Inherits:
Object
  • Object
show all
Defined in:
lib/jot_pdf/document.rb

Direct Known Subclasses

TextContext

Instance Method Summary collapse

Constructor Details

#initialize(ctx, font_manager:) ⇒ PageContext

Returns a new instance of PageContext.



79
80
81
82
# File 'lib/jot_pdf/document.rb', line 79

def initialize(ctx, font_manager:)
  @ctx = ctx
  @font_manager = font_manager
end

Instance Method Details

#color(color = nil, r: nil, g: nil, b: nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jot_pdf/document.rb', line 84

def color(color = nil, r: nil, g: nil, b: nil)
  # rubocop:disable Style/RedundantCondition
  r, g, b =
    if color
      colorspec(color)
    else
      colorspec(r:, g:, b:)
    end
  # rubocop:enable Style/RedundantCondition
  @ctx.dsl do
    op("rg") { num r; num g; num b }
  end
end

#dsl(&block) ⇒ Object



164
165
166
# File 'lib/jot_pdf/document.rb', line 164

def dsl(&block)
  Docile.dsl_eval(self, &block)
end

#fillObject



141
142
143
144
145
# File 'lib/jot_pdf/document.rb', line 141

def fill
  @ctx.dsl do
    op("f")
  end
end

#image(n, x:, y:, width:, height:) ⇒ Object



157
158
159
160
161
162
# File 'lib/jot_pdf/document.rb', line 157

def image(n, x:, y:, width:, height:)
  @ctx.dsl do
    op("cm") { num width; num 0; num 0; num height; num x; num y }
    op("Do") { name n }
  end
end

#path(*args) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/jot_pdf/document.rb', line 124

def path(*args)
  @ctx.dsl do
    x0, y0 = args.shift
    op("m") { num x0; num y0 }
    args.each do |x, y|
      op("l") { num x; num y }
    end
    op("h")
  end
end

#rect(x:, y:, width:, height:) ⇒ Object



118
119
120
121
122
# File 'lib/jot_pdf/document.rb', line 118

def rect(x:, y:, width:, height:)
  @ctx.dsl do
    op("re") { num x; num y; num width; num height }
  end
end

#strokeObject



135
136
137
138
139
# File 'lib/jot_pdf/document.rb', line 135

def stroke
  @ctx.dsl do
    op("s")
  end
end

#stroke_color(color = nil, r: nil, g: nil, b: nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jot_pdf/document.rb', line 98

def stroke_color(color = nil, r: nil, g: nil, b: nil)
  # rubocop:disable Style/RedundantCondition
  r, g, b =
    if color
      colorspec(color)
    else
      colorspec(r:, g:, b:)
    end
  # rubocop:enable Style/RedundantCondition
  @ctx.dsl do
    op("RG") { num r; num g; num b }
  end
end

#stroke_width(width) ⇒ Object



112
113
114
115
116
# File 'lib/jot_pdf/document.rb', line 112

def stroke_width(width)
  @ctx.dsl do
    op("w") { num width }
  end
end

#text(text = nil, **kwargs, &block) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/jot_pdf/document.rb', line 147

def text(text = nil, **kwargs, &block)
  @ctx.dsl do
    op("BT")
    tc = TextContext.new(@ctx, font_manager: @font_manager, **kwargs)
    tc.show text if text
    tc.dsl(&block) if block
    op("ET")
  end
end