src/Tools/jEdit/src/text_area_painter.scala
author wenzelm
Wed Jun 22 16:01:30 2011 +0200 (2011-06-22)
changeset 43505 0aca4edbfa99
parent 43451 be760a642d38
child 43506 bf7400573617
permissions -rw-r--r--
clarified chunk.offset, chunk.length;
     1 /*  Title:      Tools/jEdit/src/text_area_painter.scala
     2     Author:     Makarius
     3 
     4 Painter setup for main jEdit text area.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import java.awt.{Graphics2D, Shape}
    13 import java.awt.font.TextAttribute
    14 import java.text.AttributedString
    15 import java.util.ArrayList
    16 
    17 import org.gjt.sp.jedit.Debug
    18 import org.gjt.sp.jedit.syntax.{DisplayTokenHandler, Chunk}
    19 import org.gjt.sp.jedit.textarea.{TextAreaExtension, TextAreaPainter, JEditTextArea}
    20 
    21 
    22 class Text_Area_Painter(doc_view: Document_View)
    23 {
    24   private val model = doc_view.model
    25   private val buffer = model.buffer
    26   private val text_area = doc_view.text_area
    27 
    28 
    29   /* original painters */
    30 
    31   private def pick_extension(name: String): TextAreaExtension =
    32   {
    33     text_area.getPainter.getExtensions.iterator.filter(x => x.getClass.getName == name).toList
    34     match {
    35       case List(x) => x
    36       case _ => error("Expected exactly one " + name)
    37     }
    38   }
    39 
    40   private val orig_text_painter =
    41     pick_extension("org.gjt.sp.jedit.textarea.TextAreaPainter$PaintText")
    42 
    43 
    44   /* common painter state */
    45 
    46   @volatile private var painter_snapshot: Document.Snapshot = null
    47   @volatile private var painter_clip: Shape = null
    48 
    49   private val set_state = new TextAreaExtension
    50   {
    51     override def paintScreenLineRange(gfx: Graphics2D,
    52       first_line: Int, last_line: Int, physical_lines: Array[Int],
    53       start: Array[Int], end: Array[Int], y: Int, line_height: Int)
    54     {
    55       doc_view.robust_body(()) {
    56         painter_snapshot = doc_view.update_snapshot()
    57         painter_clip = gfx.getClip
    58       }
    59     }
    60   }
    61 
    62   private val reset_state = new TextAreaExtension
    63   {
    64     override def paintScreenLineRange(gfx: Graphics2D,
    65       first_line: Int, last_line: Int, physical_lines: Array[Int],
    66       start: Array[Int], end: Array[Int], y: Int, line_height: Int)
    67     {
    68       doc_view.robust_body(()) {
    69         painter_snapshot = null
    70         painter_clip = null
    71       }
    72     }
    73   }
    74 
    75 
    76   /* text background */
    77 
    78   private val background_painter = new TextAreaExtension
    79   {
    80     override def paintScreenLineRange(gfx: Graphics2D,
    81       first_line: Int, last_line: Int, physical_lines: Array[Int],
    82       start: Array[Int], end: Array[Int], y: Int, line_height: Int)
    83     {
    84       doc_view.robust_body(()) {
    85         val snapshot = painter_snapshot
    86         val ascent = text_area.getPainter.getFontMetrics.getAscent
    87 
    88         for (i <- 0 until physical_lines.length) {
    89           if (physical_lines(i) != -1) {
    90             val line_range = doc_view.proper_line_range(start(i), end(i))
    91 
    92             // background color: status
    93             for {
    94               (command, command_start) <- snapshot.node.command_range(snapshot.revert(line_range))
    95               if !command.is_ignored
    96               range <- line_range.try_restrict(snapshot.convert(command.range + command_start))
    97               r <- Isabelle.gfx_range(text_area, range)
    98               color <- Isabelle_Markup.status_color(snapshot, command)
    99             } {
   100               gfx.setColor(color)
   101               gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
   102             }
   103 
   104             // background color (1): markup
   105             for {
   106               Text.Info(range, Some(color)) <-
   107                 snapshot.select_markup(line_range)(Isabelle_Markup.background1).iterator
   108               r <- Isabelle.gfx_range(text_area, range)
   109             } {
   110               gfx.setColor(color)
   111               gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
   112             }
   113 
   114             // background color (2): markup
   115             for {
   116               Text.Info(range, Some(color)) <-
   117                 snapshot.select_markup(line_range)(Isabelle_Markup.background2).iterator
   118               r <- Isabelle.gfx_range(text_area, range)
   119             } {
   120               gfx.setColor(color)
   121               gfx.fillRect(r.x + 2, y + i * line_height + 2, r.length - 4, line_height - 4)
   122             }
   123 
   124             // squiggly underline
   125             for {
   126               Text.Info(range, Some(color)) <-
   127                 snapshot.select_markup(line_range)(Isabelle_Markup.message).iterator
   128               r <- Isabelle.gfx_range(text_area, range)
   129             } {
   130               gfx.setColor(color)
   131               val x0 = (r.x / 2) * 2
   132               val y0 = r.y + ascent + 1
   133               for (x1 <- Range(x0, x0 + r.length, 2)) {
   134                 val y1 = if (x1 % 4 < 2) y0 else y0 + 1
   135                 gfx.drawLine(x1, y1, x1 + 1, y1)
   136               }
   137             }
   138           }
   139         }
   140       }
   141     }
   142   }
   143 
   144 
   145   /* text */
   146 
   147   def char_width(): Int =
   148   {
   149     val painter = text_area.getPainter
   150     val font = painter.getFont
   151     val font_context = painter.getFontRenderContext
   152     font.getStringBounds(" ", font_context).getWidth.round.toInt
   153   }
   154 
   155   private def line_infos(physical_lines: Iterator[Int]): Map[Text.Offset, Chunk] =
   156   {
   157     val painter = text_area.getPainter
   158     val font = painter.getFont
   159     val font_context = painter.getFontRenderContext
   160 
   161     // see org.gjt.sp.jedit.textarea.TextArea.propertiesChanged
   162     // see org.gjt.sp.jedit.textarea.TextArea.setMaxLineLength
   163     val margin =
   164       if (buffer.getStringProperty("wrap") != "soft") 0.0f
   165       else {
   166         val max = buffer.getIntegerProperty("maxLineLen", 0)
   167         if (max > 0) font.getStringBounds(" " * max, font_context).getWidth.toInt
   168         else painter.getWidth - char_width() * 3
   169       }.toFloat
   170 
   171     val out = new ArrayList[Chunk]
   172     val handler = new DisplayTokenHandler
   173 
   174     var result = Map[Text.Offset, Chunk]()
   175     for (line <- physical_lines) {
   176       out.clear
   177       handler.init(painter.getStyles, font_context, painter, out, margin)
   178       buffer.markTokens(line, handler)
   179 
   180       val line_start = buffer.getLineStartOffset(line)
   181       for (i <- 0 until out.size) {
   182         val chunk = out.get(i)
   183         result += (line_start + chunk.offset -> chunk)
   184       }
   185     }
   186     result
   187   }
   188 
   189   private def paint_chunk_list(
   190     gfx: Graphics2D, line_start: Text.Offset, head: Chunk, x: Float, y: Float): Float =
   191   {
   192     val clip_rect = gfx.getClipBounds
   193     val painter = text_area.getPainter
   194     val font_context = painter.getFontRenderContext
   195 
   196     var w = 0.0f
   197     var chunk = head
   198     while (chunk != null) {
   199       val chunk_offset = line_start + chunk.offset
   200       if (x + w + chunk.width > clip_rect.x &&
   201           x + w < clip_rect.x + clip_rect.width &&
   202           chunk.accessable && chunk.visible)
   203       {
   204         val chunk_range = Text.Range(chunk_offset, chunk_offset + chunk.length)
   205         val chunk_font = chunk.style.getFont
   206         val chunk_color = chunk.style.getForegroundColor
   207 
   208         val caret_range =
   209           if (text_area.hasFocus) doc_view.caret_range()
   210           else Text.Range(-1)
   211 
   212         val markup =
   213           for {
   214             r1 <- painter_snapshot.select_markup(chunk_range)(Isabelle_Markup.text_color)
   215             r2 <- r1.try_restrict(chunk_range)
   216           } yield r2
   217 
   218         var x1 = x + w
   219         gfx.setFont(chunk_font)
   220         if (markup.isEmpty) {
   221           gfx.setColor(chunk_color)
   222           gfx.drawString(chunk.str, x1, y)
   223         }
   224         else {
   225           for {
   226             Text.Info(range, info) <-
   227               Iterator(Text.Info(Text.Range(chunk_range.start, markup.head.range.start), None)) ++
   228               markup.iterator ++
   229               Iterator(Text.Info(Text.Range(markup.last.range.stop, chunk_range.stop), None))
   230             if !range.is_singularity
   231           } {
   232             val str = chunk.str.substring(range.start - chunk_offset, range.stop - chunk_offset)
   233             gfx.setColor(info.getOrElse(chunk_color))
   234 
   235             range.try_restrict(caret_range) match {
   236               case Some(r) if !r.is_singularity =>
   237                 val astr = new AttributedString(str)
   238                 val i = r.start - range.start
   239                 val j = r.stop - range.start
   240                 astr.addAttribute(TextAttribute.FONT, chunk_font)
   241                 astr.addAttribute(TextAttribute.FOREGROUND, painter.getCaretColor, i, j)
   242                 astr.addAttribute(TextAttribute.SWAP_COLORS, TextAttribute.SWAP_COLORS_ON, i, j)
   243                 gfx.drawString(astr.getIterator, x1, y)
   244               case _ =>
   245                 gfx.drawString(str, x1, y)
   246             }
   247             x1 += chunk_font.getStringBounds(str, font_context).getWidth.toFloat
   248           }
   249         }
   250       }
   251       w += chunk.width
   252       chunk = chunk.next.asInstanceOf[Chunk]
   253     }
   254     w
   255   }
   256 
   257   private val text_painter = new TextAreaExtension
   258   {
   259     override def paintScreenLineRange(gfx: Graphics2D,
   260       first_line: Int, last_line: Int, physical_lines: Array[Int],
   261       start: Array[Int], end: Array[Int], y: Int, line_height: Int)
   262     {
   263       doc_view.robust_body(()) {
   264         val clip = gfx.getClip
   265         val x0 = text_area.getHorizontalOffset
   266         val fm = text_area.getPainter.getFontMetrics
   267         var y0 = y + fm.getHeight - (fm.getLeading + 1) - fm.getDescent
   268 
   269         val infos = line_infos(physical_lines.iterator.filter(i => i != -1))
   270         for (i <- 0 until physical_lines.length) {
   271           if (physical_lines(i) != -1) {
   272             val x1 =
   273               infos.get(start(i)) match {
   274                 case None => x0
   275                 case Some(chunk) =>
   276                   gfx.clipRect(x0, y + line_height * i, Integer.MAX_VALUE, line_height)
   277                   val w = paint_chunk_list(gfx, start(i), chunk, x0, y0).toInt
   278                   x0 + w.toInt
   279               }
   280             gfx.clipRect(x1, 0, Integer.MAX_VALUE, Integer.MAX_VALUE)
   281             orig_text_painter.paintValidLine(gfx,
   282               first_line + i, physical_lines(i), start(i), end(i), y + line_height * i)
   283             gfx.setClip(clip)
   284           }
   285           y0 += line_height
   286         }
   287       }
   288     }
   289   }
   290 
   291 
   292   /* foreground */
   293 
   294   private val foreground_painter = new TextAreaExtension
   295   {
   296     override def paintScreenLineRange(gfx: Graphics2D,
   297       first_line: Int, last_line: Int, physical_lines: Array[Int],
   298       start: Array[Int], end: Array[Int], y: Int, line_height: Int)
   299     {
   300       doc_view.robust_body(()) {
   301         val snapshot = painter_snapshot
   302 
   303         for (i <- 0 until physical_lines.length) {
   304           if (physical_lines(i) != -1) {
   305             val line_range = doc_view.proper_line_range(start(i), end(i))
   306 
   307             // highlighted range -- potentially from other snapshot
   308             doc_view.highlight_range match {
   309               case Some((range, color)) if line_range.overlaps(range) =>
   310                 Isabelle.gfx_range(text_area, line_range.restrict(range)) match {
   311                   case None =>
   312                   case Some(r) =>
   313                     gfx.setColor(color)
   314                     gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
   315                 }
   316               case _ =>
   317             }
   318           }
   319         }
   320       }
   321     }
   322   }
   323 
   324 
   325   /* caret -- outside of text range */
   326 
   327   private class Caret_Painter(before: Boolean) extends TextAreaExtension
   328   {
   329     override def paintValidLine(gfx: Graphics2D,
   330       screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
   331     {
   332       doc_view.robust_body(()) {
   333         if (before) gfx.clipRect(0, 0, 0, 0)
   334         else gfx.setClip(painter_clip)
   335       }
   336     }
   337   }
   338 
   339   private val before_caret_painter1 = new Caret_Painter(true)
   340   private val after_caret_painter1 = new Caret_Painter(false)
   341   private val before_caret_painter2 = new Caret_Painter(true)
   342   private val after_caret_painter2 = new Caret_Painter(false)
   343 
   344   private val caret_painter = new TextAreaExtension
   345   {
   346     override def paintValidLine(gfx: Graphics2D,
   347       screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
   348     {
   349       doc_view.robust_body(()) {
   350         if (text_area.hasFocus) {
   351           val caret = text_area.getCaretPosition
   352           if (start <= caret && caret == end - 1) {
   353             val painter = text_area.getPainter
   354             val fm = painter.getFontMetrics
   355 
   356             val offset = caret - text_area.getLineStartOffset(physical_line)
   357             val x = text_area.offsetToXY(physical_line, offset).x
   358             gfx.setColor(painter.getCaretColor)
   359             gfx.drawRect(x, y, char_width() - 1, fm.getHeight - 1)
   360           }
   361         }
   362       }
   363     }
   364   }
   365 
   366 
   367   /* activation */
   368 
   369   def activate()
   370   {
   371     val painter = text_area.getPainter
   372     painter.addExtension(TextAreaPainter.LOWEST_LAYER, set_state)
   373     painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, background_painter)
   374     painter.addExtension(TextAreaPainter.TEXT_LAYER, text_painter)
   375     painter.addExtension(TextAreaPainter.CARET_LAYER - 1, before_caret_painter1)
   376     painter.addExtension(TextAreaPainter.CARET_LAYER + 1, after_caret_painter1)
   377     painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER - 1, before_caret_painter2)
   378     painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 1, after_caret_painter2)
   379     painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 2, caret_painter)
   380     painter.addExtension(500, foreground_painter)
   381     painter.addExtension(TextAreaPainter.HIGHEST_LAYER, reset_state)
   382     painter.removeExtension(orig_text_painter)
   383   }
   384 
   385   def deactivate()
   386   {
   387     val painter = text_area.getPainter
   388     painter.addExtension(TextAreaPainter.TEXT_LAYER, orig_text_painter)
   389     painter.removeExtension(reset_state)
   390     painter.removeExtension(foreground_painter)
   391     painter.removeExtension(caret_painter)
   392     painter.removeExtension(after_caret_painter2)
   393     painter.removeExtension(before_caret_painter2)
   394     painter.removeExtension(after_caret_painter1)
   395     painter.removeExtension(before_caret_painter1)
   396     painter.removeExtension(text_painter)
   397     painter.removeExtension(background_painter)
   398     painter.removeExtension(set_state)
   399   }
   400 }
   401