Beispiel #1
0
 // フルパスを入れる
 public Converter(IOutputController controller, string inputTeXFilePath, string outputFilePath) {
     InputFile = inputTeXFilePath;
     OutputFile = outputFilePath;
     controller_ = controller;
     workingDir = Path.GetDirectoryName(inputTeXFilePath);
     tempFilesDeleter = new TempFilesDeleter(workingDir);
 }
Beispiel #2
0
        static void PDFEmbed(string file, string text)
        {
            var tmpdir = Path.GetTempPath();
            var tmp    = TempFilesDeleter.GetTempFileName(".pdf", tmpdir);

            tmp = Path.Combine(tmpdir, tmp);
            using (var tmp_deleter = new TempFilesDeleter(tmpdir)) {
                tmp_deleter.AddFile(tmp);
                using (var mupdf = new MuPDF(mudraw)) {
                    var doc = mupdf.Execute <int>("open_document", file);
                    if (doc == 0)
                    {
                        return;
                    }
                    var page = mupdf.Execute <int>("load_page", doc, 0);
                    if (page == 0)
                    {
                        return;
                    }
                    var annot = mupdf.Execute <int>("create_annot", page, "Text");
                    if (annot == 0)
                    {
                        return;
                    }
                    mupdf.Execute("set_annot_contents", annot, ChangeReturnCode(PDFsrcHead + System.Environment.NewLine + text, "\n"));
                    mupdf.Execute("set_annot_flag", annot, 35);
                    mupdf.Execute("write_document", doc, tmp);
                }
                if (File.Exists(tmp))
                {
                    File.Delete(file);
                    File.Move(tmp, file);
                }
            }
        }
Beispiel #3
0
        static string PDFRead(string file)
        {
            var srcHead = PDFsrcHead + System.Environment.NewLine;
            var tmpdir  = Path.GetTempPath();

            using (var tempFileDeleter = new TempFilesDeleter(tmpdir)) {
                var targettmp = TempFilesDeleter.GetTempFileName(".txt");
                var targetpre = Path.GetFileNameWithoutExtension(targettmp);
                using (var proc = new System.Diagnostics.Process()) {
                    proc.StartInfo.FileName         = Path.Combine(Converter.GetToolsPath(), "pdfiumdraw.exe");
                    proc.StartInfo.Arguments        = "--output-text-annots --output=\"" + Path.Combine(tmpdir, targetpre) + "-%d.txt\" \"" + Path.GetFileName(file) + "\"";
                    proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(file);
                    proc.StartInfo.CreateNoWindow   = true;
                    proc.StartInfo.UseShellExecute  = false;
                    try { proc.Start(); }
                    catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.ToString()); return(null); }
                    proc.WaitForExit(1000);
                }
                int    i  = 1;
                string rv = null;
                while (File.Exists(Path.Combine(tmpdir, targetpre + "-" + i.ToString() + ".txt")))
                {
                    string annot_txt_file = Path.Combine(tmpdir, targetpre + "-" + i.ToString() + ".txt");
                    tempFileDeleter.AddFile(annot_txt_file);
                    if (rv == null)
                    {
                        using (var fr = new StreamReader(Path.Combine(tmpdir, targetpre + "-" + i.ToString() + ".txt"), Encoding.UTF8)) {
                            var text = ChangeReturnCode(fr.ReadToEnd());
                            if (text.StartsWith(srcHead))
                            {
                                return(text.Substring(srcHead.Length));
                            }
                        }
                    }
                    ++i;
                }
                return(rv);
            }
        }
Beispiel #4
0
        // CUIモード
        static int CUIExec(bool q, List <string> files)
        {
            IOutputController Output = new CUIOutput(q);

            if (files.Count == 0)
            {
                Console.WriteLine(Properties.Resources.NOINPUTFILE);
                return(-5);
            }
            try {
                Directory.CreateDirectory(Path.GetTempPath());
            }
            catch (Exception) {
                Console.WriteLine(String.Format(Properties.Resources.FAIL_TMPFOLDER, Path.GetTempPath()));
                return(-7);
            }

            int failnum = 0;

            var outFiles = new System.Collections.Specialized.StringCollection();

            for (int i = 0; i < files.Count / 2; ++i)
            {
                string file = Path.GetFullPath(files[2 * i]);
                string dir;
                if (Properties.Settings.Default.workingDirectory == "file")
                {
                    dir = Path.GetDirectoryName(file);
                }
                else if (Properties.Settings.Default.workingDirectory == "current")
                {
                    dir = Directory.GetCurrentDirectory();
                }
                else
                {
                    dir = Path.GetTempPath();
                }
                string tmpTeXFileName = TempFilesDeleter.GetTempFileName(Path.GetExtension(file), dir);
                if (tmpTeXFileName == null)
                {
                    Console.WriteLine(String.Format(Properties.Resources.FAIL_TMPFILE, Path.GetTempPath()));
                    return(-6);
                }
                tmpTeXFileName = Path.Combine(dir, tmpTeXFileName);
                // 一時フォルダにコピー
                File.Copy(file, tmpTeXFileName, true);
                (new FileInfo(tmpTeXFileName)).Attributes = FileAttributes.Normal;
                var output = Path.GetFullPath(files[2 * i + 1]);
                // 変換!
                try {
                    using (var converter = new Converter(Output, tmpTeXFileName, output)) {
                        converter.AddInputPath(Path.GetDirectoryName(file));
                        if (!converter.Convert())
                        {
                            ++failnum;
                        }
                        else
                        {
                            outFiles.AddRange(converter.OutputFileNames.ToArray());
                        }
                    }
                    if (Properties.Settings.Default.setFileToClipBoard)
                    {
                        Clipboard.SetFileDropList(outFiles);
                    }
                }
                catch (Exception e) { Console.WriteLine(e.Message); }
            }
            return(failnum);
        }
Beispiel #5
0
        Converter converter = null;// 実行中でなければnull
        private void convertWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Properties.Settings.SetCurrentLanguage();
            try {
                Directory.CreateDirectory(Path.GetTempPath());
            }
            catch (Exception) {
                MessageBox.Show(String.Format(Properties.Resources.FAIL_TMPFOLDER, Path.GetTempPath()));
                return;
            }

            if (FirstFiles != null)
            {
                for (int i = 0; i < FirstFiles.Count / 2; ++i)
                {
                    string file = FirstFiles[2 * i];
                    string workdir;
                    if (Properties.Settings.Default.workingDirectory == "file")
                    {
                        workdir = Path.GetDirectoryName(file);
                    }
                    else if (Properties.Settings.Default.workingDirectory == "current")
                    {
                        workdir = Directory.GetCurrentDirectory();
                    }
                    else
                    {
                        workdir = Path.GetTempPath();
                    }
                    string tmppath;
                    try {
                        string inputextension = Path.GetExtension(file);
                        tmppath = Path.Combine(workdir, TempFilesDeleter.GetTempFileName(inputextension));
                        File.Delete(tmppath);
                        File.Copy(file, tmppath, true);
                    }
                    catch (Exception) {
                        MessageBox.Show(String.Format(Properties.Resources.FAIL_TMPFILE, workdir));
                        continue;
                    }
                    var output = Path.GetFullPath(FirstFiles[2 * i + 1]);
                    converter = new Converter(this, tmppath, output);
                    converter.Convert();
                }
                FirstFiles = null;
                converter  = null;
                return;
            }

            try {
                string outputFilePath = outputFileNameTextBox.Text;

                if (InputFromFileRadioButton.Checked)
                {
                    string inputTeXFilePath = inputFileNameTextBox.Text;
                    if (!File.Exists(inputTeXFilePath))
                    {
                        MessageBox.Show(String.Format(Properties.Resources.NOTEXIST, inputTeXFilePath), Properties.Resources.ERROR, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }

                string extension = Path.GetExtension(outputFilePath).ToLower();
                string tmpTeXFileName;
                string tmpDir;
                if (InputFromFileRadioButton.Checked)
                {
                    if (Properties.Settings.Default.workingDirectory == "file")
                    {
                        tmpDir = Path.GetDirectoryName(inputFileNameTextBox.Text);
                    }
                    else if (Properties.Settings.Default.workingDirectory == "current")
                    {
                        tmpDir = Directory.GetCurrentDirectory();
                    }
                    else
                    {
                        tmpDir = Path.GetTempPath();
                    }
                    tmpTeXFileName = TempFilesDeleter.GetTempFileName(Path.GetExtension(inputFileNameTextBox.Text), tmpDir);
                }
                else
                {
                    if (Properties.Settings.Default.workingDirectory == "current")
                    {
                        tmpDir = Directory.GetCurrentDirectory();
                    }
                    else
                    {
                        tmpDir = Path.GetTempPath();
                    }
                    tmpTeXFileName = TempFilesDeleter.GetTempFileName();
                }

                if (tmpTeXFileName == null)
                {
                    MessageBox.Show(String.Format(Properties.Resources.FAIL_TMPFILE, tmpDir));
                    return;
                }
                string tmpFileBaseName = Path.GetFileNameWithoutExtension(tmpTeXFileName);

                using (converter = new Converter(this, Path.Combine(tmpDir, tmpTeXFileName), outputFileNameTextBox.Text)) {
                    if (!converter.CheckFormat())
                    {
                        return;
                    }
                    if (!converter.CheckInputFormat())
                    {
                        return;
                    }

                    #region TeX ソースファイルの準備
                    // 外部ファイルから入力する場合はテンポラリディレクトリにコピー
                    if (InputFromFileRadioButton.Checked)
                    {
                        string inputTeXFilePath = inputFileNameTextBox.Text;
                        string tmpfile          = Path.Combine(tmpDir, tmpTeXFileName);
                        File.Copy(inputTeXFilePath, tmpfile, true);
                        // 読み取り専用の場合解除しておく(後でFile.Deleteに失敗するため)。
                        (new FileInfo(tmpfile)).Attributes = FileAttributes.Normal;
                        converter.AddInputPath(Path.GetDirectoryName(inputTeXFilePath));
                    }

                    // 直接入力の場合 tex ソースを出力
                    if (InputFromTextboxRadioButton.Checked)
                    {
                        using (StreamWriter sw = new StreamWriter(Path.Combine(tmpDir, tmpTeXFileName), false, Converter.GetInputEncoding())) {
                            try {
                                TeXSource.WriteTeXSourceFile(sw, myPreambleForm.PreambleTextBox.Text, sourceTextBox.Text);
                            }
                            catch { }
                        }
                    }
                    #endregion

                    if (converter.Convert())
                    {
                        if (Properties.Settings.Default.setFileToClipBoard)
                        {
                            if (converter.OutputFileNames.Count > 0)
                            {
                                Invoke(new Action(() => {
                                    var flist = new System.Collections.Specialized.StringCollection();
                                    foreach (var f in converter.OutputFileNames)
                                    {
                                        flist.Add(f);
                                    }
                                    Clipboard.SetFileDropList(flist);
                                }));
                            }
                        }
                    }
                }
            }
            finally {
                converter = null;
            }
        }
Beispiel #6
0
        static void PDFEmbed(string file, string text)
        {
            var tmpdir = Path.GetTempPath();

            using (var tempFileDeleter = new TempFilesDeleter(tmpdir)) {
                var targettmp = TempFilesDeleter.GetTempFileName(".pdf", tmpdir);
                try { File.Copy(file, Path.Combine(tmpdir, targettmp)); }
                catch (Exception) { return; }
                tempFileDeleter.AddFile(targettmp);
                var txttmp = TempFilesDeleter.GetTempFileName(".txt", tmpdir);
                tempFileDeleter.AddFile(txttmp);
                using (var fw = new BinaryWriter(new FileStream(Path.Combine(tmpdir, txttmp), FileMode.Create))) {
                    fw.Write((new UnicodeEncoding(true, false)).GetBytes(ChangeReturnCode(PDFsrcHead + System.Environment.NewLine + text + "\n", "\n")));
                }
                var tmp = TempFilesDeleter.GetTempFileName(".tex", tmpdir);
                tmp = Path.Combine(tmpdir, tmp);
                tempFileDeleter.AddTeXFile(tmp);
                using (var fw = new BinaryWriter(new FileStream(tmp, FileMode.Create))) {
                    fw.Write(ASCIIEncoding.ASCII.GetBytes(
                                 @"\pdfoutput=1\relax
\newread\teximgread
\newcount\teximgcnt
\teximgcnt=0\relax
\newif\ifteximgflag\teximgflagtrue
\def\x{{%
  \loop
    \expandafter\catcode\the\teximgcnt=12\relax
    \advance\teximgcnt by 1\relax
  \ifnum\teximgcnt<128\repeat
  \gdef\teximgannot{"));
                    fw.Write((new UnicodeEncoding(true, true)).GetPreamble());
                    fw.Write(ASCIIEncoding.ASCII.GetBytes(
                                 @"}%
  \immediate\openin\teximgread="));
                    fw.Write(ASCIIEncoding.ASCII.GetBytes(txttmp));
                    fw.Write(ASCIIEncoding.ASCII.GetBytes(
                                 @"\relax
  \ifeof\teximgread\teximgflagfalse\else
    \loop
      \read\teximgread to \teximgline
      \xdef\teximgannot{\teximgannot\teximgline}%
    \unless\ifeof\teximgread\repeat
    \immediate\closein\teximgread
  \fi
}}\x
\ifteximgflag
  \def\teximguniqtokena{\teximguniqtokenx}\def\teximguniqtokenb{\teximguniqtokenbx}\def\teximguniqtokenx{}%
  {\catcode0=12\catcode13=12\relax\def\removelast#1^^M\teximguniqtokena#2\teximguniqtokenb{#1}%
  \xdef\teximgannot{\expandafter\removelast\teximgannot\teximguniqtokena^^M\teximguniqtokena\teximguniqtokenb}%
  \def\removelast#1^^@^^M\teximguniqtokena#2\teximguniqtokenb{#1}%
  \xdef\teximgannot{\expandafter\removelast\teximgannot\teximguniqtokena^^@^^M\teximguniqtokena\teximguniqtokenb}}%
  \newcount\teximgtotalpage
  \pdfximage{" + targettmp + @"}\teximgtotalpage =\pdflastximagepages
  \advance\teximgtotalpage by 1\relax
  \teximgcnt=1\relax
  \loop
    \pdfximage page \the\teximgcnt {" + targettmp + @"}%
    \setbox0 =\hbox{\ifnum\teximgcnt=1\relax
    \pdfannot width 0pt height 0pt depth 0pt{/Subtype /Text /F 35 /Contents (\pdfescapestring{\teximgannot})}\fi
    \pdfrefximage\pdflastximage}%
    \pdfhorigin=0pt\relax
    \pdfvorigin=0pt\relax
    \pdfpagewidth=\wd0\relax
    \pdfpageheight=\ht0\relax
    \shipout\box0\relax
    \advance\teximgcnt by 1\relax
  \ifnum\teximgcnt<\teximgtotalpage\repeat
\fi
\bye"));
                }
                using (var proc = new System.Diagnostics.Process()) {
                    string arg;
                    proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(tmp);
                    proc.StartInfo.FileName         = Converter.setProcStartInfo(Properties.Settings.Default.pdftexPath, out arg);
                    proc.StartInfo.Arguments        = arg + " -no-shell-escape -interaction=nonstopmode \"" + Path.GetFileName(tmp) + "\"";
                    proc.StartInfo.UseShellExecute  = false;
                    proc.StartInfo.CreateNoWindow   = true;
                    try { proc.Start(); }
                    catch (Exception) { return; }
                    proc.WaitForExit(5000);
                }
                var tmppdf = Path.ChangeExtension(tmp, ".pdf");
                if (File.Exists(tmppdf))
                {
                    File.Delete(file);
                    File.Move(tmppdf, file);
                }
            }
        }
Beispiel #7
0
 static void PDFEmbed(string file, string text) {
     var tmpdir = Path.GetTempPath();
     var tmp = TempFilesDeleter.GetTempFileName(".pdf", tmpdir);
     tmp = Path.Combine(tmpdir, tmp);
     using (var tmp_deleter = new TempFilesDeleter(tmpdir)) {
         tmp_deleter.AddFile(tmp);
         using (var mupdf = new MuPDF(mudraw)) {
             var doc = mupdf.Execute<int>("open_document", file);
             if (doc == 0) return;
             var page = mupdf.Execute<int>("load_page", doc, 0);
             if (page == 0) return;
             var annot = mupdf.Execute<int>("create_annot", page, "Text");
             if (annot == 0) return;
             mupdf.Execute("set_annot_contents", annot, ChangeReturnCode(PDFsrcHead + System.Environment.NewLine + text, "\n"));
             mupdf.Execute("set_annot_flag", annot, 35);
             mupdf.Execute("write_document", doc, tmp);
         }
         if (File.Exists(tmp)) {
             File.Delete(file);
             File.Move(tmp, file);
         }
     }
 }