Class: Ea::Svg::EaEmitter::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/ea/svg/ea_emitter/document.rb

Overview

Top-level orchestrator: emits a complete standalone SVG document matching EA's encoding. Bound computation and layer sequencing delegated to BoundsCalculator and LayerSequencer respectively (SRP).

Constant Summary collapse

BUILD_ID =
"1624"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diagram, model_index:, frame: true, document: nil, **_options) ⇒ Document

Returns a new instance of Document.



15
16
17
18
19
20
# File 'lib/ea/svg/ea_emitter/document.rb', line 15

def initialize(diagram, model_index:, frame: true, document: nil, **_options)
  @diagram = diagram
  @model_index = model_index
  @frame = frame
  @document = document
end

Instance Attribute Details

#diagramObject (readonly)

Returns the value of attribute diagram.



13
14
15
# File 'lib/ea/svg/ea_emitter/document.rb', line 13

def diagram
  @diagram
end

#documentObject (readonly)

Returns the value of attribute document.



13
14
15
# File 'lib/ea/svg/ea_emitter/document.rb', line 13

def document
  @document
end

#frameObject (readonly)

Returns the value of attribute frame.



13
14
15
# File 'lib/ea/svg/ea_emitter/document.rb', line 13

def frame
  @frame
end

#model_indexObject (readonly)

Returns the value of attribute model_index.



13
14
15
# File 'lib/ea/svg/ea_emitter/document.rb', line 13

def model_index
  @model_index
end

Instance Method Details

#emit_imagesObject

Walks t_image rows (when the document carries a Database with an :images collection) and invokes EmfRenderer on each. Emits an <image> element per successfully converted SVG, or returns nil when no images or all conversions fail.

The emfsvg gem currently can't parse EA's specific EMF variant (see TODO.diagrams/87 + TODO.complete/56). Until upstream emfsvg supports it, this method returns nil gracefully. The wiring is correct infrastructure for when emfsvg catches up.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ea/svg/ea_emitter/document.rb', line 45

def emit_images
  return nil unless document
  return nil unless document.is_a?(Ea::Qea::Database)

  images = document.collections[:images] || []
  return nil if images.empty?

  fragments = images.filter_map { |image| render_image(image) }
  return nil if fragments.empty?

  %(<g id="images">\n#{fragments.join("\n")}\n</g>)
end

#renderObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ea/svg/ea_emitter/document.rb', line 22

def render
  canvas = Canvas.from(diagram, model_index: model_index)
  layers = LayerSequencer.new(diagram, model_index: model_index,
                                canvas: canvas, frame: frame,
                                document: document)
                          .layers
                          .reject { |s| s.nil? || s.empty? }
  image_layer = emit_images
  layers << image_layer if image_layer

  %(<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\n\n<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="#{canvas.width_cm}" height="#{canvas.height_cm}" viewBox="#{canvas.view_box}">\n<title></title>\n<desc>Created with Enterprise Architect (Build: #{BUILD_ID}) 2</desc>\n#{layers.join("\n")}\n</svg>)
end