PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index PDFsharp - moved to http://forum.pdfsharp.net/
Please visit the new PDFsharp forum at http://forum.pdfsharp.net/
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to allow shading to a FormattedText...

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/
View previous topic :: View next topic  
Author Message
ldelabre



Joined: 12 Apr 2008
Posts: 10

PostPosted: Sat Jun 14, 2008 4:05 pm    Post subject: How to allow shading to a FormattedText... Reply with quote

Hi !
I needed to shade a word inside a paragraph, so I've made some changes...
Felt like sharing Smile

First things first, I added a Shading property to the FormattedText class (MigraDocLite\MigraDoc.DocumentObjectModel\MigraDoc.DocumentObjectModel\FormattedText.cs) :

Code:

    /// <summary>
    /// Gets the shading object.
    /// </summary>
    public Shading Shading
    {
        get
        {
            if (this.shading == null)
                this.shading = new Shading(this);

            return this.shading;
        }
        set
        {
            SetParent(value);
            this.shading = value;
        }
    }
    [DV]
    internal Shading shading;


Then I've adapted the DeepCopy() method :

Code:

    /// <summary>
    /// Implements the deep copy of the object.
    /// </summary>
    protected override object DeepCopy()
    {
      FormattedText formattedText = (FormattedText)base.DeepCopy();
      if (formattedText.font != null)
      {
        formattedText.font = formattedText.font.Clone();
        formattedText.font.parent = formattedText;
      }
      if (formattedText.shading != null)
      {
          formattedText.shading = formattedText.shading.Clone();
          formattedText.shading.parent = formattedText;
      }
      if (formattedText.elements != null)
      {
        formattedText.elements = formattedText.elements.Clone();
        formattedText.elements.parent = formattedText;
      }
      return formattedText;
    }


And the Serialize() one as well (I admit, I'm quite not sure of what I've done in this one Embarassed ) :

Code:

    /// <summary>
    /// Converts FormattedText into DDL.
    /// </summary>
    internal override void Serialize(Serializer serializer)
    {
      bool isFormatted = false;
      if (!this.IsNull("Font"))
      {
        this.Font.Serialize(serializer);
        isFormatted = true;
      }
      else
      {
        if (!this.style.IsNull)
        {
          serializer.Write("\\font(\"" + this.Style + "\")");
          isFormatted = true;
        }
      }

      if (!this.IsNull("Shading"))
          this.shading.Serialize(serializer);

      if (isFormatted)
        serializer.Write("{");

      if (!this.IsNull("Elements"))
        this.Elements.Serialize(serializer);

      if (isFormatted)
        serializer.Write("}");
    }


Now that FormattedText supports shading.. we need to render it Wink

Next changes are in the ParagraphRender class (MigraDocLite\MigraDoc.Rendering\MigraDoc.Rendering\ParagraphRenderer.cs) :

First, a GetShading() method based on GetHyperlink() (lazy me Embarassed ) :

Code:

    Shading GetShading()
    {
        DocumentObject parent = DocumentRelations.GetParent(this.currentLeaf.Current);
        parent = DocumentRelations.GetParent(parent);
        if (parent is FormattedText)
        {
            FormattedText text;

            text = ((FormattedText)parent);
            if (text.IsNull("Shading"))
                return null;
            else
                return text.Shading;
        }
        else
            return null;
    }


Next is a new RealizeShading() method (there's already a RenderShading here...) :

Code:

    void RealizeShading(XUnit width)
    {       
        Shading shading = GetShading();

        if (shading != null)
        {
            ShadingRenderer shadingRenderer = new ShadingRenderer(this.gfx, shading);

            shadingRenderer.Render(this.currentXPosition, this.currentYPosition, width, this.currentVerticalInfo.height);
        }               
    }   


Now, we juste need to call this one everytime it's necessary...

...In RenderImage(), RenderTab(), RenderBlank(), RenderWord() and RenderLinebreak()

Code:

    void RenderImage(Image image)
    {
      RenderInfo renderInfo = this.CurrentImageRenderInfo;
      XUnit top = this.CurrentBaselinePosition;
      Area contentArea = renderInfo.LayoutInfo.ContentArea;
      top -= contentArea.Height;
     
      RealizeShading(contentArea.Width);
       
      RenderByInfos(this.currentXPosition, top, new RenderInfo[] { renderInfo });

      RenderUnderline(contentArea.Width, true);
      RealizeHyperlink(contentArea.Width);
     

      this.currentXPosition += contentArea.Width;
    }



Code:

    void RenderTab()
    {
      TabOffset tabOffset = NextTabOffset();
      RealizeShading(tabOffset.offset);
      RenderUnderline(tabOffset.offset, false);
      RenderTabLeader(tabOffset);
      RealizeHyperlink(tabOffset.offset);     
      this.currentXPosition += tabOffset.offset;
    }


Code:

    void RenderBlank()
    {
      if (!IgnoreBlank())
      {
        XUnit wordDistance = this.CurrentWordDistance;
        RealizeShading(wordDistance);
        RenderUnderline(wordDistance, false);
        RealizeHyperlink(wordDistance);       
        this.currentXPosition += wordDistance;
      }
      else
      {
        // RealizeShading(0); Useless !
        RenderUnderline(0, false);
        RealizeHyperlink(0);       
      }
    }


Code:

    void RenderWord(string word)
    {
      Font font = this.CurrentDomFont;
      XFont xFont = CurrentFont;
      if (font.Subscript || font.Superscript)
        xFont = FontHandler.ToSubSuperFont(xFont);
      XUnit wordWidth = MeasureString(word);

      RealizeShading(wordWidth);
      this.gfx.DrawString(word, xFont, CurrentBrush, this.currentXPosition, this.CurrentBaselinePosition);     
      RenderUnderline(wordWidth, true);
      RealizeHyperlink(wordWidth);
     
      this.currentXPosition += wordWidth;
    }


Code:

    void RenderLinebreak()
    {
      //this.RealizeShading(0); useless !
      this.RenderUnderline(0, false);
      this.RealizeHyperlink(0);     
    }


Some calls are in comments 'cuz there're kind of useless when with the width is null Rolling Eyes

I've not tested all the cases, but it's seems to be working Wink

Have a nice day !
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/ All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group. Hosted by phpBB.BizHat.com


Start Your Own YouTube Clone

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!
For Support, visit Forums.BizHat.com