Class: RSyntaxTree::RSGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rsyntaxtree.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ RSGenerator

Returns a new instance of RSGenerator.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rsyntaxtree.rb', line 58

def initialize(params = {})
  new_params = {}
  fontset = {}
  params.each do |keystr, value|
    key = keystr.to_sym
    case key
    when :data
      data = value
      data = data.gsub('-AMP-', '&')
                 .gsub('-PERCENT-', "%")
                 .gsub('-PRIME-', "'")
                 .gsub('-SCOLON-', ';')
                 .gsub('-OABRACKET-', '<')
                 .gsub('-CABRACKET-', '>')
                 .gsub('¥¥', '')
                 .gsub(/(?<!\\)¥/, "\\")
      new_params[key] = data

    when :tidy
      # One layout scale from the most spacious to the most dense:
      # "symmetric" (radical symmetrization, uniform sibling slots),
      # "off" (the traditional layout), "low" (contour packing with
      # strict leaf positions), "medium" (packing that may tuck
      # branches across rows as long as no two leaves swap their
      # left-right order), "high" (free tucking; leaf order kept per
      # row only). "on"/"compact" are accepted as legacy aliases of
      # low/high, legacy tidy_nest: "on" upgrades low to high, and
      # legacy symmetrize: "on" upgrades "off" to "symmetric" (both
      # below, in BaseGraph).
      new_params[key] = case value.to_s
                        when "high", "compact"
                          "high"
                        when "medium"
                          "medium"
                        when "low", "on", "true"
                          "low"
                        when "symmetric"
                          "symmetric"
                        else
                          "off"
                        end
    when :tidy_nest
      new_params[key] = value && (value != "off" && value != "false") ? true : false
    when :symmetrize, :transparent, :polyline, :hide_default_connectors, :mirror
      new_params[key] = value && (value != "off" && value != "false") ? true : false
    when :color
      new_params[key] = case value
                        when "modern", "on", "true"
                          "modern"
                        when "traditional"
                          "traditional"
                        else
                          "off"
                        end
    when :fontsize
      new_params[key] = value.to_i
    when :linewidth
      new_params[key] = value.to_i
    when :vheight, :hspacing, :tidy_spacing
      new_params[key] = value.to_f
    when :fontstyle
      # Fonts are resolved by name through fontconfig (measurement via
      # Pango, rendering via the SVG font-family attribute), so all a
      # style needs is its family fallback chain.
      style = case value
              when "noto-sans-mono", "mono" then :mono
              when "noto-serif", "serif" then :serif
              when "cjk zenhei", "cjk" then :cjk
              else :sans
              end
      fontset[:family] = FontFamily.for_pango(style)
      fontset[:family_cjk] = FontFamily.for_pango(style, cjk_priority: style != :cjk)
      new_params[:fontstyle] = style.to_s
    else
      new_params[key] = value
    end
  end

  # Legacy alias: tidy_spacing was the tidy-only horizontal gap factor
  # before it was generalized to hspacing (all layout modes).
  if (new_params[:hspacing].nil? || new_params[:hspacing] == 1.0) &&
     new_params[:tidy_spacing] && new_params[:tidy_spacing] != 1.0
    new_params[:hspacing] = new_params[:tidy_spacing]
  end

  # defaults to the following
  @params = DEFAULT_OPTS.dup
  @params.merge! new_params
  @params[:fontsize] = @params[:fontsize] * FONT_SCALING
  # fontset is populated above only when :fontstyle is passed explicitly;
  # fall back to the merged default style otherwise
  if fontset[:family].nil?
    fontset[:family] = FontFamily.for_pango(@params[:fontstyle])
    fontset[:family_cjk] = FontFamily.for_pango(@params[:fontstyle], cjk_priority: true)
  end
  @params[:fontset] = fontset
  single_x_metrics = FontMetrics.get_metrics("X", fontset[:family], @params[:fontsize], :normal, :normal)
  @global = {}
  @global[:single_x_metrics] = single_x_metrics
  @global[:height_connector_to_text] = single_x_metrics.height / 2.0
  @global[:single_line_height] = single_x_metrics.height * 2.0
  @global[:width_half_x] = single_x_metrics.width / 2.0
  @global[:height_connector] = single_x_metrics.height * @params[:vheight]
  # Horizontal counterpart of vheight: hspacing scales every horizontal
  # gap (sibling clearance in all layout modes, tidy minimum gap,
  # margins) the way vheight scales the vertical rhythm.
  @global[:h_gap_between_nodes] = single_x_metrics.width * 0.8 * (@params[:hspacing] || 1.0).to_f
  @global[:box_vertical_margin] = single_x_metrics.height * 0.8
end

Class Method Details

.check_data(text) ⇒ Object

Raises:



168
169
170
171
172
# File 'lib/rsyntaxtree.rb', line 168

def self.check_data(text)
  raise RSTError, +"Error: input text is empty" if text.to_s == ""

  StringParser.valid?(text)
end

Instance Method Details

#draw_gifObject



232
233
234
235
236
237
238
239
240
# File 'lib/rsyntaxtree.rb', line 232

def draw_gif
  png_data = draw_png
  images = Magick::Image.from_blob(png_data)
  image = images.first
  image.format = 'GIF'
  blob = image.to_blob
  images.each(&:destroy!)
  blob
end

#draw_jpgObject



222
223
224
225
226
227
228
229
230
# File 'lib/rsyntaxtree.rb', line 222

def draw_jpg
  png_data = draw_png
  images = Magick::Image.from_blob(png_data)
  image = images.first
  image.format = 'JPEG'
  blob = image.to_blob
  images.each(&:destroy!)
  blob
end

#draw_lsifObject



242
243
244
245
246
247
# File 'lib/rsyntaxtree.rb', line 242

def draw_lsif
  sp = StringParser.new(@params[:data].gsub('&', '&amp;'), @params[:fontset], @params[:fontsize], @global)
  sp.parse
  graph = LsifGraph.new(sp.get_elementlist, @params, @global)
  graph.lsif_data
end

#draw_pdf(binary = false) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rsyntaxtree.rb', line 195

def draw_pdf(binary = false)
  surface = nil
  context = nil
  b = nil
  b = StringIO.new
  svg = draw_svg
  rsvg = RSVG::Handle.new_from_data(svg)
  dim = rsvg.dimensions
  surface = Cairo::PDFSurface.new(b, dim.width, dim.height)
  context = Cairo::Context.new(surface)
  context.render_rsvg_handle(rsvg)
  surface.finish
  binary ? b : b.string
rescue Cairo::InvalidSize
  raise RSTError, +"Error: the result syntree is too big"
ensure
  b&.close unless binary
  context&.destroy
end

#draw_png(binary = false) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rsyntaxtree.rb', line 174

def draw_png(binary = false)
  surface = nil
  context = nil
  b = nil
  svg = draw_svg
  rsvg = RSVG::Handle.new_from_data(svg)
  dim = rsvg.dimensions
  surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, dim.width, dim.height)
  context = Cairo::Context.new(surface)
  context.render_rsvg_handle(rsvg)
  b = StringIO.new
  surface.write_to_png(b)
  binary ? b : b.string
rescue Cairo::InvalidSize
  raise RSTError, +"Error: the result syntree is too big"
ensure
  b&.close unless binary
  surface&.finish
  context&.destroy
end

#draw_svgObject



215
216
217
218
219
220
# File 'lib/rsyntaxtree.rb', line 215

def draw_svg
  sp = StringParser.new(@params[:data].gsub('&', '&amp;'), @params[:fontset], @params[:fontsize], @global)
  sp.parse
  graph = SVGGraph.new(sp.get_elementlist, @params, @global)
  graph.svg_data
end

#draw_tikz(standalone: false, font: nil) ⇒ Object



249
250
251
252
253
254
# File 'lib/rsyntaxtree.rb', line 249

def draw_tikz(standalone: false, font: nil)
  sp = StringParser.new(@params[:data].gsub('&', '&amp;'), @params[:fontset], @params[:fontsize], @global)
  sp.parse
  generator = TikZGenerator.new(sp.get_elementlist, @params)
  generator.generate(standalone: standalone, font: font)
end