From cf40dd38beac2521635e7379cdc5cb857d63d258 Mon Sep 17 00:00:00 2001
From: Yavor Doganov <yavor@gnu.org>
Date: Fri, 13 Jul 2018 18:17:54 +0300
Subject: [PATCH] Miscellaneous printing fixes

---
 ChangeLog                 | 13 +++++++++++++
 Source/NSPageLayout.m     |  8 ++------
 Source/NSPrintInfo.m      | 13 ++++++++-----
 Source/NSPrintOperation.m |  6 ++++++
 Source/NSPrinter.m        |  3 +++
 5 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 233d25ea6..bd5f73686 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2018-07-13  Yavor Doganov  <yavor@gnu.org>
+
+	* Source/NSPrintOperation.m (_print): If there is only one page to
+	print, skip any adjustment/calculation of page rects.
+	* Source/NSPrintInfo.m (initWithDictionary:): Set default margins
+	based on ImageableArea.
+	* Source/NSPageLayout.m
+	(tableView:objectValueForTableColumn:row:): With standard papers,
+	calculate margins only based on imageRect, taking into account
+	factorValue.
+	* Source/NSPrinter.m (addPPDKeyword:withScanner:withPPDPath:): Set
+	value to the empty string to avoid exception with some PPD files.
+
 2018-07-08 Ivan Vucica <ivan@vucica.net>
 
 	* Source/NSOpenGLView.m:
diff --git a/Source/NSPageLayout.m b/Source/NSPageLayout.m
index 5ddadbf13..4e9640fce 100644
--- a/Source/NSPageLayout.m
+++ b/Source/NSPageLayout.m
@@ -981,15 +981,11 @@ enum {
           if ([paperRadioMatrix selectedRow] == 0) //standard papers
             {
               NSRect imageRect;
-              NSSize paperSize;
 
               paperName = [standardPaperSizePopUp titleOfSelectedItem];
-              paperSize = [printer pageSizeForPaper: paperName];
               imageRect = [printer imageRectForPaper: paperName];
-              topMargin    = paperSize.height - imageRect.size.height;
-              bottomMargin = imageRect.origin.x;
-              leftMargin   = imageRect.origin.y;
-              rightMargin  = paperSize.width - imageRect.size.width;
+              topMargin = bottomMargin = imageRect.origin.x * factorValue;
+              leftMargin = rightMargin = imageRect.origin.y * factorValue;
             }
           else  //Custom Papers
             {
diff --git a/Source/NSPrintInfo.m b/Source/NSPrintInfo.m
index a682d1f28..2f6830d0f 100644
--- a/Source/NSPrintInfo.m
+++ b/Source/NSPrintInfo.m
@@ -143,6 +143,7 @@ static NSPrintInfo *sharedPrintInfo = nil;
 {
   NSPrinter *printer;
   NSString *pageSize;
+  NSRect imageRect;
 
   if (!(self = [super init]))
     {
@@ -171,12 +172,14 @@ static NSPrintInfo *sharedPrintInfo = nil;
     pageSize = @"A4";
   
   [self setPaperName: pageSize];
+
+  imageRect = [printer imageRectForPaper: pageSize];
   
-  /* Set default margins. FIXME: Probably should check ImageableArea */
-  [self setRightMargin: 36];
-  [self setLeftMargin: 36];
-  [self setTopMargin: 72];
-  [self setBottomMargin: 72];
+  /* Set default margins.  */
+  [self setRightMargin: imageRect.origin.y];
+  [self setLeftMargin: imageRect.origin.y];
+  [self setTopMargin: imageRect.origin.x];
+  [self setBottomMargin: imageRect.origin.x];
   [self setOrientation: NSPortraitOrientation];  
   
   if (aDict != nil)
diff --git a/Source/NSPrintOperation.m b/Source/NSPrintOperation.m
index 4b3de59a1..02dfb3955 100644
--- a/Source/NSPrintOperation.m
+++ b/Source/NSPrintOperation.m
@@ -1044,6 +1044,12 @@ scaleRect(NSRect rect, double scale)
         {
           pageRect = [_view rectForPage: _currentPage];
         }
+      else if (info.first == info.last && _currentPage == 1)
+        {
+          pageRect = NSMakeRect(0.0, 0.0,
+                                NSWidth(info.paperBounds),
+                                NSHeight(info.paperBounds));
+        }
       else
         {
           if (dir < 0)
diff --git a/Source/NSPrinter.m b/Source/NSPrinter.m
index 11d63920a..1fcc6ce0c 100644
--- a/Source/NSPrinter.m
+++ b/Source/NSPrinter.m
@@ -1159,6 +1159,9 @@ static NSMutableDictionary* printerCache;
       // Otherwise, scan up to the end of line or '/'
       [ppdData scanUpToCharactersFromSet: valueEndSet 
                               intoString: &value];
+
+      if (!value)
+        value = @"";
     }
   // If there is a value translation, scan it
   if ([ppdData scanString: @"/" 
-- 
2.18.0

