on master help sign in
Get Text module working. Move Color into Graphics/. Have Element as a parent module of Text, Form, etc. providing the raw materials but letting functions like `collage : ... -> Element` and `centered : Text -> Element` live in dependencies.
commit0fbfab7d7cf07c3d3cbb5be9a6af42187878e70a authorevancz <info@elm-lang.org> dateTue 5 Mar 2013, 10:06 (14 years ago) parentd2abba0
7 files changed+165−92
core/Graphics/Color.elm+4−4 core/Graphics/Element.elm+9−23 core/Graphics/Text.elm+20−13 core/Native/Error.js+11 core/Native/Color.js → core/Native/Graphics/Color.js core/Native/Graphics/Text.js+121 core/Native/Misc.js−52
core/Graphics/Color.elm+4−4
@@ -8,7 +8,7 @@ module Graphics.Color (rgba,rgb,hsva,hsv,
88 violet,forestGreen,
99 black,white,gray,grey) where
1010
11−import Native.Color
11+import Native.Graphics.Color as C
1212
1313data Color = Color Int Int Int Float
1414
@@ -39,6 +39,6 @@ purple = Color 128 0 128 1
3939forestGreen = Color 34 139 34 1
4040violet = Color 238 130 238 1
4141
42−complement = Native.Color.complement
43−hsva = Native.Color.hsva
44−hsv = Native.Color.hsv
42+complement = C.complement
43+hsva = C.hsva
44+hsv = C.hsv
core/Graphics/Element.elm+9−23
@@ -23,27 +23,15 @@ widthOf e = getProp .width e
2323heightOf e = getProp .height e
2424sizeOf e = getProp (\p -> (p.width, p.height)) e
2525
26−width w e =
27− case e of
28− Element props prim -> Element {props| width <- w} prim
29−height h e =
30− case e of
31− Element props prim -> Element {props| height <- h} prim
32−opacity o e =
33− case e of
34− Element props prim -> Element {props| opacity <- o} prim
35−color c e =
36− case e of
37− Element props prim -> Element {props| filling <- c} prim
38−filling f e =
39− case e of
40− Element props prim -> Element {props| filling <- f} prim
41−tag name e =
42− case e of
43− Element props prim -> Element {props| tag <- JS.fromString name } prim
44−link href e =
45− case e of
46− Element props prim -> Element {props| href <- JS.fromString href } prim
26+width w e = case e of Element props prim -> Element {props| width <- w} prim
27+height h e = case e of Element props prim -> Element {props| height <- h} prim
28+opacity o e = case e of Element props prim -> Element {props| opacity <- o} prim
29+color c e = case e of Element props prim -> Element {props| filling <- c} prim
30+filling f e = case e of Element props prim -> Element {props| filling <- f} prim
31+tag name e = case e of Element props prim ->
32+ Element {props| tag <- JS.fromString name } prim
33+link href e = case e of Element props prim ->
34+ Element {props| href <- JS.fromString href } prim
4735
4836emptyStr = JS.fromString ""
4937newElement w h e =
@@ -83,5 +71,3 @@ flow dir es =
8371above hi lo = newElement (max (widthOf hi) (widthOf lo)) (heightOf hi + heightOf lo) (Flow DDown [hi,lo])
8472below lo hi = newElement (max (widthOf hi) (widthOf lo)) (heightOf hi + heightOf lo) (Flow DDown [hi,lo])
8573beside lft rht = newElement (widthOf lft + widthOf rht) (max (heightOf lft) (heightOf rht)) (Flow right [lft,rht])
86−
87−text : Text -> Element
core/Graphics/Text.elm+20−13
@@ -1,17 +1,24 @@
11
22module Graphics.Text where
33
4−import Graphics.Color
5−import JavaScript
4+import Native.Graphics.Text as T
65
7−type Style = {
8− typeface : JSString,
9− href : JSString,
10− height : Float,
11− color : Color,
12− bold : Bool,
13− italic : Bool,
14− overline : Bool,
15− underline : Bool,
16− strikeThrough : Bool
17−}
6+
7+toText : String -> Text
8+
9+typeface : String -> Text -> Text
10+href : String -> Text -> Text
11+height : Float -> Text -> Text
12+color : Color -> Text -> Text
13+bold : Text -> Text
14+italic : Text -> Text
15+overline : Text -> Text
16+underline : Text -> Text
17+strikeThrough : Text -> Text
18+
19+justified : Text -> Element
20+centered : Text -> Element
21+righted : Text -> Element
22+text : Text -> Element
23+
24+asText : a -> Element
core/Native/Error.jsnew file+11
@@ -0,0 +1,11 @@
1+
2+Elm.Error = {
3+ Case : function(span) {
4+ var msg = 'Non-exhaustive pattern match in case expression'
5+ throw new Error(msg + " (" + span + ")")
6+ }
7+ If : function(span) {
8+ var msg = 'Non-exhaustive pattern match in multi-way-if expression'
9+ throw new Error(msg + " (" + span + ")")
10+ }
11+};
core/Native/Color.js → core/Native/Graphics/Color.jsrenamed

The content is the same.

core/Native/Graphics/Text.jsnew file+121
@@ -0,0 +1,121 @@
1+
2+/**
3+module Native.Graphics.Text where
4+
5+import JavaScript
6+import Native.Graphics.Color
7+import Graphics.Element
8+**/
9+
10+(function() {
11+ 'use strict';
12+
13+ function makeSpaces(s) {
14+ if (s.length == 0) { return s; }
15+ var arr = s.split('');
16+ if (arr[0] == ' ') { arr[0] = "&nbsp;" }
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] = '&nbsp;';
28+ }
29+ arr[i] = spaces.join('');
30+ }
31+ }
32+ arr = arr.join('');
33+ if (arr[arr.length-1] === " ") {
34+ return arr.slice(0,-1) + '&nbsp;';
35+ }
36+ return arr;
37+ }
38+
39+ function properEscape(str) {
40+ if (str.length == 0) return str;
41+ str = str //.replace(/&/g, "&#38;")
42+ .replace(/"/g, /*"*/ '&#34;')
43+ .replace(/'/g, /*'*/ "&#39;")
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+ }
53+
54+ function toText(str) { return properEscape(Elm.JavaScript.fromString(str)); }
55+
56+ function addTag(tag) { return function(text) {
57+ return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
58+ }
59+ }
60+
61+ function addStyle(style, value, text) {
62+ return "<span style='" + style + ":" + value + "'>" + text + "</span>";
63+ }
64+
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+ }
84+
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+ }
93+
94+ function asText(v) {
95+ return position('left')(typeface('monospace', toText(show(v))));
96+ }
97+
98+ Elm.Native.Graphics.Text = {
99+ toText: toText,
100+
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),
112+
113+ justified : position('justify'),
114+ centered : position('center'),
115+ righted : position('right'),
116+ text : position('left'),
117+
118+ asText : asText
119+ };
120+
121+}())
core/Native/Misc.js−52
@@ -32,57 +32,6 @@ import JSON
3232 var Tuple0 = { ctor: "Tuple0" }
3333 function Tuple2(x,y) { return { ctor = "Tuple2", _0:x, _1:y } }
3434
35− function makeSpaces(s) {
36− if (s.length == 0) { return s; }
37− var arr = s.split('');
38− if (arr[0] == ' ') { arr[0] = "&nbsp;" }
39− for (var i = arr.length; --i; ) {
40− if (arr[i][0] == ' ' && arr[i-1] == ' ') {
41− arr[i-1] = arr[i-1] + arr[i];
42− arr[i] = '';
43− }
44− }
45− for (var i = arr.length; i--; ) {
46− if (arr[i].length > 1 && arr[i][0] == ' ') {
47− var spaces = arr[i].split('');
48− for (var j = spaces.length - 2; j >= 0; j -= 2) {
49− spaces[j] = '&nbsp;';
50− }
51− arr[i] = spaces.join('');
52− }
53− }
54− arr = arr.join('');
55− if (arr[arr.length-1] === " ") {
56− return arr.slice(0,-1) + '&nbsp;';
57− }
58− return arr;
59− }
60−
61− function properEscape(str) {
62− if (str.length == 0) return str;
63− str = str //.replace(/&/g, "&#38;")
64− .replace(/"/g, /*"*/ "&#34;")
65− .replace(/'/g, /*'*/ "&#39;")
66− .replace(/</g, "&#60;")
67− .replace(/>/g, "&#62;")
68− .replace(/\n/g, "<br/>");
69− var arr = str.split('<br/>');
70− for (var i = arr.length; i--; ) {
71− arr[i] = makeSpaces(arr[i]);
72− }
73− return arr.join('<br/>');
74− }
75−
76− var toText = function(elmList) {
77− if (typeof elmList === "string") return properEscape(elmList);
78− var a = [];
79− while (elmList.ctor === "Cons") {
80− a.push(elmList._0);
81− elmList = elmList._1;
82− }
83− return properEscape(a.join(''));
84− };
85−
8635 function getTextSize(w,h,txt) {
8736 var t = document.createElement('div');
8837 t.innerHTML = txt;
@@ -248,7 +197,6 @@ import JSON
248197 Tuple0:Tuple0,
249198 Tuple2:Tuple2,
250199 toText : toText,
251− properEscape : properEscape,
252200 getTextSize : getTextSize,
253201 getSize : getSize,
254202 getExcess : getExcess,