viachpaliy / cairo-gobject

Cairo bindings for applying with crystal-gobject
MIT License
7 stars 1 forks source link

Error In lib/cairo/lib_cairo.cr:26:14, undefined constant LibCairo::Status #3

Closed gummybears closed 3 years ago

gummybears commented 4 years ago

Linux Mint 18.2, 64 bits

Followed the instructions

$ sudo apt-get install libgirepository1.0-dev libgtk-3-dev libcairo-gobject2 gir1.2-freedesktop
Reading package lists... Done
Building dependency tree       
Reading state information... Done
gir1.2-freedesktop is already the newest version (1.46.0-3ubuntu1).
libcairo-gobject2 is already the newest version (1.14.6-1).
libgirepository1.0-dev is already the newest version (1.46.0-3ubuntu1).
libgtk-3-dev is already the newest version (3.18.9-1ubuntu3.3).

shards.yml

name: test
version: 0.0.1

dependencies:
  gobject:
    github: jhass/crystal-gobject
    version: ~> 0.8.0

  cairo-gobject:
    github: viachpaliy/cairo-gobject
cd cairo-gobject
  shards install
  crystal run samples/gtk_sample.cr

I got the following error

Showing last frame. Use --error-trace for full trace.

In lib/cairo/lib_cairo.cr:26:14

 26 | status : LibCairo::Status
               ^---------------
Error: undefined constant LibCairo::Status
viachpaliy commented 4 years ago

I use Xubuntu 20.04 with gir1.2-freedesktop(1.64.02). "crystal-gobject" uses cairo-1.0.typelib from gir1.2-freedesktop package for generating cairo bindings. Enum LibCairo::Status is created in compile time (for my version OS and gir1.2-freedesktop). You may add next code in lib/cairo/lib_cairo.cr


 enum Status : UInt32
    ZERO_NONE = 0
    SUCCESS = 0
    NO_MEMORY = 1
    INVALID_RESTORE = 2
    INVALID_POP_GROUP = 3
    NO_CURRENT_POINT = 4
    INVALID_MATRIX = 5
    INVALID_STATUS = 6
    NULL_POINTER = 7
    INVALID_STRING = 8
    INVALID_PATH_DATA = 9
    READ_ERROR = 10
    WRITE_ERROR = 11
    SURFACE_FINISHED = 12
    SURFACE_TYPE_MISMATCH = 13
    PATTERN_TYPE_MISMATCH = 14
    INVALID_CONTENT = 15
    INVALID_FORMAT = 16
    INVALID_VISUAL = 17
    FILE_NOT_FOUND = 18
    INVALID_DASH = 19
    INVALID_DSC_COMMENT = 20
    INVALID_INDEX = 21
    CLIP_NOT_REPRESENTABLE = 22
    TEMP_FILE_ERROR = 23
    INVALID_STRIDE = 24
    FONT_TYPE_MISMATCH = 25
    USER_FONT_IMMUTABLE = 26
    USER_FONT_ERROR = 27
    NEGATIVE_COUNT = 28
    INVALID_CLUSTERS = 29
    INVALID_SLANT = 30
    INVALID_WEIGHT = 31
    INVALID_SIZE = 32
    USER_FONT_NOT_IMPLEMENTED = 33
    DEVICE_TYPE_MISMATCH = 34
    DEVICE_ERROR = 35
    INVALID_MESH_CONSTRUCTION = 36
    DEVICE_FINISHED = 37
    JBIG2_GLOBAL_MISSING = 38
  end
gummybears commented 4 years ago

Added the code you suggested but I am now getting the following error.

$ crystal run samples/basic_shapes.cr 
Showing last frame. Use --error-trace for full trace.

In src/lib_cairo.cr:73:12

 73 | type : LibCairo::PathDataType
             ^---------------------
Error: undefined constant LibCairo::PathDataType
viachpaliy commented 4 years ago

It is yet one compile-time enum

  enum PathDataType : UInt32
    ZERO_NONE = 0
    MOVE_TO = 0
    LINE_TO = 1
    CURVE_TO = 2
    CLOSE_PATH = 3
  end
gummybears commented 4 years ago

Allright, added this enum as well, but still getting errors. Is there not something I can do to generate this code ?

Showing last frame. Use --error-trace for full trace.

In src/lib_cairo.cr:165:62

 165 | fun pop_group = cairo_pop_group(cr : LibCairo::Context*) : LibCairo::PatternType
viachpaliy commented 4 years ago

cairo-1.0.typelib from gir1.2-freedesktop(1.46.0) don't have few enums (:

enum PatternType : UInt32
    ZERO_NONE = 0
    SOLID = 0
    SURFACE = 1
    LINEAR = 2
    RADIAL = 3
    MESH = 4
    RASTER_SOURCE = 5
  end
gummybears commented 4 years ago

Ok, understood, new error

In src/lib_cairo.cr:182:10

 182 | op : LibCairo::Operator
            ^-----------------
Error: undefined constant LibCairo::Operator
viachpaliy commented 4 years ago
enum Operator : UInt32
    ZERO_NONE = 0
    CLEAR = 0
    SOURCE = 1
    OVER = 2
    IN = 3
    OUT = 4
    ATOP = 5
    DEST = 6
    DEST_OVER = 7
    DEST_IN = 8
    DEST_OUT = 9
    DEST_ATOP = 10
    XOR = 11
    ADD = 12
    SATURATE = 13
    MULTIPLY = 14
    SCREEN = 15
    OVERLAY = 16
    DARKEN = 17
    LIGHTEN = 18
    COLOR_DODGE = 19
    COLOR_BURN = 20
    HARD_LIGHT = 21
    SOFT_LIGHT = 22
    DIFFERENCE = 23
    EXCLUSION = 24
    HSL_HUE = 25
    HSL_SATURATION = 26
    HSL_COLOR = 27
    HSL_LUMINOSITY = 28
  end
gummybears commented 4 years ago

I found this page, https://www.cairographics.org/documentation/cairomm/reference/namespaceCairo.html#feff18f1043713479dd6ceb4aa4d4f02 and could possibly add the missing enum's myself ? What do you think ?

So I got a new error

In src/lib_cairo.cr:252:17

 252 | antialias : LibCairo::Antialias
                   ^------------------

but copied/pasted and got this

 enum  Antialias
    DEFAULT = 0
    NONE = 1
    GRAY = 2
    SUBPIXEL = 3
  end
viachpaliy commented 4 years ago

I don't know which enum is omitted in cairo-1.0.typelib. You can add missing enum.

  ###########################################
  ##    Enums
  ###########################################

  enum Status : UInt32
    ZERO_NONE = 0
    SUCCESS = 0
    NO_MEMORY = 1
    INVALID_RESTORE = 2
    INVALID_POP_GROUP = 3
    NO_CURRENT_POINT = 4
    INVALID_MATRIX = 5
    INVALID_STATUS = 6
    NULL_POINTER = 7
    INVALID_STRING = 8
    INVALID_PATH_DATA = 9
    READ_ERROR = 10
    WRITE_ERROR = 11
    SURFACE_FINISHED = 12
    SURFACE_TYPE_MISMATCH = 13
    PATTERN_TYPE_MISMATCH = 14
    INVALID_CONTENT = 15
    INVALID_FORMAT = 16
    INVALID_VISUAL = 17
    FILE_NOT_FOUND = 18
    INVALID_DASH = 19
    INVALID_DSC_COMMENT = 20
    INVALID_INDEX = 21
    CLIP_NOT_REPRESENTABLE = 22
    TEMP_FILE_ERROR = 23
    INVALID_STRIDE = 24
    FONT_TYPE_MISMATCH = 25
    USER_FONT_IMMUTABLE = 26
    USER_FONT_ERROR = 27
    NEGATIVE_COUNT = 28
    INVALID_CLUSTERS = 29
    INVALID_SLANT = 30
    INVALID_WEIGHT = 31
    INVALID_SIZE = 32
    USER_FONT_NOT_IMPLEMENTED = 33
    DEVICE_TYPE_MISMATCH = 34
    DEVICE_ERROR = 35
    INVALID_MESH_CONSTRUCTION = 36
    DEVICE_FINISHED = 37
    JBIG2_GLOBAL_MISSING = 38
  end

  enum Content : UInt32
    ZERO_NONE = 0
    COLOR = 4096
    ALPHA = 8192
    COLOR_ALPHA = 12288
  end

  enum Operator : UInt32
    ZERO_NONE = 0
    CLEAR = 0
    SOURCE = 1
    OVER = 2
    IN = 3
    OUT = 4
    ATOP = 5
    DEST = 6
    DEST_OVER = 7
    DEST_IN = 8
    DEST_OUT = 9
    DEST_ATOP = 10
    XOR = 11
    ADD = 12
    SATURATE = 13
    MULTIPLY = 14
    SCREEN = 15
    OVERLAY = 16
    DARKEN = 17
    LIGHTEN = 18
    COLOR_DODGE = 19
    COLOR_BURN = 20
    HARD_LIGHT = 21
    SOFT_LIGHT = 22
    DIFFERENCE = 23
    EXCLUSION = 24
    HSL_HUE = 25
    HSL_SATURATION = 26
    HSL_COLOR = 27
    HSL_LUMINOSITY = 28
  end

  enum Antialias : UInt32
    ZERO_NONE = 0
    DEFAULT = 0
    NONE = 1
    GRAY = 2
    SUBPIXEL = 3
    FAST = 4
    GOOD = 5
    BEST = 6
  end

  enum FillRule : UInt32
    ZERO_NONE = 0
    WINDING = 0
    EVEN_ODD = 1
  end

  enum LineCap : UInt32
    ZERO_NONE = 0
    BUTT = 0
    ROUND = 1
    SQUARE = 2
  end

  enum LineJoin : UInt32
    ZERO_NONE = 0
    MITER = 0
    ROUND = 1
    BEVEL = 2
  end

  enum TextClusterFlags : UInt32
    ZERO_NONE = 0
    BACKWARD = 1
  end

  enum FontSlant : UInt32
    ZERO_NONE = 0
    NORMAL = 0
    ITALIC = 1
    OBLIQUE = 2
  end

  enum FontWeight : UInt32
    ZERO_NONE = 0
    NORMAL = 0
    BOLD = 1
  end

  enum SubpixelOrder : UInt32
    ZERO_NONE = 0
    DEFAULT = 0
    RGB = 1
    BGR = 2
    VRGB = 3
    VBGR = 4
  end

  enum HintStyle : UInt32
    ZERO_NONE = 0
    DEFAULT = 0
    NONE = 1
    SLIGHT = 2
    MEDIUM = 3
    FULL = 4
  end

  enum HintMetrics : UInt32
    ZERO_NONE = 0
    DEFAULT = 0
    OFF = 1
    ON = 2
  end

  enum FontType : UInt32
    ZERO_NONE = 0
    TOY = 0
    FT = 1
    WIN32 = 2
    QUARTZ = 3
    USER = 4
  end

  enum PathDataType : UInt32
    ZERO_NONE = 0
    MOVE_TO = 0
    LINE_TO = 1
    CURVE_TO = 2
    CLOSE_PATH = 3
  end

  enum DeviceType : Int32
    ZERO_NONE = 0
    DRM = 0
    GL = 1
    SCRIPT = 2
    XCB = 3
    XLIB = 4
    XML = 5
    COGL = 6
    WIN32 = 7
    INVALID = -1
  end

  enum SurfaceType : UInt32
    ZERO_NONE = 0
    IMAGE = 0
    PDF = 1
    PS = 2
    XLIB = 3
    XCB = 4
    GLITZ = 5
    QUARTZ = 6
    WIN32 = 7
    BEOS = 8
    DIRECTFB = 9
    SVG = 10
    OS2 = 11
    WIN32_PRINTING = 12
    QUARTZ_IMAGE = 13
    SCRIPT = 14
    QT = 15
    RECORDING = 16
    VG = 17
    GL = 18
    DRM = 19
    TEE = 20
    XML = 21
    SKIA = 22
    SUBSURFACE = 23
    COGL = 24
  end

  enum Format : Int32
    ZERO_NONE = 0
    INVALID = -1
    ARGB32 = 0
    RGB24 = 1
    A8 = 2
    A1 = 3
    RGB16_565 = 4
    RGB30 = 5
  end

  enum PatternType : UInt32
    ZERO_NONE = 0
    SOLID = 0
    SURFACE = 1
    LINEAR = 2
    RADIAL = 3
    MESH = 4
    RASTER_SOURCE = 5
  end

  enum Extend : UInt32
    ZERO_NONE = 0
    NONE = 0
    REPEAT = 1
    REFLECT = 2
    PAD = 3
  end

  enum Filter : UInt32
    ZERO_NONE = 0
    FAST = 0
    GOOD = 1
    BEST = 2
    NEAREST = 3
    BILINEAR = 4
    GAUSSIAN = 5
  end

  enum RegionOverlap : UInt32
    ZERO_NONE = 0
    IN = 0
    OUT = 1
    PART = 2
  end
gummybears commented 4 years ago

Ok, included the enum's but now a structure definition is missing

In src/lib_cairo.cr:1448:16

 1448 | device : LibCairo::Device*
                 ^---------------
Error: undefined constant LibCairo::Device
viachpaliy commented 4 years ago
 struct Device # struct
    _data : UInt8[0]
  end
gummybears commented 4 years ago

So finally I got a sample working Screenshot from 2020-07-20 12-37-44

Here are the changes I made, added the struct Device, but there is still a small error in src/status_exception.cr:3:21

Showing last frame. Use --error-trace for full trace.

In src/status_exception.cr:3:21

 3 | getter status : Status

I changed the code to read

module Cairo
  class StatusException < Exception
    getter status : LibCairo::Status # <<<<<<<<<<<<<<<< changed >>>>>>>>>>>>>>> 

    def initialize(@status : Status, message : String? = nil, cause : Exception? = nil)
      super(message, cause)
    end
  end
end

Now I ran

$ crystal run samples/basic_shapes.cr

and got the following warning

In samples/basic_shapes.cr:60:7

 60 | case event.event_type
      ^
Warning: case is not exhaustive for enum Gdk::EventType.

Missing members:
 - NOTHING
 - DESTROY
 - EXPOSE
 - MOTION_NOTIFY
 - BUTTON_PRESS
 - DOUBLE_BUTTON_PRESS
 - TRIPLE_BUTTON_PRESS
 - BUTTON_RELEASE
 - KEY_PRESS
 - KEY_RELEASE
 - ENTER_NOTIFY
 - LEAVE_NOTIFY
 - FOCUS_CHANGE
 - CONFIGURE
 - MAP
 - UNMAP
 - PROPERTY_NOTIFY
 - SELECTION_CLEAR
 - SELECTION_REQUEST
 - SELECTION_NOTIFY
 - PROXIMITY_IN
 - PROXIMITY_OUT
 - DRAG_ENTER
 - DRAG_LEAVE
 - DRAG_MOTION
 - DRAG_STATUS
 - DROP_START
 - DROP_FINISHED
 - CLIENT_EVENT
 - SCROLL
 - WINDOW_STATE
 - SETTING
 - OWNER_CHANGE
 - GRAB_BROKEN
 - DAMAGE
 - TOUCH_BEGIN
 - TOUCH_UPDATE
 - TOUCH_END
 - TOUCH_CANCEL
 - TOUCHPAD_SWIPE
 - TOUCHPAD_PINCH
 - EVENT_LAST

A total of 1 warnings were found.

How can we make sure all these changes are permanent ?

gummybears commented 4 years ago

Now I am trying to run $ crystal run samples/cairo_in_gdk_window.cr

Some typos in your code

In samples/cairo_in_gdk_window.cr:32:38

 32 | context.select_font_face("Sans", Cairo::FontSlant::NORMAL , Cairo::FontWeight::NORMAL)

This line should read

 context.select_font_face("Sans", LibCairo::FontSlant::NORMAL , LibCairo::FontWeight::NORMAL)

Another typo in src/context.cr:784:51

 784 | def select_font_face(family : String, slant : FontSlant, weight : FontWeight)

This line should read

 def select_font_face(family : String, slant : LibCairo::FontSlant, weight : LibCairo::FontWeight)

After making the changes, got the sample to run successful Screenshot from 2020-07-20 12-47-56

gummybears commented 4 years ago

Same typo in samples/cairo_in_gtk_window.cr

$ crystal run samples/cairo_in_gtk_window.cr 
Showing last frame. Use --error-trace for full trace.

In samples/cairo_in_gtk_window.cr:23:38

 23 | context.select_font_face("Sans", Cairo::FontSlant::NORMAL , Cairo::FontWeight::NORMAL)

This line should read

 context.select_font_face("Sans", LibCairo::FontSlant::NORMAL , LibCairo::FontWeight::NORMAL)

After making the change, got the sample to run successful (no screenshot uploaded)

viachpaliy commented 4 years ago

add in lib/cairo/cairo.cr next code:

  module Cairo
       alias FontSlant = LibCairo::FontSlant
       alias FontWeight = LibCairo::FontWeight
  end

You can add alias for all enums

gummybears commented 4 years ago

I did a search on the term "Cairo::" and changed it to "LibCairo::" in the sample code I ran all the samples successful which is great but all these changes should ideally be made in your code ?

viachpaliy commented 4 years ago

For Ubuntu19.04 and Ubuntu 20.04 "crystal-gobject" create main code for bindings , which contain all this omitted enums and struct. This shard only extend compile-time bindings. If I make this changes, error raises for this OS. I want make a patch - file which contain all omitted code and this file must be use with another OS.

gummybears commented 4 years ago

I cloned your repository and made the changes, Linux Mint 18.2, 64 bits, version Gtk 3.18.9. The repository can be found at https://github.com/gummybears/cairo-gobject Thanks for all your help.

viachpaliy commented 4 years ago

I add a new file - patch_for_ubuntu1604.cr. Please,check if it work. You need uncomment line 3 in lib/cairo.cr

  require "./patch_for_ubuntu1604.cr"