<commit_before>------------------------------------------------------------------------------
--
--  LGI Cairo override module.
--
--  Copyright (c) 2012 <NAME>
--  Licensed under the MIT license:
--  http://www.opensource.org/licenses/mit-license.php
--
------------------------------------------------------------------------------

local assert, pairs, ipairs, setmetatable, table, rawget, type
   = assert, pairs, ipairs, setmetatable, table, rawget, type
local lgi = require 'lgi'
local cairo = lgi.cairo

local core = require 'lgi.core'
local ffi = require 'lgi.ffi'
local component = require 'lgi.component'
local record = require 'lgi.record'
local enum = require 'lgi.enum'
local ti = ffi.types

cairo._module = core.module('cairo', 2)
local module_gobject = core.gi.cairo.resolve

-- Versioning support.
function cairo.version_encode(major, minor, micro)
   return 10000 * major + 100 * minor + micro
end
cairo.version = core.callable.new {
   addr = cairo._module.cairo_version, ret = ti.int } ()
cairo.version_string = core.callable.new {
   addr = cairo._module.cairo_version_string, ret = ti.utf8 } ()

-- Load some constants.
cairo._constant = {
   MIME_TYPE_JP2 = 'image/jp2',
   MIME_TYPE_JPEG = 'image/jpeg',
   MIME_TYPE_PNG = 'image/png',
   MIME_TYPE_URI = 'text/x-uri',
}

-- Load definitions of all enums.
cairo._enum = cairo._enum or {}
for _, name in pairs {
   'Status', 'Content', 'Operator', 'Antialias', 'FillRule', 'LineCap',
   'LineJoin', 'TextClusterFlags', 'FontSlant', 'FontWeight', 'SubpixelOrder',
   'HintStyle', 'HintMetrics', 'FontType', 'PathDataType', 'DeviceType',
   'SurfaceType', 'Format', 'PatternType', 'Extend', 'Filter', 'RegionOverlap',
   'PdfVersion', 'PsLevel', 'SvgVersion',
} do
   local lower = name:gsub('([%l%d])([%u])', '%1_%2'):lower()
   local gtype = ffi.load_gtype(
      module_gobject, 'cairo_gobject_' .. lower .. '_get_type')
   if gtype then
      cairo._enum[name] = ffi.load_enum(gtype, 'cairo.' .. name)
   else
      cairo._enum[name] = component.create(nil, enum.enum_mt, 'cairo.' .. name)
   end
end

-- Load definitions of all boxed records.
cairo._struct = cairo._struct or {}
for index, struct in pairs {
   'Context', 'Device', 'Surface', 'Rectangle', 'ScaledFont', 'FontFace',
   'FontOptions', 'Region', 'RectangleInt', 'Path', 'TextExtents',
   'FontExtents', 'Matrix', 'ImageSurface', 'PdfSurface', 'PsSurface',
   'RecordingSurface', 'SvgSurface', 'ToyFontFace', 'RectangleList',
   'SolidPattern', 'SurfacePattern', 'GradientPattern', 'LinearPattern',
   'RadialPattern',
   MeshPattern = cairo.version_encode(1, 12, 0),
} do
   local since = 0
   if type(index) == 'string' then
      since = struct
      struct = index
   end
   if cairo.version >= since then
      local lower = struct:gsub('([%l%d])([%u])', '%1_%2'):lower()
      local gtype = ffi.load_gtype(
	 module_gobject, 'cairo_gobject_' .. lower .. '_get_type')
      local obj = component.create(gtype, record.struct_mt, 'cairo.' .. struct)
      cairo._struct[struct] = obj
   end
end

local path_data_header = component.create(nil, record.struct_mt)
ffi.load_fields(path_data_header, { { 'type', cairo.PathDataType },
				    { 'length', ti.int } })
local path_data_point = component.create(nil, record.struct_mt)
ffi.load_fields(path_data_point, { { 'x', ti.double }, { 'y', ti.double } })
cairo._union = rawget(cairo, '_union') or {}
cairo._union.PathData = component.create(nil, record.union_mt, 'cairo.PathData')

-- Populate methods into records.
for _, info in ipairs {
   {  'Status',
      methods = {
	 to_string = { static = true, ret = ti.utf8, cairo.Status }
      },
   },

   {  'Rectangle',
      fields = {
	 { 'x', ti.double }, { 'y', ti.double },
	 { 'width', ti.double }, { 'height', ti.double },
      },
   },

   {  'RectangleList',
      fields = {
	 { 'status', cairo.Status },
	 { 'rectangles', cairo.Rectangle, ptr = true },
	 { 'num_rectangles', ti.int },
      },
   },

   {  'RectangleInt',
      fields = {
	 { 'x', ti.int }, { 'y', ti.int },
	 { 'width', ti.int }, { 'height', ti.int },
      },
   },

   {  'Matrix',
      fields = {
	 { 'xx', ti.double }, { 'yx', ti.double },
	 { 'xy', ti.double }, { 'yy', ti.double },
	 { 'x0', ti.double }, { 'y0', ti.double },
      },
      methods = {
	 init = { ti.double, ti.double, ti.double,
		  ti.double, ti.double, ti.double },
	 init_identity = {},
	 init_translate = { ti.double, ti.double },
	 init_scale = { ti.double, ti.double },
	 init_rotate = { ti.double },
	 translate = { ti.double, ti.double },
	 scale = { ti.double, ti.double },
	 rotate = { ti.double },
	 invert = { ret = cairo.Status },
	 multiply = { cairo.Matrix, cairo.Matrix },
	 transform_distance = { { ti.double, dir = 'inout' },
				{ ti.double, dir = 'inout' } },
	 transform_point = { { ti.double, dir = 'inout' },
			     { ti.double, dir = 'inout' } },
      },
   },

   {  'PathData',
      fields = {
	 { 'header', path_data_header },
	 { 'point', path_data_point },
      },
   },

   {  'Path',
      fields = {
	 { 'status', cairo.Status },
	 { 'data', cairo.PathData, ptr = true },
	 { 'num_data', ti.int },
      },

      methods = {
	 extents = { { ti.double, dir = 'out' }, { ti.double, dir = 'out' },
		     { ti.double, dir = 'out' }, { ti.double, dir = 'out' } },
      },
   },

   {  'FontExtents',
      fields = {
	 { 'ascent', ti.double },
	 { 'descent', ti.double },
	 { 'height', ti.double },
	 { 'max_x_advance', ti.double },
	 { 'max_y_advance', ti.double },
      },
   },

   {  'TextExtents',
      fields = {
	 { 'x_bearing', ti.double },
	 { 'y_bearing', ti.double },
	 { 'width', ti.double },
	 { 'height', ti.double },
	 { 'x_advance', ti.double },
	 { 'y_advance', ti.double },
      },<commit_msg>cairo: Remove extents() method from cairo.Path<commit_after>------------------------------------------------------------------------------
--
--  LGI Cairo override module.
--
--  Copyright (c) 2012 <NAME>
--  Licensed under the MIT license:
--  http://www.opensource.org/licenses/mit-license.php
--
------------------------------------------------------------------------------

local assert, pairs, ipairs, setmetatable, table, rawget, type
   = assert, pairs, ipairs, setmetatable, table, rawget, type
local lgi = require 'lgi'
local cairo = lgi.cairo

local core = require 'lgi.core'
local ffi = require 'lgi.ffi'
local component = require 'lgi.component'
local record = require 'lgi.record'
local enum = require 'lgi.enum'
local ti = ffi.types

cairo._module = core.module('cairo', 2)
local module_gobject = core.gi.cairo.resolve

-- Versioning support.
function cairo.version_encode(major, minor, micro)
   return 10000 * major + 100 * minor + micro
end
cairo.version = core.callable.new {
   addr = cairo._module.cairo_version, ret = ti.int } ()
cairo.version_string = core.callable.new {
   addr = cairo._module.cairo_version_string, ret = ti.utf8 } ()

-- Load some constants.
cairo._constant = {
   MIME_TYPE_JP2 = 'image/jp2',
   MIME_TYPE_JPEG = 'image/jpeg',
   MIME_TYPE_PNG = 'image/png',
   MIME_TYPE_URI = 'text/x-uri',
}

-- Load definitions of all enums.
cairo._enum = cairo._enum or {}
for _, name in pairs {
   'Status', 'Content', 'Operator', 'Antialias', 'FillRule', 'LineCap',
   'LineJoin', 'TextClusterFlags', 'FontSlant', 'FontWeight', 'SubpixelOrder',
   'HintStyle', 'HintMetrics', 'FontType', 'PathDataType', 'DeviceType',
   'SurfaceType', 'Format', 'PatternType', 'Extend', 'Filter', 'RegionOverlap',
   'PdfVersion', 'PsLevel', 'SvgVersion',
} do
   local lower = name:gsub('([%l%d])([%u])', '%1_%2'):lower()
   local gtype = ffi.load_gtype(
      module_gobject, 'cairo_gobject_' .. lower .. '_get_type')
   if gtype then
      cairo._enum[name] = ffi.load_enum(gtype, 'cairo.' .. name)
   else
      cairo._enum[name] = component.create(nil, enum.enum_mt, 'cairo.' .. name)
   end
end

-- Load definitions of all boxed records.
cairo._struct = cairo._struct or {}
for index, struct in pairs {
   'Context', 'Device', 'Surface', 'Rectangle', 'ScaledFont', 'FontFace',
   'FontOptions', 'Region', 'RectangleInt', 'Path', 'TextExtents',
   'FontExtents', 'Matrix', 'ImageSurface', 'PdfSurface', 'PsSurface',
   'RecordingSurface', 'SvgSurface', 'ToyFontFace', 'RectangleList',
   'SolidPattern', 'SurfacePattern', 'GradientPattern', 'LinearPattern',
   'RadialPattern',
   MeshPattern = cairo.version_encode(1, 12, 0),
} do
   local since = 0
   if type(index) == 'string' then
      since = struct
      struct = index
   end
   if cairo.version >= since then
      local lower = struct:gsub('([%l%d])([%u])', '%1_%2'):lower()
      local gtype = ffi.load_gtype(
	 module_gobject, 'cairo_gobject_' .. lower .. '_get_type')
      local obj = component.create(gtype, record.struct_mt, 'cairo.' .. struct)
      cairo._struct[struct] = obj
   end
end

local path_data_header = component.create(nil, record.struct_mt)
ffi.load_fields(path_data_header, { { 'type', cairo.PathDataType },
				    { 'length', ti.int } })
local path_data_point = component.create(nil, record.struct_mt)
ffi.load_fields(path_data_point, { { 'x', ti.double }, { 'y', ti.double } })
cairo._union = rawget(cairo, '_union') or {}
cairo._union.PathData = component.create(nil, record.union_mt, 'cairo.PathData')

-- Populate methods into records.
for _, info in ipairs {
   {  'Status',
      methods = {
	 to_string = { static = true, ret = ti.utf8, cairo.Status }
      },
   },

   {  'Rectangle',
      fields = {
	 { 'x', ti.double }, { 'y', ti.double },
	 { 'width', ti.double }, { 'height', ti.double },
      },
   },

   {  'RectangleList',
      fields = {
	 { 'status', cairo.Status },
	 { 'rectangles', cairo.Rectangle, ptr = true },
	 { 'num_rectangles', ti.int },
      },
   },

   {  'RectangleInt',
      fields = {
	 { 'x', ti.int }, { 'y', ti.int },
	 { 'width', ti.int }, { 'height', ti.int },
      },
   },

   {  'Matrix',
      fields = {
	 { 'xx', ti.double }, { 'yx', ti.double },
	 { 'xy', ti.double }, { 'yy', ti.double },
	 { 'x0', ti.double }, { 'y0', ti.double },
      },
      methods = {
	 init = { ti.double, ti.double, ti.double,
		  ti.double, ti.double, ti.double },
	 init_identity = {},
	 init_translate = { ti.double, ti.double },
	 init_scale = { ti.double, ti.double },
	 init_rotate = { ti.double },
	 translate = { ti.double, ti.double },
	 scale = { ti.double, ti.double },
	 rotate = { ti.double },
	 invert = { ret = cairo.Status },
	 multiply = { cairo.Matrix, cairo.Matrix },
	 transform_distance = { { ti.double, dir = 'inout' },
				{ ti.double, dir = 'inout' } },
	 transform_point = { { ti.double, dir = 'inout' },
			     { ti.double, dir = 'inout' } },
      },
   },

   {  'PathData',
      fields = {
	 { 'header', path_data_header },
	 { 'point', path_data_point },
      },
   },

   {  'Path',
      fields = {
	 { 'status', cairo.Status },
	 { 'data', cairo.PathData, ptr = true },
	 { 'num_data', ti.int },
      },
   },

   {  'FontExtents',
      fields = {
	 { 'ascent', ti.double },
	 { 'descent', ti.double },
	 { 'height', ti.double },
	 { 'max_x_advance', ti.double },
	 { 'max_y_advance', ti.double },
      },
   },

   {  'TextExtents',
      fields = {
	 { 'x_bearing', ti.double },
	 { 'y_bearing', ti.double },
	 { 'width', ti.double },
	 { 'height', ti.double },
	 { 'x_advance', ti.double },
	 { 'y_advance', ti.double },
      },
{'fraction_non_alphanumeric': 0.20406905055487054, 'fraction_numerical': 0.004932182490752158, 'mean_word_length': 2.7963891675025074, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3269733', 'n_tokens_mistral': 4077, 'n_tokens_neox': 3900, 'n_words': 861}