| 1 | module main |
| 2 | |
| 3 | import vx11 |
| 4 | import vx11.xft |
| 5 | import vx11.fontconfig |
| 6 | |
| 7 | fn main() { |
| 8 | mut connection := vx11.connect('')! |
| 9 | defer { connection.close() } |
| 10 | mut window := connection.new_window(width: 640, height: 300, title: 'Xft integration')! |
| 11 | defer { window.destroy() } |
| 12 | mut request := fontconfig.parse('monospace:pixelsize=22')! |
| 13 | defer { request.close() } |
| 14 | mut font := xft.open_pattern(connection, request)! |
| 15 | defer { font.close() } |
| 16 | assert request.string_value(fontconfig.family)! == 'monospace' |
| 17 | assert font.metrics()!.height > 0 |
| 18 | assert font.extents('Hello Ω')!.x_advance > 0 |
| 19 | assert font.contains(`A`)! |
| 20 | assert font.glyph(`A`)! > 0 |
| 21 | mut fallback := font.fallback(`Ω`)! |
| 22 | defer { fallback.close() } |
| 23 | assert fallback.contains(`Ω`)! |
| 24 | mut draw := xft.for_window(connection, window)! |
| 25 | defer { draw.close() } |
| 26 | window.show()! |
| 27 | draw.rectangle(0, 0, 640, 300, '#10151d')! |
| 28 | draw.text(font, 20, 45, 'Hello Ω — Xft', '#eef4f7')! |
| 29 | draw.glyphs([xft.Glyph{ font: font, index: font.glyph(`A`)!, x: 20, y: 90 }, |
| 30 | xft.Glyph{ font: fallback, index: fallback.glyph(`Ω`)!, x: 50, y: 90 }], '#9becb7')! |
| 31 | draw.clip([vx11.XRectangle{ x: 100, y: 100, width: 20, height: 20 }])! |
| 32 | draw.rectangle(0, 0, 640, 300, '#ff0000')! |
| 33 | draw.clear_clip()! |
| 34 | connection.sync()! |
| 35 | image := vx11.get_image(connection.raw()!, vx11.Drawable(window.id), 0, 0, 640, 300, vx11.x_all_planes, vx11.z_pixmap) |
| 36 | assert image != unsafe { nil } |
| 37 | background := vx11.get_pixel(image, 0, 0) |
| 38 | assert vx11.get_pixel(image, 105, 105) != background |
| 39 | assert vx11.get_pixel(image, 95, 105) == background |
| 40 | mut ink := 0 |
| 41 | for y in 15 .. 50 { |
| 42 | for x in 20 .. 220 { if vx11.get_pixel(image, i32(x), i32(y)) != background { ink++ } } |
| 43 | } |
| 44 | assert ink > 20 |
| 45 | vx11.destroy_image(image) |
| 46 | // Re-target a backing pixmap, as a terminal does on resize. |
| 47 | raw := connection.raw()! |
| 48 | screen := vx11.default_screen(raw) |
| 49 | pixmap := vx11.create_pixmap(raw, vx11.Drawable(window.id), 80, 80, u32(vx11.default_depth(raw, screen))) |
| 50 | draw.change(vx11.Drawable(pixmap))! |
| 51 | draw.rectangle(0, 0, 80, 80, '#223344')! |
| 52 | draw.text(font, 0, 30, 'abc', '#ffffff')! |
| 53 | draw.change(vx11.Drawable(window.id))! |
| 54 | vx11.free_pixmap(raw, pixmap) |
| 55 | mut other := vx11.connect('')! |
| 56 | mut other_font := xft.open_font(other, 'monospace:pixelsize=12')! |
| 57 | mut mismatch := false |
| 58 | draw.text(other_font, 0, 0, 'wrong display', '#ffffff') or { mismatch = true } |
| 59 | assert mismatch |
| 60 | other_font.close() |
| 61 | other.close() |
| 62 | font.close() |
| 63 | font.close() |
| 64 | mut closed_font := false |
| 65 | draw.text(font, 0, 0, 'closed', '#ffffff') or { closed_font = true } |
| 66 | assert closed_font |
| 67 | draw.close() |
| 68 | draw.close() |
| 69 | mut closed_draw := false |
| 70 | draw.clear_clip() or { closed_draw = true } |
| 71 | assert closed_draw |
| 72 | fallback.close() |
| 73 | window.destroy() |
| 74 | connection.close() |
| 75 | mut closed_connection := false |
| 76 | xft.open_font(connection, 'monospace') or { closed_connection = true } |
| 77 | assert closed_connection |
| 78 | println('PASS: pattern ownership, Xft fonts, Unicode, fallback, glyph batches, clipping, pixmaps, and lifetimes') |
| 79 | } |
| 80 | |