Class: Clef::Renderer::SvgRenderer

Inherits:
Base
  • Object
show all
Includes:
NotationHelpers
Defined in:
lib/clef/renderer/svg_renderer.rb

Constant Summary collapse

WIDTH =
1024
LEFT_PADDING =
60
RIGHT_PADDING =
60
STAFF_START_X =
140
STAFF_TOP =
90

Constants included from NotationHelpers

NotationHelpers::ACCIDENTAL_FALLBACK, NotationHelpers::ACCIDENTAL_GLYPH_KEYS, NotationHelpers::FLAT_ORDER, NotationHelpers::REST_LABELS, NotationHelpers::SHARP_ORDER

Instance Attribute Summary

Attributes inherited from Base

#font_manager, #glyph_table, #style

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Clef::Renderer::Base

Instance Method Details

#draw_element(xml, element, x, baseline, clef) ⇒ Object

Parameters:

  • xml (Nokogiri::XML::Builder)
  • element (Object)
  • x (Float)
  • baseline (Float)
  • clef (Clef::Core::Clef)


68
69
70
71
72
73
74
75
76
77
# File 'lib/clef/renderer/svg_renderer.rb', line 68

def draw_element(xml, element, x, baseline, clef)
  case element
  when Clef::Core::Note then draw_note(xml, element, x, baseline, clef, accidental_state: {}, key_signature: nil)
  when Clef::Core::Rest then draw_rest(xml, element, x, baseline)
  when Clef::Core::Chord then draw_chord(xml, element, x, baseline, clef, accidental_state: {}, key_signature: nil)
  when Clef::Core::Tuplet then draw_tuplet(xml, element, x, baseline, clef, accidental_state: {}, key_signature: nil)
  when Clef::Notation::Dynamic then draw_dynamic(xml, element, x, baseline)
  when Clef::Core::Tempo then draw_tempo_change(xml, element, x, baseline)
  end
end

#draw_score(xml, score, positions: nil, layout: nil) ⇒ Object

Parameters:

  • xml (Nokogiri::XML::Builder)
  • score (Clef::Core::Score)
  • positions (Hash, nil) (defaults to: nil)
  • layout (Hash, nil) (defaults to: nil)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/clef/renderer/svg_renderer.rb', line 37

def draw_score(xml, score, positions: nil, layout: nil)
  return draw_systems(xml, score, layout) if layout&.dig(:systems)&.any?

  draw_header(xml, score)
  staff_index = 0
  score.staff_groups.each do |group|
    group_start = STAFF_TOP + (staff_index * style.staff_gap)
    group.staves.each do |staff|
      baseline = STAFF_TOP + (staff_index * style.staff_gap)
      draw_staff(xml, staff, baseline, positions: positions, layout: layout)
      staff_index += 1
    end
    draw_staff_group(xml, group, group_start, STAFF_TOP + ((staff_index - 1) * style.staff_gap)) if group.staves.length > 1
  end
end

#draw_staff_lines(xml, baseline) ⇒ Object

Parameters:

  • xml (Nokogiri::XML::Builder)
  • baseline (Float)


55
56
57
58
59
60
61
# File 'lib/clef/renderer/svg_renderer.rb', line 55

def draw_staff_lines(xml, baseline)
  5.times do |line|
    y = baseline + (line * style.staff_space)
    xml.line(x1: LEFT_PADDING, y1: y, x2: WIDTH - RIGHT_PADDING, y2: y,
      stroke: "black", "stroke-width": 1, class: "staff-line")
  end
end

#render(score, path, positions: nil, layout: nil, **_options) ⇒ Object

Parameters:

  • score (Clef::Core::Score)
  • path (String)
  • positions (Hash) (defaults to: nil)
  • _layout (Hash, nil)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clef/renderer/svg_renderer.rb', line 20

def render(score, path, positions: nil, layout: nil, **_options)
  height = svg_height(score, layout)
  document = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.svg(xmlns: "http://www.w3.org/2000/svg",
      width: WIDTH,
      height: height,
      viewBox: "0 0 #{WIDTH} #{height}") do
      draw_score(xml, score, positions: positions, layout: layout)
    end
  end
  write_output(path, document.to_xml)
end