#region MigraDoc - Creating Documents on the Fly // // Authors: // Stefan Lange // Klaus Potzesny // David Stephensen // // Copyright (c) 2001-2017 empira Software GmbH, Cologne Area (Germany) // // http://www.pdfsharp.com // http://www.migradoc.com // http://sourceforge.net/projects/pdfsharp // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #endregion using MigraDoc.DocumentObjectModel.publics; using MigraDoc.DocumentObjectModel.Visitors; using MigraDoc.DocumentObjectModel.Fields; using MigraDoc.DocumentObjectModel.Shapes; namespace MigraDoc.DocumentObjectModel { /// /// Represents the format of a text. /// public class FormattedText : DocumentObject, IVisitable { /// /// Initializes a new instance of the FormattedText class. /// public FormattedText() { } /// /// Initializes a new instance of the FormattedText class with the specified parent. /// public FormattedText(DocumentObject parent) : base(parent) { } #region Methods /// /// Creates a deep copy of this object. /// public new FormattedText Clone() { return (FormattedText)DeepCopy(); } /// /// Implements the deep copy of the object. /// protected override object DeepCopy() { FormattedText formattedText = (FormattedText)base.DeepCopy(); if (formattedText._font != null) { formattedText._font = formattedText._font.Clone(); formattedText._font._parent = formattedText; } if (formattedText._elements != null) { formattedText._elements = formattedText._elements.Clone(); formattedText._elements._parent = formattedText; } return formattedText; } /// /// Adds a new Bookmark. /// public BookmarkField AddBookmark(string name) { return Elements.AddBookmark(name); } /// /// Adds a single character repeated the specified number of times to the formatted text. /// public Text AddChar(char ch, int count) { return Elements.AddChar(ch, count); } /// /// Adds a single character to the formatted text. /// public Text AddChar(char ch) { return Elements.AddChar(ch); } /// /// Adds a new PageField. /// public PageField AddPageField() { return Elements.AddPageField(); } /// /// Adds a new PageRefField. /// public PageRefField AddPageRefField(string name) { return Elements.AddPageRefField(name); } /// /// Adds a new NumPagesField. /// public NumPagesField AddNumPagesField() { return Elements.AddNumPagesField(); } /// /// Adds a new SectionField. /// public SectionField AddSectionField() { return Elements.AddSectionField(); } /// /// Adds a new SectionPagesField. /// public SectionPagesField AddSectionPagesField() { return Elements.AddSectionPagesField(); } /// /// Adds a new DateField. /// public DateField AddDateField() { return Elements.AddDateField(); } /// /// Adds a new DateField. /// public DateField AddDateField(string format) { return Elements.AddDateField(format); } /// /// Adds a new InfoField. /// public InfoField AddInfoField(InfoFieldType iType) { return Elements.AddInfoField(iType); } /// /// Adds a new Footnote with the specified text. /// public Footnote AddFootnote(string text) { return Elements.AddFootnote(text); } /// /// Adds a new Footnote. /// public Footnote AddFootnote() { return Elements.AddFootnote(); } /// /// Adds a text phrase to the formatted text. /// /// Content of the new text object. /// Returns a new Text object. public Text AddText(string text) { return Elements.AddText(text); } /// /// Adds a new FormattedText. /// public FormattedText AddFormattedText() { return Elements.AddFormattedText(); } /// /// Adds a new FormattedText object with the given format. /// public FormattedText AddFormattedText(TextFormat textFormat) { return Elements.AddFormattedText(textFormat); } /// /// Adds a new FormattedText with the given Font. /// public FormattedText AddFormattedText(Font font) { return Elements.AddFormattedText(font); } /// /// Adds a new FormattedText with the given text. /// public FormattedText AddFormattedText(string text) { return Elements.AddFormattedText(text); } /// /// Adds a new FormattedText object with the given text and format. /// public FormattedText AddFormattedText(string text, TextFormat textFormat) { return Elements.AddFormattedText(text, textFormat); } /// /// Adds a new FormattedText object with the given text and font. /// public FormattedText AddFormattedText(string text, Font font) { return Elements.AddFormattedText(text, font); } /// /// Adds a new FormattedText object with the given text and style. /// public FormattedText AddFormattedText(string text, string style) { return Elements.AddFormattedText(text, style); } /// /// Adds a new Hyperlink of Type "Local", /// i.e. the target is a Bookmark within the Document /// public Hyperlink AddHyperlink(string name) { return Elements.AddHyperlink(name); } /// /// Adds a new Hyperlink /// public Hyperlink AddHyperlink(string name, HyperlinkType type) { return Elements.AddHyperlink(name, type); } /// /// Adds a new Image object /// public Image AddImage(string fileName) { return Elements.AddImage(fileName); } /// /// Adds a Symbol object. /// public Character AddCharacter(SymbolName symbolType) { return Elements.AddCharacter(symbolType); } /// /// Adds one or more Symbol objects. /// public Character AddCharacter(SymbolName symbolType, int count) { return Elements.AddCharacter(symbolType, count); } /// /// Adds a Symbol object defined by a character. /// public Character AddCharacter(char ch) { return Elements.AddCharacter(ch); } /// /// Adds one or more Symbol objects defined by a character. /// public Character AddCharacter(char ch, int count) { return Elements.AddCharacter(ch, count); } /// /// Adds one or more Symbol objects defined by a character. /// public Character AddSpace(int count) { return Elements.AddSpace(count); } /// /// Adds a horizontal tab. /// public void AddTab() { Elements.AddTab(); } /// /// Adds a line break. /// public void AddLineBreak() { Elements.AddLineBreak(); } /// /// Adds a new Bookmark /// public void Add(BookmarkField bookmark) { Elements.Add(bookmark); } /// /// Adds a new PageField /// public void Add(PageField pageField) { Elements.Add(pageField); } /// /// Adds a new PageRefField /// public void Add(PageRefField pageRefField) { Elements.Add(pageRefField); } /// /// Adds a new NumPagesField /// public void Add(NumPagesField numPagesField) { Elements.Add(numPagesField); } /// /// Adds a new SectionField /// public void Add(SectionField sectionField) { Elements.Add(sectionField); } /// /// Adds a new SectionPagesField /// public void Add(SectionPagesField sectionPagesField) { Elements.Add(sectionPagesField); } /// /// Adds a new DateField /// public void Add(DateField dateField) { Elements.Add(dateField); } /// /// Adds a new InfoField /// public void Add(InfoField infoField) { Elements.Add(infoField); } /// /// Adds a new Footnote /// public void Add(Footnote footnote) { Elements.Add(footnote); } /// /// Adds a new Text /// public void Add(Text text) { Elements.Add(text); } /// /// Adds a new FormattedText /// public void Add(FormattedText formattedText) { Elements.Add(formattedText); } /// /// Adds a new Hyperlink /// public void Add(Hyperlink hyperlink) { Elements.Add(hyperlink); } /// /// Adds a new Image /// public void Add(Image image) { Elements.Add(image); } /// /// Adds a new Character /// public void Add(Character character) { Elements.Add(character); } #endregion #region Properties /// /// Gets or sets the font object. /// public Font Font { get { return _font ?? (_font = new Font(this)); } set { SetParent(value); _font = value; } } [DV] public Font _font; /// /// Gets or sets the style name. /// public string Style { get { return _style.Value; } set { _style.Value = value; } } [DV] public NString _style = NString.NullValue; /// /// Gets or sets the name of the font. /// [DV] public string FontName { get { return Font.Name; } set { Font.Name = value; } } /// /// Gets or sets the name of the font. /// For public use only. /// [DV] public string Name { get { return Font.Name; } set { Font.Name = value; } } /// /// Gets or sets the size in point. /// [DV] public Unit Size { get { return Font.Size; } set { Font.Size = value; } } /// /// Gets or sets the bold property. /// [DV] public bool Bold { get { return Font.Bold; } set { Font.Bold = value; } } /// /// Gets or sets the italic property. /// [DV] public bool Italic { get { return Font.Italic; } set { Font.Italic = value; } } /// /// Gets or sets the underline property. /// [DV] public Underline Underline { get { return Font.Underline; } set { Font.Underline = value; } } /// /// Gets or sets the color property. /// [DV] public Color Color { get { return Font.Color; } set { Font.Color = value; } } /// /// Gets or sets the superscript property. /// [DV] public bool Superscript { get { return Font.Superscript; } set { Font.Superscript = value; } } /// /// Gets or sets the subscript property. /// [DV] public bool Subscript { get { return Font.Subscript; } set { Font.Subscript = value; } } /// /// Gets the collection of paragraph elements that defines the FormattedText. /// public ParagraphElements Elements { get { return _elements ?? (_elements = new ParagraphElements(this)); } set { SetParent(value); _elements = value; } } [DV(ItemType = typeof(DocumentObject))] public ParagraphElements _elements; #endregion #region public /// /// Converts FormattedText into DDL. /// public override void Serialize(Serializer serializer) { bool isFormatted = false; if (!IsNull("Font")) { Font.Serialize(serializer); isFormatted = true; } else { if (!_style.IsNull) { serializer.Write("\\font(\"" + Style + "\")"); isFormatted = true; } } if (isFormatted) serializer.Write("{"); if (!IsNull("Elements")) Elements.Serialize(serializer); if (isFormatted) serializer.Write("}"); } /// /// Allows the visitor object to visit the document object and its child objects. /// void IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, bool visitChildren) { visitor.VisitFormattedText(this); if (visitChildren && _elements != null) ((IVisitable)_elements).AcceptVisitor(visitor, true); } /// /// Returns the meta object of this instance. /// public override Meta Meta { get { return _meta ?? (_meta = new Meta(typeof(FormattedText))); } } static Meta _meta; #endregion } }