@@ -11,13 +11,10 @@ import org.apache.batik.dom.GenericDOMImplementation
1111import org.apache.batik.svggen.SVGGeneratorContext
1212import org.apache.batik.svggen.SVGGraphics2D
1313import org.apache.commons.text.StringEscapeUtils
14- import org.scilab.forge.jlatexmath.TeXConstants
15- import org.scilab.forge.jlatexmath.TeXFormula
16- import org.scilab.forge.jlatexmath.TeXIcon
14+ import org.scilab.forge.jlatexmath.*
1715import org.w3c.dom.DOMImplementation
1816import org.w3c.dom.Document
1917import java.awt.Dimension
20- import java.awt.Insets
2118import java.io.StringWriter
2219import java.nio.file.Path
2320import kotlin.io.path.absolute
@@ -270,25 +267,60 @@ fun parseMarkdownFrontmatter(srcText: String): Pair<Map<String?, Any?>?, String>
270267 return frontmatter to srcText.substring(nextSeparator + 4 )
271268}
272269
273- fun renderTeX (string : String ): String {
274- val formula = TeXFormula (string)
275- val icon: TeXIcon = formula.createTeXIcon(TeXConstants .STYLE_DISPLAY , 20f )
276- icon.setForeground(java.awt.Color .WHITE )
270+ enum class TeXStyle (val intStyle : Int ) {
271+ /* *
272+ * The large versions of big operators are used and limits are placed under and over
273+ * these operators (default). Symbols are rendered in the largest size.
274+ */
275+ Display (TeXConstants .STYLE_DISPLAY ),
277276
278- val domImpl: DOMImplementation = GenericDOMImplementation .getDOMImplementation()
279- val document: Document = domImpl.createDocument(null , " svg" , null )
280- val svgGenerator = SVGGraphics2D (SVGGeneratorContext .createDefault(document).apply {
281- graphicContextDefaults = SVGGeneratorContext .GraphicContextDefaults ().apply {
282- paint = java.awt.Color .WHITE
283- }
284- }, true )
277+ /* *
278+ * The small versions of big operators are used and limits are attached to
279+ * these operators as scripts (default). The same size as in the display style
280+ * is used to render symbols.
281+ */
282+ Text (TeXConstants .STYLE_TEXT ),
283+
284+ /* *
285+ * The same as the text style, but symbols are rendered in a smaller size.
286+ */
287+ Script (TeXConstants .STYLE_SCRIPT ),
288+
289+ /* *
290+ * The same as the script style, but symbols are rendered in a smaller size.
291+ */
292+ SmallScript (TeXConstants .STYLE_SCRIPT_SCRIPT ),
293+ }
294+
295+ fun renderTeX (string : String , style : TeXStyle = TeXStyle .Display ): String? {
296+ try {
297+ val formula = TeXFormula (string)
298+ val icon: TeXIcon = formula.createTeXIcon(style.intStyle, 20f )
299+ icon.setForeground(java.awt.Color .WHITE )
285300
286- svgGenerator.setSVGCanvasSize(Dimension (icon.iconWidth, icon.iconHeight))
287- icon.paintIcon(null , svgGenerator, 0 , 0 )
301+ val domImpl: DOMImplementation = GenericDOMImplementation .getDOMImplementation()
302+ val document: Document = domImpl.createDocument(null , " svg" , null )
303+ val svgGenerator = SVGGraphics2D (SVGGeneratorContext .createDefault(document).apply {
304+ graphicContextDefaults = SVGGeneratorContext .GraphicContextDefaults ().apply {
305+ paint = java.awt.Color .WHITE
306+ }
307+ }, true )
308+
309+ svgGenerator.setSVGCanvasSize(Dimension (icon.iconWidth, icon.iconHeight))
310+ icon.paintIcon(null , svgGenerator, 0 , 0 )
288311
289- val writer = StringWriter ()
290- svgGenerator.stream(writer)
291- return writer.toString().replace(
292- " <svg" , """ <svg data-formula="${string.escapeXml()} " class="latex"""""
293- )
312+ val writer = StringWriter ()
313+ svgGenerator.stream(writer)
314+ return writer.toString().replace(
315+ " <svg" , """ <svg data-formula="${string.escapeXml()} " class="latex"""""
316+ )
317+ } catch (e: JMathTeXException ) {
318+ val lineCount = string.lines().size
319+ if (lineCount == 1 ) {
320+ err(" TeX formula $string failed to parse: ${e.message} " )
321+ } else {
322+ err(" TeX formula\n ${string.prependIndent(" " )} \n failed to parse: ${e.message} " )
323+ }
324+ return null
325+ }
294326}
0 commit comments