Skip to content

Commit

Permalink
First step to exporting trees to PDF (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
Serg-Norseman committed Jun 18, 2024
1 parent dbfb497 commit fc5ae6c
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 31 deletions.
1 change: 1 addition & 0 deletions projects/GKCore/GKCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@
<Compile Include="GKCore\Lists\LocationLinksListModel.cs" />
<Compile Include="GKCore\Interfaces\IScrollableContainer.cs" />
<Compile Include="GKCore\EventDefinitions.cs" />
<Compile Include="GKCore\Types\GKPageSize.cs" />
</ItemGroup>
<Import Project="GKCore.props" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
8 changes: 7 additions & 1 deletion projects/GKCore/GKCore/Controllers/TreeChartWinController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* "GEDKeeper", the personal genealogical database editor.
* Copyright (C) 2009-2023 by Sergey V. Zhdanovskih.
* Copyright (C) 2009-2024 by Sergey V. Zhdanovskih.
*
* This file is part of "GEDKeeper".
*
Expand All @@ -18,6 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

//#define PDF_RENDER

using System.IO;
using GDModel;
using GKCore.Charts;
Expand Down Expand Up @@ -277,6 +279,10 @@ public bool SelectedPersonIsReal()
public async void SaveSnapshot()
{
string filters = GKUtils.GetImageFilter(true);
#if PDF_RENDER
filters += "|" + LangMan.LS(LSID.PDFFilter);
#endif

string fileName = await AppHost.StdDialogs.GetSaveFile("", GlobalOptions.Instance.ImageExportLastDir, filters, 2, "jpg", "");
if (!string.IsNullOrEmpty(fileName)) {
GlobalOptions.Instance.ImageExportLastDir = Path.GetDirectoryName(fileName);
Expand Down
39 changes: 37 additions & 2 deletions projects/GKCore/GKCore/Export/PDFWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using BSLib;
using GKCore.Charts;
using GKCore.Design.Graphics;
using GKCore.Types;
using iTextSharp.text;
using iTextSharp.text.pdf;
using it = iTextSharp.text;
Expand Down Expand Up @@ -104,6 +105,7 @@ public ExtSizeF GetTextSize(string text)
private Document fDocument;
private bool fMulticolumns;
private PdfWriter fPdfWriter;
private GKPageSize fPredefPage;
private itTable fTable;
private Stack<ITextElementArray> fStack;

Expand All @@ -118,6 +120,12 @@ public PDFWriter()
fBaseFont = BaseFont.CreateFont("FreeSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, BaseFont.CACHED, fontBytes, null);
}

public PDFWriter(GKPageSize predefPage, bool albumPage) : this()
{
fPredefPage = predefPage;
fAlbumPage = albumPage;
}

protected override void Dispose(bool disposing)
{
if (disposing) {
Expand Down Expand Up @@ -154,9 +162,36 @@ public override ExtRectF GetPageSize()

public override void BeginWrite()
{
itRectangle pageSize = !fAlbumPage ? PageSize.A4 : PageSize.A4.Rotate();
itRectangle pageSize;
if (fPredefPage == GKPageSize.None) {
pageSize = PageSize.A4;
} else {
switch (fPredefPage) {
case GKPageSize.A0:
pageSize = PageSize.A0;
break;
case GKPageSize.A1:
pageSize = PageSize.A1;
break;
case GKPageSize.A2:
pageSize = PageSize.A2;
break;
case GKPageSize.A3:
pageSize = PageSize.A3;
break;
case GKPageSize.A4:
default:
pageSize = PageSize.A4;
break;
case GKPageSize.A5:
pageSize = PageSize.A5;
break;
}
}

itRectangle pageRect = !fAlbumPage ? pageSize : pageSize.Rotate();

fDocument = new Document(pageSize, fMargins.Left, fMargins.Right, fMargins.Top, fMargins.Bottom);
fDocument = new Document(pageRect, fMargins.Left, fMargins.Right, fMargins.Top, fMargins.Bottom);
fPdfWriter = PdfWriter.GetInstance(fDocument, new FileStream(fFileName, FileMode.Create, FileAccess.Write));

fDocument.AddTitle(fDocumentTitle);
Expand Down
27 changes: 27 additions & 0 deletions projects/GKCore/GKCore/Types/GKPageSize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* "GEDKeeper", the personal genealogical database editor.
* Copyright (C) 2009-2024 by Sergey V. Zhdanovskih.
*
* This file is part of "GEDKeeper".
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace GKCore.Types
{
public enum GKPageSize
{
None, A0, A1, A2, A3, A4, A5,
}
}
4 changes: 2 additions & 2 deletions projects/GKv2/GEDKeeper2/GKUI/Components/TreeChartBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public TreeChartOptions Options
}
}

public new float Scale
public override float Scale
{
get { return fModel.Scale; }
}
Expand Down Expand Up @@ -204,7 +204,7 @@ public override void SetRenderer(ChartRenderer renderer)
fModel.SetRenderer(renderer);
}

public void SetScale(float value)
public override void SetScale(float value)
{
fModel.Scale = value;

Expand Down
34 changes: 17 additions & 17 deletions projects/GKv2/GEDKeeper2/GKUI/Forms/OptionsDlg.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 40 additions & 3 deletions projects/GKv2/GKComponents/GKUI/Components/CustomChart.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* "GEDKeeper", the personal genealogical database editor.
* Copyright (C) 2009-2023 by Sergey V. Zhdanovskih.
* Copyright (C) 2009-2024 by Sergey V. Zhdanovskih.
*
* This file is part of "GEDKeeper".
*
Expand All @@ -26,6 +26,8 @@
using GKCore;
using GKCore.Charts;
using GKCore.Design.Graphics;
using GKCore.Export;
using GKCore.Types;
using GKUI.Platform.Handlers;

namespace GKUI.Components
Expand All @@ -38,6 +40,11 @@ public abstract class CustomChart : ScrollablePanel, IPrintable

public event EventHandler NavRefresh;

public new virtual float Scale
{
get { return 0; }
}


protected CustomChart()
{
Expand All @@ -52,6 +59,10 @@ protected CustomChart()
fNavman = new NavigationStack<object>();
}

public virtual void SetScale(float value)
{
}

protected override bool IsInputKey(Keys keyData)
{
switch (keyData) {
Expand Down Expand Up @@ -216,8 +227,6 @@ public IImage GetPrintableImage()
return new ImageHandler(image);
}

/* TODO(zsv): Need to find an appropriate icon in the general style
* for the main toolbar - screenshot capture for windows with charts. */
public void SaveSnapshot(string fileName)
{
string ext = FileHelper.GetFileExtension(fileName);
Expand All @@ -239,6 +248,34 @@ public void SaveSnapshot(string fileName)
SetRenderer(prevRenderer);
}

return;
} else if (ext == ".pdf") {
var prevRenderer = fRenderer;

var pdfWriter = new PDFWriter(GKPageSize.A4, true);
pdfWriter.SetFileName(fileName);
pdfWriter.BeginWrite();

var renderer = pdfWriter.GetPageRenderer();
SetRenderer(renderer);

var pageSize = pdfWriter.GetPageSize();
var sf = GfxHelper.ZoomToFit(imageSize.Width, imageSize.Height, pageSize.GetWidth(), pageSize.GetHeight());
var prevScale = this.Scale;
if (sf < 1.0f) this.SetScale(sf);

renderer.BeginDrawing();
try {
//GenChart(indi.IRec, indi.TreeKind, false);
RenderImage(RenderTarget.Printer);
} finally {
renderer.EndDrawing();
pdfWriter.EndWrite();

SetRenderer(prevRenderer);
this.SetScale(prevScale);
}

return;
}

Expand Down
Loading

0 comments on commit fc5ae6c

Please sign in to comment.