From 7f5de6c15243bd8f3f9952ed091e4dd15e809020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Thu, 12 Sep 2024 22:51:07 +0300 Subject: pango and cairo with shm Use pango and cairo to render text to the shared mem buffer. --- src/pangocairo.zig | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 src/pangocairo.zig (limited to 'src/pangocairo.zig') diff --git a/src/pangocairo.zig b/src/pangocairo.zig new file mode 100644 index 0000000..3995ba7 --- /dev/null +++ b/src/pangocairo.zig @@ -0,0 +1,142 @@ +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); + } + }; +}; -- cgit v1.2.3