コード例 #1
0
        //private static void DecodeLine(Stream stream, out HelpLine line)
        //{
        //    line = null;

        //    // Read text length in bytes.
        //    int textLength = stream.ReadByte();
        //    if (textLength < 0)
        //        throw new EndOfStreamException("Cannot read text length byte.");
        //    byte[] textBytes = new byte[textLength];
        //    stream.ReadFull(textBytes, 0, textBytes.Length);
        //    string text = Graphic437.GetString(textBytes);
        //    line = new HelpLine(text);

        //    // Read byte count of attributes.
        //    int attrLength = reader.ReadByte();
        //    BufferReader attrReader = reader.ReadBuffer(attrLength - 1);
        //    DecodeLineAttributes(line, attrReader);

        //    // Read hyperlinks.
        //    while (!attrReader.IsEOF)
        //    {
        //        DecodeLineHyperlink(line, attrReader);
        //    }
        //}

        private static void DecodeLineAttributes(HelpLine line, BufferReader reader)
        {
            int charIndex = 0;

            for (int chunkIndex = 0; !reader.IsEOF; chunkIndex++)
            {
                TextStyle textStyle = TextStyle.None;

                // Read attribute byte except for the first chunk (for which
                // default attributes are applied).
                if (chunkIndex > 0)
                {
                    byte a = reader.ReadByte();
                    if (a == 0xFF) // marks the beginning of hyperlinks
                    {
                        break;
                    }

                    textStyle = TextStyle.None;
                    if ((a & 1) != 0)
                    {
                        textStyle |= TextStyle.Bold;
                    }
                    if ((a & 2) != 0)
                    {
                        textStyle |= TextStyle.Italic;
                    }
                    if ((a & 4) != 0)
                    {
                        textStyle |= TextStyle.Underline;
                    }
                    if ((a & 0xF8) != 0)
                    {
                        // should exit
                        //System.Diagnostics.Debug.WriteLine(string.Format(
                        //    "Text attribute bits {0:X2} is not recognized and is ignored.",
                        //    a & 0xF8));
                        throw new InvalidDataException("Invalid text attribute");
                    }
                }

                // Read chunk length to apply this attribute to.
                int charCount = reader.ReadByte();
                if (charCount > line.Length - charIndex)
                {
                    // TODO: issue warning
                    charCount = line.Length - charIndex;
                }
                if (textStyle != TextStyle.None)
                {
                    for (int j = 0; j < charCount; j++)
                    {
                        line.Attributes[charIndex + j] = new TextAttribute(textStyle, null);
                    }
                }
                charIndex += charCount;
            }
        }
コード例 #2
0
        internal static void DecodeLine(BufferReader reader, out HelpLine line)
        {
            line = null;

            // Read text length in bytes.
            int    textLength = reader.ReadByte();
            string text       = reader.ReadFixedLengthString(textLength - 1);

            line = new HelpLine(text);

            // Read byte count of attributes.
            int          attrLength = reader.ReadByte();
            BufferReader attrReader = reader.ReadBuffer(attrLength - 1);

            DecodeLineAttributes(line, attrReader);

            // Read hyperlinks.
            while (!attrReader.IsEOF)
            {
                DecodeLineHyperlink(line, attrReader);
            }
        }
コード例 #3
0
        private static void DecodeLineHyperlink(HelpLine line, BufferReader reader)
        {
            // Read link location.
            int linkStartIndex = reader.ReadByte(); // one-base, inclusive
            int linkEndIndex   = reader.ReadByte(); // one-base, inclusive

            if (linkStartIndex == 0 || linkStartIndex > linkEndIndex)
            {
                throw new InvalidDataException("Invalid link location.");
            }
            if (linkEndIndex > line.Length)
            {
                System.Diagnostics.Debug.WriteLine(string.Format(
                                                       "WARNING: Link end {0} is past line end {1}.",
                                                       linkEndIndex, line.Length));
                linkEndIndex = line.Length;
            }
            //if (linkStartIndex

            // Read NULL-terminated context string.
            string context = reader.ReadNullTerminatedString();

            if (context == "")                        // link is WORD topic index
            {
                int numContext = reader.ReadUInt16(); // 0x8000 | topicIndex
                context = "@L" + numContext.ToString("X4");
            }

            // Add hyperlink to the line.
            HelpUri link = new HelpUri(context);

            for (int j = linkStartIndex; j <= linkEndIndex; j++)
            {
                line.Attributes[j - 1] = new TextAttribute(line.Attributes[j - 1].Style, link);
            }
        }