Skip to content

Commit 3100288

Browse files
AdrianEPPlusswmalswmal
authored
Feature/pdfnewfontdata (#2313)
* started work on adding embedded fonts in pdf. * Added more pdf objects for embedding fonts in pdf * progress embedding fonts * font embedding progress. making stuff binary. * Fixed many issues with embedding font. Still some issues left. * embedding font progress * fixed t * fixed issues from develop9 * Removed unused code, added comments and clean up some parts. * Rich Text added * Added more text features for embedding fonts. * Add Columns For Non Wrapped Text * new gridline system started. * Vertical Gridlines progress * gridlines progress. * need better algo for gridlines. * Refactoring page layouting * added * Improved support for fallbackfonts * progress * progress * Fallbackfonts refactoring * horizontal gridlines progress * vertical border progress. * More work on fallback fonts. Added fallback to Archivo fonts if the primary font is missing * Fixed some tests * gridlines appear to work correctly now. Just need some smaller adjustments * Fixed issue with ttc files * rewriting page setup started. * new system works just need some tweaks * Fallback fonts. * progress * progress * fallback fonts for emojis now work * Fixed border issues. * Added headerFooter back * Added some small fixes liike adjusting cells, removing cells and clipping text, * added comments * Fix for strings only containing of fallback fonts. Removed unitPerEm param to GetWidth functions for shaped text * Added layout: center on vertical and horizontal and page breaks * Measuring and placing text now happens after shaping. * Added GetLineHeightInPOints to shapedtext. * Added toughts for row and column headings * Added line height for alginging text in cell. * removed tests --------- Co-authored-by: swmal <897655+swmal@users.noreply.github.com> Co-authored-by: swmal <{ID}+username}@users.noreply.github.com>
1 parent 28c3dcc commit 3100288

156 files changed

Lines changed: 5765 additions & 3056 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/EPPlus.Export.Pdf.Tests/PdfTests.cs

Lines changed: 53 additions & 161 deletions
Large diffs are not rendered by default.

src/EPPlus.Export.Pdf/ExcelPdf.cs

Lines changed: 29 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ Date Author Change
1616
using System.Text;
1717
using OfficeOpenXml;
1818
using EPPlus.Graphics;
19-
using System.Drawing;
20-
using EPPlus.Export.Pdf.Pdfhelpers;
2119
using EPPlus.Export.Pdf.PdfLayout;
2220
using EPPlus.Export.Pdf.PdfObjects;
2321
using EPPlus.Export.Pdf.PdfResources;
2422
using EPPlus.Export.Pdf.PdfSettings;
2523
using OfficeOpenXml.Style;
2624
using EPPlus.Fonts.OpenType;
25+
using OfficeOpenXml.Interfaces.Fonts;
2726

2827
namespace EPPlus.Export.Pdf
2928
{
@@ -87,28 +86,6 @@ public ExcelPdf(ExcelRangeBase Range, PdfPageSettings pageSettings = null)
8786
//PageSettings = pageSettings == null ? new PdfPageSettings() : pageSettings;
8887
}
8988

90-
//Get font label //need to update this one too for same reasons as AddFontData
91-
internal PdfFontResource GetFontResource(string fontName, FontSubFamily subFamily, double fontSize)
92-
{
93-
if (!Dictionaries.Fonts.ContainsKey(fontName))
94-
{
95-
int label = 1;
96-
if (Dictionaries.Fonts.Count > 0)
97-
{
98-
label = Dictionaries.Fonts.Last().Value.labelNumber + 1;
99-
}
100-
PdfFontResource fr = new PdfFontResource(fontName, subFamily, label, PageSettings);
101-
if (fontName != "Courier New")
102-
{
103-
Document.Add(fr.GetFontDescriptorObject(Document.Count + 1));
104-
Document.Add(fr.GetWidthsObject(Document.Count + 1));
105-
}
106-
Document.Add(fr.GetFontObject(Document.Count + 1));
107-
Dictionaries.Fonts.Add(fontName, fr);
108-
}
109-
return Dictionaries.Fonts[fontName];
110-
}
111-
11289
//Get the label to use for pattern.
11390
internal string GetPatternLabel(PdfCellLayout layout)
11491
{
@@ -123,17 +100,32 @@ internal string GetPatternLabel(PdfCellLayout layout)
123100
return null;
124101
}
125102

126-
//Add Fonts //Need to update this method a bit. We should check for all default fonts and not only courier new?
103+
//Add Fonts //Need to update this method a bit. We should check for all default fonts and not only courier new? Also need to check if we are allowed to embedd the font.
127104
internal void AddFontData()
128105
{
129-
foreach (var font in Dictionaries.Fonts)
106+
if (PageSettings.EmbeddFonts)
107+
{
108+
foreach (var font in Dictionaries.Fonts)
109+
{
110+
//font.Value.CreateGidsAndCharMaps();
111+
var CidSet = font.Value.GetCidSet(Document.Count + 1);
112+
if (CidSet != null) Document.Add(CidSet);
113+
Document.Add(font.Value.GetEmbeddedFontStreamObject(Document.Count + 1));
114+
Document.Add(font.Value.GetFontDescriptorObject(Document.Count + 1));
115+
Document.Add(font.Value.GetCIDFontObject(Document.Count + 1));
116+
Document.Add(font.Value.GetUnicodeCmapObject(Document.Count + 1));
117+
Document.Add(font.Value.GetType0FontDictObject(Document.Count + 1));
118+
font.Value.GetFontObject(Document.Count);
119+
}
120+
}
121+
else
130122
{
131-
if (font.Key != "Courier New")
123+
foreach (var font in Dictionaries.Fonts)
132124
{
133125
Document.Add(font.Value.GetFontDescriptorObject(Document.Count + 1));
134126
Document.Add(font.Value.GetWidthsObject(Document.Count + 1));
127+
Document.Add(font.Value.GetFontObject(Document.Count + 1));
135128
}
136-
Document.Add(font.Value.GetFontObject(Document.Count + 1));
137129
}
138130
}
139131

@@ -167,13 +159,15 @@ private PdfPage AddPage(int pagesObjectNumber, List<int> contentObjectNumbers, P
167159
Document.Add(page);
168160
return page;
169161
}
162+
170163
//Create Pages
171164
private PdfPages AddPages()
172165
{
173166
var pages = new PdfPages(Document.Count + 1, new List<int>{});
174167
Document.Add(pages);
175168
return pages;
176169
}
170+
177171
//Create Catalog
178172
private PdfCatalog AddCatalog(int pagesObjectNumber)
179173
{
@@ -187,13 +181,14 @@ private void AddContent(Transform pageLayout, PdfPage page)
187181
{
188182
var cells = pageLayout.ChildObjects.Where(t => t is PdfCellLayout || t is PdfCellContentLayout || t is PdfCellBorderLayout).GroupBy(t => t.Name);
189183
var contentStream = new PdfContentStream(Document.Count + 1);
184+
contentStream.AddCommand($"% {pageLayout.Name} start");
185+
//Add clipping rectangle around page content.
186+
contentStream.AddCommand("q");
187+
contentStream.AddMarginClipping((PdfPageLayout)pageLayout);
190188
if (PageSettings.ShowGridLines)
191189
{
192190
contentStream.AddInnerGridLines(pageLayout);
193191
}
194-
//Add clipping rectangle around page content.
195-
contentStream.AddCommand("q");
196-
contentStream.AddMarginClipping(pageLayout, PageSettings.ContentBounds);
197192
foreach (var cell in cells)
198193
{
199194
foreach (var cellPart in cell)
@@ -223,6 +218,7 @@ private void AddContent(Transform pageLayout, PdfPage page)
223218
AddHeaderFooter(contentStream, pageLayout, page);
224219
Document.Add(contentStream);
225220
page.contentObjectNumbers.Add(contentStream.objectNumber);
221+
contentStream.AddCommand($"% {pageLayout.Name} end");
226222
}
227223

228224
//Add Header Footer
@@ -236,6 +232,7 @@ private void AddHeaderFooter(PdfContentStream contentStream, Transform pageLayou
236232
}
237233
}
238234

235+
//Add Info
239236
private PdfInfoObject AddInfoObject()
240237
{
241238
var info = new PdfInfoObject(Document.Count + 1, _workheets[0].Workbook._package.File.Name);
@@ -285,7 +282,7 @@ public void CreatePdf(string Filename)
285282
foreach (var pdfobj in Document)
286283
{
287284
crossRefTable.AddPosition(fs.Position);
288-
bw.Write(pdfobj.ToPdfBytes());
285+
pdfobj.ToPdfBytes(bw);
289286
debugString += pdfobj.ToPdfString();
290287
}
291288
//Write CrossReference
@@ -308,59 +305,5 @@ public void CreatePdf(string Filename)
308305
}
309306
}
310307
}
311-
312-
#region DEBUG
313-
//These methods need to be rewritten if they should be used.
314-
315-
//internal void DrawMarginAndHeaderLines(PdfContentBounds bounds, PdfPage page)
316-
//{
317-
// var content = new PdfContentStream(Document.Count + 1);
318-
// //Bottom line
319-
// DrawLine(content, Color.Black, 0, bounds.Bottom, PageSettings.PageSize.WidthPu, bounds.Bottom);
320-
// DrawLine(content, new Color(1, 0, 1), bounds.X, bounds.Bottom, bounds.X + bounds.Width, bounds.Bottom);
321-
// //Top line
322-
// DrawLine(content, Color.Black, 0, bounds.Top, PageSettings.PageSize.WidthPu, bounds.Top);
323-
// DrawLine(content, new Color(1, 0, 1), bounds.X, bounds.Top, bounds.X + bounds.Width, bounds.Top);
324-
// //Left line
325-
// DrawLine(content, Color.Black, bounds.Left, 0, bounds.Left, 0);
326-
// DrawLine(content, new Color(1, 0, 1), bounds.Left, bounds.Y + bounds.Height, bounds.Left, bounds.Y + bounds.Height);
327-
// //Right line
328-
// DrawLine(content, Color.Black, bounds.Right, 0, bounds.Right, 0);
329-
// DrawLine(content, new Color(1, 0, 1), bounds.Right, bounds.Y + bounds.Height, bounds.Right, bounds.Y + bounds.Height);
330-
// //Header line
331-
// DrawLine(content, new Color(1, 0, 1), bounds.Right, bounds.HeaderY, bounds.Left, bounds.HeaderY);
332-
// DrawLine(content, new Color(1, 0, 1), bounds.CenterHeaderX, bounds.Top, bounds.CenterHeaderX, bounds.Top);
333-
// DrawLine(content, new Color(1, 0, 1), bounds.RightHeaderX, bounds.Top, bounds.RightHeaderX, bounds.Top);
334-
// //Footer line
335-
// DrawLine(content, new Color(1, 0, 1), bounds.Right, bounds.FooterY, bounds.Left, bounds.FooterY);
336-
// DrawLine(content, new Color(1, 0, 1), bounds.CenterFooterX, bounds.Bottom, bounds.CenterFooterX, bounds.Bottom);
337-
// DrawLine(content, new Color(1, 0, 1), bounds.RightFooterX, bounds.Bottom, bounds.RightFooterX, bounds.Bottom);
338-
// Document.Add(content);
339-
// page.contentObjectNumbers.Add(content.objectNumber);
340-
//}
341-
342-
//internal void DrawLine(PdfContentStream content, Color color, double x1, double y1, double x2, double y2)
343-
//{
344-
// content.AddCommand(color.ToStrokeCommand());
345-
// content.AddCommand($"{x1.ToPdfString()} {y1.ToPdfString()} m");
346-
// content.AddCommand($"{x2.ToPdfString()} {y2.ToPdfString()} l");
347-
// content.AddCommand("S");
348-
//}
349-
350-
//internal void DrawCrossHair(Color color, double x, double y, double size = 2)
351-
//{
352-
// var half = size / 2d;
353-
// var content = new PdfContentStream(Document.Count + 1);
354-
// content.AddCommand(color.ToStrokeCommand());
355-
// content.AddCommand($"{x.ToPdfString()} {(y - half).ToPdfString()} m");
356-
// content.AddCommand($"{x.ToPdfString()} {(y + half).ToPdfString()} l");
357-
// content.AddCommand($"{(x - half).ToPdfString()} {y.ToPdfString()} m");
358-
// content.AddCommand($"{(x + half).ToPdfString()} {y.ToPdfString()} l");
359-
// content.AddCommand("S");
360-
// Document.Add(content);
361-
//}
362-
363-
364-
#endregion
365308
}
366309
}

src/EPPlus.Export.Pdf/PdfFontData/PdfFontDataReader.cs

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/EPPlus.Export.Pdf/PdfFontData/PdfFontProperties.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/EPPlus.Export.Pdf/PdfLayout/IBorderLayout.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ Date Author Change
1313

1414
namespace EPPlus.Export.Pdf.PdfLayout
1515
{
16+
/// <summary>
17+
/// Interface for border objects
18+
/// </summary>
1619
internal interface IBorderLayout
1720
{
21+
/// <summary>
22+
/// Update Border positions.
23+
/// </summary>
1824
abstract void UpdateLocalBorderPosition();
1925
}
2026
}

src/EPPlus.Export.Pdf/PdfLayout/IShadingLayout.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ Date Author Change
1414

1515
namespace EPPlus.Export.Pdf.PdfLayout
1616
{
17+
/// <summary>
18+
/// Interface for shading objects.
19+
/// </summary>
1720
internal interface IShadingLayout
1821
{
22+
/// <summary>
23+
/// Update position matrix.
24+
/// </summary>
1925
abstract void UpdateShadingPositionMatrix(PdfPageSettings pageSettings);
2026
}
2127
}

0 commit comments

Comments
 (0)