Пример #1
0
        public static IEnumerable<KeyValuePair<uint, uint>> FixedBitReader(byte[] Data, int BitCount = 0, int Offset = 0)
        {
            using (var BitReader = new BitReader(Data))
            {
                BitReader.Position = Offset;

                uint Index = 0;
                while (BitReader.BitsLeft >= BitCount)
                {
                    yield return new KeyValuePair<uint, uint>(Index++, BitReader.ReadBits(BitCount));
                }
            }
        }
Пример #2
0
        public void load(string FileName)
        {
            FileStream FileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);

            this.header = FileStream.ReadStruct <Header>();

            if (this.header.revision >= 3)
            {
                this.headerExtraRevision3 = FileStream.ReadStruct <HeaderRevision3>();
            }

            FileStream.ReadStructVector(ref dimensionTable, header.TableDimLength);
            FileStream.ReadStructVector(ref xAdjustTable, header.TableXAdjustLength);
            FileStream.ReadStructVector(ref yAdjustTable, header.TableYAdjustLength);
            FileStream.ReadStructVector(ref advanceTable, header.TableAdvanceLength);

            packedShadowCharMap = FileStream.ReadBytes(BitsToBytesHighAligned(header.TableShadowMapLength * header.TableShadowMapBpe));

            if (header.revision == 3)
            {
                FileStream.ReadStructVector(ref charmapCompressionTable1, headerExtraRevision3.TableCompCharMapLength1);
                FileStream.ReadStructVector(ref charmapCompressionTable2, headerExtraRevision3.TableCompCharMapLength2);
            }

            packedCharMap          = FileStream.ReadBytes(BitsToBytesHighAligned(header.TableCharMapLength * header.TableCharMapBpe));
            packedCharPointerTable = FileStream.ReadBytes(BitsToBytesHighAligned(header.TableCharPointerLength * header.TableCharPointerBpe));

            /*
             * int BytesLeft = (int)(FileStream.Length - FileStream.Position);
             * charData = new byte[BytesLeft];
             * FileStream.Read(charData, 0, BytesLeft);
             */

            charData = FileStream.ReadBytes((int)(FileStream.Length - FileStream.Position));

            var NumberOfCharacters = header.TableCharPointerLength;

            charMap        = new int[header.lastGlyph + 1];
            charPointer    = new int[NumberOfCharacters];
            Glyphs         = new Glyph[NumberOfCharacters];
            reverseCharMap = new Dictionary <int, int>();


            foreach (var Pair in BitReader.FixedBitReader(packedShadowCharMap, header.TableShadowMapBpe))
            {
                var UnicodeIndex = (int)Pair.Key + header.firstGlyph;
                var GlyphIndex   = (int)Pair.Value;
                shadowCharMap[UnicodeIndex]      = GlyphIndex;
                reverseShadowCharMap[GlyphIndex] = UnicodeIndex;
            }


            foreach (var Pair in BitReader.FixedBitReader(packedCharMap, header.TableCharMapBpe))
            {
                var UnicodeIndex = (int)Pair.Key + header.firstGlyph;
                var GlyphIndex   = (int)Pair.Value;
                charMap[UnicodeIndex]      = GlyphIndex;
                reverseCharMap[GlyphIndex] = UnicodeIndex;
            }

            foreach (var Pair in BitReader.FixedBitReader(packedCharPointerTable, header.TableCharPointerBpe))
            {
                charPointer[Pair.Key] = (int)Pair.Value;
            }

            /*
             * for (int n = 0; n < NumberOfCharacters; n++)
             * {
             *      Glyphs[n] = new Glyph().Read(this, n);
             * }
             */

            Console.WriteLine(this.header.fontName);

            /*
             * Console.WriteLine(this.header.fontName);
             * for (int n = 0; n < 300; n++)
             * {
             *      Console.WriteLine(GetGlyphId((char)n));
             * }
             */
        }
Пример #3
0
            public GlyphSymbol Read(PGF PGF, int GlyphIndex)
            {
                var br = new BitReader(PGF.charData);

                br.Position = PGF.charPointer[GlyphIndex] * 4 * 8;

                this.GlyphIndex  = GlyphIndex;
                this.UnicodeChar = (char)PGF.reverseCharMap[GlyphIndex];

                //int NextOffset = br.Position;

                //br.Position = NextOffset;
                int ShadowOffset = (int)br.Position + (int)br.ReadBits(14) * 8;

                if (GlyphType == GlyphFlags.FONT_PGF_SHADOWGLYPH)
                {
                    br.Position = ShadowOffset;
                    br.SkipBits(14);
                }

                this.Width  = br.ReadBits(7);
                this.Height = br.ReadBits(7);
                this.Left   = br.ReadBitsSigned(7);
                this.Top    = br.ReadBitsSigned(7);
                this.Flags  = (GlyphFlags)br.ReadBits(6);

                if (Flags.HasFlag(GlyphFlags.FONT_PGF_CHARGLYPH))
                {
                    br.SkipBits(7);
                    var shadowId = br.ReadBits(9);
                    br.SkipBits(24);
                    if (!Flags.HasFlag(GlyphFlags.FONT_PGF_METRIC_FLAG1))
                    {
                        br.SkipBits(56);
                    }
                    if (!Flags.HasFlag(GlyphFlags.FONT_PGF_METRIC_FLAG2))
                    {
                        br.SkipBits(56);
                    }
                    if (!Flags.HasFlag(GlyphFlags.FONT_PGF_METRIC_FLAG3))
                    {
                        br.SkipBits(56);
                    }
                    this.AdvanceIndex = br.ReadBits(8);
                }

                this.DataByteOffset = (uint)(br.Position / 8);

                uint PixelIndex           = 0;
                uint NumberOfPixels       = Width * Height;
                bool BitmapHorizontalRows = (Flags & GlyphFlags.FONT_PGF_BMP_OVERLAY) == GlyphFlags.FONT_PGF_BMP_H_ROWS;

                this.Data = new byte[NumberOfPixels];
                int  Count;
                uint Value = 0;
                uint x, y;

                //Console.WriteLine(br.BitsLeft);

                while (PixelIndex < NumberOfPixels)
                {
                    uint Code = br.ReadBits(4);

                    if (Code < 8)
                    {
                        Value = br.ReadBits(4);
                        Count = (int)Code + 1;
                    }
                    else
                    {
                        Count = 16 - (int)Code;
                    }

                    for (int n = 0; (n < Count) && (PixelIndex < NumberOfPixels); n++)
                    {
                        if (Code >= 8)
                        {
                            Value = br.ReadBits(4);
                        }

                        if (BitmapHorizontalRows)
                        {
                            x = PixelIndex % Width;
                            y = PixelIndex / Width;
                        }
                        else
                        {
                            x = PixelIndex / Height;
                            y = PixelIndex % Height;
                        }

                        this.Data[x + y * Width] = (byte)((Value << 0) | (Value << 4));
                        PixelIndex++;
                    }
                }

                /*
                 * for (int y = 0, n = 0; y < Height; y++)
                 * {
                 *      for (int x = 0; x < Width; x++, n++)
                 *      {
                 *              Console.Write("{0:X1}", this.Data[n] & 0xF);
                 *              //String.Format
                 *      }
                 *      Console.WriteLine("");
                 * }
                 *
                 */
                //Console.WriteLine(this);

                return(this);
            }
Пример #4
0
 public static uint ReadBitsAt(byte[] Data, int Offset, int Count)
 {
     var BitReader = new BitReader(Data);
     BitReader.Position = Offset;
     return BitReader.ReadBits(Count);
 }