src/Tools/jEdit/src/syntax_style.scala
author wenzelm
Mon Dec 04 17:37:26 2017 +0100 (16 months ago)
changeset 67127 cf111622c9f8
parent 67125 361b3ef643a7
child 67128 4d91b6d5d49c
permissions -rw-r--r--
font style for literal control symbols, notably for antiquotations;
wenzelm@65258
     1
/*  Title:      Tools/jEdit/src/syntax_style.scala
wenzelm@65258
     2
    Author:     Makarius
wenzelm@65258
     3
wenzelm@65258
     4
Support for extended syntax styles: subscript, superscript, bold, user fonts.
wenzelm@65258
     5
*/
wenzelm@65258
     6
wenzelm@65258
     7
package isabelle.jedit
wenzelm@65258
     8
wenzelm@65258
     9
wenzelm@65258
    10
import isabelle._
wenzelm@65258
    11
wenzelm@65258
    12
import java.awt.{Font, Color}
wenzelm@65258
    13
import java.awt.font.TextAttribute
wenzelm@65258
    14
import java.awt.geom.AffineTransform
wenzelm@65258
    15
wenzelm@65258
    16
import org.gjt.sp.util.SyntaxUtilities
wenzelm@65258
    17
import org.gjt.sp.jedit.syntax.{Token => JEditToken, SyntaxStyle}
wenzelm@65259
    18
import org.gjt.sp.jedit.textarea.TextArea
wenzelm@65258
    19
wenzelm@65258
    20
wenzelm@65258
    21
object Syntax_Style
wenzelm@65258
    22
{
wenzelm@65259
    23
  /* extended syntax styles */
wenzelm@65259
    24
wenzelm@65258
    25
  private val plain_range: Int = JEditToken.ID_COUNT
wenzelm@65258
    26
  private val full_range = 6 * plain_range + 1
wenzelm@65258
    27
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
wenzelm@65258
    28
wenzelm@65258
    29
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
wenzelm@65258
    30
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
wenzelm@65258
    31
  def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
wenzelm@65258
    32
  def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte }
wenzelm@65258
    33
  val hidden: Byte = (6 * plain_range).toByte
wenzelm@65258
    34
wenzelm@65258
    35
  private def font_style(style: SyntaxStyle, f: Font => Font): SyntaxStyle =
wenzelm@65258
    36
    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, f(style.getFont))
wenzelm@65258
    37
wenzelm@65258
    38
  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
wenzelm@65258
    39
  {
wenzelm@65258
    40
    font_style(style, font0 =>
wenzelm@65258
    41
      {
wenzelm@65258
    42
        import scala.collection.JavaConversions._
wenzelm@65258
    43
        val font1 = font0.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
wenzelm@65258
    44
wenzelm@65258
    45
        def shift(y: Float): Font =
wenzelm@65258
    46
          GUI.transform_font(font1, AffineTransform.getTranslateInstance(0.0, y.toDouble))
wenzelm@65258
    47
wenzelm@65258
    48
        val m0 = GUI.line_metrics(font0)
wenzelm@65258
    49
        val m1 = GUI.line_metrics(font1)
wenzelm@65258
    50
        val a = m1.getAscent - m0.getAscent
wenzelm@65258
    51
        val b = (m1.getDescent + m1.getLeading) - (m0.getDescent + m0.getLeading)
wenzelm@65258
    52
        if (a > 0.0f) shift(a)
wenzelm@65258
    53
        else if (b > 0.0f) shift(- b)
wenzelm@65258
    54
        else font1
wenzelm@65258
    55
      })
wenzelm@65258
    56
  }
wenzelm@65258
    57
wenzelm@65258
    58
  private def bold_style(style: SyntaxStyle): SyntaxStyle =
wenzelm@65258
    59
    font_style(style, font => font.deriveFont(if (font.isBold) Font.PLAIN else Font.BOLD))
wenzelm@65258
    60
wenzelm@65258
    61
  val hidden_color: Color = new Color(255, 255, 255, 0)
wenzelm@65258
    62
wenzelm@65261
    63
  object Extender extends SyntaxUtilities.StyleExtender
wenzelm@65258
    64
  {
wenzelm@65258
    65
    val max_user_fonts = 2
wenzelm@65258
    66
    if (Symbol.font_names.length > max_user_fonts)
wenzelm@65258
    67
      error("Too many user symbol fonts (" + max_user_fonts + " permitted): " +
wenzelm@65258
    68
        Symbol.font_names.mkString(", "))
wenzelm@65258
    69
wenzelm@65258
    70
    override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
wenzelm@65258
    71
    {
wenzelm@65258
    72
      val new_styles = new Array[SyntaxStyle](full_range)
wenzelm@65258
    73
      for (i <- 0 until plain_range) {
wenzelm@65258
    74
        val style = styles(i)
wenzelm@65258
    75
        new_styles(i) = style
wenzelm@65258
    76
        new_styles(subscript(i.toByte)) = script_style(style, -1)
wenzelm@65258
    77
        new_styles(superscript(i.toByte)) = script_style(style, 1)
wenzelm@65258
    78
        new_styles(bold(i.toByte)) = bold_style(style)
wenzelm@65258
    79
        for (idx <- 0 until max_user_fonts)
wenzelm@65258
    80
          new_styles(user_font(idx, i.toByte)) = style
wenzelm@65258
    81
        for ((family, idx) <- Symbol.font_index)
wenzelm@65258
    82
          new_styles(user_font(idx, i.toByte)) = font_style(style, GUI.imitate_font(_, family))
wenzelm@65258
    83
      }
wenzelm@65258
    84
      new_styles(hidden) =
wenzelm@65258
    85
        new SyntaxStyle(hidden_color, null,
wenzelm@65258
    86
          { val font = styles(0).getFont
wenzelm@65258
    87
            GUI.transform_font(new Font(font.getFamily, 0, 1),
wenzelm@65258
    88
              AffineTransform.getScaleInstance(1.0, font.getSize.toDouble)) })
wenzelm@65258
    89
      new_styles
wenzelm@65258
    90
    }
wenzelm@65258
    91
  }
wenzelm@65258
    92
wenzelm@67127
    93
  private def control_style(sym: String): Option[Byte => Byte] =
wenzelm@67127
    94
    if (sym == Symbol.sub_decoded) Some(subscript(_))
wenzelm@67127
    95
    else if (sym == Symbol.sup_decoded) Some(superscript(_))
wenzelm@67127
    96
    else if (sym == Symbol.bold_decoded) Some(bold(_))
wenzelm@67127
    97
    else None
wenzelm@67127
    98
wenzelm@65258
    99
  def extended(text: CharSequence): Map[Text.Offset, Byte => Byte] =
wenzelm@65258
   100
  {
wenzelm@65258
   101
    var result = Map[Text.Offset, Byte => Byte]()
wenzelm@65258
   102
    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
wenzelm@65258
   103
    {
wenzelm@65258
   104
      for (i <- start until stop) result += (i -> style)
wenzelm@65258
   105
    }
wenzelm@67127
   106
wenzelm@65258
   107
    var offset = 0
wenzelm@65258
   108
    var control = ""
wenzelm@65258
   109
    for (sym <- Symbol.iterator(text)) {
wenzelm@67127
   110
      val end_offset = offset + sym.length
wenzelm@67127
   111
wenzelm@65258
   112
      if (control_style(sym).isDefined) control = sym
wenzelm@65258
   113
      else if (control != "") {
wenzelm@66006
   114
        if (Symbol.is_controllable(sym) && !Symbol.fonts.isDefinedAt(sym)) {
wenzelm@65258
   115
          mark(offset - control.length, offset, _ => hidden)
wenzelm@67127
   116
          mark(offset, end_offset, control_style(control).get)
wenzelm@65258
   117
        }
wenzelm@65258
   118
        control = ""
wenzelm@65258
   119
      }
wenzelm@67127
   120
wenzelm@67127
   121
      if (Symbol.is_control_encoded(sym)) {
wenzelm@67127
   122
        val a = offset + Symbol.control_prefix.length
wenzelm@67127
   123
        val b = end_offset - Symbol.control_suffix.length
wenzelm@67127
   124
        mark(offset, a, _ => hidden)
wenzelm@67127
   125
        mark(a, b, _ => JEditToken.KEYWORD4)
wenzelm@67127
   126
        mark(b, end_offset, _ => hidden)
wenzelm@67127
   127
      }
wenzelm@67127
   128
wenzelm@65258
   129
      Symbol.lookup_font(sym) match {
wenzelm@67127
   130
        case Some(idx) => mark(offset, end_offset, user_font(idx, _))
wenzelm@65258
   131
        case _ =>
wenzelm@65258
   132
      }
wenzelm@67127
   133
wenzelm@67127
   134
      offset = end_offset
wenzelm@65258
   135
    }
wenzelm@67127
   136
wenzelm@65258
   137
    result
wenzelm@65258
   138
  }
wenzelm@65259
   139
wenzelm@65259
   140
wenzelm@65259
   141
  /* editing support for control symbols */
wenzelm@65259
   142
wenzelm@65259
   143
  def edit_control_style(text_area: TextArea, control: String)
wenzelm@65259
   144
  {
wenzelm@65259
   145
    GUI_Thread.assert {}
wenzelm@65259
   146
wenzelm@65259
   147
    val buffer = text_area.getBuffer
wenzelm@65259
   148
wenzelm@65259
   149
    val control_decoded = Isabelle_Encoding.maybe_decode(buffer, control)
wenzelm@65259
   150
wenzelm@65259
   151
    def update_style(text: String): String =
wenzelm@65259
   152
    {
wenzelm@65259
   153
      val result = new StringBuilder
wenzelm@65997
   154
      for (sym <- Symbol.iterator(text) if !HTML.is_control(sym)) {
wenzelm@65259
   155
        if (Symbol.is_controllable(sym)) result ++= control_decoded
wenzelm@65259
   156
        result ++= sym
wenzelm@65259
   157
      }
wenzelm@65259
   158
      result.toString
wenzelm@65259
   159
    }
wenzelm@65259
   160
wenzelm@65259
   161
    text_area.getSelection.foreach(sel => {
wenzelm@65259
   162
      val before = JEdit_Lib.point_range(buffer, sel.getStart - 1)
wenzelm@67014
   163
      JEdit_Lib.get_text(buffer, before) match {
wenzelm@65997
   164
        case Some(s) if HTML.is_control(s) =>
wenzelm@65259
   165
          text_area.extendSelection(before.start, before.stop)
wenzelm@65259
   166
        case _ =>
wenzelm@65259
   167
      }
wenzelm@65259
   168
    })
wenzelm@65259
   169
wenzelm@65259
   170
    text_area.getSelection.toList match {
wenzelm@65259
   171
      case Nil =>
wenzelm@65259
   172
        text_area.setSelectedText(control_decoded)
wenzelm@65259
   173
      case sels =>
wenzelm@65259
   174
        JEdit_Lib.buffer_edit(buffer) {
wenzelm@65259
   175
          sels.foreach(sel =>
wenzelm@65259
   176
            text_area.setSelectedText(sel, update_style(text_area.getSelectedText(sel))))
wenzelm@65259
   177
        }
wenzelm@65259
   178
    }
wenzelm@65259
   179
  }
wenzelm@65258
   180
}