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.



53
54
55
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
# File 'lib/rsyntaxtree.rb', line 53

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"
        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"
        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"
        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"
        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
  @params[:fontset] = fontset
  single_x_metrics = FontMetrics.get_metrics("X", fontset[:normal], @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:



148
149
150
151
152
# File 'lib/rsyntaxtree.rb', line 148

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

  StringParser.valid?(text)
end

Instance Method Details

#draw_pdf(binary = false) ⇒ Object



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

def draw_pdf(binary = false)
  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
  if binary
    b
  else
    b.string
  end
rescue Cairo::InvalidSize
  raise RSTError, +"Error: the result syntree is too big"
end

#draw_png(binary = false) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rsyntaxtree.rb', line 154

def draw_png(binary = false)
  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)
  if binary
    b
  else
    b.string
  end
rescue Cairo::InvalidSize
  raise RSTError, +"Error: the result syntree is too big"
end

#draw_svgObject



190
191
192
193
194
195
# File 'lib/rsyntaxtree.rb', line 190

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_treeObject

Currently not used



198
199
200
201
202
203
204
205
206
207
# File 'lib/rsyntaxtree.rb', line 198

def draw_tree
  svg = draw_svg
  image, _data = Magick::Image.from_blob(svg) do |im|
    im.format = 'svg'
  end
  image.to_blob do |im|
    im.format = @params[:format].upcase
  end
  image
end