首页 \ 问答 \ 用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

相关文章

更多

最新问答

更多
  • 如何避免MDIParent表单调整大小(How do I avoid MDIParent form from resizing)
  • Angular自定义指令调用另一个自定义指令(Angular custom directive calling another custom directive)
  • 如何通过参数将文本解析并附加到SQL Server 2005中的存储过程(How to Parse and Append text to a stored procedure in SQL Server 2005 via a parameter)
  • OpenCV中心Homography(OpenCV Center Homography)
  • 如何在linux shell脚本中计算日期-N?(How to calculate date -N in linux shell scripting?)
  • 如何使用Devise创建用户配置文件?(How Do I Create a User Profile With Devise?)
  • 清理模式以管理树上的多步异步过程(Clean pattern to manage multi-step async processes on a tree)
  • 场景的角色(Role of the Scene)
  • 组件中的Angular 2组件[重复](Angular 2 Component In Component [duplicate])
  • jsPlumb draggable element javascript函数(jsPlumb draggable element javascript function)
  • MVC4:ViewModel(带有radiobuttonlist)在HttpPost之后为空(MVC4: ViewModel (with radiobuttonlist) is empty after HttpPost)
  • 如何在同一帐户上设置“Dev repo”(在prod和团队之间)(How to set up a “Dev repo” (between the prod and the team) on the same account)
  • 如何在tcl中将eth0配置为发送方udp端口(how to configure eth0 as a sender udp port in tcl)
  • 如何使用预定义文本替换来自数据库的部分结果(How do I replace part of result coming from Database with predefined text)
  • Selenium Java注入了新的Javascript函数(Selenium Java inject new Javascript function)
  • 使用.on的多个下拉菜单选择文本仅适用于第一个下拉列表(Multiple Dropdowns Menu Selection text using .on works only on first dropdown)
  • 快速将黄土曲线添加到大型数据集图中的方法(Quick way to add loess curve to large data set graph)
  • FilteringSelect in mvc(FilteringSelect in mvc)
  • 在Delphi XE2中开发Mac或iOS应用程序需要哪些硬件/软件?(What hardware/software is necessary to develop Mac or iOS apps in Delphi XE2?)
  • 在原型的构造函数中初始化属性时获取“未定义”(Getting 'undefined' when a property is initialized in the constructor of a prototype)
  • 通过越狱加载的应用程序的Documents文件夹位置(Location of Documents folder for an app loaded via jailbreak)
  • 在OpenGL中使用可编程和固定管道功能(Using both programmable and fixed pipeline functionality in OpenGL)
  • 将任何用户输入重定向到单独的底层程序(redirect any user input to a separate underlying program)
  • 编辑文本不能正常工作android(Edit texts not working properly android)
  • “user_denied”Facebook应用页面上的Facebook用户区域设置(Facebook user locale on “user_denied” facebook app page)
  • 在大图像中找到小的部分透明图像的坐标(find coordinates of small partially-transparent image within a large image)
  • 我如何在cakephp 3.1中获得完整的相对路径?(How i can get full relative path of image in cakephp 3.1?)
  • 如何保存拖动标记的新本地化?(How to save new localization of dragged marker?)
  • MySQL UPDATE vs INSERT和DELETE(MySQL UPDATE vs INSERT and DELETE)
  • 在执行查询之前,在SQLAlchemy模型中将datetime转换为unix时间戳?(Convert datetime to unix timestamp in SQLAlchemy model before executing query?)