コード例 #1
0
        private void ProcessLine(IDocumentWriter writer, string line)
        {
            if (string.IsNullOrWhiteSpace(line))
            {
                return;
            }

            if (line.StartsWith("."))
            {
                // This is a control statement. Process it appropriately.
                if (line.StartsWith(".bold"))
                {
                    writer.SetFontStyle(FontStyle.Bold);
                }
                else if (line.StartsWith(".italic"))
                {
                    writer.SetFontStyle(FontStyle.Italic);
                }
                else if (line.StartsWith(".regular"))
                {
                    writer.SetFontStyle(FontStyle.NoStyle);
                }
                else if (line.StartsWith(".large"))
                {
                    writer.SetFontSize(FontSize.Large);
                }
                else if (line.StartsWith(".normal"))
                {
                    writer.SetFontSize(FontSize.Normal);
                }
                else if (line.StartsWith(".fill"))
                {
                    writer.SetParagraphAlignment(ParagraphAlignment.Justified);
                }
                else if (line.StartsWith(".nofill"))
                {
                    writer.SetParagraphAlignment(ParagraphAlignment.LeftAlign);
                }
                else if (line.StartsWith(".paragraph"))
                {
                    writer.StartNewParagraph();
                }
                else if (line.StartsWith(".indent"))
                {
                    // Parse out the indent value.
                    var indentValue = line.Substring(7).Trim();
                    if (int.TryParse(indentValue, out var indent))
                    {
                        writer.ChangeIndent(indent);
                    }
                    else
                    {
                        throw new Exception($"Invalid indent value '{indentValue}'");
                    }
                }
                else
                {
                    throw new Exception($"Unrecognized control statement '{line}'");
                }
            }
            else
            {
                // This is just text - write it.
                writer.Write(line);
            }
        }