123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- /*
- * *********************************************************************************************************************
- *
- * !!
- * .F88X
- * X8888Y
- * .}888888N;
- * i888888N; .:! .I$WI:
- * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
- * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
- * +888888N; .8888888Y "&&8Y.}8,
- * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
- * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
- * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
- * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
- * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
- * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
- * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
- * .:R888888I
- * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司
- * ~8888'
- * .!88~ All rights reserved.
- *
- * *********************************************************************************************************************
- */
- import { SStringBuilder } from "@persagy-web/base";
- import {
- SFont,
- SLine,
- SPaintEngine,
- SPaintEngineType,
- SPoint,
- SRect
- } from "..";
- import { SPath } from "../SPath";
- /**
- * Canvas 绘制引擎基类
- *
- * @author 庞利祥 <sybotan@126.com>
- */
- export class SSvgPaintEngine extends SPaintEngine {
- /** 字符串 builder */
- private _builder = new SStringBuilder();
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // 属性定义
- /** svg 版本号(1.1) */
- version = "1.1";
- /** 宽度 */
- width: number = 0;
- /** 高度 */
- height: number = 0;
- /**
- * 绘制引擎类型
- *
- * @return 返回 SVG 绘制引擎类型
- */
- get type(): SPaintEngineType {
- return SPaintEngineType.SVG;
- } // Get type
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // 构造函数
- /**
- * 构造函数
- *
- * @param w 宽度
- * @param h 高度
- */
- constructor(w: number, h: number) {
- super();
- this.width = w;
- this.height = h;
- this._builder.append(`<?xml version='1.0' standalone='no'?>`);
- this._builder.append(`<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"`);
- this._builder.append(
- ` "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">`
- );
- this._builder.append(
- `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"`
- );
- this._builder.append(
- `version="${this.version}" width="${w}" height="${h}">`
- );
- } // Constructor
- /**
- * 转 svg 图像
- *
- * @return svg图像
- */
- toSvg(): string {
- return this._builder.toString("\n") + "</svg>\n";
- } // Function toSvg()
- // =================================================================================================================
- // 绘制图形
- /**
- * 清空矩形区域
- *
- * @param rect 矩形
- */
- clearRect(rect: SRect): void {} // Function clearRect()
- /**
- * 绘制空心矩形
- *
- * @param rect 矩形
- */
- drawRect(rect: SRect): void {
- this._builder.append(
- // eslint-disable-next-line max-len
- `<rect x="${rect.x}" y="${rect.y}" width="${rect.width}" height="${
- rect.height
- }" ${this.getStyle(true, true, false)} ${this.getSvgMatrix()}/>`
- );
- } // Function drawRects()
- /**
- * 绘制圆形
- *
- * @param cx 圆心 X 坐标
- * @param cy 圆心 Y 坐标
- * @param r 圆半径
- */
- drawCircle(cx: number, cy: number, r: number): void {
- this._builder.append(
- `<circle cx="${cx}" cy="${cy}" r="${r}" ${this.getStyle(
- true,
- true,
- false
- )} ${this.getSvgMatrix()}/>`
- );
- } // Function drawCircle()
- /**
- * 绘制椭圆
- *
- * @param cx 圆点 X 坐标
- * @param cy 圆点 Y 坐标
- * @param rx 水平半径
- * @param ry 垂直半径
- */
- drawEllipse(cx: number, cy: number, rx: number, ry: number): void {
- this._builder.append(
- `<ellipse cx="${cx}" cy="${cy}" rx="${rx}" ry="${ry}" ${this.getStyle(
- true,
- true,
- false
- )} ${this.getSvgMatrix()}/>`
- );
- } // Function drawEllipse()
- /**
- * 绘制线段
- *
- * @param line 线段
- */
- drawLine(line: SLine): void {
- this._builder.append(
- `<line x1="${line.x1}" y1="${line.y1}" x2="${line.x2}" y2="${
- line.y2
- }" ${this.getStyle(false, true, false)} ${this.getSvgMatrix()}/>`
- );
- } // Function drawLine()
- /**
- * 绘制折线
- *
- * @param points 折线折点
- */
- drawPolyline(points: SPoint[]): void {
- this._builder.append(
- `<polyline points="${SSvgPaintEngine.pointsToStr(
- points
- )}" ${this.getStyle(false, true, false)} ${this.getSvgMatrix()}/>`
- );
- } // Function drawPolyline()
- /**
- * 绘制多边形
- *
- * @param points 多边形顶点
- */
- drawPolygon(points: SPoint[]): void {
- this._builder.append(
- `<polygon points="${SSvgPaintEngine.pointsToStr(
- points
- )}" ${this.getStyle(true, true, false)} ${this.getSvgMatrix()}/>`
- );
- } // Function drawPolyline()
- /**
- * 绘制路径
- *
- * @param path 路径
- */
- drawPath(path: SPath): void {
- // this._builder.append(
- // `<path d="${path.svgPath()}" ${this.getStyle(
- // true,
- // true,
- // false
- // )} ${this.getSvgMatrix()}/>`
- // );
- } // Function drawPath()
- /**
- * 绘制文本
- *
- * @param text 文本内容
- * @param x X 坐标
- * @param y Y 坐标
- * @param maxWidth 最大宽度
- */
- drawText(text: string, x: number, y: number, maxWidth?: number): void {
- this._builder.append(
- `<text x="${x}" y="${y}" ${this.getStyle(
- true,
- false,
- true
- )} ${this.getSvgMatrix()}>${text}</text>>`
- );
- } // Function drawText()
- /**
- * 绘制图片
- *
- * @param img 图片
- * @param x X 坐标
- * @param y Y 坐标
- * @param width 宽度
- * @param height 高度
- */
- drawImage(
- img: CanvasImageSource,
- x: number,
- y: number,
- width?: number,
- height?: number
- ): void {
- // TODO: 未完成
- } // Function drawImage()
- /**
- * 预测量文本宽度
- *
- * @param text 预测的文本
- * @return 文本长度像素
- */
- textWidth(text: string): number {
- return 0;
- } // Function textWidth()
- /**
- * 设置字体
- *
- * @param font 字体
- */
- changeFont(font: SFont): void {} // Function changeFont()
- /**
- * 获得svg需要的变形信息
- *
- * @return 变换信息
- */
- private getSvgMatrix(): string {
- // eslint-disable-next-line max-len
- return `transform="matrix(${this.state.matrix.a}, ${this.state.matrix.b}, ${this.state.matrix.c}, ${this.state.matrix.d}, ${this.state.matrix.e}, ${this.state.matrix.f})"`;
- } // Function getMatrix()
- /**
- * 将点列表转换为字符串表达形式
- *
- * @param points 被转换的点
- * @return 点列表字符串
- */
- private static pointsToStr(points: SPoint[]): string {
- let strBuilder = new SStringBuilder();
- for (let p of points) {
- strBuilder.append(`${p.x},${p.y}`);
- }
- return strBuilder.toString(" ");
- } // Function pointsToStr()
- /** 获得风格 */
- private getStyle(
- brush: boolean = false,
- pen: boolean = false,
- font: boolean = false
- ): string {
- let builder = new SStringBuilder();
- builder.append('style="');
- if (pen) {
- // 画笔
- // eslint-disable-next-line max-len
- builder.append(
- `stroke:rgba(${this.state.pen.color.red}, ${
- this.state.pen.color.green
- }, ${this.state.pen.color.blue}, ${this.state.pen.color.alpha /
- 255.0});`
- );
- builder.append(`stroke-width:${this.state.pen.lineWidth};`);
- } else {
- builder.append(`stroke-width:0;`);
- }
- if (brush) {
- // 画刷
- builder.append(
- `fill:rgba(${this.state.brush.color.red}, ${
- this.state.brush.color.green
- }, ${this.state.brush.color.blue}, ${this.state.brush.color
- .alpha / 255.0});`
- );
- } else {
- builder.append(`fill:rgba(0,0,0,0);`);
- }
- if (font) {
- // 字体
- // builder.append(`font-family:"${this.state.font.name}";`);
- builder.append(`font-size:${this.state.font.size}px;`);
- // builder.append(`text-align:${this.state.font.textAlign};`);
- }
- builder.append('"');
- return builder.toString("");
- } // Get penStyle
- /**
- * 绘制圆角矩形
- */
- drawRoundRect(): void {} // Function drawRoundRect()
- } // Class SSvgPaintEngine
|