我正在尝试自动分离字体或其他项目,但我就是找不到答案。我所说的separate是指将每个ascii值从32到126的字符保存到多个svg文件中(最好将文本转换为路径)。我的问题是,如何生成多个svg文件,每个文件都包含来自特定(ttf)字体的特定字符。
我写道https://github.com/Pomax/PHP-Font-Parser因为这是很久以前的事了;fonttest.php生成JSON,但如果您实际运行代码,您会看到JSON还包含SVG路径轮廓,您可以将其放入带有<path d="..."/>
元素的SVG框架中,并设置一个与字形的JSON提供的维度相匹配的视图框。
下面的代码将使用给定的字体从一个字母创建一个图像。按照这个步骤获取您的图像,然后在这里查看如何将其转换为SVG。。
// Create an image from the letter "A" using a certain font at 12 points
$image = createTextImage("A", "12", "font.ttf");
/**
* Make an image of text
* @param type $text
* @param type $size
* @param type $color
* @param type $font
*/
function createTextImage($text, $font_size, $font_file){
// Make sure font file exists
if(!file_exists($font_file)) throw new Exception("Font file does not exist: {$font_file}");
// Retrieve bounding box:
$type_space = imagettfbbox($font_size, 0, $font_file, $text);
// Determine image width and height, 10 pixels are added for 5 pixels padding:
$image_width = abs($type_space[4] - $type_space[0]) + 10;
$image_height = abs($type_space[5] - $type_space[1]) + 10;
$line_height = getLineHeight($font_size, $font_file) +10;
// Create image:
$image = imagecreatetruecolor($image_width, $image_height);
// Allocate text and background colors (RGB format):
$text_color = imagecolorallocate($image, 250, 250, 250);
// Fill with transparent background
imagealphablending($image, false);
imagesavealpha($image, true);
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefill($image, 0, 0, $transparent);
// Fix starting x and y coordinates for the text:
$x = 5; // Padding of 5 pixels.
$y = $line_height - 5; // So that the text is vertically centered.
// Add TrueType text to image:
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font_file, $text);
// Save the image to a temp png file to use in our constructor
$tmpname = tempnam('/tmp', 'IMG');
// Generate and save image
imagepng($image, $tmpname, 9);
return $tmpname;
}
/**
* Returns the line height based on the font and font size
* @param type $fontSize
* @param type $fontFace
*/
function getLineHeight($fontSize, $fontFace){
// Arbitrary text is drawn, can't be blank or just a space
$type_space = imagettfbbox($fontSize, 0, $fontFace, "Robert is awesome!");
$line_height = abs($type_space[5] - $type_space[1]);
return $line_height;
}