<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="TexMathsPrefs" script:language="StarBasic">&apos;
&apos;    TexMathsPrefs
&apos;
&apos;	 Copyright (C) 2012-2026 Roland Baudin (roland65@free.fr)
&apos;    Based on the work of Geoffroy Piroux (gpiroux@gmail.com)
&apos;
&apos;    This program is free software; you can redistribute it and/or modify
&apos;    it under the terms of the GNU General Public License as published by
&apos;    the Free Software Foundation; either version 2 of the License, or
&apos;    (at your option) any later version.
&apos;
&apos;    This program is distributed in the hope that it will be useful,
&apos;    but WITHOUT ANY WARRANTY; without even the implied warranty of
&apos;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
&apos;    GNU General Public License for more details.
&apos;
&apos;    You should have received a copy of the GNU General Public License
&apos;    along with this program; if not, write to the Free Software
&apos;    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
&apos;

&apos;    Macros related to main preferences

&apos; Force variable declaration
Option Explicit


&apos; Set user, temp and package paths
Sub DefaultSysConfig()

	&apos; Service to access the user&apos;s paths information
	Dim aService as Variant, oFileAccess as Variant
	aService = CreateUnoService(&quot;com.sun.star.util.PathSubstitution&quot;)
	oFileAccess = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
	
	&apos; User path and temp path
 	glb_UserPath = aService.substituteVariables(&quot;$(user)&quot;, TRUE) &amp; &quot;/TexMaths/&quot;
	glb_TmpPath  = aService.substituteVariables(&quot;$(user)&quot;, TRUE) &amp; &quot;/TexMaths/tmp/&quot;
          
	&apos; Create the user directory if it doesn&apos;t exist
	If Not oFileAccess.exists(glb_UserPath) Then MkDir(glb_UserPath)
	If Not oFileAccess.exists(glb_TmpPath)  Then MkDir(glb_TmpPath)

	&apos; Get the package path
	Const sPrefix = &quot;vnd.sun.star.expand:&quot;
	Dim oSystemInfo as Variant
	oSystemInfo = GetConfigAccess( &quot;/ooo.ext.texmaths.Registry/SystemInfo&quot;, TRUE)
	glb_PkgPath = Mid( oSystemInfo.PackageDir , len(sPrefix) + 1)
	
	&apos; Get the default context
	Dim oContext as Variant, oMacroExpander as Variant
	oContext = getProcessServiceManager().DefaultContext
	oMacroExpander = oContext.getValueByName(&quot;/singletons/com.sun.star.util.theMacroExpander&quot;)
	glb_PkgPath = oMacroExpander.ExpandMacros(glb_PkgPath)

End Sub


&apos; Set default preferences
Sub DefaultPrefs()

	&apos; LaTeX preferences
	glb_WriterSize = &quot;12&quot;
	glb_ImpressSize = &quot;28&quot;
	glb_DrawSize = &quot;14&quot;
	glb_WriterEquationType = &quot;display&quot;
	glb_ImpressEquationType = &quot;display&quot;
	glb_DrawEquationType = &quot;display&quot;

	&apos; Image preferences
	glb_Format = &quot;svg&quot;

	&apos; Force default to png if the dvisvgm path is empty
	Dim oSystemInfo as Variant
	oSystemInfo = GetConfigAccess( &quot;/ooo.ext.texmaths.Registry/SystemInfo&quot;, TRUE)	
	If oSystemInfo.DvisvgmPath = &quot;&quot; Then glb_Format = &quot;png&quot;
	
	glb_GraphicDPI = &quot;600&quot;
	glb_WriterTransparency = &quot;FALSE&quot;
	glb_ImpressTransparency = &quot;TRUE&quot;
	glb_DrawTransparency = &quot;TRUE&quot;
	glb_Status = _(&quot;Default preferences loaded...&quot;)

End Sub


&apos; Return a String with the current preferences
&apos; to be written to the preferences file
Function Preferences() as String

	Preferences = _
	&quot;GraphicDPI=&quot; &amp; glb_GraphicDPI &amp; chr(10) &amp;_
	&quot;FileFormat=&quot; &amp; glb_Format &amp; chr(10) &amp;_
	&quot;WriterTransparency=&quot; &amp; glb_WriterTransparency &amp; chr(10) &amp;_
	&quot;ImpressTransparency=&quot; &amp; glb_ImpressTransparency &amp; chr(10) &amp;_
	&quot;DrawTransparency=&quot; &amp; glb_DrawTransparency &amp; chr(10) &amp;_
	&quot;WriterCharSize=&quot; &amp; glb_WriterSize &amp; chr(10) &amp;_
	&quot;WriterEquationType=&quot; &amp; glb_WriterEquationType &amp; chr(10) &amp;_
	&quot;ImpresCharSize=&quot; &amp; glb_ImpressSize &amp; chr(10) &amp;_
	&quot;ImpressEquationType=&quot; &amp; glb_ImpressEquationType &amp; chr(10) &amp;_
	&quot;DrawCharSize=&quot; &amp; glb_DrawSize &amp; chr(10) &amp;_
	&quot;DrawEquationType=&quot; &amp; glb_DrawEquationType &amp; chr(10)

End Function


&apos; Read the user&apos;s preferences file
&apos; Call the routine for data parsing
Sub ReadPrefFileName()

	Dim iNumber as Integer
	Dim strline as String, prefs as String
	prefs = &quot;&quot;
	iNumber = Freefile()
	Open glb_UserPath &amp; &quot;TexMaths.cfg&quot; For Input as iNumber
	While Not EOF(iNumber)

		Line Input #iNumber, strline
		If strline &lt;&gt; &quot;&quot; Then prefs = prefs &amp; chr(10) &amp; strline 

	Wend
	Close #iNumber
	ReadPreferencesFrom(prefs)

	glb_Status = _(&quot;User&apos;s preferences loaded...&quot;)

End Sub


&apos; This fonction parses the preferences from string prefs
Sub ReadPreferencesFrom(prefs as String)

	Dim i as Integer, ii as Integer
	Dim SplittedLine() as String, Arguments() as String

	If StringNotContains(prefs, chr(10)) Then Exit Sub
	SplittedLine() = Split(prefs, chr(10))
	ii = 0
	
	For i = 0 to UBound(SplittedLine)

		If Not StringNotContains(SplittedLine(i), &quot;=&quot;) Then 

			Arguments() = Split(SplittedLine(i), &quot;=&quot;)

			Select Case Arguments(0)

				Case &quot;ScriptPath&quot;

					Dim scriptPath as String
					scriptPath = CheckPath(ConvertToURL(Arguments(1)))

				Case &quot;GraphicDPI&quot;

					glb_GraphicDPI = Arguments(1)

				Case &quot;FileFormat&quot;

					glb_Format = Arguments(1)

				Case &quot;WriterTransparency&quot;

					glb_WriterTransparency = Arguments(1)

				Case &quot;ImpressTransparency&quot;

					glb_ImpressTransparency = Arguments(1)

				Case &quot;DrawTransparency&quot;

					glb_DrawTransparency = Arguments(1)

				Case &quot;WriterCharSize&quot;

					glb_WriterSize = Arguments(1)

				Case &quot;WriterEquationType&quot;

					glb_WriterEquationType = Arguments(1)

				Case &quot;ImpresCharSize&quot;

					glb_ImpressSize = Arguments(1)

				Case &quot;ImpressEquationType&quot;

					glb_ImpressEquationType = Arguments(1)

				Case &quot;DrawCharSize&quot;

					glb_DrawSize = Arguments(1)

				Case &quot;DrawEquationType&quot;

					glb_DrawEquationType = Arguments(1)

			End Select

		End If

		ii = ii + 1 + Len(SplittedLine(i))
		If Len(prefs) &lt; ii  Then Exit For

	Next

End Sub


&apos; Set default preamble
Sub SetDefaultPreamble()

	Dim oSystemInfo as Variant
	oSystemInfo = GetConfigAccess(&quot;/ooo.ext.texmaths.Registry/SystemInfo&quot;, TRUE)
	
	&apos; Get compiler
	glb_Compiler = oSystemInfo.Compiler
	
	&apos; Default preamble
	glb_Preamble = &quot;\usepackage{amsmath}&quot; &amp; chr(10) &amp;_
	&quot;\usepackage{amssymb}&quot; &amp; chr(10) &amp;_
	&quot;\usepackage[usenames]{color}&quot; &amp; chr(10) &amp;_
	&quot;\usepackage{ifxetex}&quot; &amp; chr(10) &amp;_
	&quot;\usepackage{ifluatex}&quot; &amp; chr(10) &amp;_
	chr(10) &amp;_
	&quot;% XeLaTeX compiler&quot; &amp; chr(10) &amp;_
	&quot;\ifxetex&quot; &amp; chr(10) &amp;_
	chr(10) &amp;_
	&quot;    \usepackage{fontspec}&quot; &amp; chr(10) &amp;_
	&quot;    \usepackage{unicode-math}&quot; &amp; chr(10) &amp;_
	chr(10) &amp;_
	&quot;    % Uncomment these lines for alternative fonts&quot; &amp; chr(10) &amp;_
	&quot;    %\setmainfont{FreeSerif}&quot; &amp; chr(10) &amp;_
	&quot;    %\setmathfont{FreeSerif}&quot; &amp; chr(10) &amp;_
	chr(10) &amp;_
	&quot;% LuaLaTeX compiler&quot; &amp; chr(10) &amp;_
	&quot;\else\ifluatex&quot; &amp; chr(10) &amp;_
	chr(10) &amp;_
	&quot;    \usepackage{fontspec}&quot; &amp; chr(10) &amp;_
	&quot;    \usepackage{lualatex-math}&quot; &amp; chr(10) &amp;_
	chr(10) &amp;_
	&quot;% LaTeX compiler&quot; &amp; chr(10) &amp;_
	&quot;\else&quot; &amp; chr(10) &amp;_
	chr(10) &amp;_
	&quot;    % Uncomment this line for sans-serif maths font&quot; &amp; chr(10) &amp;_
	&quot;    %\everymath{\mathsf{\xdef\mysf{\mathgroup\the\mathgroup\relax}}\mysf}&quot; &amp; chr(10) &amp;_
	chr(10) &amp;_
	&quot;\fi\fi&quot; &amp;_
	chr(10) &amp;_
	chr(10) &amp;_
	&quot;% Uncomment this line for custom color&quot; &amp; chr(10) &amp;_
	&quot;%\definecolor{mycolor}{RGB}{0, 130, 180}&quot; &amp; chr(10) &amp;_
	chr(10)
	
	&apos; Default ignore preamble state
	glb_IgnorePreamble = FALSE 

End Sub


&apos; Read local preamble - Return 1 if a local preamble was found, else return 0
Function ReadLocalPreamble() as Integer
	
	&apos; Read the local preamble from a UserDefinedProperties property
	Dim oDoc as Variant, oProperties as Variant
	
	oDoc = ThisComponent
	oProperties = oDoc.DocumentProperties.UserDefinedProperties
	
	&apos; No local stored LaTeX preamble
	If Not oProperties.PropertySetInfo.hasPropertyByName(&quot;TexMathsPreamble&quot;) Then
	
		ReadLocalPreamble = 0
		Exit Function
	
	&apos; Read local stored LaTeX preamble
	Else
	
		&apos; Decode the local stored preamble String to restore the newline characters (workaround for an LO bug)
		glb_Preamble = DecodeNewline( oProperties.getPropertyValue(&quot;TexMathsPreamble&quot;) )

		&apos; Set the ignore preamble state
		If oProperties.PropertySetInfo.hasPropertyByName(&quot;TexMathignorePreamble&quot;) Then 		
			
			If oProperties.getPropertyValue(&quot;TexMathignorePreamble&quot;) = &quot;TRUE&quot; Then glb_IgnorePreamble = TRUE Else glb_IgnorePreamble = FALSE
		
		End If
	
		ReadLocalPreamble = 1
	
	End If

End Function



&apos; Read global preamble - Return 1 if a global preamble was found, else return 0
Function ReadGlobalPreamble() as Integer

	Dim URL as String
	 
	URL = ConvertFromURL(glb_UserPath) &amp; &quot;GlobalPreamble.tex&quot;

	&apos; Global preamble does not exist, return 0
	If Not FileExists(URL) Then

		ReadGlobalPreamble = 0
		Exit Function

	&apos; Global preamble exists, read it and return 1
	Else

		&apos; Read and set global preamble
		Dim oTextFile as Variant, oFileAccess as Variant, oFileStream as Variant
		Dim str as String, strline as String
	
		oFileAccess = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
		oFileStream = oFileAccess.openFileRead(URL)
		oTextFile = createUnoService(&quot;com.sun.star.io.TextInputStream&quot;)
		oTextFile.InputStream = oFileStream
	
		str = &quot;&quot;
		Do While Not oTextFile.IsEOF

	  		strline = oTextFile.readLine
			str = str &amp; strline &amp; chr(10)

		Loop
	
		oFileStream.closeInput
		oTextFile.closeInput
	
		glb_Preamble = str
	
	 	&apos; Read IgnorePreamble variable
		Dim oSystemInfo as Variant
		oSystemInfo = GetConfigAccess(&quot;/ooo.ext.texmaths.Registry/SystemInfo&quot;, TRUE)
		
		If oSystemInfo.IgnorePreamble = &quot;TRUE&quot; Then glb_IgnorePreamble = TRUE Else glb_IgnorePreamble = FALSE

		ReadGlobalPreamble = 1

	End If
	
End Function
	
</script:module>