open X11
+let font = font_load "Times New Roman:size=12"
+
let onfocus focused =
print_endline "onfocus"
let onupdate width height =
Printf.printf "onupdate: %d %d\n" width height;
- (*draw_rect { x = 0; y = 0; w = width; h = height; c = Cfg.Color.palette.(0) };*)
+ draw_rect { x = 0; y = 0; w = width; h = height; c = Cfg.Color.palette.(0) };
+ draw_string font Cfg.Color.palette.(5) "FooBarBaz" (0,0);
flip ()
let onshutdown () =
| Update e -> onupdate e.width e.height
| Shutdown -> onshutdown ()
-let test_glyph () =
- let font = X11.font_load "Monaco:size=10" in
- let glyph = X11.font_glyph font 0x30 in
- Printf.printf "%d %d %d %d %d %d %d\n"
- glyph.index
- glyph.rune
- glyph.width
- glyph.x
- glyph.y
- glyph.xoff
- glyph.yoff
-
let () =
let win = make_window 640 480 in
- test_glyph ();
show_window win true;
event_loop 50 onevent
external draw_rect : xrect -> unit
= "x11_draw_rect"
-(*
-external draw_glyphs : glyph array -> unit
- = "x11_draw_glyphs"
-*)
external event_loop : int -> (xevent -> unit) -> unit
= "x11_event_loop"
external font_glyph : font -> int -> glyph
= "x11_font_glyph"
+external draw_glyph : int -> glyph -> (int * int) -> int
+ = "x11_draw_glyph"
+
+let draw_rune font color rune coord =
+ draw_glyph color (font_glyph font rune) coord
+
+let draw_char font color ch coord =
+ draw_rune font color (Char.code ch) coord
+
+let rec draw_stringi font color str coord index =
+ if index < (String.length str) then
+ let x,y = coord in
+ let ch = String.get str index in
+ let xoff = draw_char font color ch coord in
+ draw_stringi font color str (x + xoff, y) (index + 1)
+
+let draw_string font color str coord =
+ draw_stringi font color str coord 0
+
(* Automatically connect and disconnect to the display server *)
let () =
connect ();
CAMLreturn(glyph);
}
+CAMLprim value x11_draw_glyph(value color, value glyph, value coord) {
+ CAMLparam3(color, glyph, coord);
+ XftFont* font = (XftFont*)Field(glyph,0);
+ XftGlyphFontSpec spec = {
+ .font = font,
+ .glyph = intfield(glyph,1),
+ .x = intfield(coord,0),
+ .y = intfield(coord,1) + font->ascent
+ };
+ //printf("x: %d y: %d xoff: %d yoff: %d\n", spec.x, spec.y, intfield(glyph,6), intfield(glyph,7));
+ XftColor fgc;
+ xftcolor(&fgc, Int_val(color));
+ XftDrawGlyphFontSpec(X.xft, &fgc, &spec, 1);
+ XftColorFree(X.display, X.visual, X.colormap, &fgc);
+ CAMLreturn(Field(glyph,6)); // Return xOff so we can chain operations
+}
+
/* X11 Event Handlers and Utilities
******************************************************************************/
static char* readprop(Window win, Atom prop) {