private BBTextChunk SetChunkText(int tokenLine, BBTextChunk chunk, string text) { float fntSize; ExtFontStyle fntStyle; if (chunk != null) { fntSize = chunk.Size; fntStyle = chunk.Style; } else { fntSize = fDefaultFontSize; fntStyle = ExtFontStyle.None; } if (chunk == null) { chunk = new BBTextChunk(tokenLine, fntSize, fntStyle, fTextColor); fChunks.Add(chunk); } chunk.Text += text; return(chunk); }
private BBTextChunk SetChunkFontStyle(int tokenLine, BBTextChunk chunk, ExtFontStyle style, bool active) { float fntSize; ExtFontStyle fntStyle; if (chunk != null) { fntSize = chunk.Size; fntStyle = chunk.Style; } else { fntSize = fDefaultFontSize; fntStyle = ExtFontStyle.None; } if (active) { fntStyle |= style; } else { fntStyle &= ~style; } if (chunk == null || chunk.Text.Length != 0) { chunk = new BBTextChunk(tokenLine, fntSize, fntStyle, fTextColor); fChunks.Add(chunk); } chunk.Style = fntStyle; return(chunk); }
private BBTextChunk SetChunkColor(int tokenLine, BBTextChunk chunk, IColor color) { float fntSize; ExtFontStyle fntStyle; if (chunk != null) { fntSize = chunk.Size; fntStyle = chunk.Style; } else { fntSize = fDefaultFontSize; fntStyle = ExtFontStyle.None; } if (chunk == null || chunk.Text.Length != 0) { chunk = new BBTextChunk(tokenLine, fntSize, fntStyle, color); fChunks.Add(chunk); } chunk.Color = color; return(chunk); }
private BBTextChunk SetChunkFontSize(int tokenLine, BBTextChunk chunk, float newSize) { ExtFontStyle fntStyle = (chunk != null) ? chunk.Style : ExtFontStyle.None; if (chunk == null || chunk.Text.Length != 0) { chunk = new BBTextChunk(tokenLine, newSize, fntStyle, fTextColor); fChunks.Add(chunk); } chunk.Size = newSize; return(chunk); }
public void ParseText(List <BBTextChunk> chunksList, string text) { fChunks = chunksList; fChunks.Clear(); float lastFontSize = fDefaultFontSize; BBTextChunk lastChunk = null; Stack <SizeChange> stackSizes = new Stack <SizeChange>(); //lastChunk = SetChunkFontSize(0, lastChunk, fDefaultFontSize); if (string.IsNullOrEmpty(text)) { text = EMPTY_CHUNK; lastChunk = SetChunkText(0, lastChunk, text); return; } StringTokenizer strTok = new StringTokenizer(text); strTok.IgnoreWhiteSpace = false; strTok.RecognizeDecimals = false; Token tok = strTok.Next(); while (tok.Kind != TokenKind.EOF) { if (tok.Kind == TokenKind.Symbol && tok.Value == "[") { string temp = tok.Value; tok = strTok.Next(); bool closedTag; if (tok.Kind == TokenKind.Symbol && tok.Value == "/") { closedTag = true; temp += tok.Value; tok = strTok.Next(); } else { closedTag = false; } if (tok.Kind != TokenKind.Word) { // not tag lastChunk = SetChunkText(tok.Line, lastChunk, temp + tok.Value); } else { string tag = tok.Value; //bool skipTag = false; if (tag == "color") { // [color="{red|#ff0000}"][/color] IColor color = fTextColor; if (!closedTag) { tok = strTok.Next(); if (tok.Kind == TokenKind.Symbol && tok.Value == "=") { tok = strTok.Next(); if (tok.Kind == TokenKind.Word) { color = fGfxProvider.CreateColor(tok.Value); lastChunk = SetChunkColor(tok.Line, lastChunk, color); } } } else { // TODO: colorStack color = fTextColor; lastChunk = SetChunkColor(tok.Line, lastChunk, color); } } else if (tag == "size") { // [size={+/-x}][/size] if (!closedTag) { tok = strTok.Next(); if (tok.Kind == TokenKind.Symbol && tok.Value == "=") { tok = strTok.Next(); int factor = 0; if (tok.Kind == TokenKind.Symbol) { if (tok.Value == "+") { factor = +1; } else if (tok.Value == "-") { factor = -1; } tok = strTok.Next(); } if (tok.Kind == TokenKind.Number) { float newSize = lastFontSize + factor * SysUtils.ParseInt(tok.Value, 0); stackSizes.Push(new SizeChange(lastFontSize, newSize)); lastChunk = SetChunkFontSize(tok.Line, lastChunk, newSize); lastFontSize = newSize; } } } else { if (stackSizes.Count > 0) { SizeChange sizeChange = stackSizes.Pop(); lastChunk = SetChunkFontSize(tok.Line, lastChunk, sizeChange.PrevSize); lastFontSize = sizeChange.PrevSize; } } } else if (tag == "b") { // [b][/b] lastChunk = SetChunkFontStyle(tok.Line, lastChunk, ExtFontStyle.Bold, !closedTag); } else if (tag == "i") { // [i][/i] lastChunk = SetChunkFontStyle(tok.Line, lastChunk, ExtFontStyle.Italic, !closedTag); } else if (tag == "s") { // [s][/s] lastChunk = SetChunkFontStyle(tok.Line, lastChunk, ExtFontStyle.Strikeout, !closedTag); } else if (tag == "u") { // [u][/u] lastChunk = SetChunkFontStyle(tok.Line, lastChunk, ExtFontStyle.Underline, !closedTag); } else if (tag == "url") { // bad impementation // [url][/url] and [url=...][/url], but now only [url=...][/url] string url = ""; tok = strTok.Next(); if (tok.Kind == TokenKind.Symbol && tok.Value == "=") { tok = strTok.Next(); do { url += tok.Value; tok = strTok.Next(); } while (tok.Kind != TokenKind.Symbol || tok.Value != "]"); } else { // } lastChunk = SetChunkFontStyle(tok.Line, lastChunk, ExtFontStyle.Underline, !closedTag); IColor color = (closedTag) ? fTextColor : fLinkColor; lastChunk = SetChunkColor(tok.Line, lastChunk, color); if (!closedTag) { lastChunk.URL = url; } } else { // not tag lastChunk = SetChunkText(tok.Line, lastChunk, temp + tok.Value); } if (tok.Kind != TokenKind.Symbol || tok.Value != "]") { // Possible syntax error? strTok.Next(); } } } else if (tok.Kind == TokenKind.EOL) { lastChunk = SetChunkText(tok.Line, null, EMPTY_CHUNK); lastChunk = null; } else { lastChunk = SetChunkText(tok.Line, lastChunk, tok.Value); } tok = strTok.Next(); } // eof lastChunk = SetChunkText(tok.Line, null, EMPTY_CHUNK); }