View Issue Details

IDProjectCategoryView StatusLast Update
0037682LazarusOtherpublic2020-11-15 18:52
ReporterBob Richardson Assigned ToJuha Manninen  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
OSWindows 
Summary0037682: Printer.PageNumber initialization error
DescriptionIn Printer.BeginDoc, fPageNumber is set to 1. It is immediately increased to 2 when BeginPage is executed. Thus the very first page number is 2, not 1. This is easily fixed by initializing fPageNumber to 0, not 1.

Works for me, although I'm not sure if there are other ramifications I don't know about.

Perhaps very few use PageNumber in the Printer object.
TagsNo tags attached.
Fixed in Revisionr64072, r64104
LazTarget-
Widgetset
Attached Files

Relationships

related to 0038027 closedJuha Manninen [Patch] Unix Print Dialogs (Printer4Lazarus) 

Activities

Bart Broersma

2020-09-02 00:43

developer   ~0125288

Isn't this Lazarus?

jamie philbrook

2020-09-02 12:42

reporter   ~0125292

Well if being in the LCL\printers.pas means this I guess I would be...

Fixable on the Lazarus side but maybe someone could fix the heading to correct the post but in any case I think it should be fixed.

Bob Richardson

2020-09-02 16:47

reporter   ~0125296

I seem to have misfiled this report. The error is with Lazarus, not FPC.

Juha Manninen

2020-10-25 09:25

developer   ~0126533

I changed it in r64072.
Please also test TPostScript in unit PostScriptPrinter. It initialized fPageNumber in many places.

Joeny Ang

2020-11-04 07:19

reporter   ~0126718

Hi Juha, TPostScript is a separate class not inherited from TPrinter and it handles updating fPageNumber well. I think it is best that we pattern TPrinter after it. Initialize fPageNumber to 1 in BeginDoc() and increment it in NewPage() instead of doing it in BeginPage().

Same applies to TPrinterCanvas class. It also has a fPageNum that is being incremented inside BeginPage(). In one of its descendant, TCairoPrinterCanvas, the inherited BeginPage() is not being called, thus fPageNum is not being incremented. Now, if we make BeginPage() call its inherited method, the resulting fPageNum will be wrong (greater), since TPrinterCanvas.BeginPage() is also called by TPrinter.BeginPage().

Attached is a patch containing the proposed changes.

Browsed through the code (not tested :) of the following and I think these will not be affected by the change, except for TCairoPrinterCanvas where a call to its inherited method was added to NewPage().

Classes derived from TPrinter: TWinPrinter, TCUPSPrinter, TCarbonPRinter, TQtPrinter
Classes derived from TPrinterCanvas: TFilePrinterCanvas, TCocoaPrinterCanvas, TPostScriptPrinterCanvas, TCairoPrinterCanvas and descendants
printer-pagenumber-init.patch (1,319 bytes)   
--- components/cairocanvas/cairocanvas.pas.64102
+++ components/cairocanvas/cairocanvas.pas
@@ -362,6 +362,7 @@
 
 procedure TCairoPrinterCanvas.NewPage;
 begin
+  inherited NewPage;
   EndPage;
   BeginPage;
 end;
--- lcl/printers.pas.64102
+++ lcl/printers.pas
@@ -376,7 +376,7 @@
 
   Include(fFlags, pfPrinting);
   Exclude(fFlags, pfAborted);
-  fPageNumber := 0;
+  fPageNumber := 1;
 
   if not RawMode then begin
     Canvas.Refresh;
@@ -414,6 +414,7 @@
 //Create an new page
 procedure TPrinter.NewPage;
 begin
+  Inc(fPageNumber);
   if TMethod(@Self.DoNewPage).Code = Pointer(@TPrinter.DoNewPage) then
   begin
     // DoNewPage has not been overriden, use the new method
@@ -423,7 +424,6 @@
   begin
     // Use the old method as DoNewPage has been overriden in descendat TPrinter
     CheckPrinting(True);
-    Inc(fPageNumber);
     if not RawMode then
       TPrinterCanvas(Canvas).NewPage;
     DoNewPage;
@@ -433,7 +433,6 @@
 procedure TPrinter.BeginPage;
 begin
   CheckPrinting(True);
-  inc(fPageNumber);
   if not RawMode then
     TPrinterCanvas(Canvas).BeginPage;
   DoBeginPage;
@@ -1397,12 +1396,12 @@
 
 procedure TPrinterCanvas.NewPage;
 begin
-  BeginPage;
+  Inc(fPageNum);
 end;
 
 procedure TPrinterCanvas.BeginPage;
 begin
-  Inc(fPageNum);
+
 end;
 
 procedure TPrinterCanvas.EndPage;

printer-pagenumber-init.patch (1,319 bytes)   

Joeny Ang

2020-11-04 07:33

reporter   ~0126719

Update: Restored TPrinterCanvas.NewPage() call to BeginPage()
printer-pagenumber-init-v2.patch (1,319 bytes)   
--- components/cairocanvas/cairocanvas.pas.64102
+++ components/cairocanvas/cairocanvas.pas
@@ -362,6 +362,7 @@
 
 procedure TCairoPrinterCanvas.NewPage;
 begin
+  inherited NewPage;
   EndPage;
   BeginPage;
 end;
--- lcl/printers.pas.64102
+++ lcl/printers.pas
@@ -376,7 +376,7 @@
 
   Include(fFlags, pfPrinting);
   Exclude(fFlags, pfAborted);
-  fPageNumber := 0;
+  fPageNumber := 1;
 
   if not RawMode then begin
     Canvas.Refresh;
@@ -414,6 +414,7 @@
 //Create an new page
 procedure TPrinter.NewPage;
 begin
+  Inc(fPageNumber);
   if TMethod(@Self.DoNewPage).Code = Pointer(@TPrinter.DoNewPage) then
   begin
     // DoNewPage has not been overriden, use the new method
@@ -423,7 +424,6 @@
   begin
     // Use the old method as DoNewPage has been overriden in descendat TPrinter
     CheckPrinting(True);
-    Inc(fPageNumber);
     if not RawMode then
       TPrinterCanvas(Canvas).NewPage;
     DoNewPage;
@@ -433,7 +433,6 @@
 procedure TPrinter.BeginPage;
 begin
   CheckPrinting(True);
-  inc(fPageNumber);
   if not RawMode then
     TPrinterCanvas(Canvas).BeginPage;
   DoBeginPage;
@@ -1397,12 +1396,13 @@
 
 procedure TPrinterCanvas.NewPage;
 begin
+  Inc(fPageNum);
   BeginPage;
 end;
 
 procedure TPrinterCanvas.BeginPage;
 begin
-  Inc(fPageNum);
+
 end;
 
 procedure TPrinterCanvas.EndPage;

Juha Manninen

2020-11-04 18:50

developer   ~0126731

Thanks, I applied the last patch in r64104.
I will keep this open for a week or so. Maybe the reporter Bob Richardson wants to test and comment, too.

Juha Manninen

2020-11-15 18:52

developer   ~0126972

Resolving. I guess it works.

Issue History

Date Modified Username Field Change
2020-09-01 23:47 Bob Richardson New Issue
2020-09-02 00:43 Bart Broersma Note Added: 0125288
2020-09-02 12:42 jamie philbrook Note Added: 0125292
2020-09-02 16:47 Bob Richardson Note Added: 0125296
2020-09-02 19:02 Jonas Maebe Project FPC => Lazarus
2020-10-25 01:18 Juha Manninen Assigned To => Juha Manninen
2020-10-25 01:18 Juha Manninen Status new => assigned
2020-10-25 09:25 Juha Manninen Status assigned => feedback
2020-10-25 09:25 Juha Manninen LazTarget => -
2020-10-25 09:25 Juha Manninen Note Added: 0126533
2020-11-03 09:17 Juha Manninen Relationship added related to 0038027
2020-11-04 07:19 Joeny Ang Note Added: 0126718
2020-11-04 07:19 Joeny Ang File Added: printer-pagenumber-init.patch
2020-11-04 07:33 Joeny Ang Note Added: 0126719
2020-11-04 07:33 Joeny Ang File Added: printer-pagenumber-init-v2.patch
2020-11-04 18:50 Juha Manninen Note Added: 0126731
2020-11-04 18:50 Juha Manninen Product Version 3.2.0 =>
2020-11-04 18:50 Juha Manninen Fixed in Revision => r64072, r64104
2020-11-15 18:52 Juha Manninen Status feedback => resolved
2020-11-15 18:52 Juha Manninen Resolution open => fixed
2020-11-15 18:52 Juha Manninen Note Added: 0126972