Half a year ago, I experimented with gtk+ and cairo to extract a PDF screenshot out of a progam, i.e. a screenshot of the window that is not a table of colored dots as a regular PNG file would be, but rather a vector graphic that scales perfectly and has selectable text. The main use that I see is documentation: A scalable screenshots looks much better in print, and even in an online-PDF-file, having the full-text-search also find the text in the images is useful.
My experiments back then were successful in a proof-of-concept way, but they were very ugly hacks. With the new version 3 of gtk+, though, things become much easier, as only one cairo context is used, instead of each widget creating its own context to draw on.
By now, the first gtk-3 applications have reached Debian unstable, so I thought it would be time to re-do the experiment. And the result is very pleasing: The pdf screenshot program comes as a gtk module that you can load by setting an environment variable before launching a gtk-3 program. The application will run as usual, but there is an additional PDF screenshot button. If you click it, you get a “Save File” dialog (with a nice, live-updated preview of the screenshot, as an additional gimmick) that lets you save a PDF file. Its as easy as that. You can check out the resulting PDF file of gucharmap and a screencast of me creating that file, including copy’n’pasting from the resulting document.
PS: I would offer the video as well on YouTube, but it seems it does not accept Ogg Theora videos. I hope all of you can make use of the above links.
Update: By popular demand, I also created a SVG version. It even looks better because the embedded pixmaps have a higher resolution – probably gtk is confused by the resolution of the PDF cairo surface. The code is almost trivially the same. I guess I could add other formats (PNG, PS) as well. Now I just have to find out how to tame GtkFileChooser to offer this feature better.
It could be released soon. It still needs some UI to select the window in case the application has more than one.
Ideally, I’d like to take screenshots of any running program, not only those started with the right GTK module. But I don’t know if it is possible to hook into a running gtk program without resorting to too much hacks.
Or maybe the gtk module is included by default in all running applications, and activated by some signalling, e.g. via the X properties of the window.
But other than those bells and whistles, the code is ok so far.
It does not work in all cases, of course. It can only handle gtk-3 applications, and excludes those that do not use cairo for drawing (e.g. gnome-terminal’s vty widget).
Pretty nifty. You can actually do similar things in Qt: QPainter draw calls can be serialized into SVG via QSvg. The resulting SVG lacks some polish, and if the app buffers any rendering through pixmaps it will embed raster data for those elements which then can't scale, but depending on the app it can be of some use. Perhaps your work will inspire some additional improvements there.
but when i read it on golem at first i thought that text remains text...:)
would be pretty cool! no, it really would be awesome!!
i do not mean ocr stuff. i was thinking about exporting "selectable text" as text.
if you can select text as a user, it should be possible for a process as well, right?
ps: have you finished your double-diploma yet?
ah ok, i just checked the svg version, where text is converted :/
not checked pdf file yet, but man, that's awesome!
ps: thumbs up for finishing :)
pps: the text-selection color in this comment box is not distinguishable from the background color, intention or does something went wrong with css? (ff4, win7)
Ah, I see. I did not know that with SVG, the font gets included as shapes. Slight disadvantage, but maybe that can be fixed within Cairo. Filed a bug report at https://bugs.freedesktop.org/show_bug.cgi?id=38516
It is here. Try tilting your laptop screen, if I tilt mine down, it becomes indistinguishable.
le Gall Erwan tried to comment here but it did not work properly, hence I am pasting his mailed comment:
Inkscape can convert Pdf to Svg, while asking you if you want to convert text to shape (or not).
A test made with the Joachim's PDF sample worked very well: I was able to edit and translate all texts from German to French, even the smileys! ;-]
Very useful to send translation in context (i. e. showing labels that are too short to contain a translated text), both in a portable manner (all modern browsers are indeed able to show svg graphics) and an indexable one (svg are text, easy to index and edit). Gzip compressed svgz graphics are generally lighter (i. e. 1/7 in this case) than equivalent png bitmaps. Very nice for good quality printing too!
Lots of advantages. Thanks to Joachim for this brilliant idea.
I'm very interested in your application as I'm working on documentation for an application that we've developed. The application interface is through a supported browser i.e. Firefox. I would love to be able to get vectorised screenshots. My question: If install firefox running it on a distro that supports GTK3 (I think Fedora would be easy) will I be able to capture vectored screenshots of the application using GTK Vector?