Skip to content

Commit

Permalink
Prefer UTF-8 when loading SVG files
Browse files Browse the repository at this point in the history
  • Loading branch information
misohena committed Feb 7, 2025
1 parent 7fff3a2 commit 3c819e8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion edraw-dom-svg.el
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ This function may change attributes for compatibility."
(defun edraw-svg-read-from-file (path &optional accepts-top-level-comments-p)
(edraw-svg-decode-svg
(with-temp-buffer
(insert-file-contents path)
(edraw-insert-xml-file-contents path)
(buffer-substring-no-properties (point-min) (point-max)))
nil
accepts-top-level-comments-p))
Expand Down
2 changes: 1 addition & 1 deletion edraw-generator.el
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
(pop-to-buffer outbuf)
(error "Error: %s" dvisvgm-cmd))
(with-temp-buffer
(insert-file-contents svg-file)
(edraw-insert-xml-file-contents svg-file)
(buffer-string)))
(delete-file svg-file)
(delete-file aux-file)
Expand Down
2 changes: 1 addition & 1 deletion edraw-import.el
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ presentation attributes."
(keyboard-quit))
(edraw-import-svg-dom
(with-temp-buffer
(insert-file-contents file)
(edraw-insert-xml-file-contents file)
(edraw-import-svg-decode-buffer))))))

(defun edraw-import-svg-decode-buffer ()
Expand Down
4 changes: 2 additions & 2 deletions edraw-org.el
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ the time `edraw-org-link-recover-mouse-face' is called."
(insert data)
(edraw-decode-buffer t))
(file
(insert-file-contents file))))))
(edraw-insert-xml-file-contents file))))))

(defun edraw-org-link-show-svg-at-point ()
"Display the SVG data of the link at point."
Expand Down Expand Up @@ -641,7 +641,7 @@ Allowed values for TARGET-TYPE are:
(error (edraw-msg "No need to convert")))
(let ((new-data
(with-temp-buffer
(insert-file-contents file)
(edraw-insert-xml-file-contents file)
(edraw-encode-buffer t edraw-org-link-compress-data-p)
(buffer-string))))
(setf (alist-get "file" props nil t #'string=) nil)
Expand Down
22 changes: 20 additions & 2 deletions edraw-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@
(defun edraw-clipboard-data ()
(cdr edraw-clipboard-type-data))

;;;; File Coding Systems

(defun edraw-insert-xml-file-contents (path)
"Insert the contents of the XML file into the buffer.
PATH is the path to the XML file.
UTF-8 is the preferred character encoding for the file.
The encoding specified in the XML declaration is also taken into account
by the `auto-coding-functions'."
;; The default value of `auto-coding-functions' includes
;; `sgml-xml-auto-coding-function', but set it just to be safe.
(let ((auto-coding-functions '(sgml-xml-auto-coding-function)))
;; Prefer UTF-8 over the `coding-system-priority-list' setting.
(with-coding-priority '(utf-8)
(insert-file-contents path))))

;;;; gzip

(defconst edraw-detect-coding-system-p nil)
Expand Down Expand Up @@ -867,7 +885,7 @@ If there are multiple KEYs, the first one is removed."

(defun edraw-plist-remove-nil (plist)
"Return a new property list from PLIST where all properties whose keys or
values ​​are nil have been removed."
values are nil have been removed."
(cl-loop for (k v) on plist by #'cddr
when (and k v)
collect k and collect v))
Expand Down Expand Up @@ -942,7 +960,7 @@ SEPARATOR results in spaces between the values returned by FUNCTION.
FUNCTION must be a function of two arguments (a key and a value), and
must return a value that is a string.
A non-nil REMOVE-NIL means to remove properties whose keys or values ​​are nil."
A non-nil REMOVE-NIL means to remove properties whose keys or values are nil."
(let ((result "")
(p plist))
(while (and (consp p) (consp (cdr p)))
Expand Down
8 changes: 8 additions & 0 deletions todo.org
Original file line number Diff line number Diff line change
Expand Up @@ -6296,3 +6296,11 @@ CLOSED: [2025-02-04 Tue 16:48]
recent-colorsから最後に使った有彩色を求めようと思ったが、必ずしも良いとは言えなそう。キャンセルする場合だってあるわけだし。

それよりも単純にグローバル変数に最後にスライダーで選択した色相を保存しておく方が、最終的に使い勝手は良さそう。理想は色をRGBではなくHSBで直接保持することだけど、さすがに今から全部を直すわけにも行かないし、そもそもSVGでHSBは表現できないはず。HSLで表現して変換するという手もあるけど。
** DONE edrawで作成した図が非ASCII文字を入れると読み込めない
CLOSED: [2025-02-07 Fri 17:41]
私の環境では ~(set-coding-system-priority 'utf-8 'euc-jp 'iso-2022-jp 'cp932)~ という設定があるから読み込めるが、そうでない設定だとUTF-8と認識できず読み込めない場合がある。Windowsの日本語環境でもデフォルトではちゃんと読み込めない。

with-coding-priorityという便利なマクロがあるので、これでUTF-8を優先させる。
XML宣言でencodingが指定されている場合は、auto-coding-functionsに設定されたsgml-xml-auto-coding-functionによってエンコーディングが解決される。念のためこれも強制する。

edraw-insert-xml-file-contentsという関数を追加して、SVGファイルをバッファへ挿入するときは必ずこれを使うようにする。

0 comments on commit 3c819e8

Please sign in to comment.