// Native XRender helpers. Pictures and glyph sets are owned handles that borrow // an Extension and its connection; formats and filter lists are V-owned copies. module render import math import vx11 #include "@VMODROOT/render/bridge.h" fn C.vx11_render_backdrop(&vx11.Display, i32, i32, i32, i32) vx11.Pixmap // Largest polygon for polygon(): libXrender allocates points² trapezoids. pub const max_polygon_points = 1024 pub fn available(connection &vx11.Connection) !bool { mut events := i32(0) mut errors := i32(0) return query_extension(connection.raw()!, &events, &errors) != 0 } // Extension borrows its connection and owns no server resources. Handles it // creates must be closed before the connection; methods fail after it closes. @[heap; noinit] pub struct Extension { connection &vx11.Connection pub: event_base int error_base int major int minor int } // bind negotiates the protocol version and loads libXrender's format cache. pub fn bind(connection &vx11.Connection) !&Extension { d := connection.raw()! mut events := i32(0) mut errors := i32(0) if query_extension(d, &events, &errors) == 0 { return error('XRender extension is unavailable') } mut major := i32(0) mut minor := i32(0) if query_version(d, &major, &minor) == 0 { return error('cannot query XRender version') } if query_formats(d) == 0 { return error('cannot query XRender picture formats') } return &Extension{ connection: connection, event_base: int(events), error_base: int(errors), major: int(major), minor: int(minor) } } // supports reports whether the negotiated version is at least major.minor. pub fn (e &Extension) supports(major int, minor int) bool { return e.major > major || (e.major == major && e.minor >= minor) } fn (e &Extension) require(major int, minor int, feature string) ! { if !e.supports(major, minor) { return error('${feature} requires XRender ${major}.${minor}; server provides ${e.major}.${e.minor}') } } fn fits_i16(value int) bool { return value >= -32768 && value <= 32767 } fn fits_u16(value int) bool { return value >= 0 && value <= 65535 } // check_op rejects operators the negotiated version does not define. fn (e &Extension) check_op(op int) ! { if op >= op_minimum && op <= op_maximum { return } if (op >= op_disjoint_minimum && op <= op_disjoint_maximum) || (op >= op_conjoint_minimum && op <= op_conjoint_maximum) { return e.require(0, 2, 'disjoint and conjoint operators') } if op >= op_blend_minimum && op <= op_blend_maximum { return e.require(0, 11, 'blend mode operators') } return error('invalid XRender operator ${op}') } // request_fits checks a request length in 4-byte units against the server limit. fn request_fits(d &vx11.Display, words i64) bool { mut limit := i64(vx11.extended_max_request_size(d)) if limit <= 0 { limit = i64(vx11.max_request_size(d)) } return words <= limit } // to_fixed converts a coordinate to 16.16 notation after checking its range. pub fn to_fixed(value f64) !Fixed { if math.is_nan(value) || math.is_inf(value, 0) || value < -32768.0 || value >= 32768.0 { return error('value ${value} is outside the XRender fixed-point range') } return fixed(value) } // premultiplied scales the color channels by alpha, as fill colors require. pub fn premultiplied(red u16, green u16, blue u16, alpha u16) Color { a := u32(alpha) return Color{ red: u16((u32(red) * a + 0x7fff) / 0xffff) green: u16((u32(green) * a + 0x7fff) / 0xffff) blue: u16((u32(blue) * a + 0x7fff) / 0xffff) alpha: alpha } } // identity_transform returns the 16.16 fixed-point identity matrix. pub fn identity_transform() Transform { mut t := Transform{} for i in 0 .. 3 { t.matrix[i][i] = fixed(1.0) } return t } // scale_transform maps destination pixels to source pixels: sample the source // at (x * sx, y * sy). Use 1/2 to enlarge twofold. pub fn scale_transform(sx f64, sy f64) !Transform { mut t := identity_transform() t.matrix[0][0] = to_fixed(sx)! t.matrix[1][1] = to_fixed(sy)! return t } // Formats // formats lists every picture format the server supports. pub fn (e &Extension) formats() ![]Format { d := e.connection.raw()! template := Format{} mut result := []Format{} for i := 0; i < 0x7fffffff; i++ { f := find_format(d, 0, &template, i32(i)) if f == unsafe { nil } { break } result << *f } return result } // find_format returns the index-th format whose mask-selected fields (format_* // bits) equal those of template. pub fn (e &Extension) find_format(mask vx11.CULong, template Format, index int) ?Format { if index < 0 || index > 0x7fffffff { return none } d := e.connection.raw() or { return none } f := find_format(d, mask, &template, i32(index)) if f == unsafe { nil } { return none } return *f } // standard_format returns one of the standard_* formats. pub fn (e &Extension) standard_format(kind int) !Format { if kind < 0 || kind >= standard_num { return error('invalid standard format ${kind}') } f := find_standard_format(e.connection.raw()!, i32(kind)) if f == unsafe { nil } { return error('the server has no standard format ${kind}') } return *f } pub fn (e &Extension) visual_format(visual &vx11.Visual) !Format { if visual == unsafe { nil } { return error('invalid visual') } f := find_visual_format(e.connection.raw()!, visual) if f == unsafe { nil } { return error('the visual has no XRender format') } return *f } // default_format matches the default visual of the default screen. pub fn (e &Extension) default_format() !Format { d := e.connection.raw()! return e.visual_format(vx11.default_visual(d, vx11.default_screen(d))) } // index_values copies the colormap entries of an indexed format. pub fn (e &Extension) index_values(format Format) ![]IndexValue { e.require(0, 7, 'picture index values')! if format.@type != pict_type_indexed { return error('format is not indexed') } mut count := i32(0) p := query_pict_index_values(e.connection.raw()!, &format, &count) defer { if p != unsafe { nil } { vx11.free(p) } } if p == unsafe { nil } || count < 0 { return error('cannot query picture index values') } mut result := []IndexValue{cap: int(count)} for i in 0 .. int(count) { result << unsafe { p[i] } } return result } // subpixel_order returns a subpixel_* value for a screen. pub fn (e &Extension) subpixel_order(screen int) !int { d := e.connection.raw()! if screen < 0 || screen >= int(vx11.screen_count(d)) { return error('invalid screen ${screen}') } return int(query_subpixel_order(d, i32(screen))) } // set_subpixel_order changes libXrender's cached order for this client only. pub fn (e &Extension) set_subpixel_order(screen int, order int) ! { d := e.connection.raw()! if screen < 0 || screen >= int(vx11.screen_count(d)) { return error('invalid screen ${screen}') } if order < subpixel_unknown || order > subpixel_none { return error('invalid subpixel order') } if set_subpixel_order(d, i32(screen), i32(order)) == 0 { return error('cannot set the subpixel order') } } // FilterInfo names a filter. alias is the name of the filter it stands for, or // '' when it is a filter in its own right. pub struct FilterInfo { pub: name string alias string } // filters lists the filters available for pictures on drawable's screen. pub fn (e &Extension) filters(drawable vx11.Drawable) ![]FilterInfo { e.require(0, 6, 'picture filters')! if drawable == 0 { return error('invalid drawable') } p := query_filters(e.connection.raw()!, drawable) if p == unsafe { nil } { return error('cannot query XRender filters') } defer { vx11.free(p) } if p.nfilter < 0 || p.nalias < 0 || (p.nfilter > 0 && p.filter == unsafe { nil }) || (p.nalias > 0 && p.alias == unsafe { nil }) { return error('invalid XRender filter list') } mut names := []string{cap: int(p.nfilter)} for i in 0 .. int(p.nfilter) { names << unsafe { cstring_to_vstring(p.filter[i]) } } mut result := []FilterInfo{cap: names.len} for i, filter_name in names { mut target := '' if i < int(p.nalias) { index := int(u16(unsafe { p.alias[i] })) if index != 0xffff && index < names.len { target = names[index] } } result << FilterInfo{ name: filter_name, alias: target } } return result } // parse_color accepts X color names, #rgb forms and rgba:r/g/b/a. The result // is premultiplied, ready for fill and solid_fill. pub fn (e &Extension) parse_color(spec string) !Color { if spec == '' || spec.contains('\x00') { return error('invalid color specification') } mut c := Color{} if parse_color(e.connection.raw()!, &char(spec.str), &c) == 0 { return error('cannot parse color "${spec}"') } return c } // Pictures @[params] pub struct PictureOptions { pub: repeat int = repeat_none poly_edge int = poly_edge_smooth poly_mode int = poly_mode_precise component_alpha bool include_inferiors bool } // PictureHandle owns one Picture. Close it before its drawable is destroyed // (a window's pictures die with it) and before the connection. @[heap; noinit] pub struct PictureHandle { extension &Extension pub: // drawable is 0 for solid fills and gradients. drawable vx11.Drawable width int height int depth int mut: handle Picture root vx11.Window } fn (e &Extension) check_repeat(mode int) ! { if mode == repeat_none || mode == repeat_normal { return } if mode == repeat_pad || mode == repeat_reflect { return e.require(0, 10, 'pad and reflect repeat modes') } return error('invalid repeat mode ${mode}') } // create_picture wraps a pixmap or window. format.depth must match the // drawable, and a window needs its visual's format (see window_picture). pub fn (e &Extension) create_picture(drawable vx11.Drawable, format Format, options PictureOptions) !&PictureHandle { if drawable == 0 { return error('invalid drawable') } if format.id == 0 { return error('invalid picture format') } e.check_repeat(options.repeat)! if options.poly_edge !in [poly_edge_sharp, poly_edge_smooth] { return error('invalid polygon edge mode') } if options.poly_mode !in [poly_mode_precise, poly_mode_imprecise] { return error('invalid polygon mode') } d := e.connection.raw()! mut root := vx11.Window(0) mut x := i32(0) mut y := i32(0) mut w := u32(0) mut h := u32(0) mut border := u32(0) mut depth := u32(0) if vx11.get_geometry(d, drawable, &root, &x, &y, &w, &h, &border, &depth) == 0 { return error('cannot query drawable geometry') } if int(depth) != int(format.depth) { return error('format depth ${format.depth} does not match drawable depth ${depth}') } attributes := PictureAttributes{ repeat: i32(options.repeat) poly_edge: i32(options.poly_edge) poly_mode: i32(options.poly_mode) component_alpha: if options.component_alpha { 1 } else { 0 } subwindow_mode: if options.include_inferiors { vx11.include_inferiors } else { vx11.clip_by_children } } mask := cp_repeat | cp_poly_edge | cp_poly_mode | cp_component_alpha | cp_subwindow_mode id := create_picture(d, drawable, &format, mask, &attributes) if id == 0 { return error('cannot create picture') } return &PictureHandle{ extension: e, drawable: drawable, width: int(w), height: int(h), depth: int(depth), handle: id, root: root } } // window_picture wraps a window using the format of its visual. pub fn (e &Extension) window_picture(window vx11.Window, options PictureOptions) !&PictureHandle { if window == 0 { return error('invalid window') } mut attributes := vx11.XWindowAttributes{} if vx11.get_window_attributes(e.connection.raw()!, window, &attributes) == 0 { return error('cannot query window attributes') } return e.create_picture(vx11.Drawable(window), e.visual_format(attributes.visual)!, options) } fn (e &Extension) source_picture(id Picture, what string) !&PictureHandle { if id == 0 { return error('cannot create ${what}') } return &PictureHandle{ extension: e, handle: id } } // solid_fill creates an infinite single-color source. color is premultiplied. pub fn (e &Extension) solid_fill(color Color) !&PictureHandle { e.require(0, 10, 'solid fills')! return e.source_picture(create_solid_fill(e.connection.raw()!, &color), 'solid fill') } // Point is a position in pixels; gradient helpers convert it to 16.16 notation. pub struct Point { pub: x f64 y f64 } fn (p Point) fixed() !PointFixed { return PointFixed{ x: to_fixed(p.x)!, y: to_fixed(p.y)! } } // ColorStop places a non-premultiplied color at offset 0..1 along a gradient. pub struct ColorStop { pub: offset f64 color Color } fn gradient_stops(d &vx11.Display, stops []ColorStop, header_words int) !([]Fixed, []Color) { offsets, colors := stop_arrays(stops)! if !request_fits(d, i64(header_words) + i64(stops.len) * 3) { return error('too many gradient stops for one request') } return offsets, colors } // stop_arrays checks that offsets rise within [0, 1] and splits the stops. fn stop_arrays(stops []ColorStop) !([]Fixed, []Color) { if stops.len == 0 { return error('a gradient needs at least one color stop') } mut offsets := []Fixed{cap: stops.len} mut colors := []Color{cap: stops.len} mut previous := 0.0 for s in stops { if math.is_nan(s.offset) || s.offset < 0 || s.offset > 1 || s.offset < previous { return error('gradient stop offsets must rise from 0 to 1') } previous = s.offset offsets << fixed(s.offset) colors << s.color } return offsets, colors } pub fn (e &Extension) linear_gradient(start Point, end Point, stops []ColorStop) !&PictureHandle { e.require(0, 10, 'gradients')! gradient := LinearGradient{ p1: start.fixed()!, p2: end.fixed()! } if gradient.p1.x == gradient.p2.x && gradient.p1.y == gradient.p2.y { return error('a linear gradient needs distinct end points') } d := e.connection.raw()! offsets, colors := gradient_stops(d, stops, 7)! return e.source_picture(create_linear_gradient(d, &gradient, offsets.data, colors.data, i32(stops.len)), 'linear gradient') } pub fn (e &Extension) radial_gradient(inner_center Point, inner_radius f64, outer_center Point, outer_radius f64, stops []ColorStop) !&PictureHandle { e.require(0, 10, 'gradients')! if !(inner_radius >= 0) || !(outer_radius >= 0) { return error('gradient radii must not be negative') } ic := inner_center.fixed()! oc := outer_center.fixed()! gradient := RadialGradient{ inner: Circle{ x: ic.x, y: ic.y, radius: to_fixed(inner_radius)! } outer: Circle{ x: oc.x, y: oc.y, radius: to_fixed(outer_radius)! } } d := e.connection.raw()! offsets, colors := gradient_stops(d, stops, 9)! return e.source_picture(create_radial_gradient(d, &gradient, offsets.data, colors.data, i32(stops.len)), 'radial gradient') } // conical_gradient sweeps the stops around center, starting at angle degrees. pub fn (e &Extension) conical_gradient(center Point, angle f64, stops []ColorStop) !&PictureHandle { e.require(0, 10, 'gradients')! gradient := ConicalGradient{ center: center.fixed()!, angle: to_fixed(angle)! } d := e.connection.raw()! offsets, colors := gradient_stops(d, stops, 6)! return e.source_picture(create_conical_gradient(d, &gradient, offsets.data, colors.data, i32(stops.len)), 'conical gradient') } pub fn (p &PictureHandle) id() !Picture { if p.handle == 0 { return error('picture is closed') } p.extension.connection.raw()! return p.handle } pub fn (mut p PictureHandle) close() { if p.handle != 0 { if d := p.extension.connection.raw() { free_picture(d, p.handle) } p.handle = 0 } } fn (p &PictureHandle) display() !(&vx11.Display, Picture) { id := p.id()! return p.extension.connection.raw()!, id } // set_transform maps destination coordinates to source coordinates. pub fn (p &PictureHandle) set_transform(transform Transform) ! { p.extension.require(0, 6, 'picture transforms')! d, id := p.display()! set_picture_transform(d, id, &transform) } // set_filter selects a filter from Extension.filters. Only convolution takes // parameters: width and height as whole 16.16 numbers, then width*height values. pub fn (p &PictureHandle) set_filter(filter string, params []Fixed) ! { p.extension.require(0, 6, 'picture filters')! if filter == '' || filter.contains('\x00') { return error('invalid filter name') } d, id := p.display()! // Source-only pictures use the first screen's filters, as the server does. root := if p.root != 0 { p.root } else { vx11.root_window(d, 0) } known := p.extension.filters(vx11.Drawable(root))! if !known.any(it.name == filter) { return error('the server has no "${filter}" filter') } if filter == filter_convolution { if params.len < 3 || params[0] & 0xffff != 0 || params[1] & 0xffff != 0 || params[0] <= 0 || params[1] <= 0 || i64(params[0] >> 16) * i64(params[1] >> 16) > i64(params.len - 2) { return error('convolution needs width, height and width*height values') } } else if params.len > 0 && filter in [filter_nearest, filter_bilinear, filter_fast, filter_good, filter_best] { return error('the ${filter} filter takes no parameters') } if !request_fits(d, 3 + (i64(filter.len) + 3) / 4 + i64(params.len)) { return error('too many filter parameters for one request') } set_picture_filter(d, id, &char(filter.str), if params.len == 0 { unsafe { nil } } else { params.data }, i32(params.len)) } pub fn (p &PictureHandle) set_repeat(mode int) ! { p.extension.check_repeat(mode)! d, id := p.display()! attributes := PictureAttributes{ repeat: i32(mode) } change_picture(d, id, cp_repeat, &attributes) } pub fn (p &PictureHandle) set_component_alpha(enabled bool) ! { d, id := p.display()! attributes := PictureAttributes{ component_alpha: if enabled { 1 } else { 0 } } change_picture(d, id, cp_component_alpha, &attributes) } // Rectangle uses native integers; conversion checks the protocol's 16-bit limits. pub struct Rectangle { pub: x int y int width int height int } // x_rectangles converts rectangles. header_words < 0 skips the request length // check, for calls that libXrender splits into several requests. fn x_rectangles(d &vx11.Display, rectangles []Rectangle, header_words int) ![]vx11.XRectangle { if header_words >= 0 && !request_fits(d, i64(header_words) + i64(rectangles.len) * 2) { return error('too many rectangles for one request') } mut raw := []vx11.XRectangle{cap: rectangles.len} for r in rectangles { if !fits_i16(r.x) || !fits_i16(r.y) || !fits_u16(r.width) || !fits_u16(r.height) { return error('rectangle exceeds X11 coordinate limits') } raw << vx11.XRectangle{ x: i16(r.x), y: i16(r.y), width: u16(r.width), height: u16(r.height) } } return raw } // set_clip limits drawing to rectangles; an empty list hides everything. pub fn (p &PictureHandle) set_clip(rectangles []Rectangle) ! { d, id := p.display()! raw := x_rectangles(d, rectangles, 3)! set_picture_clip_rectangles(d, id, 0, 0, raw.data, i32(raw.len)) } pub fn (p &PictureHandle) clear_clip() ! { d, id := p.display()! attributes := PictureAttributes{ clip_mask: 0 } change_picture(d, id, cp_clip_mask, &attributes) } // fill combines a premultiplied color with each rectangle of this picture. pub fn (p &PictureHandle) fill(op int, color Color, rectangles []Rectangle) ! { p.extension.check_op(op)! d, id := p.display()! if rectangles.len == 0 { return } raw := x_rectangles(d, rectangles, -1)! fill_rectangles(d, i32(op), id, &color, raw.data, i32(raw.len)) } @[params] pub struct CompositeOptions { pub: mask &PictureHandle = unsafe { nil } src_x int src_y int mask_x int mask_y int dst_x int dst_y int width int height int } // composite combines src (through an optional mask) into this picture. pub fn (p &PictureHandle) composite(op int, src &PictureHandle, options CompositeOptions) ! { p.extension.check_op(op)! d, id := p.display()! source := src.id()! mask := if options.mask == unsafe { nil } { Picture(0) } else { options.mask.id()! } for v in [options.src_x, options.src_y, options.mask_x, options.mask_y, options.dst_x, options.dst_y] { if !fits_i16(v) { return error('composite offset exceeds X11 coordinate limits') } } if !fits_u16(options.width) || !fits_u16(options.height) { return error('composite size exceeds X11 limits') } composite(d, i32(op), source, mask, id, i32(options.src_x), i32(options.src_y), i32(options.mask_x), i32(options.mask_y), i32(options.dst_x), i32(options.dst_y), u32(options.width), u32(options.height)) } @[params] pub struct ShapeOptions { pub: src_x int src_y int // antialias accumulates edges in an A8 mask; false uses A1. antialias bool = true // even_odd selects the even-odd fill rule instead of nonzero winding. even_odd bool } fn (e &Extension) shape_mask(options ShapeOptions) !Format { return e.standard_format(if options.antialias { standard_a8 } else { standard_a1 }) } // polygon fills a closed polygon with src. libXrender tessellates it into // trapezoids; at most max_polygon_points points are accepted. pub fn (p &PictureHandle) polygon(op int, src &PictureHandle, points []Point, options ShapeOptions) ! { p.extension.require(0, 4, 'polygons')! p.extension.check_op(op)! if points.len < 3 { return error('a polygon needs at least three points') } if points.len > max_polygon_points { return error('a polygon may have at most ${max_polygon_points} points') } if !fits_i16(options.src_x) || !fits_i16(options.src_y) { return error('source offset exceeds X11 limits') } d, id := p.display()! source := src.id()! mut raw := []PointDouble{cap: points.len} for pt in points { to_fixed(pt.x)! to_fixed(pt.y)! raw << PointDouble{ x: pt.x, y: pt.y } } mask := p.extension.shape_mask(options)! composite_double_poly(d, i32(op), source, id, &mask, i32(options.src_x), i32(options.src_y), 0, 0, raw.data, i32(raw.len), if options.even_odd { 0 } else { 1 }) } // triangles fills each group of three points with src. pub fn (p &PictureHandle) triangles(op int, src &PictureHandle, points []Point, options ShapeOptions) ! { p.extension.require(0, 4, 'triangles')! p.extension.check_op(op)! if points.len == 0 || points.len % 3 != 0 { return error('triangles need a multiple of three points') } if points.len / 3 > 0x7fffffff { return error('too many triangles') } if !fits_i16(options.src_x) || !fits_i16(options.src_y) { return error('source offset exceeds X11 limits') } d, id := p.display()! source := src.id()! mut raw := []Triangle{cap: points.len / 3} for i := 0; i < points.len; i += 3 { raw << Triangle{ p1: points[i].fixed()!, p2: points[i + 1].fixed()!, p3: points[i + 2].fixed()! } } mask := p.extension.shape_mask(options)! composite_triangles(d, i32(op), source, id, &mask, i32(options.src_x), i32(options.src_y), raw.data, i32(raw.len)) } // Glyph sets // GlyphMetrics mirrors GlyphInfo as a V struct, so the map holding it needs no // C-aligned allocation. struct GlyphMetrics { width u16 height u16 x i16 y i16 x_off i16 y_off i16 } // GlyphSetHandle owns one GlyphSet and tracks the glyphs added through it. // Close it before the connection. @[heap; noinit] pub struct GlyphSetHandle { extension &Extension pub: format Format mut: handle GlyphSet bits_per_pixel int glyphs map[u64]GlyphMetrics } pub fn (e &Extension) create_glyph_set(format Format) !&GlyphSetHandle { if format.id == 0 { return error('invalid glyph format') } d := e.connection.raw()! mut count := i32(0) formats := vx11.list_pixmap_formats(d, &count) if formats == unsafe { nil } { return error('cannot query pixmap formats') } mut bpp := 0 for i in 0 .. int(count) { f := unsafe { formats[i] } if f.depth == format.depth { bpp = int(f.bits_per_pixel) } } vx11.free(formats) if bpp == 0 { return error('the server has no pixmap format for depth ${format.depth}') } id := create_glyph_set(d, &format) if id == 0 { return error('cannot create glyph set') } return &GlyphSetHandle{ extension: e, format: format, handle: id, bits_per_pixel: bpp } } pub fn (g &GlyphSetHandle) id() !GlyphSet { if g.handle == 0 { return error('glyph set is closed') } g.extension.connection.raw()! return g.handle } pub fn (mut g GlyphSetHandle) close() { if g.handle != 0 { if d := g.extension.connection.raw() { free_glyph_set(d, g.handle) } g.handle = 0 g.glyphs.clear() } } // stride returns the bytes per image row for a glyph of this set's format. pub fn (g &GlyphSetHandle) stride(width int) int { return int((i64(width) * g.bits_per_pixel + 31) / 32 * 4) } // add stores or replaces a glyph. image is a Z-format bitmap of // info.height rows, each stride(info.width) bytes; the bytes are copied. pub fn (mut g GlyphSetHandle) add(id Glyph, info GlyphInfo, image []u8) ! { set := g.id()! d := g.extension.connection.raw()! if id > Glyph(0xffffffff) { return error('glyph IDs are 32-bit') } expected := i64(g.stride(int(info.width))) * i64(info.height) if i64(image.len) != expected { return error('glyph image has ${image.len} bytes; expected ${expected}') } if !request_fits(d, 3 + 1 + 3 + expected / 4) { return error('glyph image is too large for one request') } gid := id images := if image.len == 0 { &char(unsafe { nil }) } else { &char(image.data) } add_glyphs(d, set, &gid, &info, 1, images, i32(image.len)) g.glyphs[u64(id)] = GlyphMetrics{info.width, info.height, info.x, info.y, info.xOff, info.yOff} } // remove frees glyphs that were added through this handle. pub fn (mut g GlyphSetHandle) remove(ids []Glyph) ! { set := g.id()! for id in ids { if u64(id) !in g.glyphs { return error('glyph ${id} was not added to this glyph set') } } if ids.len == 0 { return } d := g.extension.connection.raw()! if !request_fits(d, 2 + i64(ids.len)) { return error('too many glyphs for one request') } free_glyphs(d, set, ids.data, i32(ids.len)) for id in ids { g.glyphs.delete(u64(id)) } } pub fn (g &GlyphSetHandle) contains(id Glyph) bool { return u64(id) in g.glyphs } // info returns the metrics of a glyph added through this handle. pub fn (g &GlyphSetHandle) info(id Glyph) ?GlyphInfo { m := g.glyphs[u64(id)] or { return none } return GlyphInfo{ width: m.width, height: m.height, x: m.x, y: m.y, xOff: m.x_off, yOff: m.y_off } } @[params] pub struct GlyphOptions { pub: src_x int src_y int // separate composites each glyph on its own instead of through a mask in // the glyph set's format. Use it for color glyphs. separate bool } // glyphs draws glyph IDs from set with their origin at (x, y), advancing by // each glyph's offset. The server skips IDs the set does not contain. pub fn (p &PictureHandle) glyphs(op int, src &PictureHandle, set &GlyphSetHandle, x int, y int, ids []Glyph, options GlyphOptions) ! { p.extension.check_op(op)! for v in [x, y, options.src_x, options.src_y] { if !fits_i16(v) { return error('glyph position exceeds X11 coordinate limits') } } if ids.len > 0x7fffffff { return error('too many glyphs') } d, id := p.display()! source := src.id()! glyph_set := set.id()! if ids.len == 0 { return } mut chars := []u32{cap: ids.len} for g in ids { if g > Glyph(0xffffffff) { return error('glyph IDs are 32-bit') } chars << u32(g) } mask := if options.separate { &Format(unsafe { nil }) } else { &set.format } composite_string32(d, i32(op), source, id, mask, glyph_set, i32(options.src_x), i32(options.src_y), i32(x), i32(y), chars.data, i32(chars.len)) } // Cursors // create_cursor makes a cursor from a drawable-backed picture. The hot spot // must lie inside it. The caller owns the cursor: release it with // vx11.free_cursor before closing the connection. pub fn (e &Extension) create_cursor(source &PictureHandle, hot_x int, hot_y int) !vx11.Cursor { e.require(0, 5, 'picture cursors')! id := source.id()! if source.drawable == 0 { return error('cursors need a picture backed by a drawable') } if hot_x < 0 || hot_y < 0 || hot_x >= source.width || hot_y >= source.height { return error('cursor hot spot lies outside the picture') } cursor := create_cursor(e.connection.raw()!, id, u32(hot_x), u32(hot_y)) if cursor == 0 { return error('cannot create cursor') } return cursor } // CursorFrame shows cursor for delay_ms milliseconds. pub struct CursorFrame { pub: cursor vx11.Cursor delay_ms int } // create_anim_cursor copies the frames into a new cursor. The caller keeps // ownership of the frame cursors and owns the result; free each with // vx11.free_cursor. pub fn (e &Extension) create_anim_cursor(frames []CursorFrame) !vx11.Cursor { e.require(0, 8, 'animated cursors')! if frames.len == 0 { return error('an animated cursor needs at least one frame') } d := e.connection.raw()! if !request_fits(d, 2 + i64(frames.len) * 2) { return error('too many cursor frames for one request') } mut raw := []AnimCursor{cap: frames.len} for f in frames { if f.cursor == 0 { return error('invalid cursor frame') } if f.delay_ms < 0 || i64(f.delay_ms) > 0xffffffff { return error('invalid frame delay') } raw << AnimCursor{ cursor: f.cursor, delay: vx11.CULong(f.delay_ms) } } cursor := create_anim_cursor(d, i32(raw.len), raw.data) if cursor == 0 { return error('cannot create animated cursor') } return cursor } // Snapshots // dimmed_snapshot copies a visible root region and applies a dark translucent // tint. Capture before mapping an overlay. Caller owns the returned pixmap and // must free it before closing c. Target uses the default screen visual/depth. pub fn dimmed_snapshot(c &vx11.Connection, x int, y int, width int, height int) !vx11.Pixmap { if !available(c)! { return error('XRender extension is unavailable') } sw, sh := c.dimensions()! if x < 0 || y < 0 || width <= 0 || height <= 0 || width > sw || height > sh || x > sw - width || y > sh - height { return error('snapshot region is outside the root window') } return C.vx11_render_backdrop(c.raw()!, i32(x), i32(y), i32(width), i32(height)) }