/// <summary> /// Returns whether the given font is installed on this system. This can either be the /// font Filename (e.g., "Arial.ttf") or the font family / user friendly name of a font. /// (The filename lookup is faster.) /// </summary> /// <param name="font">Font family or font filename</param> /// <returns>true if font is installed</returns> public static bool IsInstalled(string font) { // first check - filename lookup in the font folder if (font.EndsWith(".ttf") || font.EndsWith(".otf")) { if (Common.UsingMonoVM) { // mono - find the font filename using linux "find" var args = new StringBuilder(); args.Append("-name \""); args.Append(font); args.Append("\""); string stdOut = string.Empty; string stdErr = string.Empty; SubProcess.Run("/", "find", args.ToString(), out stdOut, out stdErr); // if stdOut returns something, the file is installed return(stdOut.Length > 0); } // Windows / first check - this font should reside in the fonts folder String fullFilename = Common.PathCombine(GetFontFolderPath(), font); return(File.Exists(fullFilename)); } // second check - look up the filename and see if the file exists string filename = GetFontFileName(font, "normal"); if (filename != null) { if (File.Exists(filename)) { return(true); } } // third check - look through the InstalledFontCollection var ifc = new InstalledFontCollection(); foreach (FontFamily family in ifc.Families) { if (family.Name.Contains(font)) { return(true); } } return(false); }
public static string GetFontFileName(string familyName, string style) { // Linux lookup if (Common.UsingMonoVM) { // Linux fonts are not listed in the registry; instead, linux (and mono) use the fontconfig library to // provide support. // To find the font, we'll make a call to fc-list (one of the fontconfig commands) for the font, and parse the // results for the filename const string prog = "fc-list"; var args = new StringBuilder(); args.Append("-v \""); args.Append(familyName); if (style.Length == 0 || style.ToLower().Equals("normal")) { args.Append(":style=Regular\""); } else { args.Append(":style="); args.Append(style); args.Append("\""); } string stdOut = string.Empty; string stdErr = string.Empty; SubProcess.Run(Directory.GetCurrentDirectory(), prog, args.ToString(), out stdOut, out stdErr); if (stdOut.Length < 1) { return(string.Empty); } // call returned successfully -- read the results var start = stdOut.IndexOf("file: \""); if (start > 0) { start += 7; var stop = stdOut.IndexOf("\"", start); if (stop > 0) { return(stdOut.Substring(start, (stop - start))); } return(string.Empty); } return(string.Empty); } // Windows lookup RegistryKey fontsKey; Dictionary <string, string> fontFileNames = new Dictionary <string, string>(); try { fontsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\Fonts\"); string[] fontNames = fontsKey.GetValueNames(); for (int i = 0; i < fontNames.Length; i++) { if (fontNames[i].IndexOf(familyName) < 0) { continue; } string fontFileName = fontsKey.GetValue(fontNames[i]) as string; fontFileNames[fontNames[i]] = fontFileName; } } catch (Exception) { } string resultFileName = string.Empty; switch (fontFileNames.Count) { case 1: string[] values = new string[1]; fontFileNames.Values.CopyTo(values, 0); resultFileName = values[0]; break; case 0: return(null); default: string pattern = string.Format("{0} {1} (", familyName, style); resultFileName = SearchFontFileNames(pattern, fontFileNames); if (resultFileName != string.Empty) { break; } pattern = string.Format("{0} (", familyName); resultFileName = SearchFontFileNames(pattern, fontFileNames); if (resultFileName != string.Empty) { break; } resultFileName = SearchFontFileNames(familyName, fontFileNames); break; } return(Common.PathCombine(GetFontFolderPath(), resultFileName)); }
public static bool Launch(string type, PublicationInformation publicationInformation) { string xhtmlFile = publicationInformation.DefaultXhtmlFileWithPath; CreateVerbose(publicationInformation); var localType = type.Replace(@"\", "/").ToLower(); try { //Code to call PathwayExport Commandline Utility -commented for now string mainXhtmlFile = Path.GetFileNameWithoutExtension(publicationInformation.DefaultXhtmlFileWithPath); if (mainXhtmlFile.ToLower() == "flexrev") { publicationInformation.IsReversalExist = false; } StringBuilder sb = new StringBuilder(); sb.Append("\""); sb.Append(publicationInformation.DefaultXhtmlFileWithPath); sb.Append(",\" "); sb.Append("\""); sb.Append(publicationInformation.DefaultCssFileWithPath); if (publicationInformation.IsReversalExist) { sb.Append(",\" "); sb.Append("\""); sb.Append(Path.Combine(Path.GetDirectoryName(publicationInformation.DefaultXhtmlFileWithPath), "FlexRev.xhtml")); sb.Append(",\" "); sb.Append("\""); sb.Append(publicationInformation.DefaultRevCssFileWithPath); sb.Append("\""); } else { sb.Append("\""); } Common.SaveInputType(publicationInformation.ProjectInputType); string dbName = Common.IsUnixOS() ? Param.DatabaseName.Replace(" ", "\\ ").Replace("'", "\\'") : Param.DatabaseName; string argument = string.Format("--target \"{0}\" --directory \"{1}\" --files {2} --nunit {3} --database \"{4}\"", type.Replace(@"\", "/").ToLower(), publicationInformation.DictionaryPath, sb.ToString(), Common.Testing.ToString(), dbName); string pathwayExportFile = Path.Combine(Common.GetApplicationPath(), "PathwayExport.exe"); if (!File.Exists(pathwayExportFile)) { pathwayExportFile = Path.Combine(Common.GetApplicationPath(), "Export", "PathwayExport.exe"); } var cmd = Common.IsUnixOS()? "/usr/bin/PathwayExport": pathwayExportFile; if (HasPathwayExportBatchRegistryValueForCurrentUserRegistry()) { var batchFullName = Path.Combine(publicationInformation.DictionaryPath, "PathwayExport.bat"); using (var sw = new StreamWriter(batchFullName)) { sw.WriteLine("\"" + cmd + "\" " + argument); sw.Close(); } MessageBox.Show(@"Batch command written to " + batchFullName + @" since PathwayExportBatch variable present"); if (Common.IsUnixOS()) { SubProcess.Run("", "nautilus", Common.HandleSpaceinLinuxPath(publicationInformation.DictionaryPath), false); } else { SubProcess.Run(publicationInformation.DictionaryPath, "explorer.exe", publicationInformation.DictionaryPath, false); } } else { SubProcess.WindowStyle = ProcessWindowStyle.Minimized; SubProcess.Run(publicationInformation.DictionaryPath, cmd, argument, true); } } catch (Exception ex) { throw new Exception(ex.Message); } finally { publicationInformation.DefaultXhtmlFileWithPath = xhtmlFile; ShowVerbose(publicationInformation); } return(true); }