-
Notifications
You must be signed in to change notification settings - Fork 194
implement a wrapper of CSS Font Loading API #1833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
open Js_of_ocaml | ||
|
||
let is_loading = ref true | ||
|
||
let render_loop (context : Dom_html.canvasRenderingContext2D Js.t) = | ||
let next_time = Js.float 15. in | ||
let rec inner () = | ||
context##save; | ||
context##.fillStyle := Js.string "rgb(0, 0, 0)"; | ||
context##fillRect (Js.float 0.) (Js.float 0.) (Js.float 800.) (Js.float 600.); | ||
context##.fillStyle := Js.string "rgb(255,255,255)"; | ||
context##.textBaseline := Js.string "top"; | ||
context##.lineWidth := Js.float 3.; | ||
if !is_loading | ||
then ( | ||
context##.font := Js.string "48px caption"; | ||
context##fillText (Js.string "Loading") (Js.float 0.) (Js.float 0.)) | ||
else ( | ||
context##.font := Js.string "48px caption"; | ||
context##fillText (Js.string "ABCDEFG") (Js.float 0.) (Js.float 0.); | ||
context##.font := Js.string "48px 'Bebas Neue', caption"; | ||
context##fillText (Js.string "ABCDEFG") (Js.float 0.) (Js.float 50.)); | ||
context##restore; | ||
ignore @@ Dom_html.window##setTimeout (Js.wrap_callback inner) next_time | ||
in | ||
inner () | ||
|
||
let _ = | ||
let canvas = | ||
Option.get @@ Dom_html.getElementById_coerce "main-canvas" Dom_html.CoerceTo.canvas | ||
in | ||
let context = canvas##getContext Dom_html._2d_ in | ||
let bebas = | ||
Css_font.create_font_face | ||
(Js.string "Bebas Neue") | ||
(Js.string "url(BebasNeue-Regular.ttf)") | ||
in | ||
Css_font.load_and_then bebas (fun () -> is_loading := false); | ||
Dom_html.document##.fonts##add bebas; | ||
render_loop context |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
(executables | ||
(names css_font_load) | ||
(libraries js_of_ocaml) | ||
(modes js) | ||
(preprocess | ||
(pps js_of_ocaml-ppx))) | ||
|
||
(alias | ||
(name default) | ||
(deps css_font_load.bc.js index.html)) | ||
|
||
(alias | ||
(name default) | ||
(deps BebasNeue-Regular.ttf)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" | ||
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<title>CSS FONT LOAD</title> | ||
<link rel="preconnect" href="https://fonts.googleapis.com"> | ||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | ||
<script type="text/javascript" src="css_font_load.bc.js" defer></script> | ||
</head> | ||
<body> | ||
<canvas id="main-canvas" width="800" height="600"></canvas> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
(* Js_of_ocaml library | ||
* http://www.ocsigen.org/js_of_ocaml/ | ||
* Copyright (C) 2014 Hugo Heuzard | ||
* Copyright (C) 2014 Jérôme Vouillon | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, with linking exception; | ||
* either version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
*) | ||
|
||
(** CSS Font Loading API binding | ||
|
||
This is a partial binding to the CSS Font Loading API | ||
*) | ||
|
||
open Js | ||
|
||
class type fontFaceElement = object | ||
method family : js_string t readonly_prop | ||
|
||
method style : js_string t readonly_prop | ||
|
||
method weight : js_string t readonly_prop | ||
|
||
method stretch : js_string t readonly_prop | ||
|
||
method unicodeRange : js_string t readonly_prop | ||
|
||
method variant : js_string t readonly_prop | ||
|
||
method featureSettings : js_string t readonly_prop | ||
|
||
method display : js_string t readonly_prop | ||
|
||
method src : js_string t readonly_prop | ||
|
||
method status : js_string t readonly_prop | ||
|
||
method load : unit -> 'a meth | ||
end | ||
|
||
class type fontFaceSet = object | ||
method add : fontFaceElement Js.t -> unit meth | ||
|
||
method check : js_string t -> js_string t -> bool meth | ||
|
||
method delete : fontFaceElement Js.t -> unit meth | ||
|
||
method load : unit -> 'a meth | ||
|
||
method ready : bool readonly_prop | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
method status : js_string t readonly_prop | ||
end | ||
|
||
let constr : (js_string t -> js_string t -> fontFaceElement t) Js.constr = | ||
Unsafe.pure_js_expr "FontFace" | ||
|
||
let create_font_face (family : js_string Js.t) (source : js_string Js.t) = | ||
new%js constr family source | ||
|
||
let load_and_then (font_face : fontFaceElement Js.t) (f : unit -> unit) = | ||
let f = Unsafe.inject @@ Js.wrap_meth_callback f in | ||
let js_promise = font_face##load () in | ||
Unsafe.meth_call js_promise "then" [| Unsafe.coerce f |] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
(* Js_of_ocaml library | ||
* http://www.ocsigen.org/js_of_ocaml/ | ||
* Copyright (C) 2014 Hugo Heuzard | ||
* Copyright (C) 2014 Jérôme Vouillon | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, with linking exception; | ||
* either version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
*) | ||
|
||
(** CSS Font Loading API binding | ||
|
||
This is a partial binding to the CSS Font Loading API | ||
*) | ||
|
||
open Js | ||
|
||
class type fontFaceElement = object | ||
method family : js_string t readonly_prop | ||
|
||
method style : js_string t readonly_prop | ||
|
||
method weight : js_string t readonly_prop | ||
|
||
method stretch : js_string t readonly_prop | ||
|
||
method unicodeRange : js_string t readonly_prop | ||
|
||
method variant : js_string t readonly_prop | ||
|
||
method featureSettings : js_string t readonly_prop | ||
|
||
method display : js_string t readonly_prop | ||
|
||
method src : js_string t readonly_prop | ||
|
||
method status : js_string t readonly_prop | ||
|
||
method load : unit -> 'a meth | ||
end | ||
|
||
class type fontFaceSet = object | ||
method add : fontFaceElement Js.t -> unit meth | ||
|
||
method check : js_string t -> js_string t -> bool meth | ||
|
||
method delete : fontFaceElement Js.t -> unit meth | ||
|
||
method load : unit -> 'a meth | ||
|
||
method ready : bool readonly_prop | ||
|
||
method status : js_string t readonly_prop | ||
end | ||
|
||
val create_font_face : js_string t -> js_string t -> fontFaceElement t | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we allow users to provide an optional descriptors as defined in https://developer.mozilla.org/en-US/docs/Web/API/FontFace/FontFace ? |
||
|
||
val load_and_then : fontFaceElement Js.t -> (unit -> unit) -> unit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this documented ?