on ea4fe54 help sign in
121 lines 3.4 kB history raw
2/**
3module Native.Graphics.Text where
5import JavaScript
6import Native.Graphics.Color
7import Graphics.Element
8**/
10(function() {
11 'use strict';
13 function makeSpaces(s) {
14 if (s.length == 0) { return s; }
15 var arr = s.split('');
16 if (arr[0] == ' ') { arr[0] = " " }
17 for (var i = arr.length; --i; ) {
18 if (arr[i][0] == ' ' && arr[i-1] == ' ') {
19 arr[i-1] = arr[i-1] + arr[i];
20 arr[i] = '';
21 }
22 }
23 for (var i = arr.length; i--; ) {
24 if (arr[i].length > 1 && arr[i][0] == ' ') {
25 var spaces = arr[i].split('');
26 for (var j = spaces.length - 2; j >= 0; j -= 2) {
27 spaces[j] = ' ';
28 }
29 arr[i] = spaces.join('');
30 }
31 }
32 arr = arr.join('');
33 if (arr[arr.length-1] === " ") {
34 return arr.slice(0,-1) + ' ';
35 }
36 return arr;
37 }
39 function properEscape(str) {
40 if (str.length == 0) return str;
41 str = str //.replace(/&/g, "&")
42 .replace(/"/g, /*"*/ '"')
43 .replace(/'/g, /*'*/ "'")
44 .replace(/</g, "&#60;")
45 .replace(/>/g, "&#62;")
46 .replace(/\n/g, "<br/>");
47 var arr = str.split('<br/>');
48 for (var i = arr.length; i--; ) {
49 arr[i] = makeSpaces(arr[i]);
50 }
51 return arr.join('<br/>');
52 }
54 function toText(str) { return properEscape(Elm.JavaScript.fromString(str)); }
56 function addTag(tag) { return function(text) {
57 return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
58 }
59 }
61 function addStyle(style, value, text) {
62 return "<span style='" + style + ":" + value + "'>" + text + "</span>";
63 }
65 function typeface(name, text) {
66 return addStyle('font-family', Elm.JavaScript.fromString(name), text);
67 }
68 function size(px, text) { return addStyle('font-size', px + 'px', text) }
69 var header = addTag('h1');
70 function height(h, text) { return addStyle('font-size', hf+'em', text) }
71 function italic(text) { return addStyle('font-style', 'italic', text) }
72 var bold = addTag('b');
73 function color(c, text) {
74 return addStyle('color', Elm.Native.Graphics.Color.extract(c), text);
75 }
76 function underline(text) { return addStyle('text-decoration', 'underline', text) }
77 function overline(text) { return addStyle('text-decoration', 'overline', text) }
78 function strikeThrough(text) {
79 return addStyle('text-decoration', 'line-through', text);
80 }
81 function link(href, text) {
82 return "<a href='" + toText(href) + "'>" + text + "</a>";
83 }
85 var newElem = Elm.Graphics.Element.newElement;
86 function position(pos) { return function(text) {
87 var e = {ctor:'RawHtml',
88 _0: '<div style="padding:0;margin:0;text-align:'+pos+'">'+text+'</div>'
89 };
90 return A3(newElem, 0, 0, e);
91 }
92 }
94 function asText(v) {
95 return position('left')(typeface('monospace', toText(show(v))));
96 }
98 Elm.Native.Graphics.Text = {
99 toText: toText,
101 header : header,
102 height : F2(height),
103 italic : italic,
104 bold : bold,
105 underline : underline,
106 overline : overline,
107 strikeThrough : strikeThrough,
108 monospace : function(text) { return typeface('monospace', text) },
109 typeface : F2(typeface),
110 color : F2(color),
111 link : F2(link),
113 justified : position('justify'),
114 centered : position('center'),
115 righted : position('right'),
116 text : position('left'),
118 asText : asText
119 };
121}())