Page 1 of 1

OSX and Cairo libs questions

Posted: Wed Feb 13, 2008 9:14 pm
by adamredwoods
Some questions:

1. Does the Mac OSX _demo_ version of PureBasic allow OpenLibrary commands and interfacing to dynlibs?

2. I am experimenting with Cairo to draw vector images. Code from here:
http://www.purebasic.fr/english/viewtop ... 4efc84a70c

On this example (which works fine on Windows Demo),I see that StartDrawing() returns a DC handle, which then Cairo uses internally to create the surface.

What does OSX return for StartDrawing()? Is it a Quartz context?

3. Then, how would the Cairo library use this context? Or could I grab the memory address using an internal Quartz function, and pass that to create the Cairo surface?


I realize I'm using the PB Demo version, but was wondering how to overcome this question before final purchase.

Many Thanks.

Posted: Sat Feb 16, 2008 8:56 pm
by adamredwoods
Ok, perhaps I have discovered that perhaps OpenLibrary command does not work with Mac universal binary libraries.

Posted: Mon Feb 18, 2008 7:40 am
by adamredwoods
So, my first attempt at the dylib failed, because I took the binary build from Inkscape. Since that dylib was Universal Binary, PureBasic couldn't open it.

I built Cairo using the MacPorts Sudo command, with the options +atsui +quartz and (I don't know if this made a difference) +no_x11.
I compiled on the second try (after a clean command inbetween tries).

So I have a PPC library, unknown if its functional, but am still trying to get the Quartz context to the Cairo surface somehow.

Posted: Mon Feb 18, 2008 9:39 pm
by adamredwoods
I have been unable to get Cairo to work with Mac OSX.
Here is my code:

Code: Select all

XIncludeFile "cairo-include.pb"

;;dll.s = "libcairo2.dylib"
dll.s = "libcairo.2.11.7.dylib"
If InitCairo(dll) = 0
    MessageRequester("Error", "Cannot open '" + dll + "'")
    End
EndIf

Global hDC.l
Global surface.l

        
Procedure Recreate(avar.l)
    ;;a routine to draw something into a Cairo surface
    
    Static oldsizex.l = - 1, oldsizey = - 1
    
    sizex.l = WindowWidth(0)
    sizey.l = WindowHeight(0)
    If sizex > 0 And sizey > 0
        
        ;hDC = StartDrawing(WindowOutput(0)) 

        hDC = StartDrawing(ImageOutput(1) )
        ;hDC = ImageOutput(1)

        If hDC
          ;surface = cairo_image_surface_create_for_data(*hBitmap, 0, 800, 800, 1 )
          ;;surface = cairo_win32_surface_create(hDC );; works on Win32

          surface = cairo_quartz_surface_create_for_cg_context(hDC, 800, 800)
          ;Debug(hDC)
        EndIf
        
        
        If surface <> 0
            ;Debug(surface)
            cr = cairo_create(surface )
            If cr <> 0
                scalex.d = sizex / 256
                cairo_scale(cr, sizex / 256, sizey / 256)
                pat = cairo_pattern_create_linear(0.0, 0.0, 0.0, 256.0)
                cairo_pattern_add_color_stop_rgba(pat, 1, 0, 0, 0, 1)
                cairo_pattern_add_color_stop_rgba(pat, 0, 1, 1, 1, 1)
                cairo_rectangle(cr, 0, 0, 256, 256)
                cairo_set_source(cr, pat)
                cairo_fill(cr)
                cairo_pattern_destroy(pat)
                
                pat = cairo_pattern_create_radial(115.2, 102.4, 25.6, 102.4, 102.4, 128.0)
                cairo_pattern_add_color_stop_rgba(pat, 0, 1, 1, 1, 1)
                cairo_pattern_add_color_stop_rgba(pat, 1, 0, 0, 0, 1)
                cairo_set_source(cr, pat)
                cairo_arc(cr, avar, 128.0, 76.8, 0, 2 * 3.141)
                cairo_fill(cr)
                cairo_pattern_destroy(pat)
                
                cairo_destroy(cr)
            EndIf
            
            cairo_surface_destroy(surface) 
        EndIf
        
        StopDrawing()
        
        oldsizex = sizex
        oldsizey = sizey
    EndIf
EndProcedure

Global OldMessageProc.l


;; INIT________________
;;InitSprite()
OpenWindow(0, 0, 0, 400, 400, "Cairo example", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)

;;OpenWindowedScreen(WindowID(0),0,0,800,800,0,0,0)

CreateGadgetList(WindowID(0))
;;CreateSprite(1, 800,800)

CreateImage(1, 800, 800 )

;LoadImage(1, "PureBasicLogo.bmp") ;;to test
ImageGadget(2, 0,0, 800, 800, ImageID(1) )

Repeat
  n = n+15
  If n>600 : n=1: EndIf
  
  Recreate(n)
  
  ;;DisplaySprite(1, 0, 0)
  
  j = WindowEvent()
  While j <> 0
  
    If j = #PB_Event_SizeWindow
      ;
    EndIf
    If j = #PB_Event_CloseWindow : Break :EndIf
    j = WindowEvent()
  Wend
  
  SetGadgetState(2, ImageID(1) ) 
  ;;FlipBuffers()
  
Until j = #PB_Event_CloseWindow
End

Here is the include Lib, again, this is taken from the reference link above, but I have added the MacOS routines.

Code: Select all

Structure cairo_text_extents
    x_bearing.d
    y_bearing.d
    width.d
    height.d
    x_advance.d
    y_advance.d
EndStructure

Enumeration 0
    #CAIRO_FONT_SLANT_NORMAL
    #CAIRO_FONT_SLANT_ITALIC
    #CAIRO_FONT_SLANT_OBLIQUE
EndEnumeration

Enumeration 0
    #CAIRO_FONT_WEIGHT_NORMAL
    #CAIRO_FONT_WEIGHT_BOLD
EndEnumeration

PrototypeC.l cairo_set_source_rgb(cr.l, a.d, b.d, c.d)
PrototypeC.l cairo_set_source_rgba(cr.l, a.d, b.d, c.d, d.d)
PrototypeC.l cairo_image_surface_create(cr.l, a.d, b.d)
PrototypeC.l cairo_image_surface_create_for_data(hDC.l, cr.l, a.d, b.d, stride.l)
PrototypeC.l cairo_win32_surface_create(hDC.l)
PrototypeC.l cairo_create(surface.l)
PrototypeC.l cairo_destroy(cr.l)
PrototypeC.l cairo_status(cr.l)
PrototypeC.l cairo_move_to(cr.l, a.d, b.d)
PrototypeC.l cairo_line_to(cr.l, a.d, b.d)
PrototypeC.l cairo_paint(cr.l)
PrototypeC.l cairo_paint_with_alpha(cr.l, a.d)
PrototypeC.l cairo_stroke(cr.l)
PrototypeC.l cairo_rectangle(cr.l, a.d, b.d, c.d, d.d)
PrototypeC.l cairo_svg_surface_create(a.s, b.d, c.d)
PrototypeC.l cairo_translate(cr.l, a.d, b.d)
PrototypeC.l cairo_rotate(cr.l, a.d)
PrototypeC.l cairo_scale(cr.l, a.d, b.d)
PrototypeC.l cairo_surface_finish(cr.l)
PrototypeC.l cairo_surface_destroy(cr.l)
PrototypeC.l cairo_win32_font_face_create_for_hfont(a.l)
PrototypeC.l cairo_set_font_face(a.l, b.l)
PrototypeC.l cairo_set_font_size(a.l, b.d)
PrototypeC.l cairo_show_text(a.l, b.p-utf8)
PrototypeC.l cairo_select_font_face(a.l, b.s, c.l, d.l)
PrototypeC.l cairo_text_extentsproto(a.l, b.p-utf8, c.l)
PrototypeC.l cairo_set_antialias(a.l, b.l)
PrototypeC.l cairo_fill(a.l)
PrototypeC.l cairo_set_fill_rule(a.l, b.l)
PrototypeC.l cairo_pattern_create_linear(a.d, b.d, c.d, d.d)
PrototypeC.l cairo_pattern_add_color_stop_rgba(pat.l, a.d, b.d, c.d, d.d, e.d)
PrototypeC.l cairo_set_source(cr.l, pat.l)
PrototypeC.l cairo_pattern_destroy(pat.l)
PrototypeC.l cairo_pattern_create_radial(a.d, b.d, c.d, d.d, e.d, f.d)
PrototypeC.l cairo_arc(a.l, b.d, c.d, d.d, e.d, f.d)

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  ;;set quartz routines
  PrototypeC.l cairo_quartz_surface_create_for_cg_context(a.l, b.d, c.d)
  PrototypeC.l cairo_quartz_surface_create(a.l, b.d, c.d)
  PrototypeC.l cairo_atsui_font_face_create_for_atsu_font_id(fontID.l)
CompilerEndIf          

Global cairolibno.l

Procedure.l InitCairo(dll.s)
    cairolibno.l = OpenLibrary(#PB_Any, dll)
    
    If cairolibno <> 0
        Global cairo_set_source_rgb.cairo_set_source_rgb = GetFunction(cairolibno, "cairo_set_source_rgb")
        Global cairo_set_source_rgba.cairo_set_source_rgba = GetFunction(cairolibno, "cairo_set_source_rgba")
        Global cairo_image_surface_create.cairo_image_surface_create = GetFunction(cairolibno, "cairo_image_surface_create" )
        Global cairo_image_surface_create_for_data.cairo_image_surface_create_for_data = GetFunction(cairolibno, "cairo_image_surface_create_for_data" )
        Global cairo_win32_surface_create.cairo_win32_surface_create = GetFunction(cairolibno, "cairo_win32_surface_create")
        Global cairo_create.cairo_create = GetFunction(cairolibno, "cairo_create")
        Global cairo_destroy.cairo_destroy = GetFunction(cairolibno, "cairo_destroy")
        Global cairo_status.cairo_status = GetFunction(cairolibno, "cairo_status")
        Global cairo_move_to.cairo_move_to = GetFunction(cairolibno, "cairo_move_to")
        Global cairo_line_to.cairo_line_to = GetFunction(cairolibno, "cairo_line_to")
        Global cairo_paint.cairo_paint = GetFunction(cairolibno, "cairo_paint")
        Global cairo_paint_with_alpha.cairo_paint_with_alpha = GetFunction(cairolibno, "cairo_paint_with_alpha")
        Global cairo_stroke.cairo_stroke = GetFunction(cairolibno, "cairo_stroke")
        Global cairo_rectangle.cairo_rectangle = GetFunction(cairolibno, "cairo_rectangle")
        Global cairo_svg_surface_create.cairo_svg_surface_create = GetFunction(cairolibno, "cairo_svg_surface_create")
        Global cairo_translate.cairo_translate = GetFunction(cairolibno, "cairo_translate")
        Global cairo_rotate.cairo_rotate = GetFunction(cairolibno, "cairo_rotate")
        Global cairo_scale.cairo_scale = GetFunction(cairolibno, "cairo_scale")
        Global cairo_surface_finish.cairo_surface_finish = GetFunction(cairolibno, "cairo_surface_finish")
        Global cairo_surface_destroy.cairo_surface_destroy = GetFunction(cairolibno, "cairo_surface_destroy")
        Global cairo_win32_font_face_create_for_hfont.cairo_win32_font_face_create_for_hfont = GetFunction(cairolibno, "cairo_win32_font_face_create_for_hfont")
        Global cairo_set_font_face.cairo_set_font_face = GetFunction(cairolibno, "cairo_set_font_face")
        Global cairo_set_font_size.cairo_set_font_size = GetFunction(cairolibno, "cairo_set_font_size")
        Global cairo_show_text.cairo_show_text = GetFunction(cairolibno, "cairo_show_text")
        Global cairo_select_font_face.cairo_select_font_face = GetFunction(cairolibno, "cairo_select_font_face")
        Global cairo_text_extents.cairo_text_extentsproto = GetFunction(cairolibno, "cairo_text_extents")
        Global cairo_set_antialias.cairo_set_antialias = GetFunction(cairolibno, "cairo_set_antialias")
        Global cairo_fill.cairo_fill = GetFunction(cairolibno, "cairo_fill")
        Global cairo_set_fill_rule.cairo_set_fill_rule = GetFunction(cairolibno, "cairo_set_fill_rule")
        Global cairo_pattern_create_linear.cairo_pattern_create_linear = GetFunction(cairolibno, "cairo_pattern_create_linear")
        Global cairo_pattern_add_color_stop_rgba.cairo_pattern_add_color_stop_rgba = GetFunction(cairolibno, "cairo_pattern_add_color_stop_rgba")
        Global cairo_set_source.cairo_set_source = GetFunction(cairolibno, "cairo_set_source")
        Global cairo_pattern_destroy.cairo_pattern_destroy = GetFunction(cairolibno, "cairo_pattern_destroy")
        Global cairo_pattern_create_radial.cairo_pattern_create_radial = GetFunction(cairolibno, "cairo_pattern_create_radial")
        Global cairo_arc.cairo_arc = GetFunction(cairolibno, "cairo_arc")
        CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
          ;;set quartz routines
          Global cairo_quartz_surface_create_for_cg_context.cairo_quartz_surface_create_for_cg_context = GetFunction(cairolibno, "cairo_quartz_surface_create_for_cg_context")
          Global cairo_quartz_surface_create .cairo_quartz_surface_create  = GetFunction(cairolibno, "cairo_quartz_surface_create")
          Global cairo_atsui_font_face_create_for_atsu_font_id.cairo_atsui_font_face_create_for_atsu_font_id =GetFunction(cairolibno, "cairo_atsui_font_face_create_for_atsu_font_id")
          
        CompilerEndIf
    EndIf
    
    ProcedureReturn cairolibno
EndProcedure
Cairo has a barely documented command that creates a surface from a Quartz context CGContextRef. This may be the key, but does PureBasic return any CG contexts? You can see the above code how I am trying to extract the information.

I am at a loss, but have contacted Fred to see what else can be done. It could be the compiled library, it could be anything. I will attempt to test my library in C/C++, which will take a week.