Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[PDFs] Support emoji (#77034)

Braden Shepherdson931afd85c50c
diff --git a/resources/fonts/pdf/NotoEmoji-Regular.ttf b/resources/fonts/pdf/NotoEmoji-Regular.ttf
new file mode 100644
index 00000000000..c2c26ab612a
Binary files /dev/null and b/resources/fonts/pdf/NotoEmoji-Regular.ttf differ
diff --git a/resources/fonts/pdf/OFL.txt b/resources/fonts/pdf/OFL.txt
new file mode 100644
index 00000000000..d09d3d0e091
--- /dev/null
+++ b/resources/fonts/pdf/OFL.txt
@@ -0,0 +1,93 @@
+Copyright 2013 Google LLC
+
+This Font Software is licensed under the SIL Open Font License, Version 1.1.
+This license is copied below, and is also available with a FAQ at:
+https://scripts.sil.org/OFL
+
+
+-----------------------------------------------------------
+SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
+-----------------------------------------------------------
+
+PREAMBLE
+The goals of the Open Font License (OFL) are to stimulate worldwide
+development of collaborative font projects, to support the font creation
+efforts of academic and linguistic communities, and to provide a free and
+open framework in which fonts may be shared and improved in partnership
+with others.
+
+The OFL allows the licensed fonts to be used, studied, modified and
+redistributed freely as long as they are not sold by themselves. The
+fonts, including any derivative works, can be bundled, embedded,
+redistributed and/or sold with any software provided that any reserved
+names are not used by derivative works. The fonts and derivatives,
+however, cannot be released under any other type of license. The
+requirement for fonts to remain under this license does not apply
+to any document created using the fonts or their derivatives.
+
+DEFINITIONS
+"Font Software" refers to the set of files released by the Copyright
+Holder(s) under this license and clearly marked as such. This may
+include source files, build scripts and documentation.
+
+"Reserved Font Name" refers to any names specified as such after the
+copyright statement(s).
+
+"Original Version" refers to the collection of Font Software components as
+distributed by the Copyright Holder(s).
+
+"Modified Version" refers to any derivative made by adding to, deleting,
+or substituting -- in part or in whole -- any of the components of the
+Original Version, by changing formats or by porting the Font Software to a
+new environment.
+
+"Author" refers to any designer, engineer, programmer, technical
+writer or other person who contributed to the Font Software.
+
+PERMISSION & CONDITIONS
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of the Font Software, to use, study, copy, merge, embed, modify,
+redistribute, and sell modified and unmodified copies of the Font
+Software, subject to the following conditions:
+
+1) Neither the Font Software nor any of its individual components,
+in Original or Modified Versions, may be sold by itself.
+
+2) Original or Modified Versions of the Font Software may be bundled,
+redistributed and/or sold with any software, provided that each copy
+contains the above copyright notice and this license. These can be
+included either as stand-alone text files, human-readable headers or
+in the appropriate machine-readable metadata fields within text or
+binary files as long as those fields can be easily viewed by the user.
+
+3) No Modified Version of the Font Software may use the Reserved Font
+Name(s) unless explicit written permission is granted by the corresponding
+Copyright Holder. This restriction only applies to the primary font name as
+presented to the users.
+
+4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
+Software shall not be used to promote, endorse or advertise any
+Modified Version, except to acknowledge the contribution(s) of the
+Copyright Holder(s) and the Author(s) or with their explicit written
+permission.
+
+5) The Font Software, modified or unmodified, in part or in whole,
+must be distributed entirely under this license, and must not be
+distributed under any other license. The requirement for fonts to
+remain under this license does not apply to any document created
+using the Font Software.
+
+TERMINATION
+This license becomes null and void if any of the above conditions are
+not met.
+
+DISCLAIMER
+THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
+OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
+COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
+DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
+OTHER DEALINGS IN THE FONT SOFTWARE.
diff --git a/src/metabase/channel/render/pdf/font.clj b/src/metabase/channel/render/pdf/font.clj
index 31d3f35626f..25377f12be4 100644
--- a/src/metabase/channel/render/pdf/font.clj
+++ b/src/metabase/channel/render/pdf/font.clj
@@ -56,7 +56,14 @@
:kr-regular "fonts/pdf/NotoSansKR-Regular.ttf"
:kr-bold "fonts/pdf/NotoSansKR-Bold.ttf"
:sc-regular "fonts/pdf/NotoSansSC-Regular.ttf"
- :sc-bold "fonts/pdf/NotoSansSC-Bold.ttf"})
+ :sc-bold "fonts/pdf/NotoSansSC-Bold.ttf"
+
+ ;; Monochrome (black outline) emoji -- PDFBox's showText can only draw glyph outlines, so we use Noto's outline
+ ;; emoji font rather than the colour (COLR/CBDT) one, which it can't render. This is the Noto Emoji variable font;
+ ;; PDFBox embeds its default (Regular) instance, subset to the emoji actually used. One face serves both regular
+ ;; and bold text (emoji have no weight). See [[metabase.channel.render.pdf.font/normalize-ws]] re: variation
+ ;; selectors, and note that ZWJ/skin-tone sequences render as their component glyphs (no GSUB shaping).
+ :emoji "fonts/pdf/NotoEmoji-Regular.ttf"})
(def ^:dynamic *fonts*
"Per-document map of `{face-keyword {:fallbacks [<phys> ...]} ...}`, where each `<phys>` is
@@ -90,8 +97,10 @@
face* (fn [id & fallbacks]
{:id id
:fallbacks (mapv phys (flatten fallbacks))})
- regulars [:hebrew-regular :arabic-regular :jp-regular :kr-regular :sc-regular]
- bolds [:hebrew-bold :arabic-bold :jp-bold :kr-bold :sc-bold]]
+ ;; `:emoji` is last so it only catches codepoints no text/CJK font covers (a dual-use char like U+2764 keeps
+ ;; its text-font glyph); it serves both weights since emoji have no bold.
+ regulars [:hebrew-regular :arabic-regular :jp-regular :kr-regular :sc-regular :emoji]
+ bolds [:hebrew-bold :arabic-bold :jp-bold :kr-bold :sc-bold :emoji]]
;; The fallback order for each style is important: first ordering by preference in case they overlap; and then
;; where there's no overlap (especially CJK) to put the heaviest fonts last.
{:regular (face* :regular :brand-regular :noto-regular regulars)
@@ -115,8 +124,18 @@
;; Script utilities -- RTL shaping/reordering and whitespace normalisation
;; --------------------------------------------------------------------------------------------
+(defn- variation-selector?
+ "True for a Unicode variation selector: the VS1-16 block (U+FE00-FE0F, incl. the emoji/text presentation
+ selectors VS15/VS16) or the supplementary VS17-256 block (U+E0100-E01EF). These are zero-width presentation
+ hints with no printable glyph, so we drop them rather than let them fall back to `?`."
+ [cp]
+ (or (<= 0xFE00 cp 0xFE0F)
+ (<= 0xE0100 cp 0xE01EF)))
+
(defn normalize-ws
- "Turn tabs, newlines and other control chars into spaces. PDFBox `showText` doesn't handle them."
+ "Turn tabs, newlines and other control chars into spaces, and drop variation selectors (see
+ [[variation-selector?]]). PDFBox `showText` doesn't handle control chars, and no font has printable glyphs for the
+ selectors."
^String [s]
(let [s (str s)
n (.length s)
@@ -124,7 +143,10 @@
(loop [i 0]
(when (< i n)
(let [cp (.codePointAt s i)]
- (.appendCodePoint sb (if (< cp 32) (int \space) cp))
+ (cond
+ (variation-selector? cp) nil ; drop
+ (< cp 32) (.append sb \space)
+ :else (.appendCodePoint sb cp))
(recur (+ i (Character/charCount cp))))))
(.toString sb)))
diff --git a/test/metabase/channel/render/pdf_test.clj b/test/metabase/channel/render/pdf_test.clj
index 60394aada81..712d341181a 100644
--- a/test/metabase/channel/render/pdf_test.clj
+++ b/test/metabase/channel/render/pdf_test.clj
@@ -658,6 +658,28 @@
(is (> (cap-y :top) (cap-y :middle) (cap-y :bottom))))))
(.close cs)))))
+(deftest ^:parallel normalize-ws-strips-variation-selectors-test
+ (testing "variation selectors are dropped so they don't fall back to '?' (UXW-4704)"
+ (let [vs16 (char 0xFE0F) ; emoji-presentation selector (as in ❤️)
+ vs15 (char 0xFE0E) ; text-presentation selector
+ heart (char 0x2764)]
+ (is (= (str heart) (#'font/normalize-ws (str heart vs16))) "VS16 dropped, base char kept")
+ (is (= "A" (#'font/normalize-ws (str \A vs15))) "VS15 dropped")
+ (is (= "" (#'font/normalize-ws (str vs16))) "a lone selector leaves nothing")
+ (is (= "a b" (#'font/normalize-ws "a\tb")) "control chars still normalize to a space"))))
+
+(deftest ^:synchronized emoji-font-fallback-test
+ (testing "emoji codepoints resolve to a font glyph instead of the '?' placeholder (UXW-4704)"
+ (with-open [doc (PDDocument.)]
+ (binding [font/*fonts* (#'font/load-fonts! doc)]
+ ;; grinning face, party popper, rocket, thumbs-up, fire, heavy-black-heart
+ (let [emoji "😀🎉🚀👍🔥❤"
+ runs (#'font/font-runs (#'font/face :regular) (#'font/normalize-ws emoji))
+ drawn (apply str (map second runs))]
+ (is (= emoji drawn)
+ "every emoji survived font resolution (none replaced by '?')")
+ (is (not (str/includes? drawn "?"))))))))
+
(deftest ^:synchronized em-width-memoization-test
(with-open [doc (PDDocument.)]
(binding [font/*fonts* (#'font/load-fonts! doc)]