Class: Graphomaton::Exporters::Svg

Inherits:
Object
  • Object
show all
Includes:
Graphomaton::ExporterIntrospection
Defined in:
lib/graphomaton/exporters/svg.rb

Defined Under Namespace

Classes: SpatialIndex

Constant Summary collapse

DEFAULT_STATE_RADIUS =
40
DEFAULT_AUTO_STATE_RADIUS =
false
DEFAULT_MIN_STATE_RADIUS =
24
DEFAULT_MAX_STATE_RADIUS =
72
DEFAULT_STATE_SHAPE =
:circle
DEFAULT_STATE_STROKE_WIDTH =
2
DEFAULT_TRANSITION_STROKE_WIDTH =
1.5
DEFAULT_THEME =
:light
DEFAULT_LAYOUT =
:linear
DEFAULT_DIRECTION =
:lr
DEFAULT_MERGE_PARALLEL_TRANSITIONS =
true
DEFAULT_WRAP =
false
DEFAULT_SORT_LABELS =
false
DEFAULT_ROTATE_LABELS =
false
DEFAULT_LABEL_TOOLTIPS =
false
DEFAULT_HTML_TOOLTIPS =
false
DEFAULT_LABEL_BACKGROUND =
true
DEFAULT_LABEL_BORDER =
false
DEFAULT_LABEL_PADDING =
10
DEFAULT_LABEL_RADIUS =
3
DEFAULT_FONT_FAMILY =
'Arial, sans-serif'
DEFAULT_STATE_FONT_WEIGHT =
nil
DEFAULT_TRANSITION_FONT_WEIGHT =
nil
DEFAULT_HIGHLIGHT_UNREACHABLE =
false
DEFAULT_HIGHLIGHT_DEAD_STATES =
false
DEFAULT_HIGHLIGHT_INITIAL_STATE =
false
DEFAULT_HIGHLIGHT_FINAL_STATES =
false
DEFAULT_HIGHLIGHT_TRANSITIONS =
[].freeze
DEFAULT_UNREACHABLE_ZONE =
:none
DEFAULT_XML_DECLARATION =
false
DEFAULT_CSS_VARIABLES =
false
DEFAULT_EMBED_STYLES =
true
DEFAULT_PRETTY =
false
DEFAULT_MINIFY =
false
DEFAULT_STATE_EFFECT =
:none
DEFAULT_LOOP_POSITION =
:auto
DEFAULT_EDGE_STYLE =
:auto
DEFAULT_SHOW_FINAL_ARROWS =
false
DEFAULT_PADDING =
80
DEFAULT_NODE_SPACING =
120
DEFAULT_RANK_SPACING =
120
DEFAULT_AUTO_DENSITY_SPACING =
false
DEFAULT_FORCE_ITERATIONS =
120
DEFAULT_ARROW_SIZE =
10
DEFAULT_ARROW_SHAPE =
:triangle
DEFAULT_INITIAL_ARROW_LENGTH =
30
DEFAULT_INITIAL_ARROW_LABEL =
'start'
DEFAULT_FINAL_ARROW_LENGTH =
32
DEFAULT_FINAL_ARROW_LABEL =
'final'
DEFAULT_INITIAL_POSITION =
:auto
DEFAULT_FINAL_POSITION =
:auto
DEFAULT_AUTO_SIZE =
false
DEFAULT_MAX_LABEL_WIDTH =
120
DEFAULT_STATE_WRAP =
false
DEFAULT_MAX_STATE_LABEL_WIDTH =
120
DEFAULT_SCC_GROUPS =
false
DEFAULT_FOLD_GROUPS =
false
LAYOUT_OPTIONS =
%i[linear circle grid layered bfs force graphviz dot manual].freeze
DIRECTION_OPTIONS =
%i[lr tb rl bt].freeze
LOOP_POSITION_OPTIONS =
%i[auto top right bottom left].freeze
EDGE_STYLE_OPTIONS =
%i[auto straight curved orthogonal spline].freeze
UNREACHABLE_ZONE_OPTIONS =
%i[none right bottom left top].freeze
STATE_SHAPE_OPTIONS =
%i[circle ellipse rounded_rect diamond bar].freeze
STATE_EFFECT_OPTIONS =
%i[none shadow glow pulse].freeze
ARROW_SHAPE_OPTIONS =
%i[triangle vee stealth].freeze
TRANSITION_LINE_STYLE_OPTIONS =
%i[solid dashed dotted].freeze
SAFE_STYLE_PROPERTIES =
%w[
  color fill fill-opacity font-size font-style font-weight opacity stroke stroke-dasharray
  stroke-linecap stroke-linejoin stroke-opacity stroke-width
].freeze
TEXT_UNIT_WIDTH =
14.0
COMBINING_MARK_RANGES =
[
  0x0300..0x036F,
  0x1AB0..0x1AFF,
  0x1DC0..0x1DFF,
  0x20D0..0x20FF,
  0xFE20..0xFE2F
].freeze
EAST_ASIAN_WIDE_RANGES =
[
  0x1100..0x115F,
  0x2E80..0xA4CF,
  0xAC00..0xD7A3,
  0xF900..0xFAFF,
  0xFE10..0xFE6F,
  0xFF01..0xFF60,
  0xFFE0..0xFFE6,
  0x1F300..0x1FAFF
].freeze
THEMES =
{
  light: {
    background: nil,
    state_fill: 'white',
    stroke: '#333',
    state_text: '#333',
    transition_label: '#666',
    label_background: 'white',
    label_opacity: '0.9',
    initial_fill: '#dbeafe',
    final_fill: '#dcfce7',
    highlight_stroke: '#ef4444',
    inactive_opacity: '0.25'
  },
  dark: {
    background: '#111827',
    state_fill: '#1f2937',
    stroke: '#e5e7eb',
    state_text: '#f9fafb',
    transition_label: '#d1d5db',
    label_background: '#111827',
    label_opacity: '0.95',
    initial_fill: '#1e3a8a',
    final_fill: '#14532d',
    highlight_stroke: '#f87171',
    inactive_opacity: '0.35'
  },
  forest: {
    background: '#f0fdf4',
    state_fill: '#ecfdf5',
    stroke: '#166534',
    state_text: '#14532d',
    transition_label: '#15803d',
    label_background: '#f0fdf4',
    label_opacity: '0.95'
  },
  ocean: {
    background: '#eff6ff',
    state_fill: '#f8fafc',
    stroke: '#0369a1',
    state_text: '#0c4a6e',
    transition_label: '#0284c7',
    label_background: '#eff6ff',
    label_opacity: '0.95'
  },
  high_contrast: {
    background: '#000000',
    state_fill: '#ffffff',
    stroke: '#ffffff',
    state_text: '#000000',
    transition_label: '#ffff00',
    label_background: '#000000',
    label_opacity: '0.95'
  },
  color_blind: {
    background: '#f7f7f7',
    state_fill: '#ffffff',
    stroke: '#0072b2',
    state_text: '#000000',
    transition_label: '#d55e00',
    label_background: '#f7f7f7',
    label_opacity: '0.95'
  },
  print: {
    background: '#ffffff',
    state_fill: '#ffffff',
    stroke: '#000000',
    state_text: '#000000',
    transition_label: '#000000',
    label_background: '#ffffff',
    label_opacity: '1'
  },
  minimal: {
    background: nil,
    state_fill: '#ffffff',
    stroke: '#111827',
    state_text: '#111827',
    transition_label: '#374151',
    label_background: '#ffffff',
    label_opacity: '0.85'
  },
  academic: {
    background: '#ffffff',
    state_fill: '#f8fafc',
    stroke: '#1e3a8a',
    state_text: '#111827',
    transition_label: '#1e40af',
    label_background: '#ffffff',
    label_opacity: '0.95'
  },
  presentation: {
    background: '#0f172a',
    state_fill: '#f8fafc',
    stroke: '#38bdf8',
    state_text: '#0f172a',
    transition_label: '#facc15',
    label_background: '#0f172a',
    label_opacity: '0.9'
  }
}.transform_values do |theme|
  theme.transform_values(&:freeze).freeze
end.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Graphomaton::ExporterIntrospection

#capabilities, #losses_for

Constructor Details

#initialize(automaton) ⇒ Svg

Returns a new instance of Svg.



255
256
257
258
259
# File 'lib/graphomaton/exporters/svg.rb', line 255

def initialize(automaton)
  @automaton = automaton
  @state_radius = DEFAULT_STATE_RADIUS
  @label_padding = DEFAULT_LABEL_PADDING
end

Instance Attribute Details

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



261
262
263
# File 'lib/graphomaton/exporters/svg.rb', line 261

def diagnostics
  @diagnostics
end

Instance Method Details

#export(width = 800, height = 600, theme: DEFAULT_THEME, layout: DEFAULT_LAYOUT, direction: DEFAULT_DIRECTION, responsive: false, state_radius: DEFAULT_STATE_RADIUS, auto_state_radius: DEFAULT_AUTO_STATE_RADIUS, min_state_radius: DEFAULT_MIN_STATE_RADIUS, max_state_radius: DEFAULT_MAX_STATE_RADIUS, state_shape: DEFAULT_STATE_SHAPE, state_stroke_width: DEFAULT_STATE_STROKE_WIDTH, transition_stroke_width: DEFAULT_TRANSITION_STROKE_WIDTH, wrap: DEFAULT_WRAP, max_transition_label_width: DEFAULT_MAX_LABEL_WIDTH, state_wrap: DEFAULT_STATE_WRAP, max_state_label_width: DEFAULT_MAX_STATE_LABEL_WIDTH, sort_labels: DEFAULT_SORT_LABELS, label_tooltips: DEFAULT_LABEL_TOOLTIPS, html_tooltips: DEFAULT_HTML_TOOLTIPS, font_family: DEFAULT_FONT_FAMILY, state_font_weight: DEFAULT_STATE_FONT_WEIGHT, transition_font_weight: DEFAULT_TRANSITION_FONT_WEIGHT, padding: DEFAULT_PADDING, node_spacing: DEFAULT_NODE_SPACING, rank_spacing: DEFAULT_RANK_SPACING, force_iterations: DEFAULT_FORCE_ITERATIONS, layout_seed: nil, auto_size: DEFAULT_AUTO_SIZE, graphviz_command: Graphomaton::DEFAULT_GRAPHVIZ_COMMAND, auto_density_spacing: DEFAULT_AUTO_DENSITY_SPACING, arrow_size: DEFAULT_ARROW_SIZE, arrow_shape: DEFAULT_ARROW_SHAPE, initial_arrow_length: DEFAULT_INITIAL_ARROW_LENGTH, initial_arrow_label: DEFAULT_INITIAL_ARROW_LABEL, final_arrow_length: DEFAULT_FINAL_ARROW_LENGTH, final_arrow_label: DEFAULT_FINAL_ARROW_LABEL, initial_position: DEFAULT_INITIAL_POSITION, final_position: DEFAULT_FINAL_POSITION, merge_parallel_transitions: DEFAULT_MERGE_PARALLEL_TRANSITIONS, label_background: DEFAULT_LABEL_BACKGROUND, label_border: DEFAULT_LABEL_BORDER, label_padding: DEFAULT_LABEL_PADDING, label_radius: DEFAULT_LABEL_RADIUS, rotate_labels: DEFAULT_ROTATE_LABELS, highlight_unreachable: DEFAULT_HIGHLIGHT_UNREACHABLE, highlight_dead_states: DEFAULT_HIGHLIGHT_DEAD_STATES, highlight_initial_state: DEFAULT_HIGHLIGHT_INITIAL_STATE, highlight_final_states: DEFAULT_HIGHLIGHT_FINAL_STATES, highlight_transitions: DEFAULT_HIGHLIGHT_TRANSITIONS, unreachable_zone: DEFAULT_UNREACHABLE_ZONE, xml_declaration: DEFAULT_XML_DECLARATION, css_variables: DEFAULT_CSS_VARIABLES, embed_styles: DEFAULT_EMBED_STYLES, pretty: DEFAULT_PRETTY, minify: DEFAULT_MINIFY, state_effect: DEFAULT_STATE_EFFECT, loop_position: DEFAULT_LOOP_POSITION, edge_style: DEFAULT_EDGE_STYLE, show_final_arrows: DEFAULT_SHOW_FINAL_ARROWS, scc_groups: DEFAULT_SCC_GROUPS, fold_groups: DEFAULT_FOLD_GROUPS, preserve_manual_positions: Graphomaton::DEFAULT_PRESERVE_MANUAL_POSITIONS, fit: Graphomaton::DEFAULT_FIT, title: nil, description: nil, svg_id: nil) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/graphomaton/exporters/svg.rb', line 263

def export(width = 800, height = 600, theme: DEFAULT_THEME, layout: DEFAULT_LAYOUT, direction: DEFAULT_DIRECTION, responsive: false,
           state_radius: DEFAULT_STATE_RADIUS, auto_state_radius: DEFAULT_AUTO_STATE_RADIUS,
           min_state_radius: DEFAULT_MIN_STATE_RADIUS, max_state_radius: DEFAULT_MAX_STATE_RADIUS,
           state_shape: DEFAULT_STATE_SHAPE,
           state_stroke_width: DEFAULT_STATE_STROKE_WIDTH,
           transition_stroke_width: DEFAULT_TRANSITION_STROKE_WIDTH,
           wrap: DEFAULT_WRAP, max_transition_label_width: DEFAULT_MAX_LABEL_WIDTH,
           state_wrap: DEFAULT_STATE_WRAP, max_state_label_width: DEFAULT_MAX_STATE_LABEL_WIDTH,
           sort_labels: DEFAULT_SORT_LABELS,
           label_tooltips: DEFAULT_LABEL_TOOLTIPS,
           html_tooltips: DEFAULT_HTML_TOOLTIPS,
           font_family: DEFAULT_FONT_FAMILY,
           state_font_weight: DEFAULT_STATE_FONT_WEIGHT,
           transition_font_weight: DEFAULT_TRANSITION_FONT_WEIGHT,
           padding: DEFAULT_PADDING, node_spacing: DEFAULT_NODE_SPACING, rank_spacing: DEFAULT_RANK_SPACING,
           force_iterations: DEFAULT_FORCE_ITERATIONS, layout_seed: nil, auto_size: DEFAULT_AUTO_SIZE,
           graphviz_command: Graphomaton::DEFAULT_GRAPHVIZ_COMMAND,
           auto_density_spacing: DEFAULT_AUTO_DENSITY_SPACING,
           arrow_size: DEFAULT_ARROW_SIZE,
           arrow_shape: DEFAULT_ARROW_SHAPE,
           initial_arrow_length: DEFAULT_INITIAL_ARROW_LENGTH,
           initial_arrow_label: DEFAULT_INITIAL_ARROW_LABEL,
           final_arrow_length: DEFAULT_FINAL_ARROW_LENGTH,
           final_arrow_label: DEFAULT_FINAL_ARROW_LABEL,
           initial_position: DEFAULT_INITIAL_POSITION, final_position: DEFAULT_FINAL_POSITION,
           merge_parallel_transitions: DEFAULT_MERGE_PARALLEL_TRANSITIONS,
           label_background: DEFAULT_LABEL_BACKGROUND,
           label_border: DEFAULT_LABEL_BORDER,
           label_padding: DEFAULT_LABEL_PADDING,
           label_radius: DEFAULT_LABEL_RADIUS,
           rotate_labels: DEFAULT_ROTATE_LABELS,
           highlight_unreachable: DEFAULT_HIGHLIGHT_UNREACHABLE,
           highlight_dead_states: DEFAULT_HIGHLIGHT_DEAD_STATES,
           highlight_initial_state: DEFAULT_HIGHLIGHT_INITIAL_STATE,
           highlight_final_states: DEFAULT_HIGHLIGHT_FINAL_STATES,
           highlight_transitions: DEFAULT_HIGHLIGHT_TRANSITIONS,
           unreachable_zone: DEFAULT_UNREACHABLE_ZONE,
           xml_declaration: DEFAULT_XML_DECLARATION,
           css_variables: DEFAULT_CSS_VARIABLES,
           embed_styles: DEFAULT_EMBED_STYLES,
           pretty: DEFAULT_PRETTY,
           minify: DEFAULT_MINIFY,
           state_effect: DEFAULT_STATE_EFFECT,
           loop_position: DEFAULT_LOOP_POSITION,
           edge_style: DEFAULT_EDGE_STYLE,
           show_final_arrows: DEFAULT_SHOW_FINAL_ARROWS,
           scc_groups: DEFAULT_SCC_GROUPS,
           fold_groups: DEFAULT_FOLD_GROUPS,
           preserve_manual_positions: Graphomaton::DEFAULT_PRESERVE_MANUAL_POSITIONS,
           fit: Graphomaton::DEFAULT_FIT,
           title: nil, description: nil, svg_id: nil)
  source_automaton = @automaton
  @diagnostics = []
  @state_radius = resolve_state_radius(state_radius, auto_state_radius, min_state_radius, max_state_radius)
  @state_shape = resolve_state_shape(state_shape)
  @state_stroke_width = finite_number!(state_stroke_width, 'state_stroke_width', positive: true)
  @transition_stroke_width = finite_number!(transition_stroke_width, 'transition_stroke_width', positive: true)
  @arrow_size = finite_number!(arrow_size, 'arrow_size', positive: true)
  @arrow_shape = resolve_arrow_shape(arrow_shape)
  @initial_arrow_length = finite_number!(initial_arrow_length, 'initial_arrow_length', positive: true)
  @initial_arrow_label = validated_output_text(initial_arrow_label, 'initial_arrow_label')
  @final_arrow_length = finite_number!(final_arrow_length, 'final_arrow_length', positive: true)
  @final_arrow_label = validated_output_text(final_arrow_label, 'final_arrow_label')
  @auto_dark_theme = false
  @theme = resolve_theme(theme)
  @layout = resolve_layout(layout)
  @direction = resolve_direction(direction)
  @loop_position = resolve_loop_position(loop_position)
  @edge_style = resolve_edge_style(edge_style)
  @state_effect = resolve_state_effect(state_effect)
  @show_final_arrows = show_final_arrows
  @merge_parallel_transitions = merge_parallel_transitions
  @label_background = label_background
  @label_border = label_border
  @label_padding = finite_number!(label_padding, 'label_padding', nonnegative: true)
  @label_radius = finite_number!(label_radius, 'label_radius', nonnegative: true)
  @rotate_labels = rotate_labels
  @highlight_unreachable = highlight_unreachable
  @highlight_dead_states = highlight_dead_states
  @highlight_initial_state = highlight_initial_state
  @highlight_final_states = highlight_final_states
  @highlight_transitions = Array(highlight_transitions)
  @unreachable_zone = resolve_unreachable_zone(unreachable_zone)
  @css_variables = css_variables || @auto_dark_theme
  @unreachable_states = (@highlight_unreachable || @unreachable_zone != :none) ? @automaton.unreachable_states : []
  @dead_states = @highlight_dead_states ? @automaton.dead_states : []
  @trap_states = @highlight_dead_states ? @automaton.trap_states : []
  @padding = finite_number!(padding, 'padding', nonnegative: true)
  @node_spacing, @rank_spacing = density_adjusted_spacings(node_spacing, rank_spacing, auto_density_spacing)
  unless force_iterations.is_a?(Integer) && force_iterations >= 0
    raise ArgumentError, 'force_iterations must be a non-negative Integer'
  end
  @force_iterations = force_iterations
  @layout_seed = layout_seed
  @wrap_labels = wrap
  @state_wrap = state_wrap
  @max_state_label_width = finite_number!(max_state_label_width, 'max_state_label_width', nonnegative: true)
  @scc_groups = scc_groups
  @max_transition_label_width = finite_number!(max_transition_label_width, 'max_transition_label_width', nonnegative: true)
  @sort_labels = sort_labels
  @label_tooltips = label_tooltips
  @html_tooltips = html_tooltips
  @font_family = safe_css_value(font_family, context: 'font_family')
  @state_font_weight = safe_css_value(state_font_weight, context: 'state_font_weight', allow_nil: true)
  @transition_font_weight = safe_css_value(transition_font_weight, context: 'transition_font_weight', allow_nil: true)
  @automaton = folded_automaton(@automaton) if fold_groups
  layout_padding = automatic_layout_padding
  @positions = @automaton.layout_positions(
    width,
    height,
    layout: @layout,
    direction: @direction,
    state_radius: @state_radius,
    padding: layout_padding,
    node_spacing: @node_spacing,
    rank_spacing: @rank_spacing,
    force_iterations: @force_iterations,
    layout_seed: @layout_seed,
    graphviz_command: graphviz_command,
    initial_position: initial_position,
    final_position: final_position,
    preserve_manual_positions: preserve_manual_positions,
    fit: fit
  )
  @positions = apply_unreachable_zone(@positions, width, height)
  if auto_size
    width, height = auto_size_canvas(width, height)
  end
  @canvas_width = width.to_f
  @canvas_height = height.to_f
  @label_boxes = state_collision_boxes + group_label_collision_boxes
  cell_size = [@state_radius * 2, 64].max
  @label_spatial_index = SpatialIndex.new(cell_size: cell_size)
  @label_boxes.each { |box| @label_spatial_index.insert(box) }
  @state_spatial_index = SpatialIndex.new(cell_size: cell_size)
  @positions.each_value do |state|
    next if state[:x].nil? || state[:y].nil?

    bounds = {
      x: state[:x] - @state_radius - 10.0,
      y: state[:y] - @state_radius - 10.0,
      width: (@state_radius + 10.0) * 2,
      height: (@state_radius + 10.0) * 2
    }
    @state_spatial_index.insert(bounds, state)
  end
  @title_text = validated_output_text(title, 'SVG title')
  @description_text = validated_output_text(description, 'SVG description')
  @svg_id = svg_id ? svg_id_component(svg_id) : default_svg_id(width, height)
  @arrowhead_id = "#{@svg_id}-arrowhead"
  @element_id_counts = Hash.new(0)

  doc = REXML::Document.new
  svg = doc.add_element('svg', svg_root_attributes(width, height, responsive: responsive))

  add_defs(svg)
  add_style(svg) if embed_styles
  (svg)
  (svg)
  add_background(svg, width, height)
  transition_group = svg.add_element('g', { 'class' => 'transitions' })
  state_group = svg.add_element('g', { 'class' => 'states' })
  add_transitions(transition_group)
  add_initial_arrow(transition_group) if @automaton.initial_state
  add_final_arrows(transition_group) if @show_final_arrows
  add_state_groups(state_group)
  add_states(state_group)
  apply_auto_content_bounds(svg, responsive: responsive) if auto_size

  svg_output = serialize_document(doc, pretty: pretty, minify: minify)
  return svg_output unless xml_declaration

  %(<?xml version="1.0" encoding="UTF-8"?>\n#{svg_output})
ensure
  @automaton = source_automaton if source_automaton
end

#export_result(*arguments, **options) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/graphomaton/exporters/svg.rb', line 440

def export_result(*arguments, **options)
  output = export(*arguments, **options)
  render_diagnostics = (@diagnostics || []).dup
  render_diagnostics.concat(
    @automaton.layout_diagnostics_for(@positions, @canvas_width, @canvas_height, @state_radius)
  )
  RenderResult.new(
    output: output.dup.freeze,
    diagnostics: render_diagnostics.freeze,
    bounds: { width: @canvas_width, height: @canvas_height }.freeze,
    layout: @positions.transform_values { |position| position.dup.freeze }.freeze
  )
end