Refactor parts of the RenderingService (to make it thread-safe soon).
diff --git a/PowerPointLaTeX/PowerPointLaTeX/Equations/EquationEditor.cs b/PowerPointLaTeX/PowerPointLaTeX/Equations/EquationEditor.cs
index 9bc8f83..52b4ab2 100644
--- a/PowerPointLaTeX/PowerPointLaTeX/Equations/EquationEditor.cs
+++ b/PowerPointLaTeX/PowerPointLaTeX/Equations/EquationEditor.cs
@@ -56,9 +56,6 @@
             //updatePreview();

         }

 

-        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {

-        }

-

         void updatePreviewTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {

             formulaPreview.Invoke( new MethodInvoker(updatePreview) );

         }

diff --git a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/ILaTeXRenderingService.cs b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/ILaTeXRenderingService.cs
index 0b686b0..7e32cd4 100644
--- a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/ILaTeXRenderingService.cs
+++ b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/ILaTeXRenderingService.cs
@@ -26,6 +26,30 @@
 

 namespace PowerPointLaTeX

 {

+    public struct LaTeXCompilationTask

+    {

+        public string code;

+

+        /// <summary>

+        /// target font size (in pixels)

+        /// </summary>

+        public float pixelsPerEmHeight;

+    }

+

+    public struct LaTeXCompilationResult

+    {

+        public byte[] imageData;

+

+        public float baselineOffset;

+

+        /// <summary>

+        /// actual font size (in pixels)

+        /// </summary>

+        public float pixelsPerEmHeight;

+

+        public string report;

+    }

+

     public interface ILaTeXRenderingService

     {

         string AboutNotice {

@@ -47,8 +71,6 @@
         /// The depth is a negative offset in this case, so the minus sign is necessary, and the unit is pixels (px).

         /// </param>

         /// <returns>returns false if there was an error</returns>

-        bool RenderLaTeXCode(string latexCode, out byte[] imageData, ref float pixelsPerEmHeight, out int baselineOffset);

-

-        string GetLastErrorReport();

+        LaTeXCompilationResult RenderLaTeXCode(LaTeXCompilationTask task);

     }

 }

diff --git a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/LaTeXRendering.cs b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/LaTeXRendering.cs
index c661fa6..72da55c 100644
--- a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/LaTeXRendering.cs
+++ b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/LaTeXRendering.cs
@@ -34,6 +34,11 @@
         // renders all formulas at a higher resolution than necessary to allow for zooming

         public const int RenderDPISetting = 300;

 

+        public static string LastLog {

+            get;

+            private set;

+        }

+

         static public Shape GetPictureShapeFromLaTeXCode(Slide currentSlide, string latexCode, float fontSize)

         {

             // check the cache first

@@ -154,7 +159,16 @@
             else

             {

                 // try to render the formula using our LaTeX service

-                Globals.ThisAddIn.LaTeXRenderingServices.Service.RenderLaTeXCode(latexCode, out imageData, ref pixelsPerEmHeight, out baselineOffset);

+                // TODO: needs further refactoring [10/1/2010 Andreas]

+                LaTeXCompilationTask task;

+                task.code = latexCode;

+                task.pixelsPerEmHeight = pixelsPerEmHeight;

+

+                LaTeXCompilationResult result = Globals.ThisAddIn.LaTeXRenderingServices.Service.RenderLaTeXCode(task);

+                LastLog = result.report;

+                imageData = result.imageData;

+                baselineOffset = (int) result.baselineOffset;

+                pixelsPerEmHeight = result.pixelsPerEmHeight;

 

                 if (imageData != null && imageData.Length > 0)

                 {

diff --git a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/MiKTeXService.cs b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/MiKTeXService.cs
index 88cd326..52fdd8d 100644
--- a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/MiKTeXService.cs
+++ b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/MiKTeXService.cs
@@ -34,8 +34,6 @@
         private const string latexOptions = "-enable-installer -interaction=nonstopmode";

         private const string dvipngOptions = "-T tight --depth --height -D DPI --noghostscript --picky -q -z 0";

 

-        private string lastLog;

-

         private MiKTexSettings settings {

             get { return MiKTexSettings.Default; }

         }

@@ -57,9 +55,10 @@
             return output;

         }

 

-        private bool compileLatexCode(string code, int DPI, out byte[] imageData, out int baselineOffset) {

+        private bool compileLatexCode(string code, int DPI, out byte[] imageData, out float baselineOffset, out string log) {

             baselineOffset = 0;

             imageData = null;

+            log = "";

 

             string tempTexFileName = Path.GetTempFileName();

             String tempDir = Path.GetDirectoryName( tempTexFileName );

@@ -77,7 +76,7 @@
 

                 string latexOutput = runConsoleProcess( tempDir, settings.LatexPath, latexOptions + " \"" + tempTexFileName + "\"" );

 

-                lastLog += "Latex Output:\n\n" + latexOutput;

+                log += "Latex Output:\n\n" + latexOutput;

 

                 // run it twice...

                 // HACK: it wont work with changing dpi for some reason :( [5/23/2010 Andreas]

@@ -88,7 +87,7 @@
                         + " -o \"" + outputImagePath + "\""

                         + " \"" + Path.ChangeExtension( tempTexFileName, "dvi" ) );

 

-                    lastLog += "\nDVIPNG Output:\n\n" + dvipngOutput;

+                    log += "\nDVIPNG Output:\n\n" + dvipngOutput;

 

                     if( File.Exists(outputImagePath) ) {

                         break;

@@ -115,7 +114,7 @@
             return (imageData != null);

         }

 

-        #region ILaTeXService Members

+        #region ILaTeXRenderingService Members

 

         public string AboutNotice

         {

@@ -127,35 +126,31 @@
             get { return "MiKTeX Service"; }

         }

 

-        public bool RenderLaTeXCode( string latexCode, out byte[] imageData, ref float pixelsPerEmHeight, out int baselineOffset )

+

+        public LaTeXCompilationResult RenderLaTeXCode(LaTeXCompilationTask task)

         {

+            LaTeXCompilationResult result;

+

             // ignore pixelsPerEmHeight and simple use our fixed DPI value for now

             float latexFontSizePt = 10;

             float latexPrintPtPerInch = 72;

-            int DPI = (int)( 0.5f + pixelsPerEmHeight / (latexFontSizePt / latexPrintPtPerInch) );

+            int DPI = (int)( 0.5f + task.pixelsPerEmHeight / (latexFontSizePt / latexPrintPtPerInch) );

             if( DPI < 150 ) {

                 DPI = 150;

             }

             // only allow steps of 10..

             DPI = DPI - DPI % 10;

 

-            pixelsPerEmHeight = latexFontSizePt / latexPrintPtPerInch * DPI;

+            result.pixelsPerEmHeight = latexFontSizePt / latexPrintPtPerInch * DPI;

 

-            string fullLatexCode = LaTeXTool.ActivePresentation.SettingsTags().MiKTeXTemplate.value.Replace("LATEXCODE", latexCode);

+            string fullLatexCode = LaTeXTool.ActivePresentation.SettingsTags().MiKTeXTemplate.value.Replace("LATEXCODE", task.code);

 

-            lastLog = "";

-

-            if( !compileLatexCode( fullLatexCode, DPI, out imageData, out baselineOffset ) ) {

-                imageData = null;

-                return false;

+            if( !compileLatexCode( fullLatexCode, DPI, out result.imageData, out result.baselineOffset, out result.report ) ) {

+                result.imageData = null;

+                return result;

             }

 

-            return true;

-        }

-

-        public string GetLastErrorReport()

-        {

-            return lastLog;

+            return result;

         }

 

         #endregion

diff --git a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/NullService.cs b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/NullService.cs
index e36efde..d32829e 100644
--- a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/NullService.cs
+++ b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/NullService.cs
@@ -34,17 +34,14 @@
             get { return "Cache Only"; }

         }

 

-        public bool RenderLaTeXCode( string latexCode, out byte[] imageData, ref float pixelsPerEmHeight, out int baselineOffset ) {

-            imageData = null;

-            pixelsPerEmHeight = 0;

-            baselineOffset = 0;

-            return false;

+        public LaTeXCompilationResult RenderLaTeXCode(LaTeXCompilationTask task) {

+            LaTeXCompilationResult result;

+            result.imageData = null;

+            result.pixelsPerEmHeight = 0;

+            result.baselineOffset = 0;

+            result.report = "No LaTeX service chosen!";

+            return result;

         }

-

-        public string GetLastErrorReport() {

-            return "No LaTeX service chosen!";

-        }

-

         #endregion

     }

 }

diff --git a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/WebService.cs b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/WebService.cs
index 93f6761..a29db90 100644
--- a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/WebService.cs
+++ b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRendering/WebService.cs
@@ -65,7 +65,7 @@
             Stream responseStream = response.GetResponseStream();

             byte[] buffer = new byte[1024*512];

             int offset = 0;

-            // apparently we need to everything packet by packet

+            // apparently we need to read everything packet by packet

             while (true) {

                 int numBytesRead = responseStream.Read(buffer, offset, 2048);

                 offset += numBytesRead;

@@ -122,34 +122,27 @@
             return URLData.content;

         }

 

-        public bool RenderLaTeXCode(string latexCode, out byte[] imageData, ref float pixelsPerEmHeight, out int baselineOffset) {

+        public LaTeXCompilationResult RenderLaTeXCode( LaTeXCompilationTask task ) {

+            LaTeXCompilationResult result;

+            result.report = "No information provided.";

+

             // measured value

-            pixelsPerEmHeight = 59;

+            result.pixelsPerEmHeight = 59;

  

             // baseline offset not supported!

-            baselineOffset = 0;

+            result.baselineOffset = 0;

 

-            URLData URLData = compileLaTeX(latexCode);

+            URLData URLData = compileLaTeX(task.code);

             // TODO: replace all the null checks with exception handling? [2/26/2009 Andreas]

             if (URLData.content == null)

             {

-                imageData = null;

-                return false;

+                result.imageData = null;

+                return result;

             }

             Trace.Assert(System.Text.RegularExpressions.Regex.IsMatch(URLData.contentType, "gif|bmp|jpeg|png"));

 

-            imageData = URLData.content;

-            return true;

-        }

-

-        public string GetLastErrorReport() {

-            return "No information provided.";

-        }

-

-        public float DPI {

-            get {

-                return 300;

-            }

+            result.imageData = URLData.content;

+            return result;

         }

 

         #endregion

diff --git a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRibbon.cs b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRibbon.cs
index 64e0679..8b12ae1 100644
--- a/PowerPointLaTeX/PowerPointLaTeX/LaTeXRibbon.cs
+++ b/PowerPointLaTeX/PowerPointLaTeX/LaTeXRibbon.cs
@@ -284,7 +284,7 @@
         }

 

         private void showLastLogButton_Click( object sender, RibbonControlEventArgs e ) {

-            LogForm logForm = new LogForm( Globals.ThisAddIn.LaTeXRenderingServices.Service.GetLastErrorReport() );

+            LogForm logForm = new LogForm( LaTeXRendering.LastLog );

             logForm.ShowDialog();

         }