const c = @cImport({ @cInclude("pango/pangocairo.h"); }); pub const pango = struct { pub const Layout = struct { layout: *c.PangoLayout, pub fn createForCairo(s: *cairo.Surface) !Layout { const layout = c.pango_cairo_create_layout( s.cairo, ) orelse return error.PangoCairoError; return .{ .layout = layout }; } pub fn setFont(l: Layout, f: FontDescription) void { c.pango_layout_set_font_description(l.layout, f.font); } pub fn setFontFromString(l: Layout, font: [*:0]const u8) !void { const pfont = c.pango_font_description_from_string( font, ) orelse return error.BadFont; defer c.pango_font_description_free(pfont); c.pango_layout_set_font_description(l.layout, pfont); } pub fn setText(l: Layout, text: [*:0]const u8, len: i32) void { c.pango_layout_set_text(l.layout, text, len); } pub fn show(l: Layout, s: *cairo.Surface) void { c.pango_cairo_show_layout(s.cairo, l.layout); } pub fn unref(l: Layout) void { c.g_object_unref(l.layout); } }; pub const FontDescription = struct { font: *c.PangoFontDescription, pub fn create() !FontDescription { const font = c.pango_font_description_new() orelse return error.PangoError; return .{ .font = font }; } pub fn fromString(str: [*:0]const u8) !FontDescription { const font = c.pango_font_description_from_string(str) orelse return error.PangoError; return .{ .font= font }; } pub fn setFamily(f: FontDescription, name: [*:0]const u8) void { c.pango_font_description_set_family(f.font, name); } pub fn setWeight(f: FontDescription, weight: u32) void { c.pango_font_description_set_weight(f.font, weight); } pub fn setSize(f: FontDescription, size: i32) void { c.pango_font_description_set_size(f.font, size); } pub fn setSizePt(f: FontDescription, pt: i32) void { c.pango_font_description_set_size(f.font, pt * c.PANGO_SCALE); } pub fn destroy(f: FontDescription) void { c.pango_font_description_free(f.font); } }; }; pub const cairo = struct { pub const Format = enum(c_int) { invalid = -1, argb32, rgb24, }; pub const Surface = struct { cairo: *c.cairo_t, surface: *c.cairo_surface_t, pub fn formatStrideForWidth(format: Format, width: i32) i32 { return c.cairo_format_stride_for_width(@intFromEnum(format), width); } pub fn createImage(format: Format, width: i32, height: i32) !Surface { const surface = c.cairo_image_surface_create( @intFromEnum(format), width, height, ) orelse return error.CairoError; const cr = c.cairo_create(surface) orelse return error.CairoError; return .{ .cairo = cr, .surface = surface }; } pub fn createImageForData( data: []u8, format: Format, width: i32, height: i32, stride: i32, ) !Surface { const surface = c.cairo_image_surface_create_for_data( data.ptr, @intFromEnum(format), width, height, stride, ) orelse return error.CairoError; const cr = c.cairo_create(surface) orelse return error.CairoError; return .{ .cairo = cr, .surface = surface }; } pub fn moveTo(s: Surface, x: f64, y: f64) void { c.cairo_move_to(s.cairo, x, y); } pub fn setSourceRgba( s: Surface, red: f64, green: f64, blue: f64, alpha: f64, ) void { c.cairo_set_source_rgba(s.cairo, red, green, blue, alpha); } pub fn paint(s: Surface) void { c.cairo_paint(s.cairo); } pub fn destroy(s: Surface) void { c.cairo_destroy(s.cairo); c.cairo_surface_destroy(s.surface); } }; };