src/Tools/jEdit/src/syntax_style.scala
author wenzelm
Thu Jun 01 21:15:56 2017 +0200 (22 months ago)
changeset 65997 e3dc9ea67a62
parent 65265 f994a61376eb
child 66006 cec184536dfd
permissions -rw-r--r--
output control symbols like ML version, with optionally hidden source;
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@65258
    93
  def extended(text: CharSequence): Map[Text.Offset, Byte => Byte] =
wenzelm@65258
    94
  {
wenzelm@65258
    95
    // FIXME Symbol.bsub_decoded etc.
wenzelm@65258
    96
    def control_style(sym: String): Option[Byte => Byte] =
wenzelm@65258
    97
      if (sym == Symbol.sub_decoded) Some(subscript(_))
wenzelm@65258
    98
      else if (sym == Symbol.sup_decoded) Some(superscript(_))
wenzelm@65258
    99
      else if (sym == Symbol.bold_decoded) Some(bold(_))
wenzelm@65258
   100
      else None
wenzelm@65258
   101
wenzelm@65258
   102
    var result = Map[Text.Offset, Byte => Byte]()
wenzelm@65258
   103
    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
wenzelm@65258
   104
    {
wenzelm@65258
   105
      for (i <- start until stop) result += (i -> style)
wenzelm@65258
   106
    }
wenzelm@65258
   107
    var offset = 0
wenzelm@65258
   108
    var control = ""
wenzelm@65258
   109
    for (sym <- Symbol.iterator(text)) {
wenzelm@65258
   110
      if (control_style(sym).isDefined) control = sym
wenzelm@65258
   111
      else if (control != "") {
wenzelm@65258
   112
        if (Symbol.is_controllable(sym) && sym != "\"" && !Symbol.fonts.isDefinedAt(sym)) {
wenzelm@65258
   113
          mark(offset - control.length, offset, _ => hidden)
wenzelm@65258
   114
          mark(offset, offset + sym.length, control_style(control).get)
wenzelm@65258
   115
        }
wenzelm@65258
   116
        control = ""
wenzelm@65258
   117
      }
wenzelm@65258
   118
      Symbol.lookup_font(sym) match {
wenzelm@65258
   119
        case Some(idx) => mark(offset, offset + sym.length, user_font(idx, _))
wenzelm@65258
   120
        case _ =>
wenzelm@65258
   121
      }
wenzelm@65258
   122
      offset += sym.length
wenzelm@65258
   123
    }
wenzelm@65258
   124
    result
wenzelm@65258
   125
  }
wenzelm@65259
   126
wenzelm@65259
   127
wenzelm@65259
   128
  /* editing support for control symbols */
wenzelm@65259
   129
wenzelm@65259
   130
  def edit_control_style(text_area: TextArea, control: String)
wenzelm@65259
   131
  {
wenzelm@65259
   132
    GUI_Thread.assert {}
wenzelm@65259
   133
wenzelm@65259
   134
    val buffer = text_area.getBuffer
wenzelm@65259
   135
wenzelm@65259
   136
    val control_decoded = Isabelle_Encoding.maybe_decode(buffer, control)
wenzelm@65259
   137
wenzelm@65259
   138
    def update_style(text: String): String =
wenzelm@65259
   139
    {
wenzelm@65259
   140
      val result = new StringBuilder
wenzelm@65997
   141
      for (sym <- Symbol.iterator(text) if !HTML.is_control(sym)) {
wenzelm@65259
   142
        if (Symbol.is_controllable(sym)) result ++= control_decoded
wenzelm@65259
   143
        result ++= sym
wenzelm@65259
   144
      }
wenzelm@65259
   145
      result.toString
wenzelm@65259
   146
    }
wenzelm@65259
   147
wenzelm@65259
   148
    text_area.getSelection.foreach(sel => {
wenzelm@65259
   149
      val before = JEdit_Lib.point_range(buffer, sel.getStart - 1)
wenzelm@65259
   150
      JEdit_Lib.try_get_text(buffer, before) match {
wenzelm@65997
   151
        case Some(s) if HTML.is_control(s) =>
wenzelm@65259
   152
          text_area.extendSelection(before.start, before.stop)
wenzelm@65259
   153
        case _ =>
wenzelm@65259
   154
      }
wenzelm@65259
   155
    })
wenzelm@65259
   156
wenzelm@65259
   157
    text_area.getSelection.toList match {
wenzelm@65259
   158
      case Nil =>
wenzelm@65259
   159
        text_area.setSelectedText(control_decoded)
wenzelm@65259
   160
      case sels =>
wenzelm@65259
   161
        JEdit_Lib.buffer_edit(buffer) {
wenzelm@65259
   162
          sels.foreach(sel =>
wenzelm@65259
   163
            text_area.setSelectedText(sel, update_style(text_area.getSelectedText(sel))))
wenzelm@65259
   164
        }
wenzelm@65259
   165
    }
wenzelm@65259
   166
  }
wenzelm@65258
   167
}