factor/extra/graphviz/render/render.factor

201 lines
4.4 KiB
Factor
Raw Normal View History

2013-11-27 16:36:54 -05:00
! Copyright (C) 2012 Alex Vondrak.
2011-05-22 15:06:48 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors combinators compiler.units continuations
2013-11-27 16:36:54 -05:00
destructors graphviz.dot images.viewer io.backend
io.directories io.encodings.8-bit.latin1 io.encodings.utf8
io.files io.files.unique io.launcher io.standard-paths kernel
locals make namespaces parser sequences summary system
unicode.case vocabs words ;
2011-05-22 15:06:48 -04:00
IN: graphviz.render
2013-11-27 16:36:54 -05:00
<PRIVATE
! "Layout Commands" from http://graphviz.org/Documentation.php
CONSTANT: standard-layouts {
"circo"
"dot"
"fdp"
"neato"
"osage"
"sfdp"
"twopi"
}
PRIVATE>
2011-05-22 15:06:48 -04:00
SYMBOL: default-layout
"dot" default-layout set-global
2013-11-27 16:36:54 -05:00
SYMBOL: preview-format
"png" preview-format set-global
2011-05-22 15:06:48 -04:00
2013-11-27 16:36:54 -05:00
ERROR: unsupported-preview-format preview-format ;
2011-05-22 15:06:48 -04:00
2013-11-27 16:36:54 -05:00
M: unsupported-preview-format summary
drop "Unsupported preview format" ;
2011-05-22 15:06:48 -04:00
2013-11-27 16:36:54 -05:00
SYMBOL: graph-encoding
utf8 graph-encoding set-global
2011-05-22 15:06:48 -04:00
2013-11-27 16:36:54 -05:00
ERROR: unsupported-encoding graph-encoding ;
2011-05-22 15:06:48 -04:00
2013-11-27 16:36:54 -05:00
M: unsupported-encoding summary
drop "Must use utf8 or latin1 (match the graph's charset attribute)" ;
2011-05-22 15:06:48 -04:00
2013-11-27 16:36:54 -05:00
HOOK: default-graphviz-program os ( -- path/f )
2011-05-22 15:06:48 -04:00
2013-11-27 16:36:54 -05:00
M: object default-graphviz-program ( -- path/f )
standard-layouts [ find-in-path ] find nip ;
2011-05-22 15:06:48 -04:00
2013-11-27 16:36:54 -05:00
ERROR: cannot-find-graphviz-installation ;
M: cannot-find-graphviz-installation summary
drop "Cannot find Graphviz installation" ;
: ?default-graphviz-program ( -- path )
default-graphviz-program
[ cannot-find-graphviz-installation ] unless* ;
2011-05-22 15:06:48 -04:00
2013-11-27 16:36:54 -05:00
<PRIVATE
: try-graphviz-command ( path format layout -- )
2011-05-22 15:06:48 -04:00
[
2013-11-27 16:36:54 -05:00
?default-graphviz-program ,
[ , "-O" , ]
[ "-T" , , ]
[ "-K" , , ] tri*
] { } make try-output-process ;
: ?encoding ( -- encoding )
graph-encoding get-global
dup [ utf8? ] [ latin1? ] bi or
[ unsupported-encoding ] unless ;
: ?delete-file ( path -- )
dup exists? [ delete-file ] [ drop ] if ;
2011-05-22 15:06:48 -04:00
PRIVATE>
2013-11-27 16:36:54 -05:00
:: graphviz ( graph path format layout -- )
path normalize-path :> dot-file
[
graph dot-file ?encoding write-dot
dot-file format layout try-graphviz-command
]
[ dot-file ?delete-file ] [ ] cleanup ;
: graphviz* ( graph path format -- )
default-layout get-global graphviz ;
<PRIVATE
: try-preview-command ( from-path to-path -- )
[
?default-graphviz-program ,
[ , ]
[ "-o" , , ] bi*
"-T" , preview-format get-global ,
"-K" , default-layout get-global ,
] { } make try-output-process ;
! Not only must Graphviz support the image format, but so must
! images.loader
: preview-extension ( -- extension )
preview-format get-global >lower {
{ "bmp" [ ".bmp" ] }
{ "gif" [ ".gif" ] }
{ "ico" [ ".ico" ] }
{ "jpg" [ ".jpg" ] }
{ "jpeg" [ ".jpg" ] }
{ "jpe" [ ".jpg" ] }
{ "png" [ ".png" ] }
{ "tif" [ ".tif" ] }
{ "tiff" [ ".tif" ] }
[ unsupported-preview-format ]
} case ;
:: with-preview ( graph quot: ( path -- ) -- )
"preview" ".dot" [| code-file |
"preview" preview-extension [| image-file |
graph code-file ?encoding write-dot
code-file image-file try-preview-command
image-file quot call( path -- )
] cleanup-unique-file
] cleanup-unique-file ;
2011-05-22 15:06:48 -04:00
2013-11-27 16:36:54 -05:00
PRIVATE>
2011-05-22 15:06:48 -04:00
: preview ( graph -- )
2013-11-27 16:36:54 -05:00
[ image. ] with-preview ;
2011-05-22 15:06:48 -04:00
: preview-window ( graph -- )
2013-11-27 16:36:54 -05:00
[ image-window ] with-preview ;
2011-05-22 15:06:48 -04:00
<PRIVATE
2013-11-27 16:36:54 -05:00
! http://graphviz.org/content/output-formats
CONSTANT: standard-formats {
"bmp"
"canon"
"dot"
"xdot"
"cmap"
"eps"
"fig"
"gd"
"gd2"
"gif"
"ico"
"imap"
"cmapx"
"imap_np"
"cmapx_np"
"ismap"
"jpg"
"jpeg"
"jpe"
"pdf"
"plain"
"plain-ext"
"png"
"ps"
"ps2"
"svg"
"svgz"
"tif"
"tiff"
"vml"
"vmlz"
"vrml"
"wbmp"
"webp"
! ! ! Canvas formats don't actually use path argument...
! "gtk"
! "xlib"
}
: define-graphviz-by-layout ( layout -- )
[ "graphviz.render" create ]
2011-05-22 15:06:48 -04:00
[ [ graphviz ] curry ] bi
2013-11-27 16:36:54 -05:00
( graph path format -- )
2011-05-22 15:06:48 -04:00
define-declared ;
2013-11-27 16:36:54 -05:00
: define-graphviz-by-format ( format -- )
2011-05-22 15:06:48 -04:00
[
2013-11-27 16:36:54 -05:00
dup standard-layouts member? [ "-file" append ] when
"graphviz.render" create
2011-05-22 15:06:48 -04:00
]
[ [ graphviz* ] curry ] bi
2013-11-27 16:36:54 -05:00
( graph path -- )
2011-05-22 15:06:48 -04:00
define-declared ;
PRIVATE>
[
2013-11-27 16:36:54 -05:00
standard-layouts [ define-graphviz-by-layout ] each
standard-formats [ define-graphviz-by-format ] each
] with-compilation-unit
2013-11-27 16:36:54 -05:00
os windows? [ "graphviz.render.windows" require ] when