Method: HexaPDF::Content::Canvas#font_size

Defined in:
lib/hexapdf/content/canvas.rb

#font_size(size = nil) ⇒ Object Also known as: font_size=

:call-seq:

canvas.font_size             => font_size
canvas.font_size(size)       => canvas

Specifies the font size.

Note that an error is raised if no font has been set before via #font (this is due to how setting font and font size works in PDF).

Returns the current font size when no argument is given, otherwise returns self. The setter version can also be called in the font_size= form.

Examples:

#>pdf
canvas.font("Helvetica", size: 10)     # Necessary only the first time
canvas.font_size(12)
canvas.font_size                       # => 12
canvas.font_size = 10

# visual example
6.step(to: 20, by: 2).each_with_index do |size, index|
  canvas.font_size(size)
  canvas.text("Text in size #{size}", at: [15, 180 - index * 20])
end

See: PDF2.0 s9.2.2, #font, #text



2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
# File 'lib/hexapdf/content/canvas.rb', line 2296

def font_size(size = nil)
  if size
    unless @font
      raise HexaPDF::Error, "A font needs to be set before the font size can be set"
    end
    invoke_font_operator(@font.pdf_object, size)
    self
  else
    graphics_state.font_size
  end
end