@@ -0,0 +1,121 @@
3+module Native.Graphics.Text where
6+import Native.Graphics.Color
7+import Graphics.Element
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];
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] = ' ';
29+ arr[i] = spaces.join('');
33+ if (arr[arr.length-1] === " ") {
34+ return arr.slice(0,-1) + ' ';
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, "<")
45+ .replace(/>/g, ">")
46+ .replace(/\n/g, "<br/>");
47+ var arr = str.split('<br/>');
48+ for (var i = arr.length; i--; ) {
49+ arr[i] = makeSpaces(arr[i]);
51+ return arr.join('<br/>');
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 + '>';
61+ function addStyle(style, value, text) {
62+ return "<span style='" + style + ":" + value + "'>" + text + "</span>";
65+ function typeface(name, text) {
66+ return addStyle('font-family', Elm.JavaScript.fromString(name), text);
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);
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);
81+ function link(href, text) {
82+ return "<a href='" + toText(href) + "'>" + text + "</a>";
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>'
90+ return A3(newElem, 0, 0, e);
95+ return position('left')(typeface('monospace', toText(show(v))));
98+ Elm.Native.Graphics.Text = {
102+ height : F2(height),
105+ underline : underline,
106+ overline : overline,
107+ strikeThrough : strikeThrough,
108+ monospace : function(text) { return typeface('monospace', text) },
109+ typeface : F2(typeface),
113+ justified : position('justify'),
114+ centered : position('center'),
115+ righted : position('right'),
116+ text : position('left'),