From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Visuwesh Newsgroups: gmane.emacs.bugs Subject: bug#62713: 29.0.60; DocView shows wrong conversion indicator when configured to use svg images Date: Mon, 25 Sep 2023 18:17:01 +0530 Message-ID: <87edimmncq.fsf@gmail.com> References: <87v8i7llz9.fsf@no.lan> <87ttriri1b.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10030"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 62713@debbugs.gnu.org, Gregor Zattler To: Stefan Kangas Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Sep 25 15:04:50 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qklGc-0002Ou-Ft for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 25 Sep 2023 15:04:50 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qkl0C-00060M-FU; Mon, 25 Sep 2023 08:47:52 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qkl0A-0005zl-Kn for bug-gnu-emacs@gnu.org; Mon, 25 Sep 2023 08:47:50 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qkl0A-0000gs-C8 for bug-gnu-emacs@gnu.org; Mon, 25 Sep 2023 08:47:50 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qkl0M-0008HM-Eb for bug-gnu-emacs@gnu.org; Mon, 25 Sep 2023 08:48:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Visuwesh Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 25 Sep 2023 12:48:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 62713 X-GNU-PR-Package: emacs Original-Received: via spool by 62713-submit@debbugs.gnu.org id=B62713.169564605031717 (code B ref 62713); Mon, 25 Sep 2023 12:48:02 +0000 Original-Received: (at 62713) by debbugs.gnu.org; 25 Sep 2023 12:47:30 +0000 Original-Received: from localhost ([127.0.0.1]:44538 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkkzp-0008FV-U9 for submit@debbugs.gnu.org; Mon, 25 Sep 2023 08:47:30 -0400 Original-Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]:53268) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkkzk-0008FE-J6 for 62713@debbugs.gnu.org; Mon, 25 Sep 2023 08:47:28 -0400 Original-Received: by mail-pl1-x642.google.com with SMTP id d9443c01a7336-1c0ecb9a075so41221355ad.2 for <62713@debbugs.gnu.org>; Mon, 25 Sep 2023 05:47:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695646026; x=1696250826; darn=debbugs.gnu.org; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=CRzbZ9VPB7SwbitgDycRT55E8KTk9Re6iBMbJ4ymxgs=; b=MkglgepamT2EegOvR/iOY3WbfOtnLMNqCws99VmSxKinjl+oIi26vwDDugtGezPuxc Gf9nOzdTMCc1lgYsdhsgnEsMrW/OMrqWTCI1r12GMQVLHZOjlk43bXD/DC7PIeaYjYIb r5zSTHEU8q2G7x/nNg3SbTXFVtj8hgrVo9MAsKOOmcaAZRcRm3XqVIGtKCtMCCrtRDxE 9jc1F0wt+Ah+le0xF6/wvLkvKhyzYGMZ3VS5Y1uL37lqf2SSU87QHXPcIkiN+XOVUnWi 1sq+fT1XhJRPHOYqoaHabd/a9iw7/0+DlmxHHMlKkNgEUpv1zxwUZYk6dVHAeW20luW6 BRzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695646026; x=1696250826; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=CRzbZ9VPB7SwbitgDycRT55E8KTk9Re6iBMbJ4ymxgs=; b=IisI1/mjz2Ut5vecA/8u2JgcpXyJscWZcnM4FjtNu1DzKqMaPjgxG7YLT2lGnv8CJu 95vtuVYsLG4YTmq16Sb7psP9W7Bh3Y2cE/DTLWpRhyb/18ovWGo76LmoH45mlKiNCkhI IQJbG7j/6cVBOh7sL+FYJCXokK+ujqMLqHTTRWVvfQJnPgygTzriZiGXT0CVcjvFkE4A 3dJG+vd+fyYB1KhXuiSDB6K0UO4bn3RjjKq+SqNEraQsSt9sXWtlOkg+aH21btb+FdMo t+vLfCcUL0mxMmBBJwYvV0wGBxVj1pTjz0wdoNgrbbOqh6fAfaD/fqbTs1NSSmB47VJ8 1VDw== X-Gm-Message-State: AOJu0YwDn6VllvpWHNnjKhtLHwZFvijoKAdZ1GSw2Xtg+LbjZB+TaBSx uNQoY1Y8e99fU7YeVd5Khxc= X-Google-Smtp-Source: AGHT+IE2Rjr16XSu8hhnJVQvw4rfhVjcGQc63iR7cCsbP+NBsg2uPrnDWayJKqVt2xU4Ov2zVVIcGA== X-Received: by 2002:a17:902:c38d:b0:1bd:f69e:6630 with SMTP id g13-20020a170902c38d00b001bdf69e6630mr4429079plg.65.1695646025991; Mon, 25 Sep 2023 05:47:05 -0700 (PDT) Original-Received: from localhost ([115.240.90.130]) by smtp.gmail.com with ESMTPSA id u6-20020a170902b28600b001c61acd5bd2sm2117556plr.112.2023.09.25.05.47.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 05:47:05 -0700 (PDT) In-Reply-To: <87ttriri1b.fsf@gmail.com> (Visuwesh's message of "Mon, 25 Sep 2023 10:00:40 +0530") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:271330 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable [=E0=AE=A4=E0=AE=BF=E0=AE=99=E0=AF=8D=E0=AE=95=E0=AE=B3=E0=AF=8D =E0=AE=9A= =E0=AF=86=E0=AE=AA=E0=AF=8D=E0=AE=9F=E0=AE=AE=E0=AF=8D=E0=AE=AA=E0=AE=B0=E0= =AF=8D 25, 2023] Visuwesh wrote: > It is better solved by writing a separate pdf->svg converter function. > Right now, the svg conversion is tacked on top of pdf->png converter > function used for MuPDF. This was done because I thought only MuPDF > supported pdf->svg conversion but later I was informed that poppler has > utilities to do too. And looks like the poppler utility pdftocairo does not work the doc-view expects it to when generating svg images. AFAICT, there is no way to make pdftocairo to generate a svg image per page like other tools do. I cannot loop either as there is also no way to know the number of pages beforehand. The png function also has hacks since pdftocairo is not cooperative, and changing doc-view to not use doc-view--image-file-pattern does not seem to be easy. I attach the patch named "doc-view-general-svg-wip.patch" which has my WIP and it works just fine for MuPDF. This particular problem can be easily solved by the following simple patch instead: diff --git a/lisp/doc-view.el b/lisp/doc-view.el index b856b09c8b7..c0d27bdc68a 100644 --- a/lisp/doc-view.el +++ b/lisp/doc-view.el @@ -1283,7 +1283,8 @@ doc-view-pdf->png-converter-mupdf (expand-file-name doc-view-epub-user-stylesheet))))))) (doc-view-start-process - "pdf->png" doc-view-pdfdraw-program + (concat "pdf->" (symbol-name doc-view--image-type)) + doc-view-pdfdraw-program `(,@(doc-view-pdfdraw-program-subcommand) ,@options ,pdf But if you say we don't bother about pdftocairo support and implement the generic support system, I can prepare a patch without the pdftocairo stuff, and add some documentation to aid the users to write custom converter functions. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=doc-view-general-svg-wip.patch diff --git a/lisp/doc-view.el b/lisp/doc-view.el index b856b09c8b7..03b01de5033 100644 --- a/lisp/doc-view.el +++ b/lisp/doc-view.el @@ -193,28 +193,63 @@ doc-view-pdfdraw-program :type 'file :version "24.4") +(defcustom doc-view-poppler-program "pdftocairo" + "Name of pdfcairo program from Poppler." + :type 'file + :version "30.1") + (defcustom doc-view-pdftotext-program-args '("-raw") "Parameters to give to the pdftotext command." :version "27.1" :type '(repeat string)) (defcustom doc-view-pdf->png-converter-function - (if (executable-find doc-view-pdfdraw-program) - #'doc-view-pdf->png-converter-mupdf - #'doc-view-pdf->png-converter-ghostscript) + (cond + ((executable-find doc-view-poppler-program) + #'doc-view-pdf->png-converter-poppler) + ((executable-find doc-view-pdfdraw-program) + #'doc-view-pdf->png-converter-mupdf) + (t #'doc-view-pdf->png-converter-ghostscript)) "Function to call to convert a PDF file into a PNG file." :type '(radio (function-item doc-view-pdf->png-converter-ghostscript :doc "Use ghostscript") (function-item doc-view-pdf->png-converter-mupdf :doc "Use mupdf") + (function-item doc-view-pdf->png-converter-poppler + :doc "Use pdfcairo from poppler") function) :version "24.4") +(defcustom doc-view-pdf->svg-converter-function + (cond + ((executable-find doc-view-poppler-program) + #'doc-view-pdf->svg-converter-poppler) + ((executable-find doc-view-pdfdraw-program) + #'doc-view-pdf->svg-converter-mupdf) + (t #'doc-view-pdf->svg-converter-mupdf)) + "Function to call to convert a PDF file into a SVG file." + :type '(radio + (function-item doc-view-pdf->svg-converter-mupdf + :doc "Use mupdf") + (function-item doc-view-pdf->svg-converter-poppler + :doc "Use pdfcairo from poppler") + function) + :version "30.1") + +(defcustom doc-view-use-svg (image-type-available-p 'svg) + "Whether to use svg images for files whenever possible." + :type 'boolean + :version "30.1") + (defcustom doc-view-mupdf-use-svg (image-type-available-p 'svg) "Whether to use svg images for PDF files." :type 'boolean - :version "30.1") + :version "30.1" + :set (lambda (sym val) + (set-default-toplevel-value sym val) + (setq doc-view-use-svg val))) +(make-obsolete 'doc-view-mupdf-use-svg 'doc-view-use-svg "30.1") (defcustom doc-view-imenu-enabled (and (executable-find "mutool") t) "Whether to generate an imenu outline when \"mutool\" is available." @@ -241,7 +276,7 @@ doc-view-imenu-flatten (defface doc-view-svg-face '((t :inherit default)) "Face used for SVG images. Only background and foreground colors are used. -See `doc-view-mupdf-use-svg'." +See `doc-view-use-svg'." :version "30.1") (make-obsolete 'doc-view-svg-background 'doc-view-svg-face "30.1") @@ -1266,10 +1301,10 @@ doc-view-pdf-password-protected-pdfdraw-p (goto-char (point-min)) (search-forward "error: cannot authenticate password" nil t))) -(defun doc-view-pdf->png-converter-mupdf (pdf png page callback) +(defun doc-view-pdf->img-converter-mupdf (pdf type img page callback) (let* ((pdf-passwd (if (doc-view-pdf-password-protected-pdfdraw-p pdf) (read-passwd "Enter password for PDF file: "))) - (options `(,(concat "-o" png) + (options `(,(concat "-o" img) ,(format "-r%d" (round doc-view-resolution)) ,@(if pdf-passwd `("-p" ,pdf-passwd))))) (when (eq doc-view-doc-type 'epub) @@ -1283,13 +1318,74 @@ doc-view-pdf->png-converter-mupdf (expand-file-name doc-view-epub-user-stylesheet))))))) (doc-view-start-process - "pdf->png" doc-view-pdfdraw-program + (concat "pdf->" type) doc-view-pdfdraw-program `(,@(doc-view-pdfdraw-program-subcommand) ,@options ,pdf ,@(if page `(,(format "%d" page)))) callback))) +(defun doc-view-pdf->png-converter-mupdf (pdf img page callback) + (doc-view-pdf->img-converter-mupdf pdf "png" img page callback)) + +(defun doc-view-pdf->svg-converter-mupdf (pdf img page callback) + (doc-view-pdf->img-converter-mupdf pdf "svg" img page callback)) + +(defun doc-view-pdf-password-protected-poppler-p (pdf) + "Return non-nil if a PDF file is password-protected." + (with-temp-buffer + (let* ((tmp (make-temp-name "emacs-doc-view-test")) + (abs (expand-file-name (concat tmp ".png") temporary-file-directory))) + (call-process doc-view-poppler-program nil (current-buffer) nil + "-png" "-f" "1" "-singlefile" pdf abs) + (when (file-regular-p abs) + (delete-file abs)) + (goto-char (point-min)) + (search-forward "Incorrect password" nil t)))) + +(defun doc-view-pdf->png-converter-poppler (pdf imgfile page callback) + (let ((passwd (when (doc-view-pdf-password-protected-poppler-p pdf) + (read-passwd "Enter password for PDF file: ")))) + ;; HACK: pdftocairo doesn't accept format arguments in the image + ;; filename argument for PNG, JPEG, TIFF, and doc-view relies on a + ;; valid `doc-view--image-file-pattern' that can be passed to + ;; `format' to change pages in `doc-view-goto-page'. But + ;; thankfully, when PAGE is nil it means to fetch all pages. + (when (and (null page) (string-match-p "%d" imgfile)) + (setq imgfile (string-remove-suffix + (concat "-%d." (symbol-name doc-view--image-type)) + imgfile))) + (doc-view-start-process + "pdf->png" + doc-view-poppler-program + `("-png" "-r" ,(format "%d" (round doc-view-resolution)) + ,@(when passwd `("-opw" ,passwd)) + ,pdf + ,@(if page ; Single page. + (list "-f" (format "%d" page) "-singlefile" + (file-name-sans-extension imgfile)) + (list imgfile))) + callback))) + +(defun doc-view-pdf->svg-converter-poppler (pdf imgfile page callback) + (let ((passwd (when (doc-view-pdf-password-protected-poppler-p pdf) + (read-passwd "Enter password for PDF file: ")))) + ;; For vector formats such as svg, output-file name is handled + ;; differently. *Sigh* + ;; There seems to be no way to generate separate image per-page so + ;; we give up? + (unless (null page) + (doc-view-start-process + "pdf->svg" + doc-view-poppler-program + `("-svg" "-r" ,(format "%d" (round doc-view-resolution)) + ,@(when passwd `("-opw" ,passwd)) + ,pdf + ;; -singlefile is not supported for svg... + "-f" ,(format "%d" page) "-l" ,(format "%d" page) + ,imgfile) + callback)))) + (defun doc-view-odf->pdf-converter-unoconv (odf callback) "Convert ODF to PDF asynchronously and call CALLBACK when finished. The converted PDF is put into the current cache directory, and it @@ -1329,7 +1425,9 @@ doc-view-pdf/ps->png (funcall (pcase doc-view-doc-type ((or 'pdf 'odf 'epub 'cbz 'fb2 'xps 'oxps) - doc-view-pdf->png-converter-function) + (if doc-view-use-svg + doc-view-pdf->svg-converter-function + doc-view-pdf->png-converter-function)) ('djvu #'doc-view-djvu->tiff-converter-ddjvu) (_ #'doc-view-ps->png-converter-ghostscript)) pdf-ps png nil @@ -2148,10 +2246,8 @@ doc-view-set-up-single-converter (pcase-let ((`(,conv-function ,type ,extension) (pcase doc-view-doc-type ('djvu (list #'doc-view-djvu->tiff-converter-ddjvu 'tiff "tif")) - (_ (if (and (eq doc-view-pdf->png-converter-function - #'doc-view-pdf->png-converter-mupdf) - doc-view-mupdf-use-svg) - (list doc-view-pdf->png-converter-function 'svg "svg") + (_ (if doc-view-use-svg + (list doc-view-pdf->svg-converter-function 'svg "svg") (list doc-view-pdf->png-converter-function 'png "png")))))) (setq-local doc-view-single-page-converter-function conv-function) (setq-local doc-view--image-type type) --=-=-=--