首页 \ 问答 \ 用javafx创建一个Truetype字体文件(Creating a Truetype Font file with javafx)

用javafx创建一个Truetype字体文件(Creating a Truetype Font file with javafx)

我有一个画布。 它要求用户从AZ,az或0-9中绘制一个字符。 一旦用户画一个字符说A,当前画布对象被保存在画布的数组列表中。 另一个空白画布弹出,要求用户画B.等等。

程序工作正常。 但我想创建一个包含所有可接受字符的.ttf文件。

我在最后一个显示的画布下面有一个按钮,当点击它时,将从画布的数组列表中提取所有字符并从中创建一个.ttf文件。 但是如何?


I have a canvas. It asks the user to draw a character from A-Z, a-z or 0-9. once the user draws a character say A, the current canvas object is saved in an arraylist of canvases. and another blank canvas pops up that asks the user to draw B. and so on.

The programs works fine. But I want to create a .ttf file with all the accepted characters drawn.

I have a button below the last displayed canvas which when clicked, will extract all chars from the arraylist of canvases and create a .ttf file from it. But how?


原文:https://stackoverflow.com/questions/26332766
更新时间:2019-07-08 13:39

最满意答案

第一解决方案

您可以使用java.awt.Font ,它是一个用于管理和创建字体的Java类,您可以使用它的Font(String name,int style,int size)构造函数创建您的自定义Font:

字体(字符串名称,int样式,int大小):

从指定的名称,样式和磅值中创建一个新的字体。 字体名称可以是字体名称或字体系列名称。 它与风格一起使用以找到合适的字体。 当指定字体系列名称时,style参数用于从系列中选择最合适的面。 当指定字体名称时,会合并面部的样式和样式参数,以找到来自同一族的最佳匹配字体。

例如,如果使用样式Font.ITALIC指定了名称“Arial Bold”,则字体系统会在“Arial”系列中查找粗体和斜体中的一个面,并且可能会将字体实例与物理字体面“Arial Bold斜体”。 样式参数与指定的人脸样式合并,不添加或减少。 这意味着,指定粗体和粗体不会增加字体的粗体,并且指定粗体和粗体不会减淡字体。

参数:

名称 - 字体名称。 这可以是字体名称或字体系列名称,并且可以表示在此GraphicsEnvironment中找到的逻辑字体或物理字体。 逻辑字体的族名是:Dialog,DialogInput,Monospaced,Serif或SansSerif。 所有这些名称均存在预定义的字符串常量,例如DIALOG。 如果name为null,则由getName()返回的新Font的逻辑字体名称将设置为名称“Default”。

样式 - 字体的样式常数样式参数是一个整数位掩码,可以是PLAIN,也可以是BOLD和/或ITALIC的按位联合(例如,ITALIC或BOLD | ITALIC)。 如果样式参数不符合预期的整数位掩码之一,则样式设置为PLAIN。

大小 - Fon的磅值

正如你所看到的,它允许你根据现有的字体创建新的字体,你也可以改变风格和大小。

这是您可以遵循的教程


第二个方案

正如评论中所述,您还可以使用Fontastic Library,它似乎是创建新字体的好工具,它允许您根据数据,传感器,实时馈送或任何其他算法制作字体,或者操纵现有的字体来创建你自己的版本。

这些是向您展示其工作原理所需的基本步骤。 有关完整参考,请参阅文档

如何创建一个新的Fontastic对象:

Fontastic f = new Fontastic(this, "ExampleFont");  // Create a new Fontastic object

如何设置更多的字体属性:

f.setAuthor("Andreas Koller"); 

如何为具有四点随机形状的字符A创建字形:

PVector[] points = new PVector[4];              // Define a PVector array containing the points of the shape
points[0] = new PVector(0, 0);                  // Start at bottom left
points[1] = new PVector(random(512), 0);        // The normal width is 512, the normal height 1024
points[2] = new PVector(random(512), random(1024)); // y coordinates are from bottom to top!
points[3] = new PVector(0, random(1024));

f.addGlyph('A').addContour(points);

如何生成TrueType字体文件:

f.buildFont();  

事后如何清理:

f.cleanup();

你可以看到他们的例子


结论:

但在这两种情况下,您将无法从手写扫描图像创建字体, Fantastic不允许您从图像创建字体,但它使用矢量图形来创建字体,因此在这种情况下,解决方案是:

  1. 要使用其他工具将图像转换为矢量( Vector MagicInkscapemyScriptFont.com

  2. 然后使用生成的vectors创建您的Font


First Solution:

You can use the java.awt.Font, it is a Java class used to manage and create Fonts, you can use its Font(String name, int style, int size) constructor to create your custom Font:

Font(String name, int style, int size):

Creates a new Font from the specified name, style and point size. The font name can be a font face name or a font family name. It is used together with the style to find an appropriate font face. When a font family name is specified, the style argument is used to select the most appropriate face from the family. When a font face name is specified, the face's style and the style argument are merged to locate the best matching font from the same family.

For example if face name "Arial Bold" is specified with style Font.ITALIC, the font system looks for a face in the "Arial" family that is bold and italic, and may associate the font instance with the physical font face "Arial Bold Italic". The style argument is merged with the specified face's style, not added or subtracted. This means, specifying a bold face and a bold style does not double-embolden the font, and specifying a bold face and a plain style does not lighten the font.

Parameters:

name - the font name. This can be a font face name or a font family name, and may represent either a logical font or a physical font found in this GraphicsEnvironment. The family names for logical fonts are: Dialog, DialogInput, Monospaced, Serif, or SansSerif. Pre-defined String constants exist for all of these names, for example, DIALOG. If name is null, the logical font name of the new Font as returned by getName() is set to the name "Default".

style - the style constant for the Font The style argument is an integer bitmask that may be PLAIN, or a bitwise union of BOLD and/or ITALIC (for example, ITALIC or BOLD|ITALIC). If the style argument does not conform to one of the expected integer bitmasks then the style is set to PLAIN.

size - the point size of the Fon

And as you can see it allows you to create new Fonts depending on existing fonts, you can also change the style and the size.

This is a Tutorial that you can follow.


Second Solution:

As stated in comments, you can also use the Fontastic Library which seems to be a great tool for creating new Fonts, It allows you to make fonts based on data, sensors, live feeds, or any other algorithm, or manipulate existing fonts to create your own version.

These are the basic steps needed to show you how it works. For a full reference see the Documentation.

How to create a new Fontastic object:

Fontastic f = new Fontastic(this, "ExampleFont");  // Create a new Fontastic object

How to set further font properties:

f.setAuthor("Andreas Koller"); 

How to create a glyph for the character A with a random shape of four points:

PVector[] points = new PVector[4];              // Define a PVector array containing the points of the shape
points[0] = new PVector(0, 0);                  // Start at bottom left
points[1] = new PVector(random(512), 0);        // The normal width is 512, the normal height 1024
points[2] = new PVector(random(512), random(1024)); // y coordinates are from bottom to top!
points[3] = new PVector(0, random(1024));

f.addGlyph('A').addContour(points);

How to generate the TrueType font file:

f.buildFont();  

How to clean up afterwards:

f.cleanup();

You can see their examples.


Conclusion:

But in the two cases you will not be able to create Fonts from handwritten scanned images, Fantastic won't allow you to create a Font from images but it uses vector graphics to create Fonts, so in this case the solution would be :

  1. To use another tool to convert your images to vectors (Vector Magic, Inkscape, myScriptFont.com)

  2. Then create your Font using the generated vectors.

2015-08-24

相关问答

更多

将Postscript字体转换为TrueType(Converting a Postscript font to TrueType)

你不应该试着这样做。 字体技术是完全不同的,并且制作原版的高质量版本将需要来自印刷师的输入以正确地提示字体。 但是,fontforge可以做你想要的事情。 You shouldn't try to do this really. The font technologies are quite different and making a good quality version of the original would require input from a typographer to p

使用PHP进行字体文件操作(Font-File manipulation with PHP)

建议:通过python工具TTX运行字体。 将字体转换为TTX xml格式,将TTFGlyph元素交换为这些点,然后通过TTX运行它以形成新的TTF,完成。 如果您试图使用CFF的OpenType十六进制编辑器来实现这一点: 这可能是一个坏主意 。 对于具有TrueType数据的OpenType,它需要更新cmap; 对于使用CFF的OpenType,它还需要更新CFF块,这是一项很多工作。 Recommendation: run the font through the python tool

JavaFX2:使用java.awt.print.PrinterJob.printDialog()时的HeadlessException(JavaFX2: HeadlessException when using java.awt.print.PrinterJob.printDialog())

我写了一个小类,从JavaFX应用程序打开AWT打印对话框。 我用JDK 1.8测试了这个,所以我不知道它对你的情况有帮助。 如果没有,请告诉我,然后我会用JDK 1.7进行调查。 [更新]我也在Mac OS X 10.9.3上使用JDK1.7(带有集成的JavaFx 2.2) import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.event.ActionEvent; impo

带有Assetic的Synmfony 2中的CSS文件中的@ font-face规则(@font-face rule in CSS files in Synmfony 2 with Assetic)

我找到了解决方案: 只需输入您链接css文件的Document.html.twig: <style type="text/css"> @font-face { font-family: 'Corbert'; src: url({{asset('fonts/Corbert-Regular.otf')}}) format('opentype'), url({{asset('fonts/Corbert-Regular.woff')}}) format('woff'),

在JavaFx中创建一个可滚动的VBox(Creating a Scrollable VBox in JavaFx)

只需将侧栏包裹在滚动窗格中即可。 如果您希望它只能垂直滚动,请调用setFitToWidth(true) 。 sideBar = new SideBarView(borderPane); ScrollPane sideBarScroller = new ScrollPane(sideBar); sideBarScroller.setFitToWidth(true); borderPane.setRight(sideBarScroller); Just wrap the side bar i

JavaFX - 无法从FXML文件加载节点[重复](JavaFX - Can't load node from FXML file [duplicate])

FXMLLoader.load创建Test类的新实例。 所以这和你现在的班级不一样。 用户使用FXMLLoader类的setControllerFactory方法来更改此行为。 FXMLLoader.load creates a new instance of your Test class. So this will not be the same as your current class. User the setControllerFactory method of the FXMLLoa ...

JavaFX FileChooser(JavaFX FileChooser)

将DirectoryChooser作为2.1版本的一部分添加到JavaFX中。 用法是: DirectoryChooser chooser = new DirectoryChooser(); chooser.setTitle("JavaFX Projects"); File defaultDirectory = new File("c:/dev/javafx"); chooser.setInitialDirectory(defaultDirectory); File selectedDirecto ...

Chrome 10 / Windows @ font-face编码有问题(Chrome 10/Windows @font-face encoding trouble)

webfont很可能没有正确组装。 我已经下载了在Sebastian的网站上使用的CaviarDreams字体并得到了相同的bug。 然后我从这里下载了它: http : //www.dafont.com/caviar-dreams.font ,使用了fontsquirrel.com的typekit生成器 ,结果非常好。 It is highly likely that the webfont is not assembled properly. I have downloaded Caviar

相关文章

更多

最新问答

更多
  • Wireshark捕获过滤器示例(Wireshark Capture Filter Example)
  • 什么是NoSQL?(What exactly is NoSQL?)
  • 如何在sass中编写媒体查询?(How to write media queries in sass?)
  • spring destroy-method +请求范围bean(spring destroy-method + request scope bean)
  • ssis包中全局表的范围(Scope of Global Table in ssis package)
  • 如何在较小的屏幕上禁用Bootstrap的“词缀”?(How can I disable Bootstrap's “affix” on smaller screens?)
  • SQLite Min函数在使用String数据的行中返回NULL(SQLite Min function returns NULL in row with String data)
  • 用于从3-D阵列中选择具有不同起始索引的相同长度子阵列的纯numpy表达式(pure numpy expression for selecting same-length subarrays with different starting indices from 3-D array)
  • 如何在Cordova中更新应用程序版本(How to update app version in Cordova)
  • 当我在while循环中使用char []读取Android中的文本文件时出现重复行(repeated lines when I use char[] in while loop for reading text file in Android)
  • 有没有办法按顺序打印数组,在[TCL]中输入条目(Is there a way to print array in order with entries entered in that [TCL])
  • 如何在倒排索引结构中搜索短语查询?(How to search phrase queries in inverted index structure?)
  • 导致在WCF中返回HTTP错误状态(Causing HTTP error status to be returned in WCF)
  • qsqltablemodel不兼容的数据类型(qsqltablemodel incompatible data types)
  • 如何在没有包的情况下引用Java文件?(How can I reference Java files without packages?)
  • Rails 4.1.1相关模型属性,嵌套表单不保存(Rails 4.1.1 related model attribute with nested form not saving)
  • 如何将byte []转换成字符串?(How to convert UTF-8 byte[] to string?)
  • 学习如何使用Guzzle从API访问数据(Learning how to access data from an API using Guzzle)
  • 如何在pascal中将短语拆分为单词?(How to split a phrase into words in pascal?)
  • MySQL将两列合并成一列(MySQL combine two columns into one column)
  • Delphi SHGetFolderPath和null终止字符串(Delphi SHGetFolderPath and null terminated string)
  • 显示文本框onchange asp.net(Display textbox onchange asp.net)
  • 改变骆驼的交换内容(change exchange content in camel)
  • 字符串索引超出范围,全部替换(String index out of range with replace all)
  • 如何在Objective C中启用下一个文本字段(How to enable the next textfield in Objective C)
  • 如何在Spring webflux WebExceptionHandlder中将消息写入http主体(How to write messages to http body in Spring webflux WebExceptionHandlder)
  • 将sap.ui.table.Table的行绑定到sap.ui.model.odata.v2.ODataModel的方法 - url中缺少参数(Bind sap.ui.table.Table's rows to sap.ui.model.odata.v2.ODataModel's method - parameter is missing from url)
  • 两个“全局”/应用程序布局(Two “global”/application layouts)
  • UIView的子类没有view属性(Subclass of a UIView doesn't have a view property)
  • PCA Biplot:一种隐藏矢量的方法,可清楚地查看所有数据点(PCA Biplot : A way to hide vectors to see all data points clearly)