FontsRepository.RegisterFont data Parameter

2 posts, 0 answers
  1. Mi
    Mi avatar
    16 posts
    Member since:
    Apr 2011

    Posted 02 Mar 2015 Link to this post

    The help is little bit scarce on registering a font: see http://docs.telerik.com/devtools/wpf/controls/radpdfprocessing/concepts/fonts

    There is a data parameter at the end, which should contain the raw data for the font. Looking for more information about the subject I came across https://github.com/telerik/xaml-sdk/blob/f5002a15eb1def98405472bee700f20ad8b65cc9/SpreadProcessing/RegisterAndExportPdfFonts.Web/PdfFontsService.svc.cs where 

    public class FontData {
      public bool IsValid { get; set; }
      public byte[] Bytes { get; set; }
      public string FontFamilyName { get; set; }
      public bool IsItalic { get; set; }
      public bool IsBold { get; set; }
    }

    is defined. Which is used from https://github.com/telerik/xaml-sdk/blob/f5002a15eb1def98405472bee700f20ad8b65cc9/SpreadProcessing/RegisterAndExportPdfFonts/MainPage.xaml.cs to get the Bytes property of the above class to register a font:

    PdfFontsServiceFontData result = e.Result;
    FontFamily fontFamily = new FontFamily(result.FontFamilyName); FontStyle fontStyle = GetFontStyle(result.IsItalic); FontWeight fontWeight = GetFontWeight(result.IsBold);
    FontsRepository.RegisterFont(fontFamily, fontStyle, fontWeight, result.Bytes);

    So is this the right way to create the font data:

    FontFamily fontFamily = new FontFamily(fontFamilyName);
    FontStyle fontStyle = isItalic ? FontStyles.Italic : FontStyles.Normal;
    FontWeight fontWeight = isBold ? FontWeights.Bold : FontWeights.Normal;
    Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, FontStretches.Normal);
    GlyphTypeface glyphTypeface;
    if (typeface.TryGetGlyphTypeface(out glyphTypeface)) {
       using (var memoryStream = new MemoryStream()) {
           glyphTypeface.GetFontStream().CopyTo(memoryStream);
           data.Bytes = memoryStream.ToArray();
           data.IsValid = true;
       }
    }

    Thanks

    Mike

  2. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 04 Mar 2015 Link to this post

    Hi,

    Thank you for your feedback. We will consider some improvements to the mentioned article. Here is some additional information on the matter.

    1. In WPF you don't need to register a font, unless the font is not installed on the machine. The FontsRepository can access all fonts that are installed and register them for you.

    2. If you want to use a font that is not installed on the machine, then you should register it. Every font can be described by 3 components: font family - the family of the font; font style - the font style (normal, italic, etc.); the font weight - the weight of the font (normal, bold, narrow, etc.). These components are the key that later is used to create an instance of this font. For example if you have arialitalic.ttf font file, that contains the italic version of the Arial font, and want to register it in your application, your code should look like this:
    FontsRepository.RegisterFont(new FontFamily("Arial"), FontStyles.Italic, FontWeights.Normal, File.ReadAllBytes("arialitalic.ttf"));

    And you can use it later in your application by calling the following method:
    FontsRepository.TryCreateFont(new FontFamily("Arial"), FontStyles.Italic, FontWeights.Normal, out font)

    Hope this makes things clearer. If you have other questions do not hesitate to contact us.

    Regards,
    Kammen
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top