ex11, plotting and Cairo ...

Jouni Rynö <>
Wed Jul 20 07:28:43 CEST 2005

Good, the open questions I wanted to see :)

The reason I prompted for Cairo was the moment I needed justified text
on the plot labels in ex11. The xlib provides function to calculate the
string width, it's done by taking the font metrics and calculating the
total width from individual characters. That was easy in erlang :)

text_width(Parent, Font, Str) ->
    Display = sw:rpc(Parent, display),
    F = ex11_lib:xQueryFont(Display, Font),
    CharList = F#fontInfo.char_infos,
    {Sizes, CharList} = lists:mapfoldl(fun(Char, CharList) ->
			CI = lists:nth(Char, CharList),
			{CI#charInfo.width, CharList}
			CharList, Str),

But adding new routines opens the pandora box, I want more ...

Cairo has the PDF 1.4 programming model, and seems to handle the font
and text related needs at reasonable level (alpha and such ...) And it
would be the canvas only API ... Windows or whatever drawing backend is
needed, it's selected before the drawing is done.

About the GDK: from the GDK-Cairo interaction

Cairo is a graphics library that supports vector graphics and image
compositing that can be used with GDK. Since 2.8, GTK+ does most of its
drawing using Cairo. 

GDK does not wrap the Cairo API, instead it allows to create Cairo
contexts which can be used to draw on GDK drawables. Additional
functions allow to convert GDK's rectangles and regions into Cairo paths
and to use pixbufs as sources for drawing operations.

Anyway, it would seem to me, that Cairo API for canvas would be
sufficient, for windows and events, X/ex11 is enough :). But I take a
look on the GDK ...


On Wed, 2005-07-20 at 09:11 +0200, Joe Armstrong (AL/EAB) wrote:
>  What fun. As regards graphics in Erlang the situation is still a mess.
>  I see things the problem is more one of "programming models" than making
> an Erlang interface to X (replace X by your favourite package).
>  Suppose you have a rock solid interface to Gtk - then you still have to
> learn the Gtk programming model - which is not an easy job.
>  The problem with most graphics packages is the programming model which are often
> horribly difficult. Most "packers" are difficult to use, and often, for no apparent
> reason get layouts wrong. For simple graphics, just a window with a few boxes,
> most graphics packages are far too complex to use.
> Worse, the programs are so complex that the use of a "visual tool" to create the
> code is almost obligatory - that you need to use such a tool is a sure sign that
> something is wrong.
> What I want is VSG (very simple graphics) - I have an "acid test" for VSG, I want to
> write three applications:
> 	1) A traditional GUI (say for an IRC chat widget)
> 	2) A drag-and-drop interface (say for drawing state diagrams)
> 	3) An image manipulation application (think GIMP)
> All of these should be easy to write using VSG, using a small number of 
> intuitive primitives.
> The ex11 programming model was an attempt at this.
> I'm currently working on VSG with a TCL/TK backend.
> One thing I did find was that the only model I like is based on a canvas
> widget. To create buttons (and controls in general) I just draw rectangles and lines
> onto a canvas widget and animate them on mouse clicks.
> If we want to make a decent graphics system I think the way to do so is as follows:
> 	1) Implement a solid set of cross-platform graphics primitives
> 	2) Implement a canvas widget on top of 1)
> 	3) Implement a GUI set on top of 2)
> ex11 just used the X11 primitives - which were *very* simple to use. But they were
> not portable.
> I had thought that one way to do this is to implement an interface to GDK and then
> reimplement something like GTK in Erlang. The latter step should be pretty easy,
> writing complex widgets with concurrency at your fingertips is pretty easy.
> So if you want to spend your 2 weeks of holiday fruitfully try to make
> a solid interface to GDK that runs on windows and linux.
> << aside - why can't we just define a low-level API and then layer graphics
> on top of this. The low level could be implemented using your favorite package.
>       The following API would suffice
> 	mkWin(X, Y, Width, Ht) => Win
> 	draw(Win, rectangle, X, Y, Width, Ht, Color) => Tag
> 	draw(Win, line, X1, Y1, X2, Y2, Width, Color) => Tag
> 	draw(Win, text, X, Y, Font, Size, Text) => Tag
> 	draw(Win, image, X, Y, ImageFile) => Tag
> 	...
> 	add_event_handler(Win, X, Y, Width, Ht, mouse_click, Fun) => Tag
> 	delete(Win, Tag) => true 
> So you can create a window (mkWin) - draw things (draw) - add event handlers
> and deleted tagged things.
> Given this (or something like this) then pretty fancy applications can be layered on
> top of this. 
> >>
> Cheers
> /Joe
> > -----Original Message-----
> > From: 
> > [mailto:]On Behalf Of Jouni Rynö
> > Sent: den 19 juli 2005 11:33
> > To: 
> > Subject: ex11, plotting and Cairo ...
> > 
> > 
> > Dear All
> > 
> > As a summer time hobby I started to program a plotting widget 
> > for Joe's
> > ex11 (2004.09.09) version. Although there are better tools and
> > programs :) than Erlang for graphics, sometimes a plot could be handy
> > just from Erlang, like algorithm testing, timing checking etc.
> > 
> > After coming to the "hello, world" -level, one gets a bit greedy. What
> > else should it do and how? From Tcl/Tk there's the Blt-library, but
> > aiming to that would be a bit too ambitious. Live zooming at least :)
> > 
> > The other issue is the plot itself. It's easy to get to the 
> > screen, but
> > hardcopy gets more complicated (xwd -> xwdtopnm -> pnmtojpeg -> ...).
> > And no publications with these plots :), although that was not the
> > intention at first either.
> > 
> > The Gtk, Mozilla and OpenOffice seem to be converting to use Cairo
> > library as 2D drawing library. It has backends to at least ps, gl and
> > xlib. Drawing and printing can then be done with the same code. On the
> > other hand, the whole point of ex11 is bypassing the slow drawing
> > library in between. But maybe a special Cairo-canvas on top of
> > ex11-canvas.
> > 
> > So would there be any point of implementing Erlang binding to 
> > Cairo? Or
> > would it then make more sense to use Gtk, which would then use Cairo
> > indirectly (and provide themes and other fancy (and slow) stuff.
> > 
> > still 2 weeks of holiday left (minus wife and the kids)
> > 	Jouni
> > 
> > ps. the plot is done by explot:plot("Hello, World").
> > -- 
> > 
> >   Jouni Rynö                            mailto://Jouni.Ryno@fmi.fi/
> >                                         http://www.geo.fmi.fi/~ryno/
> >   Finnish Meteorological Institute      http://www.fmi.fi/
> >   Space Research                        http://www.geo.fmi.fi/
> >   P.O.BOX 503                           Tel      (+358)-9-19294656
> >   FIN-00101 Helsinki                    FAX      (+358)-9-19294603
> >   Finland                               priv-GSM (+358)-50-5302903
> >   
> >   "It's just zeros and ones, it cannot be hard"
> > 
> > 

  Jouni Rynö                            mailto://Jouni.Ryno@fmi.fi/
  Finnish Meteorological Institute      http://www.fmi.fi/
  Space Research                        http://www.geo.fmi.fi/
  P.O.BOX 503                           Tel      (+358)-9-19294656
  FIN-00101 Helsinki                    FAX      (+358)-9-19294603
  Finland                               priv-GSM (+358)-50-5302903
  "It's just zeros and ones, it cannot be hard"

More information about the erlang-questions mailing list