public string ResolveFont(string fontFamilySource, object fontStretch, object fontStyle, object fontWeight, IDocumentContext documentContext) { string fontFamilyPath = FontEmbedder.GetFontFamilyPath(fontFamilySource); FontFamily fontFamily = FontEmbedder.MakeDesignTimeFontReference(new FontFamily(fontFamilySource), documentContext); Dictionary <string, List <KeyValuePair <Typeface, string> > > dictionary; if (string.IsNullOrEmpty(fontFamilyPath)) { FontResolver.EnsureSystemFontCached(fontFamilySource); dictionary = this.useGdiFontNames ? FontResolver.gdiFontFamilyLookup : FontResolver.wpfFontFamilyLookup; } else { Uri uri = documentContext.MakeDesignTimeUri(new Uri(fontFamilyPath, UriKind.RelativeOrAbsolute)); if (!uri.IsAbsoluteUri) { return(fontFamilySource); } string localPath = uri.LocalPath; if (Microsoft.Expression.Framework.Documents.PathHelper.FileExists(localPath) && !localPath.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) { return(FontEmbedder.MakeSilverlightFontReference(this.GetCachedFont(localPath) + FontEmbedder.GetFontFamilySpecifier(fontFamilySource))); } if (!this.projectFontFamilyLookup.TryGetValue(Path.GetDirectoryName(uri.LocalPath), out dictionary)) { int num = fontFamilySource.IndexOf(";component", StringComparison.OrdinalIgnoreCase); if (num != -1 && fontFamilySource.StartsWith("/", StringComparison.OrdinalIgnoreCase)) { IFontResolver resolverForComponent = this.GetFontResolverForComponent(fontFamilySource.Substring(1, num - 1)); if (resolverForComponent != null && resolverForComponent != this) { return(resolverForComponent.ResolveFont(fontFamilySource, fontStretch, fontStyle, fontWeight, documentContext)); } } return(fontFamilySource); } } string fontNameFromSource = FontEmbedder.GetFontNameFromSource(fontFamily); List <KeyValuePair <Typeface, string> > list; if (dictionary.TryGetValue(fontNameFromSource, out list)) { string path = (string)null; FontResolver.FontMatch fontMatch1 = (FontResolver.FontMatch)null; foreach (KeyValuePair <Typeface, string> keyValuePair in list) { FontResolver.FontMatch fontMatch2 = new FontResolver.FontMatch(keyValuePair.Key, fontStretch, fontStyle, fontWeight); if (fontMatch1 == null || fontMatch2.CompareTo((object)fontMatch1) > 0) { path = keyValuePair.Value; fontMatch1 = fontMatch2; } } fontFamilySource = FontEmbedder.MakeSilverlightFontReference(Path.GetFullPath(path)); } return(fontFamilySource); }
static FontManager() { try { _fontResolver = new FontResolver(); return; } catch (Exception) { // ignore } _fontResolver = new DefaultFontResolver(); }
private string FindFontFromProject(string fontNameToken, string modifierToken) { ObservableCollection <IProjectFont> projectFonts = this.SceneViewModel.ProjectContext.ProjectFonts; IFontResolver fontResolver = this.SceneViewModel.ProjectContext.FontResolver; IEnumerable <IProjectFont> source = Enumerable.Where <IProjectFont>((IEnumerable <IProjectFont>)projectFonts, (Func <IProjectFont, bool>)(fontFamily => { if (!ImportManager.TokenizeFontName(fontResolver.ConvertToGdiFontName(fontFamily.FontFamilyName)).StartsWith(fontNameToken, StringComparison.OrdinalIgnoreCase)) { return(ImportManager.TokenizeFontName(fontResolver.ConvertToWpfFontName(fontFamily.FontFamilyName)).StartsWith(fontNameToken, StringComparison.OrdinalIgnoreCase)); } return(true); })); string fontMatching = modifierToken == null ? fontNameToken : fontNameToken + modifierToken; IProjectFont projectFont1 = (IProjectFont)null; foreach (IProjectFont projectFont2 in source) { string strA1 = ImportManager.TokenizeFontName(fontResolver.ConvertToGdiFontName(projectFont2.FontFamilyName)); string strA2 = ImportManager.TokenizeFontName(fontResolver.ConvertToWpfFontName(projectFont2.FontFamilyName)); if (string.Compare(strA1, fontMatching, StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(strA2, fontMatching, StringComparison.OrdinalIgnoreCase) == 0) { return(FontEmbedder.MakeProjectFontReference(projectFont2, this.SceneViewModel.Document.DocumentContext).Source); } if (projectFont1 == null || projectFont1.FontFamilyName.Length > projectFont2.FontFamilyName.Length) { projectFont1 = projectFont2; } } if (projectFont1 == null) { Enumerable.Where <IProjectFont>((IEnumerable <IProjectFont>)projectFonts, (Func <IProjectFont, bool>)(fontFamily => { if (!ImportManager.TokenizeFontName(fontResolver.ConvertToGdiFontName(fontFamily.FontFamilyName)).StartsWith(fontMatching, StringComparison.OrdinalIgnoreCase)) { return(ImportManager.TokenizeFontName(fontResolver.ConvertToWpfFontName(fontFamily.FontFamilyName)).StartsWith(fontMatching, StringComparison.OrdinalIgnoreCase)); } return(true); })); if (Enumerable.Count <IProjectFont>(source) > 0) { projectFont1 = Enumerable.First <IProjectFont>(source); } } if (projectFont1 != null) { return(FontEmbedder.MakeProjectFontReference(projectFont1, this.SceneViewModel.Document.DocumentContext).Source); } return((string)null); }
private string FindFontFromSystem(string fontNameToken, string modifierToken) { IEnumerable <SystemFontFamily> systemFonts = FontEmbedder.GetSystemFonts((ITypeResolver)this.SceneViewModel.ProjectContext); IFontResolver fontResolver = this.SceneViewModel.ProjectContext.FontResolver; IEnumerable <SystemFontFamily> enumerable = Enumerable.Where <SystemFontFamily>(systemFonts, (Func <SystemFontFamily, bool>)(fontFamily => { if (!ImportManager.TokenizeFontName(fontResolver.ConvertToGdiFontName(fontFamily.FontFamilyName)).StartsWith(fontNameToken, StringComparison.OrdinalIgnoreCase)) { return(ImportManager.TokenizeFontName(fontResolver.ConvertToWpfFontName(fontFamily.FontFamilyName)).StartsWith(fontNameToken, StringComparison.OrdinalIgnoreCase)); } return(true); })); string fontMatching = modifierToken == null ? fontNameToken : fontNameToken + modifierToken; string str = (string)null; foreach (SystemFontFamily systemFontFamily in enumerable) { string strA1 = ImportManager.TokenizeFontName(fontResolver.ConvertToGdiFontName(systemFontFamily.FontFamilyName)); string strA2 = ImportManager.TokenizeFontName(fontResolver.ConvertToWpfFontName(systemFontFamily.FontFamilyName)); if (string.Compare(strA1, fontMatching, StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(strA2, fontMatching, StringComparison.OrdinalIgnoreCase) == 0) { return(systemFontFamily.FontFamilyName); } if (str == null || str.Length > systemFontFamily.FontFamilyName.Length) { str = systemFontFamily.FontFamilyName; } } if (str == null) { IEnumerable <SystemFontFamily> source = Enumerable.Where <SystemFontFamily>(systemFonts, (Func <SystemFontFamily, bool>)(fontFamily => { if (!ImportManager.TokenizeFontName(fontResolver.ConvertToGdiFontName(fontFamily.FontFamilyName)).StartsWith(fontMatching, StringComparison.OrdinalIgnoreCase)) { return(ImportManager.TokenizeFontName(fontResolver.ConvertToWpfFontName(fontFamily.FontFamilyName)).StartsWith(fontMatching, StringComparison.OrdinalIgnoreCase)); } return(true); })); if (Enumerable.Count <SystemFontFamily>(source) > 0) { str = Enumerable.First <SystemFontFamily>(source).FontFamilyName; } } return(str); }
//// Suffix for internal face names to indicate that the font data comes from the platform //// and not from the users font resolver. //public const string PlatformTag = "platform:"; /// <summary> /// Converts specified information about a required typeface into a specific font. /// </summary> /// <param name="familyName">Name of the font family.</param> /// <param name="fontResolvingOptions">The font resolving options.</param> /// <param name="typefaceKey">Typeface key if already known by caller, null otherwise.</param> /// <returns> /// Information about the typeface, or null if no typeface can be found. /// </returns> public static FontResolverInfo ResolveTypeface(string familyName, FontResolvingOptions fontResolvingOptions, string typefaceKey) { if (string.IsNullOrEmpty(typefaceKey)) { typefaceKey = XGlyphTypeface.ComputeKey(familyName, fontResolvingOptions); } try { Lock.EnterFontFactory(); // Was this typeface requested before? FontResolverInfo fontResolverInfo; if (FontResolverInfosByName.TryGetValue(typefaceKey, out fontResolverInfo)) { return(fontResolverInfo); } // Case: This typeface was not resolved before. // Is there a custom font resolver available? IFontResolver customFontResolver = GlobalFontSettings.FontResolver; if (customFontResolver != null) { // Case: Use custom font resolver. fontResolverInfo = customFontResolver.ResolveTypeface(familyName, fontResolvingOptions.IsBold, fontResolvingOptions.IsItalic); // If resolved by custom font resolver register info and font source. if (fontResolverInfo != null && !(fontResolverInfo is PlatformFontResolverInfo)) { string resolverInfoKey = fontResolverInfo.Key; FontResolverInfo existingFontResolverInfo; if (FontResolverInfosByName.TryGetValue(resolverInfoKey, out existingFontResolverInfo)) { // Case: A new typeface was resolved with the same info as a previous one. // Discard new object an reuse previous one. fontResolverInfo = existingFontResolverInfo; // Associate with typeface key. FontResolverInfosByName.Add(typefaceKey, fontResolverInfo); #if DEBUG // The font source should exist. Debug.Assert(FontSourcesByName.ContainsKey(fontResolverInfo.FaceName)); #endif } else { // Case: No such font resolver info exists. // Add to both dictionaries. FontResolverInfosByName.Add(typefaceKey, fontResolverInfo); Debug.Assert(resolverInfoKey == fontResolverInfo.Key); FontResolverInfosByName.Add(resolverInfoKey, fontResolverInfo); // Create font source if not yet exists. XFontSource previousFontSource; if (FontSourcesByName.TryGetValue(fontResolverInfo.FaceName, out previousFontSource)) { // Case: The font source exists, because a previous font resolver info comes // with the same face name, but was different in style simulation flags. // Nothing to do. } else { // Case: Get font from custom font resolver and create font source. byte[] bytes = customFontResolver.GetFont(fontResolverInfo.FaceName); XFontSource fontSource = XFontSource.GetOrCreateFrom(bytes); // Add font source's font resolver name if it is different to the face name. if (string.Compare(fontResolverInfo.FaceName, fontSource.FontName, StringComparison.OrdinalIgnoreCase) != 0) { FontSourcesByName.Add(fontResolverInfo.FaceName, fontSource); } } } } } else { // Case: There was no custom font resolver set. // Use platform font resolver. // If it was successful resolver info and font source are cached // automatically by PlatformFontResolver.ResolveTypeface. fontResolverInfo = PlatformFontResolver.ResolveTypeface(familyName, fontResolvingOptions, typefaceKey); } // Return value is null if the typeface could not be resolved. // In this case PDFsharp stops. return(fontResolverInfo); } finally { Lock.ExitFontFactory(); } }
static FontManager() { _fontResolver = new FontResolver(); }
public FlowDocumentRichFormattingRenderer(IFontResolver fontResolver, IWebBrowser webBrowser) { this.webBrowser = webBrowser; this.fontResolver = fontResolver; }
public static FlowDocument Render(IFontResolver fontResolver, RichFormatting document) { var flow = new FlowDocument(); flow.TextAlignment = TextAlignment.Left; foreach (var paragraph in document.Paragraphs) { switch (paragraph) { case TextParagraph text: { var p = new System.Windows.Documents.Paragraph(); foreach (var c in text.Content) { Inline i; var r = new Run(c.Content); switch (c.FontSize) { case FontSize.ExtraSmall: break; case FontSize.Small: break; case FontSize.Normal: // do nothing break; case FontSize.Large: r.FontSize = 24; break; case FontSize.ExtraLarge: break; case FontSize.Humonguous: r.FontSize = 120; break; default: throw new InvalidOperationException("not a valid enum value"); } if (fontResolver.Resolve(c.FontName) is System.Windows.Media.FontFamily f) { r.FontFamily = f; } if (c.Emphasis) { i = new Bold(r); } else { i = r; } p.Inlines.Add(i); } flow.Blocks.Add(p); } break; } } return(flow); }