on 8a700e8 help sign in
120 lines 3.5 kB history raw
2Elm.Native.Graphics.Text = function(elm) {
3 'use strict';
5 elm.Native = elm.Native || {};
6 elm.Native.Graphics = elm.Native.Graphics || {};
7 if (elm.Native.Graphics.Text) return elm.Native.Graphics.Text;
9 var JS = Elm.JavaScript(elm);
10 var Color = Elm.Native.Graphics.Color(elm);
11 var Element = Elm.Graphics.Element(elm);
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(JS.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', JS.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', 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 function position(pos) { return function(text) {
86 var e = {ctor:'RawHtml',
87 _0: '<div style="padding:0;margin:0;text-align:'+pos+'">'+text+'</div>'
88 };
89 return A3(Element.newElement, 0, 0, e);
90 }
91 }
93 function asText(v) {
94 return position('left')(typeface('monospace', toText(show(v))));
95 }
97 return elm.Native.Graphics.Text = {
98 toText: toText,
100 header : header,
101 height : F2(height),
102 italic : italic,
103 bold : bold,
104 underline : underline,
105 overline : overline,
106 strikeThrough : strikeThrough,
107 monospace : function(text) { return typeface('monospace', text) },
108 typeface : F2(typeface),
109 color : F2(color),
110 link : F2(link),
112 justified : position('justify'),
113 centered : position('center'),
114 righted : position('right'),
115 text : position('left'),
117 asText : asText
118 };
120};