Наследование: IDisposable
Пример #1
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;
        }
Пример #2
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;
            }
        }
Пример #3
0
        public void sizeTest() {
            SetOutputDir("size");
            PrepareTest();
            using (converter = new Converter(controller, Path.Combine(WorkDir, testfile + ".tex"), Path.Combine(OutputDir, testfile + ".pdf"))) {
                BeforeTest();
                tex2dvi_test(testfile + ".tex");
                dvi2pdf_test(testfile + ".dvi");
                var pageref = new ByRefType(typeof(int));
                var versionref = new ByRefType(typeof(int));
                CallMethod(converter, "pdfinfo", Path.Combine(OutputDir, "dvi2pdf.pdf"), pageref, versionref);
                int page =(int)pageref.val, version =(int)versionref.val;
                AfterTest();
                var orighiresbb = GetPDFBB("dvi2pdf.pdf", 1, page);
                var origbb = GetPDFBB("dvi2pdf.pdf", 1, page, false);
                var orighiresbox = GetPDFBox("dvi2pdf.pdf", Enumerable.Range(1, page).ToList());
                var origbox = GetPDFBox("dvi2pdf.pdf", Enumerable.Range(1, page).ToList(), false);
                Settings.Default.keepPageSize = false;
                var exts = new string[] { ".pdf", ".eps", ".jpg", ".png" };
                foreach (var ext in exts) {
                    doGenerateOneTest("dvi2pdf.pdf", testfile + "-not-keep" + ext);
                }
                Func<string, List<string>> get_output_files_func = (s) => {
                    if (page == 1) return new List<string>() { s };
                    else {
                        var basen = Path.GetFileNameWithoutExtension(s);
                        var ext = Path.GetExtension(s);
                        return Enumerable.Range(1, page).Select(i => basen + "-" + i.ToString() + ext).ToList();
                    }
                };
                Func<string, List<Size>> get_pdf_box = (s) => {
                    var rv = new List<Size>();
                    var files = get_output_files_func(s);
                    foreach (var file in files) {
                        if (File.Exists(Path.Combine(OutputDir, file))) {
                            rv.AddRange(GetPDFBox(file, new List<int>() { 1 }));
                        }
                    }
                    return rv;
                };
                Assert.IsTrue(CheckVerctorImageSize(orighiresbb, get_pdf_box(testfile + "-not-keep.pdf")));
                Assert.IsTrue(CheckVerctorImageSize(orighiresbb, GetEPSBB(get_output_files_func(testfile + "-not-keep.eps"))));
                Assert.IsTrue(CheckBitmapImageSize(orighiresbb, GetBitmapSize(get_output_files_func(testfile + "-not-keep.png"))));
                Assert.IsTrue(CheckBitmapImageSize(orighiresbb, GetBitmapSize(get_output_files_func(testfile + "-not-keep.jpg"))));

                Settings.Default.keepPageSize = true;
                foreach (var ext in exts) {
                    doGenerateOneTest("dvi2pdf.pdf", testfile + "-keep" + ext);
                }
                Assert.IsTrue(CheckVerctorImageSize(orighiresbox, get_pdf_box(testfile + "-keep.pdf")));
                Assert.IsTrue(CheckVerctorImageSize(orighiresbox, GetEPSBB(get_output_files_func(testfile + "-keep.eps"))));
                Assert.IsTrue(CheckBitmapImageSize(orighiresbox, GetBitmapSize(get_output_files_func(testfile + "-keep.png"))));
                Assert.IsTrue(CheckBitmapImageSize(orighiresbox, GetBitmapSize(get_output_files_func(testfile + "-keep.jpg"))));
            }
        }
Пример #4
0
        static int TeX2imgMain(List<string> cmds) {
            Properties.Settings.Default.SaveSettings = !nogui;
            // オプション解析
            List<string> files;
            var options = GetOptiontSet();
            try { files = options.Parse(cmds); }
            catch (Mono.Options.OptionException e) {
                if (e.OptionName != null) {
                    var msg = String.Format(Properties.Resources.INVALID_INPUT_TO_OPTION, e.OptionName);
                    if (e.Message != "") msg += " : " + e.Message;
                    msg += "\n" + String.Format(Properties.Resources.SEEHELPMSG, "TeX2img" + (nogui ? "c" : "") + ".exe /help");
                    if (nogui) Console.WriteLine(msg);
                    else MessageBox.Show(msg, "TeX2img");
                }
                return -1;
            }
            // 各種バイナリのパスが設定されていなかったら推測する。
            // "/exit"が指定されている場合はメッセージ表示をしない。
            setPath(exit);
            if (help) {
                ShowHelp();
                return 0;
            }
            if (version) {
                ShowVersion();
                return 0;
            }
            // CUIモードの引数なしはエラー
            if (nogui && files.Count == 0) {
                Console.WriteLine(Properties.Resources.NOARGUMENT + "\n");
                ShowHelp();
                return -1;
            }

            if (preview != null) Properties.Settings.Default.previewFlag = (bool)preview;
            //Console.WriteLine(preview == null ? "null" : preview.ToString());

            // すぐに終了
            if (exit) {
                Properties.Settings.Default.Save();
                return 0;
            }
            // filesのチェック
            string err = "";
            for (int i = 0; i < files.Count / 2; ++i) {
                var chkconv = new Converter(null, files[2 * i], files[2 * i + 1]);
                if (!chkconv.CheckInputFormat()) {
                    err += String.Format(Properties.Resources.INVALID_EXTENSION, files[2 * i]) + "\n";
                }
                if (!File.Exists(files[2 * i])) {
                    err += String.Format(Properties.Resources.NOTEXIST, files[2 * i]);
                    if (files[2 * i].StartsWith("-") || files[2 * i].StartsWith("/")) err += Properties.Resources.MAY_BE_OPTION;
                    err += "\n";
                }
                if (!chkconv.CheckFormat()) {
                    err += String.Format(Properties.Resources.INVALID_EXTENSION, files[2 * i + 1]) + "\n";
                }
            }
            if (files.Count % 2 != 0) {
                err += String.Format(Properties.Resources.NOOUTPUT_FILE, files[files.Count - 1]);
                if (files[files.Count - 1].StartsWith("-") || files[files.Count - 1].StartsWith("/")) err += Properties.Resources.MAY_BE_OPTION;
                err += "\n";
            }
            if (err != "") {
                err = err.Remove(err.Length - 1);// 最後の改行を削除
                if (nogui) Console.WriteLine(err);
                else MessageBox.Show(err, "TeX2img");
                return -2;
            }

            if (nogui) {
                // CUIでオプション指定がないときは,設定によらずプレビューをしないようにする。
                if (preview == null) {
                    preview = Properties.Settings.Default.previewFlag;
                    Properties.Settings.Default.previewFlag = false;
                }
                int r = CUIExec(quiet, files);
                Properties.Settings.Default.previewFlag = (bool)preview;
                return r;
            } else {
                Application.Run(new MainForm(files));
                return 0;
            }
        }
Пример #5
0
 List<Size> GetEPSBB(List<string> files,bool hires = true){
     var rv = new List<Size>();
     foreach(var f in files) {
         var file = f;
         var dir = Path.GetDirectoryName(file);
         if(dir == "") dir = OutputDir;
         else file = Path.GetFileName(file);
         if(!File.Exists(Path.Combine(dir, file))) continue;
         using(var conv = new Converter(null, Path.Combine(dir, "dummy.tex"), file)) {
             var bb = (BoundingBoxPair)CallMethod(conv, "readBB", Path.GetFileName(file));
             rv.Add(BBToSize(bb, hires));
         }
     }
     return rv;
 }
Пример #6
0
 List<Size> GetPDFBB(string file, int firstpage, int lastpage, bool hires = true) {
     var dir = Path.GetDirectoryName(file);
     if(dir == "") dir = OutputDir;
     else file = Path.GetFileName(file);
     var rv = new List<Size>();
     using(var conv = new Converter(null, Path.Combine(dir, "dummy.tex"), file)) {
         var bb = (List<BoundingBoxPair>)CallMethod(conv, "readPDFBB", Path.GetFileName(file), firstpage, lastpage);
         for(int i = 0 ; i < bb.Count ; ++i )rv.Add(BBToSize(bb[i], hires));
         return rv;
     }
 }
Пример #7
0
 void doGenerateOneTest(string inputfile, string outputfile) { 
     BeforeTest();
     if(!Path.IsPathRooted(inputfile))inputfile = Path.Combine(OutputDir,inputfile);
     string tempfile = Path.Combine(WorkDir, Path.GetFileName(inputfile));
     File.Copy(inputfile, tempfile, true);
     if(!Path.IsPathRooted(outputfile))outputfile = Path.Combine(OutputDir,outputfile);
     using(converter = new Converter(controller, tempfile, outputfile)) {
         converter.Convert();
         Assert.IsTrue(File.Exists(outputfile) || File.Exists(Path.Combine(Path.GetDirectoryName(outputfile),Path.GetFileNameWithoutExtension(outputfile) + "-1" + Path.GetExtension(outputfile))));
     }
     AfterTest();
 }
Пример #8
0
        void doGenerateTest(string source, string output, string[] exts) {
            Tuple<List<Size>, List<Size>> expected = null;
            var pdf = "dvi2pdf.pdf";
            using (var fs = new StreamWriter(Path.Combine(WorkDir, testfile + ".tex"))) {
                fs.Write(source);
            }
            using (converter = new Converter(controller, Path.Combine(WorkDir, testfile + ".tex"), Path.Combine(OutputDir, testfile + "-" + output + ".pdf"))) {
                tex2dvi_test(testfile + ".tex");
                dvi2pdf_test(testfile + ".dvi");
                var pageref = new ByRefType(typeof(int));
                var versionref = new ByRefType(typeof(int));
                CallMethod(converter, "pdfinfo", Path.Combine(OutputDir, pdf), pageref, versionref);
                int page = (int)pageref.val, version = (int)versionref.val;
                if (Settings.Default.keepPageSize) {
                    expected = new Tuple<List<Size>, List<Size>>(
                        GetPDFBox(pdf, Enumerable.Range(1, page).ToList(), false),
                        GetPDFBox(pdf, Enumerable.Range(1, page).ToList(), true));
                } else {
                    expected = new Tuple<List<Size>, List<Size>>(
                        GetPDFBB(pdf, 1, page, false),
                        GetPDFBB(pdf, 1, page, true));
                }
                File.Delete(Path.Combine(WorkDir, pdf));
                File.Move(Path.Combine(OutputDir,pdf),Path.Combine(WorkDir,pdf));
            }
            foreach (var ext in exts) {
                File.Copy(Path.Combine(WorkDir, pdf), Path.Combine(WorkDir, testfile + ".pdf"), true);
                using (converter = new Converter(controller, Path.Combine(WorkDir, testfile + ".pdf"), Path.Combine(OutputDir, testfile + "-" + output + ext))) {
                    converter.Convert();
                    if (expected.Item1.Count == 1 || Settings.Default.mergeOutputFiles) {
                        Assert.IsTrue(File.Exists(Path.Combine(OutputDir, testfile + "-" + output + ext)));
                    } else {
                        bool widthpos = (Settings.Default.leftMargin + Settings.Default.rightMargin > 0);
                        bool heightpos = (Settings.Default.topMargin + Settings.Default.bottomMargin > 0);
                        for (int i = 0; i < expected.Item1.Count; ++i) {
                            if ((expected.Item1[i].width != 0 || widthpos) && (expected.Item1[i].height != 0 || heightpos)) {
                                Assert.IsTrue(File.Exists(Path.Combine(OutputDir, testfile + "-" + output + "-" + (i + 1).ToString() + ext)));
                            } else {
                                Assert.IsFalse(File.Exists(Path.Combine(OutputDir, testfile + "-" + output + "-" + (i + 1).ToString() + ext)));
                            }
                        }
                    }

                }
            }
            File.Delete(Path.Combine(WorkDir,pdf));
        }
Пример #9
0
 public void eachTest() {
     SetOutputDir("each");
     PrepareTest();
     BeforeTest();
     using (converter = new Converter(controller, Path.Combine(WorkDir, testfile + ".tex"), Path.Combine(OutputDir, testfile + ".pdf"))) {
         doEachTest();
     }
     AfterTest();
 }