emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: feng shu <tumashu@gmail.com>
To: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: [PATCH] [need test]Restruct `org-create-formula-image' function
Date: Sat, 20 Jul 2013 17:32:02 +0800	[thread overview]
Message-ID: <CAJpRBmcqRE=dYJOj7aNARK9y4PQc_748E_ktOgs6pGrTQQL7mw@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 146 bytes --]

`org-create-formula-image-with-dvipng' and
`org-create-formula-image-with-imagemagick'
share a good deal of logic, so combine them to a function.

[-- Attachment #1.2: Type: text/html, Size: 185 bytes --]

[-- Attachment #2: 0001-Restruct-org-create-formula-image-function.patch --]
[-- Type: application/octet-stream, Size: 11324 bytes --]

From 8c980da62b8f2438de3f3136cb9096e419f81fbb Mon Sep 17 00:00:00 2001
From: Feng Shu <tumashu@gmail.com>
Date: Wed, 17 Jul 2013 08:16:45 +0800
Subject: [PATCH] Restruct `org-create-formula-image' function

* org.el (org-create-formula-image-processes): New variable,
define `dvipng' and `imagemagick' processes.
(org-create-formula-image): Combine
`org-create-formula-image-with-dvipng' and
`org-create-formula-image-with-imagemagick'.
(org-create-formula-image-with-dvipng): Removed.
(org-create-formula-image-with-imagemagick): Removed.

`org-create-formula-image-with-dvipng' and
 `org-create-formula-image-with-imagemagick'
share a good deal of logic, so combine them to a function.
---
 lisp/org.el |  208 +++++++++++++++++++++++------------------------------------
 1 个文件被修改,插入 82 行(+),删除 126 行(-)

diff --git a/lisp/org.el b/lisp/org.el
index fb5099e..99cbb92 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18483,30 +18483,6 @@ inspection."
       ;; Failed conversion.  Return the LaTeX fragment verbatim
       latex-frag)))
 
-(defun org-create-formula-image (string tofile options buffer &optional type)
-  "Create an image from LaTeX source using dvipng or convert.
-This function calls either `org-create-formula-image-with-dvipng'
-or `org-create-formula-image-with-imagemagick' depending on the
-value of `org-latex-create-formula-image-program' or on the value
-of the optional TYPE variable.
-
-Note: ultimately these two function should be combined as they
-share a good deal of logic."
-  (org-check-external-command
-   "latex" "needed to convert LaTeX fragments to images")
-  (funcall
-   (case (or type org-latex-create-formula-image-program)
-     ('dvipng
-      (org-check-external-command
-       "dvipng" "needed to convert LaTeX fragments to images")
-      #'org-create-formula-image-with-dvipng)
-     ('imagemagick
-      (org-check-external-command
-       "convert" "you need to install imagemagick")
-      #'org-create-formula-image-with-imagemagick)
-     (t (error
-         "Invalid value of `org-latex-create-formula-image-program'")))
-   string tofile options buffer))
 
 (declare-function org-export-get-backend "ox" (name))
 (declare-function org-export--get-global-options "ox" (&optional backend))
@@ -18528,88 +18504,55 @@ share a good deal of logic."
        (plist-get info :latex-header)))
      info)))
 
-;; This function borrows from Ganesh Swami's latex2png.el
-(defun org-create-formula-image-with-dvipng (string tofile options buffer)
-  "This calls dvipng."
-  (require 'ox-latex)
-  (let* ((tmpdir (if (featurep 'xemacs)
-		     (temp-directory)
-		   temporary-file-directory))
-	 (texfilebase (make-temp-name
-		       (expand-file-name "orgtex" tmpdir)))
-	 (texfile (concat texfilebase ".tex"))
-	 (dvifile (concat texfilebase ".dvi"))
-	 (pngfile (concat texfilebase ".png"))
-	 (fnh (if (featurep 'xemacs)
-                  (font-height (face-font 'default))
-                (face-attribute 'default :height nil)))
-	 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
-	 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
-	 (fg (or (plist-get options (if buffer :foreground :html-foreground))
-		 "Black"))
-	 (bg (or (plist-get options (if buffer :background :html-background))
-		 "Transparent")))
-    (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
-      (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
-    (if (eq bg 'default) (setq bg (org-dvipng-color :background))
-      (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
-    (let ((latex-header (org-create-formula--latex-header)))
-      (with-temp-file texfile
-	(insert latex-header)
-	(insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
-    (let ((dir default-directory))
-      (condition-case nil
-	  (progn
-	    (cd tmpdir)
-	    (call-process "latex" nil nil nil texfile))
-	(error nil))
-      (cd dir))
-    (if (not (file-exists-p dvifile))
-	(progn (message "Failed to create dvi file from %s" texfile) nil)
-      (condition-case nil
-	  (if (featurep 'xemacs)
-	      (call-process "dvipng" nil nil nil
-			    "-fg" fg "-bg" bg
-			    "-T" "tight"
-			    "-o" pngfile
-			    dvifile)
-	    (call-process "dvipng" nil nil nil
-			  "-fg" fg "-bg" bg
-			  "-D" dpi
-			  ;;"-x" scale "-y" scale
-			  "-T" "tight"
-			  "-o" pngfile
-			  dvifile))
-	(error nil))
-      (if (not (file-exists-p pngfile))
-	  (if org-format-latex-signal-error
-	      (error "Failed to create png file from %s" texfile)
-	    (message "Failed to create png file from %s" texfile)
-	    nil)
-	;; Use the requested file name and clean up
-	(copy-file pngfile tofile 'replace)
-	(loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
-	      (if (file-exists-p (concat texfilebase e))
-		  (delete-file (concat texfilebase e))))
-	pngfile))))
-
 (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
-(defun org-create-formula-image-with-imagemagick (string tofile options buffer)
-  "This calls convert, which is included into imagemagick."
+
+(defvar org-create-formula-image-processes
+  '((dvipng
+     ("latex" "dvipng")
+     ("latex  %TEXFILE" "dvipng -T tight -D 96 -o  %PNGFILE  %DVIFILE"))
+    (imagemagick
+     ("latex" "convert")
+     ((lambda (texfile) (org-latex-compile texfile t))
+      "convert -density 96 -trim -antialias %PDFFILE -quality 100 %PNGFILE")))
+  "Commands to process a LaTeX snippet to a png file.
+This is an alist. The first element of its association is the name of process,
+the second is a list of prerequired system commands, the third is a list of elisp
+functions or shell commands, which will be launched when compile latex snippet file
+and convert the pdf or dvi file to png.
+
+Before the shell commands launched, %TEXFILE, %DVIFILE, %PDFFILE and %PNGFILE will
+be replaced by the corresponding files.
+
+NOTE: Elisp functions should accept a file name as its single argument.")
+
+(defun org-create-formula-image (string tofile options buffer &optional type)
+  "Create an image from LaTeX source using a process predefined in
+`org-create-formula-image-processes' depending on the value of
+`org-latex-create-formula-image-program' or on the value of the
+optional TYPE variable."
   (require 'ox-latex)
-  (let* ((tmpdir (if (featurep 'xemacs)
+  (let* ((processes (assoc
+		     (or type org-latex-create-formula-image-program)
+		     org-create-formula-image-processes))
+	 (commands-need-checked (cadr processes))
+	 (latex-process (caar (cddr processes)))
+	 (png-process (cadar (cddr processes)))
+	 (default-dir default-directory)
+	 (tmpdir (if (featurep 'xemacs)
 		     (temp-directory)
 		   temporary-file-directory))
 	 (texfilebase (make-temp-name
 		       (expand-file-name "orgtex" tmpdir)))
 	 (texfile (concat texfilebase ".tex"))
 	 (pdffile (concat texfilebase ".pdf"))
+	 (dvifile (concat texfilebase ".dvi"))
 	 (pngfile (concat texfilebase ".png"))
 	 (fnh (if (featurep 'xemacs)
                   (font-height (face-font 'default))
                 (face-attribute 'default :height nil)))
 	 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
-	 (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
+	 (fontsize (* scale (if buffer (* 0.1 fnh) 12.)))
+	 (baselineskip (* 1.2 fontsize))
 	 (fg (or (plist-get options (if buffer :foreground :html-foreground))
 		 "black"))
 	 (bg (or (plist-get options (if buffer :background :html-background))
@@ -18619,50 +18562,63 @@ share a good deal of logic."
     (if (eq bg 'default) (setq bg (org-latex-color :background))
       (setq bg (org-latex-color-format
 		(if (string= bg "Transparent") "white" bg))))
+    ;; Build a tex file for latex formula.
     (let ((latex-header (org-create-formula--latex-header)))
       (with-temp-file texfile
 	(insert latex-header)
 	(insert "\n\\begin{document}\n"
 		"\\definecolor{fg}{rgb}{" fg "}\n"
 		"\\definecolor{bg}{rgb}{" bg "}\n"
+		"\\fontsize{" (format "%0.1f" fontsize) "pt}{" (format "%0.1f" baselineskip) "pt}\n"
+		"\\selectfont\n"
 		"\n\\pagecolor{bg}\n"
 		"\n{\\color{fg}\n"
 		string
 		"\n}\n"
 		"\n\\end{document}\n")))
-    (org-latex-compile texfile t)
-    (if (not (file-exists-p pdffile))
-	(progn (message "Failed to create pdf file from %s" texfile) nil)
-      (condition-case nil
-	  (if (featurep 'xemacs)
-	      (call-process "convert" nil nil nil
-			    "-density" "96"
-			    "-trim"
-			    "-antialias"
-			    pdffile
-			    "-quality" "100"
-			    ;; "-sharpen" "0x1.0"
-			    pngfile)
-	    (call-process "convert" nil nil nil
-			  "-density" dpi
-			  "-trim"
-			  "-antialias"
-			  pdffile
-			  "-quality" "100"
-			  ;; "-sharpen" "0x1.0"
-			  pngfile))
-	(error nil))
-      (if (not (file-exists-p pngfile))
-	  (if org-format-latex-signal-error
-	      (error "Failed to create png file from %s" texfile)
-	    (message "Failed to create png file from %s" texfile)
-	    nil)
-	;; Use the requested file name and clean up
-	(copy-file pngfile tofile 'replace)
-	(loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
-	      (if (file-exists-p (concat texfilebase e))
-		  (delete-file (concat texfilebase e))))
-	pngfile))))
+    (save-window-excursion
+      ;; Check required commands.
+      (if (some 'null (mapcar (lambda (cmd) (org-check-external-command cmd nil t)) commands-need-checked))
+	  (error (format "You need to install commands: %s" (mapconcat 'concat commands-need-checked ", ")))
+	;; Compile tex file to pdf or dvi file.
+	(cd tmpdir)
+	(condition-case nil
+	    (if (stringp latex-process)
+		(shell-command
+		 (replace-regexp-in-string
+		  "%TEXFILE" (shell-quote-argument texfile)
+		  (replace-regexp-in-string
+		   "%DVIFILE" (shell-quote-argument dvifile)
+		   (replace-regexp-in-string
+		    "%PDFFILE" (shell-quote-argument pdffile) latex-process t t) t t) t t)
+		 (get-buffer-create "*Org Preview LaTeX Snippet*"))
+	      (funcall latex-process (shell-quote-argument texfile)))
+	  (error nil))
+	(cd default-dir)
+	(if (and (not (file-exists-p pdffile))
+		 (not (file-exists-p dvifile)))
+	    (error "Failed to create dvi file or pdf file from %s" texfile)
+	  ;; Convert pdf or dvi file to png file.
+	  (if (stringp png-process)
+	      (shell-command
+	       (replace-regexp-in-string
+		"%PNGFILE" (shell-quote-argument pngfile)
+		(replace-regexp-in-string
+		 "%DVIFILE" (shell-quote-argument dvifile)
+		 (replace-regexp-in-string
+		  "%PDFFILE" (shell-quote-argument pdffile) png-process t t) t t) t t) nil)
+	    (funcall png-process (shell-quote-argument (if (file-exists-p pdffile) pdffile dvifile))))
+	  (if (not (file-exists-p pngfile))
+	      (if org-format-latex-signal-error
+		  (error "Failed to create png file from %s" texfile)
+		(message "Failed to create png file from %s" texfile)
+		nil)
+	    ;; Use the requested file name and clean up
+	    (copy-file pngfile tofile 'replace)
+	    (loop for e in '(".dvi" ".pdf" ".tex" ".aux" ".log" ".png") do
+		  (if (file-exists-p (concat texfilebase e))
+		      (delete-file (concat texfilebase e))))
+	    pngfile))))))
 
 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
   "Fill a LaTeX header template TPL.
-- 
1.7.10.4


             reply	other threads:[~2013-07-20  9:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-20  9:32 feng shu [this message]
2013-07-20 14:33 ` [PATCH] [need test]Restruct `org-create-formula-image' function Nick Dokos
2013-07-20 22:20   ` Feng Shu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAJpRBmcqRE=dYJOj7aNARK9y4PQc_748E_ktOgs6pGrTQQL7mw@mail.gmail.com' \
    --to=tumashu@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).