private void ClearWordBuffer(List <SMWordBase> list, StringBuilder word, RunningFormat fmt) { if (word.Length > 0) { AppendWord(list, word.ToString(), fmt); word.Clear(); } }
private void AppendWord(List <SMWordBase> list, string text, RunningFormat fmt) { // TODO SMWordText wt = new SMWordText(Font); wt.text = text; wt.tag = text.Trim(); wt.Draggable = fmt.dragResponse; wt.Evaluation = MNEvaluationType.None; wt.Font = fmt.GetFont(); wt.lineOffset = Convert.ToInt32(-55 * fmt.lineOffset); list.Add(wt); }
/// <summary> /// Text is normal text and contains words, spaces and tags /// tags are like HTML tags /// examples are given bellow /// </summary> /// <param name="text"></param> /// <returns></returns> // examples of tags: // <tag> // <tag attr1=val1> // <tag attr2="val1" attr3='val3' attr4=val5> public List <SMWordBase> WordListFromString(string text) { List <SMWordBase> list = new List <SMWordBase>(); TextParseMode mode = TextParseMode.General; StringBuilder word = new StringBuilder(); StringBuilder sb = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); RunningFormat fmt = new RunningFormat(); TextTag tt = new TextTag(); string argumentName = ""; fmt.SetFontStyle(Font.Style); fmt.fontSize = Font.Size; fmt.fontName = Font.Name; fmt.defaultFontSize = fmt.fontSize; foreach (char readedChar in text) { if (mode == TextParseMode.General) { if (readedChar == '&') { mode = TextParseMode.SpecChar; sb.Clear(); continue; } else if (readedChar == '<') { mode = TextParseMode.WaitForTagNameStart; sb.Clear(); continue; } else if (Char.IsWhiteSpace(readedChar)) { ClearWordBuffer(list, word, fmt); if (readedChar == '\n') { list.Add(new SMWordSpecial(Font) { Type = SMWordSpecialType.Newline }); } else if (readedChar == '\r') { } else { AppendWord(list, " ", fmt); } } else { word.Append(readedChar); } } else if (mode == TextParseMode.SpecChar) { if (readedChar == ';') { word.Append(GetCharFromCode(sb.ToString())); mode = TextParseMode.General; } else { sb.Append(readedChar); } } else if (mode == TextParseMode.WaitForTagNameStart) { if (readedChar == '>') { mode = TextParseMode.General; } else if (!Char.IsWhiteSpace(readedChar)) { sb.Append(readedChar); mode = TextParseMode.ReadTagName; } } else if (mode == TextParseMode.ReadTagName) { if (Char.IsWhiteSpace(readedChar)) { tt.tag = sb.ToString(); sb.Clear(); mode = TextParseMode.WaitForArgOrEnd; } else if (readedChar == '>') { tt.tag = sb.ToString(); mode = TextParseMode.General; AppendTag(list, word, tt, fmt); tt.Clear(); } else { sb.Append(readedChar); } } else if (mode == TextParseMode.WaitForArgOrEnd) { if (readedChar == '>') { mode = TextParseMode.General; AppendTag(list, word, tt, fmt); tt.Clear(); } else if (Char.IsWhiteSpace(readedChar)) { } else { sb.Clear(); sb.Append(readedChar); mode = TextParseMode.ReadArgName; } } else if (mode == TextParseMode.ReadArgName) { if (readedChar == '>') { mode = TextParseMode.General; if (sb.Length > 0) { tt.attrs.Add(sb.ToString(), string.Empty); AppendTag(list, word, tt, fmt); tt.Clear(); } } else if (readedChar == '=') { argumentName = sb.ToString(); sb.Clear(); mode = TextParseMode.WaitForArgValue; } else if (Char.IsWhiteSpace(readedChar)) { argumentName = sb.ToString(); sb.Clear(); mode = TextParseMode.WaitForAssignOrEnd; } else { sb.Append(readedChar); } } else if (mode == TextParseMode.WaitForAssignOrEnd) { if (readedChar == '=') { mode = TextParseMode.WaitForArgValue; } else if (readedChar == '>') { mode = TextParseMode.General; if (argumentName.Length > 0) { tt.attrs.Add(argumentName, string.Empty); AppendTag(list, word, tt, fmt); tt.Clear(); } } else if (!Char.IsWhiteSpace(readedChar)) { mode = TextParseMode.ReadArgName; if (argumentName.Length > 0) { tt.attrs.Add(argumentName, string.Empty); } sb.Clear(); sb.Append(readedChar); } } else if (mode == TextParseMode.WaitForArgValue) { if (readedChar == '>') { mode = TextParseMode.General; if (argumentName.Length > 0) { tt.attrs.Add(argumentName, string.Empty); AppendTag(list, word, tt, fmt); tt.Clear(); } } else if (readedChar == '\"') { sb.Clear(); mode = TextParseMode.ReadArgValueString; } else if (readedChar == '\'') { sb.Clear(); mode = TextParseMode.ReadArgValueQuote; } else if (!Char.IsWhiteSpace(readedChar)) { sb.Clear(); sb.Append(readedChar); mode = TextParseMode.ReadArgValue; } } else if (mode == TextParseMode.ReadArgValue) { if (readedChar == '>') { mode = TextParseMode.General; if (argumentName.Length > 0) { tt.attrs.Add(argumentName, sb.ToString()); AppendTag(list, word, tt, fmt); tt.Clear(); } } else if (readedChar == '&') { sb2.Clear(); mode = TextParseMode.ReadArgValueSpecChar; } else if (Char.IsWhiteSpace(readedChar)) { mode = TextParseMode.WaitForArgOrEnd; if (argumentName.Length > 0) { tt.attrs.Add(argumentName, sb.ToString()); sb.Clear(); argumentName = ""; } } else { sb.Append(readedChar); } } else if (mode == TextParseMode.ReadArgValueSpecChar) { if (readedChar == ';') { sb.Append(GetCharFromCode(sb2.ToString())); mode = TextParseMode.ReadArgValue; } else { sb2.Append(readedChar); } } else if (mode == TextParseMode.ReadArgValueString) { if (readedChar == '&') { sb2.Clear(); mode = TextParseMode.ReadArgValueStringSpecChar; } else if (readedChar == '\"') { mode = TextParseMode.WaitForArgOrEnd; if (argumentName.Length > 0) { tt.attrs.Add(argumentName, sb.ToString()); sb.Clear(); argumentName = ""; } } else { sb.Append(readedChar); } } else if (mode == TextParseMode.ReadArgValueStringSpecChar) { if (readedChar == ';') { sb.Append(GetCharFromCode(sb2.ToString())); mode = TextParseMode.ReadArgValueString; } else { sb2.Append(readedChar); } } else if (mode == TextParseMode.ReadArgValueQuote) { if (readedChar == '&') { sb2.Clear(); mode = TextParseMode.ReadArgValueQuoteSpecChar; } else if (readedChar == '\"') { mode = TextParseMode.WaitForArgOrEnd; if (argumentName.Length > 0) { tt.attrs.Add(argumentName, sb.ToString()); sb.Clear(); argumentName = ""; } } else { sb.Append(readedChar); } } else if (mode == TextParseMode.ReadArgValueQuoteSpecChar) { if (readedChar == ';') { sb.Append(GetCharFromCode(sb2.ToString())); mode = TextParseMode.ReadArgValueQuote; } else { sb2.Append(readedChar); } } } // finalization if (word.Length > 0) { AppendWord(list, word.ToString(), fmt); word.Clear(); } // set first editable as focused foreach (SMWordBase wb in list) { if (wb is SMWordToken) { SMWordToken wt = (SMWordToken)wb; if (wt.Editable) { wt.Focused = true; break; } } } return(list); }
private void AppendTag(List <SMWordBase> list, StringBuilder word, TextTag tt, RunningFormat fmt) { // TODO switch (tt.tag) { case "draggable": fmt.dragResponse = SMDragResponse.Drag; break; case "/draggable": ClearWordBuffer(list, word, fmt); fmt.dragResponse = SMDragResponse.None; break; case "drop": { SMWordToken wt = new SMWordToken(Font); wt.text = tt.attrs.ContainsKey("text") ? tt.attrs["text"] : "_____"; wt.tag = tt.attrs.ContainsKey("tag") ? tt.attrs["tag"] : ""; wt.Draggable = SMDragResponse.None; wt.Cardinality = SMConnectionCardinality.One; wt.Evaluation = EvaluationType; list.Add(wt); } break; case "edit": { SMWordToken wt = new SMWordToken(Font); wt.text = tt.attrs.ContainsKey("text") ? tt.attrs["text"] : "_____"; wt.tag = tt.attrs.ContainsKey("tag") ? tt.attrs["tag"] : ""; wt.Draggable = SMDragResponse.None; wt.Editable = true; wt.Cardinality = SMConnectionCardinality.One; wt.Evaluation = EvaluationType; list.Add(wt); } break; case "page": ClearWordBuffer(list, word, fmt); list.Add(new SMWordSpecial(Font) { Type = SMWordSpecialType.NewPage }); break; case "hr": ClearWordBuffer(list, word, fmt); list.Add(new SMWordSpecial(Font) { Type = SMWordSpecialType.HorizontalLine }); break; case "br": ClearWordBuffer(list, word, fmt); list.Add(new SMWordSpecial(Font) { Type = SMWordSpecialType.Newline }); AppendWord(list, "\n", fmt); break; case "col": ClearWordBuffer(list, word, fmt); list.Add(new SMWordSpecial(Font) { Type = SMWordSpecialType.NewColumn }); //AppendWord(list, "\n", control, fmt); break; case "r": ClearWordBuffer(list, word, fmt); fmt.Bold = false; fmt.Italic = false; fmt.Strikeout = false; fmt.Underline = false; //fmt.fontStyleValid = (fmt.fontStyle != control.Style.Font.Style); break; case "/r": ClearWordBuffer(list, word, fmt); fmt.Bold = Font.Bold; fmt.Italic = Font.Italic; fmt.Underline = Font.Underline; fmt.Strikeout = false; //fmt.fontStyleValid = (fmt.fontStyle != control.Style.Font.Style); break; case "b": ClearWordBuffer(list, word, fmt); fmt.Bold = true; //fmt.fontStyleValid = (fmt.fontStyle != control.Style.Font.Style); break; case "/b": ClearWordBuffer(list, word, fmt); fmt.Bold = false; //fmt.fontStyleValid = (fmt.fontStyle != control.Style.Font.Style); break; case "i": ClearWordBuffer(list, word, fmt); fmt.Italic = true; //fmt.fontStyleValid = (fmt.fontStyle != control.Style.Font.Style); break; case "/i": ClearWordBuffer(list, word, fmt); fmt.Italic = false; //fmt.fontStyleValid = (fmt.fontStyle != control.Style.Font.Style); break; case "u": ClearWordBuffer(list, word, fmt); fmt.Underline = true; //fmt.fontStyleValid = (fmt.fontStyle != control.Style.Font.Style); break; case "/u": ClearWordBuffer(list, word, fmt); fmt.Underline = false; //fmt.fontStyleValid = (fmt.fontStyle != control.Style.Font.Style); break; case "so": ClearWordBuffer(list, word, fmt); fmt.Strikeout = true; //fmt.fontStyleValid = (fmt.fontStyle != control.Style.Font.Style); break; case "/so": ClearWordBuffer(list, word, fmt); fmt.Strikeout = false; //fmt.fontStyleValid = (fmt.fontStyle != control.Style.Font.Style); break; default: if (tt.tag.StartsWith("fs")) { int arg = 0; if (int.TryParse(tt.tag.Substring(2), out arg)) { fmt.fontSize = (float)arg / 100 * fmt.defaultFontSize; fmt.lineOffset = (float)arg / 100 - 1f; } } break; } }
private static void AppendWord(List <SMWordBase> list, string text, SMControl control, RunningFormat fmt) { // TODO SMWordText wt = new SMWordText(control.Font); wt.text = text; wt.tag = text.Trim(); wt.Draggable = fmt.dragResponse; wt.Evaluation = MNEvaluationType.None; wt.Font = fmt.GetFont(); wt.replacementTarget = fmt.selectForReplacementTarget; list.Add(wt); }