[cairo] Qahirah: A High-Level Cairo API Binding For Python 3

Olaf Schmidt ng at vbRichClient.com
Sun Mar 8 19:04:13 PDT 2015


Am 07.03.2015 um 04:10 schrieb Lawrence D'Oliveiro:
> On Fri, 06 Mar 2015 19:18:57 -0700, Dan Raymond wrote:
>
>> I could do it in one line of C code.  That is what functions are for.
>
> *Yawn* Enough with the content-free hand-waving hot air already. Show
> us the code!
>

Sorry, for not showing C-Code - but a nice challenge nevertheless. :-)

Below comes VB(Script)-Code, which makes use of my COM-Wrapper for
cairo... so that's Windows-only - for those interested - it requires
installation of the COM-lib vbRichClient5.dll -> free downloadable at:
http://vbRichClient.com/#/en/Downloads.htm

The code-example is available as a TextFile here:
http://vbRichClient.com/Downloads/AlhambraTiles.txt

It produces three Png-Files in different Zoom-Levels
(0.5, 1, 1.5 - in the same path the *.vbs is started from):
http://vbRichClient.com/Downloads/Alhambra_Zoom_0.5.png
http://vbRichClient.com/Downloads/Alhambra_Zoom_1.png
http://vbRichClient.com/Downloads/Alhambra_Zoom_1.5.png

The code-size is about two/thirds of your python-script here:
https://github.com/ldo/qahirah_examples/blob/master/alhambra_tiles

And it produces 3 (instead of 2) different "Alhambra-Patterns".
(the 3rd a slight, more "angular" variation of the winged-pattern).

As for C being less efficient (lines-of-code-wise) - I'd be carefull
with such an assumption, because the needed LOC depend a great deal
on the experience in a given environment (with regards to the language
and the libs in question).

What I find the most useful in "higher-level"-languages and wrappers,
is their less stressing (auto-)memory-management - along with automatic
Handle-freeing behind the Class-encapsulations for the wrapped C-structs...

I've approached the "Winged-Pattern" a bit differently than you -
(a bit more "straight forward" - closer to the flat-C-API) - and
can imagine that a C-implementation of the following routine, will
not need all that many more LOC...

It's basically the same routine, as in the larger VBScript-TextFile
above - but slightly changed, usable standalone, able to render
directly into a Rectangle on a given cairo-context, supporting
different ZoomLevels for the pattern directly in an Optional Param.

'the Zoom-Param works for 1/4, 1/3, 1/2, 3/4, 1, 1.25, 1.33, ... etc.
Sub RenderTriWingPattern(CC As cCairoContext, Color, Optional Zoom=1)
Dim R As Double, A As Double, P(0 To 17) As Double, i, j, n

   CC.PushGroup , 0, 0, 48 * Zoom, 84 * Zoom
   CC.ScaleDrawings Zoom, Zoom

   For j = 1 To 3
     For i = 1 To 3
       A = 270 * Cairo.PI / 180
       For n = 0 To 8
         R = Choose(n Mod 3 + 1, -7, 33, 28)
         A = A + Choose(n Mod 3 + 1, 5, 45, 70) * Cairo.PI / 180
         P(2 * n + 0) = R * Cos(A)
         P(2 * n + 1) = R * Sin(A)
       Next n
       CC.MoveTo P(16), P(17)
       For n = 0 To 17 Step 6
         CC.CurveTo P(n), P(n+1), P(n+2), P(n+3), P(n+4), P(n+5)
       Next n
       CC.Fill , Cairo.CreateSolidPatternLng(Color)
       CC.TranslateDrawings 48, 0
     Next i
     CC.TranslateDrawings -48 * (i - 0.5), 42
   Next j

   CC.SetSourcePattern CC.PopGroup(True, CAIRO_EXTEND_REPEAT)
   CC.Rectangle 20, 20, 400, 400
   CC.Fill
End Sub

As said, a C-porting of the above routine would not need much more code.

Olaf



More information about the cairo mailing list