context: // --------------------------------------------------------------------------------------------
  // <copyright file="ExportXeLaTexConvert.cs" from='2009' to='2014' company='SIL International'>
  //      Copyright ( c ) 2014, SIL International. All Rights Reserved.   
  //    
  //      Distributable under the terms of either the Common Public License or the
  //      GNU Lesser General Public License, as specified in the LICENSING.txt file.
  // </copyright> 
  // <author>Greg Trihus</author>
  // <email>greg_trihus@sil.org</email>
  // Last reviewed: 
  // 
  // <remarks>
  //
  // </remarks>
  // --------------------------------------------------------------------------------------------
  
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Drawing;
  using System.IO;
  using System.Text;
  using System.Windows.Forms;
  using System.Xml;
  using System.Xml.Xsl;
  using L10NSharp;
  using SIL.Tool;
  
  namespace SIL.PublishingSolution
  {
      public class ExportXeLaTex : IExportProcess
      {
  
          private Dictionary<string, string> _langFontDictionary; // languages and font names in use for this export
          protected string _inputType = "dictionary";
          private bool _tableOfContent;
          private bool _coverImage;
          private bool _titleInCoverPage;
          private bool _copyrightInformation;
          private bool _includeBookTitleintheImage;
          private string _copyrightInformationPagePath;
          private string _coverPageImagePath;
          private bool _xelatexDocumentOpenClosedRequired = false;
          private bool _copyrightTexCreated = false;
          private string _copyrightTexFileName = string.Empty;
          private string _reversalIndexTexFileName = string.Empty;
          private bool _reversalIndexTexCreated = false;
          private bool _isInputTypeFound = false;
          private bool _isFileFontCodeandFontNameFound = false;
          private Dictionary<string, string> _tocPropertyList = new Dictionary<string, string>();
          private Dictionary<string, string> _langFontCodeandName;
          private Dictionary<string, string> _fontLangMap = new Dictionary<string, string>();
          private readonly XslCompiledTransform _xhtmlXelatexXslProcess = new XslCompiledTransform();
          private List<string> _xeLaTexPropertyFullFontStyleList = new List<string>();
          private bool _isUnixOs = false;
          private string paraTextEnvVariable = string.Empty;
          #region Public Functions
          public string ExportType
          {
              get
              {
                  return "XeLaTex";
              }
          }
  
          public bool Handle(string inputDataType)
          {
              bool returnValue = false;
              string dataType = inputDataType.ToLower();
              if (dataType == "dictionary" || dataType == "scripture")
              {
                  returnValue = true;
              }
              if (string.IsNullOrEmpty(XeLaTexInstallation.GetXeLaTexDir()))
              {
                  returnValue = false;
              }
              return returnValue;
          }
  
          /// <summary>
          /// Convert XHTML to ODT
          /// </summary>
          public bool Export(PublicationInformation projInfo)
          {
              string xeLatexFullFile;
              StreamWriter xeLatexFile;
              XeLaTexStyles xeLaTexStyles;
              _xhtmlXelatexXslProcess.Load(XmlReader.Create(Common.UsersXsl("AddBidi.xsl")));
              _isUnixOs = Common.IsUnixOS();
              _langFontDictionary = new Dictionary<string, string>();
              _langFontCodeandName = new Dictionary<string, string>();
              string mainXhtmlFileWithPath = projInfo.DefaultXhtmlFileWithPath;
              projInfo.OutputExtension = "pdf";
  
              var preProcessor = new PreExportProcess(projInfo);
              ExportPreprocessForXelatex(projInfo, preProcessor);
              var organization = SettingFrontmatter();
              BuildLanguagesList(projInfo.DefaultXhtmlFileWithPath);
  
              string fileName = Path.GetFileNameWithoutExtension(projInfo.DefaultXhtmlFileWithPath);
  
              AssignExportFile(projInfo, preProcessor);
              ModifyXeLaTexStyles modifyXeLaTexStyles = new ModifyXeLaTexStyles();
              modifyXeLaTexStyles.LangFontDictionary = _langFontCodeandName;
  
              Dictionary<string, Dictionary<string, string>> newProperty;
              var cssClass = WrittingTexFile(projInfo, fileName, out xeLatexFullFile, out xeLatexFile, out xeLaTexStyles, out newProperty);
              string include = xeLaTexStyles.PageStyle.ToString();
  
              InitilizeXelatexStyle(modifyXeLaTexStyles);
  
              if (ExportCopyright(projInfo, mainXhtmlFileWithPath))
              {
                  _copyrightTexCreated = true;
                  modifyXeLaTexStyles.CopyrightTexCreated = true;
                  modifyXeLaTexStyles.CopyrightTexFilename = Path.GetFileName(_copyrightTexFileName);
              }
  
              ExportReversalProcess(projInfo, modifyXeLaTexStyles);
              ProcessWrittingStyles(projInfo, modifyXeLaTexStyles, newProperty, xeLatexFile, cssClass, xeLatexFullFile, include);
              Dictionary<string, string> imgPath = new Dictionary<string, string>();
              if (newProperty.ContainsKey("ImagePath"))
              {
                  imgPath = newProperty["ImagePath"];
              }
              UpdateXeLaTexFontCacheIfNecessary();
              CallXeLaTex(projInfo, xeLatexFullFile, true, imgPath);
              ProcessRampFile(projInfo, xeLatexFullFile, organization);
              return true;
          }
  
          private Dictionary<string, Dictionary<string, string>> WrittingTexFile(PublicationInformation projInfo, string fileName, out string xeLatexFullFile,
                                             out StreamWriter xeLatexFile, out XeLaTexStyles xeLaTexStyles,
                                             out Dictionary<string, Dictionary<string, string>> newProperty)
          {
              CheckFontFamilyAvailable(projInfo.DefaultCssFileWithPath);
  
              Dictionary<string, Dictionary<string, string>> cssClass = new Dictionary<string, Dictionary<string, string>>();
              CssTree cssTree = new CssTree();
              cssTree.OutputType = Common.OutputType.XELATEX;
              cssClass = cssTree.CreateCssProperty(projInfo.DefaultCssFileWithPath, true);
              int pageWidth = Common.GetPictureWidth(cssClass, projInfo.ProjectInputType);
  
              xeLatexFullFile = Common.PathCombine(projInfo.ProjectPath, fileName + ".tex");
              xeLatexFile = new StreamWriter(xeLatexFullFile);
  
              Dictionary<string, List<string>> classInlineStyle = new Dictionary<string, List<string>>();
              xeLaTexStyles = new XeLaTexStyles();
              xeLaTexStyles.LangFontDictionary = _langFontCodeandName;
              classInlineStyle = xeLaTexStyles.CreateXeTexStyles(projInfo, xeLatexFile, cssClass);
  
              XeLaTexContent xeLaTexContent = new XeLaTexContent();
              xeLaTexContent.IsUnix = _isUnixOs;
              Dictionary<string, List<string>> classInlineText = xeLaTexStyles._classInlineText;
              newProperty = xeLaTexContent.CreateContent(projInfo, cssClass, xeLatexFile, classInlineStyle,
                                                         cssTree.SpecificityClass, cssTree.CssClassOrder, classInlineText,
                                                         pageWidth);
  
              if (projInfo.IsReversalExist)
              {
                  var revFile = Common.PathCombine(Path.GetDirectoryName(projInfo.DefaultXhtmlFileWithPath), "FlexRev.xhtml");
                  string fileNameXhtml = Path.GetFileNameWithoutExtension(revFile);
                  string reversalFileName = fileNameXhtml + ".tex";
  
                  CloseDocument(xeLatexFile, true, reversalFileName);
              }
              else
              {
                  CloseDocument(xeLatexFile, false, string.Empty);
              }
              return cssClass;
          }
  
          private void ProcessWrittingStyles(PublicationInformation projInfo, ModifyXeLaTexStyles modifyXeLaTexStyles, Dictionary<string, Dictionary<string, string>> newProperty,
                                StreamWriter xeLatexFile, Dictionary<string, Dictionary<string, string>> cssClass, string xeLatexFullFile, string include)
          {
              modifyXeLaTexStyles.XelatexDocumentOpenClosedRequired = false;
              _xelatexDocumentOpenClosedRequired = false;
              modifyXeLaTexStyles.ProjectType = projInfo.ProjectInputType;
  
              if (newProperty.ContainsKey("TableofContent") && newProperty["TableofContent"].Count > 0)
              {
                  foreach (var tocSection in _tocPropertyList)
                  {
                      if (tocSection.Key.Contains("PageStock"))
                      {
                          newProperty["TableofContent"].Add(tocSection.Key, tocSection.Value);
                      }
                  }
              }
  
              modifyXeLaTexStyles.XeLaTexPropertyFontStyleList = _xeLaTexPropertyFullFontStyleList;
              modifyXeLaTexStyles.ModifyStylesXML(projInfo.ProjectPath, xeLatexFile, newProperty, cssClass, xeLatexFullFile,
                                                  include, _langFontCodeandName, true);
          }
  
          private void InitilizeXelatexStyle(ModifyXeLaTexStyles modifyXeLaTexStyles)
          {
              modifyXeLaTexStyles.ProjectType = _inputType;
              modifyXeLaTexStyles.TocChecked = _tableOfContent.ToString();
  
              modifyXeLaTexStyles.CoverImage = _coverImage.ToString();
              modifyXeLaTexStyles.TitleInCoverPage = _titleInCoverPage.ToString();
              modifyXeLaTexStyles.CopyrightInformation = _copyrightInformation.ToString();
              modifyXeLaTexStyles.IncludeBookTitleintheImage = _includeBookTitleintheImage.ToString();
              modifyXeLaTexStyles.CopyrightInformationPagePath = _copyrightInformationPagePath;
              modifyXeLaTexStyles.CoverPageImagePath = _coverPageImagePath;
          }
  
          private void ExportReversalProcess(PublicationInformation projInfo, ModifyXeLaTexStyles modifyXeLaTexStyles)
          {
              if (projInfo.IsReversalExist)
              {
                  if (_inputType.ToLower() == "dictionary")
                  {
                      Common.ApplyXslt(projInfo.DefaultXhtmlFileWithPath, _xhtmlXelatexXslProcess);
                  }
                  if (ExportReversalIndex(projInfo))
                  {
                      _reversalIndexTexCreated = true;
                      var revFile = Common.PathCombine(Path.GetDirectoryName(projInfo.DefaultXhtmlFileWithPath),
                                                       "FlexRev.xhtml");
                      string fileNameXhtml = Path.GetFileNameWithoutExtension(revFile);
                      string xeLatexReversalFile = Common.PathCombine(projInfo.ProjectPath, fileNameXhtml + ".tex");
  
                      modifyXeLaTexStyles.ReversalIndexExist = true;
                      modifyXeLaTexStyles.ReversalIndexTexFilename = Path.GetFileName(xeLatexReversalFile);
                  }
              }
          }
  
          private static void AssignExportFile(PublicationInformation projInfo, PreExportProcess preProcessor)
          {
              if (projInfo.DefaultXhtmlFileWithPath.Contains("FlexRev.xhtml"))
              {
                  projInfo.IsReversalExist = false;
              }
  
              projInfo.DefaultCssFileWithPath = preProcessor.ProcessedCss;
              projInfo.ProjectPath = Path.GetDirectoryName(preProcessor.ProcessedXhtml);
              projInfo.DefaultXhtmlFileWithPath = preProcessor.PreserveSpace();
              preProcessor.InsertPropertyForXelatexCss(projInfo.DefaultCssFileWithPath);
              projInfo.DefaultCssFileWithPath = preProcessor.RemoveTextIndent(projInfo.DefaultCssFileWithPath);
          }
  
          private string SettingFrontmatter()
          {
              Param.LoadSettings();
              string organization;
              try
              {
                  // get the organization
                  organization = Param.Value["Organization"];
              }
              catch (Exception)
              {
                  // shouldn't happen (ExportThroughPathway dialog forces the user to select an organization), 
                  // but just in case, specify a default org.
                  organization = "SIL International";
              }
              _coverImage = (Param.GetMetadataValue(Param.CoverPage, organization) == null)
                                ? false
                                : Boolean.Parse(Param.GetMetadataValue(Param.CoverPage, organization));
              _coverPageImagePath = Param.GetMetadataValue(Param.CoverPageFilename, organization);
  
  
              _titleInCoverPage = (Param.GetMetadataValue(Param.TitlePage, organization) == null)
                                      ? false
                                      : Boolean.Parse(Param.GetMetadataValue(Param.TitlePage, organization));
  
  
              _copyrightInformation = (Param.GetMetadataValue(Param.CopyrightPage, organization) == null)
                                          ? false
                                          : Boolean.Parse(Param.GetMetadataValue(Param.CopyrightPage, organization));
              _copyrightInformationPagePath = Param.GetMetadataValue(Param.CopyrightPageFilename, organization);
  
              _includeBookTitleintheImage = (Param.GetMetadataValue(Param.CoverPageTitle, organization) == null)
                                                ? false
                                                : Boolean.Parse(Param.GetMetadataValue(Param.CoverPageTitle, organization));
  
              _tableOfContent = (Param.GetMetadataValue(Param.TableOfContents, organization) == null)
                                    ? false
                                    : Boolean.Parse(Param.GetMetadataValue(Param.TableOfContents, organization));
              return organization;
          }
  
          private void ExportPreprocessForXelatex(PublicationInformation projInfo, PreExportProcess preProcessor)
          {
              if (_isUnixOs)
              {
                  Common.RemoveDTDForLinuxProcess(projInfo.DefaultXhtmlFileWithPath, "xelatex");
              }
              preProcessor.SetLangforLetter(projInfo.DefaultXhtmlFileWithPath);
              preProcessor.XelatexImagePreprocess();
  
              if (_inputType.ToLower() == "dictionary" && projInfo.ProjectInputType.ToLower() == "dictionary")
              {
                  Common.ApplyXslt(projInfo.DefaultXhtmlFileWithPath, _xhtmlXelatexXslProcess);
              }
          }
  
          private void ProcessRampFile(PublicationInformation projInfo, string xeLatexFullFile, string organization)
          {
              string pdfFullName = string.Empty;
              string texNameOnly = Path.GetFileNameWithoutExtension(xeLatexFullFile);
              string userFolder = Path.GetDirectoryName(xeLatexFullFile);
  
              string exportTitle = string.Empty;
              exportTitle = Param.GetMetadataValue(Param.Title, organization);
  
              if (exportTitle == string.Empty)
              {
                  exportTitle = texNameOnly;
              }
  
              if (userFolder != null)
                  pdfFullName = Common.PathCombine(userFolder, exportTitle + ".pdf");
  
              if (File.Exists(pdfFullName))
              {
                  Common.CleanupExportFolder(xeLatexFullFile, ".tmp,.de,.jpg,.tif,.log,.exe,.xml,.jar",
                                             "layout.css,mergedmain1,preserve", string.Empty);
                  CreateRAMP(projInfo);
              }
          }
  
          private void CreateRAMP(PublicationInformation projInfo)
          {
              Ramp ramp = new Ramp();
              ramp.Create(projInfo.DefaultXhtmlFileWithPath, ".pdf", projInfo.ProjectInputType);
          }
  
          public bool ExportCopyright(PublicationInformation projInfo, string mainXhtmlFileWithPath)
          {
              if (_copyrightInformation)
              {
                  var preProcess = new PreExportProcess(projInfo);
                  var processFolder = Path.GetDirectoryName(projInfo.DefaultXhtmlFileWithPath);
                  preProcess.CopyCopyrightPage(processFolder);
                  string copyRightFilePath = Common.PathCombine(processFolder, "File2Cpy.xhtml");
  
                  if (copyRightFilePath.Trim().Length <= 0 && !File.Exists(copyRightFilePath))
                  {
                      return false;
                  }
                  if (File.Exists(copyRightFilePath))
                  {
                      if (Common.UnixVersionCheck())
                      {
                          string draftTempFileName = Path.GetFileName(copyRightFilePath);
                          draftTempFileName = Common.PathCombine(Path.GetTempPath(), draftTempFileName);
                          if (!File.Exists(draftTempFileName))
                          {
                              File.Copy(copyRightFilePath, draftTempFileName, true);
                              Common.RemoveDTDForLinuxProcess(draftTempFileName, "xelatex");
                          }
                          projInfo.DefaultXhtmlFileWithPath = draftTempFileName;
                          copyRightFilePath = draftTempFileName;
                      }
                      else
                      {
                          projInfo.DefaultXhtmlFileWithPath = copyRightFilePath;
                      }
                  }
                  else
                  {
                      return false;
                  }
  
                  string filepath = Path.GetFullPath(copyRightFilePath);
  
                  Dictionary<string, Dictionary<string, string>> cssClass =
                      new Dictionary<string, Dictionary<string, string>>();
                  CssTree cssTree = new CssTree();
                  cssTree.OutputType = Common.OutputType.XELATEX;
                  cssClass = cssTree.CreateCssProperty(Common.PathCombine(filepath, "copy.css"), true);
                  string fileNameXhtml = Path.GetFileNameWithoutExtension(copyRightFilePath);
                  string xeLatexCopyrightFile = Common.PathCombine(projInfo.ProjectPath, fileNameXhtml + ".tex");
                  _copyrightTexFileName = xeLatexCopyrightFile;
                  int pageWidth = Common.GetPictureWidth(cssClass, projInfo.ProjectInputType);
  
                  StreamWriter xeLatexFile = new StreamWriter(xeLatexCopyrightFile);
                  Dictionary<string, List<string>> classInlineStyle = new Dictionary<string, List<string>>();
                  XeLaTexStyles xeLaTexStyles = new XeLaTexStyles();
                  xeLaTexStyles.LangFontDictionary = _langFontCodeandName;
                  classInlineStyle = xeLaTexStyles.CreateXeTexStyles(projInfo, xeLatexFile, cssClass);
  
                  XeLaTexContent xeLaTexContent = new XeLaTexContent();
                  xeLaTexContent.IsUnix = _isUnixOs;
                  Dictionary<string, List<string>> classInlineText = xeLaTexStyles._classInlineText;
                  xeLaTexContent.CreateContent(projInfo, cssClass, xeLatexFile, classInlineStyle, cssTree.SpecificityClass, cssTree.CssClassOrder, classInlineText, pageWidth);
  
                  _xelatexDocumentOpenClosedRequired = true; //Don't change the place.
                  CloseDocument(xeLatexFile, false, string.Empty);
                  ModifyXeLaTexStyles modifyXeLaTexStyles = new ModifyXeLaTexStyles();
                  modifyXeLaTexStyles.XelatexDocumentOpenClosedRequired = true;
                  modifyXeLaTexStyles.ProjectType = projInfo.ProjectInputType;
  
                  string copyright = GetLanguageInfo(mainXhtmlFileWithPath, projInfo);
                  InsertInFile(xeLatexCopyrightFile, "copyright information", copyright);
  
                  return true;
              }
              return false;
          }
          static public void InsertInFile(string filePath, string searchText, string insertText)
          {
              if (!File.Exists(filePath)) return;
              string tempFile = Common.PathCombine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + "1.tex");
              File.Copy(filePath, tempFile, true);
              var reader = new StreamReader(tempFile);
              string contentWriter;
              var writer = new StreamWriter(filePath);
              while ((contentWriter = reader.ReadLine()) != null)
              {
                  if (contentWriter.ToLower().IndexOf(searchText) >= 0)
                  {
                      writer.WriteLine(insertText);
                      writer.WriteLine(contentWriter);
                      string st = string.Empty;
                      string contributors = Param.GetMetadataValue(Param.Contributor);
                      if (contributors.Trim().Length > 0)
                      {
                          st = "(" + contributors + "), ";
                      }
                      string rights = Common.UpdateCopyrightYear(Param.GetMetadataValue(Param.CopyrightHolder));
                      if (rights.Trim().Length > 0)
                      {
                          st = st + rights;
                      }
  
                      writer.WriteLine(@"\OtherCopyrights{" + st + @"}\end{adjustwidth}");
                      writer.WriteLine("\\mbox{}");
  
                  }
                  else
                  {
                      writer.WriteLine(contentWriter.Replace("&", @"\&"));
                  }
              }
              reader.Close();
              writer.Close();
              File.Delete(tempFile);
          }
  
          private string GetLanguageInfo(string mainXhtmlFileWithPath, PublicationInformation projInfo)
          {
              var sb = new StringBuilder();
              // append what we know about this language, including a hyperlink to the ethnologue.
              string languageCode = Common.GetLanguageCode(mainXhtmlFileWithPath, projInfo.ProjectInputType);
              if (languageCode.Length > 0)
              {
                  var languageName = Common.GetLanguageName(languageCode);
  
                  sb.AppendLine(@"\empFrontMatterdiv{ABOUT THIS DOCUMENT}\end{adjustwidth}");
  
                  string txt = "This document contains data written in ";
                  if (languageName.Length > 0)
                  {
                      txt = txt + languageName;
                  }
                  txt = txt + "[" + languageCode + "].";
                  sb.AppendLine(@"\empFrontMatterdiv{" + txt + @"}\end{adjustwidth}");
  
                  txt = " For more information about this language, visit http://www.ethnologue.com/show_language.asp?code=";
                  var codeLen = languageCode.Length > 3 ? 3 : languageCode.Length;
                  txt = txt + languageCode.Substring(0, codeLen);
                  sb.Append(@"\empFrontMatterdiv{" + txt + @" \newline \newline}\end{adjustwidth}");
              }
              return sb.ToString();
          }
  
          public bool ExportReversalIndex(PublicationInformation projInfo)
          {
  
              if (projInfo.IsReversalExist)
              {
                  var revFile = Common.PathCombine(projInfo.ProjectPath, "FlexRev.xhtml");
                  if (!File.Exists(revFile))
                  {
                      return false;
                  }
  
                  if (File.Exists(revFile))
                  {
                      if (Common.UnixVersionCheck())
                      {
                          Common.RemoveDTDForLinuxProcess(revFile, "xelatex");
                      }
                  }
  
                  projInfo.DefaultXhtmlFileWithPath = revFile;
                  PreExportProcess preProcessor = new PreExportProcess(projInfo);
                  preProcessor.SetLangforLetter(projInfo.DefaultXhtmlFileWithPath);
  
                  CheckFontFamilyAvailable(projInfo.DefaultRevCssFileWithPath);
  
                  Dictionary<string, Dictionary<string, string>> cssClass =
                      new Dictionary<string, Dictionary<string, string>>();
                  CssTree cssTree = new CssTree();
                  cssTree.OutputType = Common.OutputType.XELATEX;
                  cssClass = cssTree.CreateCssProperty(projInfo.DefaultRevCssFileWithPath, true);
                  string fileNameXhtml = Path.GetFileNameWithoutExtension(revFile);
                  string xeLatexRevesalIndexFile = Common.PathCombine(projInfo.ProjectPath, fileNameXhtml + ".tex");
                  _reversalIndexTexFileName = xeLatexRevesalIndexFile;
                  StreamWriter xeLatexFile = new StreamWriter(xeLatexRevesalIndexFile);
                  Dictionary<string, List<string>> classInlineStyle = new Dictionary<string, List<string>>();
                  XeLaTexStyles xeLaTexStyles = new XeLaTexStyles();
                  xeLaTexStyles.LangFontDictionary = _langFontCodeandName;
                  classInlineStyle = xeLaTexStyles.CreateXeTexStyles(projInfo, xeLatexFile, cssClass);
                  int pageWidth = Common.GetPictureWidth(cssClass, projInfo.ProjectInputType);
  
                  XeLaTexContent xeLaTexContent = new XeLaTexContent();
                  xeLaTexContent.IsUnix = _isUnixOs;
                  Dictionary<string, List<string>> classInlineText = xeLaTexStyles._classInlineText;
                  Dictionary<string, Dictionary<string, string>> newProperty = xeLaTexContent.CreateContent(projInfo,
                                                                                                            cssClass,
                                                                                                            xeLatexFile,
                                                                                                            classInlineStyle,
                                                                                                            cssTree
                                                                                                                .SpecificityClass,
                                                                                                            cssTree
                                                                                                                .CssClassOrder,
                                                                                                            classInlineText,
                                                                                                            pageWidth);
  
                  _xelatexDocumentOpenClosedRequired = true; //Don't change the place.
                  CloseDocument(xeLatexFile, false, string.Empty);
                  string include = xeLaTexStyles.PageStyle.ToString();
                  ModifyXeLaTexStyles modifyXeLaTexStyles = new ModifyXeLaTexStyles();
                  modifyXeLaTexStyles.XelatexDocumentOpenClosedRequired = true;
                  modifyXeLaTexStyles.ProjectType = projInfo.ProjectInputType;
  	            bool createPageNumber = false;
  	            if (projInfo.DefaultXhtmlFileWithPath.IndexOf("PreserveFlexRev.xhtml") > 0)
  		            createPageNumber = true;
  
                  modifyXeLaTexStyles.ModifyStylesXML(projInfo.ProjectPath, xeLatexFile, newProperty, cssClass,
  													xeLatexRevesalIndexFile, include, _langFontCodeandName, createPageNumber);
                  _xeLaTexPropertyFullFontStyleList = modifyXeLaTexStyles.XeLaTexPropertyFontStyleList;
  
                  if (newProperty.ContainsKey("TableofContent") && newProperty["TableofContent"].Count > 0)
                  {
                      foreach (var tocSection in newProperty["TableofContent"])
                      {
                          if (tocSection.Key.Contains("PageStock"))
                          {
                              _tocPropertyList.Add(tocSection.Key, tocSection.Value);
                          }
                      }
                  }
                  return true;
              }
              return false;
          }
  
          private static void CheckFontFamilyAvailable(string cssFileName)
          {
              var installedFontList = XelatexFontMapping.InstalledFontList();
              if (!installedFontList.ContainsKey("Scheherazade Graphite Alpha"))
              {
                  if (installedFontList.ContainsKey("Scheherazade"))
                  {
                      Common.StreamReplaceInFile(cssFileName, "Scheherazade Graphite Alpha", "Scheherazade");
                  }
              }
          }
  
          protected void UpdateXeLaTexFontCacheIfNecessary()
          {
              Debug.Assert(XeLaTexInstallation.GetXeLaTexDir() != "");
              var systemFontList = FontFamily.Families;
              if (systemFontList.Length != XeLaTexInstallation.GetXeLaTexFontCount())
              {
  				var xelatexPath = XeLaTexInstallation.GetXeLaTexDir();
  	            if (!Directory.Exists(xelatexPath))
  		            return;
  
                  using (var p2 = new Process())
                  {
                      if (!Common.IsUnixOS())
                      {
                          xelatexPath = Common.PathCombine(xelatexPath, "bin");
                          xelatexPath = Common.PathCombine(xelatexPath, "win32");
                      }
                      p2.StartInfo.WorkingDirectory = xelatexPath;
                      p2.StartInfo.FileName = "fc-cache";
                      p2.StartInfo.Arguments = "-v -r";
                      p2.Start();
                      p2.WaitForExit();
                  }
                  XeLaTexInstallation.SetXeLaTexFontCount(systemFontList.Length);
              }
          }
  
          public void CallXeLaTex(PublicationInformation projInfo, string xeLatexFullFile, bool openFile, Dictionary<string, string> ImageFilePath)
          {
  			if(Common.Testing)
  			{
  				return;
  			}
  
              string originalDirectory = Directory.GetCurrentDirectory();
              string[] pdfFiles = Directory.GetFiles(Path.GetDirectoryName(xeLatexFullFile), "*.pdf");
              foreach (string pdfFile in pdfFiles)
              {
                  try
                  {
                      File.Delete(pdfFile);
                  }
                  catch { }
              }
              string exportDirectory = Path.GetDirectoryName(xeLatexFullFile);
              string xeLaTexInstallationPath = XeLaTexInstallation.GetXeLaTexDir();
              if (!Directory.Exists(xeLaTexInstallationPath))
              {
                  var msg = LocalizationManager.GetString("ExportXelatex.CallXelatex.Message", "Please install the Xelatex application.", "");
                  MessageBox.Show(msg);
                  return;
              }
              string name = "xelatex.exe";
              string arguments = "-interaction=batchmode \"" + Path.GetFileName(xeLatexFullFile) + "\"";
              if (_isUnixOs)
              {
                  string path = Environment.GetEnvironmentVariable("PATH");
                  Debug.Assert(path != null);
                  if (!path.Contains(xeLaTexInstallationPath))
                  {
                      Environment.SetEnvironmentVariable("PATH", string.Format("{0}:{1}", xeLaTexInstallationPath, path));
                  }
                  name = Common.PathCombine(xeLaTexInstallationPath, "xelatex");
                  arguments = " -interaction=batchmode \"" + xeLatexFullFile + "\"";
  
                  if (_inputType.ToLower() == "scripture" || projInfo.ProjectInputType == "scripture")
                  {
                      paraTextEnvVariable = Environment.GetEnvironmentVariable("TEXINPUTS");
                      Environment.SetEnvironmentVariable("TEXINPUTS", "");
                  }
                  if (exportDirectory != null)
                      Directory.SetCurrentDirectory(exportDirectory);
              }
              else
              {
                  xeLaTexInstallationPath = Common.PathCombine(xeLaTexInstallationPath, "bin");
                  xeLaTexInstallationPath = Common.PathCombine(xeLaTexInstallationPath, "win32");
  
                  string dest = Common.PathCombine(xeLaTexInstallationPath, Path.GetFileName(xeLatexFullFile));
                  if (xeLatexFullFile != dest)
                      File.Copy(xeLatexFullFile, dest, true);
  
                  if (_copyrightTexCreated)
                  {
                      string copyrightDest = Common.PathCombine(xeLaTexInstallationPath, Path.GetFileName(_copyrightTexFileName));
                      if (_copyrightTexFileName != copyrightDest)
                          File.Copy(_copyrightTexFileName, copyrightDest, true);
                  }
  
                  if (_reversalIndexTexCreated)
                  {
                      string copyrightDest = Common.PathCombine(xeLaTexInstallationPath,
                                                                Path.GetFileName(_reversalIndexTexFileName));
                      if (_reversalIndexTexFileName != copyrightDest)
                          File.Copy(_reversalIndexTexFileName, copyrightDest, true);
                  }
                  Directory.SetCurrentDirectory(xeLaTexInstallationPath);
              }
  
              ExecuteXelatexProcess(xeLatexFullFile, name, arguments);
              OpenXelatexOutput(xeLatexFullFile, openFile, originalDirectory, xeLaTexInstallationPath);
          }
  
          private void ExecuteXelatexProcess(string xeLatexFullFile, string name, string arguments)
          {
              string p1Error = string.Empty;
              using (Process p1 = new Process())
              {
                  p1.StartInfo.FileName = name;
                  if (xeLatexFullFile != null)
                      p1.StartInfo.Arguments = arguments;
                  p1.StartInfo.RedirectStandardOutput = true;
                  p1.StartInfo.RedirectStandardError = p1.StartInfo.RedirectStandardOutput;
                  p1.StartInfo.UseShellExecute = !p1.StartInfo.RedirectStandardOutput;
                  p1.Start();
                  p1.WaitForExit();
                  p1Error = p1.StandardError.ReadToEnd();
              }
  
              if (Convert.ToBoolean(_tableOfContent))
              {
                  using (Process p1 = new Process())
                  {
                      p1.StartInfo.FileName = name;
                      if (xeLatexFullFile != null)
                          p1.StartInfo.Arguments = arguments;
                      p1.StartInfo.RedirectStandardOutput = true;
                      p1.StartInfo.RedirectStandardError = p1.StartInfo.RedirectStandardOutput;
                      p1.StartInfo.UseShellExecute = !p1.StartInfo.RedirectStandardOutput;
                      p1.Start();
                      p1.WaitForExit();
                      p1Error = p1.StandardError.ReadToEnd();
                  }
  
                  using (Process p1 = new Process())
                  {
                      p1.StartInfo.FileName = name;
                      if (xeLatexFullFile != null)
                          p1.StartInfo.Arguments = arguments;
                      p1.StartInfo.RedirectStandardOutput = true;
                      p1.StartInfo.RedirectStandardError = p1.StartInfo.RedirectStandardOutput;
                      p1.StartInfo.UseShellExecute = !p1.StartInfo.RedirectStandardOutput;
                      p1.Start();
                      p1.WaitForExit();
  
gt:                     p1Error = p1.StandardError.ReadToEnd();