From 338d44ef147198b49b18b8d25091cf0403c00100 Mon Sep 17 00:00:00 2001 From: EduardoRFS Date: Tue, 23 Jun 2020 07:05:06 -0300 Subject: [PATCH] font-manager: add option API --- src/reason-font-manager/FontManager.cpp | 101 +++++++++++++----------- src/reason-font-manager/FontManager.re | 38 ++++++--- src/reason-font-manager/FontManager.rei | 25 ++++++ 3 files changed, 109 insertions(+), 55 deletions(-) create mode 100644 src/reason-font-manager/FontManager.rei diff --git a/src/reason-font-manager/FontManager.cpp b/src/reason-font-manager/FontManager.cpp index 6c17fe92c..e44f52729 100644 --- a/src/reason-font-manager/FontManager.cpp +++ b/src/reason-font-manager/FontManager.cpp @@ -1,46 +1,55 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "FontDescriptor.h" - -extern "C" { - extern FontDescriptor *findFont(FontDescriptor *query); - extern ResultSet *findFonts(FontDescriptor *query); - - CAMLprim value fm_findFont(value family, value weight, value width, value italic, value monospace) { - CAMLparam5(family, weight, width, italic, monospace); - CAMLlocal1(ret); - - const char* fontFamily = String_val(family); - FontWeight weightToCheck = (FontWeight)Int_val(weight); - FontWidth widthToCheck = (FontWidth)Int_val(width); - int isItalic = Bool_val(italic); - int isMono = Bool_val(monospace); - - caml_release_runtime_system(); - FontDescriptor *query = new FontDescriptor(NULL, NULL, fontFamily, NULL, weightToCheck, widthToCheck, isItalic, isMono); - - FontDescriptor *font = findFont(query); - delete query; - - caml_acquire_runtime_system(); - - ret = caml_alloc(7, 0); - Store_field(ret, 0, caml_copy_string(font->path)); - Store_field(ret, 1, caml_copy_string(font->postscriptName)); - Store_field(ret, 2, caml_copy_string(font->family)); - Store_field(ret, 3, Val_int(font->weight)); - Store_field(ret, 4, Val_int(font->width)); - Store_field(ret, 5, Val_bool(font->italic)); - Store_field(ret, 6, Val_bool(font->monospace)); - - delete font; - - CAMLreturn(ret); - } -} +#include +#include +#include +#include +#include +#include +#include +#include + +#include "FontDescriptor.h" + +extern "C" { + +extern FontDescriptor *findFont(FontDescriptor *query); +extern ResultSet *findFonts(FontDescriptor *query); + +CAMLprim value fm_findFont(value family, value weight, value width, value italic, value monospace) { + CAMLparam5(family, weight, width, italic, monospace); + CAMLlocal1(ret); + + const char *fontFamily = String_val(family); + FontWeight weightToCheck = (FontWeight)Int_val(weight); + FontWidth widthToCheck = (FontWidth)Int_val(width); + int isItalic = Bool_val(italic); + int isMono = Bool_val(monospace); + + caml_release_runtime_system(); + + FontDescriptor *query = new FontDescriptor(NULL, NULL, fontFamily, NULL, weightToCheck, widthToCheck, isItalic, isMono); + FontDescriptor *font = findFont(query); + + delete query; + + if (font == NULL) { + caml_raise_not_found(); + return Val_unit; + } + + caml_acquire_runtime_system(); + + ret = caml_alloc(7, 0); + + Store_field(ret, 0, caml_copy_string(font->path)); + Store_field(ret, 1, caml_copy_string(font->postscriptName)); + Store_field(ret, 2, caml_copy_string(font->family)); + Store_field(ret, 3, Val_int(font->weight)); + Store_field(ret, 4, Val_int(font->width)); + Store_field(ret, 5, Val_bool(font->italic)); + Store_field(ret, 6, Val_bool(font->monospace)); + + delete font; + + CAMLreturn(ret); +} +} diff --git a/src/reason-font-manager/FontManager.re b/src/reason-font-manager/FontManager.re index 05b578194..f4c702710 100644 --- a/src/reason-font-manager/FontManager.re +++ b/src/reason-font-manager/FontManager.re @@ -2,9 +2,33 @@ module FontDescriptor = FontDescriptor; module FontWeight = FontWeight; module FontWidth = FontWidth; -external _findFont: (string, int, int, bool, bool) => FontDescriptor.raw = +external findFont': (string, int, int, bool, bool) => FontDescriptor.raw = "fm_findFont"; +let findFontOpt = + ( + ~weight=FontWeight.Normal, + ~width=FontWidth.Undefined, + ~family: string, + ~italic: bool, + ~mono: bool, + (), + ) => { + switch ( + findFont'( + family, + FontWeight.toInt(weight), + FontWidth.toInt(width), + italic, + mono, + ) + ) { + | exception Not_found => None + | raw => Some(FontDescriptor.ofRaw(raw)) + }; +}; + +exception Font_not_found; let findFont = ( ~weight=FontWeight.Normal, @@ -14,12 +38,8 @@ let findFont = ~mono: bool, (), ) => { - _findFont( - family, - FontWeight.toInt(weight), - FontWidth.toInt(width), - italic, - mono, - ) - |> FontDescriptor.ofRaw; + switch (findFontOpt(~weight, ~width, ~family, ~italic, ~mono, ())) { + | None => raise(Font_not_found) + | Some(fontDescriptor) => fontDescriptor + }; }; diff --git a/src/reason-font-manager/FontManager.rei b/src/reason-font-manager/FontManager.rei new file mode 100644 index 000000000..2998f0e0b --- /dev/null +++ b/src/reason-font-manager/FontManager.rei @@ -0,0 +1,25 @@ +module FontDescriptor = FontDescriptor; +module FontWeight = FontWeight; +module FontWidth = FontWidth; + +exception Font_not_found; +let findFont: + ( + ~weight: FontWeight.t=?, + ~width: FontWidth.t=?, + ~family: string, + ~italic: bool, + ~mono: bool, + unit + ) => + FontDescriptor.t; +let findFontOpt: + ( + ~weight: FontWeight.t=?, + ~width: FontWidth.t=?, + ~family: string, + ~italic: bool, + ~mono: bool, + unit + ) => + option(FontDescriptor.t);