Exemplo n.º 1
0
        /// <summary>
        /// Convert a bitmap object with depth 1bpp, 2bpp, 4bpp, or 8bpp to a bitmap object with depth 8bpp, making the
        /// number of used bytes per line (a.k.a. the ‘pitch’) a multiple of ‘alignment’.
        /// </summary>
        /// <remarks><para>
        /// It is possible to call <see cref="Convert"/> multiple times without calling
        /// <see cref="Dispose()"/> (the memory is simply reallocated).
        /// </para><para>
        /// Use <see cref="Dispose()"/> to finally remove the bitmap object.
        /// </para><para>
        /// The ‘library’ argument is taken to have access to FreeType's memory handling functions.
        /// </para></remarks>
        /// <param name="library">A handle to a library object.</param>
        /// <param name="alignment">
        /// The pitch of the bitmap is a multiple of this parameter. Common values are 1, 2, or 4.
        /// </param>
        /// <returns>The target bitmap.</returns>
        public FTBitmap Convert(Library library, int alignment)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
            }

            if (library == null)
            {
                throw new ArgumentNullException("library");
            }

            FTBitmap newBitmap = new FTBitmap(library);
            IntPtr   bmpRef    = newBitmap.reference;
            Error    err       = FT.FT_Bitmap_Convert(library.Reference, Reference, bmpRef, alignment);

            newBitmap.Reference = bmpRef;

            if (err != Error.Ok)
            {
                throw new FreeTypeException(err);
            }

            return(newBitmap);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Render an outline within a bitmap. The outline's image is simply OR-ed to the target bitmap.
        /// </summary>
        /// <remarks><para>
        /// This function does NOT CREATE the bitmap, it only renders an outline image within the one you pass to it!
        /// Consequently, the various fields in ‘abitmap’ should be set accordingly.
        /// </para><para>
        /// It will use the raster corresponding to the default glyph format.
        /// </para><para>
        /// The value of the ‘num_grays’ field in ‘abitmap’ is ignored. If you select the gray-level rasterizer, and
        /// you want less than 256 gray levels, you have to use <see cref="Render(RasterParams)"/> directly.
        /// </para></remarks>
        /// <param name="bitmap">A pointer to the target bitmap descriptor.</param>
        public void GetBitmap(FTBitmap bitmap)
        {
            Error err = FT.FT_Outline_Get_Bitmap(parentLibrary.Reference, Reference, bitmap.Reference);

            if (err != Error.Ok)
            {
                throw new FreeTypeException(err);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Render an outline within a bitmap. The outline's image is simply OR-ed to the target bitmap.
        /// </summary>
        /// <remarks><para>
        /// This function does NOT CREATE the bitmap, it only renders an outline image within the one you pass to it!
        /// Consequently, the various fields in ‘abitmap’ should be set accordingly.
        /// </para><para>
        /// It will use the raster corresponding to the default glyph format.
        /// </para><para>
        /// The value of the ‘num_grays’ field in ‘abitmap’ is ignored. If you select the gray-level rasterizer, and
        /// you want less than 256 gray levels, you have to use <see cref="Render(RasterParams)"/> directly.
        /// </para></remarks>
        /// <param name="bitmap">A pointer to the target bitmap descriptor.</param>
        public void GetBitmap(FTBitmap bitmap)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("Outline", "Cannot access a disposed object.");
            }

            Error err = FT.FT_Outline_Get_Bitmap(parentLibrary.Reference, reference, bitmap.Reference);

            if (err != Error.Ok)
            {
                throw new FreeTypeException(err);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Convert a bitmap object with depth 1bpp, 2bpp, 4bpp, or 8bpp to a bitmap object with depth 8bpp, making the
        /// number of used bytes per line (a.k.a. the ‘pitch’) a multiple of ‘alignment’.
        /// </summary>
        /// <remarks><para>
        /// It is possible to call <see cref="Convert"/> multiple times without calling
        /// <see cref="Dispose"/> (the memory is simply reallocated).
        /// </para><para>
        /// Use <see cref="Dispose"/> to finally remove the bitmap object.
        /// </para><para>
        /// The ‘library’ argument is taken to have access to FreeType's memory handling functions.
        /// </para></remarks>
        /// <param name="library">A handle to a library object.</param>
        /// <param name="alignment">
        /// The pitch of the bitmap is a multiple of this parameter. Common values are 1, 2, or 4.
        /// </param>
        /// <returns>The target bitmap.</returns>
        public FTBitmap Convert(Library library, int alignment)
        {
            if (library == null)
            {
                throw new ArgumentNullException("library");
            }

            FTBitmap newBitmap = new FTBitmap(library);
            IntPtr   bmpRef    = newBitmap.Reference;
            Error    err       = FT.FT_Bitmap_Convert(library.Reference, Reference, bmpRef, alignment);

            if (err != Error.Ok)
            {
                throw new FreeTypeException(err);
            }

            return(newBitmap);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Render an outline within a bitmap. The outline's image is simply OR-ed to the target bitmap.
        /// </summary>
        /// <remarks><para>
        /// This function does NOT CREATE the bitmap, it only renders an outline image within the one you pass to it!
        /// Consequently, the various fields in ‘abitmap’ should be set accordingly.
        /// </para><para>
        /// It will use the raster corresponding to the default glyph format.
        /// </para><para>
        /// The value of the ‘num_grays’ field in ‘abitmap’ is ignored. If you select the gray-level rasterizer, and
        /// you want less than 256 gray levels, you have to use <see cref="Render(Library, RasterParams)"/> directly.
        /// </para></remarks>
        /// <param name="library">A handle to a FreeType library object.</param>
        /// <param name="bitmap">A pointer to the target bitmap descriptor.</param>
        public void GetBitmap(Library library, FTBitmap bitmap)
        {
            if (library == null)
            {
                throw new ArgumentNullException(nameof(library));
            }

            if (bitmap == null)
            {
                throw new ArgumentNullException(nameof(bitmap));
            }

            Error err = FT.FT_Outline_Get_Bitmap(library.Reference, Reference, bitmap.Reference);

            if (err != Error.Ok)
            {
                throw new FreeTypeException(err);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Render an outline within a bitmap. The outline's image is simply OR-ed to the target bitmap.
        /// </summary>
        /// <remarks><para>
        /// This function does NOT CREATE the bitmap, it only renders an outline image within the one you pass to it!
        /// Consequently, the various fields in ‘abitmap’ should be set accordingly.
        /// </para><para>
        /// It will use the raster corresponding to the default glyph format.
        /// </para><para>
        /// The value of the ‘num_grays’ field in ‘abitmap’ is ignored. If you select the gray-level rasterizer, and
        /// you want less than 256 gray levels, you have to use <see cref="Render(Library, RasterParams)"/> directly.
        /// </para></remarks>
        /// <param name="library">A handle to a FreeType library object.</param>
        /// <param name="bitmap">A pointer to the target bitmap descriptor.</param>
        public void GetBitmap(Library library, FTBitmap bitmap)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("Outline", "Cannot access a disposed object.");
            }

            if (library == null)
            {
                throw new ArgumentNullException("library");
            }

            Error err = FT.FT_Outline_Get_Bitmap(library.Reference, reference, bitmap.Reference);

            if (err != Error.Ok)
            {
                throw new FreeTypeException(err);
            }
        }
Exemplo n.º 7
0
		/// <summary>
		/// Render an outline within a bitmap. The outline's image is simply OR-ed to the target bitmap.
		/// </summary>
		/// <remarks><para>
		/// This function does NOT CREATE the bitmap, it only renders an outline image within the one you pass to it!
		/// Consequently, the various fields in ‘abitmap’ should be set accordingly.
		/// </para><para>
		/// It will use the raster corresponding to the default glyph format.
		/// </para><para>
		/// The value of the ‘num_grays’ field in ‘abitmap’ is ignored. If you select the gray-level rasterizer, and
		/// you want less than 256 gray levels, you have to use <see cref="Render(Library, RasterParams)"/> directly.
		/// </para></remarks>
		/// <param name="library">A handle to a FreeType library object.</param>
		/// <param name="bitmap">A pointer to the target bitmap descriptor.</param>
		public void GetBitmap(Library library, FTBitmap bitmap)
		{
			if (disposed)
				throw new ObjectDisposedException("Outline", "Cannot access a disposed object.");

			if (library == null)
				throw new ArgumentNullException("library");

			if (bitmap == null)
				throw new ArgumentNullException("bitmap");

			Error err = FT.FT_Outline_Get_Bitmap(library.Reference, reference, bitmap.Reference);

			if (err != Error.Ok)
				throw new FreeTypeException(err);
		}
Exemplo n.º 8
0
		/// <summary>
		/// Render an outline within a bitmap. The outline's image is simply OR-ed to the target bitmap.
		/// </summary>
		/// <remarks><para>
		/// This function does NOT CREATE the bitmap, it only renders an outline image within the one you pass to it!
		/// Consequently, the various fields in ‘abitmap’ should be set accordingly.
		/// </para><para>
		/// It will use the raster corresponding to the default glyph format.
		/// </para><para>
		/// The value of the ‘num_grays’ field in ‘abitmap’ is ignored. If you select the gray-level rasterizer, and
		/// you want less than 256 gray levels, you have to use <see cref="Render(RasterParams)"/> directly.
		/// </para></remarks>
		/// <param name="bitmap">A pointer to the target bitmap descriptor.</param>
		public void GetBitmap(FTBitmap bitmap)
		{
			if (disposed)
				throw new ObjectDisposedException("Outline", "Cannot access a disposed object.");

			Error err = FT.FT_Outline_Get_Bitmap(parentLibrary.Reference, reference, bitmap.Reference);

			if (err != Error.Ok)
				throw new FreeTypeException(err);
		}
Exemplo n.º 9
0
        private Glyph LoadGlyph(int codePoint, int characterSize, bool bold, float outlineThickness)
        {
            Glyph glyph = new Glyph();

            if (_face == null)
            {
                return(null);
            }

            // Set the character size
            if (!SetCurrentSize(characterSize))
            {
                return(glyph);
            }

            // Load the glyph corresponding to the code point
            var flags = LoadFlags.ForceAutohint;

            if (outlineThickness != 0)
            {
                flags |= LoadFlags.NoBitmap;
            }

            _face.LoadChar((uint)codePoint, flags, SharpFont.LoadTarget.Normal);

            // Retrieve the glyph
            SharpFont.Glyph glyphDesc = _face.Glyph.GetGlyph();

            // Apply bold if necessary -- first technique using outline (highest quality)
            SharpFont.Fixed26Dot6 weight = new SharpFont.Fixed26Dot6(1);
            bool outline = glyphDesc.Format == SharpFont.GlyphFormat.Outline;

            if (outline)
            {
                if (bold)
                {
                    SharpFont.OutlineGlyph outlineGlyph = glyphDesc.ToOutlineGlyph();
                    outlineGlyph.Outline.Embolden(weight);
                }

                if (outlineThickness != 0)
                {
                    _stroker.Set((int)(outlineThickness * Fixed26Dot6.FromInt32(1).Value),
                                 StrokerLineCap.Round, StrokerLineJoin.Round, Fixed16Dot16.FromSingle(0));

                    // This function returning a new instance of Glyph
                    // Because the pointer may changed upon applying stroke to the glyph
                    glyphDesc = glyphDesc.Stroke(_stroker, false);
                }
            }

            // Convert the glyph to a bitmap (i.e. rasterize it)
            glyphDesc.ToBitmap(SharpFont.RenderMode.Normal, new FTVector26Dot6(0, 0), true);
            SharpFont.FTBitmap bitmap = glyphDesc.ToBitmapGlyph().Bitmap;

            // Apply bold if necessary -- fallback technique using bitmap (lower quality)
            if (!outline)
            {
                if (bold)
                {
                    bitmap.Embolden(_library, weight, weight);
                }

                if (outlineThickness != 0)
                {
                    Logger.Warning("Failed to outline glyph (no fallback available)");
                }
            }

            // Compute the glyph's advance offset
            glyph.Advance = _face.Glyph.Metrics.HorizontalAdvance.ToSingle();
            if (bold)
            {
                glyph.Advance += weight.ToSingle();
            }

            int width  = bitmap.Width;
            int height = bitmap.Rows;

            if ((width > 0) && (height > 0))
            {
                // Leave a small padding around characters, so that filtering doesn't
                // pollute them with pixels from neighbors
                int padding = 1;

                // Get the glyphs page corresponding to the character size
                Page page = _pages[characterSize];

                // Find a good position for the new glyph into the texture
                glyph.TexCoords = FindGlyphRectangle(page, width + 2 * padding, height + 2 * padding);
                var texRect = glyph.TexCoords;

                // Make sure the texture data is positioned in the center
                // of the allocated texture rectangle
                glyph.TexCoords = new Rectangle(texRect.X + padding, texRect.Y + padding,
                                                texRect.Width - 2 * padding, texRect.Height - 2 * padding);

                // Compute the glyph's bounding box
                float boundsX      = (float)(_face.Glyph.Metrics.HorizontalBearingX);
                float boundsY      = -(float)(_face.Glyph.Metrics.HorizontalBearingY);
                float boundsWidth  = (float)(_face.Glyph.Metrics.Width) + outlineThickness * 2;
                float boundsHeight = (float)(_face.Glyph.Metrics.Height) + outlineThickness * 2;
                glyph.Bounds = new RectangleF(boundsX, boundsY, boundsWidth, boundsHeight);

                // Extract the glyph's pixels from the bitmap
                byte[] pixelBuffer = new byte[width * height * 4];
                for (int i = 0; i < pixelBuffer.Length; i++)
                {
                    pixelBuffer[i] = 255;
                }

                unsafe
                {
                    byte *pixels = (byte *)bitmap.Buffer.ToPointer();
                    if (bitmap.PixelMode == SharpFont.PixelMode.Mono)
                    {
                        // Pixels are 1 bit monochrome values
                        for (int y = 0; y < height; ++y)
                        {
                            for (int x = 0; x < width; ++x)
                            {
                                // The color channels remain white, just fill the alpha channel
                                int index = (x + y * width) * 4 + 3;
                                pixelBuffer[index] = (byte)((((pixels[x / 8]) & (1 << (7 - (x % 8)))) > 0) ? 255 : 0);
                            }
                            pixels += bitmap.Pitch;
                        }
                    }
                    else
                    {
                        // Pixels are 8 bits gray levels
                        for (int y = 0; y < height; ++y)
                        {
                            for (int x = 0; x < width; ++x)
                            {
                                // The color channels remain white, just fill the alpha channel
                                int index = (x + y * width) * 4 + 3;
                                pixelBuffer[index] = pixels[x];
                            }
                            pixels += bitmap.Pitch;
                        }
                    }
                }

                // Write the pixels to the texture
                int tx = glyph.TexCoords.Left;
                int ty = glyph.TexCoords.Top;
                int tw = glyph.TexCoords.Width;
                int th = glyph.TexCoords.Height;
                page.texture.Update(pixelBuffer, tx, ty, tw, th);
            }

            // Delete the FT glyph
            glyphDesc.Dispose();

            return(glyph);
        }
Exemplo n.º 10
0
		/// <summary>
		/// Convert a bitmap object with depth 1bpp, 2bpp, 4bpp, or 8bpp to a bitmap object with depth 8bpp, making the
		/// number of used bytes per line (a.k.a. the ‘pitch’) a multiple of ‘alignment’.
		/// </summary>
		/// <remarks><para>
		/// It is possible to call <see cref="Convert"/> multiple times without calling
		/// <see cref="Dispose()"/> (the memory is simply reallocated).
		/// </para><para>
		/// Use <see cref="Dispose()"/> to finally remove the bitmap object.
		/// </para><para>
		/// The ‘library’ argument is taken to have access to FreeType's memory handling functions.
		/// </para></remarks>
		/// <param name="library">A handle to a library object.</param>
		/// <param name="alignment">
		/// The pitch of the bitmap is a multiple of this parameter. Common values are 1, 2, or 4.
		/// </param>
		/// <returns>The target bitmap.</returns>
		public FTBitmap Convert(Library library, int alignment)
		{
			if (disposed)
				throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");

			if (library == null)
				throw new ArgumentNullException("library");

			FTBitmap newBitmap = new FTBitmap(library);
			IntPtr bmpRef = newBitmap.reference;
			Error err = FT.FT_Bitmap_Convert(library.Reference, Reference, bmpRef, alignment);
			newBitmap.Reference = bmpRef;

			if (err != Error.Ok)
				throw new FreeTypeException(err);

			return newBitmap;
		}