Esempio n. 1
0
        /// <summary>
        ///     Adds <i>fontFile</i> to this private font collection.
        /// </summary>
        /// <param name="fontFile">
        ///     Absolute path to a TrueType font or collection.
        /// </param>
        /// <exception cref="FileNotFoundException">
        ///     If <i>fontFile</i> does not exist.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     If <i>fontFile</i> has already been added.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     If <i>fontFile</i> cannot be added to the system font collection.
        /// </exception>
        public void AddFontFile(FileInfo fontFile)
        {
            if (fontFile == null)
            {
                throw new ArgumentNullException("fontFile", "Parameter cannot be null");
            }
            if (!fontFile.Exists)
            {
                throw new FileNotFoundException("Font file does not exist", fontFile.FullName);
            }
            if (fonts.Contains(fontFile.FullName))
            {
                throw new ArgumentException("Font file already exists", "fontFile");
            }

            // Dispose needs the font filename to remove it from the system
            string absolutePath = fontFile.FullName;

            fonts.Add(absolutePath, String.Empty);

            // AddFontResourceEx returns the number of fonts added which
            // may be greater than 1 if adding a TrueType collection.
            if (LibWrapper.AddFontResourceEx(absolutePath, FR_PRIVATE, 0) == 0)
            {
                throw new ArgumentException("Unable to add font file: " + absolutePath, "fontFile");
            }
        }
        internal GdiFontMetrics(GdiDeviceContent dc, GdiFont currentFont)
        {
            if (dc.Handle == IntPtr.Zero)
            {
                throw new ArgumentNullException("dc", "Handle to device context cannot be null");
            }
            if (dc.GetCurrentObject(GdiDcObject.Font) == IntPtr.Zero)
            {
                throw new ArgumentException("dc", "No font selected into supplied device context");
            }
            this.dc          = dc;
            this.currentFont = currentFont;

            // FontFileReader requires the font facename because the font may exist in
            // a TrueType collection.
            StringBuilder builder = new StringBuilder(255);

            LibWrapper.GetTextFace(dc.Handle, builder.Capacity, builder);
            faceName = builder.ToString();

            ranges    = new GdiUnicodeRanges(dc);
            reader    = new FontFileReader(new MemoryStream(GetFontData()), faceName);
            converter = new PdfUnitConverter(EmSquare);

            // After we have cached the font data, we can safely delete the resource
            currentFont.Dispose();
        }
        /// <summary>
        ///     Gets font metric data for a TrueType font or TrueType collection.
        /// </summary>
        /// <returns></returns>
        public byte[] GetFontData()
        {
            if (data == null)
            {
                try {
                    // Check if this is a TrueType font collection
                    uint ttcfTag  = TableNames.ToUint(TableNames.Ttcf);
                    uint ttcfSize = LibWrapper.GetFontData(dc.Handle, ttcfTag, 0, null, 0);

                    if (ttcfSize != 0 && ttcfSize != 0xFFFFFFFF)
                    {
                        data = ReadFontFromCollection();
                    }
                    else
                    {
                        data = ReadFont();
                    }
                }
                catch (Exception e) {
                    throw new Exception(
                              String.Format("Failed to load data for font {0}", FaceName), e);
                }
            }

            return(data);
        }
Esempio n. 4
0
        /// <summary>
        ///     Populates the <i>indices</i> array with the glyph index of each
        ///     character represented by this rnage starting at <see cref="Start"/>.
        /// </summary>
        private void LoadGlyphIndices()
        {
            string characters = BuildString();

            indices = new ushort[characters.Length];
            LibWrapper.GetGlyphIndices(dc.Handle, characters, characters.Length, indices, 0);
        }
Esempio n. 5
0
 protected virtual void Dispose(bool disposing)
 {
     if (hFont != IntPtr.Zero)
     {
         //Console.WriteLine("Dispoing of font {0}, {1}pt ({2})", faceName, height, hFont);
         LibWrapper.DeleteObject(hFont);
         hFont = IntPtr.Zero;
     }
 }
Esempio n. 6
0
        /// <summary>
        ///     Returns a list of font styles associated with <i>familyName</i>.
        /// </summary>
        /// <param name="familyName"></param>
        /// <returns></returns>
        public FontStyles GetStyles(string familyName)
        {
            styles.Clear();
            FontEnumDelegate font = new FontEnumDelegate(EnumFontMethod);

            LibWrapper.EnumFontFamilies(dc.Handle, familyName, font, ExtractStyles);

            return(styles);
        }
        /// <summary>
        ///     Delete the device context freeing the associated memory.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (hDC != IntPtr.Zero)
            {
                LibWrapper.DeleteDC(hDC);

                // Mark as deleted
                hDC = IntPtr.Zero;
            }
        }
Esempio n. 8
0
        /// <summary>
        ///     Delete the device context freeing the associated memory.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (this.hDC != IntPtr.Zero)
            {
                //LibWrapper.DeleteDC(hDC);
                LibWrapper.ReleaseDC(IntPtr.Zero, this.hDC);

                // Mark as deleted
                this.hDC = IntPtr.Zero;
            }
        }
Esempio n. 9
0
        /// <summary>
        ///     Creates a font based on the supplied typeface name and size.
        /// </summary>
        /// <param name="faceName">The typeface name of a font.</param>
        /// <param name="height">
        ///     The height, in logical units, of the font's character
        ///     cell or character.
        /// </param>
        /// <returns></returns>
        public static GdiFont CreateFont(string faceName, int height, bool bold, bool italic)
        {
            LogFont lf = new LogFont();

            lf.lfCharSet  = 1; // Default charset
            lf.lfFaceName = faceName;
            lf.lfHeight   = height;
            lf.lfWeight   = (bold) ? 700 : 0;
            lf.lfItalic   = Convert.ToByte(italic);

            return(new GdiFont(LibWrapper.CreateFontIndirect(lf), faceName, height));
        }
Esempio n. 10
0
        private byte[] ReadTableData(string tableName)
        {
            uint tag  = TableNames.ToUint(tableName);
            uint size = LibWrapper.GetFontData(dc.Handle, tag, 0, null, 0);

            byte[] data = new byte[size];
            uint   rv   = LibWrapper.GetFontData(dc.Handle, tag, 0, data, (uint)data.Length);

            if (rv == GdiFontMetrics.GDI_ERROR)
            {
                throw new Exception("Failed to retrieve table " + tableName);
            }

            return(data);
        }
        /// <summary>
        ///     Loads all the unicode ranges.
        /// </summary>
        private void LoadRanges(GdiDeviceContent dc)
        {
            GlyphSet glyphSet = new GlyphSet();
            uint     size     = LibWrapper.GetFontUnicodeRanges(dc.Handle, glyphSet);

            if (size == 0)
            {
                throw new Exception("Unable to retrieve unicode ranges.");
            }

            unicodeRanges = new UnicodeRange[glyphSet.cRanges];
            for (int i = 0, offset = 0; i < glyphSet.cRanges; i++)
            {
                ushort wcLow   = (ushort)(glyphSet.ranges[offset++] + (glyphSet.ranges[offset++] << 8));
                ushort cGlyphs = (ushort)(glyphSet.ranges[offset++] + (glyphSet.ranges[offset++] << 8));
                unicodeRanges[i] = new UnicodeRange(dc, wcLow, (ushort)(wcLow + cGlyphs - 1));
            }
        }
Esempio n. 12
0
        private byte[] ReadFont()
        {
            uint bufferSize = LibWrapper.GetFontData(dc.Handle, 0, 0, null, 0);

            if (bufferSize == 0xFFFFFFFF)
            {
                throw new InvalidOperationException("No font selected into device context");
            }

            byte[] buffer = new byte[bufferSize];
            uint   rv     = LibWrapper.GetFontData(dc.Handle, 0, 0, buffer, bufferSize);

            if (rv == GDI_ERROR)
            {
                throw new Exception("Failed to retrieve table data for font " + FaceName);
            }

            return(buffer);
        }
 /// <summary>
 ///     Gets a handle to an object of the specified type that has been
 ///     selected into this device context.
 /// </summary>
 public IntPtr GetCurrentObject(GdiDcObject objectType)
 {
     return(LibWrapper.GetCurrentObject(hDC, objectType));
 }
 /// <summary>
 ///     Selects a font into a device context (DC). The new object
 ///     replaces the previous object of the same type.
 /// </summary>
 /// <param name="font">Handle to object.</param>
 /// <returns>A handle to the object being replaced.</returns>
 public IntPtr SelectFont(GdiFont font)
 {
     return(LibWrapper.SelectObject(hDC, font.Handle));
 }
 /// <summary>
 ///     Creates a new device context that matches the desktop display surface
 /// </summary>
 public GdiDeviceContent()
 {
     //this.hDC = LibWrapper.CreateDC("Display", String.Empty, null, IntPtr.Zero);
     this.hDC = LibWrapper.GetDC(IntPtr.Zero);
 }