python如何画svg路径_如何将文本转换为SVG路径?

小编典典

我创建了自己的类来处理SVG字体文件并将文本转换为字形。使用示例:

include "SVGFont.php";

$svgFont = new SVGFont();

$svgFont->load("/path/to/font.svg");

$result = $svgFont->textToPaths("Simple text", 20);

结果示例:

我班的代码:

/**

* This class represents SVG pa

* @author Łukasz Ledóchowski lukasz@ledochowski.pl

* @version 0.1

*/

class SVGFont {

protected $id = '';

protected $horizAdvX = 0;

protected $unitsPerEm = 0;

protected $ascent = 0;

protected $descent = 0;

protected $glyphs = array();

/**

* Function takes UTF-8 encoded string and returns unicode number for every character.

* Copied somewhere from internet, thanks.

*/

function utf8ToUnicode( $str ) {

$unicode = array();

$values = array();

$lookingFor = 1;

for ($i = 0; $i < strlen( $str ); $i++ ) {

$thisValue = ord( $str[ $i ] );

if ( $thisValue < 128 ) $unicode[] = $thisValue;

else {

if ( count( $values ) == 0 ) $lookingFor = ( $thisValue < 224 ) ? 2 : 3;

$values[] = $thisValue;

if ( count( $values ) == $lookingFor ) {

$number = ( $lookingFor == 3 ) ?

( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):

( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );

$unicode[] = $number;

$values = array();

$lookingFor = 1;

}

}

}

return $unicode;

}

/**

* Function takes path to SVG font (local path) and processes its xml

* to get path representation of every character and additional

* font parameters

*/

public function load($filename) {

$this->glyphs = array();

$z = new XMLReader;

$z->open($filename);

// move to the first node

while ($z->read()) {

$name = $z->name;

if ($z->nodeType == XMLReader::ELEMENT) {

if ($name == 'font') {

$this->id = $z->getAttribute('id');

$this->horizAdvX = $z->getAttribute('horiz-adv-x');

}

if ($name == 'font-face') {

$this->unitsPerEm = $z->getAttribute('units-per-em');

$this->ascent = $z->getAttribute('ascent');

$this->descent = $z->getAttribute('descent');

}

if ($name == 'glyph') {

$unicode = $z->getAttribute('unicode');

$unicode = $this->utf8ToUnicode($unicode);

$unicode = $unicode[0];

$this->glyphs[$unicode] = new stdClass();

$this->glyphs[$unicode]->horizAdvX = $z->getAttribute('horiz-adv-x');

if (empty($this->glyphs[$unicode]->horizAdvX)) {

$this->glyphs[$unicode]->horizAdvX = $this->horizAdvX;

}

$this->glyphs[$unicode]->d = $z->getAttribute('d');

}

}

}

}

/**

* Function takes UTF-8 encoded string and size, returns xml for SVG paths representing this string.

* @param string $text UTF-8 encoded text

* @param int $asize size of requested text

* @return string xml for text converted into SVG paths

*/

public function textToPaths($text, $asize) {

$lines = explode("\n", $text);

$result = "";

$horizAdvY = 0;

foreach($lines as $text) {

$text = $this->utf8ToUnicode($text);

$size = ((float)$asize) / $this->unitsPerEm;

$result .= "";

$horizAdvX = 0;

for($i = 0; $i < count($text); $i++) {

$letter = $text[$i];

$result .= "glyphs[$letter]->d}\" />";

$horizAdvX += $this->glyphs[$letter]->horizAdvX;

}

$result .= "";

$horizAdvY += $this->ascent + $this->descent;

}

return $result;

}

}

2020-05-29

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值