Class: RSyntaxTree::RSGenerator
- Inherits:
-
Object
- Object
- RSyntaxTree::RSGenerator
- Defined in:
- lib/rsyntaxtree.rb
Class Method Summary collapse
Instance Method Summary collapse
- #draw_gif ⇒ Object
- #draw_jpg ⇒ Object
- #draw_lsif ⇒ Object
- #draw_pdf(binary = false) ⇒ Object
- #draw_png(binary = false) ⇒ Object
- #draw_svg ⇒ Object
- #draw_tikz(standalone: false, font: nil) ⇒ Object
-
#initialize(params = {}) ⇒ RSGenerator
constructor
A new instance of RSGenerator.
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 |
# 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" 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
151 152 153 154 155 |
# File 'lib/rsyntaxtree.rb', line 151 def self.check_data(text) raise RSTError, +"Error: input text is empty" if text.to_s == "" StringParser.valid?(text) end |
Instance Method Details
#draw_gif ⇒ Object
215 216 217 218 219 220 221 222 223 |
# File 'lib/rsyntaxtree.rb', line 215 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_jpg ⇒ Object
205 206 207 208 209 210 211 212 213 |
# File 'lib/rsyntaxtree.rb', line 205 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_lsif ⇒ Object
225 226 227 228 229 230 |
# File 'lib/rsyntaxtree.rb', line 225 def draw_lsif sp = StringParser.new(@params[:data].gsub('&', '&'), @params[:fontset], @params[:fontsize], @global) sp.parse graph = LsifGraph.new(sp.get_elementlist, @params, @global) graph.lsif_data end |
#draw_pdf(binary = false) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/rsyntaxtree.rb', line 178 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
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rsyntaxtree.rb', line 157 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_svg ⇒ Object
198 199 200 201 202 203 |
# File 'lib/rsyntaxtree.rb', line 198 def draw_svg sp = StringParser.new(@params[:data].gsub('&', '&'), @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
232 233 234 235 236 237 |
# File 'lib/rsyntaxtree.rb', line 232 def draw_tikz(standalone: false, font: nil) sp = StringParser.new(@params[:data].gsub('&', '&'), @params[:fontset], @params[:fontsize], @global) sp.parse generator = TikZGenerator.new(sp.get_elementlist, @params) generator.generate(standalone: standalone, font: font) end |