src/Tools/jEdit/src/syntax_style.scala
author wenzelm
Mon Dec 04 22:52:16 2017 +0100 (8 months ago)
changeset 67130 b023f64e0d16
parent 67128 4d91b6d5d49c
permissions -rw-r--r--
tuned signature;
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 def check_range(i: Int) { require(0 <= i && i < plain_range) }
wenzelm@65258
    27
wenzelm@65258
    28
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
wenzelm@65258
    29
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
wenzelm@65258
    30
  def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
wenzelm@65258
    31
  def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte }
wenzelm@65258
    32
  val hidden: Byte = (6 * plain_range).toByte
wenzelm@67128
    33
  val control: Byte = (hidden + JEditToken.DIGIT).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@67128
    72
      val style0 = styles(0)
wenzelm@67128
    73
      val font0 = style0.getFont
wenzelm@67128
    74
wenzelm@67128
    75
      val new_styles = Array.fill[SyntaxStyle](java.lang.Byte.MAX_VALUE)(styles(0))
wenzelm@65258
    76
      for (i <- 0 until plain_range) {
wenzelm@65258
    77
        val style = styles(i)
wenzelm@65258
    78
        new_styles(i) = style
wenzelm@65258
    79
        new_styles(subscript(i.toByte)) = script_style(style, -1)
wenzelm@65258
    80
        new_styles(superscript(i.toByte)) = script_style(style, 1)
wenzelm@65258
    81
        new_styles(bold(i.toByte)) = bold_style(style)
wenzelm@65258
    82
        for (idx <- 0 until max_user_fonts)
wenzelm@65258
    83
          new_styles(user_font(idx, i.toByte)) = style
wenzelm@65258
    84
        for ((family, idx) <- Symbol.font_index)
wenzelm@65258
    85
          new_styles(user_font(idx, i.toByte)) = font_style(style, GUI.imitate_font(_, family))
wenzelm@65258
    86
      }
wenzelm@65258
    87
      new_styles(hidden) =
wenzelm@65258
    88
        new SyntaxStyle(hidden_color, null,
wenzelm@67128
    89
          GUI.transform_font(new Font(font0.getFamily, 0, 1),
wenzelm@67128
    90
            AffineTransform.getScaleInstance(1.0, font0.getSize.toDouble)))
wenzelm@67128
    91
      new_styles(control) =
wenzelm@67128
    92
        new SyntaxStyle(style0.getForegroundColor, style0.getBackgroundColor,
wenzelm@67128
    93
          { val font_style =
wenzelm@67128
    94
              (if (font0.isItalic) 0 else Font.ITALIC) |
wenzelm@67128
    95
              (if (font0.isBold) 0 else Font.BOLD)
wenzelm@67128
    96
            new Font(font0.getFamily, font_style, font0.getSize) })
wenzelm@65258
    97
      new_styles
wenzelm@65258
    98
    }
wenzelm@65258
    99
  }
wenzelm@65258
   100
wenzelm@67127
   101
  private def control_style(sym: String): Option[Byte => Byte] =
wenzelm@67127
   102
    if (sym == Symbol.sub_decoded) Some(subscript(_))
wenzelm@67127
   103
    else if (sym == Symbol.sup_decoded) Some(superscript(_))
wenzelm@67127
   104
    else if (sym == Symbol.bold_decoded) Some(bold(_))
wenzelm@67127
   105
    else None
wenzelm@67127
   106
wenzelm@65258
   107
  def extended(text: CharSequence): Map[Text.Offset, Byte => Byte] =
wenzelm@65258
   108
  {
wenzelm@65258
   109
    var result = Map[Text.Offset, Byte => Byte]()
wenzelm@65258
   110
    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
wenzelm@65258
   111
    {
wenzelm@65258
   112
      for (i <- start until stop) result += (i -> style)
wenzelm@65258
   113
    }
wenzelm@67127
   114
wenzelm@65258
   115
    var offset = 0
wenzelm@67128
   116
    var control_sym = ""
wenzelm@65258
   117
    for (sym <- Symbol.iterator(text)) {
wenzelm@67127
   118
      val end_offset = offset + sym.length
wenzelm@67127
   119
wenzelm@67128
   120
      if (control_style(sym).isDefined) control_sym = sym
wenzelm@67128
   121
      else if (control_sym != "") {
wenzelm@66006
   122
        if (Symbol.is_controllable(sym) && !Symbol.fonts.isDefinedAt(sym)) {
wenzelm@67128
   123
          mark(offset - control_sym.length, offset, _ => hidden)
wenzelm@67128
   124
          mark(offset, end_offset, control_style(control_sym).get)
wenzelm@65258
   125
        }
wenzelm@67128
   126
        control_sym = ""
wenzelm@65258
   127
      }
wenzelm@67127
   128
wenzelm@67127
   129
      if (Symbol.is_control_encoded(sym)) {
wenzelm@67127
   130
        val a = offset + Symbol.control_prefix.length
wenzelm@67127
   131
        val b = end_offset - Symbol.control_suffix.length
wenzelm@67127
   132
        mark(offset, a, _ => hidden)
wenzelm@67128
   133
        mark(a, b, _ => control)
wenzelm@67127
   134
        mark(b, end_offset, _ => hidden)
wenzelm@67127
   135
      }
wenzelm@67127
   136
wenzelm@65258
   137
      Symbol.lookup_font(sym) match {
wenzelm@67127
   138
        case Some(idx) => mark(offset, end_offset, user_font(idx, _))
wenzelm@65258
   139
        case _ =>
wenzelm@65258
   140
      }
wenzelm@67127
   141
wenzelm@67127
   142
      offset = end_offset
wenzelm@65258
   143
    }
wenzelm@67127
   144
wenzelm@65258
   145
    result
wenzelm@65258
   146
  }
wenzelm@65259
   147
wenzelm@65259
   148
wenzelm@65259
   149
  /* editing support for control symbols */
wenzelm@65259
   150
wenzelm@65259
   151
  def edit_control_style(text_area: TextArea, control: String)
wenzelm@65259
   152
  {
wenzelm@65259
   153
    GUI_Thread.assert {}
wenzelm@65259
   154
wenzelm@65259
   155
    val buffer = text_area.getBuffer
wenzelm@65259
   156
wenzelm@67130
   157
    val control_decoded = Isabelle_Encoding.perhaps_decode(buffer, control)
wenzelm@65259
   158
wenzelm@65259
   159
    def update_style(text: String): String =
wenzelm@65259
   160
    {
wenzelm@65259
   161
      val result = new StringBuilder
wenzelm@65997
   162
      for (sym <- Symbol.iterator(text) if !HTML.is_control(sym)) {
wenzelm@65259
   163
        if (Symbol.is_controllable(sym)) result ++= control_decoded
wenzelm@65259
   164
        result ++= sym
wenzelm@65259
   165
      }
wenzelm@65259
   166
      result.toString
wenzelm@65259
   167
    }
wenzelm@65259
   168
wenzelm@65259
   169
    text_area.getSelection.foreach(sel => {
wenzelm@65259
   170
      val before = JEdit_Lib.point_range(buffer, sel.getStart - 1)
wenzelm@67014
   171
      JEdit_Lib.get_text(buffer, before) match {
wenzelm@65997
   172
        case Some(s) if HTML.is_control(s) =>
wenzelm@65259
   173
          text_area.extendSelection(before.start, before.stop)
wenzelm@65259
   174
        case _ =>
wenzelm@65259
   175
      }
wenzelm@65259
   176
    })
wenzelm@65259
   177
wenzelm@65259
   178
    text_area.getSelection.toList match {
wenzelm@65259
   179
      case Nil =>
wenzelm@65259
   180
        text_area.setSelectedText(control_decoded)
wenzelm@65259
   181
      case sels =>
wenzelm@65259
   182
        JEdit_Lib.buffer_edit(buffer) {
wenzelm@65259
   183
          sels.foreach(sel =>
wenzelm@65259
   184
            text_area.setSelectedText(sel, update_style(text_area.getSelectedText(sel))))
wenzelm@65259
   185
        }
wenzelm@65259
   186
    }
wenzelm@65259
   187
  }
wenzelm@65258
   188
}