From 7081940aa55a6adc9035a334d6ece9165b2c2588 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 11 Sep 2017 21:28:48 -0400 Subject: [PATCH] implemented glyph retrieval primitive for loaded font --- edit.ml | 13 +++++++++++++ lib/x11.ml | 7 ++++++- lib/x11_prims.c | 23 ++++++++++++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/edit.ml b/edit.ml index 40c16ea..afbbe9b 100644 --- a/edit.ml +++ b/edit.ml @@ -34,8 +34,21 @@ 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 ed7ed4a..aab9734 100644 --- a/lib/x11.ml +++ b/lib/x11.ml @@ -44,9 +44,14 @@ type font = } type glyph = { + font: xfont; + index: int; rune: int; width: int; - font: font; + x: int; + y: int; + xoff: int; + yoff: int; } external connect : unit -> unit diff --git a/lib/x11_prims.c b/lib/x11_prims.c index 00f3ca6..cfb9ab9 100644 --- a/lib/x11_prims.c +++ b/lib/x11_prims.c @@ -230,9 +230,26 @@ CAMLprim value x11_font_load(value fontname) { CAMLreturn(font); } -CAMLprim value x11_font_glyph(value name) { - CAMLparam1(name); - CAMLreturn(Val_unit); +CAMLprim value x11_font_glyph(value font, value rune) { + CAMLparam2(font, rune); + CAMLlocal1(glyph); + /* search for the rune in currently loaded fonts */ + FcChar32 codept = Int_val(rune); + XftFont* xfont = (XftFont*)Field(font, 0); + FT_UInt glyphidx = XftCharIndex(X.display, xfont, codept); + XGlyphInfo extents; + XftTextExtents32 (X.display, xfont, &codept, 1, &extents); + /* create the glyph structure */ + glyph = caml_alloc(8, 0); + Store_field(glyph, 0, (value)xfont); + Store_field(glyph, 1, Val_int(glyphidx)); + Store_field(glyph, 2, rune); + Store_field(glyph, 3, Val_int(extents.width)); + Store_field(glyph, 4, Val_int(extents.x)); + Store_field(glyph, 5, Val_int(extents.y)); + Store_field(glyph, 6, Val_int(extents.xOff)); + Store_field(glyph, 7, Val_int(extents.yOff)); + CAMLreturn(glyph); } /* X11 Event Handlers and Utilities -- 2.52.0