author | wenzelm |
Tue, 12 Nov 2024 11:32:07 +0100 | |
changeset 81432 | 85fc3b482924 |
parent 81428 | 257ec066b360 |
child 81433 | c3793899b880 |
permissions | -rw-r--r-- |
81416 | 1 |
/* Title: Pure/GUI/rich_text.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Support for rendering of rich text, based on individual messages (XML.Elem). |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
81417
964b85e04f1f
clarified margin operations (again, reverting 4794576828df);
wenzelm
parents:
81416
diff
changeset
|
10 |
import javax.swing.JComponent |
964b85e04f1f
clarified margin operations (again, reverting 4794576828df);
wenzelm
parents:
81416
diff
changeset
|
11 |
|
81418 | 12 |
import scala.collection.mutable |
13 |
||
81417
964b85e04f1f
clarified margin operations (again, reverting 4794576828df);
wenzelm
parents:
81416
diff
changeset
|
14 |
|
81416 | 15 |
object Rich_Text { |
81420 | 16 |
/* margin */ |
17 |
||
18 |
def make_margin(metric: Font_Metric, margin: Int, limit: Int = -1): Int = { |
|
19 |
val m = (margin * metric.average).toInt |
|
20 |
(if (limit < 0) m else m min limit) max 20 |
|
21 |
} |
|
22 |
||
23 |
def component_margin(metric: Font_Metric, component: JComponent): Int = |
|
24 |
make_margin(metric, (component.getWidth.toDouble / metric.average_width).toInt) |
|
25 |
||
26 |
||
27 |
/* format */ |
|
28 |
||
81432
85fc3b482924
clarified persistent values: Command.Results does not suitable for caching, because it contains all other messages;
wenzelm
parents:
81428
diff
changeset
|
29 |
sealed case class Formatted(id: Document_ID.Generic, body: XML.Body) { |
81421
8c1680ac4160
clarified signature and data storage: incremental lazy values;
wenzelm
parents:
81420
diff
changeset
|
30 |
lazy val text: String = XML.content(body) |
81432
85fc3b482924
clarified persistent values: Command.Results does not suitable for caching, because it contains all other messages;
wenzelm
parents:
81428
diff
changeset
|
31 |
lazy val markups: Command.Markups = Command.Markups.init(Markup_Tree.from_XML(body)) |
85fc3b482924
clarified persistent values: Command.Results does not suitable for caching, because it contains all other messages;
wenzelm
parents:
81428
diff
changeset
|
32 |
def command(results: Command.Results): Command = |
85fc3b482924
clarified persistent values: Command.Results does not suitable for caching, because it contains all other messages;
wenzelm
parents:
81428
diff
changeset
|
33 |
Command.unparsed(text, id = id, results = results, markups = markups) |
81416 | 34 |
} |
81417
964b85e04f1f
clarified margin operations (again, reverting 4794576828df);
wenzelm
parents:
81416
diff
changeset
|
35 |
|
81432
85fc3b482924
clarified persistent values: Command.Results does not suitable for caching, because it contains all other messages;
wenzelm
parents:
81428
diff
changeset
|
36 |
def format(msgs: List[XML.Elem], margin: Double, metric: Font_Metric): List[Formatted] = { |
81421
8c1680ac4160
clarified signature and data storage: incremental lazy values;
wenzelm
parents:
81420
diff
changeset
|
37 |
val result = new mutable.ListBuffer[Formatted] |
81418 | 38 |
for (msg <- msgs) { |
81432
85fc3b482924
clarified persistent values: Command.Results does not suitable for caching, because it contains all other messages;
wenzelm
parents:
81428
diff
changeset
|
39 |
if (result.nonEmpty) result += Formatted(Document_ID.make(), Pretty.Separator) |
81421
8c1680ac4160
clarified signature and data storage: incremental lazy values;
wenzelm
parents:
81420
diff
changeset
|
40 |
|
8c1680ac4160
clarified signature and data storage: incremental lazy values;
wenzelm
parents:
81420
diff
changeset
|
41 |
val id = Protocol_Message.get_serial(msg) |
81418 | 42 |
val body = Pretty.formatted(List(msg), margin = margin, metric = metric) |
81432
85fc3b482924
clarified persistent values: Command.Results does not suitable for caching, because it contains all other messages;
wenzelm
parents:
81428
diff
changeset
|
43 |
result += Formatted(id, body) |
81418 | 44 |
|
45 |
Exn.Interrupt.expose() |
|
46 |
} |
|
47 |
result.toList |
|
48 |
} |
|
81428 | 49 |
|
50 |
def formatted_lines(formatted: List[Formatted]): Int = |
|
51 |
if (formatted.isEmpty) 0 |
|
52 |
else { |
|
53 |
1 + formatted.iterator.map(form => |
|
54 |
XML.traverse_text(form.body, 0, (n, s) => n + Library.count_newlines(s))).sum |
|
55 |
} |
|
56 |
||
57 |
def formatted_margin(metric: Font_Metric, formatted: List[Formatted]): Double = |
|
58 |
split_lines(formatted.map(_.text).mkString) |
|
59 |
.foldLeft(0.0) { case (m, line) => m max metric(line) } |
|
81416 | 60 |
} |