我有一个canvas,要求用户绘制字母A-Z、a-z或数字0-9。一旦用户绘制了一个字符比如A,当前的canvas对象就会被保存在一个canvas列表中;然后另一个空白的canvas弹出来,让用户绘制B,以此类推。
这个程序运行得很好。但是我想创建一个包含所有接受绘制字符的.ttf
文件。
在最后一个显示的canvas下面,我有一个按钮,当点击它时,将从canvas列表中提取所有字符,并从中创建一个.ttf
文件。但怎么做呢?
我有一个canvas,要求用户绘制字母A-Z、a-z或数字0-9。一旦用户绘制了一个字符比如A,当前的canvas对象就会被保存在一个canvas列表中;然后另一个空白的canvas弹出来,让用户绘制B,以此类推。
这个程序运行得很好。但是我想创建一个包含所有接受绘制字符的.ttf
文件。
在最后一个显示的canvas下面,我有一个按钮,当点击它时,将从canvas列表中提取所有字符,并从中创建一个.ttf
文件。但怎么做呢?
我使用过这个库: Fontastic字体制作器
它的工作原理如下:
如何创建一个新的Fontastic对象:
Fontastic f = new Fontastic(this, "ExampleFont"); // Create a new Fontastic object
f.setAuthor("Andreas Koller"); // Set author name - will be saved in TTF file too
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); // Assign contour to character A
f.buildFont(); // Build the font resulting in .ttf and .woff files
// and a HTML template to preview the WOFF
您可以使用java.awt.Font,这是一个Java类,用于管理和创建字体。您可以使用它的Font(String name, int style, int size)构造函数来创建自定义字体:
Font(String name, int style, int size):
从指定的名称、样式和点大小创建新字体。字体名称可以是字体面名或字体系列名。它与样式一起用于查找适当的字体面。当指定字体系列名称时,样式参数用于从系列中选择最合适的字体面。当指定字体面名称时,将合并面的样式和样式参数以从同一系列中定位最佳匹配字体。
例如,如果指定面名为“Arial Bold”和样式Font.ITALIC,则字体系统会在“Arial”系列中查找加粗和斜体的字体面,并可能将字体实例与物理字体面“Arial Bold Italic”相关联。样式参数与指定面的样式合并,而不是添加或减去。这意味着,指定粗体面和粗体样式不会使字体加倍加粗,指定粗体面和普通样式不会使字体变亮。
参数:
name - 字体名称。这可以是字体面名称或字体系列名称,并且可以表示此GraphicsEnvironment中找到的逻辑字体或物理字体。逻辑字体的家族名称为:Dialog、DialogInput、Monospaced、Serif或SansSerif。对于所有这些名称都有预定义的String常量,例如DIALOG。如果name为null,则将新Font的逻辑字体名称设置为getName()返回的名称“Default”。
style - Font的样式常数。样式参数是一个整数位掩码,可以是PLAIN,也可以是BOLD和/或ITALIC的按位联合(例如ITALIC或BOLD|ITALIC)。如果样式参数不符合预期的整数位掩码之一,则样式设置为PLAIN。
size - 字体大小。
正如您所看到的,它允许您基于现有字体创建新字体,您还可以更改样式和大小。
这是教程,您可以按照其进行操作。
Fontastic f = new Fontastic(this, "ExampleFont"); // Create a new Fontastic object
f.setAuthor("Andreas Koller");
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);
f.buildFont();
如何进行清理:
f.cleanup();
您可以查看他们的示例。
但在这两种情况下,您将无法从手写扫描图像创建字体,Fantastic
不允许您从图像创建字体,但它使用矢量图形来创建字体,因此在这种情况下,解决方案是:
使用另一个工具将您的图像转换为矢量图形(Vector Magic,Inkscape,myScriptFont.com)
然后使用生成的矢量
创建您的字体
。