private Dictionary<int, List<HTMLTag>> m_Words = new Dictionary<int, List<HTMLTag>>(); // see GetWordIndex #endregion Fields #region Constructors public HTMLContent( int pages, int maxLines, HTMLBook book ) { m_Book = book; m_CachedHTMLContent = new BookPageInfo[pages]; for ( int i = 0; i < m_CachedHTMLContent.Length; i++ ) { m_CachedHTMLContent[i] = new BookPageInfo(); m_CachedHTMLContent[i].Lines = new string[maxLines]; } // set default body styling m_Body = new List<HTMLTag>(); HTMLTag tag = new ColorTag(); tag.Value = "111111"; // #000000 (black) is invisible m_Body.Add( tag ); tag = new LeftAlignTag(); m_Body.Add( tag ); tag = new MediumFontTag(); m_Body.Add( tag ); }
public int CalculateCharacterCost( int page, int line, int word ) { // Returns Character Cost for a specific word, taking into account all html levels int cost = 0; HTMLTag fontTag = new MediumFontTag(); HTMLTag boldTag = null; HTMLTag italicTag = null; // We have to loop through each of these since we don't know where they might be overridden foreach ( HTMLTag tag in Body ) { if ( tag.Type == TagType.Font ) fontTag = tag; else if ( tag.Type == TagType.Bold ) boldTag = tag; else if ( tag.Type == TagType.Italic ) italicTag = tag; } foreach ( HTMLTag tag in GetPageTags( page ) ) { if ( tag.Type == TagType.Font ) fontTag = tag; else if ( tag.Type == TagType.Bold ) boldTag = tag; else if ( tag.Type == TagType.Italic ) italicTag = tag; } foreach ( HTMLTag tag in GetLineTags( page, line ) ) { if ( tag.Type == TagType.Font ) fontTag = tag; else if ( tag.Type == TagType.Bold ) boldTag = tag; else if ( tag.Type == TagType.Italic ) italicTag = tag; } foreach ( HTMLTag tag in GetWordTags( page, line, word ) ) { if ( tag.Type == TagType.Font ) fontTag = tag; else if ( tag.Type == TagType.Bold ) boldTag = tag; else if ( tag.Type == TagType.Italic ) italicTag = tag; } if ( fontTag != null ) cost += fontTag.CharacterCost; if ( boldTag != null ) cost += boldTag.CharacterCost; if ( italicTag != null ) cost += italicTag.CharacterCost; return cost; }
public virtual void FixContent() { string content = ""; StringBuilder sb; for( int i = 0; i < Pages.Length; i++ ) { for ( int k = 0; k < Pages[i].Lines.Length; k++ ) { if ( Pages[i].Lines[k].Length < 1 ) continue; sb = null; // make sure html tags are surrounded by spaces, but don't remove illegal html tags here, we'll do that later if ( Pages[i].Lines[k].IndexOf( '<' ) != -1 ) { sb = new StringBuilder(Pages[i].Lines[k]); for ( int q = 1; q < sb.Length; q++ ) { if ( sb[q] == '<' && sb[q-1] != ' ' ) sb.Insert( q, ' ' ); else if ( sb[q-1] == '>' && sb[q] != ' ' ) sb.Insert( q, ' ' ); } } if ( sb != null ) content += sb.ToString(); else content += Pages[i].Lines[k]; if ( content[content.Length-1] != ' ' ) content += ' '; } } string[] pieces = content.Split( new char[] { ' ' } ); int page = 0; int j = 0; m_FormattedBookContent = new BookPageInfo[PagesCount]; for ( int i = 0; i < m_FormattedBookContent.Length; i++ ) m_FormattedBookContent[i] = new BookPageInfo(); m_FormattedBookContent[page].Lines = new string[MaxLines]; for ( int i = 0; i < MaxLines; i++ ) m_FormattedBookContent[page].Lines[i] = ""; bool skipLine = false; int lineFillFactor = 0; int textCost = new MediumFontTag().CharacterCost; foreach( string word in pieces ) { if ( word.IndexOf( '<' ) != -1 ) { if ( String.Compare( word, "<BR>", true ) != 0 ) // ignore case continue; // do not want else { m_FormattedBookContent[page].Lines[j] += word; skipLine = true; // avoids text screwups } } if ( lineFillFactor + word.Length + 1 + textCost > CharactersPerLineMax || skipLine ) // count the space towards limit { j++; lineFillFactor = 0; if ( j >= MaxLines ) { j = 0; page++; if ( page >= m_FormattedBookContent.Length ) { // no more space in book return; } m_FormattedBookContent[page].Lines = new string[MaxLines]; for ( int i = 0; i < MaxLines; i++ ) m_FormattedBookContent[page].Lines[i] = ""; } } if ( !skipLine ) { m_FormattedBookContent[page].Lines[j] += word; m_FormattedBookContent[page].Lines[j] += ' '; lineFillFactor += 1 + textCost + word.Length; } else skipLine = false; } }