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