Module: Sevgi::Sundries::Export::Renderer Private

Extended by:
Renderer
Included in:
Renderer
Defined in:
lib/sevgi/sundries/export/native.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Low-level format renderers used by #call.

Instance Method Summary collapse

Instance Method Details

#[](format) ⇒ Method?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a renderer method for a format.

Parameters:

  • format (Symbol, String, nil)

    format name

Returns:

  • (Method, nil)


244
245
246
247
248
249
250
251
# File 'lib/sevgi/sundries/export/native.rb', line 244

def [](format)
  case format&.to_sym
  when :png
    method(:png)
  when :pdf
    method(:pdf)
  end
end

#pdf(handle:, output:, tw:, th:, dpi:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Renders SVG data to a PDF surface.

Parameters:

  • handle (Rsvg::Handle)

    parsed SVG handle

  • output (String)

    output file path

  • tw (Numeric)

    target width in CSS pixels

  • th (Numeric)

    target height in CSS pixels

  • dpi (Numeric)

    CSS pixel density

Raises:

  • (Cairo::Error)

    when Cairo cannot write the PDF surface

  • (Rsvg::Error)

    when librsvg cannot render the document



262
263
264
265
266
267
268
269
270
# File 'lib/sevgi/sundries/export/native.rb', line 262

def pdf(handle:, output:, tw:, th:, dpi:, **)
  pw, ph = tw * (72.0 / dpi), th * (72.0 / dpi)
  surface = Cairo::PDFSurface.new(output, pw, ph)
  context = Cairo::Context.new(surface)
  context.scale(pw / tw, ph / th)
  handle.render_document(context, viewport(tw, th))
  context.show_page
  surface.finish
end

#png(handle:, output:, tw:, th:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Renders SVG data to a PNG image.

Parameters:

  • handle (Rsvg::Handle)

    parsed SVG handle

  • output (String)

    output file path

  • tw (Numeric)

    target width in CSS pixels

  • th (Numeric)

    target height in CSS pixels

Raises:

  • (Cairo::Error)

    when Cairo cannot write the PNG surface

  • (Rsvg::Error)

    when librsvg cannot render the document



280
281
282
283
284
285
# File 'lib/sevgi/sundries/export/native.rb', line 280

def png(handle:, output:, tw:, th:, **)
  surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, tw.round, th.round)
  context = Cairo::Context.new(surface)
  handle.render_document(context, viewport(tw, th))
  surface.write_to_png(output)
end