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.



56
57
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
# File 'lib/rsyntaxtree.rb', line 56

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 :symmetrize, :transparent, :polyline, :hide_default_connectors
      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
      new_params[key] = value.to_f
    when :fontstyle
      case value
      when "noto-sans-mono", "mono"
        fontset[:normal] = FONT_DIR + "/NotoSansMono_SemiCondensed-Regular.ttf"
        fontset[:italic] = FONT_DIR + "/NotoSansMono_SemiCondensed-Regular.ttf"
        fontset[:bold] = FONT_DIR + "/NotoSansMono_SemiCondensed-Bold.ttf"
        fontset[:bolditalic] = FONT_DIR + "/NotoSansMono_SemiCondensed-Bold.ttf"
        fontset[:math] = FONT_DIR + "/latinmodern-math.otf"
        fontset[:cjk] = FONT_DIR + "/NotoSansJP-Regular.otf"
        fontset[:emoji] = FONT_DIR + "/OpenMoji-black-glyf.ttf"
        fontset[:family] = FontFamily.for_pango(:mono)
        fontset[:family_cjk] = FontFamily.for_pango(:mono, cjk_priority: true)
        new_params[:fontstyle] = "mono"
      when "noto-sans", "sans"
        fontset[:normal] = FONT_DIR + "/NotoSans-Regular.ttf"
        fontset[:italic] = FONT_DIR + "/NotoSans-Italic.ttf"
        fontset[:bold] = FONT_DIR + "/NotoSans-Bold.ttf"
        fontset[:bolditalic] = FONT_DIR + "/NotoSans-BoldItalic.ttf"
        fontset[:math] = FONT_DIR + "/NotoSansMath-Regular.ttf"
        fontset[:cjk] = FONT_DIR + "/NotoSansJP-Regular.otf"
        fontset[:emoji] = FONT_DIR + "/OpenMoji-black-glyf.ttf"
        fontset[:family] = FontFamily.for_pango(:sans)
        fontset[:family_cjk] = FontFamily.for_pango(:sans, cjk_priority: true)
        new_params[:fontstyle] = "sans"
      when "noto-serif", "serif"
        fontset[:normal] = FONT_DIR + "/NotoSerif-Regular.ttf"
        fontset[:italic] = FONT_DIR + "/NotoSerif-Italic.ttf"
        fontset[:bold] = FONT_DIR + "/NotoSerif-Bold.ttf"
        fontset[:bolditalic] = FONT_DIR + "/NotoSerif-BoldItalic.ttf"
        fontset[:math] = FONT_DIR + "/latinmodern-math.otf"
        fontset[:cjk] = FONT_DIR + "/NotoSerifJP-Regular.otf"
        fontset[:emoji] = FONT_DIR + "/OpenMoji-black-glyf.ttf"
        fontset[:family] = FontFamily.for_pango(:serif)
        fontset[:family_cjk] = FontFamily.for_pango(:serif, cjk_priority: true)
        new_params[:fontstyle] = "serif"
      when "cjk zenhei", "cjk"
        fontset[:normal] = FONT_DIR + "/wqy-zenhei.ttf"
        fontset[:italic] = FONT_DIR + "/NotoSans-Italic.ttf"
        fontset[:bold] = FONT_DIR + "/NotoSans-Bold.ttf"
        fontset[:bolditalic] = FONT_DIR + "/NotoSans-BoldItalic.ttf"
        fontset[:math] = FONT_DIR + "/NotoSansMath-Regular.ttf"
        fontset[:cjk] = FONT_DIR + "/wqy-zenhei.ttf"
        fontset[:emoji] = FONT_DIR + "/OpenMoji-black-glyf.ttf"
        fontset[:family] = FontFamily.for_pango(:cjk)
        fontset[:family_cjk] = FontFamily.for_pango(:cjk)
        new_params[:fontstyle] = "cjk"
      end
    else
      new_params[key] = value
    end
  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]
  @global[:h_gap_between_nodes] = single_x_metrics.width * 0.8
  @global[:box_vertical_margin] = single_x_metrics.height * 0.8
end

Class Method Details

.check_data(text) ⇒ Object

Raises:



165
166
167
168
169
# File 'lib/rsyntaxtree.rb', line 165

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



229
230
231
232
233
234
235
236
237
# File 'lib/rsyntaxtree.rb', line 229

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



219
220
221
222
223
224
225
226
227
# File 'lib/rsyntaxtree.rb', line 219

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



239
240
241
242
243
244
# File 'lib/rsyntaxtree.rb', line 239

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



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

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



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

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



212
213
214
215
216
217
# File 'lib/rsyntaxtree.rb', line 212

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



246
247
248
249
250
251
# File 'lib/rsyntaxtree.rb', line 246

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