Creare file SVG con le funzioni esterne del API di Cairo

Da Gambas-it.org - Wikipedia.

La libreria Cairo consente, fra l'altro, di creare un file immagine di tipo SVG.

Per fare ciò, si dovranno utilizzare alcune funzioni esterne della libreria condivisa: "libcairo.so.2.11802.2 ".

Va considerato che la libreria Cairo sviluppa il disegno dell'immagine in tre fasi:
1) viene creata una maschera, che comprende uno o più primitive o forme vettoriali, cioè cerchi, quadrati, i font TrueType, curve di Bézier, ecc;
2) viene quindi definito un sorgente, che può essere un colore, una sfumatura di colore, una bitmap o qualche grafica vettoriale;
3) infine il risultato viene trasferito al file di destinazione o alla superficie di un oggetto grafico.


Mostriamo un semplice esempio, nel quale verrà creato un file SVG:

Library "libcairo:2.11802.2"

Private Enum CAIRO_FONT_SLANT_NORMAL = 0, CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_SLANT_OBLIQUE
Private Enum CAIRO_FONT_WEIGHT_NORMAL = 0, CAIRO_FONT_WEIGHT_BOLD

' cairo_surface_t *cairo_svg_surface_create(const char * filename, double width_in_points, double height_in_points)
' Creates a SVG surface of the specified size in points to be written to filename.
Private Extern cairo_svg_surface_create(filename As String, width_in_points As Float, height_in_points As Float) As Pointer
  
' cairo_t* cairo_create(cairo_surface_t *target)
' Creates a new cairo_t with all graphics state parameters set to default values and with target as a target surface.
Private Extern cairo_create(target As Pointer) As Pointer

' void cairo_rectangle(cairo_t *cr, double x, double y, double width, double height)
' Adds a closed sub-path rectangle of the given size to the current path at position (x, y) in user-space coordinates.
Private Extern cairo_rectangle(cr As Pointer, xF As Float, yF As Float, width As Float, height As Float)

' cairo_pattern_t * cairo_pattern_create_radial(double cx0, double cy0, double radius0, double cx1, double cy1, double radius1)
' Creates a new radial gradient cairo_pattern_t between the two circles defined by (cx0, cy0, radius0) and (cx1, cy1, radius1).
Private Extern cairo_pattern_create_radial(cx0 As Float, cy0 As Float, radius0 As Float, cx1 As Float, cy1 As Float, radius1 As Float) As Pointer

' void cairo_pattern_add_color_stop_rgb(cairo_pattern_t *pattern, double offset, double red, double green, double blue)
' Adds an opaque color stop to a gradient pattern.
Private Extern cairo_pattern_add_color_stop_rgb(patternP As Pointer, offset As Float, red As Float, green As Float, blue As Float)

' void cairo_set_source(cairo_t *cr, cairo_pattern_t *source)
' Sets the source pattern within cr to source.
Private Extern cairo_set_source(cr As Pointer, patternP As Pointer)

' void cairo_fill(cairo_t *cr)
' A drawing operator that fills the current path according to the current fill rule.
Private Extern cairo_fill(cr As Pointer)

' void cairo_set_font_size(cairo_t *cr, double size)
' Sets the current font matrix to a scale by a factor of size.
Private Extern cairo_set_font_size(cr As Pointer, size As Float)

' void cairo_select_font_face(cairo_t *cr, const char *family, cairo_font_slant_t slant, cairo_font_weight_t weight)
' Selects a family and style of font from a simplified description as a family name, slant and weight.
Private Extern cairo_select_font_face(cr As Pointer, family As String, slant As Integer, font_weight As Integer)

' void cairo_set_source_rgb(cairo_t *cr, double red, double green, double blue)
' Sets the source pattern within cr to an opaque color.
Private Extern cairo_set_source_rgb(cr As Pointer, red As Float, green As Float, blue As Float)

' void cairo_move_to(cairo_t *cr, double x, double y)
' Begin a new sub-path.
Private Extern cairo_move_to(cr As Pointer, xF As Float, yF As Float)

' void cairo_show_text(cairo_t *cr, const char *utf8)
' A drawing operator that generates the shape from a string of UTF-8 characters.
Private Extern cairo_show_text(cr As Pointer, utf8 As String)

' void cairo_destroy(cairo_t *cr)
' Decreases the reference count on cr by one.
Private Extern cairo_destroy(cr As Pointer)

' void cairo_surface_destroy(cairo_surface_t *surface)
' Decreases the reference count on surface by one.
Private Extern cairo_surface_destroy(cairo_surface As Pointer)


Public Sub Main()

 Dim surface, cairo, pattern As Pointer
 Dim x, y As Byte

' Crea la superficie grafica per il file "PostScript" e il percorso ove esso sarà generato (il 2° e il 3° argomento sono espressi in pixel):
 surface = cairo_svg_surface_create("/tmp/esempio_Cairo.svg", 100.0, 100.0)
 If surface == 0 Then Error.Raise("Errore !")
  
 cairo = cairo_create(surface)
 If cairo == 0 Then Error.Raise("Errore !")
 
' Disegna i quadrati sullo sfondo:
 For x = 0 To 9
   For y = 0 To 9
     cairo_rectangle(cairo, x * 10.0, y * 10.0, 5, 5)
   Next
 Next
    
 pattern = cairo_pattern_create_radial(50, 50, 5, 50, 50, 50)
 If pattern == 0 Then Error.Raise("Errore !")

 cairo_pattern_add_color_stop_rgb(pattern, 0, 0.75, 0.15, 0.99)

 cairo_pattern_add_color_stop_rgb(pattern, 0.9, 1, 1, 1)
   
 cairo_set_source(cairo, pattern)

 cairo_fill(cairo)

' Scrive in primo piano.

' Imposta la dimensione del font:
 cairo_set_font_size(cairo, 15)
' Imposta il font del testo:
 cairo_select_font_face(cairo, "Ubuntu", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD)
' Attribuisce un colore al testo:
 cairo_set_source_rgb(cairo, 0, 0, 0)
' Imposta le coordinate ove sarà disegnato il testo:
 cairo_move_to(cairo, 5, 25)
' Imposta il testo:
 cairo_show_text(cairo, "Crea un SVG")

 cairo_set_source_rgb(cairo, 255, 0, 0)
 cairo_move_to(cairo, 0, 75)
 cairo_show_text(cairo, "Testo qualsiasi")

' Per generare il file SVG, è necessario distruggere il contesto grafico "cairo_t" e la superficie dell'immagine:
 cairo_destroy(cairo)
 cairo_surface_destroy(surface)

End


Riferimenti