Creare file PNG con le funzioni esterne del API di Cairo e di Pango
Da Gambas-it.org - Wikipedia.
Versione del 27 ago 2016 alle 16:56 di Vuott (Discussione | contributi) (Creata pagina con "In questa pagina mostreremo un semplice esempio, nel quale si genererà un file immagine di tipo PNG mediante le risorse del API di Cairo e di Pango. E' necessario avere inst...")
In questa pagina mostreremo un semplice esempio, nel quale si genererà un file immagine di tipo PNG mediante le risorse del API di Cairo e di Pango.
E' necessario avere installate nel sistema e richiamare nel progetto Gambas tre librerie dinamiche condivise:
- "libcairo.so.2.11301.0"
- "libpango-1.0.so.0.3600.3"
- "libpangocairo-1.0.so.0.3600.3"
Il seguente codice creerà un'immagine con sfondo colorato contenente del testo:
Library "libcairo:2.11301.0" Private Const CAIRO_FORMAT_ARGB32 As Integer = 0 ' 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_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_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_paint (cairo_t *cr) ' A drawing operator that paints the current source everywhere within the current clip region. Private Extern cairo_paint(cr As Pointer) ' void cairo_move_to (cairo_t *cr, double x, double y) ' Begin a new sub-path. After this call the current point will be (x , y ). Private Extern cairo_move_to(cr As Pointer, x As Float, y As Float) ' 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 ' 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) Library "libpango-1.0:0.3600.3" Private Enum PANGO_GRAVITY_SOUTH = 0, PANGO_GRAVITY_EAST, PANGO_GRAVITY_NORTH, PANGO_GRAVITY_WEST, PANGO_GRAVITY_AUTO Private Enum PANGO_GRAVITY_HINT_NATURAL = 0, PANGO_GRAVITY_HINT_STRONG, PANGO_GRAVITY_HINT_LINE Private Enum PANGO_WEIGHT_THIN = 0, PANGO_WEIGHT_ULTRALIGHT, PANGO_WEIGHT_LIGHT, PANGO_WEIGHT_SEMILIGHT, PANGO_WEIGHT_BOOK, PANGO_WEIGHT_NORMAL, PANGO_WEIGHT_MEDIUM, PANGO_WEIGHT_SEMIBOLD, PANGO_WEIGHT_BOLD, PANGO_WEIGHT_ULTRABOLD, PANGO_WEIGHT_HEAVY, PANGO_WEIGHT_ULTRAHEAVY Private Const PANGO_SCALE As Integer = 1024 ' PangoFontDescription * pango_font_description_new (void) ' Creates a new font description structure with all fields unset. Private Extern pango_font_description_new() As Pointer ' void pango_font_description_set_family (PangoFontDescription *desc, const char *family) ' Sets the family name field of a font description. Private Extern pango_font_description_set_family(desc As Pointer, family As String) ' void pango_font_description_set_weight (PangoFontDescription *desc, PangoWeight weight) ' Sets the weight field of a font description. Private Extern pango_font_description_set_weight(desc As Pointer, weight As Integer) ' void pango_font_description_set_absolute_size (PangoFontDescription *desc, double size) ' Sets the size field of a font description, in device units. Private Extern pango_font_description_set_absolute_size(desc As Pointer, weight As Float) ' PangoContext * pango_layout_get_context (PangoLayout *layout) ' Retrieves the PangoContext used for this layout. Private Extern pango_layout_get_context(layout As Pointer) As Pointer ' void pango_context_set_base_gravity (PangoContext *context, PangoGravity gravity) ' Sets the base gravity for the context. Private Extern pango_context_set_base_gravity(context As Pointer, gravity As Integer) ' void pango_context_set_gravity_hint (PangoContext *context, PangoGravityHint hint) ' Sets the gravity hint for the context. Private Extern pango_context_set_gravity_hint(context As Pointer, hint As Integer) ' void pango_layout_set_font_description (PangoLayout *layout, const PangoFontDescription *desc) ' Sets the default font description for the layout. Private Extern pango_layout_set_font_description(layout As Pointer, desc As Pointer) ' void pango_layout_set_text (PangoLayout *layout, const char *text, int length) ' Sets the text of the layout. Private Extern pango_layout_set_text(layout As Pointer, text As String, length As Integer) ' void pango_layout_context_changed (PangoLayout *layout) ' Forces recomputation of any state in the PangoLayout that might depend on the layout's context. Private Extern pango_layout_context_changed(layout As Pointer) ' void g_object_unref (gpointer object) ' Decreases the reference count of object. Private Extern g_object_unref(gobject As Pointer) Library "libpangocairo-1.0:0.3600.3" ' PangoLayout * pango_cairo_create_layout (cairo_t *cr) ' Creates a layout object set up to match the current transformation and target surface of the Cairo context. Private Extern pango_cairo_create_layout(cr As Pointer) As Pointer ' void pango_cairo_update_layout (cairo_t *cr, PangoLayout *layout) ' Updates the private PangoContext of a PangoLayout created with pango_cairo_create_layout(). Private Extern pango_cairo_update_layout(cr As Pointer, layout As Pointer) ' void pango_cairo_show_layout (cairo_t *cr, PangoLayout *layout) ' Draws a PangoLayout in the specified cairo context. Private Extern pango_cairo_show_layout(cr As Pointer, layout As Pointer) Public Sub Main() Dim surface, cairo, cntx, lay As Pointer Dim font_description As Pointer Dim err As Integer ' Crea la superficie dell'immagine: surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 620, 480) cairo = cairo_create(surface) font_description = pango_font_description_new() pango_font_description_set_family(font_description, "serif") pango_font_description_set_weight(font_description, PANGO_WEIGHT_BOLD) pango_font_description_set_absolute_size(font_description, 50 * PANGO_SCALE) lay = pango_cairo_create_layout(cairo) pango_layout_set_font_description(lay, font_description) pango_layout_set_text(lay, "testo qualsiasi", -1) cntx = pango_layout_get_context(lay) ' Ruota il testo in questo caso sul suo asse orizzontale: pango_context_set_base_gravity(cntx, PANGO_GRAVITY_NORTH) pango_context_set_gravity_hint(cntx, PANGO_GRAVITY_HINT_STRONG) pango_layout_context_changed(lay) ' Colora lo sfondo della superficie del disegno: cairo_set_source_rgb(cairo, 1.0, 1.0, 0.0) cairo_paint(cairo) ' Colora i caratteri del testo testo: cairo_set_source_rgb(cairo, 1.0, 0.0, 0.0) cairo_move_to(cairo, 100.0, 200.0) pango_cairo_update_layout(cairo, lay) pango_cairo_show_layout(cairo, lay) err = cairo_surface_write_to_png(surface, "/percorso/del/file.png") If err > 0 Then Error.Raise("Errore nella creazione del file immagine .png !") ' Va in chiusura: g_object_unref(lay) g_object_unref(cntx) cairo_destroy(cairo) cairo_surface_destroy(surface) End