Esempio n. 1
0
        private int FindASCIIOffset(byte i)
        {
            int offset;

            for (offset = 0; offset < _UnicodeMappings.Count; offset++)
            {
                UnicodeMapping mapping = _UnicodeMappings[offset];
                if (mapping.ASCIICharacters.Contains(i))
                {
                    break;
                }
            }

            return(offset);
        }
Esempio n. 2
0
        public PCScreenFont(byte[] aFileData)
        {
            bool version1 = aFileData[0] == 0x36 && aFileData[1] == 0x04;
            bool version2 = BitConverter.ToUInt32(aFileData, 0) == 0x864ab572;

            //Check the header
            if (!version1 && !version2)
            {
                Global.mDebugger.Send($"Invalid magic {aFileData[0]} {aFileData[1]} {aFileData[2]} {aFileData[3]}");
                throw new Exception($"Invalid magic {aFileData[0]} {aFileData[1]} {aFileData[2]} {aFileData[3]}");
            }
            if (version1)
            {
                byte mode = aFileData[2];
                CharHeight = aFileData[3];
                CharWidth  = 8; //Always 8 in this case
                int    length          = (mode & (int)PSFVersion1Mode.MODE512) == 1 ? 512 : 256;
                bool   hasUnicodeTable = (mode & (int)PSFVersion1Mode.HASTAB) > 0;
                ushort seperator       = 0xFFFF;
                ushort sequenceStart   = 0xFFFE;
                _FontData = new byte[length * CharHeight]; //Every row is one byte
                for (int i = 0; i < length; i++)
                {
                    for (int k = 0; k < CharHeight; k++)
                    {
                        _FontData[i * CharHeight + k] = aFileData[4 + i * CharHeight + k];
                    }
                }
                int position = 4 + length * CharHeight;
                if (hasUnicodeTable)
                {
                    _UnicodeMappings = new List <UnicodeMapping>();
                    var currentEntry = new List <byte>();
                    while (position < aFileData.Length)
                    {
                        if (BitConverter.ToUInt16(aFileData, position) == seperator)
                        {
                            var mapping = new UnicodeMapping
                            {
                                FontPosition      = _UnicodeMappings.Count,
                                UnicodeCharacters = new List <ushort>(),
                                UnicodeCharactersWithModifiers = new List <ushort[]>(),
                                ASCIICharacters = new List <byte>()
                            };
                            for (int i = 0; i < currentEntry.Count / 2; i++)
                            {
                                mapping.UnicodeCharacters.Add(BitConverter.ToUInt16(currentEntry.ToArray(), i * 2));
                            }
                            //At this point we filter combined unicode letters out of the unicode charactesr
                            bool reachedFirstSeperator = false;
                            var  unicodeCombination    = new List <ushort>();
                            int  index = 0;
                            while (index < mapping.UnicodeCharacters.Count)
                            {
                                if (mapping.UnicodeCharacters[index] == sequenceStart)
                                {
                                    mapping.UnicodeCharacters.RemoveAt(index);
                                    if (!reachedFirstSeperator)
                                    {
                                        reachedFirstSeperator = true;
                                    }
                                    else
                                    {
                                        mapping.UnicodeCharactersWithModifiers.Add(unicodeCombination.ToArray());
                                    }
                                }
                                else
                                {
                                    if (reachedFirstSeperator)
                                    {
                                        unicodeCombination.Add(mapping.UnicodeCharacters[index]);
                                        mapping.UnicodeCharacters.RemoveAt(index);
                                    }
                                    else
                                    {
                                        index++;
                                    }
                                }
                            }
                            // Now convert all the unicode characters we can to ASCII
                            foreach (var uc in mapping.UnicodeCharacters)
                            {
                                byte ac = Encoding.ASCII.GetBytes(Encoding.Unicode.GetString(BitConverter.GetBytes(uc)))[0];
                                if (ac == 63 && uc != 0x003F)
                                {
                                    //ignore
                                }
                                else
                                {
                                    if (!mapping.ASCIICharacters.Contains(ac))
                                    {
                                        mapping.ASCIICharacters.Add(ac);
                                    }
                                }
                            }

                            _UnicodeMappings.Add(mapping);
                            currentEntry.Clear();
                            position++; //Skip the second seperator character as well
                        }
                        else
                        {
                            currentEntry.Add(aFileData[position]);
                        }
                        position++;
                    }
                }
            }
            if (version2)
            {
                throw new NotImplementedException();
            }
        }