Class: PdfCanvas

Inherits:
Object
  • Object
show all
Defined in:
lib/majorsilence_pdf/pdf_native.rb

Instance Method Summary collapse

Constructor Details

#initialize(fns, handle) ⇒ PdfCanvas

Returns a new instance of PdfCanvas.



231
232
233
234
# File 'lib/majorsilence_pdf/pdf_native.rb', line 231

def initialize(fns, handle)
  @fns    = fns
  @handle = handle
end

Instance Method Details

Add a clickable hyperlink over the given rectangle.



291
292
293
# File 'lib/majorsilence_pdf/pdf_native.rb', line 291

def add_link(x, y, width, height, uri)
  check(@fns[:pdf_canvas_add_link].call(@handle, x.to_f, y.to_f, width.to_f, height.to_f, c_str(uri)), 'pdf_canvas_add_link')
end

#closeObject



295
296
297
298
299
300
# File 'lib/majorsilence_pdf/pdf_native.rb', line 295

def close
  return unless @handle

  @fns[:pdf_canvas_close].call(@handle)
  @handle = nil
end

#draw_ellipse(x, y, width, height, fill_rgb: nil, stroke_rgb: nil, stroke_width: 1.0) ⇒ Object

Draw an ellipse bounded by the given rectangle.



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/majorsilence_pdf/pdf_native.rb', line 272

def draw_ellipse(x, y, width, height, fill_rgb: nil, stroke_rgb: nil, stroke_width: 1.0)
  fr, fg, fb = fill_rgb   || [0, 0, 0]
  sr, sg, sb = stroke_rgb || [0, 0, 0]
  check(
    @fns[:pdf_canvas_draw_ellipse].call(
      @handle, x.to_f, y.to_f, width.to_f, height.to_f,
      fr, fg, fb, fill_rgb ? 1 : 0,
      sr, sg, sb, stroke_width.to_f, stroke_rgb ? 1 : 0
    ),
    'pdf_canvas_draw_ellipse'
  )
end

#draw_line(x1, y1, x2, y2, r: 0, g: 0, b: 0, width: 1.0) ⇒ Object

Draw a line from (x1,y1) to (x2,y2). r, g, b are 0-255.



253
254
255
# File 'lib/majorsilence_pdf/pdf_native.rb', line 253

def draw_line(x1, y1, x2, y2, r: 0, g: 0, b: 0, width: 1.0)
  check(@fns[:pdf_canvas_draw_line].call(@handle, x1.to_f, y1.to_f, x2.to_f, y2.to_f, r, g, b, width.to_f), 'pdf_canvas_draw_line')
end

#draw_rect(x, y, width, height, fill_rgb: nil, stroke_rgb: nil, stroke_width: 1.0) ⇒ Object

Draw a rectangle. fill_rgb and stroke_rgb are [r, g, b] arrays or nil.



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/majorsilence_pdf/pdf_native.rb', line 258

def draw_rect(x, y, width, height, fill_rgb: nil, stroke_rgb: nil, stroke_width: 1.0)
  fr, fg, fb = fill_rgb   || [0, 0, 0]
  sr, sg, sb = stroke_rgb || [0, 0, 0]
  check(
    @fns[:pdf_canvas_draw_rect].call(
      @handle, x.to_f, y.to_f, width.to_f, height.to_f,
      fr, fg, fb, fill_rgb ? 1 : 0,
      sr, sg, sb, stroke_width.to_f, stroke_rgb ? 1 : 0
    ),
    'pdf_canvas_draw_rect'
  )
end

#draw_table(table_handle, x, y) ⇒ Object

Draw a PdfTable at (x, y). Pass the table's handle via table.handle.



286
287
288
# File 'lib/majorsilence_pdf/pdf_native.rb', line 286

def draw_table(table_handle, x, y)
  check(@fns[:pdf_canvas_draw_table].call(@handle, table_handle, x.to_f, y.to_f), 'pdf_canvas_draw_table')
end

#draw_text(text, x, y, style_handle = nil) ⇒ Object

Draw text with its baseline at (x, y). style_handle is the raw handle from PdfStyle#handle, or nil for default.



238
239
240
241
# File 'lib/majorsilence_pdf/pdf_native.rb', line 238

def draw_text(text, x, y, style_handle = nil)
  style_ptr = style_handle || Fiddle::NULL
  check(@fns[:pdf_canvas_draw_text].call(@handle, c_str(text), x.to_f, y.to_f, style_ptr), 'pdf_canvas_draw_text')
end

#draw_textbox(text, x, y, width, height, style_handle = nil, line_spacing = 0.0) ⇒ Object

Draw word-wrapped text in a box. Returns overflow char offset.



244
245
246
247
248
249
250
# File 'lib/majorsilence_pdf/pdf_native.rb', line 244

def draw_textbox(text, x, y, width, height, style_handle = nil, line_spacing = 0.0)
  style_ptr = style_handle || Fiddle::NULL
  ret = @fns[:pdf_canvas_draw_textbox].call(@handle, c_str(text), x.to_f, y.to_f, width.to_f, height.to_f, style_ptr, line_spacing.to_f)
  raise "pdf_canvas_draw_textbox failed: #{last_error}" if ret < 0

  ret
end

#handleObject

Expose the raw handle so PdfDocument#add_page callers can pass it to draw_table.



303
# File 'lib/majorsilence_pdf/pdf_native.rb', line 303

def handle = @handle