Skip to content

Instantly share code, notes, and snippets.

@alanthird
Last active April 10, 2021 09:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanthird/7b86dc66df1ed3b9006bcd3fddd7350f to your computer and use it in GitHub Desktop.
Save alanthird/7b86dc66df1ed3b9006bcd3fddd7350f to your computer and use it in GitHub Desktop.
Use SVGs to replace text in Emacs
;; Score out this text
(require 'svg)
(defun replace-text (start end)
(let* ((str (buffer-substring start end))
(scale (cadr (assoc :height (assoc 'default face-remapping-alist))))
(family (face-attribute 'default :family))
(height (* (aref (font-info family) 2) (if scale scale 1)))
(ascent (* (aref (font-info family) 8) (if scale scale 1)))
(img (svg-create (car (window-text-pixel-size nil start end))
(line-pixel-height)
:font-size height
:font-family family)))
(svg-text img str :y ascent :fill "red" :textLength "100%")
(svg-line img 0 0 "100%" "100%" :stroke "currentColor")
(svg-line img "100%" 0 0 "100%" :stroke "currentColor")
(add-text-properties start end `(display
,(svg-image img :ascent 'center :scale 1)
rear-nonsticky t))))
(replace-text 14 18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment