private void HandleText(RTF.RTF rtf) { string str = rtf.EncodedText; //todo - simplistically skips characters, should skip bytes? if (rtf_skip_count > 0 && str.Length > 0) { int iToRemove = Math.Min (rtf_skip_count, str.Length); str = str.Substring (iToRemove); rtf_skip_count-=iToRemove; } /* if ((RTF.StandardCharCode)rtf.Minor != RTF.StandardCharCode.nothing) { rtf_line.Append(rtf_text_map[(RTF.StandardCharCode)rtf.Minor]); } else { if ((int)rtf.Major > 31 && (int)rtf.Major < 128) { rtf_line.Append((char)rtf.Major); } else { //rtf_line.Append((char)rtf.Major); Console.Write("[Literal:0x{0:X2}]", (int)rtf.Major); } } */ if (rtf_style.rtf_visible) rtf_line.Append (str); }
private void FlushText(RTF.RTF rtf, bool newline) { int length; Font font; length = rtf_line.Length; if (!newline && (length == 0)) { return; } if (rtf_style.rtf_rtffont == null) { // First font in table is default rtf_style.rtf_rtffont = System.Windows.Forms.RTF.Font.GetFont (rtf, 0); } font = new Font (rtf_style.rtf_rtffont.Name, rtf_style.rtf_rtffont_size, rtf_style.rtf_rtfstyle); if (rtf_style.rtf_color == Color.Empty) { System.Windows.Forms.RTF.Color color; // First color in table is default color = System.Windows.Forms.RTF.Color.GetColor (rtf, 0); if ((color == null) || (color.Red == -1 && color.Green == -1 && color.Blue == -1)) { rtf_style.rtf_color = ForeColor; } else { rtf_style.rtf_color = Color.FromArgb (color.Red, color.Green, color.Blue); } } rtf_chars += rtf_line.Length; // Try to re-use if we are told so - this usually happens when we are inserting a flow of rtf text // with an already alive line. if (rtf_cursor_x == 0 && !reuse_line) { if (newline && rtf_line.ToString ().EndsWith (Environment.NewLine) == false) rtf_line.Append (Environment.NewLine); document.Add (rtf_cursor_y, rtf_line.ToString (), rtf_style.rtf_rtfalign, font, rtf_style.rtf_color, newline ? LineEnding.Rich : LineEnding.Wrap); if (rtf_style.rtf_par_line_left_indent != 0) { Line line = document.GetLine (rtf_cursor_y); line.indent = rtf_style.rtf_par_line_left_indent; } } else { Line line; line = document.GetLine (rtf_cursor_y); line.indent = rtf_style.rtf_par_line_left_indent; if (rtf_line.Length > 0) { document.InsertString (line, rtf_cursor_x, rtf_line.ToString ()); document.FormatText (line, rtf_cursor_x + 1, line, rtf_cursor_x + 1 + length, font, rtf_style.rtf_color, Color.Empty, FormatSpecified.Font | FormatSpecified.Color); } if (newline) { line = document.GetLine (rtf_cursor_y); line.ending = LineEnding.Rich; if (line.Text.EndsWith (Environment.NewLine) == false) line.Text += Environment.NewLine; } reuse_line = false; // sanity assignment - in this case we have already re-used one line. } if (newline) { rtf_cursor_x = 0; rtf_cursor_y++; } else { rtf_cursor_x += length; } rtf_line.Length = 0; // Empty line }
private void SpecialChar(RTF.RTF rtf) { switch(rtf.Minor) { case RTF.Minor.Page: case RTF.Minor.Sect: case RTF.Minor.Row: case RTF.Minor.Line: case RTF.Minor.Par: { FlushText(rtf, true); break; } case RTF.Minor.Cell: { Console.Write(" "); break; } case RTF.Minor.NoBrkSpace: { Console.Write(" "); break; } case RTF.Minor.Tab: { rtf_line.Append ("\t"); // FlushText (rtf, false); break; } case RTF.Minor.NoReqHyphen: case RTF.Minor.NoBrkHyphen: { rtf_line.Append ("-"); // FlushText (rtf, false); break; } case RTF.Minor.Bullet: { Console.WriteLine("*"); break; } case RTF.Minor.WidowCtrl: break; case RTF.Minor.EmDash: { rtf_line.Append ("\u2014"); break; } case RTF.Minor.EnDash: { rtf_line.Append ("\u2013"); break; } /* case RTF.Minor.LQuote: { Console.Write("\u2018"); break; } case RTF.Minor.RQuote: { Console.Write("\u2019"); break; } case RTF.Minor.LDblQuote: { Console.Write("\u201C"); break; } case RTF.Minor.RDblQuote: { Console.Write("\u201D"); break; } */ default: { // Console.WriteLine ("skipped special char: {0}", rtf.Minor); // rtf.SkipGroup(); break; } } }
private void HandleControl(RTF.RTF rtf) { switch(rtf.Major) { case RTF.Major.Unicode: { switch(rtf.Minor) { case RTF.Minor.UnicodeCharBytes: { rtf_style.rtf_skip_width = rtf.Param; break; } case RTF.Minor.UnicodeChar: { FlushText (rtf, false); rtf_skip_count += rtf_style.rtf_skip_width; rtf_line.Append((char)rtf.Param); break; } } break; } case RTF.Major.Destination: { // Console.Write("[Got Destination control {0}]", rtf.Minor); rtf.SkipGroup(); break; } case RTF.Major.PictAttr: if (rtf.Picture != null && rtf.Picture.IsValid ()) { Line line = document.GetLine (rtf_cursor_y); document.InsertPicture (line, 0, rtf.Picture); rtf_cursor_x++; FlushText (rtf, true); rtf.Picture = null; } break; case RTF.Major.CharAttr: { switch(rtf.Minor) { case RTF.Minor.ForeColor: { System.Windows.Forms.RTF.Color color; color = System.Windows.Forms.RTF.Color.GetColor(rtf, rtf.Param); if (color != null) { FlushText(rtf, false); if (color.Red == -1 && color.Green == -1 && color.Blue == -1) { this.rtf_style.rtf_color = ForeColor; } else { this.rtf_style.rtf_color = Color.FromArgb(color.Red, color.Green, color.Blue); } FlushText (rtf, false); } break; } case RTF.Minor.FontSize: { FlushText(rtf, false); this.rtf_style.rtf_rtffont_size = rtf.Param / 2; break; } case RTF.Minor.FontNum: { System.Windows.Forms.RTF.Font font; font = System.Windows.Forms.RTF.Font.GetFont(rtf, rtf.Param); if (font != null) { FlushText(rtf, false); this.rtf_style.rtf_rtffont = font; } break; } case RTF.Minor.Plain: { FlushText(rtf, false); rtf_style.rtf_rtfstyle = FontStyle.Regular; break; } case RTF.Minor.Bold: { FlushText(rtf, false); if (rtf.Param == RTF.RTF.NoParam) { rtf_style.rtf_rtfstyle |= FontStyle.Bold; } else { rtf_style.rtf_rtfstyle &= ~FontStyle.Bold; } break; } case RTF.Minor.Italic: { FlushText(rtf, false); if (rtf.Param == RTF.RTF.NoParam) { rtf_style.rtf_rtfstyle |= FontStyle.Italic; } else { rtf_style.rtf_rtfstyle &= ~FontStyle.Italic; } break; } case RTF.Minor.StrikeThru: { FlushText(rtf, false); if (rtf.Param == RTF.RTF.NoParam) { rtf_style.rtf_rtfstyle |= FontStyle.Strikeout; } else { rtf_style.rtf_rtfstyle &= ~FontStyle.Strikeout; } break; } case RTF.Minor.Underline: { FlushText(rtf, false); if (rtf.Param == RTF.RTF.NoParam) { rtf_style.rtf_rtfstyle |= FontStyle.Underline; } else { rtf_style.rtf_rtfstyle = rtf_style.rtf_rtfstyle & ~FontStyle.Underline; } break; } case RTF.Minor.Invisible: { FlushText (rtf, false); rtf_style.rtf_visible = false; break; } case RTF.Minor.NoUnderline: { FlushText(rtf, false); rtf_style.rtf_rtfstyle &= ~FontStyle.Underline; break; } } break; } case RTF.Major.ParAttr: { switch (rtf.Minor) { case RTF.Minor.ParDef: FlushText (rtf, false); rtf_style.rtf_par_line_left_indent = 0; rtf_style.rtf_rtfalign = HorizontalAlignment.Left; break; case RTF.Minor.LeftIndent: using (Graphics g = CreateGraphics ()) rtf_style.rtf_par_line_left_indent = (int) (((float) rtf.Param / 1440.0F) * g.DpiX + 0.5F); break; case RTF.Minor.QuadCenter: FlushText (rtf, false); rtf_style.rtf_rtfalign = HorizontalAlignment.Center; break; case RTF.Minor.QuadJust: FlushText (rtf, false); rtf_style.rtf_rtfalign = HorizontalAlignment.Center; break; case RTF.Minor.QuadLeft: FlushText (rtf, false); rtf_style.rtf_rtfalign = HorizontalAlignment.Left; break; case RTF.Minor.QuadRight: FlushText (rtf, false); rtf_style.rtf_rtfalign = HorizontalAlignment.Right; break; } break; } case RTF.Major.SpecialChar: { //Console.Write("[Got SpecialChar control {0}]", rtf.Minor); SpecialChar (rtf); break; } } }
// To allow us to keep track of the sections and revert formatting // as we go in and out of sections of the document. private void HandleGroup (RTF.RTF rtf) { //start group - save the current formatting on to a stack //end group - go back to the formatting at the current group if (rtf_section_stack == null) { rtf_section_stack = new Stack (); } if (rtf.Major == RTF.Major.BeginGroup) { rtf_section_stack.Push (rtf_style.Clone ()); //spec specifies resetting unicode ignore at begin group as an attempt at error //recovery. rtf_skip_count = 0; } else if (rtf.Major == RTF.Major.EndGroup) { if (rtf_section_stack.Count > 0) { FlushText (rtf, false); rtf_style = (RtfSectionStyle) rtf_section_stack.Pop (); } } }
internal PictureTag (Line line, int start, RTF.Picture picture) : base (line, start) { this.picture = picture; }
internal void InsertPicture (Line line, int pos, RTF.Picture picture) { //LineTag next_tag; LineTag tag; int len; len = 1; // Just a place holder basically line.text.Insert (pos, "I"); PictureTag picture_tag = new PictureTag (line, pos + 1, picture); tag = LineTag.FindTag (line, pos); picture_tag.CopyFormattingFrom (tag); /*next_tag = */tag.Break (pos + 1); picture_tag.Previous = tag; picture_tag.Next = tag.Next; tag.Next = picture_tag; // // Picture tags need to be surrounded by text tags // if (picture_tag.Next == null) { picture_tag.Next = new LineTag (line, pos + 1); picture_tag.Next.CopyFormattingFrom (tag); picture_tag.Next.Previous = picture_tag; } tag = picture_tag.Next; while (tag != null) { tag.Start += len; tag = tag.Next; } line.Grow (len); line.recalc = true; UpdateView (line, pos); }