Creare file PNG 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 PNG.

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

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.


In quest'altro codice creeremo un file immagine di formato PNG:

Library "libcairo:2.11600.0"

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
Private Enum CAIRO_FORMAT_INVALID = -1, CAIRO_FORMAT_ARGB32, CAIRO_FORMAT_RGB24, CAIRO_FORMAT_A8,
             CAIRO_FORMAT_A1, CAIRO_FORMAT_RGB16_565, CAIRO_FORMAT_RGB30

' cairo_surface_t * cairo_image_surface_create(cairo_format_t formatI, int width, int height)
' Creates an image surface of the specified format and dimensions.
Private Extern cairo_image_surface_create(formatI As Integer, width As Integer, height As Integer) As Pointer
 
' cairo_status_t cairo_surface_write_to_png(cairo_surface_t *surface, const char *filename)
' Writes the contents of surface to a new file filename as a PNG image.
Private Extern cairo_surface_write_to_png(cairo_surface As Pointer, filename As String) As Integer

' 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 cairo, surface, pattern As Pointer
 Dim x, y, err As Byte

' Crea la superficie dell'immagine (in questo caso trasparente):
 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 100, 100)
 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:
 cairo_set_font_size(cairo, 15)

 cairo_select_font_face(cairo, "URW Palladio L", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD)

 cairo_set_source_rgb(cairo, 0, 0, 0)

 cairo_move_to(cairo, 10, 25)

 cairo_show_text(cairo, "Crea")

 cairo_move_to(cairo, 10, 75)

 cairo_show_text(cairo, "file PNG")

 err = cairo_surface_write_to_png(surface, "/tmp/esempio_Cairo.png")
 If err > 0 Then Error.Raise("Errore nella creazione del file immagine .png !")

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

End


Riferimenti