]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
Added logic for drawing strings to the window
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 12 Sep 2017 17:56:27 +0000 (13:56 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 12 Sep 2017 17:56:27 +0000 (13:56 -0400)
edit.ml
lib/x11.ml
lib/x11_prims.c

diff --git a/edit.ml b/edit.ml
index afbbe9b09938484750f652fea121798c4c1a8b55..203c28057f715301ad8d36f5a902513b259cc712 100644 (file)
--- a/edit.ml
+++ b/edit.ml
@@ -1,5 +1,7 @@
 open X11
 
+let font = font_load "Times New Roman:size=12"
+
 let onfocus focused =
   print_endline "onfocus"
 
@@ -14,7 +16,8 @@ let onmousemove mods x y =
 
 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 () =
@@ -34,21 +37,8 @@ let onevent = function
   | 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
 
index aab97343dc9fa1e970b76ee3e540ebe27dfc3ad5..6b4360357ce651fc9ae3d1d009e0789dd0d30254 100644 (file)
@@ -74,10 +74,6 @@ external flip : unit -> unit
 
 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"
@@ -108,6 +104,25 @@ external font_load : string -> font
 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 ();
index cfb9ab97b0d1b510f03cc28b718168bd0e297581..9bcc87f07b0c4878fb8dd100b57bbe6b163bf559 100644 (file)
@@ -252,6 +252,23 @@ CAMLprim value x11_font_glyph(value font, value rune) {
     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) {