on 3540449 help sign in
123 lines 3.6 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 htmlHeight = Elm.Native.Utils(elm).htmlHeight;
11 var Color = Elm.Native.Graphics.Color(elm);
12 var Element = Elm.Graphics.Element(elm);
14 function makeSpaces(s) {
15 if (s.length == 0) { return s; }
16 var arr = s.split('');
17 if (arr[0] == ' ') { arr[0] = " " }
18 for (var i = arr.length; --i; ) {
19 if (arr[i][0] == ' ' && arr[i-1] == ' ') {
20 arr[i-1] = arr[i-1] + arr[i];
21 arr[i] = '';
22 }
23 }
24 for (var i = arr.length; i--; ) {
25 if (arr[i].length > 1 && arr[i][0] == ' ') {
26 var spaces = arr[i].split('');
27 for (var j = spaces.length - 2; j >= 0; j -= 2) {
28 spaces[j] = ' ';
29 }
30 arr[i] = spaces.join('');
31 }
32 }
33 arr = arr.join('');
34 if (arr[arr.length-1] === " ") {
35 return arr.slice(0,-1) + ' ';
36 }
37 return arr;
38 }
40 function properEscape(str) {
41 if (str.length == 0) return str;
42 str = str //.replace(/&/g, "&")
43 .replace(/"/g, /*"*/ '"')
44 .replace(/'/g, /*'*/ "'")
45 .replace(/</g, "&#60;")
46 .replace(/>/g, "&#62;")
47 .replace(/\n/g, "<br/>");
48 var arr = str.split('<br/>');
49 for (var i = arr.length; i--; ) {
50 arr[i] = makeSpaces(arr[i]);
51 }
52 return arr.join('<br/>');
53 }
55 function toText(str) { return properEscape(JS.fromString(str)); }
57 function addTag(tag) { return function(text) {
58 return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
59 }
60 }
62 function addStyle(style, value, text) {
63 return "<span style='" + style + ":" + value + "'>" + text + "</span>";
64 }
66 function typeface(name, text) {
67 return addStyle('font-family', JS.fromString(name), text);
68 }
69 function size(px, text) { return addStyle('font-size', px + 'px', text) }
70 var header = addTag('h1');
71 function height(h, text) { return addStyle('font-size', hf+'em', text) }
72 function italic(text) { return addStyle('font-style', 'italic', text) }
73 var bold = addTag('b');
74 function color(c, text) {
75 return addStyle('color', Color.extract(c), text);
76 }
77 function underline(text) { return addStyle('text-decoration', 'underline', text) }
78 function overline(text) { return addStyle('text-decoration', 'overline', text) }
79 function strikeThrough(text) {
80 return addStyle('text-decoration', 'line-through', text);
81 }
82 function link(href, text) {
83 return "<a href='" + toText(href) + "'>" + text + "</a>";
84 }
86 function position(pos) { return function(text) {
87 var e = {ctor:'RawHtml',
88 _0: '<div style="padding:0;margin:0;text-align:' +
89 pos + '">' + text + '</div>'
90 };
91 var w = elm.node.clientWidth;
92 return A3(Element.newElement, w, A2(htmlHeight, w, text), e);
93 }
94 }
96 function asText(v) {
97 return position('left')(typeface('monospace', toText(show(v))));
98 }
100 return elm.Native.Graphics.Text = {
101 toText: toText,
103 header : header,
104 height : F2(height),
105 italic : italic,
106 bold : bold,
107 underline : underline,
108 overline : overline,
109 strikeThrough : strikeThrough,
110 monospace : function(text) { return typeface('monospace', text) },
111 typeface : F2(typeface),
112 color : F2(color),
113 link : F2(link),
115 justified : position('justify'),
116 centered : position('center'),
117 righted : position('right'),
118 text : position('left'),
120 asText : asText
121 };
123};