Ejemplo n.º 1
0
        internal static PixelBitmapContent <RgbaVector> Resize(
            this BitmapContent bitmap, int newWidth, int newHeight)
        {
            BitmapContent src = bitmap;

            src.TryGetFormat(out SurfaceFormat format);
            if (format != SurfaceFormat.Vector4)
            {
                var v4 = new PixelBitmapContent <RgbaVector>(src.Width, src.Height);
                BitmapContent.Copy(src, v4);
                src = v4;
            }

            using (var image = Image.WrapMemory <RgbaVector>(src.GetPixelData(), new Size(src.Height, src.Width)))
                using (var resized = image.ProcessRows(x => x.Resize(new Size(newWidth, newHeight), 0)))
                {
                    var result = new PixelBitmapContent <RgbaVector>(newWidth, newHeight);
                    result.SetPixelData(resized.GetPixelSpan());
                    return(result);
                }
        }
Ejemplo n.º 2
0
        // Rasterizes a single character glyph.
        private Glyph ImportGlyph(Rune character, Face face)
        {
            uint glyphIndex = face.GetCharIndex((uint)character.Value);

            face.LoadGlyph(glyphIndex, LoadFlags.Default, LoadTarget.Normal);
            face.Glyph.RenderGlyph(RenderMode.Normal);

            // Render the character.
            BitmapContent glyphBitmap = null;

            if (face.Glyph.Bitmap.Width > 0 && face.Glyph.Bitmap.Rows > 0)
            {
                glyphBitmap = new PixelBitmapContent <Alpha8>(face.Glyph.Bitmap.Width, face.Glyph.Bitmap.Rows);
                byte[] gpixelAlphas = new byte[face.Glyph.Bitmap.Width * face.Glyph.Bitmap.Rows];

                //if the character bitmap has 1bpp we have to expand the buffer data to get the 8bpp pixel data
                //each byte in bitmap.bufferdata contains the value of to 8 pixels in the row
                //if bitmap is of width 10, each row has 2 bytes with 10 valid bits, and the last 6 bits of 2nd byte must be discarded
                if (face.Glyph.Bitmap.PixelMode == PixelMode.Mono)
                {
                    //variables needed for the expansion, amount of written data, length of the data to write
                    int written = 0, length = face.Glyph.Bitmap.Width * face.Glyph.Bitmap.Rows;
                    for (int i = 0; written < length; i++)
                    {
                        //width in pixels of each row
                        int width = face.Glyph.Bitmap.Width;
                        while (width > 0)
                        {
                            //valid data in the current byte
                            int stride = Math.Min(8, width);
                            //copy the valid bytes to pixeldata
                            //System.Array.Copy(ExpandByte(face.Glyph.Bitmap.BufferData[i]), 0, gpixelAlphas, written, stride);
                            ExpandByteAndCopy(face.Glyph.Bitmap.BufferData[i], stride, gpixelAlphas, written);
                            written += stride;
                            width   -= stride;
                            if (width > 0)
                            {
                                i++;
                            }
                        }
                    }
                }
                else
                {
                    Marshal.Copy(face.Glyph.Bitmap.Buffer, gpixelAlphas, 0, gpixelAlphas.Length);
                }
                glyphBitmap.SetPixelData(gpixelAlphas);
            }

            if (glyphBitmap == null)
            {
                var gHA = face.Glyph.Metrics.HorizontalAdvance >> 6;
                var gVA = face.Size.Metrics.Height >> 6;

                gHA = gHA > 0 ? gHA : gVA;
                gVA = gVA > 0 ? gVA : gHA;

                glyphBitmap = new PixelBitmapContent <Alpha8>(gHA, gVA);
            }

            // not sure about this at all
            var abc = new ABCFloat
            {
                A = face.Glyph.Metrics.HorizontalBearingX >> 6,
                B = face.Glyph.Metrics.Width >> 6
            };

            abc.C = (face.Glyph.Metrics.HorizontalAdvance >> 6) - (abc.A + abc.B);

            // Construct the output Glyph object.
            return(new Glyph(character, glyphBitmap)
            {
                XOffset = -(face.Glyph.Advance.X >> 6),
                XAdvance = face.Glyph.Metrics.HorizontalAdvance >> 6,
                YOffset = -(face.Glyph.Metrics.HorizontalBearingY >> 6),
                CharacterWidths = abc
            });
        }