Example #1
0
 public virtual TextFile.Token[] GetPageTokens(int page)
 {
     if (IsBookOpen)
     {
         return(bookFile.GetPageTokens(page));
     }
     else
     {
         return(null);
     }
 }
Example #2
0
        public bool ReformatBook(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                return(false);
            }

            // Try to open book
            BookFile book = new BookFile();

            if (!BookReplacement.TryImportBook(filename, book) &&
                !book.OpenBook(DaggerfallUnity.Instance.Arena2Path, filename))
            {
                return(false);
            }

            // Clear existing
            Clear();

            // Combine all tokens from all pages
            // The classic concept of pages becomes
            List <TextFile.Token> allPageTokens = new List <TextFile.Token>();

            for (int page = 0; page < book.PageCount; page++)
            {
                TextFile.Token[] tokens = book.GetPageTokens(page);
                foreach (TextFile.Token token in tokens)
                {
                    allPageTokens.Add(token);
                }
            }

            // Read all tokens and merge into text groups
            DaggerfallFont prevFont = DaggerfallUI.DefaultFont;

            TextFile.Token prevToken    = new TextFile.Token(TextFile.Formatting.Nothing);
            TextGroup      workingGroup = CreateEmptyTextGroup(prevFont);

            foreach (TextFile.Token token in allPageTokens)
            {
                switch (token.formatting)
                {
                // Set font on current group
                case TextFile.Formatting.FontPrefix:
                    workingGroup.font = prevFont = DaggerfallUI.Instance.GetFont(token.x);
                    break;

                // Text is added to working group
                case TextFile.Formatting.Text:
                    workingGroup.text += token.text;
                    break;

                // Newline becomes a space unless previous token was also newline
                // This will be treated as a paragraph break
                case TextFile.Formatting.NewLine:
                    if (prevToken.formatting == TextFile.Formatting.NewLine)
                    {
                        StoreGroup(workingGroup);
                        StoreLineBreakGroup();
                        workingGroup = CreateEmptyTextGroup(prevFont);
                    }
                    else
                    {
                        workingGroup.text += space;
                    }
                    break;

                // Set left justify on current group
                case TextFile.Formatting.JustifyLeft:
                    workingGroup.alignment = HorizontalAlignment.None;
                    break;

                // Set centre justify on current group
                case TextFile.Formatting.JustifyCenter:
                    workingGroup.alignment = HorizontalAlignment.Center;
                    break;
                }

                prevToken = token;
            }

            return(true);
        }