Skip to content

Commit

Permalink
added support for supperscript
Browse files Browse the repository at this point in the history
  • Loading branch information
W0dan committed Apr 4, 2013
1 parent 7259e00 commit 6f5d60e
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 46 deletions.
108 changes: 79 additions & 29 deletions PdfCraft/API/TextBox.cs
Expand Up @@ -86,6 +86,16 @@ public void SetItalicOff()
SetFont(_currentFont, true);
}

public void SetSuperscriptOn()
{
_textCommands.Add(new TextCommand(Command.SetSuperscriptOn, null));
}

public void SetSuperscriptOff()
{
_textCommands.Add(new TextCommand(Command.SetSuperscriptOff, null));
}

public void SetAlignment(TextAlignment alignment)
{
_textCommands.Add(new TextCommand(Command.SetAlignment, alignment));
Expand All @@ -110,14 +120,14 @@ public void AddText(string text)
text = text.Replace("", @"\200"); //128

text = text.Replace("", @"\202"); //130
text = text.Replace("ƒ", @"\203");
text = text.Replace("", @"\204");
text = text.Replace("", @"\205");
text = text.Replace("", @"\206");
text = text.Replace("", @"\207");
text = text.Replace("ƒ", @"\203");
text = text.Replace("", @"\204");
text = text.Replace("", @"\205");
text = text.Replace("", @"\206");
text = text.Replace("", @"\207");

text = text.Replace("ˆ", @"\210"); //136
text = text.Replace("", @"\211");
text = text.Replace("", @"\211");
text = text.Replace("Š", @"\212");
text = text.Replace("", @"\213");
text = text.Replace("Œ", @"\214");
Expand Down Expand Up @@ -182,7 +192,7 @@ public void AddText(string text)
text = text.Replace("Å", @"\305");
text = text.Replace("Æ", @"\306");
text = text.Replace("Ç", @"\307");

text = text.Replace("È", @"\310"); //200
text = text.Replace("É", @"\311");
text = text.Replace("Ê", @"\312");
Expand All @@ -191,7 +201,7 @@ public void AddText(string text)
text = text.Replace("Í", @"\315");
text = text.Replace("Î", @"\316");
text = text.Replace("Ï", @"\317");

text = text.Replace("Ð", @"\320"); //208
text = text.Replace("Ñ", @"\321");
text = text.Replace("Ò", @"\322");
Expand Down Expand Up @@ -258,9 +268,18 @@ internal override IByteContainer Content
{
get
{
FontDefinition currentFont = null;
var currentAlignment = TextAlignment.Left;
var currentColor = Color.Black;
//FontDefinition currentFont = null;
//var currentAlignment = TextAlignment.Left;
//var currentColor = Color.Black;
//var superscript = false;

var currentStyle = new TextStyle
{
Alignment = TextAlignment.Left,
Color = Color.Black,
Superscript = false,
Font = null
};

var buffer = new List<TextboxLineBuffer>();

Expand All @@ -273,25 +292,36 @@ internal override IByteContainer Content
switch (textCommand.Command)
{
case Command.SetFont:
currentFont = (FontDefinition)textCommand.Data;
currentStyle = currentStyle.Clone();
currentStyle.Font = (FontDefinition)textCommand.Data;
break;
case Command.SetColor:
currentColor = (Color)textCommand.Data;
currentStyle = currentStyle.Clone();
currentStyle.Color = (Color)textCommand.Data;
break;
case Command.SetAlignment:
currentAlignment = (TextAlignment)textCommand.Data;
currentStyle = currentStyle.Clone();
currentStyle.Alignment = (TextAlignment)textCommand.Data;
break;
case Command.SetSuperscriptOn:
currentStyle = currentStyle.Clone();
currentStyle.Superscript = true;
break;
case Command.SetSuperscriptOff:
currentStyle = currentStyle.Clone();
currentStyle.Superscript = false;
break;
case Command.AddText:
if (currentFont == null)
if (currentStyle.Font == null)
throw new ApplicationException("before adding text to a textbox, a font must be set");

if (lineBuffer == null)
{
lineBuffer = new TextboxLineBuffer(currentAlignment);
lineBuffer = new TextboxLineBuffer(currentStyle.Alignment);
buffer.Add(lineBuffer);
}

lineBuffer.CurrentAlignment = currentAlignment;
lineBuffer.CurrentAlignment = currentStyle.Alignment;
var addedText = textCommand.Data.ToString();
var texts = addedText.Split('\n');

Expand All @@ -304,24 +334,24 @@ internal override IByteContainer Content
wordWrapper = new WordWrapping(_size.Width);
}

var breaks = wordWrapper.WrapIt(texts[i], currentFont);
var breaks = wordWrapper.WrapIt(texts[i], currentStyle.Font);

foreach (var textItem in breaks)
{
if (lineBuffer.Parts.Any() && lineBuffer.Parts.Last().EndOfLine)
{
lineBuffer = new TextboxLineBuffer(currentAlignment);
lineBuffer = new TextboxLineBuffer(currentStyle.Alignment);
buffer.Add(lineBuffer);
}

if (!textItem.HasToBreak)
{
var bufferItemPart = new TextboxLinePart(textItem, currentFont, currentColor, false);
var bufferItemPart = new TextboxLinePart(textItem, currentStyle, false);
lineBuffer.AddPart(bufferItemPart);
}
else
{
var bufferItemPart = new TextboxLinePart(textItem, currentFont, currentColor, true);
var bufferItemPart = new TextboxLinePart(textItem, currentStyle, true);
lineBuffer.AddPart(bufferItemPart);
}
}
Expand All @@ -341,6 +371,7 @@ private IByteContainer WriteBufferToPdf(IEnumerable<TextboxLineBuffer> buffer)

var textLeadingIsSet = false;
var colorIsSet = false;
var superscriptIsSet = false;
FontDefinition previousFont = null;
var previousColor = Color.Black;

Expand All @@ -357,31 +388,50 @@ private IByteContainer WriteBufferToPdf(IEnumerable<TextboxLineBuffer> buffer)
if (!textLeadingIsSet)
{
textLeadingIsSet = true;
textLeading = (int)(part.Font.Size * 1.21);
textLeading = (int)(part.Style.Font.Size * 1.21);
sbBuffered.Append(string.Format("0 -{0} TD ", textLeading));
}

if (!colorIsSet || part.Color != previousColor)
if (!colorIsSet || part.Style.Color != previousColor)
{
colorIsSet = true;
previousColor = part.Color;
sbBufferedLine.Append(string.Format(part.Color.ToPdfColor() + " rg "));
previousColor = part.Style.Color;
sbBufferedLine.Append(string.Format(part.Style.Color.ToPdfColor() + " rg "));
}

if (part.Font != previousFont)
if (part.Style.Font != previousFont)
{
previousFont = part.Font;
sbBufferedLine.Append(string.Format("{0} {1} Tf ", part.Font.Font.FontName, part.Font.Size));
previousFont = part.Style.Font;
sbBufferedLine.Append(string.Format("{0} {1} Tf ", part.Style.Font.Font.FontName, part.Style.Font.Size));

textLeading = (int)(part.Font.Size * 1.21);
textLeading = (int)(part.Style.Font.Size * 1.21);
sbBuffered.Append(string.Format("{0} TL ", textLeading));
}

if (!superscriptIsSet && part.Style.Superscript)
{
superscriptIsSet = true;
sbBufferedLine.Append(string.Format("{0} {1} Tf ", part.Style.Font.Font.FontName, part.Style.Font.Size / 2));
sbBufferedLine.Append(string.Format("{0} Ts ", part.Style.Font.Size / 2));
}

if (superscriptIsSet && !part.Style.Superscript)
{
superscriptIsSet = false;
sbBufferedLine.Append(string.Format("{0} {1} Tf ", part.Style.Font.Font.FontName, part.Style.Font.Size));
sbBufferedLine.Append("0 Ts ");
}

if (part.TextItem.Text.Length > 0)
sbBufferedLine.Append(string.Format("({0}) Tj", part.TextItem.Text));

if (part.EndOfLine)
{
superscriptIsSet = false;
sbBufferedLine.Append(string.Format("{0} {1} Tf ", part.Style.Font.Font.FontName, part.Style.Font.Size));
sbBufferedLine.Append("0 Ts ");
sbBufferedLine.Append(" T*" + StringConstants.NewLine);
}
else
sbBufferedLine.Append(StringConstants.NewLine);
}
Expand Down
4 changes: 3 additions & 1 deletion PdfCraft/Contents/Text/Command.cs
Expand Up @@ -5,6 +5,8 @@ public enum Command
SetFont,
AddText,
SetColor,
SetAlignment
SetAlignment,
SetSuperscriptOn,
SetSuperscriptOff
}
}
28 changes: 28 additions & 0 deletions PdfCraft/Contents/Text/TextStyle.cs
@@ -0,0 +1,28 @@
using System.Drawing;
using PdfCraft.API;
using PdfCraft.Fonts;

namespace PdfCraft.Contents.Text
{
internal class TextStyle
{
public TextAlignment Alignment { get; set; }

public Color Color { get; set; }

public bool Superscript { get; set; }

public FontDefinition Font { get; set; }

public TextStyle Clone()
{
return new TextStyle
{
Alignment = Alignment,
Color = Color,
Superscript = Superscript,
Font = Font
};
}
}
}
22 changes: 6 additions & 16 deletions PdfCraft/Contents/Text/TextboxLinePart.cs
@@ -1,22 +1,17 @@
using System.Drawing;
using PdfCraft.Fonts;

namespace PdfCraft.Contents.Text
namespace PdfCraft.Contents.Text
{
/// <summary>
/// defines a part of a line of text, with layout
/// </summary>
internal class TextboxLinePart
{
private readonly BreakPoint _textItem;
private readonly FontDefinition _font;
private readonly Color _color;
private readonly TextStyle _style;

public TextboxLinePart(BreakPoint textItem, FontDefinition font, Color color, bool endOfLine)
public TextboxLinePart(BreakPoint textItem, TextStyle style, bool endOfLine)
{
_textItem = textItem;
_font = font;
_color = color;
_style = style;
EndOfLine = endOfLine;
}

Expand All @@ -27,14 +22,9 @@ public BreakPoint TextItem
get { return _textItem; }
}

public FontDefinition Font
{
get { return _font; }
}

public Color Color
public TextStyle Style
{
get { return _color; }
get { return _style; }
}
}
}
1 change: 1 addition & 0 deletions PdfCraft/PdfCraft.csproj
Expand Up @@ -49,6 +49,7 @@
<Compile Include="Contents\Text\BreakPoint.cs" />
<Compile Include="Contents\Text\TextboxLineBuffer.cs" />
<Compile Include="Contents\Text\TextboxLinePart.cs" />
<Compile Include="Contents\Text\TextStyle.cs" />
<Compile Include="Extensions\IntExtensions.cs" />
<Compile Include="Fonts\FontDefinition.cs" />
<Compile Include="Fonts\FontDescriptor.cs" />
Expand Down
@@ -0,0 +1,51 @@
using System.Drawing;
using NUnit.Framework;
using PdfCraft.API;
using PdfCraft.Containers;
using PdfCraft.Fonts;

namespace Tests.Concerning_the_API.Given_a_document
{
public class When_Superscript_is_used : BaseTest
{
private TextBox _sut;
private Document _document;
private Page _page;

public override void Arrange()
{
_document = new Document();
_page = _document.AddPage();
_sut = _document.CreateTextBox(new Rectangle(33, 12, 155, 255));

_sut.SetFont(new FontProperties("Helvetica", 10));

_page.AddTextBox(_sut);
}

public override void Act()
{
_sut.AddText("normal text");
_sut.SetSuperscriptOn();
_sut.AddText("superscripted text");
_sut.SetSuperscriptOff();
_sut.AddText(" normal text again");
}

[Test]
public void It_should_render_a_pdf()
{
var test = new TestExecutor(this);

test.Assert(() =>
{
var generatedBytes = _document.Generate();
//DumpToRandomFile(generatedBytes, "pdf");
var content = new ByteArrayByteContainer(generatedBytes);
Assert.IsNotNull(content);
});
}
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Expand Up @@ -66,6 +66,7 @@
<Compile Include="Concerning_the_API\Given_a_GraphicsCanvas\When_DrawImage_is_called_with_a_filename.cs" />
<Compile Include="Concerning_the_API\Given_a_GraphicsCanvas\When_DrawCircle_is_called.cs" />
<Compile Include="Concerning_the_API\Given_a_GraphicsCanvas\When_DrawImage_is_called_with_a_stream.cs" />
<Compile Include="Concerning_the_API\Given_a_document\When_Superscript_is_used.cs" />
<Compile Include="Concerning_the_API\Given_a_TextBox\When_text_with_a_Euro_sign_is_added.cs" />
<Compile Include="Concerning_the_API\Given_a_TextBox\When_the_color_is_set_to_gray.cs" />
<Compile Include="Concerning_the_API\Given_a_TextBox\When_text_is_added.cs" />
Expand Down

0 comments on commit 6f5d60e

Please sign in to comment.