class GerbilCharts::SVGDC::SVGDC

SVGDC

Simulates a simple Device Context like CDC in MFC/Windows

A simple example

Create a 500x400 window, draw a circle, draw 10 rectangles and save it !

g=SVGDC.new(500,400)
g.circle(40,50,20)
1.upto(10) do |w|
          g.rectangle(80+w*4,10,10*w,45)
end
g.render("/tmp/tbasic1.svg")

Attributes

ajaxContext[R]
ajaxOptions[R]
base_win[R]
curr_presentation_context[R]
curr_win[R]
embed_style[R]
height[RW]
ref_javascripts[R]
ref_styles[R]
svg_javascript[R]
svg_predefs[R]
svg_styles[R]
use_tooltips[R]
width[RW]

Public Class Methods

new(w,h) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 37
def initialize(w,h)
  @width,@height=w,h
  @ops = Array.new
  @curr_presentation_context={}
  @base_win=SVGWin.new("Gerbil_Root_Window")
  setactivewindow(@base_win)
  @svg_predefs = [] 
  @use_tooltips=true
      @embed_style=false
end

Public Instance Methods

add_filter(filter) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 110
def add_filter(filter)
  @svg_predefs << filter
end
add_javascriptfile(jsfile_in) click to toggle source

To inline javascript use “inline:<javascript-file-name>”

# File lib/gerbilcharts/svgdc/svgdc.rb, line 74
def add_javascriptfile(jsfile_in)
      return unless jsfile_in
      jsfile=jsfile_in.gsub(/^inline:/,'')
      if jsfile.length <  jsfile_in.length
            @svg_javascript ||= ""  
            @svg_javascript << get_file_content(jsfile)      
      else
            @ref_javascripts ||= [] 
            @ref_javascripts << jsfile
      end
end
add_options(gobj, opt) click to toggle source

add other options (eg, CSS class)

# File lib/gerbilcharts/svgdc/svgdc.rb, line 265
def add_options(gobj, opt)
  gobj.add_options(opt)
  gobj
end
add_selected_presentations(gobj) click to toggle source

add presentation attributes

# File lib/gerbilcharts/svgdc/svgdc.rb, line 258
def add_selected_presentations(gobj)
  @curr_presentation_context.each do |k,v|
    gobj.add_presentation v
  end
end
add_stylesheetfile(cssfile_in) click to toggle source

Note on Stylesheet

Searches for the stylesheet in the current directory or in the gerbilcharts/public directory
If you want to use your own stylesheets put them in the working directory, or in the
public directory of gerbilcharts.

To inline stylesheet use “inline:<stylesheetname>” , eg “inline:brushmetal.css” To embed stylesheet use “embed:<stylesheetname>” , eg “embed:brushmetal.css” Default is to xref a stylesheet

# File lib/gerbilcharts/svgdc/svgdc.rb, line 55
def add_stylesheetfile(cssfile_in)
      return unless cssfile_in
      cssfile=cssfile_in.gsub(/^inline:/,'')
      if cssfile.length <  cssfile_in.length
          @svg_styles = get_file_content(cssfile)
      else
              cssfile=cssfile_in.gsub(/^embed:/,'')
              if cssfile.length < cssfile_in.length
                  @svg_styles = get_file_content(cssfile)
                      @embed_style = true
              else
                      @ref_styles ||= []
                      @ref_styles << cssfile 
              end
      end
end
addshape(shape,opt={}) click to toggle source

add a shape directly with options

# File lib/gerbilcharts/svgdc/svgdc.rb, line 271
def addshape(shape,opt={})
    @curr_win << prime_object(shape,opt)
end
begin_polygon() click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 300
def begin_polygon
  @curr_polygon=SVGPolygon.new
end
begin_squarized_polygon() click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 304
def begin_squarized_polygon
  @curr_polygon=SVGSquarizedPolygon.new
end
circle(x,y,r,opt={}) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 275
def circle(x,y,r,opt={})
  @curr_win << prime_object(SVGCircle.new(x,y,r),opt)
end
circle_r(r, opt={} ) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 348
def circle_r(r, opt={} )
  circle( (r.left + r.right)/2, (r.bottom+r.top)/2, [r.width,r.height].min/2, opt)
end
clear_presentation() click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 357
def clear_presentation
      @curr_presentation_context = []
end
ellipse(x,y,r1,r2,opt={}) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 279
def ellipse(x,y,r1,r2,opt={})
  @curr_win << prime_object(SVGEllipse.new(x,y,r1,r2),opt)
end
enable_tooltips(bval) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 86
def enable_tooltips(bval)
  @use_tooltips=bval
end
end_polygon(opt={}) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 313
def end_polygon(opt={})
    @curr_win << prime_object(@curr_polygon,opt)
    @curr_polygon=nil
end
endline(opt={}) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 293
def endline(opt={})
  if @curr_polyline
    @curr_win << prime_object(@curr_polyline,opt)
  end
  @curr_polyline=nil
end
line(x1,y1,x2,y2, opt={}) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 319
def line(x1,y1,x2,y2, opt={})
  @curr_win << prime_object(SVGLine.new(x1,y1,x2,y2), opt)
end
lineto(x,y) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 288
def lineto(x,y)
  @curr_polyline=SVGPolyline.new if not @curr_polyline
  @curr_polyline.lineto(x,y)
end
moveto(x,y) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 283
def moveto(x,y)
  @curr_polyline=SVGPolyline.new
  @curr_polyline.lineto(x,y)
end
newwin(name, opts={}) { |self| ... } click to toggle source

create a new window , all subsequent operations work on this new window

# File lib/gerbilcharts/svgdc/svgdc.rb, line 116
def newwin(name, opts={})
  w=SVGWin.new(name, opts)
  @curr_win << w
      @curr_win = w
      yield self  if block_given?
      reset if block_given?
      return w if not block_given?
end
polygon_point(x,y) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 308
def polygon_point(x,y)
        @curr_polygon ||= SVGPolygon.new
    @curr_polygon.addpoint(x,y)
end
prime_object(gobj, opt) click to toggle source

prime the graphics object

# File lib/gerbilcharts/svgdc/svgdc.rb, line 251
def prime_object(gobj, opt)
  add_selected_presentations(gobj)
  add_options(gobj,opt)
  gobj
end
rectangle(x,y,w,h, opt={}) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 340
def rectangle(x,y,w,h, opt={})
  @curr_win << prime_object(SVGRect.new(x,y,w,h), opt)
end
rectangle_r(r, opt={} ) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 344
def rectangle_r(r, opt={} )
  rectangle(r.left, r.top, r.width, r.height, opt)
end
render(opts={}) click to toggle source

synonym for render_to_file with no options

# File lib/gerbilcharts/svgdc/svgdc.rb, line 244
def render(opts={})
    opts[:string]=render_to_string(opts) if opts[:string]
    render_to_file(opts[:file],opts)     if opts[:file]
    render_to_frag(opts[:xfrag],opts)  if opts[:xfrag]
end
render_to_file(outpath,opts={}) click to toggle source

render the SVG to a file

# File lib/gerbilcharts/svgdc/svgdc.rb, line 236
def render_to_file(outpath,opts={})
  svg_string = render_to_string(opts)
  File.open(outpath, 'w') { |file|
     file.write(svg_string)
  } 
end
render_to_frag(doc,opts) click to toggle source

render to XML Builder fragment

# File lib/gerbilcharts/svgdc/svgdc.rb, line 134
def render_to_frag(doc,opts)
  
     # if options has windowonly option, then just render the top most window
     if opts[:windowonly] 
       # render contents of top window
       doc.g(:id => "GerbilSVGGraph") {
           @curr_win.render(doc)
       }        
       return
     end
  
  
     # svg base attributes
     svg_attributes = {:xmlns => "http://www.w3.org/2000/svg", 
                                              "xmlns:xlink" => "http://www.w3.org/1999/xlink", 
                                               :version => "1.1", :width => @width, :height => @height}

         # hook up javascript support
     if requires_ajax_block? or requires_tooltips?
       svg_attributes.store(:onload,"Init(evt)") 
               svg_attributes.store(:onunload,"Uninit(evt)")
     end
     
     doc.svg(svg_attributes)   {
     
     # stylesheet (inlined into SVG)
     doc.style(@svg_styles, :type => "text/css")  if @svg_styles and not @embed_style
  
     # javascripts (ajax, etc) hrefed
     @ref_javascripts &&   @ref_javascripts.each do |scr|
         doc.script(:type => "text/javascript", "xlink:href" => scr)
     end
  
     # javascript inlined again
     if @svg_javascript
       doc.script(:type => "text/javascript") { doc.cdata!(@svg_javascript) }
     end
      
     # defs (all image filters go here)
     doc.defs() {
       @svg_predefs.each do |filter|
         filter.render(doc)
       end
     }
     
     # Ajax block (for gerbilAjax.js library)
     if requires_ajax_block?
       doc.g(:id => "GerbilAjaxBlock") {
          
          ao = {:id => "GerbilAjaxOptions"}
          @ajaxOptions.each_pair do |k,v|
            ao.store(k,v)
          end
          doc.g(ao)
          
          if @ajaxContext
            ac = {:id => "GerbilAjaxContext"}
            @ajaxContext.each_pair do |k,v|
              ac.store(k,v)
            end
            doc.g(ac)
          end
       }
     end
     
     # render contents of top window
     doc.g(:id => "GerbilSVGGraph") {
         @curr_win.render(doc)
     }
     
     # render tooltips optional
   }      

       # convert external stylesheet css to inline styles
       
end
render_to_string(opts={}) click to toggle source

render the SVG fragments to a string options

# File lib/gerbilcharts/svgdc/svgdc.rb, line 213
def render_to_string(opts={})
 
 # render all the elements accumulated so far
 svg_string=""
 doc = Builder::XmlMarkup.new(:target => svg_string, :indent => 2, :standalone => "no")
 doc.instruct!
 doc.declare! :DOCTYPE, :svg, :PUBLIC,"-//W3C//DTD SVG 1.1//EN","http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"

 # stylesheet instruction if hreferenced
 if @ref_styles
      @ref_styles.each do |css|
         doc.instruct! "xml-stylesheet",  :href => css, :type => "text/css"
      end
 end
 
 render_to_frag(doc,opts)
 
 # return the svg_string (inline if embedding desired)
 return @embed_style ?  do_embed_styles(svg_string) : svg_string

end
requires_ajax_block?() click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 90
def requires_ajax_block?
  if @ajaxOptions.nil? and @ajaxContext.nil?
    return false
  else
    return true
  end
end
requires_tooltips?() click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 98
def requires_tooltips?
  return @use_tooltips
end
reset() click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 125
def reset
      @curr_win = @base_win
end
select_presentation(opt={}) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 352
def select_presentation(opt={})
  @curr_presentation_context.store(:pen,opt[:pen])            if opt[:pen]
  @curr_presentation_context.store(:brush,opt[:brush])      if opt[:brush]
end
set_ajaxContext(h) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 102
def set_ajaxContext(h)
  @ajaxContext=h
end
set_ajaxOptions(h) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 106
def set_ajaxOptions(h)
  @ajaxOptions=h
end
setactivewindow(w=nil) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 129
def setactivewindow(w=nil)
  @curr_win = w ? w : @base_win
end
textout(x,y,str, opt={}) click to toggle source
# File lib/gerbilcharts/svgdc/svgdc.rb, line 323
def textout(x,y,str, opt={})
  @curr_win << prime_object(SVGText.new(x,y,str), opt)
end
textout_monospace(x,y,str,opt={}) click to toggle source

utility method

automatically adds the 'font-family: monospace" , "stroke :none", "fill: black"
they can all be overridden by passing in an opt
# File lib/gerbilcharts/svgdc/svgdc.rb, line 331
def textout_monospace(x,y,str,opt={})
      defopt = { "font-family" => "monospace", "stroke" => "none", "fill" =>  "black" }
      defopt.merge!(opt) if opt

      textout(x,y,str,defopt)

end

Private Instance Methods

do_embed_styles(svgdoc_string) click to toggle source

external styles to inline (for svg_web)

# File lib/gerbilcharts/svgdc/svgdc.rb, line 375
def do_embed_styles(svgdoc_string)

      if @ref_styles
              css_string = File.read('@ref_styles')
      else
              css_string = @svg_styles
      end

      css_inliner = GerbilCharts::SVGDC::CssInliner.new(css_string)
      return css_inliner.inline_doc(svgdoc_string)

end
get_file_content(fname) click to toggle source

search for the file in the current directory or the gem path

# File lib/gerbilcharts/svgdc/svgdc.rb, line 364
def  get_file_content(fname)
              if File.exists? fname
                 return File.read(fname)
              else
                 try_from_gem = File.dirname(__FILE__) + "/../public/" + fname
                 return File.read(try_from_gem) if File.exists? try_from_gem
              end
         raise "Resource #{fname} not found in current directory or in the gerbilcharts/public folder"
end