View Issue Details

IDProjectCategoryView StatusLast Update
0038350PackagesPackagespublic2021-04-07 11:40
Reporterjoellinn Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityhave not tried
Status resolvedResolutionfixed 
Summary0038350: [PATCH] FPReport: Fixes for canvas and print export
Description- Make different font and font styles work in TCanvas exporter
- Correct pen (line) width for different canvas DPIs
- Offset the canvas origin so that it converges with the page origin (and not the printable area origin)
TagsNo tags attached.
Fixed in Revision64939
LazTarget-
Widgetset
Attached Files

Activities

joellinn

2021-01-13 10:49

reporter  

0001-FPReport-Properly-translate-the-PostScript-font-name.patch (1,716 bytes)   
From fae137cdb92fde477f4d3a919853f0f96cfcde30 Mon Sep 17 00:00:00 2001
From: Joel Linn <jl@conductive.de>
Date: Tue, 12 Jan 2021 13:04:38 +0100
Subject: [PATCH 1/3] FPReport: Properly translate the PostScript font name
 back to the family name in the lcl export

---
 components/fpreport/fpreportlclexport.pas | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/components/fpreport/fpreportlclexport.pas b/components/fpreport/fpreportlclexport.pas
index d7ecc6e1e1..dbc0e95751 100644
--- a/components/fpreport/fpreportlclexport.pas
+++ b/components/fpreport/fpreportlclexport.pas
@@ -177,6 +177,7 @@ const
 implementation
 
 uses
+  fpTTF,
   fpwritepng,
   math;
 
@@ -456,6 +457,8 @@ Type
 function TFPReportExportCanvas.GetFont(const AFontName: String): TFont;
 
 Var
+  fontCached : TFPFontCacheItem;   
+  fontStyles : TFontStyles;
   ftFont : TFont;
 
 begin
@@ -465,6 +468,19 @@ begin
     begin
     ftFont:=TFont.create;
     ftFont.Name:=AFontName;
+    fontCached := gTTFontCache.Find(AFontName);
+    if Assigned(fontCached) then
+      begin
+      // This still requires that the Font is available to the lcl back-end,
+      // custom fpTTF fonts are not implicitly available. E.g. on Windows a
+      // custom font would require the use of AddFontMemResourceEx() to
+      // make it available to GDI (and thus lcl Canvas).
+      ftFont.Name := fontCached.FamilyName;
+      fontStyles := [];
+      if fontCached.IsBold then Include(fontStyles, TFontStyle.fsBold);
+      if fontCached.IsItalic then Include(fontStyles, TFontStyle.fsItalic);
+      ftFont.Style := fontStyles;
+      end;  
     Result:=ftFont;
     FFonts.Add(AFontName,Result);
     end;
-- 
2.29.2.windows.1

0002-FPReport-Correctly-set-pen-width-in-the-lcl-export.patch (2,594 bytes)   
From e1943c8c3d43a77db96a5c0c644dadccdfc82d9d Mon Sep 17 00:00:00 2001
From: Joel Linn <jl@conductive.de>
Date: Wed, 13 Jan 2021 01:06:32 +0100
Subject: [PATCH 2/3] FPReport: Correctly set pen width in the lcl export

---
 components/fpreport/fpreportlclexport.pas | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/components/fpreport/fpreportlclexport.pas b/components/fpreport/fpreportlclexport.pas
index dbc0e95751..8705a62f48 100644
--- a/components/fpreport/fpreportlclexport.pas
+++ b/components/fpreport/fpreportlclexport.pas
@@ -143,6 +143,7 @@ type
     function    CoordToRect(const APos: TFPReportPoint; const AWidth: TFPReportUnits=0; const AHeight: TFPReportUnits=0): TRect;
     function    HmmToPixels(const AValue: TFPReportUnits): Integer;
     function    VmmToPixels(const AValue: TFPReportUnits): Integer;
+    function    PtToPixels(const AValue: Integer): Integer;
     Function    GetPageRect(APage : TFPReportCustomPage; WithoutMargin : Boolean = False)  : TRect;
     Function    GetBandRect(L : TFPReportLayout;IncludeHandle: Boolean) : TRect;
     Function    GetBandRect(ABand : TFPReportCustomBand; IncludeHandle : Boolean) : TRect;
@@ -172,6 +173,7 @@ type
 
 const
   cInchToMM = 25.4;
+  cPtToDPI = 72;
   RGBA_Width = 4;
 
 implementation
@@ -281,6 +283,15 @@ begin
   Result := Round(AValue * (VDPI * Zoom/ cInchToMM));
 end;
 
+function TFPReportExportCanvas.PtToPixels(const AValue: Integer): Integer;
+begin
+  // This is used for line widths and ideally should be individually 
+  // calculated for every line angle as HDPI and VDPI differ on some
+  // printers. They do not differ greatly though (usually factor 2)
+  // so we get away with an average to keep things simple.
+  Result := Round(AValue * (((HDPI + VDPI) / 2) * Zoom / cPtToDPI));
+end;
+
 procedure TFPReportExportCanvas.SetupPageRender(const APage: TFPReportPage);
 
 begin
@@ -385,7 +396,7 @@ begin
   begin
     Canvas.Pen.Style:=AFrame.Pen;
     Canvas.Pen.Color:= RGBtoBGR(AFrame.Color);
-    Canvas.Pen.Width:=AFrame.Width;
+    Canvas.Pen.Width:=PtToPixels(AFrame.Width);
   end;
   {$IFDEF DEBUGRD}
   Writeln('Rendering frame [',AFrame.Shape,'] (',ARect.Left,',',ARect.Top,',',ARect.right,',',ARect.Bottom,') : ',(bStroke or bFill));
@@ -780,7 +791,7 @@ begin
     exit;
   Canvas.Pen.Color:=TFPReportShape(AShape).Color;
   Canvas.Pen.Style:=psSolid;
-  Canvas.Pen.Width:=1;
+  Canvas.Pen.Width:=PtToPixels(1);
   lPt1.Left := BL.Left + SL.Left;
   lPt1.Top := BL.Top + SL.Top;
   case TFPReportShape(AShape).ShapeType of
-- 
2.29.2.windows.1

0003-FPReport-Ignore-printer-sprecific-borders.patch (1,087 bytes)   
From a356c1caadb5aa19712cb97c847b0a1159bce0a5 Mon Sep 17 00:00:00 2001
From: Joel Linn <jl@conductive.de>
Date: Wed, 13 Jan 2021 01:19:08 +0100
Subject: [PATCH 3/3] FPReport: Ignore printer sprecific borders

---
 components/fpreport/fpreportprinterexport.pas | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/components/fpreport/fpreportprinterexport.pas b/components/fpreport/fpreportprinterexport.pas
index d53ae53a10..1f1bfcd087 100644
--- a/components/fpreport/fpreportprinterexport.pas
+++ b/components/fpreport/fpreportprinterexport.pas
@@ -110,6 +110,7 @@ Var
   E : TFPReportExportCanvas;
   I : Integer;
   First : Boolean;
+  WorkRect : TRect;
 
 begin
   if ShowPrinterDialog then
@@ -124,6 +125,10 @@ begin
     E.Report:=Self.Report;
     E.HDPI:=P.XDPI;
     E.VDPI:=P.YDPI;
+    // Ignore printer specific borders
+    WorkRect := P.PaperSize.PaperRect.WorkRect;
+    E.HorzOffset:=-WorkRect.Left;
+    E.VertOffset:=-WorkRect.Top; 
     First:=true;
     For I:=0 to ARTObjects.Count-1 do
       if MustPrintPage(I+1) and Not P.Aborted then
-- 
2.29.2.windows.1

joellinn

2021-01-14 13:26

reporter   ~0128316

There is still an issue though with some fonts not displaying as bold, that I could not solve.
Like "UbuntuMono-Bold" which is displayed as "UbuntuMono" instead.

joellinn

2021-04-06 20:34

reporter   ~0130141

Has this been reviewed in the meantime?
I've been using the patches in production since February 👍

Michael Van Canneyt

2021-04-07 11:40

administrator   ~0130151

Checked applied & committed, thanks for the patches.

Issue History

Date Modified Username Field Change
2021-01-13 10:49 joellinn New Issue
2021-01-13 10:49 joellinn File Added: 0001-FPReport-Properly-translate-the-PostScript-font-name.patch
2021-01-13 10:49 joellinn File Added: 0002-FPReport-Correctly-set-pen-width-in-the-lcl-export.patch
2021-01-13 10:49 joellinn File Added: 0003-FPReport-Ignore-printer-sprecific-borders.patch
2021-01-13 10:55 Michael Van Canneyt Assigned To => Michael Van Canneyt
2021-01-13 10:55 Michael Van Canneyt Status new => assigned
2021-01-14 13:26 joellinn Note Added: 0128316
2021-04-06 20:34 joellinn Note Added: 0130141
2021-04-07 11:40 Michael Van Canneyt Status assigned => resolved
2021-04-07 11:40 Michael Van Canneyt Resolution open => fixed
2021-04-07 11:40 Michael Van Canneyt Fixed in Revision => 64939
2021-04-07 11:40 Michael Van Canneyt LazTarget => -
2021-04-07 11:40 Michael Van Canneyt Note Added: 0130151