Beispiel #1
0
 public static void ImportImageFile(string path, out string preamble, out string body)
 {
     preamble = null; body = null;
     try {
         var text = TeXSource.ReadEmbededSource(path);
         if (text != null)
         {
             using (var sr = new StringReader(text)) {
                 if (!TeXSource.ParseTeXSourceFile(sr, out preamble, out body))
                 {
                     MessageBox.Show(Properties.Resources.ANALYZESOURCEERROR, "TeX2img");
                 }
             }
         }
         else
         {
             throw new IOException();
         }
     }
     catch (NotImplementedException) {
         MessageBox.Show(Properties.Resources.EMBED_IS_ONLY_NTFS);
     }
     catch (Exception) {
         if (File.Exists(path))
         {
             MessageBox.Show(String.Format(Properties.Resources.NO_EMBEDDED_SOURCE, path), "TeX2img");
         }
         else
         {
             MessageBox.Show(String.Format(Properties.Resources.FAIL_OPEN, path), "TeX2img");
         }
     }
 }
Beispiel #2
0
        public void ImportFile(string path)
        {
            string preamble, body;

            ImportFile(path, out preamble, out body);
            if (preamble != null)
            {
                myPreambleForm.PreambleTextBox.Text = TeXSource.ChangeReturnCode(preamble);
            }
            if (body != null)
            {
                sourceTextBox.Text = TeXSource.ChangeReturnCode(body);
            }
        }
Beispiel #3
0
 private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (TeXsourceSaveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         var encoding = Converter.GetInputEncoding();
         if (encoding.CodePage == Encoding.UTF8.CodePage)
         {
             encoding = new System.Text.UTF8Encoding(false);
         }
         try {
             using (var file = new StreamWriter(TeXsourceSaveFileDialog.FileName, false, encoding)) {
                 TeXSource.WriteTeXSourceFile(file, myPreambleForm.PreambleTextBox.Text, sourceTextBox.Text);
             }
         }
         catch (UnauthorizedAccessException) {
             MessageBox.Show(String.Format(Properties.Resources.AUTHORIZEDERROR, TeXsourceSaveFileDialog.FileName));
         }
     }
 }
Beispiel #4
0
        public static void ImportTeXFile(string path, out string preamble, out string body)
        {
            preamble = null; body = null;
            var extension = Path.GetExtension(path).ToLower();

            byte[] buf;
            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read)) {
                buf = new byte[fs.Length];
                fs.Read(buf, 0, (int)fs.Length);
            }

            var  GuessedEncoding = KanjiEncoding.CheckBOM(buf);
            bool bom             = false;

            if (GuessedEncoding != null)
            {
                bom = true;
                buf = KanjiEncoding.DeleteBOM(buf, GuessedEncoding);
            }
            else
            {
                var guessed = KanjiEncoding.GuessKajiEncoding(buf);
                if (guessed.Length == 0)
                {
                    GuessedEncoding = null;
                }
                else
                {
                    GuessedEncoding = guessed[0];
                }
            }
            Encoding encoding;

            switch (Properties.Settings.Default.encode)
            {
            case "euc": encoding = Encoding.GetEncoding("euc-jp"); break;

            case "jis": encoding = Encoding.GetEncoding("iso-2022-jp"); break;

            case "utf8": encoding = Encoding.UTF8; break;

            case "sjis": encoding = Encoding.GetEncoding("shift_jis"); break;

            case "_utf8":
                if (GuessedEncoding != null)
                {
                    encoding = GuessedEncoding;
                }
                else
                {
                    encoding = Encoding.UTF8;
                }
                break;

            case "_sjis":
            default:
                if (GuessedEncoding != null)
                {
                    encoding = GuessedEncoding;
                }
                else
                {
                    encoding = Encoding.GetEncoding("shift_jis");
                }
                break;
            }
            if (encoding.CodePage != GuessedEncoding.CodePage)
            {
                if (bom || MessageBox.Show(String.Format(Properties.Resources.CHECK_ENCODEMSG, encoding.EncodingName, GuessedEncoding.EncodingName, GuessedEncoding.EncodingName), "TeX2img", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    encoding = GuessedEncoding;
                }
            }
            using (var sr = new StringReader(encoding.GetString(buf))) {
                if (!TeXSource.ParseTeXSourceFile(sr, out preamble, out body))
                {
                    MessageBox.Show(Properties.Resources.ANALYZESOURCEERROR, "TeX2img");
                }
            }
        }
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;
            }
        }