kartsims/easysvg

用于生成SVG XML的PHP库

2.5 2023-03-31 08:58 UTC

This package is auto-updated.

Last update: 2024-08-23 10:07:16 UTC


README

轻松地将SVG字体生成SVG图像。

这里生成的SVG数据直接从字体.svg文件中提取。这 不使用<text> 标签。

简单用法

require 'easySVG.php';

$svg = new EasySVG();
$svg->setFont("om_telolet_om-webfont.svg", 100, '#000000');
$svg->addText("Simple text display");
$svg->addAttribute("width", "800px");
$svg->addAttribute("height", "120px");
echo $svg->asXML();

高级用法

require 'easySVG.php';

$text = "Simple text display\netc.";

$svg = new EasySVG();
$svg->setFontSVG("om_telolet_om-webfont.svg");
$svg->setFontSize(100);
$svg->setFontColor('#000000');
$svg->setLineHeight(1.2);
$svg->setLetterSpacing(.1);
$svg->setUseKerning(true);
$svg->addText($text);
// set width/height according to text
list($textWidth, $textHeight) = $svg->textDimensions($text);
$svg->addAttribute("width", $textWidth."px");
$svg->addAttribute("height", $textHeight."px");
echo $svg->asXML();

这将为您输出内联SVG,您可以 echo 它,保存 它到文件或做其他任何事。

方法参考

setFont($path, $size, $color = null)

设置字体属性。这是一个快捷方式

$this->setFontSVG($path);
$this->setFontSize($size);
$this->setFontColor($color);

这三个方法已经足够明确,我不会在这里详细说明。

setUseKerning($bool)

使用SVG字体 kerning 对。默认为 false

setLineHeight($value)

添加类似CSS的行高值。一个数字值(浮点数),其中1是字体本身定义的行高。

setLetterSpacing($value)

添加类似CSS的字母间距值。一个数字值(浮点数),以em为单位表示,其中1是字符 m 的宽度。

addText($text, $x, $y, $attributes=array())

向SVG添加文本(将被转换为简单路径)

  • $text : UTF-8编码的字符串
  • $x : 文本的X位置(从左侧开始),可以是 center 以水平居中文本
  • $y : 文本的Y位置(从顶部开始),可以是 center 以垂直居中文本
  • $attributes (可选) : 标签属性列表

asXML()

返回整个SVG的XML字符串。

addAttribute($key, $value)

向主SVG添加属性。

SVG数据操作方法

您可能需要这些方法来处理SVG定义。

defTranslate($def, $x=0, $y=0)

将平移变换应用于定义。这基本上是对定义应用矩阵计算。

defRotate($def, $angle, $x=0, $y=0)

将平移变换应用于定义。这基本上是对定义应用矩阵计算。

defScale($def, $x=1, $y=1)

将缩放变换应用于定义。这基本上是对定义应用矩阵计算。

textDef($text)

返回字符串的SVG格式定义。此方法由addText方法使用。

  • $text : UTF-8编码的字符串

textDimensions($text)

返回字符串的宽度和高度。此方法还用于设置SVG的宽度/高度(如果未指定)。

  • $text : UTF-8编码的字符串

unicodeDef($code)

返回Unicode字符的SVG格式定义。

  • $code : Unicode定义(十六进制格式)

characterWidth($char, $is_unicode=false)

返回字符的宽度。

  • $char : 字符
  • $is_unicode : 布尔值,指示字符是否为unicode字符串或UTF-8字符。

addPath($def, $attributes=array())

向SVG数据添加路径

  • $def : SVG定义
  • $attributes (可选) : 标签属性列表

实用方法

clearSVG()

重置SVG数据。用于在不创建新实例的情况下开始新的SVG。

defApplyMatrix($def, $matrix)

将矩阵应用于定义。用于应用任何类型的变换,您可能不需要它,但它可用,您可以玩玩它。

许可

MIT。请随意拉取、分支等。