Howto embed font variants in JavaFX

自闭症网瘾萝莉.ら 提交于 2021-01-27 06:20:08

问题


I need to embed local .ttf files on a JavaFX project with some variants (bold, italic and so on) but the css parser seems to not support font properties in @font-face declaration:

@font-face {
    font-family: 'Open Sans', sans-serif;
    src: url('fonts/OpenSans-Regular.ttf');
}
@font-face {
    font-family: 'Open Sans', sans-serif;
    src: url('fonts/OpenSans-Bold.ttf');
    font-weight: bold;
}
@font-face {
    font-family: 'Open Sans', sans-serif;
    src: url('fonts/OpenSans-ExtraBoldItalic.ttf');
    font-style: italic;
}

WARNING: CSS Error parsing jar:file:/home/test/dist/run736633436/test.jar!/it/test/vending/css/application.css: Unexpected TOKEN [:] at [8,15] mag 06, 2015 3:40:15 PM com.sun.javafx.css.parser.CSSParser fontFace WARNING: CSS Error parsing jar:file:/home/test/dist/run736633436/test.jar!/it/test/vending/css/application.css: Unexpected TOKEN [:] at [13,14]

How could I achieve this?

==EDIT==


Notice that the parser complains on font-weight: bold; and font-style: italic; rules.


回答1:


I think I found a solution. Try to swap the font-weight and font-style attributes with the src attribute. In your case your css would look like:

@font-face {
    font-family: 'Open Sans', sans-serif;
    src: url('fonts/OpenSans-Regular.ttf');
}
@font-face {
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    src: url('fonts/OpenSans-Bold.ttf');
}
@font-face {
    font-family: 'Open Sans', sans-serif;
    font-style: italic;
    src: url('fonts/OpenSans-ExtraBoldItalic.ttf');
}

I had the same issue and swapping the attributes did the job. I have no idea why that is but it seems to resolve the issue.



来源:https://stackoverflow.com/questions/30079036/howto-embed-font-variants-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!