Skip to content
Browse files

[enhance] canvas: Text-related functions

  • Loading branch information...
1 parent 836bb0d commit c8a5055cba2718eea9cff49d556d207e57df8d9b Nicolas Glondu committed Nov 8, 2011
Showing with 82 additions and 2 deletions.
  1. +36 −1 plugins/browser_canvas/bslCanvas.js
  2. +46 −1 stdlib/web/canvas/canvas.opa
View
37 plugins/browser_canvas/bslCanvas.js
@@ -434,7 +434,42 @@ function bslcanvas_create_pattern(context, image, repeat)
//todo
// text
-//todo
+
+##register set_font: Canvas.context, string -> void
+##args(context, fontText)
+{
+ context.font = fontText
+}
+
+##register set_text_align: Canvas.context, string -> void
+##args(context, alignText)
+{
+ context.textAlign = alignText
+}
+
+##register set_text_baseline: Canvas.context, string -> void
+##args(context, baselineText)
+{
+ context.textBaseline = baselineText
+}
+
+##register stroke_text: Canvas.context, string, int, int -> void
+##args(context, text, x, y)
+{
+ context.strokeText(text, x, y)
+}
+
+##register fill_text: Canvas.context, string, int, int -> void
+##args(context, text, x, y)
+{
+ context.fillText(text, x, y)
+}
+
+##register measure_text: Canvas.context, string -> int
+##args(context, text)
+{
+ return context.measureText(text).width
+}
// drawing images
View
47 stdlib/web/canvas/canvas.opa
@@ -83,6 +83,21 @@ type Image.image = external
type Video.video = external
type Image.data = external
+type Canvas.textalign =
+ {align_start}
+/ {align_end}
+/ {align_left}
+/ {align_right}
+/ {align_center}
+
+type Canvas.textbaseline =
+ {top}
+/ {hanging}
+/ {middle}
+/ {alphabetic}
+/ {ideographic}
+/ {bottom}
+
/**
* {1 Interface}
*/
@@ -433,7 +448,37 @@ Canvas = {{
* {2 Text}
**/
- // TODO
+ // TODO: type font
+ set_font(context:Canvas.context, font:string) : void =
+ %% BslCanvas.set_font %%(context,font)
+
+ set_text_align(context:Canvas.context, align:Canvas.textalign) : void =
+ align_text = match align with
+ | {align_start} -> "start"
+ | {align_end} -> "end"
+ | {align_left} -> "left"
+ | {align_right} -> "right"
+ | {align_center} -> "center"
+ %% BslCanvas.set_text_align %%(context,align_text)
+
+ set_text_baseline(context:Canvas.context, baseline:Canvas.textbaseline) : void =
+ baseline_text = match baseline with
+ | {top} -> "top"
+ | {hanging} -> "hanging"
+ | {middle} -> "middle"
+ | {alphabetic} -> "alphabetic"
+ | {ideographic} -> "ideographic"
+ | {bottom} -> "bottom"
+ %% BslCanvas.set_text_baseline %%(context,baseline_text)
+
+ stroke_text(context:Canvas.context, text:string, x:int, y:int) : void =
+ %% BslCanvas.stroke_text %%(context,text,x,y)
+
+ fill_text(context:Canvas.context, text:string, x:int, y:int) : void =
+ %% BslCanvas.fill_text %%(context,text,x,y)
+
+ measure_text(context:Canvas.context, text:string) : int =
+ %% BslCanvas.measure_text %%(context,text)
/**
* {2 Drawing images}

0 comments on commit c8a5055

Please sign in to comment.
Something went wrong with that request. Please try again.