Class: JotPDF::Document::TextContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PageContext

#color, #dsl, #fill, #image, #path, #rect, #stroke, #stroke_color, #stroke_width, #text

Constructor Details

#initialize(ctx, font_manager:, x: 0.0, y: 0.0, font: nil, size: nil, line_height: nil) ⇒ TextContext

Returns a new instance of TextContext.



194
195
196
197
198
199
200
201
# File 'lib/jot_pdf/document.rb', line 194

def initialize(ctx, font_manager:, x: 0.0, y: 0.0, font: nil, size: nil, line_height: nil)
  super(ctx, font_manager:)
  @base_x = 0.0
  @base_y = 0.0
  move(x:, y:)
  font(font, **{ size: }.compact)
  @line_height = line_height
end

Instance Attribute Details

#base_xObject (readonly)

Returns the value of attribute base_x.



192
193
194
# File 'lib/jot_pdf/document.rb', line 192

def base_x
  @base_x
end

#base_yObject (readonly)

Returns the value of attribute base_y.



192
193
194
# File 'lib/jot_pdf/document.rb', line 192

def base_y
  @base_y
end

Instance Method Details

#font(spec, size: nil) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/jot_pdf/document.rb', line 203

def font(spec, size: nil)
  @font = @font_manager.require(spec)
  size ||= @size
  @size ||= size
  @ctx.dsl do
    op("Tf") { name @font.name; num(size || 15) }
  end
end

#linebreak(factor: 1.0) ⇒ Object



220
221
222
223
# File 'lib/jot_pdf/document.rb', line 220

def linebreak(factor: 1.0)
  # @type var factor: ::Numeric
  move x: 0.0, y: -line_height * factor
end

#move(x:, y:) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/jot_pdf/document.rb', line 212

def move(x:, y:)
  @base_x += x
  @base_y += y
  @ctx.dsl do
    op("Td") { num x; num y }
  end
end

#show(text) ⇒ Object



225
226
227
228
229
230
231
232
233
# File 'lib/jot_pdf/document.rb', line 225

def show(text)
  @ctx.dsl do
    text.each_line(chomp: true).with_index do |line, idx|
      # @type self: JotPDF::Core::ContentStreamWriteContext & TextContext
      move x: 0.0, y: -line_height unless idx.zero?
      op("Tj") { hexstr @font.use(line) }
    end
  end
end