branch: externals/auctex
commit b7083eeaca861322c3b5daa1400e2067bda033fb
Author: Ikumi Keita <ik...@ikumi.que.jp>
Commit: Ikumi Keita <ik...@ikumi.que.jp>

    Change color adjustment method (bug#35571, bug#35696, bug#36039)
    
    * preview.el.in (preview-pdf-color-adjust-method): New option.
    (preview-pdf-color-string): Use new code using DELAYBIND feature when
    the new option is t.
    (preview-gs-color-string): Add new optional argument to suppress code
    for explicit colors.
    (preview-gs-open): Don't adjust color when the latex command produces
    PDF and `preview-pdf-color-adjust-method' is nil.
    (preview-pdf2dsc-sentinel): Add "-dDELAYBIND" option to gs invocation
    when `preview-pdf-color-adjust-method' is t.
    * doc/changes.texi: Add a new entiry.
    * doc/preview-problems.texi: Add a section to explain how to do with
    the case no images are displayed with gs 9.27 and newer.
---
 doc/changes.texi          |  14 +++++-
 doc/preview-problems.texi |  57 +++++++++++++++++++++++
 preview.el.in             | 115 ++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 172 insertions(+), 14 deletions(-)

diff --git a/doc/changes.texi b/doc/changes.texi
index 8475ced..f99e62d 100644
--- a/doc/changes.texi
+++ b/doc/changes.texi
@@ -1,5 +1,5 @@
 @c This is part of the AUCTeX manual.
-@c Copyright (C) 1994-2002, 2004-2010, 2012-2017 Free Software
+@c Copyright (C) 1994-2002, 2004-2010, 2012-2019 Free Software
 @c Foundation, Inc.
 @c See file auctex.texi for copying conditions.
 @include macros.texi
@@ -12,6 +12,18 @@
 
 @itemize @bullet
 @item
+A new method is implemented in @previewlatex{} to adjust the foreground
+colors of generated images to those of Emacs, when the @LaTeX{} command
+produces @acronym{PDF}.  The traditional method became invalid because
+of the change introduced in Ghostscript 9.27.  Unfortunately, the new
+method doesn't work due to a bug in Ghostscript 9.27 and is valid only
+for Ghostscript > 9.27.  There is also a fallback method for gs 9.27
+users which displays plain ``black on white'' images.  For successful
+function of @previewlatex{}, the users are encouraged to set up the new
+user option @code{preview-pdf-color-adjust-method} to choose appropriate
+option among the three: new, traditional and fallback method.
+
+@item
 @AUCTeX{} has support for the Flymake package in Emacs 26 or newer.  To
 enable, call @kbd{M-x flymake-mode RET} or add this to your
 @file{.emacs} file:
diff --git a/doc/preview-problems.texi b/doc/preview-problems.texi
index 824892c..e10fdd7 100644
--- a/doc/preview-problems.texi
+++ b/doc/preview-problems.texi
@@ -19,6 +19,7 @@ newer versions of the problematic software or by simple 
patches.
 * Too small bounding boxes::    
 * x-symbol interoperation::     
 * Middle-clicks paste instead of toggling::  
+* No images are displayed with gs 9.27 and newer::
 @end menu
 
 If you find something not mentioned here, please send a bug report using
@@ -137,3 +138,59 @@ such a complicated mess that no patch is in sight.  Better 
just end the
 search with @kbd{@key{RET}} before toggling and resume with @kbd{C-s
 C-s} or similar afterwards.  Since previews over the current match will
 auto-open, anyway, this should not be much of a problem in practice.
+
+@node No images are displayed with gs 9.27 and newer
+@section No images are displayed with gs 9.27 and newer
+
+@previewlatex{} tries to adjust the foreground and background colors of
+generated images to those of Emacs.  Unfortunately, incompatible changes
+introduced in Ghostscript 9.27 breaks the traditional method partially,
+and @previewlatex{} can display no images under certain circumstances.
+
+A new method implemented alternatively works only with Ghostscript >
+9.27, so is disabled by default.  If you are using those new
+Ghostscript, customize the option
+@code{preview-pdf-adjust-color-method}.
+
+@defopt preview-pdf-adjust-color-method
+Method to adjust colors of images generated from @acronym{PDF}.  It is
+not consulted when the @LaTeX{} command produces @acronym{DVI} files.
+
+When the option is @code{t}, @previewlatex{} adjusts the FG and BG colors
+of the generated images by the new method.  This method requires that
+Ghostscript has working @code{DELAYBIND} feature, thus is invalid with
+gs 9.27 (and possibly < 9.27).
+
+When it is @code{compatible} (default), @previewlatex{} uses traditional
+method.  This option is provided for backward compatibility with older
+gs.  See the below explanation for detail.
+
+When @code{nil}, no adjustment is done and ``black on white'' image is
+generated regardless of Emacs color.  This is provided for fallback for
+gs 9.27 users with customized foreground color.  See the below
+explanation for detail.
+
+When the @LaTeX{} command produces @acronym{PDF} rather than
+@acronym{DVI} and Emacs has non-trivial foreground color, the
+traditional method (@code{compatible}) makes gs >= 9.27 to stop with
+error.  Here, ``non-trivial foreground color'' includes customized
+themes.
+
+If you use such non-trivial foreground color and the version of
+Ghostscript equals to 9.27, you have two options:
+@enumerate
+@item
+Choose the value @code{compatible} and customize
+@code{preview-reference-face} to have default (black) foreground color.
+This makes the generated image almost non-readable on dark background,
+so the next option would be your only choice in that case.
+@item
+Choose the value @code{nil}, which forces plain ``black on white''
+appearance for the generated image.  You can at least read what are
+written in the image although they may not match with your Emacs color
+well.
+@end enumerate
+
+The default value will be changed to @code{t} after Ghostscript 9.28 is
+released.
+@end defopt
diff --git a/preview.el.in b/preview.el.in
index 30bf45b..e107d5e 100644
--- a/preview.el.in
+++ b/preview.el.in
@@ -602,6 +602,52 @@ tag in the mode line."
       (setq preview-error-condition nil
            compilation-in-progress (delq process compilation-in-progress)))))
 
+(defcustom preview-pdf-color-adjust-method 'compatible
+  "Method to adjust colors of images generated from PDF.
+It is not consulted when the latex command produces DVI files.
+
+The valid values are:
+
+t: preview-latex transfers the foreground and background colors
+of Emacs to the generated images.  This option requires that
+Ghostscript has working DELAYBIND feature, thus is invalid with
+gs 9.27 (and possibly < 9.27).
+
+`compatible': preview-latex uses another mothod to transfer
+colors.  This option is provided for compatibility with older gs.
+See the below explanation for detail.
+
+nil: no adjustment is done and \"black on white\" image is
+generated regardless of Emacs color.  This is provided for fallback for
+gs 9.27 users with customized foreground color.  See the below
+explanation for detail.
+
+When the latex command produces PDF rather than DVI and Emacs has
+non-trivial foreground color, the traditional method (`compatible')
+makes gs >= 9.27 to stop with error.  Here, \"non-trivial foreground
+color\" includes customized themes.
+
+If you use such non-trivial foreground color and the version of
+Ghostscript equals to 9.27, you have two options:
+
+- Choose the value `compatible' and customize
+`preview-reference-face' to have default (black) foreground
+color.  This makes the generated image almost non-readable on
+dark background, so the next option would be your only choice in
+that case.
+- Choose the value nil, which forces plain \"black on white\"
+appearance for the generated image.  You can at least read what
+are written in the image although they may not match with your
+Emacs color well.
+
+The default value will be changed to t after Ghostscript 9.28 is
+released."
+  :group 'preview-appearance
+  :type '(choice
+         (const :tag "Adjust to Emacs color (gs > 9.27)" t)
+         (const :tag "Compatibility for gs =< 9.27" compatible)
+         (const :tag "No adjustment (B/W, for gs 9.27)" nil)))
+
 (defun preview-gs-sentinel (process string)
   "Sentinel function for rendering process.
 Gets the default PROCESS and STRING arguments
@@ -732,7 +778,19 @@ null eq{pop{pop}bind}if def\
 {pop}{setpagedevice}{ifelse exec}\
 stopped{handleerror quit}if \
 .preview-ST aload pop restore}bind def "
-                 (preview-gs-color-string preview-colors)))
+                 (preview-gs-color-string
+                  preview-colors
+                  ;; Compatibility for gs 9.27 with non-trivial
+                  ;; foreground color and dark background.
+                  ;; Suppress color adjustment with PDF backend
+                  ;; when `preview-pdf-color-adjust-method' is nil.
+                  (and (not preview-pdf-color-adjust-method)
+                       ;; The switch `preview-parsed-pdfoutput' isn't
+                       ;; set before parsing the latex output, so use
+                       ;; heuristic here.
+                       (with-current-buffer TeX-command-buffer
+                         (and TeX-PDF-mode
+                              (not TeX-PDF-from-DVI)))))))
     (preview-gs-queue-empty)
     (preview-parse-messages (or setup #'preview-gs-dvips-process-setup))))
 
@@ -744,21 +802,46 @@ to Ghostscript floats."
 
 (defun preview-pdf-color-string (colors)
   "Return a string that patches PDF foreground color to work properly."
-  ;; Actually, this is rather brutal.  It will only be invoked in
-  ;; cases, however, where previously it was not expected that
-  ;; anything readable turned up, anyway.
   (let ((fg (aref colors 1)))
     (if fg
-       (concat
-        "/GS_PDF_ProcSet GS_PDF_ProcSet dup maxlength dict copy dup begin\
+       (cond ((eq preview-pdf-color-adjust-method t)
+              ;; New code for gs > 9.27.
+              ;; This assumes DELAYBIND feature, which is known to be
+              ;; broken in gs 9.27 (and possibly, < 9.27).
+              ;; 
<URL:https://lists.gnu.org/archive/html/auctex-devel/2019-07/msg00000.html>
+              ;; DELAYBIND is sometimes mentioned in association with
+              ;; security holes in the changelog of Ghostscript:
+              ;; <URL:https://www.ghostscript.com/doc/9.27/History9.htm>
+              ;; Thus we might have to be prepared for removal of this
+              ;; feature in future Ghostscript.
+              (concat
+               "/initgraphics {
+  //initgraphics
+  /RG where {
+    pop "
+               (mapconcat #'preview-gs-color-value fg " ")
+               " 3 copy rg RG
+  } if
+} bind def .bindnow "))
+             ((eq preview-pdf-color-adjust-method 'compatible)
+              ;; Traditional code for gs < 9.27.
+              (concat
+               "/GS_PDF_ProcSet GS_PDF_ProcSet dup maxlength dict copy dup 
begin\
 /graphicsbeginpage{//graphicsbeginpage exec "
-        (mapconcat #'preview-gs-color-value fg " ")
-        " 3 copy rg RG}bind store end readonly store "))))
-
-(defun preview-gs-color-string (colors)
-  "Return a string setting up colors"
-  (let ((bg (aref colors 0))
-       (fg (aref colors 1))
+               (mapconcat #'preview-gs-color-value fg " ")
+               " 3 copy rg RG}bind store end readonly store "))
+             (;; Do nothing otherwise.
+              t
+              "")))))
+
+(defun preview-gs-color-string (colors &optional suppress-fgbg)
+  "Return a string setting up COLORS.
+If optional argument SUPPRESS-FGBG is non-nil, behave as if FG/BG
+colors were just the default value."
+  (let ((bg (and (not suppress-fgbg)
+                (aref colors 0)))
+       (fg (and (not suppress-fgbg)
+                (aref colors 1)))
        (mask (aref colors 2))
        (border (aref colors 3)))
     (concat
@@ -910,6 +993,12 @@ The usual PROCESS and COMMAND arguments for
        (cond ((eq status 'exit)
               (delete-process process)
               (setq TeX-sentinel-function nil)
+              ;; Add DELAYBIND option for adjustment of foreground
+              ;; color to work.
+              (if (eq preview-pdf-color-adjust-method t)
+                  (setq preview-gs-command-line (append
+                                                 preview-gs-command-line
+                                                 '("-dDELAYBIND"))))
               (setq preview-gs-init-string
                     (concat preview-gs-init-string
                             (preview-pdf-color-string preview-colors)))

_______________________________________________
auctex-diffs mailing list
auctex-di...@gnu.org
https://lists.gnu.org/mailman/listinfo/auctex-diffs

Reply via email to