如何在JavaFX中嵌入字体变体 [英] Howto embed font variants in JavaFX

查看:193
本文介绍了如何在JavaFX中嵌入字体变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在具有某些变体(粗体,斜体等)的JavaFX项目中嵌入本地.ttf文件,但是css解析器似乎不支持 @ font-face 声明中的字体属性:

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;
}

警告:CSS错误解析 jar:文件:/home/test/dist/run736633436/test.jar!/it/test/vending/css/application.css: 意外的令牌[:]在[8,15] mag 06,2015 3:40:15 PM com.sun.javafx.css.parser.CSSParser fontFace警告:CSS错误 解析 jar:文件:/home/test/dist/run736633436/test.jar!/it/test/vending/css/application.css: [13,14]出现意外的令牌[:]

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]

我怎么能做到这一点?

== EDIT ==

==EDIT==

请注意,解析器会抱怨 font-weight:粗体; font-style:斜体; 规则.

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

推荐答案

我想我找到了解决方案.尝试将font-weightfont-style属性替换为src属性.在您的情况下,您的CSS看起来像:

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.

这篇关于如何在JavaFX中嵌入字体变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆