Skip to content

Commit

Permalink
font-manager: add option API
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoRFS committed Jun 23, 2020
1 parent 505fde9 commit 338d44e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 55 deletions.
101 changes: 55 additions & 46 deletions src/reason-font-manager/FontManager.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
#include <stdio.h>
#include <caml/alloc.h>
#include <caml/bigarray.h>
#include <caml/callback.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>
#include <caml/threads.h>
#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 <caml/alloc.h>
#include <caml/bigarray.h>
#include <caml/callback.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>
#include <caml/threads.h>
#include <stdio.h>

#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);
}
}
38 changes: 29 additions & 9 deletions src/reason-font-manager/FontManager.re
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
};
};
25 changes: 25 additions & 0 deletions src/reason-font-manager/FontManager.rei
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 338d44e

Please sign in to comment.