Skip to content

Commit

Permalink
Implemented the feature to save trees to PDF files (fix #574)
Browse files Browse the repository at this point in the history
  • Loading branch information
Serg-Norseman committed Aug 28, 2024
1 parent b7ec656 commit 57b5753
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 36 deletions.
1 change: 1 addition & 0 deletions locales/help_enu/gkhHistory.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>Change log</h1>

<p>
<b>??.??.2024 [v2.32.0 &amp; v3.8.0]</b><ul>
<li>Added the feature to save trees to PDF files.
<li>Supplemented citations (links) to repositories to support source call numbers.
<li>Added partial (local) views of record lists.
<li>Added output of portraits to pedigrees.
Expand Down
1 change: 1 addition & 0 deletions locales/help_rus/gkhHistory.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>История версий</h1>

<p>
<b>??.??.2024 [v2.32.0 &amp; v3.8.0]</b><ul>
<li>Добавлена возможность сохранения диаграмм деревьев в PDF-файлы.
<li>Дополнены цитаты (ссылки) на архивы для поддержки учетных номеров источников.
<li>Добавлены частные представления списков записей.
<li>Добавлен вывод портретов в родословные росписи.
Expand Down
4 changes: 0 additions & 4 deletions projects/GKCore/GKCore/Controllers/TreeChartWinController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* 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 @@ -279,9 +277,7 @@ 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)) {
Expand Down
10 changes: 8 additions & 2 deletions projects/GKCore/GKCore/Export/PDFRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,21 @@ namespace GKCore.Export
public sealed class PDFRenderer : ChartRenderer
{
private PdfContentByte fCanvas;
private readonly float fPageHeight;
private readonly float fPageWidth;
private float fPageHeight;
private float fPageWidth;

public PDFRenderer(float pageWidth, float pageHeight)
{
fPageHeight = pageHeight;
fPageWidth = pageWidth;
}

public void SetPageSize(ExtSize size)
{
fPageHeight = size.Height;
fPageWidth = size.Width;
}

public override void SetTarget(object target)
{
PdfContentByte gfx = target as PdfContentByte;
Expand Down
8 changes: 8 additions & 0 deletions projects/GKCore/GKCore/Export/PDFWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ public override ExtRectF GetPageSize()
return ExtRectF.Create(fDocument.Left, fDocument.Bottom, fDocument.Right, fDocument.Top);
}

public void SetPageSize(ExtSize size)
{
itRectangle pageRect = new itRectangle(0, 0, size.Height, size.Width);
pageRect = pageRect.Rotate();
fDocument.SetPageSize(pageRect);
fPdfWriter.SetPageSize(pageRect);
}

public override void BeginWrite()
{
itRectangle pageSize;
Expand Down
60 changes: 34 additions & 26 deletions projects/GKv2/GKComponents/GKUI/Components/CustomChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,32 +250,7 @@ public void SaveSnapshot(string fileName)

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);
}

RenderPDF(fileName);
return;
}

Expand Down Expand Up @@ -322,6 +297,39 @@ public void SaveSnapshot(string fileName)
}
}

private void RenderPDF(string fileName)
{
var prevRenderer = fRenderer;
var prevScale = this.Scale;

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

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

this.SetScale(1.0f);
var imageSize = GetImageSize();
pdfWriter.SetPageSize(imageSize);
((PDFRenderer)renderer).SetPageSize(imageSize);

// It is necessary to recreate the document with new page parameters
// (the first one will not be saved by default, because it is empty).
pdfWriter.NewPage();

renderer.BeginDrawing();
try {
RenderImage(RenderTarget.Printer);
} finally {
renderer.EndDrawing();
pdfWriter.EndWrite();

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

public virtual void SetRenderer(ChartRenderer renderer)
{
fRenderer = renderer;
Expand Down
4 changes: 2 additions & 2 deletions projects/GKv3/GEDKeeper3/GKUI/Components/TreeChartBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public TreeChartOptions Options
}
}

public float Scale
public override float Scale
{
get { return fModel.Scale; }
}
Expand Down Expand Up @@ -203,7 +203,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
47 changes: 47 additions & 0 deletions projects/GKv3/GKComponents/GKUI/Components/CustomChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 @@ -39,6 +41,11 @@ public abstract class CustomChart : ScrollablePanel, IPrintable

public event EventHandler NavRefresh;

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


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

public virtual void SetScale(float value)
{
}

protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.Key) {
Expand Down Expand Up @@ -174,6 +185,9 @@ public void SaveSnapshot(string fileName)
SetRenderer(prevRenderer);
}

return;
} else if (ext == ".pdf") {
RenderPDF(fileName);
return;
}

Expand Down Expand Up @@ -214,6 +228,39 @@ public void SaveSnapshot(string fileName)
}
}

private void RenderPDF(string fileName)
{
var prevRenderer = fRenderer;
var prevScale = this.Scale;

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

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

this.SetScale(1.0f);
var imageSize = GetImageSize();
pdfWriter.SetPageSize(imageSize);
((PDFRenderer)renderer).SetPageSize(imageSize);

// It is necessary to recreate the document with new page parameters
// (the first one will not be saved by default, because it is empty).
pdfWriter.NewPage();

renderer.BeginDrawing();
try {
RenderImage(RenderTarget.Printer);
} finally {
renderer.EndDrawing();
pdfWriter.EndWrite();

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

public virtual void SetRenderer(ChartRenderer renderer)
{
fRenderer = renderer;
Expand Down
47 changes: 47 additions & 0 deletions projects/GKvX/GEDKeeperX/GKUI/Components/CustomChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using BSLib;
using GKCore.Charts;
using GKCore.Design.Graphics;
using GKCore.Export;
using GKCore.Types;

namespace GKUI.Components
{
Expand All @@ -35,12 +37,21 @@ public abstract class CustomChart : ScrollablePanel, IPrintable

public event EventHandler NavRefresh;

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


protected CustomChart()
{
fNavman = new NavigationStack<object>();
}

public virtual void SetScale(float value)
{
}

#region Print and snaphots support

public abstract ExtSize GetImageSize();
Expand Down Expand Up @@ -78,6 +89,9 @@ public void SaveSnapshot(string fileName)
SetRenderer(prevRenderer);
}
return;
} else if (ext == ".pdf") {
RenderPDF(fileName);
return;
}
Expand Down Expand Up @@ -116,6 +130,39 @@ public void SaveSnapshot(string fileName)
}*/
}

private void RenderPDF(string fileName)
{
var prevRenderer = fRenderer;
var prevScale = this.Scale;

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

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

this.SetScale(1.0f);
var imageSize = GetImageSize();
pdfWriter.SetPageSize(imageSize);
((PDFRenderer)renderer).SetPageSize(imageSize);

// It is necessary to recreate the document with new page parameters
// (the first one will not be saved by default, because it is empty).
pdfWriter.NewPage();

renderer.BeginDrawing();
try {
RenderImage(RenderTarget.Printer);
} finally {
renderer.EndDrawing();
pdfWriter.EndWrite();

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

public virtual void SetRenderer(ChartRenderer renderer)
{
fRenderer = renderer;
Expand Down
4 changes: 2 additions & 2 deletions projects/GKvX/GEDKeeperX/GKUI/Components/TreeChartBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public TreeChartOptions Options
}
}

public new float Scale
public override float Scale
{
get { return fModel.Scale; }
}
Expand Down Expand Up @@ -221,7 +221,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

0 comments on commit 57b5753

Please sign in to comment.