PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Feb 24, 2020 2:59 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Dec 17, 2007 12:31 pm 
Offline

Joined: Fri Nov 23, 2007 12:10 pm
Posts: 18
Hello, this code do soport for fonts.
For example your can create local folder "Fonts" and copy and past fonts type Barcode TrueType ;)

....

and update line 73 in file MigraDoc.Rendering.FontHandler
Code:
        XPdfFontOptions options = null;
        options = new XPdfFontOptions(encoding, fontEmbedding);     
       
        XFontStyle style = GetXStyle(font);
        System.Drawing.Font gdiFont = FontConverter.ConvertToFont(font.Name, (float)font.Size, (System.Drawing.FontStyle)style, System.Drawing.GraphicsUnit.World);
        XFont xFont = new XFont(gdiFont, options);


Other problem is simulate bolt and italic. This is to do.
See: PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer line 520 and 525
Code:
      if (bold && !descriptor.IsBoldFace)
      {
        // TODO: emulate bold by thicker outline
      }

      if (italic && !descriptor.IsBoldFace)
      {
        // TODO: emulate italic by shearing transformation
      }


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Dec 17, 2007 12:37 pm 
Offline

Joined: Fri Nov 23, 2007 12:10 pm
Posts: 18
The code of the FontConverter, i can not send.
This form detect how spam....


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Dec 17, 2007 12:38 pm 
Offline

Joined: Fri Nov 23, 2007 12:10 pm
Posts: 18
Code:
using System;
using System.Drawing;
using System.Drawing.Text;
using System.IO;

namespace MigraDoc.Rendering
{
    /// <summary>
    /// FontConverter
    /// </summary>
    public class FontConverter
    {
        /// <summary>
        /// Convert string name to Font GDI+ type, include private fonts from "Fonts" folder
        /// </summary>
        /// <param name="fontName"></param>
        /// <param name="size"></param>
        /// <param name="style"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public static Font ConvertToFont(string fontName, float size, FontStyle style, GraphicsUnit unit)
        {
            fontName = fontName.ToLowerInvariant();

            FontFamily family = LocalFontCollection.Instance.Find(fontName, style);

            if (family == null)
                family = LocalFontCollection.Find(fontName, FontFamily.Families);

            if (family == null)
                return new Font(fontName, size, style, unit);

            return new Font(family, size, style, unit);
        }

        private class LocalFontCollection
        {
            private static LocalFontCollection _instance = new LocalFontCollection();
            public static LocalFontCollection Instance
            {
                get { return _instance; }
            }

            private System.Drawing.Text.PrivateFontCollection _privateFontCollectionRegular;
            private System.Drawing.Text.PrivateFontCollection _privateFontCollectionBold;
            private System.Drawing.Text.PrivateFontCollection _privateFontCollectionItalic;
            private System.Drawing.Text.PrivateFontCollection _privateFontCollectionBoldItalic;

            public LocalFontCollection()
            {
                _privateFontCollectionRegular = new System.Drawing.Text.PrivateFontCollection();
                _privateFontCollectionBold = new System.Drawing.Text.PrivateFontCollection();
                _privateFontCollectionItalic = new System.Drawing.Text.PrivateFontCollection();
                _privateFontCollectionBoldItalic = new System.Drawing.Text.PrivateFontCollection();

                string path = "fonts/";
                if (System.Web.HttpContext.Current != null)
                    path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + path;
#if DEBUG
                else
                    path = "../../" + path;
#endif

                string[] files = System.IO.Directory.GetFiles(path);
                foreach (string file in files)
                {
                    if (System.IO.Path.GetFileName(file).ToLower().Contains("bolditalic"))
                        _privateFontCollectionBoldItalic.AddFontFile(file);
                    else if (System.IO.Path.GetFileName(file).ToLower().Contains("bold"))
                        _privateFontCollectionBold.AddFontFile(file);
                    else if (System.IO.Path.GetFileName(file).ToLower().Contains("italic"))
                        _privateFontCollectionItalic.AddFontFile(file);
                    else
                        _privateFontCollectionRegular.AddFontFile(file);
                }
            }

            public FontFamily Find(string fontName, FontStyle style)
            {
                FontFamily family = null;

                if ((style & FontStyle.Bold) == FontStyle.Bold)
                {
                    if ((style & FontStyle.Italic) == FontStyle.Italic)
                    {
                        // BoldItalic
                        family = Find(fontName, _privateFontCollectionBoldItalic.Families);
                    }
                    else
                    {
                        // Bold
                        family = Find(fontName, _privateFontCollectionBold.Families);
                    }
                }
                else if ((style & FontStyle.Italic) == FontStyle.Italic)
                {
                    // Italic
                    family = Find(fontName, _privateFontCollectionItalic.Families);
                }
               
                if (family == null)
                {
                    // Regular
                    family = Find(fontName, _privateFontCollectionRegular.Families);
                }

                return family;
            }

            public static FontFamily Find(string fontName, System.Drawing.FontFamily[] families)
            {
                foreach (System.Drawing.FontFamily family in families)
                    if (family.Name.ToLowerInvariant().Equals(fontName))
                        return family;

                return null;
            }
        }
    }
}


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Dec 17, 2007 3:00 pm 
Offline

Joined: Fri Nov 23, 2007 12:10 pm
Posts: 18
New update in FontConverter
Code:
                string[] files = System.IO.Directory.GetFiles(path);
                foreach (string file in files)
                {
                    System.Drawing.Text.PrivateFontCollection temp = new System.Drawing.Text.PrivateFontCollection();
                    temp.AddFontFile(file);

                    if (temp.Families[0].IsStyleAvailable(FontStyle.Bold & FontStyle.Italic))
                        _privateFontCollectionRegular.AddFontFile(file);
                    else if (temp.Families[0].IsStyleAvailable(FontStyle.Bold))
                        _privateFontCollectionBold.AddFontFile(file);
                    else if (temp.Families[0].IsStyleAvailable(FontStyle.Italic))
                        _privateFontCollectionItalic.AddFontFile(file);
                    else
                        _privateFontCollectionBoldItalic.AddFontFile(file);

                    //if (System.IO.Path.GetFileName(file).ToLower().Contains("bolditalic"))
                    //    _privateFontCollectionBoldItalic.AddFontFile(file);
                    //else if (System.IO.Path.GetFileName(file).ToLower().Contains("bold"))
                    //    _privateFontCollectionBold.AddFontFile(file);
                    //else if (System.IO.Path.GetFileName(file).ToLower().Contains("italic"))
                    //    _privateFontCollectionItalic.AddFontFile(file);
                    //else
                    //    _privateFontCollectionRegular.AddFontFile(file);
                }


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group