From 7b8d7aaea8f626c621b780f2455a1919da69b849 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 12 Sep 2017 13:56:27 -0400 Subject: [PATCH] Added logic for drawing strings to the window --- edit.ml | 18 ++++-------------- lib/x11.ml | 23 +++++++++++++++++++---- lib/x11_prims.c | 17 +++++++++++++++++ 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/edit.ml b/edit.ml index afbbe9b..203c280 100644 --- 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 diff --git a/lib/x11.ml b/lib/x11.ml index aab9734..6b43603 100644 --- a/lib/x11.ml +++ b/lib/x11.ml @@ -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 (); diff --git a/lib/x11_prims.c b/lib/x11_prims.c index cfb9ab9..9bcc87f 100644 --- a/lib/x11_prims.c +++ b/lib/x11_prims.c @@ -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) { -- 2.52.0