diff src/name/blackcap/clipman/Main.kt @ 13:fe0fcfc8b2aa

Braindead POS wasn't honoring <style> tags; fixed that.
author David Barts <n5jrn@me.com>
date Mon, 20 Jan 2020 21:53:15 -0800
parents 0fb6da371b80
children 0cd912d29184
line wrap: on
line diff
--- a/src/name/blackcap/clipman/Main.kt	Sun Jan 19 19:44:42 2020 -0800
+++ b/src/name/blackcap/clipman/Main.kt	Mon Jan 20 21:53:15 2020 -0800
@@ -13,12 +13,14 @@
 import java.awt.event.WindowEvent
 import java.awt.event.WindowListener
 import java.util.Date
+import java.util.concurrent.Semaphore
 import java.util.logging.Level
 import java.util.logging.Logger
-import java.util.concurrent.Semaphore
 import javax.swing.*
 import javax.swing.border.*
 import javax.swing.text.JTextComponent
+import javax.swing.text.html.StyleSheet
+import javax.swing.text.html.HTMLEditorKit
 import kotlin.concurrent.thread
 import org.jsoup.Jsoup
 import org.jsoup.nodes.*
@@ -37,6 +39,10 @@
 val INNER_BORDER = 1
 val MARGIN_BORDER = 3
 
+/* default font sizes in the text-display panes */
+val MONO_SIZE = 14
+val PROP_SIZE = 16
+
 /* kills the updating thread (and does a system exit) when needed */
 class KillIt(val thr: Thread) : WindowListener {
     // events we don't care about
@@ -80,7 +86,7 @@
                 if (contents != null) {
                     newContents = when (contents) {
                         is PasteboardItem.Plain -> contents.plain
-                        is PasteboardItem.HTML -> contents.plain
+                        is PasteboardItem.HTML -> contents.html
                     }
                     if (oldContents != newContents) {
                         var stdWidth: Int? = null
@@ -98,7 +104,7 @@
                                 add(ClipText().apply {
                                     contentType = "text/plain"
                                     text = contents.plain
-                                    font = Font(Font.MONOSPACED, Font.PLAIN, 14)
+                                    font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
                                     border = stdBorder
                                     autoSize(stdWidth!!)
                                     setEditable(false)
@@ -107,9 +113,13 @@
                             }
                             is PasteboardItem.HTML -> widget.run {
                                 add(stdLabel("Styled text"))
+                                val (html, style) = preproc(contents.html)
+                                val hek = HTMLEditorKit().apply {
+                                    styleSheet = style
+                                }
                                 add(ClipText().apply {
-                                    contentType = "text/html"
-                                    text = scrub(contents.html)
+                                    editorKit = hek
+                                    text = html
                                     border = stdBorder
                                     autoSize(stdWidth!!)
                                     setEditable(false)
@@ -138,14 +148,22 @@
         alignmentX = JLabel.LEFT_ALIGNMENT
     }
 
-    private fun scrub(html: String): String {
-        return Jsoup.parse(html).run {
+    private fun preproc(html: String): Pair<String, StyleSheet> {
+        val sty = StyleSheet().apply {
+            addRule("html { font-family: serif; font-size: %d; }".format(PROP_SIZE))
+            addRule("code, kbd, pre, samp { font-family: monospace; font-size: %d; }".format(MONO_SIZE))
+        }
+        val scrubbed = Jsoup.parse(html).run {
+            select("style").forEach {
+                it.dataNodes().forEach { sty.addRule(it.wholeData) }
+            }
             select(":root>head>meta").remove()
             outputSettings()
                 .charset(CHARSET_NAME)
                 .syntax(Document.OutputSettings.Syntax.xml)
             outerHtml()
         }
+        return Pair(scrubbed, sty)
     }
 }