Class: AsciiChem::Formatter::StructuralSvg

Inherits:
Base
  • Object
show all
Defined in:
lib/asciichem/formatter/structural_svg.rb

Overview

Renders a Model tree as a 2D structural SVG diagram using elkrb-computed atom positions. Bonds render as parallel lines (single/double/triple/quadruple), wedges, hashes, or arrows depending on kind.

Falls back to the linear Svg formatter for inputs that don't contain a molecule with bonds (formulae, reactions, etc.).

Constant Summary collapse

ATOM_RADIUS =
14
BOND_SPACING =
4
WEDGE_WIDTH =
6
HASH_DASH_COUNT =
5
ATOM_COLORS =
{
  'C' => '#1a1a1a',
  'H' => '#555',
  'O' => '#b91c1c',
  'N' => '#1e40af',
  'S' => '#a16207',
  'Cl' => '#16a34a',
  'F' => '#15803d',
  'Br' => '#7c2d12',
  'I' => '#6b21a8',
  'P' => '#a16207'
}.freeze
DEFAULT_ATOM_COLOR =
'#0c4a3e'

Instance Method Summary collapse

Methods inherited from Base

#render

Instance Method Details

#visit_formula(formula) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/asciichem/formatter/structural_svg.rb', line 33

def visit_formula(formula)
  molecule = formula.nodes.find { |n| n.is_a?(AsciiChem::Model::Molecule) }
  return visit_formula_linear(formula) unless molecule

  has_bonds = molecule.nodes.any?(AsciiChem::Model::Bond)
  return visit_formula_linear(formula) unless has_bonds

  result = AsciiChem::Layout.layout(molecule)
  return visit_formula_linear(formula) if result.empty?

  render_svg(result)
end