Arc Forumnew | comments | leaders | submitlogin
SVG
9 points by skenney26 6072 days ago | 11 comments
Recently I've been playing around with generating Scalable Vector Graphics in Arc. The code can be found at http://github.com/skenney26/sarc/tree/master/svg.arc

For SVG to display properly the content-type of the document needs to be set to xhtml rather than html. For these examples header* in srv.arc needs to be changed to:

  (= header* "HTTP/1.0 200 OK
  Content-Type: application/xhtml+xml; charset=utf-8
  Connection: close")
(If you're using the anarki version you'll need to change textmime* instead. Btw, I downloaded anarki on saturday and had to comment out ffi.scm from as.scm and ffi.arc from lib.arc in order to make everything work. I don't know if this is still an issue.)

Then just (load "svg.arc") and run (asv) to view the following examples in your browser (http://localhost:8080/...). (These examples have been tested in Firefox, Safari, and Opera.)

Display a blue circle:

  (defop circle req
    (svgpage
      (svg (svg width 400 height 400)
        (svg (circle cx 200 cy 200 r 50 fill 'blue)))))
This can be shortened (but it won't work properly in Opera) to:

  (svgop circle2
    (circle 200 200 50 "#00ff00"))
Display several circles with random positions, colors, and opacities:

  (defop circles req
    (svgpage
      (svg (svg width 500 height 500)
        (repeat 20
          (svg (circle cx (rand-range 100 400)
                       cy (rand-range 100 400)
                       r (rand-range 10 70)
                       fill (rand-hex-color)
                       opacity (num (/ (rand-range 4 8) 10))))))))
Display several rectangles in a row with random colors:

  (defop rects req
    (svgpage
      (svg (svg width 800 height 600)
        (for i 1 20
          (svg (rect x (* i 10) y (* i 10)
                     width 40 height 40
                     fill (rand-hex-color)
                     opacity .5))))))
There's alot more that can be added to this library. Over the next few weeks I'll add support for lines, patterns, transformations, etc.


1 point by drcode 6072 days ago | link

Great tool- I will probably use it at some point...

Does that change in header* break regular web pages from serving? If not, how is it possible for Content-type to be the same for SVG and regular web pages?

-----

1 point by skenney26 6072 days ago | link

Changing the Content-type just lets the browser know that you're using xhtml rather than html. This will occasionally cause some issues when using svg.arc in combination with html.arc because the browser will expect your html to be xhtml compliant. This could probably be smoothed over by creating an xhtml version of html.arc and modifying the generated code so that its xhtml compliant.

-----

1 point by almkglor 6072 days ago | link

I think it would be better to completely update html.arc to use proper xhtml, or at least let the operator specify the MIME type.

-----

3 points by kens 6072 days ago | link

The server will need to specify arbitrary content-types eventually, and that's way easier than supporting xhtml, so specifying an arbitrary MIME type seems like the obvious solution to me. Also, the official MIME type for SVG is image/svg+xml, so trying to use xhtml both for SVG and HTML seems like a fragile hack.

-----

2 points by drcode 6072 days ago | link

Thanks for the great replies- I was actually also wondering how the RSS feed in news.arc deals with this... Is there some hack in there for overriding the Content-Type or are web browsers more forgiving for RSS feeds? I couldn't find any specific handling in news.arc for this issue on inspection the other day...

-----

4 points by kens2 6072 days ago | link

Firefox's Live HTTP Headers plugin tells me that news.ycombinator.com/rss has "Content-Type: text/html; charset=utf-8". I imagine RSS clients are not very strict about what they accept.

-----

1 point by drcode 6072 days ago | link

ahh... I need to get me that plugin :)

-----

1 point by listic 6071 days ago | link

I'm trying to use this on Windows. mzscheme v352. Anarki version, with srv.arc patched as suggested. (I have git.)

When I do (load "svg.arc") the following error occurs:

Error: "open-input-file: cannot open input file: \"Z:\\LAB\\Arc\\arc\\svg.arc\" (The system cannot find the file specified.; errno=2)"

What's wrong?

-----

1 point by cchooper 6071 days ago | link

I'm running the same and I don't get that error. Can you open other files ok? Have you tried running (open-input-file "svg.arc") from MzScheme? Also, have you double checked that the file is actually in the right place? (I only say that because that's the only way you should be getting this error, so it's worth looking again just in case.)

-----

1 point by Jesin 6072 days ago | link

Messing with XML in general should be pretty easy in Lisp.

-----

1 point by drcode 6072 days ago | link

FYI- In case anyone is doing this, here is my version of the canonical sexp->xml function:

  (def xexp (x (o indent 0))
    (if acons.x
        (withs ((tg . lst) x
	        atts nil
	        pad (apply string (n-of indent " "))
  	      pretty (all acons lst))
  	(and lst (acons caar.lst) (= atts pop.lst))
  	(string pad 
  		"<" 
  		tg 
  		(tostring:map [pr " " car._ "=\"" cdr._ "\""] atts) 
  		">" 
  		(when pretty
  		  "\n")
  		(apply string (map [xexp _ (+ indent 3)] lst)) 
  		(when pretty
  		  pad)
  		"</" 
  		tg 
  		">\n"))
        string.x))
It assumes the standard PLT-scheme "xexpr" format...it is naive about character encoding right now BTW.

Example:

  (xexp '(ying (foo ((bar . baz)) "zing") (yang)))

  <ying>
     <foo bar="baz">zing</foo>
     <yang>
     </yang>
  </ying>
(fyi, 'tag makes html assumptions and breaks on most xml, nor is it really appropriate in style for building xml)

-----