font-parse-tests.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. ;;; font-parse-tests.el --- Test suite for font parsing.
  2. ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
  3. ;; Author: Chong Yidong <cyd@stupidchicken.com>
  4. ;; Keywords: internal
  5. ;; Human-Keywords: internal
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Type M-x test-font-parse RET to generate the test buffer.
  19. ;;; Code:
  20. (require 'ert)
  21. (defvar font-parse-tests--data
  22. `((" " ,(intern " ") nil nil nil nil)
  23. ("Monospace" Monospace nil nil nil nil)
  24. ("Foo1" Foo1 nil nil nil nil)
  25. ("12" nil 12.0 nil nil nil)
  26. ("12 " ,(intern "12 ") nil nil nil nil)
  27. ;; Fontconfig format
  28. ("Foo:" Foo nil nil nil nil)
  29. ("Foo-8" Foo 8.0 nil nil nil)
  30. ("Foo-18:" Foo 18.0 nil nil nil)
  31. ("Foo-18:light" Foo 18.0 light nil nil)
  32. ("Foo 10:weight=bold" ,(intern "Foo 10") nil bold nil nil)
  33. ("Foo-12:weight=bold" Foo 12.0 bold nil nil)
  34. ("Foo 8-20:slant=oblique" ,(intern "Foo 8") 20.0 nil oblique nil)
  35. ("Foo:light:roman" Foo nil light roman nil)
  36. ("Foo:italic:roman" Foo nil nil roman nil)
  37. ("Foo 12:light:oblique" ,(intern "Foo 12") nil light oblique nil)
  38. ("Foo-12:demibold:oblique" Foo 12.0 demibold oblique nil)
  39. ("Foo:black:proportional" Foo nil black nil 0)
  40. ("Foo-10:black:proportional" Foo 10.0 black nil 0)
  41. ("Foo:weight=normal" Foo nil normal nil nil)
  42. ("Foo:weight=bold" Foo nil bold nil nil)
  43. ("Foo:weight=bold:slant=italic" Foo nil bold italic)
  44. ("Foo:weight=bold:slant=italic:mono" Foo nil bold italic 100)
  45. ("Foo-10:demibold:slant=normal" Foo 10.0 demibold normal nil)
  46. ("Foo 11-16:oblique:weight=bold" ,(intern "Foo 11") 16.0 bold oblique nil)
  47. ("Foo:oblique:randomprop=randomtag:weight=bold" Foo nil bold oblique nil)
  48. ("Foo:randomprop=randomtag:bar=baz" Foo nil nil nil nil)
  49. ("Foo Book Light:bar=baz" ,(intern "Foo Book Light") nil nil nil nil)
  50. ("Foo Book Light 10:bar=baz" ,(intern "Foo Book Light 10") nil nil nil nil)
  51. ("Foo Book Light-10:bar=baz" ,(intern "Foo Book Light") 10.0 nil nil nil)
  52. ;; GTK format
  53. ("Oblique" nil nil nil oblique nil)
  54. ("Bold 17" nil 17.0 bold nil nil)
  55. ("17 Bold" ,(intern "17") nil bold nil nil)
  56. ("Book Oblique 2" nil 2.0 book oblique nil)
  57. ("Bar 7" Bar 7.0 nil nil nil)
  58. ("Bar Ultra-Light" Bar nil ultra-light nil nil)
  59. ("Bar Light 8" Bar 8.0 light nil nil)
  60. ("Bar Book Medium 9" Bar 9.0 medium nil nil)
  61. ("Bar Semi-Bold Italic 10" Bar 10.0 semi-bold italic nil)
  62. ("Bar Semi-Condensed Bold Italic 11" Bar 11.0 bold italic nil)
  63. ("Foo 10 11" ,(intern "Foo 10") 11.0 nil nil nil)
  64. ("Foo 1985 Book" ,(intern "Foo 1985") nil book nil nil)
  65. ("Foo 1985 A Book" ,(intern "Foo 1985 A") nil book nil nil)
  66. ("Foo 1 Book 12" ,(intern "Foo 1") 12.0 book nil nil)
  67. ("Foo A Book 12 A" ,(intern "Foo A Book 12 A") nil nil nil nil)
  68. ("Foo 1985 Book 12 Oblique" ,(intern "Foo 1985 Book 12") nil nil oblique nil)
  69. ("Foo 1985 Book 12 Italic 10" ,(intern "Foo 1985 Book 12") 10.0 nil italic nil)
  70. ("Foo Book Bar 6 Italic" ,(intern "Foo Book Bar 6") nil nil italic nil)
  71. ("Foo Book Bar Bold" ,(intern "Foo Book Bar") nil bold nil nil))
  72. "List of font names parse data.
  73. Each element should have the form
  74. (NAME FAMILY SIZE WEIGHT SLANT SPACING)
  75. where NAME is the name to parse, and the remainder are the
  76. expected font properties from parsing NAME.")
  77. (defun font-parse-check (name prop expected)
  78. (let ((result (font-get (font-spec :name name) prop)))
  79. (if (and (symbolp result) (symbolp expected))
  80. (eq result expected)
  81. (equal result expected))))
  82. (put 'font-parse-check 'ert-explainer 'font-parse-explain)
  83. (defun font-parse-explain (name prop expected)
  84. (let ((result (font-get (font-spec :name name) prop))
  85. (propname (symbol-name prop)))
  86. (format "Parsing `%s': expected %s `%s', got `%s'."
  87. name (substring propname 1) expected
  88. (font-get (font-spec :name name) prop))))
  89. (ert-deftest font-parse-tests ()
  90. "Test parsing of Fontconfig-style and GTK-style font names."
  91. (dolist (test font-parse-tests--data)
  92. (let* ((name (nth 0 test)))
  93. (should (font-parse-check name :family (nth 1 test)))
  94. (should (font-parse-check name :size (nth 2 test)))
  95. (should (font-parse-check name :weight (nth 3 test)))
  96. (should (font-parse-check name :slant (nth 4 test)))
  97. (should (font-parse-check name :spacing (nth 5 test))))))
  98. (defun test-font-parse ()
  99. "Test font name parsing."
  100. (interactive)
  101. (switch-to-buffer (generate-new-buffer "*Font Pase Test*"))
  102. (setq show-trailing-whitespace nil)
  103. (let ((pass-face '((t :foreground "green")))
  104. (fail-face '((t :foreground "red"))))
  105. (dolist (test font-parse-tests--data)
  106. (let* ((name (nth 0 test))
  107. (fs (font-spec :name name))
  108. (family (font-get fs :family))
  109. (size (font-get fs :size))
  110. (weight (font-get fs :weight))
  111. (slant (font-get fs :slant))
  112. (spacing (font-get fs :spacing)))
  113. (insert name)
  114. (if (> (current-column) 20)
  115. (insert "\n"))
  116. (indent-to-column 21)
  117. (insert (propertize (symbol-name family)
  118. 'face (if (eq family (nth 1 test))
  119. pass-face
  120. fail-face)))
  121. (indent-to-column 40)
  122. (insert (propertize (format "%s" size)
  123. 'face (if (equal size (nth 2 test))
  124. pass-face
  125. fail-face)))
  126. (indent-to-column 48)
  127. (insert (propertize (format "%s" weight)
  128. 'face (if (eq weight (nth 3 test))
  129. pass-face
  130. fail-face)))
  131. (indent-to-column 60)
  132. (insert (propertize (format "%s" slant)
  133. 'face (if (eq slant (nth 4 test))
  134. pass-face
  135. fail-face)))
  136. (indent-to-column 69)
  137. (insert (propertize (format "%s" spacing)
  138. 'face (if (eq spacing (nth 5 test))
  139. pass-face
  140. fail-face)))
  141. (insert "\n"))))
  142. (goto-char (point-min)))
  143. ;; Local Variables:
  144. ;; no-byte-compile: t
  145. ;; End:
  146. ;;; font-parse-tests.el ends here.