//static XPrivateFontCollection()
    //{
    //  // HACK: Use one global PrivateFontCollection in GDI+
    //  // TODO: Make a list of it
    //  if (global != null)
    //    throw new InvalidOperationException("Because of limitations in GDI+ you can only have one instance of XPrivateFontCollection in your application.");
    //  global = this;
    //}
    //internal static XPrivateFontCollection global;

    /// <summary>
    /// Disposes all fonts from the collection.
    /// </summary>
    public void Dispose()
    {
#if GDI
      //privateFonts.Clear();
      this.privateFontCollection.Dispose();
      this.privateFontCollection = new PrivateFontCollection();
#endif
      s_global = null;
      //GC.SuppressFinalize(this);
    }
        //static XPrivateFontCollection()
        //{
        //  // HACK: Use one global PrivateFontCollection in GDI+
        //  // TODO: Make a list of it
        //  if (global != null)
        //    throw new InvalidOperationException("Because of limitations in GDI+ you can only have one instance of XPrivateFontCollection in your application.");
        //  global = this;
        //}
        //internal static XPrivateFontCollection global;

        /// <summary>
        /// Disposes all fonts from the collection.
        /// </summary>
        public void Dispose()
        {
#if GDI
            //privateFonts.Clear();
            this.privateFontCollection.Dispose();
            this.privateFontCollection = new PrivateFontCollection();
#endif
            s_global = null;
            //GC.SuppressFinalize(this);
        }
        /// <summary>
        /// Sets a new global font collection and returns the previous one, or null if no previous one exists.
        /// </summary>
        public static XPrivateFontCollection SetGlobalFontCollection(XPrivateFontCollection fontCollection)
        {
            if (fontCollection == null)
            {
                throw new ArgumentNullException("fontCollection");
            }

            XPrivateFontCollection old = s_global;

            s_global = fontCollection;
            return(old);
        }
Example #4
0
 //#if 
 public TrueTypeDescriptor(XFont font, XPdfFontOptions options, XPrivateFontCollection privateFontCollection)
 {
   try
   {
     this.fontData = new FontData(font, options);
     this.fontName = font.Name;
     Initialize();
   }
   catch (Exception ex)
   {
     throw ex;
   }
 }
Example #5
0
        public static GdiFont CreateFont(string familyName, double emSize, GdiFontStyle style, out XFontSource fontSource)
        {
            GdiFont font = null;

            fontSource = null;
            // Use font resolver in CORE build. XPrivateFontCollection exists only in GDI and WPF build.
            // Try private font collection first.
            font = XPrivateFontCollection.TryCreateFont(familyName, emSize, style, out fontSource);
            if (font != null)
            {
                // Get font source is different for this font because Win32 does not know it.
                return(font);
            }
            return(font);
        }
Example #6
0
        public static GdiFont CreateFont(string familyName, double emSize, GdiFontStyle style, out XFontSource fontSource)
        {
            fontSource = null;
            // ReSharper disable once JoinDeclarationAndInitializer
            GdiFont font;

            // Use font resolver in CORE build. XPrivateFontCollection exists only in GDI and WPF build.
#if GDI
            // Try private font collection first.
            font = XPrivateFontCollection.TryCreateFont(familyName, emSize, style, out fontSource);
            if (font != null)
            {
                // Get font source is different for this font because Win32 does not know it.
                return(font);
            }
#endif
            // Create ordinary Win32 font.
            font = new GdiFont(familyName, (float)emSize, style, GraphicsUnit.World);
            return(font);
        }
    /// <summary>
    /// Sets a new global font collection and returns the previous one, or null if no previous one exists.
    /// </summary>
    public static XPrivateFontCollection SetGlobalFontCollection(XPrivateFontCollection fontCollection)
    {
      if (fontCollection==null)
        throw new ArgumentNullException("fontCollection");

      XPrivateFontCollection old = s_global;
      s_global = fontCollection;
      return old;
    }
Example #8
0
        /// <summary>
        /// Connects the specifications of a font from XFont to a real glyph type face.
        /// </summary>
        void Initialize()
        {
            XFontMetrics fm = null;

#if DEBUG___
            FontData[] fontDataArray = FontDataStock.Global.GetFontDataList();
            if (fontDataArray.Length > 0)
            {
                ////        GetType();
                ////#if GDI
                ////        var x = XPrivateFontCollection.global.GlobalPrivateFontCollection;
                ////        families = x.Families;

                ////        bool fff = families[0].IsStyleAvailable(System.Drawing.FontStyle.Regular);
                ////        fff.GetType();
                ////        this.font = new Font(families[0].Name, 12, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel);

                ////        this.font = new Font("Oblivious", 12, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel);

                ////        this.font = new Font(families[0], 12, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel);

                ////        System.Drawing.FontFamily f = new System.Drawing.FontFamily(families[0].Name);
                ////        f.GetType();
                ////#endif
            }
#endif
#if GDI
            if (this.font == null)
            {
                if (this.gdifamily != null)
                {
                    this.font       = new Font(this.gdifamily, (float)this.emSize, (System.Drawing.FontStyle) this.style, GraphicsUnit.World);
                    this.familyName = this.gdifamily.Name; // Do we need this???
                }
                else
                {
                    // First check private fonts
                    this.font = XPrivateFontCollection.TryFindPrivateFont(this.familyName, this.emSize, (System.Drawing.FontStyle) this.style) ??
                                new Font(this.familyName, (float)this.emSize, (System.Drawing.FontStyle) this.style, GraphicsUnit.World);
                }
#if DEBUG
                // new Font returns MSSansSerif if the requested font was not found ...
                //Debug.Assert(this.familyName == this.font.FontFamily.Name);
#endif
            }

            fm = Metrics;
            System.Drawing.FontFamily fontFamily = this.font.FontFamily;
            this.unitsPerEm = fm.UnitsPerEm;

            System.Drawing.FontFamily fontFamily2 = this.font.FontFamily;
            this.cellSpace = fontFamily2.GetLineSpacing(font.Style);
            //Debug.Assert(this.cellSpace == fm.Ascent + Math.Abs(fm.Descent) + fm.Leading, "Value differs from information retrieved from font image.");

            this.cellAscent = fontFamily.GetCellAscent(font.Style);
#pragma warning disable 1030
#warning delTHHO
            //!!!delTHHO 14.08.2008 Debug.Assert(this.cellAscent == fm.Ascent, "Value differs from information retrieved from font image.");
            //Debug.Assert(this.cellAscent == fm.Ascent, "Value differs from information retrieved from font image.");

            this.cellDescent = fontFamily.GetCellDescent(font.Style);
#if DEBUG
            int desc = Math.Abs(fm.Descent);
            if (this.cellDescent != desc)
            {
                Debug.Assert(false, "Value differs from information retrieved from font image.");
            }
#endif
#endif
#if WPF
#if !SILVERLIGHT
            if (this.family == null)
            {
                Debug.Assert(this.typeface == null);
                this.typeface = XPrivateFontCollection.TryFindTypeface(Name, this.style, out this.family);
#if true
                if (this.typeface != null)
                {
                    GlyphTypeface glyphTypeface;

                    ICollection <Typeface> list = this.family.GetTypefaces();
                    foreach (Typeface tf in list)
                    {
                        if (!tf.TryGetGlyphTypeface(out glyphTypeface))
                        {
                            Debugger.Break();
                        }
                    }

                    if (!this.typeface.TryGetGlyphTypeface(out glyphTypeface))
                    {
                        throw new InvalidOperationException(PSSR.CannotGetGlyphTypeface(Name));
                    }
                }
#endif
            }

            if (this.family == null)
            {
                this.family = new System.Windows.Media.FontFamily(Name);
            }

            if (typeface == null)
            {
                this.typeface = FontHelper.CreateTypeface(this.family, style);
            }

            fm = Metrics;
            Debug.Assert(this.unitsPerEm == 0 || this.unitsPerEm == fm.UnitsPerEm);
            this.unitsPerEm = fm.UnitsPerEm;

            //Debug.Assert(this.cellSpace == 0 || this.cellSpace == fm.Ascent + Math.Abs(fm.Descent) + fm.Leading);
            this.cellSpace = fm.Ascent + Math.Abs(fm.Descent) + fm.Leading;

            Debug.Assert(this.cellAscent == 0 || this.cellAscent == fm.Ascent);
            this.cellAscent = fm.Ascent;

            Debug.Assert(this.cellDescent == 0 || this.cellDescent == Math.Abs(fm.Descent));
            this.cellDescent = Math.Abs(fm.Descent);
#else
            if (fm != null)
            {
                fm.GetType();
            }
#endif
#endif
        }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="XFont"/> class.
 /// </summary>
 /// <param name="family">The font family.</param>
 /// <param name="emSize">The em size.</param>
 /// <param name="style">The font style.</param>
 /// <param name="pdfOptions">Additional PDF options.</param>
 /// <param name="privateFontCollection">The private font collection.</param>
 public XFont(System.Drawing.FontFamily family, double emSize, XFontStyle style, XPdfFontOptions pdfOptions,
   XPrivateFontCollection privateFontCollection)
 {
   this.familyName = null;
   this.gdifamily = family;
   this.size = emSize;
   this.style = style;
   this.pdfOptions = pdfOptions;
   this.privateFontCollection = privateFontCollection;
   Initialize();
 }
Example #10
0
    //#endif

    //#if WPF
    //    public TrueTypeDescriptor(XFont font, XPdfFontOptions options)
    //    {
    //      try
    //      {
    //        this.fontData = new FontData(font, options);
    //        this.fontName = font.Name;
    //        Initialize();
    //      }
    //      catch (Exception ex)
    //      {
    //        throw ex;
    //      }
    //    }
    //#endif

    //internal TrueTypeDescriptor(FontSelector selector)
    //{
    //  throw new NotImplementedException("TrueTypeDescriptor(FontSelector selector)");
    //}

    internal TrueTypeDescriptor(XFont font,
      XPrivateFontCollection privateFontCollection)
      : this(font, font.PdfOptions, privateFontCollection)
    { }
Example #11
0
    /// <summary>
    /// Create the font image using GDI+ functionality.
    /// </summary>
    void CreateGdiFontImage(XFont font, XPdfFontOptions options, XPrivateFontCollection privateFontCollection)
    {
      System.Drawing.Font gdiFont = font.RealizeGdiFont();
      NativeMethods.LOGFONT logFont;
#if DEBUG_
      logFont = new NativeMethods.LOGFONT();
      gdiFont.ToLogFont(logFont);
      Debug.WriteLine("FontData: " + logFont.lfFaceName);
#endif
      this.data = null;
      if (privateFontCollection != null)
      {
        //XPrivateFont privateFont = privateFontCollection.FindFont(logFont.lfFaceName, logFont.lfWeight >= 700, logFont.lfItalic > 0);
        XPrivateFont privateFont = privateFontCollection.FindFont(font.Name, font.Bold, font.Italic);
        if (privateFont != null)
        {
          int size = privateFont.GetFontData(ref this.data, 0);
          if (size > 0)
          {
            this.data = new byte[size];
            privateFont.GetFontData(ref this.data, size);
          }
        }
      }
      if (this.data == null)
      {
        int error;
        IntPtr hfont = gdiFont.ToHfont();
        IntPtr hdc = NativeMethods.GetDC(IntPtr.Zero);
        error = Marshal.GetLastWin32Error();
        IntPtr oldFont = NativeMethods.SelectObject(hdc, hfont);
        error = Marshal.GetLastWin32Error();
        // size is exactly the size of the font file.
        int size = NativeMethods.GetFontData(hdc, 0, 0, null, 0);
        error = Marshal.GetLastWin32Error();
        if (size > 0)
        {
          this.data = new byte[size];
          int effectiveSize = NativeMethods.GetFontData(hdc, 0, 0, this.data, this.data.Length);
          Debug.Assert(size == effectiveSize);
          NativeMethods.SelectObject(hdc, oldFont);
          NativeMethods.ReleaseDC(IntPtr.Zero, hdc);
          error.GetType();
        }
        else
        {
        // Sometimes size is -1 (GDI_ERROR), but I cannot determine why. It happens only with the font 'Symbol'.
        // The issue occurs the first time in early 2005, when I start writing PDFsharp. I could not fix it and after
        // some code refactoring the problem disappears.
        // There was never a report from anyone about this issue.
        // Now I get it again (while debugging QBX 2006). Maybe it is a problem with my PC at my home office.
        // As a work-around I create a new font handle with a different height value. This works. Maybe the
        // font file gets locked somewhere. Very very strange.

        // IF SOMEONE ELSE COMES HERE PLEASE LET ME KNOW!

        // Clean up old handles
        NativeMethods.SelectObject(hdc, oldFont);
        NativeMethods.ReleaseDC(IntPtr.Zero, hdc);

        // Try again with new font handle
        logFont = new NativeMethods.LOGFONT();
        gdiFont.ToLogFont(logFont);
        logFont.lfHeight += 1; // force new handle
        IntPtr hfont2 = NativeMethods.CreateFontIndirect(logFont);
        hdc = NativeMethods.GetDC(IntPtr.Zero);
        error = Marshal.GetLastWin32Error();
        oldFont = NativeMethods.SelectObject(hdc, hfont2);
        error = Marshal.GetLastWin32Error();
        // size is exactly the size of the font file.
        size = NativeMethods.GetFontData(hdc, 0, 0, null, 0);
        error = Marshal.GetLastWin32Error();
        if (size > 0)
        {
          this.data = new byte[size];
          int effectiveSize = NativeMethods.GetFontData(hdc, 0, 0, this.data, this.data.Length);
          Debug.Assert(size == effectiveSize);
        }
        NativeMethods.SelectObject(hdc, oldFont);
        NativeMethods.ReleaseDC(IntPtr.Zero, hdc);
        NativeMethods.DeleteObject(hfont2);
        error.GetType();
        }
      }
      if (this.data == null)
        throw new InvalidOperationException("Internal error. Font data could not retrieved.");
    }
Example #12
0
    /// <summary>
    /// Converts an DOM Font to an XFont.
    /// </summary>
    internal static XFont FontToXFont(Font font, XPrivateFontCollection pfc, 
      PdfFontEncoding encoding, PdfFontEmbedding fontEmbedding)
    {
      XFont xFont = null;
#if GDI____  // done in PDFsharp
#if CACHE_FONTS
      string signature = BuildSignature(font, unicode, fontEmbedding);
      xFont = fontCache[signature] as XFont;
      if (xFont == null)
      {
        XPdfFontOptions options = null;
        options = new XPdfFontOptions(fontEmbedding, unicode);
        XFontStyle style = GetXStyle(font);
        xFont = new XFont(font.Name, font.Size, style, options);
        fontCache[signature] = xFont;
      }
#else
      XPdfFontOptions options = null;
      options = new XPdfFontOptions(encoding, fontEmbedding);
      XFontStyle style = GetXStyle(font);
      if (pfc != null && pfc.PrivateFontCollection != null)
      {
        // Is it a private font?
        try
        {
          foreach (System.Drawing.FontFamily ff in pfc.PrivateFontCollection.Families)
          {
            if (String.Compare(ff.Name, font.Name, true) == 0)
            {
              xFont = new XFont(ff, font.Size, style, options, pfc);
              break;
            }
          }
        }
        catch
        {
#if DEBUG
          pfc.GetType();
#endif
        }
      }
#endif
#endif

#if WPF___
      XPdfFontOptions options = null;
      options = new XPdfFontOptions(encoding, fontEmbedding);
      XFontStyle style = GetXStyle(font);
      //if (pfc != null &&
      //  pfc.PrivateFontCollection != null)
      //{
      //  // Is it a private font?
      //  try
      //  {
      //    foreach (System.Drawing.FontFamily ff in pfc.PrivateFontCollection.Families)
      //    {
      //      if (String.Compare(ff.Name, font.Name, true) == 0)
      //      {
      //        xFont = new XFont(ff, font.Size, style, options, pfc);
      //        break;
      //      }
      //    }
      //  }
      //  catch
      //  {
      //  }
      //}
#endif

      // #PFC
      XPdfFontOptions options = null;
      options = new XPdfFontOptions(encoding, fontEmbedding);
      XFontStyle style = GetXStyle(font);

      if (xFont == null)
        xFont = new XFont(font.Name, font.Size, style, options);
#if DEBUG
      CreateFontCounter++;
#endif
      return xFont;
    }