author | wenzelm |
Tue, 18 Sep 2012 14:51:12 +0200 | |
changeset 49416 | 1053a564dd25 |
parent 49414 | d7b5fb2e9ca2 |
child 49468 | 8b06b99fb85c |
permissions | -rw-r--r-- |
36683 | 1 |
/* Title: Pure/General/pretty.scala |
2 |
Author: Makarius |
|
3 |
||
36687 | 4 |
Generic pretty printing module. |
36683 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
36820 | 10 |
import java.awt.FontMetrics |
11 |
||
12 |
||
36683 | 13 |
object Pretty |
14 |
{ |
|
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
15 |
/* spaces */ |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
16 |
|
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
17 |
val spc = ' ' |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
18 |
val space = " " |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
19 |
|
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
20 |
private val static_spaces = space * 4000 |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
21 |
|
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
22 |
def spaces(k: Int): String = |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
23 |
{ |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
24 |
require(k >= 0) |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
25 |
if (k < static_spaces.length) static_spaces.substring(0, k) |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
26 |
else space * k |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
27 |
} |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
28 |
|
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
29 |
|
36687 | 30 |
/* markup trees with physical blocks and breaks */ |
31 |
||
38573 | 32 |
def block(body: XML.Body): XML.Tree = Block(2, body) |
33 |
||
36683 | 34 |
object Block |
35 |
{ |
|
38573 | 36 |
def apply(i: Int, body: XML.Body): XML.Tree = |
45666 | 37 |
XML.Elem(Markup(Isabelle_Markup.BLOCK, Isabelle_Markup.Indent(i)), body) |
36683 | 38 |
|
38573 | 39 |
def unapply(tree: XML.Tree): Option[(Int, XML.Body)] = |
36683 | 40 |
tree match { |
45666 | 41 |
case XML.Elem(Markup(Isabelle_Markup.BLOCK, Isabelle_Markup.Indent(i)), body) => |
42 |
Some((i, body)) |
|
36683 | 43 |
case _ => None |
44 |
} |
|
45 |
} |
|
46 |
||
47 |
object Break |
|
48 |
{ |
|
38414
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38230
diff
changeset
|
49 |
def apply(w: Int): XML.Tree = |
45666 | 50 |
XML.Elem(Markup(Isabelle_Markup.BREAK, Isabelle_Markup.Width(w)), |
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
51 |
List(XML.Text(spaces(w)))) |
36683 | 52 |
|
53 |
def unapply(tree: XML.Tree): Option[Int] = |
|
54 |
tree match { |
|
45666 | 55 |
case XML.Elem(Markup(Isabelle_Markup.BREAK, Isabelle_Markup.Width(w)), _) => Some(w) |
36683 | 56 |
case _ => None |
57 |
} |
|
58 |
} |
|
59 |
||
36689
379f5b1e7f91
replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents:
36687
diff
changeset
|
60 |
val FBreak = XML.Text("\n") |
36687 | 61 |
|
62 |
||
63 |
/* formatted output */ |
|
64 |
||
38573 | 65 |
private def standard_format(tree: XML.Tree): XML.Body = |
36689
379f5b1e7f91
replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents:
36687
diff
changeset
|
66 |
tree match { |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
37374
diff
changeset
|
67 |
case XML.Elem(markup, body) => List(XML.Elem(markup, body.flatMap(standard_format))) |
48996 | 68 |
case XML.Text(text) => Library.separate(FBreak, split_lines(text).map(XML.Text)) |
36689
379f5b1e7f91
replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents:
36687
diff
changeset
|
69 |
} |
379f5b1e7f91
replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents:
36687
diff
changeset
|
70 |
|
45251 | 71 |
private sealed case class Text(tx: XML.Body = Nil, val pos: Double = 0.0, val nl: Int = 0) |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
72 |
{ |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
73 |
def newline: Text = copy(tx = FBreak :: tx, pos = 0.0, nl = nl + 1) |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
74 |
def string(s: String, len: Double): Text = copy(tx = XML.Text(s) :: tx, pos = pos + len) |
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
75 |
def blanks(wd: Int): Text = string(spaces(wd), wd.toDouble) |
38573 | 76 |
def content: XML.Body = tx.reverse |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
77 |
} |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
78 |
|
36745 | 79 |
private val margin_default = 76 |
36818 | 80 |
private def metric_default(s: String) = s.length.toDouble |
36734
d9b10c173330
Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents:
36689
diff
changeset
|
81 |
|
36820 | 82 |
def font_metric(metrics: FontMetrics): String => Double = |
83 |
if (metrics == null) ((s: String) => s.length.toDouble) |
|
84 |
else { |
|
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
85 |
val unit = metrics.charWidth(spc).toDouble |
36820 | 86 |
((s: String) => if (s == "\n") 1.0 else metrics.stringWidth(s) / unit) |
87 |
} |
|
88 |
||
38573 | 89 |
def formatted(input: XML.Body, margin: Int = margin_default, |
90 |
metric: String => Double = metric_default): XML.Body = |
|
36687 | 91 |
{ |
92 |
val breakgain = margin / 20 |
|
93 |
val emergencypos = margin / 2 |
|
94 |
||
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
95 |
def content_length(tree: XML.Tree): Double = |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
96 |
tree match { |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
37374
diff
changeset
|
97 |
case XML.Elem(_, body) => (0.0 /: body)(_ + content_length(_)) |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
98 |
case XML.Text(s) => metric(s) |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
99 |
} |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
100 |
|
38573 | 101 |
def breakdist(trees: XML.Body, after: Double): Double = |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
102 |
trees match { |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
103 |
case Break(_) :: _ => 0.0 |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
104 |
case FBreak :: _ => 0.0 |
36818 | 105 |
case t :: ts => content_length(t) + breakdist(ts, after) |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
106 |
case Nil => after |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
107 |
} |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
108 |
|
38573 | 109 |
def forcenext(trees: XML.Body): XML.Body = |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
110 |
trees match { |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
111 |
case Nil => Nil |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
112 |
case FBreak :: _ => trees |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
113 |
case Break(_) :: ts => FBreak :: ts |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
114 |
case t :: ts => t :: forcenext(ts) |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
115 |
} |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
116 |
|
38573 | 117 |
def format(trees: XML.Body, blockin: Double, after: Double, text: Text): Text = |
36687 | 118 |
trees match { |
119 |
case Nil => text |
|
120 |
||
121 |
case Block(indent, body) :: ts => |
|
122 |
val pos1 = text.pos + indent |
|
123 |
val pos2 = pos1 % emergencypos |
|
124 |
val blockin1 = |
|
125 |
if (pos1 < emergencypos) pos1 |
|
126 |
else pos2 |
|
127 |
val btext = format(body, blockin1, breakdist(ts, after), text) |
|
128 |
val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts |
|
129 |
format(ts1, blockin, after, btext) |
|
130 |
||
131 |
case Break(wd) :: ts => |
|
132 |
if (text.pos + wd <= (margin - breakdist(ts, after)).max(blockin + breakgain)) |
|
133 |
format(ts, blockin, after, text.blanks(wd)) |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
134 |
else format(ts, blockin, after, text.newline.blanks(blockin.toInt)) |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
135 |
case FBreak :: ts => format(ts, blockin, after, text.newline.blanks(blockin.toInt)) |
36687 | 136 |
|
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
37374
diff
changeset
|
137 |
case XML.Elem(markup, body) :: ts => |
36734
d9b10c173330
Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents:
36689
diff
changeset
|
138 |
val btext = format(body, blockin, breakdist(ts, after), text.copy(tx = Nil)) |
d9b10c173330
Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents:
36689
diff
changeset
|
139 |
val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
37374
diff
changeset
|
140 |
val btext1 = btext.copy(tx = XML.Elem(markup, btext.content) :: text.tx) |
36734
d9b10c173330
Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents:
36689
diff
changeset
|
141 |
format(ts1, blockin, after, btext1) |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
142 |
case XML.Text(s) :: ts => format(ts, blockin, after, text.string(s, metric(s))) |
36687 | 143 |
} |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
144 |
format(input.flatMap(standard_format), 0.0, 0.0, Text()).content |
36687 | 145 |
} |
36734
d9b10c173330
Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents:
36689
diff
changeset
|
146 |
|
38573 | 147 |
def string_of(input: XML.Body, margin: Int = margin_default, |
36818 | 148 |
metric: String => Double = metric_default): String = |
49416
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
49414
diff
changeset
|
149 |
XML.content(formatted(input, margin, metric)) |
36736 | 150 |
|
151 |
||
152 |
/* unformatted output */ |
|
153 |
||
38573 | 154 |
def unformatted(input: XML.Body): XML.Body = |
36736 | 155 |
{ |
38573 | 156 |
def fmt(tree: XML.Tree): XML.Body = |
36736 | 157 |
tree match { |
158 |
case Block(_, body) => body.flatMap(fmt) |
|
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
159 |
case Break(wd) => List(XML.Text(spaces(wd))) |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
160 |
case FBreak => List(XML.Text(space)) |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
37374
diff
changeset
|
161 |
case XML.Elem(markup, body) => List(XML.Elem(markup, body.flatMap(fmt))) |
36736 | 162 |
case XML.Text(_) => List(tree) |
163 |
} |
|
164 |
input.flatMap(standard_format).flatMap(fmt) |
|
165 |
} |
|
166 |
||
49416
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
49414
diff
changeset
|
167 |
def str_of(input: XML.Body): String = XML.content(unformatted(input)) |
36683 | 168 |
} |