[PyQt] SVG Glyphs in PyQt

Kyle Covington kyle at red-r.org
Tue Sep 14 17:54:26 BST 2010


>
> I can't speak for Cairo-generated SVG files, but PyQt4 doesn't have a
> problem displaying glyphs from SVG font files generated by FontForge, for
> example.  Assuming that file  /Users/colin/MyFonts/MagicFont.svg  contains a
> valid <font id="MagicFont" ...> ... </font> definition then a test file such
> as the following should cause "Sample text here" to be displayed using
> glyphs from MagicFont:
>
> <?xml version="1.0" standalone="no"?>
> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20001102//EN" "
> http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd">
> <svg width="400px" height="300px" version="1.1"
>     xmlns = 'http://www.w3.org/2000/svg'
>     xmlns:xlink="http://www.w3.org/1999/xlink">
> <defs>
>    <font-face font-family="MagicFont">
>        <font-face-src>
>            <font-face-uri
>
>  xlink:href="file:///Users/colin/MyFonts/MagicFont.svg#MagicFont"/>
>        </font-face-src>
>    </font-face>
> </defs>
>
> <text font-size="32" x="20" y="40" style="font-family: MagicFont;">
>        Sample text here
> </text>
>
> </svg>
>
> I used the svgviewer example from the PyQt4 demo to display the sample
> text.
>
> Hope this helps.
> -- Colin


Thanks for the input.  Cairo embeds the glyphs in the svg and uses a tag
called use to place the glyph into the plot.

This is a sample from a cairo svg that I just generated.

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="
http://www.w3.org/1999/xlink" width="432pt" height="432pt" viewBox="0 0 432
432" version="1.1">
<defs>
<g>
<symbol overflow="visible" id="glyph2-0">
<path style="stroke: none;" d="M 0 -1.050781 L -5.261719 -1.050781 L
-5.261719 -5.261719 L 0 -5.261719 Z M -0.132813 -1.183594 L -0.132813
-5.132813 L -5.132813 -5.132813 L -5.132813 -1.183594 Z M -0.132813
-1.183594 "/>
</symbol>
<symbol overflow="visible" id="glyph2-1">
<path style="stroke: none;" d="M 0 -2.722656 L -1.441406 -2.722656 L
-1.441406 -0.105469 L -2.121094 -0.105469 L -6.027344 -2.855469 L -6.027344
-3.460938 L -2.121094 -3.460938 L -2.121094 -4.277344 L -1.441406 -4.277344
L -1.441406 -3.460938 L 0 -3.460938 Z M -2.121094 -2.722656 L -4.839844
-2.722656 L -2.121094 -0.835938 Z M -2.121094 -2.722656 "/>
</symbol>
...
</g>
...
</defs>
<g id="surface0">
<g clip-path="url(#clip1)" clip-rule="nonzero">
<path style="fill: none; stroke-width: 1; stroke-linecap: round;
stroke-linejoin: round; stroke: rgb(0%,0%,0%); stroke-opacity:
1;stroke-miterlimit: 10; " d="M 42.769531 104.457031 L 104.457031 104.457031
L 104.457031 42.769531 L 42.769531 42.769531 L 42.769531 104.457031 "/>
</g>
<g clip-path="url(#clip2)" clip-rule="nonzero">
<g style="fill: rgb(0%,0%,0%); fill-opacity: 1;">
  <use xlink:href="#glyph0-1" x="41.738281" y="76.289063"/>
  <use xlink:href="#glyph0-2" x="48.738281" y="76.289063"/>
  <use xlink:href="#glyph0-3" x="54.738281" y="76.289063"/>
  <use xlink:href="#glyph0-4" x="60.738281" y="76.289063"/>
  <use xlink:href="#glyph0-5" x="66.738281" y="76.289063"/>
  <use xlink:href="#glyph0-6" x="69.738281" y="76.289063"/>
  <use xlink:href="#glyph0-7" x="72.738281" y="76.289063"/>
  <use xlink:href="#glyph0-2" x="78.738281" y="76.289063"/>
  <use xlink:href="#glyph0-8" x="84.738281" y="76.289063"/>
  <use xlink:href="#glyph0-9" x="90.738281" y="76.289063"/>
  <use xlink:href="#glyph0-10" x="96.738281" y="76.289063"/>
  <use xlink:href="#glyph0-11" x="99.738281" y="76.289063"/>
</g>
</g>
...

I don't really know a lot about svg tags and which ones are conventional or
not.  I'm sure that cairo makes appropriate syntax because it renders on
other svg viewers that I have tried (GIMP, photoshop).  I'm in kind of a
bind to use cairo for svg generation.  My application (Red-R) uses R for
data analysis and cairo seems to be the best suited svg generating graphics
package for R.

I'm not using the most up-to-date version of pyqt so that might also be the
problem.

Anyway, my solution is providing a good workaround for the problem, I just
wanted to give the code out in case someone else was having the same issue.
 If pyqt would like an example of a cairo svg from R that didn't render I
can send one if you would like to tackle the problem.

Thanks again for the reply.

Kyle


>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100914/e232c080/attachment-0001.html>


More information about the PyQt mailing list