Skip to content

Commit

Permalink
Merge pull request ststeiger#312 from IndexGit/feature/fonts_striketh…
Browse files Browse the repository at this point in the history
…rough

Add Strikethrough to Font
  • Loading branch information
ststeiger authored Dec 5, 2022
2 parents 8b9076c + edc83d0 commit a4519a5
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ internal void ApplyFont(Font font, Font refFont)
if (!font.underline.IsNull && (refFont == null || font.Underline != refFont.Underline))
this.Underline = font.Underline;

if (!font.strikethrough.IsNull && (refFont == null || font.Strikethrough != refFont.Strikethrough))
this.Strikethrough = font.Strikethrough;

if (!font.color.IsNull && (refFont == null || font.Color.Argb != refFont.Color.Argb))
this.Color = font.Color;
}
Expand Down Expand Up @@ -142,6 +145,9 @@ public void ApplyFont(Font font)
if (!font.underline.IsNull)
this.Underline = font.Underline;

if (!font.strikethrough.IsNull)
this.Strikethrough = font.Strikethrough;

if (!font.color.IsNull)
this.Color = font.Color;
}
Expand Down Expand Up @@ -244,6 +250,15 @@ public bool Subscript
[DV]
internal NBool subscript = NBool.NullValue;


public Strikethrough Strikethrough
{
get { return (Strikethrough)this.strikethrough.Value; }
set { this.strikethrough.Value = (int)value; }
}
[DV(Type = typeof(Strikethrough))]
internal NEnum strikethrough = NEnum.NullValue(typeof(Strikethrough));

// + .Name = "Verdana"
// + .Size = 8
// + .Bold = False
Expand Down Expand Up @@ -362,6 +377,9 @@ internal void Serialize(Serializer serializer, Font font)
if (!this.underline.IsNull)
serializer.WriteSimpleAttribute("Underline", this.Underline);

if (!this.strikethrough.IsNull)
serializer.WriteSimpleAttribute("Strikethrough", this.Strikethrough);

if (!this.superscript.IsNull)
serializer.WriteSimpleAttribute("Superscript", this.Superscript);

Expand Down Expand Up @@ -399,6 +417,9 @@ internal void Serialize(Serializer serializer, Font font)
if (!underline.IsNull && (font == null || Underline != font.Underline || font.underline.IsNull))
serializer.WriteSimpleAttribute("Underline", Underline);

if (!strikethrough.IsNull && (font == null || Strikethrough != font.Strikethrough || font.strikethrough.IsNull))
serializer.WriteSimpleAttribute("Strikethrough", Strikethrough);

if (!superscript.IsNull && (font == null || Superscript != font.Superscript || font.superscript.IsNull))
serializer.WriteSimpleAttribute("Superscript", Superscript);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ internal void SetupStyles()
style.Font.Color = Colors.Black;
style.Font.Subscript = false;
style.Font.Superscript = false;
style.Font.Strikethrough = Strikethrough.None;
style.ParagraphFormat.Alignment = ParagraphAlignment.Left;
style.ParagraphFormat.FirstLineIndent = 0;
style.ParagraphFormat.LeftIndent = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#region MigraDoc - Creating Documents on the Fly
//
// Authors:
// Stefan Lange (mailto:[email protected])
// Klaus Potzesny (mailto:[email protected])
// David Stephensen (mailto:[email protected])
//
// Copyright (c) 2001-2009 empira Software GmbH, Cologne (Germany)
//
// http://www.PdfSharpCore.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 System;
using System.Collections.Generic;
using System.Text;

namespace MigraDocCore.DocumentObjectModel
{
/// <summary>
/// Specify the strike out type for font
/// </summary>
public enum Strikethrough
{
None,
Single,
Words,
Dotted,
Dash,
DotDash,
DotDotDash,

/* --- unsupported ---
Double = 3,
Thick = 6,
Wavy = 11,
WavyHeavy = 27,
DottedHeavy = 20,
DashHeavy = 23,
DotDashHeavy = 25,
DotDotDashHeavy = 26,
DashLong = 39,
DashLongHeavy = 55,
WavyDouble = 43
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void MapObject(Font font, DocumentObjectModel.Font domFont)
font.Size = domFont.Size.Point;
font.Subscript = domFont.Subscript;
font.Superscript = domFont.Superscript;
font.Strikethrough = (Strikethrough)domFont.Strikethrough;
font.Underline = (Underline)domFont.Underline;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void Formatted(string pdfOutputFile)

internal static void FillFormattedParagraph(Paragraph par)
{
for (int idx = 0; idx <= 140; ++idx)
for (int idx = 0; idx <= 200; ++idx)
{
if (idx < 60)
{
Expand All @@ -61,12 +61,42 @@ internal static void FillFormattedParagraph(Paragraph par)
par.AddText((idx).ToString());
par.AddText(" ");
}
else
else if (idx < 140)
{
FormattedText formText = par.AddFormattedText((idx).ToString(), TextFormat.Italic);
formText.Font.Size = 6;
formText.AddText(" ");
}
// Strikethrough tests
else if (idx < 150)
{
FormattedText formText = par.AddFormattedText((idx).ToString());
formText.Font.Size = 16;
formText.Font.Strikethrough = Strikethrough.Single;
formText.AddText(" ");
}
else if (idx < 160)
{
FormattedText formText = par.AddFormattedText((idx).ToString());
formText.Font.Size = 8;
formText.Font.Strikethrough = Strikethrough.DotDash;
formText.AddText(" ");
}
else if (idx < 170)
{
FormattedText formText = par.AddFormattedText((idx).ToString());
formText.Font.Size = 14;
formText.Font.Strikethrough = Strikethrough.DotDotDash;
formText.AddText(" ");
}
else if (idx < 180)
{
FormattedText formText = par.AddFormattedText((idx).ToString());
formText.Font.Size = 20;
formText.Font.Strikethrough = Strikethrough.Dotted;
formText.AddText(" ");
}

if (idx % 50 == 0)
par.AddLineBreak();
}
Expand Down
108 changes: 108 additions & 0 deletions MigraDocCore.Rendering/MigraDoc.Rendering/ParagraphRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ void RenderImage(Image image)
RenderByInfos(this.currentXPosition, top, new RenderInfo[] { renderInfo });

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

this.currentXPosition += contentArea.Width;
Expand Down Expand Up @@ -907,6 +908,7 @@ void RenderSectionPagesField(SectionPagesField sectionPagesField)
void RenderBookmarkField()
{
RenderUnderline(0, false);
RenderStrikethrough(0, false);
}

void RenderPageRefField(PageRefField pageRefField)
Expand Down Expand Up @@ -946,6 +948,7 @@ void RenderSpace(Character character)
void RenderLinebreak()
{
this.RenderUnderline(0, false);
this.RenderStrikethrough(0, false);
this.RealizeHyperlink(0);
}

Expand All @@ -963,6 +966,7 @@ void RenderTab()
{
TabOffset tabOffset = NextTabOffset();
RenderUnderline(tabOffset.offset, false);
RenderStrikethrough(tabOffset.offset, false);
RenderTabLeader(tabOffset);
RealizeHyperlink(tabOffset.offset);
this.currentXPosition += tabOffset.offset;
Expand Down Expand Up @@ -1065,12 +1069,14 @@ void RenderBlank()
{
XUnit wordDistance = this.CurrentWordDistance;
RenderUnderline(wordDistance, false);
RenderStrikethrough(wordDistance, false);
RealizeHyperlink(wordDistance);
this.currentXPosition += wordDistance;
}
else
{
RenderUnderline(0, false);
RenderStrikethrough(0, false);
RealizeHyperlink(0);
}
}
Expand All @@ -1096,6 +1102,7 @@ void RenderWord(string word)
this.gfx.DrawString(word, xFont, CurrentBrush, this.currentXPosition, CurrentBaselinePosition);
XUnit wordWidth = MeasureString(word);
RenderUnderline(wordWidth, true);
RenderStrikethrough(wordWidth, true);
RealizeHyperlink(wordWidth);
this.currentXPosition += wordWidth;
}
Expand Down Expand Up @@ -2410,6 +2417,66 @@ bool UnderlinePenChanged(XPen pen)
return pen.Width != this.currentUnderlinePen.Width;
}


void RenderStrikethrough(XUnit width, bool isWord)
{
XPen pen = GetStrikethroughPen(isWord);

bool penChanged = StrikethroughPenChanged(pen);
if (penChanged)
{
if (this.currentStrikethroughPen != null)
EndStrikethrough(this.currentStrikethroughPen, this.currentXPosition);

if (pen != null)
StartStrikethrough(this.currentXPosition);

this.currentStrikethroughPen = pen;
}

if (this.currentLeaf.Current == this.endLeaf.Current)
{
if (this.currentStrikethroughPen != null)
EndStrikethrough(this.currentStrikethroughPen, this.currentXPosition + width);

this.currentStrikethroughPen = null;
}
}

void StartStrikethrough(XUnit xPosition)
{
this.strikethroughStartPos = xPosition;
}

void EndStrikethrough(XPen pen, XUnit xPosition)
{
XUnit yPosition = CurrentBaselinePosition;
yPosition -= pen.Width / 2;
yPosition -= this.currentVerticalInfo.descent;

this.gfx.DrawLine(pen, this.strikethroughStartPos, yPosition, xPosition, yPosition);
}

XPen currentStrikethroughPen = null;
XUnit strikethroughStartPos;

bool StrikethroughPenChanged(XPen pen)
{
if (pen == null && this.currentStrikethroughPen == null)
return false;

if (pen == null && this.currentStrikethroughPen != null)
return true;

if (pen != null && this.currentStrikethroughPen == null)
return true;

if (pen.Color != this.currentStrikethroughPen.Color)
return true;

return pen.Width != this.currentStrikethroughPen.Width;
}

RenderInfo CurrentImageRenderInfo
{
get
Expand Down Expand Up @@ -2474,6 +2541,47 @@ XPen GetUnderlinePen(bool isWord)
return pen;
}

XPen GetStrikethroughPen(bool isWord)
{
Font font = CurrentDomFont;
Strikethrough strikethroughType = font.Strikethrough;
if (strikethroughType == Strikethrough.None)
return null;

if (strikethroughType == Strikethrough.Words && !isWord)
return null;

#if noCMYK
XPen pen = new XPen(XColor.FromArgb(font.Color.Argb), font.Size / 16);
#else
XPen pen = new XPen(ColorHelper.ToXColor(font.Color, this.paragraph.Document.UseCmykColor), font.Size / 16);
#endif
switch (font.Strikethrough)
{
case Strikethrough.DotDash:
pen.DashStyle = XDashStyle.DashDot;
break;

case Strikethrough.DotDotDash:
pen.DashStyle = XDashStyle.DashDotDot;
break;

case Strikethrough.Dash:
pen.DashStyle = XDashStyle.Dash;
break;

case Strikethrough.Dotted:
pen.DashStyle = XDashStyle.Dot;
break;

case Strikethrough.Single:
default:
pen.DashStyle = XDashStyle.Solid;
break;
}
return pen;
}

private static XStringFormat StringFormat
{
get
Expand Down
10 changes: 10 additions & 0 deletions PdfSharpCore.Charting/PdfSharp.Charting/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public Underline Underline
}
internal Underline underline;

/// <summary>
/// Gets or sets the strikethrough property.
/// </summary>
public Strikethrough Strikethrough
{
get { return this.strikethrough; }
set { this.strikethrough = value; }
}
internal Strikethrough strikethrough;

/// <summary>
/// Gets or sets the color property.
/// </summary>
Expand Down
Loading

0 comments on commit a4519a5

Please sign in to comment.