Cairo Clock Example

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Cairo Clock Example

Post by jj2007 »

caseih wrote:the .dll.a files are stubs that help the linker link against the .dll
So the real thing is probably libcairo-2.dll
The Cisco version found above named cairo.dll exports about three times as many functions as libcairo-2.dll - it's messy as usual.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Cairo Clock Example

Post by Roland Chastain »

jj2007 wrote:Is the .dll.a extension exactly the same as *.dll?
It would be nice to read the answers given to you. ;)
Roland Chastain wrote:To build the examples, you need libcairo.dll.a.

To run the examples, you will need some *.dll.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Cairo Clock Example

Post by lizard »

Oh, now i have overwritten a previous post by accident...

What i want to post:

Code: Select all

...
/usr/lib/i386-linux-gnu/libcairo.a
/usr/lib/i386-linux-gnu/libcairo.so
Yes, and for Linux they are libcairo.a to compile and libcairo.so to run. But because they are installed automatically deep in the filesystem with the mentioned apt-get install, the user could give his source code to others and forgets that it depends on files he has not even seen before...
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Cairo Clock Example

Post by jj2007 »

Roland Chastain wrote:It would be nice to read the answers given to you. ;)
Roland Chastain wrote:To build the examples, you need libcairo.dll.a.

To run the examples, you will need some *.dll.
I do read your answers ("Maybe you have a software installed which came with these files"), and replied with a long post. To make you happy: The FB exe finds and uses C:\Program Files (x86)\FreeArc\bin\libcairo-2.dll

Which probably means that FreeArc registers its DLL, and thus makes it available for any other program. Btw if I rename the FreeArc DLL, your example will not work unless the user performs some steps that are not yet explained in the mini tutorial here. But yeah, it's just BASIC, isn't it? ;-)
paul doe
Moderator
Posts: 1732
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Cairo Clock Example

Post by paul doe »

jj2007 wrote:...Is the .dll.a extension exactly the same as *.dll?
A clarification will help you understand the difference:

The .dll file contains the executable code. It's the library per-se. The .dll.a file is an import library, needed to bind the library statically (at compile time). The difference, in FreeBasic, looks like this:

Static binding (compile time):

Code: Select all

declare function foo lib "bar" alias "foo"() as whatever
This will bind the library to your executable at compile time. It uses both the .dll and the .dll.a files. Once compiled, the .dll.a file is no longer necessary.

Dynamic binding (run time):

Code: Select all

var library = dyLibLoad( "bar" )
var foo = dyLibSymbol( library, "foo" )
This will load a library and load the symbol at run time. It uses only the .dll file.

I hope this helps to clarify matters a little.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Cairo Clock Example

Post by Roland Chastain »

jj2007 wrote:To make you happy: The FB exe finds and uses C:\Program Files (x86)\FreeArc\bin\libcairo-2.dll

Which probably means that FreeArc registers its DLL, and thus makes it available for any other program.
Yes, it is available for any program, because the folder is in the PATH environment variable. To check it, start a command prompt and type "echo %PATH%".
jj2007 wrote:Btw if I rename the FreeArc DLL, your example will not work unless the user performs some steps that are not yet explained in the mini tutorial here. But yeah, it's just BASIC, isn't it? ;-)
It will certainly work, since all the files are included in the package and are in the same folder than the source code...
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Cairo Clock Example

Post by jj2007 »

paul doe wrote:The .dll file contains the executable code. It's the library per-se. The .dll.a file is an import library, needed to bind the library statically (at compile time).
Thanks for the explanation. In Masm (and afaik also in C), the import libraries have a .lib ending. The .a seems to be a Linux thing.
Roland Chastain wrote:the folder is in the PATH environment variable.
You are right, it's there. Haven't checked for a while, it contains now 26 paths, i.e. 26 folders to search if an exe doesn't find a DLL in its folder. Programs that use the path variable should be banned imho, or flagged as malware by the AVs.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Cairo Clock Example

Post by lizard »

Another interesting from Rolands package is this:

Code: Select all

' StandByCairo.bas

' an example for cairo library
' (C) 2011 by dodicat and Thomas[ dot ]Freiherr[ at ]gmx{ dot }net
' License GPLv 3
'
' See for details
' http://www.freebasic.net/forum/viewtopic.php?p=163599&highlight=#163599

#INCLUDE ONCE "cairo/cairo.bi"

CONST Pag_W = 700, Pag_H = 700
CONST M_PI = 4 * ATN(1) '                                             Pi

TYPE arc_seg_data
  AS cairo_t PTR c_t
  AS DOUBLE xc, yc, ri, ra, a1, a2, fg, fb
END TYPE

' draw a colored circle segment / farbiges Kreissegment zeichnen
SUB arc_seg(BYVAL seg AS arc_seg_data PTR)
  VAR pa = NEW cairo_path_t
  WITH *seg
    cairo_arc_negative(.c_t, .xc, .yc, .ri, .a2, .a1)
    cairo_arc(.c_t, .xc, .yc, .ra, .a1, .a2)
    cairo_close_path(.c_t)
    pa = cairo_copy_path(.c_t)
    cairo_set_source_rgba(.c_t, 1, .fg, .fb, 0.9)
    cairo_fill(.c_t)
    cairo_append_path(.c_t, pa)
    cairo_set_source_rgb(.c_t, 0.0, 0.0, 0.0)
    cairo_stroke(.c_t)
  END WITH
  cairo_path_destroy(pa)
END SUB

' draw / zeichnen
SUB DoDrawing(BYVAL C AS cairo_surface_t PTR, BYVAL W AS DOUBLE)
  VAR seg = NEW arc_seg_data, t = "Cairo grafics example"
  WITH *seg
    .c_t = cairo_create(C)
    cairo_set_source_rgb(.c_t, 1.0, 1.0, 1.0) '         white background
    cairo_paint(.c_t) '                                        fill page
    cairo_set_line_width(.c_t, 0.5)
    VAR f = Pag_W / 4.3
    .xc = f
    .yc = f
    FOR z AS INTEGER = 0 TO 1 '                        two center points
      .ri = 0.1 * f
      FOR j AS INTEGER = 1 TO 5 STEP 1 '                     five radius
        .ra = .ri + 0.35 * f / j
        FOR i AS INTEGER = 0 TO 5 STEP 2 '                three segments
          .a1 = (1 + (2 * (z = 1))) * W + 60.0 * M_PI / 180 * i
          .a2 = .a1 + 60.0 * M_PI / 180
          .fb = 60.0 * M_PI / 180 * i / M_PI / 2
          .fg = .ra / f
          arc_seg(seg)
        NEXT
        .ri = .ra
      NEXT
      cairo_set_font_size (.c_t, 0.2 * f)
      DIM AS cairo_font_extents_t fe '                         font data
      cairo_font_extents (.c_t, @fe)

      DIM AS cairo_text_extents_t te '                         text size
      cairo_text_extents (.c_t, t, @te)
      cairo_move_to (.c_t, _ '                 lower left corner of text
                     Pag_W / 2 - (te.width / 2 + te.x_bearing), _
                     Pag_H / 2 + (te.height / 2) - fe.descent)
      cairo_show_text(.c_t, t)
      cairo_stroke(.c_t)

      .yc = Pag_H - f
      .xc = Pag_W - f
    NEXT
    cairo_show_page(.c_t)
    cairo_destroy(.c_t)
  END WITH
  cairo_surface_flush(C)
END SUB

' main1 / Hauptprogramm1
VAR S_W = CUINT(Pag_W) + 1, S_H = CUINT(Pag_H) + 1
SCREENRES S_W, S_H, 32
VAR c_s_t = cairo_image_surface_create_for_data(_
              SCREENPTR, CAIRO_FORMAT_ARGB32, _
              S_W, S_H, S_W * 4)
VAR wi = 0.0
DO
  SCREENLOCK
  DoDrawing(c_s_t, wi)
  SCREENUNLOCK
  wi += 1.73 * M_PI / 180
  WHILE wi > M_PI * 2 : wi -= M_PI * 2 : WEND
  SLEEP 35
LOOP UNTIL LEN(INKEY)
cairo_surface_destroy(c_s_t)
'END 0

sleep
It shows the cairo_circles demo moving.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Cairo Clock Example

Post by lizard »

Another one from my workshop, based on the work of Roland:

Code: Select all

' cairo_colortable.bas

' FreeBASIC Cairo example by lizard
' 256 colors with names
' based on the work of Roland Chastain
' source : wingraph.pas by Stefan Berinde
' http://math.ubbcluj.ro/~sberinde/wingraph/main.html#colornames

#include once "cairo/cairo.bi"

#define SetSourceColor(cairo, colour) cairo_set_source_rgb(cairo, _ 
(colour and &hFF0000) / &hFF0000, (colour and &h00FF00) / &h00FF00, (colour and &h0000FF) / &h0000FF)

dim as string names(255) = {"AliceBlue", "AlizarinCrimson","Amber","Amethyst","AntiqueWhite", _
"Aquamarine","Asparagus","Azure", "Beige", "Bisque", "Bistre", "BitterLemon", "Black", _
"BlanchedAlmond", "BlazeOrange", "Blue", "BlueViolet", "BondiBlue", "Brass", "BrightGreen", _
"BrightTurquoise", "BrightViolet", "Bronze", "Brown", "Buff", "Burgundy", "BurlyWood", _
"BurntOrange", "BurntSienna", "BurntUmber", "CadetBlue", "CamouflageGreen", "Cardinal", _
"Carmine", "Carrot", "Casper", "Celadon", "Cerise", "Cerulean", "CeruleanBlue", "Chartreuse", _
"Chocolate", "Cinnamon", "Cobalt", "Copper", "Coral", "Corn", "CornflowerBlue", "Cornsilk", _
"Cream", "Crimson", "Cyan", "DarkBlue", "DarkBrown", "DarkCerulean", "DarkChestnut", "DarkCoral", _
"DarkCyan", "DarkGoldenrod", "DarkGray", "DarkGreen", "DarkIndigo", "DarkKhaki", "DarkMagenta", _
"DarkOlive", "DarkOliveGreen", "DarkOrange", "DarkOrchid", "DarkPastelGreen", "DarkPink", "DarkRed", _
"DarkSalmon", "DarkScarlet", "DarkSeaGreen", "DarkSlateBlue", "DarkSlateGray", "DarkSpringGreen", _
"DarkTan", "DarkTangerine", "DarkTeaGreen", "DarkTerraCotta", "DarkTurquoise", "DarkViolet", _
"DeepPink", "DeepSkyBlue", "Denim", "DimGray", "DodgerBlue", "Emerald", "Eggplant", "FernGreen", _
"FireBrick", "Flax", "FloralWhite", "ForestGreen", "Fractal", "Fuchsia", "Gainsboro", "Gamboge", _
"GhostWhite", "Gold", "Goldenrod", "Gray", "GrayAsparagus", "GrayTeaGreen", "Green", "GreenYellow", _
"Heliotrope", "Honeydew", "HotPink", "IndianRed", "Indigo", "InternationalKleinBlue", _
"InternationalOrange", "Ivory", "Jade", "Khaki", "Lavender", "LavenderBlush", "LawnGreen", "Lemon", _
"LemonChiffon", "LightBlue", "LightBrown", "LightCoral", "LightCyan", "LightGoldenrodYellow", _
"LightGray", "LightGreen", "LightMagenta", "LightPink", "LightRed", "LightSalmon", "LightSeaGreen", _
"LightSkyBlue", "LightSlateGray", "LightSteelBlue", "LightYellow", "Lilac", "Lime", "LimeGreen", _
"Linen", "Magenta", "Malachite", "Maroon", "Mauve", "MediumAquamarine", "MediumBlue", "MediumOrchid", _
"MediumPurple", "MediumSeaGreen", "MediumSlateBlue", "MediumSpringGreen", "MediumTurquoise", _
"MediumVioletRed", "MidnightBlue", "MintCream", "MistyRose", "Moccasin", "MoneyGreen", "Monza", _
"MossGreen", "MountbattenPink", "Mustard", "NavajoWhite", "Navy", "Ochre", "OldGold", "OldLace", _
"Olive", "OliveDrab", "Orange", "OrangeRed", "Orchid", "PaleBrown", "PaleCarmine", "PaleChestnut", _
"PaleCornflowerBlue", "PaleGoldenrod", "PaleGreen", "PaleMagenta", "PaleMauve", "PalePink", _
"PaleSandyBrown", "PaleTurquoise", "PaleVioletRed", "PapayaWhip", "PastelGreen", "PastelPink", _
"Peach", "PeachOrange", "PeachPuff", "PeachYellow", "Pear", "Periwinkle", "PersianBlue", "Peru", _
"PineGreen", "Pink", "PinkOrange", "Plum", "PowderBlue", "PrussianBlue", "Puce", "Pumpkin", "Purple", _
"RawUmber", "Red", "Reef", "RobinEggBlue", "RosyBrown", "RoyalBlue", "Russet", "Rust", "SaddleBrown", _
"Saffron", "Salmon", "SandyBrown", "Sangria", "Sapphire", "Scarlet", "SchoolBusYellow", "SeaGreen", _
"SeaShell", "SelectiveYellow", "Sepia", "Sienna", "Silver", "SkyBlue", "SlateBlue", "SlateGray", _
"Snow", "SpringGreen", "SteelBlue", "SwampGreen", "Taupe", "Tangerine", "Teal", "TeaGreen", "Tenne", _
"TerraCotta", "Thistle", "Tomato", "Turquoise", "Ultramarine", "Vermilion", "Violet", "VioletEggplant", _
"Viridian", "Wheat", "White", "WhiteSmoke", "Wisteria", "Yellow", "YellowGreen", "Zinnwaldite"}

dim as long colors(255) =    {&HF0F8FF, &HE32636, &HFFBF00, &H9966CC, &HFAEBD7, &H7FFFD4, _
&H7BA05B, &HF0FFFF, &HF5F5DC, &HFFE4C4, &H3D2B1F, &HCAE00D, &H000000, &HFFEBCD, &HFF9900, _
&H0000FF, &H8A2BE2, &H0095B6, &HB5A642, &H66FF00, &H08E8DE, &HCD00CD, &HCD7F32, &HA52A2A, _
&HF0DC82, &H900020, &HDEB887, &HCC5500, &HE97451, &H8A3324, &H5F9EA0, &H78866B, &HC41E3A, _
&H960018, &HED9121, &HADBED1, &HACE1AF, &HDE3163, &H007BA7, &H2A52BE, &H7FFF00, &HD2691E, _
&H7B3F00, &H0047AB, &HB87333, &HFF7F50, &HFBEC5D, &H6495ED, &HFFF8DC, &HFFFDD0, &HDC143C, _
&H00FFFF, &H00008B, &H654321, &H08457E, &H986960, &HCD5B45, &H008B8B, &HB8860B, &H545454, _
&H006400, &H310062, &HBDB76B, &H8B008B, &H556832, &H556B2F, &HFF8C00, &H9932CC, &H03C03C, _
&HE75480, &H8B0000, &HE9967A, &H560319, &H8FBC8F, &H483D8B, &H2F4F4F, &H177245, &H918151, _
&HFFA812, &HBADBAD, &HCC4E5C, &H00CED1, &H9400D3, &HFF1493, &H00BFFF, &H1560BD, &H696969, _
&H1E90FF, &H50C878, &H990066, &H4F7942, &HB22222, &HEEDC82, &HFFFAF0, &H228B22, &H808080, _
&HF400A1, &HDCDCDC, &HE49B0F, &HF8F8FF, &HFFD700, &HDAA520, &H7E7E7E, &H465945, &HCADABA, _
&H008000, &HADFF2F, &HDF73FF, &HF0FFF0, &HFF69B4, &HCD5C5C, &H4B0082, &H002FA7, &HFF4F00, _
&HFFFFF0, &H00A86B, &HF0E68C, &HE6E6FA, &HFFF0F5, &H7CFC00, &HFDE910, &HFFFACD, &HADD8E6, _
&HD2B48C, &HF08080, &HE0FFFF, &HFAFAD2, &HA8A8A8, &H90EE90, &HFF80FF, &HFFB6C1, &HFF8080, _
&HFFA07A, &H20B2AA, &H87CEFA, &H778899, &HB0C4DE, &HFFFFE0, &HC8A2C8, &H00FF00, &H32CD32, _
&HFAF0E6, &HFF00FF, &H0BDA51, &H800000, &HE0B0FF, &H66CDAA, &H0000CD, &HBA55D3, &H9370DB, _
&H3CB371, &H7B68EE, &H00FA9A, &H48D1CC, &HC71585, &H191970, &HF5FFFA, &HFFE4E1, &HFFE4B5, _
&HC0DCC0, &HC7031E, &HADDFAD, &H997A8D, &HFFDB58, &HFFDEAD, &H000080, &HCC7722, &HCFB53B, _
&HFDF5E6, &H808000, &H6B8E23, &HFFA500, &HFF4500, &HDA70D6, &H987654, &HAF4035, &HDDADAF, _
&HABCDEF, &HEEE8AA, &H98FB98, &HF984E5, &H996666, &HFADADD, &HDABDAB, &HAFEEEE, &HDB7093, _
&HFFEFD5, &H77DD77, &HFFD1DC, &HFFE5B4, &HFFCC99, &HFFDAB9, &HFADFAD, &HD1E231, &HCCCCFF, _
&H6600FF, &HCD853F, &H01796F, &HFFC0CB, &HFF9966, &HDDA0DD, &HB0E0E6, &H003153, &HCC8899, _
&HFF7518, &H800080, &H734A12, &HFF0000, &HC9FFA2, &H00CCCC, &HBC8F8F, &H4169E1, &H80461B, _
&HB7410E, &H8B4513, &HF4C430, &HFA8072, &HF4A460, &H92000A, &H082567, &HFF2400, &HFFD800, _
&H2E8B57, &HFFF5EE, &HFFBA00, &H704214, &HA0522D, &HC0C0C0, &H87CEEB, &H6A5ACD, &H708090, _
&HFFFAFA, &H00FF7F, &H4682B4, &HACB78E, &HBC987E, &HFFCC00, &H008080, &HD0F0C0, &HCD5700, _
&HE2725B, &HD8BFD8, &HFF6347, &H40E0D0, &H120A8F, &HFF4D00, &HEE82EE, &H991199, &H40826D, _
&HF5DEB3, &HFFFFFF, &HF5F5F5, &HC9A0DC, &HFFFF00, &H9ACD32, &HEBC2AF}

const screen_w = 1200
const screen_h = 716

screenres screen_w, screen_h, 32
windowtitle "Colortable"

' create a cairo drawing context, using the FB screen as surface 
Dim As cairo_surface_t Ptr surface = cairo_image_surface_create_for_data(ScreenPtr(), _
       CAIRO_FORMAT_ARGB32, screen_w, screen_h, screen_w * 4)
      
Dim As cairo_t Ptr canvas = cairo_create(surface)
dim as short i

screenlock

for x as short = 0 to 7
  for y as short = 0 to 31
    SetSourceColor(canvas,colors(i))
    cairo_rectangle (canvas, x * 150 + 10, y * 22 + 10, 28, 12)
    cairo_fill(canvas)
    cairo_set_source_rgba(canvas, 1, 1, 1, 1) ' white
    cairo_move_to(canvas, x * 150 + 44, y * 22 + 20)
    cairo_show_text(canvas,names(i))
    i += 1
  next y
next x

screenunlock

' clean up the cairo context
cairo_destroy(canvas)

sleep
I must confess, the usage of such a table is limited. It may serve as reference. But with the source code it is possible to find another use for the tables.

Edit: Just seen, i have forgotten the screenlock/screenunlock here.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Cairo Clock Example

Post by lizard »

Cairo looks complicated in the beginning. But then you recognize, you can do a lot of things with it. Like a .png as pattern for a fillstyle. For this example you need to insert your own .png name.

Code: Select all

' cairo_imagepattern.bas 

' Translated from the C example written by Øyvind Kolås
' Public domain from here
' https://www.cairographics.org/samples/

#include once "cairo/cairo.bi"

CONST M_PI = 4 * ATN(1)

Const SCREEN_W = 256
Const SCREEN_H = 256

ScreenRes SCREEN_W, SCREEN_H, 32

' Create a cairo drawing context, using the FB screen as surface.
Dim As cairo_surface_t Ptr surface = cairo_image_surface_create_for_data(ScreenPtr(), _
      CAIRO_FORMAT_ARGB32, SCREEN_W, SCREEN_H, SCREEN_W * 4 )

Dim As cairo_t Ptr cr = cairo_create(surface)

ScreenLock

 ' draw the entire context white
cairo_set_source_rgba(cr, 1, 1, 1, 1)
cairo_paint(cr)

' black
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0)
dim as long w, h
dim as cairo_surface_t ptr image

dim as cairo_pattern_t ptr pattern
dim as cairo_matrix_t matrix

image = cairo_image_surface_create_from_png ("myown.png") ' <---- insert your own
w = cairo_image_surface_get_width (image)
h = cairo_image_surface_get_height (image)

pattern = cairo_pattern_create_for_surface (image)
cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT)

cairo_translate (cr, 128.0, 128.0)
cairo_rotate (cr, M_PI / 4)
cairo_scale (cr, 1 / sqr (2), 1 / sqr (2))
cairo_translate (cr, -128.0, -128.0)

cairo_matrix_init_scale (@matrix, w/256.0 * 5.0, h/256.0 * 5.0)
cairo_pattern_set_matrix (pattern, @matrix)

cairo_set_source (cr, pattern)

cairo_rectangle (cr, 0, 0, 256.0, 256.0)
cairo_fill (cr)

cairo_pattern_destroy (pattern)
cairo_surface_destroy (image)

ScreenUnlock

' Clean up the cairo context
cairo_destroy(cr)

sleep
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Cairo Clock Example

Post by lizard »

A nice feature is having text outlines. :-)

Code: Select all

' cairo_textoutline.bas 

' Translated from the C example written by Øyvind Kolås
' Public domain from here
' https://www.cairographics.org/samples/

#include once "cairo/cairo.bi"

Const SCREEN_W = 300
Const SCREEN_H = 250

CONST M_PI = 4 * ATN(1)

ScreenRes SCREEN_W, SCREEN_H, 32

' Create a cairo drawing context, using the FB screen as surface.
Dim As cairo_surface_t Ptr surface = cairo_image_surface_create_for_data(ScreenPtr(), _
      CAIRO_FORMAT_ARGB32, SCREEN_W, SCREEN_H, SCREEN_W * 4 )

Dim As cairo_t Ptr cr = cairo_create(surface)

ScreenLock

' draw the entire context white
cairo_set_source_rgba(cr, 1, 1, 1, 1)
cairo_paint(cr)

' black
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0)
cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD)
cairo_set_font_size (cr, 90.0)

cairo_move_to (cr, 10.0, 135.0)
cairo_show_text (cr, "Hello")

cairo_move_to (cr, 70.0, 165.0)
cairo_text_path (cr, "void")
cairo_set_source_rgb (cr, 0.5, 0.5, 1)
cairo_fill_preserve (cr)
cairo_set_source_rgb (cr, 0, 0, 0)
cairo_set_line_width (cr, 2.56)
cairo_stroke (cr)

' draw helping lines
cairo_set_source_rgba (cr, 1, 0.2, 0.2, 0.6)
cairo_arc (cr, 10.0, 135.0, 5.12, 0, 2 * M_PI)
cairo_arc (cr, 70.0, 165.0, 5.12, 0, 2 * M_PI)
cairo_fill (cr)

ScreenUnlock

' Clean up the cairo context
cairo_destroy(cr)

sleep
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Cairo Clock Example

Post by lizard »

"Toy fonts" is named not because i played around some time to find out, but because they are a bit limited. They are not fully described in docs and with google i found not much. Seems they are builtin, because i have no fonts with these names installed. So far i found five fonts each with three properties, working on both Windows 10 and Mint 18.3.

Because i havent deinstalled fonts from standard installation, these should work. However, the safest choice are sans, times and courier.

Code: Select all

' cairo_toy_fonts.bas 

' for FreeBASIC 1.05 by Lizard
' these are builtin "toy fonts" from Cairo "CAIRO_FONT_TYPE_TOY"
' "CAIRO_FONT_SLANT_OBLIQUE" had no other effect on them
' https://www.cairographics.org/manual/cairo-cairo-font-face-t.html#cairo-font-face-t

' these five fonts are tested on Windows 10 and Mint 18.3

#include once "cairo/cairo.bi"

Const SCREEN_W = 300
Const SCREEN_H = 550

ScreenRes SCREEN_W, SCREEN_H, 32
windowtitle " Cairo Toy Fonts"

' Create a cairo drawing context, using the FB screen as surface.
Dim As cairo_surface_t Ptr surface = cairo_image_surface_create_for_data(ScreenPtr(), _
      CAIRO_FORMAT_ARGB32, SCREEN_W, SCREEN_H, SCREEN_W * 4 )

Dim As cairo_t Ptr cr = cairo_create(surface)

ScreenLock

 ' draw the entire context white
cairo_set_source_rgba(cr, 1, 1, 1, 1)
cairo_paint(cr)

' text color black, size 20
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0)
cairo_set_font_size (cr, 20.0)


cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
cairo_move_to (cr, 50, 50)
cairo_show_text (cr, "Sans normal")

cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT_NORMAL)
cairo_move_to (cr, 50, 70)
cairo_show_text (cr, "Sans italic")

cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD)
cairo_move_to (cr, 50, 90)
cairo_show_text (cr, "Sans bold")


cairo_select_font_face (cr, "courier", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
cairo_move_to (cr,50, 150)
cairo_show_text (cr, "Courier normal")

cairo_select_font_face (cr, "courier", CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT_NORMAL)
cairo_move_to (cr, 50, 170)
cairo_show_text (cr, "Courier italic")

cairo_select_font_face (cr, "courier", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD)
cairo_move_to (cr, 50, 190)
cairo_show_text (cr, "Courier bold")


cairo_select_font_face (cr, "calibri", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
cairo_move_to (cr, 50, 250)
cairo_show_text (cr, "Calibri normal")

cairo_select_font_face (cr, "calibri", CAIRO_FONT_SLANT_ITALiC, CAIRO_FONT_WEIGHT_NORMAL)
cairo_move_to (cr, 50, 270)
cairo_show_text (cr, "Calibri italic")

cairo_select_font_face (cr, "calibri", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD)
cairo_move_to (cr, 50, 290)
cairo_show_text (cr, "Calibri bold")


cairo_select_font_face (cr, "times", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
cairo_move_to (cr, 50, 350)
cairo_show_text (cr, "Times normal")

cairo_select_font_face (cr, "times", CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT_NORMAL)
cairo_move_to (cr,50, 370)
cairo_show_text (cr, "Times italic")

cairo_select_font_face (cr, "times", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD)
cairo_move_to (cr, 50, 390)
cairo_show_text (cr, "Times bold")


cairo_select_font_face (cr, "georgia", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
cairo_move_to (cr, 50, 450)
cairo_show_text (cr, "Georgia normal")

cairo_select_font_face (cr, "georgia", CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT_NORMAL)
cairo_move_to (cr, 50, 470)
cairo_show_text (cr, "Georgia italic")

cairo_select_font_face (cr, "georgia", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD)
cairo_move_to (cr, 50, 490)
cairo_show_text (cr, "Georgia bold")

ScreenUnlock

' Clean up the cairo context
cairo_destroy(cr)

Sleep
The best find is Georgia, looks good. For more fonts one should use additional libs like freetype, pango etc. But the toy fonts will be enough for most purposes.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Cairo Clock Example

Post by lizard »

The situation is confusing. Normally with cairos, "select_font_face" a font family is selected, and the OS chooses which concrete font to take. But sometimes it works with single font names like "purisa", too. So i got a list of font families from wiki and tested which works on Windows 10 and Linux Mint 18.3.

Code: Select all

' cairo_font_families.bas 

' for FreeBASIC 1.05 by Lizard
' Note: fonts that are working in current OS must do it not necessarily in others
' https://www.cairographics.org/manual/cairo-cairo-font-face-t.html#cairo-font-face-t

#include once "cairo/cairo.bi"

Const SCREEN_W = 700
Const SCREEN_H = 700

dim as string family(...) = {"Antiqua", "Architect", "Arial", _
"BankFuturistic", "BankGothic", "Blackletter", "Blagovest", "Calibri", "Comic Sans MS", _
"Courier", "Cursive", "Decorative", "Fantasy", "Fraktur", "Frosty", "Garamond", _
"Georgia", "Helvetica", "Impact", "Minion", "Modern", "Monospace", "Open Sans", _
"Palatino", "Roman", "Sans-serif", "Serif", "Script", "Swiss", "Times", "Times New Roman", _
"Tw Cen MT", "Verdana"}

ScreenRes SCREEN_W, SCREEN_H, 32
windowtitle " Cairo Font Families"

' Create a cairo drawing context, using the FB screen as surface.
Dim As cairo_surface_t Ptr surface = cairo_image_surface_create_for_data(ScreenPtr(), _
      CAIRO_FORMAT_ARGB32, SCREEN_W, SCREEN_H, SCREEN_W * 4 )

Dim As cairo_t Ptr cr = cairo_create(surface)

ScreenLock

	 ' draw the entire context white
	cairo_set_source_rgba(cr, 1, 1, 1, 1)
	cairo_paint(cr)
	
	' text color black, size 20
	cairo_set_source_rgb(cr, 0.0, 0.0, 0.0)
	cairo_set_font_size (cr, 14.0)
	
	for i as short = 0 to ubound(family)
	cairo_select_font_face (cr, family(i), CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
	cairo_move_to (cr, 40, 20 + i * 20)
	cairo_show_text (cr, family(i))
	
	cairo_move_to (cr, 190, 20 +i * 20)
	cairo_show_text (cr, "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz")

next i


ScreenUnlock

' Clean up the cairo context
cairo_destroy(cr)

Sleep
Would be interesting to read Wilfredos comment.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Cairo Clock Example

Post by lizard »

Wilfredo, this one will kick you out of the socks!

Code: Select all

' cairo_text_gradient.bas 

' Translated after the C example from here
' http://zetcode.com/gfx/cairo/cairotext/

#include once "cairo/cairo.bi"

Const SCREEN_W = 350
Const SCREEN_H = 110

ScreenRes SCREEN_W, SCREEN_H, 32

' Create a cairo drawing context, using the FB screen as surface.
Dim As cairo_surface_t Ptr surface = cairo_image_surface_create_for_data(ScreenPtr(), _
      CAIRO_FORMAT_ARGB32, SCREEN_W, SCREEN_H, SCREEN_W * 4 )

Dim As cairo_t Ptr cr = cairo_create(surface)
dim as cairo_pattern_t ptr pat
dim as long h = 100
  
ScreenLock

 ' draw the entire context green
  cairo_set_source_rgb(cr, 0.2, 0.8, 0.5)
  cairo_paint(cr)

  cairo_select_font_face(cr, "Georgia", CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT_BOLD)
  cairo_set_font_size(cr, h)
  
  pat = cairo_pattern_create_linear(0, 15, 0, h*0.8)
  cairo_pattern_set_extend(pat, CAIRO_EXTEND_REPEAT)
  cairo_pattern_add_color_stop_rgb(pat, 0.0, 1, 0.9, 0)
  cairo_pattern_add_color_stop_rgb(pat, 0.5, 1, 0.1, 0)
                  
  cairo_move_to(cr, 20, 90)
  cairo_text_path(cr, "Cairo")
  cairo_set_source(cr, pat)
  cairo_fill(cr)

ScreenUnlock

' Clean up the cairo context
cairo_destroy(cr)

Sleep
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Cairo Clock Example

Post by Roland Chastain »

@lizard

Thank you for sharing all these examples. One observation: I believe you should also free the surface, by calling cairo_surface_destroy().
Post Reply