I had the same problem when writing a program in Clojure, i.e. using fonts downloaded from TTF to JEditorPane displaying HTML text. The solution worked well here - I copy the interesting part here for future reference:
(def font1 (with-open [s (FileInputStream. "SomeFont.ttf")] (.deriveFont (Font/createFont Font/TRUETYPE_FONT s) (float 14)))) (def font2 (Font. "SansSerif") Font/PLAIN 14) (let [editor (JEditorPane. "text/html" "")] (.setDocument editor (proxy [HTMLDocument] [] (getFont [attr] (if (= (.getAttribute attr StyleConstants/FontFamily) "MyFont") font1 font2)))))
This assumes that the HTML document belongs to the "MyFont" font family, for example. with CSS snippet like
p { font-family: "MyFont" }
Please note that with this you must handle all font requests. This is because the proxy restriction cannot call member functions of the superclass. In addition, if you want to handle different font sizes, you must do this "manually" by checking the StyleConstants / FontSize attribute and creating the font using deiveFont, respectively.
Hope this helps someone :)
sunwukong
source share