src/Pure/GUI/rich_text.scala
author wenzelm
Sun, 10 Nov 2024 12:33:20 +0100
changeset 81420 d25a241502c1
parent 81419 b242c7603e08
child 81421 8c1680ac4160
permissions -rw-r--r--
tuned comments;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
81416
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/GUI/rich_text.scala
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
     3
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
     4
Support for rendering of rich text, based on individual messages (XML.Elem).
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
     6
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
     8
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
     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
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    12
import scala.collection.mutable
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    13
81417
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81416
diff changeset
    14
81416
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    15
object Rich_Text {
81420
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    16
  /* margin */
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    17
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    18
  def make_margin(metric: Font_Metric, margin: Int, limit: Int = -1): Int = {
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    19
    val m = (margin * metric.average).toInt
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    20
    (if (limit < 0) m else m min limit) max 20
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    21
  }
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    22
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    23
  def component_margin(metric: Font_Metric, component: JComponent): Int =
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    24
    make_margin(metric, (component.getWidth.toDouble / metric.average_width).toInt)
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    25
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    26
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    27
  /* format */
d25a241502c1 tuned comments;
wenzelm
parents: 81419
diff changeset
    28
81416
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    29
  def command(
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    30
    body: XML.Body = Nil,
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    31
    id: Document_ID.Command = Document_ID.none,
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    32
    results: Command.Results = Command.Results.empty
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    33
  ): Command = {
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    34
    val source = XML.content(body)
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    35
    val markups = Command.Markups.init(Markup_Tree.from_XML(body))
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    36
    Command.unparsed(source, id = id, results = results, markups = markups)
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    37
  }
81417
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81416
diff changeset
    38
81418
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    39
  def format(
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    40
    msgs: List[XML.Elem],
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    41
    margin: Double,
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    42
    metric: Font_Metric,
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    43
    results: Command.Results
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    44
  ) : List[Command] = {
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    45
    val result = new mutable.ListBuffer[Command]
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    46
    for (msg <- msgs) {
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    47
      if (result.nonEmpty) {
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    48
        result += command(body = Pretty.Separator, id = Document_ID.make())
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    49
      }
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    50
      val body = Pretty.formatted(List(msg), margin = margin, metric = metric)
81419
b242c7603e08 clarified signature;
wenzelm
parents: 81418
diff changeset
    51
      result += command(body = body, id = Protocol_Message.get_serial(msg))
81418
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    52
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    53
      Exn.Interrupt.expose()
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    54
    }
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    55
    result.toList
de8dbdadda76 clarified modules;
wenzelm
parents: 81417
diff changeset
    56
  }
81416
206dd586f3d7 clarified modules;
wenzelm
parents:
diff changeset
    57
}