src/Tools/jEdit/src/token_markup.scala
author wenzelm
Sun Aug 21 20:04:02 2011 +0200 (2011-08-21)
changeset 44355 9c38bdc6d755
parent 44238 36120feb70ed
child 44356 f6a2e5ce2ce5
permissions -rw-r--r--
default style for user fonts -- to prevent org.gjt.sp.jedit.print.BufferPrintable from choking on null;
wenzelm@43414
     1
/*  Title:      Tools/jEdit/src/token_markup.scala
wenzelm@43414
     2
    Author:     Makarius
wenzelm@43414
     3
wenzelm@43414
     4
Outer syntax token markup.
wenzelm@43414
     5
*/
wenzelm@43414
     6
wenzelm@43414
     7
package isabelle.jedit
wenzelm@43414
     8
wenzelm@43414
     9
wenzelm@43414
    10
import isabelle._
wenzelm@43414
    11
wenzelm@43482
    12
import java.awt.{Font, Color}
wenzelm@43502
    13
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics}
wenzelm@43491
    14
import java.awt.geom.AffineTransform
wenzelm@43482
    15
wenzelm@43482
    16
import org.gjt.sp.util.SyntaxUtilities
wenzelm@43452
    17
import org.gjt.sp.jedit.Mode
wenzelm@43452
    18
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler,
wenzelm@43482
    19
  ParserRuleSet, ModeProvider, XModeHandler, SyntaxStyle}
wenzelm@43452
    20
wenzelm@43414
    21
import javax.swing.text.Segment
wenzelm@43414
    22
wenzelm@43414
    23
wenzelm@43414
    24
object Token_Markup
wenzelm@43414
    25
{
wenzelm@43502
    26
  /* font operations */
wenzelm@43502
    27
wenzelm@43502
    28
  private def font_metrics(font: Font): LineMetrics =
wenzelm@43502
    29
    font.getLineMetrics("", new FontRenderContext(null, false, false))
wenzelm@43502
    30
wenzelm@43502
    31
  private def imitate_font(family: String, font: Font): Font =
wenzelm@43502
    32
  {
wenzelm@43502
    33
    val font1 = new Font (family, font.getStyle, font.getSize)
wenzelm@43502
    34
    font1.deriveFont(font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize)
wenzelm@43502
    35
  }
wenzelm@43502
    36
wenzelm@43502
    37
  private def transform_font(font: Font, transform: AffineTransform): Font =
wenzelm@43502
    38
  {
wenzelm@43502
    39
    import scala.collection.JavaConversions._
wenzelm@43502
    40
    font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform)))
wenzelm@43502
    41
  }
wenzelm@43502
    42
wenzelm@43502
    43
wenzelm@43443
    44
  /* extended syntax styles */
wenzelm@43414
    45
wenzelm@43440
    46
  private val plain_range: Int = JEditToken.ID_COUNT
wenzelm@43488
    47
  private val full_range = 6 * plain_range + 1
wenzelm@43414
    48
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
wenzelm@43414
    49
wenzelm@43414
    50
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
wenzelm@43414
    51
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
wenzelm@43460
    52
  def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
wenzelm@43487
    53
  def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte }
wenzelm@43487
    54
  val hidden: Byte = (6 * plain_range).toByte
wenzelm@43487
    55
wenzelm@43487
    56
  private def font_style(style: SyntaxStyle, f: Font => Font): SyntaxStyle =
wenzelm@43487
    57
    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, f(style.getFont))
wenzelm@43414
    58
wenzelm@43482
    59
  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
wenzelm@43482
    60
  {
wenzelm@43502
    61
    font_style(style, font0 =>
wenzelm@43502
    62
      {
wenzelm@43502
    63
        import scala.collection.JavaConversions._
wenzelm@43502
    64
        val font1 = font0.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
wenzelm@43502
    65
wenzelm@43502
    66
        def shift(y: Float): Font =
wenzelm@43502
    67
          transform_font(font1, AffineTransform.getTranslateInstance(0.0, y.toDouble))
wenzelm@43502
    68
wenzelm@43502
    69
        val m0 = font_metrics(font0)
wenzelm@43502
    70
        val m1 = font_metrics(font1)
wenzelm@43502
    71
        val a = m1.getAscent - m0.getAscent
wenzelm@43502
    72
        val b = (m1.getDescent + m1.getLeading) - (m0.getDescent + m0.getLeading)
wenzelm@43502
    73
        if (a > 0.0f) shift(a)
wenzelm@43502
    74
        else if (b > 0.0f) shift(- b)
wenzelm@43502
    75
        else font1
wenzelm@43502
    76
      })
wenzelm@43482
    77
  }
wenzelm@43482
    78
wenzelm@43482
    79
  private def bold_style(style: SyntaxStyle): SyntaxStyle =
wenzelm@43487
    80
    font_style(style, _.deriveFont(Font.BOLD))
wenzelm@43482
    81
wenzelm@43661
    82
  class Style_Extender extends SyntaxUtilities.StyleExtender
wenzelm@43482
    83
  {
wenzelm@44355
    84
    val max_user_fonts = 2
wenzelm@44355
    85
    if (Symbol.font_names.length > max_user_fonts)
wenzelm@44355
    86
      error("Too many user symbol fonts (max " + max_user_fonts + " permitted): " +
wenzelm@44355
    87
        Symbol.font_names.mkString(", "))
wenzelm@43487
    88
wenzelm@43482
    89
    override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
wenzelm@43482
    90
    {
wenzelm@43482
    91
      val new_styles = new Array[SyntaxStyle](full_range)
wenzelm@43482
    92
      for (i <- 0 until plain_range) {
wenzelm@43482
    93
        val style = styles(i)
wenzelm@43482
    94
        new_styles(i) = style
wenzelm@43482
    95
        new_styles(subscript(i.toByte)) = script_style(style, -1)
wenzelm@43482
    96
        new_styles(superscript(i.toByte)) = script_style(style, 1)
wenzelm@43482
    97
        new_styles(bold(i.toByte)) = bold_style(style)
wenzelm@44355
    98
        for (idx <- 0 until max_user_fonts)
wenzelm@44355
    99
          new_styles(user_font(idx, i.toByte)) = style
wenzelm@43695
   100
        for ((family, idx) <- Symbol.font_index)
wenzelm@43502
   101
          new_styles(user_font(idx, i.toByte)) = font_style(style, imitate_font(family, _))
wenzelm@43482
   102
      }
wenzelm@43502
   103
      new_styles(hidden) =
wenzelm@43502
   104
        new SyntaxStyle(Color.white, null,
wenzelm@43502
   105
          { val font = styles(0).getFont
wenzelm@43502
   106
            transform_font(new Font(font.getFamily, 0, 1),
wenzelm@43502
   107
              AffineTransform.getScaleInstance(1.0, font.getSize.toDouble)) })
wenzelm@43482
   108
      new_styles
wenzelm@43482
   109
    }
wenzelm@43482
   110
  }
wenzelm@43482
   111
wenzelm@43661
   112
  def extended_styles(text: CharSequence): Map[Text.Offset, Byte => Byte] =
wenzelm@43443
   113
  {
wenzelm@44238
   114
    // FIXME Symbol.bsub_decoded etc.
wenzelm@43482
   115
    def ctrl_style(sym: String): Option[Byte => Byte] =
wenzelm@44238
   116
      if (sym == Symbol.sub_decoded || sym == Symbol.isub_decoded) Some(subscript(_))
wenzelm@44238
   117
      else if (sym == Symbol.sup_decoded || sym == Symbol.isup_decoded) Some(superscript(_))
wenzelm@44238
   118
      else if (sym == Symbol.bold_decoded) Some(bold(_))
wenzelm@43482
   119
      else None
wenzelm@43455
   120
wenzelm@43482
   121
    var result = Map[Text.Offset, Byte => Byte]()
wenzelm@43482
   122
    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
wenzelm@43482
   123
    {
wenzelm@43482
   124
      for (i <- start until stop) result += (i -> style)
wenzelm@43482
   125
    }
wenzelm@43482
   126
    var offset = 0
wenzelm@43482
   127
    var ctrl = ""
wenzelm@43675
   128
    for (sym <- Symbol.iterator(text)) {
wenzelm@43482
   129
      if (ctrl_style(sym).isDefined) ctrl = sym
wenzelm@43482
   130
      else if (ctrl != "") {
wenzelm@43695
   131
        if (Symbol.is_controllable(sym) && sym != "\"" && !Symbol.fonts.isDefinedAt(sym)) {
wenzelm@43482
   132
          mark(offset - ctrl.length, offset, _ => hidden)
wenzelm@43482
   133
          mark(offset, offset + sym.length, ctrl_style(ctrl).get)
wenzelm@43482
   134
        }
wenzelm@43482
   135
        ctrl = ""
wenzelm@43443
   136
      }
wenzelm@43695
   137
      Symbol.lookup_font(sym) match {
wenzelm@43488
   138
        case Some(idx) => mark(offset, offset + sym.length, user_font(idx, _))
wenzelm@43487
   139
        case _ =>
wenzelm@43487
   140
      }
wenzelm@43482
   141
      offset += sym.length
wenzelm@43443
   142
    }
wenzelm@43482
   143
    result
wenzelm@43443
   144
  }
wenzelm@43443
   145
wenzelm@43414
   146
wenzelm@43414
   147
  /* token marker */
wenzelm@43414
   148
wenzelm@43414
   149
  private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
wenzelm@43414
   150
wenzelm@43429
   151
  private class Line_Context(val context: Scan.Context)
wenzelm@43429
   152
    extends TokenMarker.LineContext(isabelle_rules, null)
wenzelm@43414
   153
  {
wenzelm@43414
   154
    override def hashCode: Int = context.hashCode
wenzelm@43414
   155
    override def equals(that: Any): Boolean =
wenzelm@43414
   156
      that match {
wenzelm@43414
   157
        case other: Line_Context => context == other.context
wenzelm@43414
   158
        case _ => false
wenzelm@43414
   159
      }
wenzelm@43414
   160
  }
wenzelm@43414
   161
wenzelm@43452
   162
  class Marker extends TokenMarker
wenzelm@43452
   163
  {
wenzelm@43452
   164
    override def markTokens(context: TokenMarker.LineContext,
wenzelm@43452
   165
        handler: TokenHandler, line: Segment): TokenMarker.LineContext =
wenzelm@43452
   166
    {
wenzelm@43553
   167
      val context1 =
wenzelm@43553
   168
        if (Isabelle.session.is_ready) {
wenzelm@43553
   169
          val syntax = Isabelle.session.current_syntax()
wenzelm@43553
   170
    
wenzelm@43553
   171
          val ctxt =
wenzelm@43553
   172
            context match {
wenzelm@43553
   173
              case c: Line_Context => c.context
wenzelm@43553
   174
              case _ => Scan.Finished
wenzelm@43553
   175
            }
wenzelm@43553
   176
          val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
wenzelm@43553
   177
          val context1 = new Line_Context(ctxt1)
wenzelm@43553
   178
    
wenzelm@43661
   179
          val extended = extended_styles(line)
wenzelm@43553
   180
    
wenzelm@43553
   181
          var offset = 0
wenzelm@43553
   182
          for (token <- tokens) {
wenzelm@43553
   183
            val style = Isabelle_Markup.token_markup(syntax, token)
wenzelm@43553
   184
            val length = token.source.length
wenzelm@43553
   185
            val end_offset = offset + length
wenzelm@43553
   186
            if ((offset until end_offset) exists extended.isDefinedAt) {
wenzelm@43553
   187
              for (i <- offset until end_offset) {
wenzelm@43553
   188
                val style1 =
wenzelm@43553
   189
                  extended.get(i) match {
wenzelm@43553
   190
                    case None => style
wenzelm@43553
   191
                    case Some(ext) => ext(style)
wenzelm@43553
   192
                  }
wenzelm@43553
   193
                handler.handleToken(line, style1, i, 1, context1)
wenzelm@43553
   194
              }
wenzelm@43553
   195
            }
wenzelm@43553
   196
            else handler.handleToken(line, style, offset, length, context1)
wenzelm@43553
   197
            offset += length
wenzelm@43553
   198
          }
wenzelm@43553
   199
          handler.handleToken(line, JEditToken.END, line.count, 0, context1)
wenzelm@43553
   200
          context1
wenzelm@43452
   201
        }
wenzelm@43553
   202
        else {
wenzelm@43553
   203
          val context1 = new Line_Context(Scan.Finished)
wenzelm@43553
   204
          handler.handleToken(line, JEditToken.NULL, 0, line.count, context1)
wenzelm@43553
   205
          handler.handleToken(line, JEditToken.END, line.count, 0, context1)
wenzelm@43553
   206
          context1
wenzelm@43414
   207
        }
wenzelm@43452
   208
      val context2 = context1.intern
wenzelm@43452
   209
      handler.setLineContext(context2)
wenzelm@43452
   210
      context2
wenzelm@43452
   211
    }
wenzelm@43452
   212
  }
wenzelm@43452
   213
wenzelm@43416
   214
wenzelm@43452
   215
  /* mode provider */
wenzelm@43452
   216
wenzelm@43452
   217
  class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider
wenzelm@43452
   218
  {
wenzelm@43452
   219
    for (mode <- orig_provider.getModes) addMode(mode)
wenzelm@43452
   220
wenzelm@43452
   221
    val isabelle_token_marker = new Token_Markup.Marker
wenzelm@43452
   222
wenzelm@43452
   223
    override def loadMode(mode: Mode, xmh: XModeHandler)
wenzelm@43452
   224
    {
wenzelm@43452
   225
      super.loadMode(mode, xmh)
wenzelm@43452
   226
      if (mode.getName == "isabelle")
wenzelm@43452
   227
        mode.setTokenMarker(isabelle_token_marker)
wenzelm@43414
   228
    }
wenzelm@43452
   229
  }
wenzelm@43414
   230
}
wenzelm@43414
   231