2019-03-05 08:36:59 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright 2019 Abakkk
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2021-05-27 13:54:08 -03:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2019-03-05 08:36:59 -03:00
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-05-27 13:54:08 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-FileCopyrightText: 2019 Abakkk
|
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2019-03-05 08:36:59 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-05-27 13:54:08 -03:00
|
|
|
/* jslint esversion: 6 */
|
|
|
|
|
/* exported Shape, TextAlignment, Transformation, getAllFontFamilies, DrawingElement */
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
const Cairo = imports.cairo;
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
|
const Lang = imports.lang;
|
2020-06-26 07:19:53 -03:00
|
|
|
const Pango = imports.gi.Pango;
|
|
|
|
|
const PangoCairo = imports.gi.PangoCairo;
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2020-09-25 06:55:36 -03:00
|
|
|
const Me = imports.misc.extensionUtils.getCurrentExtension();
|
2020-10-04 05:01:55 -03:00
|
|
|
const UUID = Me.uuid.replace(/@/gi, '_at_').replace(/[^a-z0-9+_-]/gi, '_');
|
2020-09-25 06:55:36 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
var Shape = { NONE: 0, LINE: 1, ELLIPSE: 2, RECTANGLE: 3, TEXT: 4, POLYGON: 5, POLYLINE: 6, IMAGE: 7 };
|
2021-02-17 10:27:35 -03:00
|
|
|
var TextAlignment = { LEFT: 0, CENTER: 1, RIGHT: 2 };
|
2021-02-17 10:53:13 -03:00
|
|
|
var Transformation = { TRANSLATION: 0, ROTATION: 1, SCALE_PRESERVE: 2, STRETCH: 3, REFLECTION: 4, INVERSION: 5, SMOOTH: 100 };
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2020-09-10 10:19:17 -03:00
|
|
|
var getAllFontFamilies = function() {
|
2020-08-05 18:30:25 -03:00
|
|
|
return PangoCairo.font_map_get_default().list_families().map(fontFamily => fontFamily.get_name()).sort((a,b) => a.localeCompare(b));
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 13:56:48 -03:00
|
|
|
const getFillRuleSvgName = function(fillRule) {
|
2020-09-09 07:12:29 -03:00
|
|
|
return fillRule == Cairo.FillRule.EVEN_ODD ? 'evenodd' : 'nonzero';
|
2020-09-07 13:56:48 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getLineCapSvgName = function(lineCap) {
|
2020-09-09 07:12:29 -03:00
|
|
|
return lineCap == Cairo.LineCap.BUTT ? 'butt' :
|
|
|
|
|
lineCap == Cairo.LineCap.SQUASH ? 'square' :
|
2020-09-07 13:56:48 -03:00
|
|
|
'round';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getLineJoinSvgName = function(lineJoin) {
|
2020-09-09 07:12:29 -03:00
|
|
|
return lineJoin == Cairo.LineJoin.MITER ? 'miter' :
|
|
|
|
|
lineJoin == Cairo.LineJoin.BEVEL ? 'bevel' :
|
2020-09-07 13:56:48 -03:00
|
|
|
'round';
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-04 03:35:59 -03:00
|
|
|
const SVG_DEBUG_SUPERPOSES_CAIRO = false;
|
2020-06-19 15:58:23 -03:00
|
|
|
const RADIAN = 180 / Math.PI; // degree
|
2020-06-18 21:48:23 -03:00
|
|
|
const INVERSION_CIRCLE_RADIUS = 12; // px
|
|
|
|
|
const REFLECTION_TOLERANCE = 5; // px, to select vertical and horizontal directions
|
|
|
|
|
const STRETCH_TOLERANCE = Math.PI / 8; // rad, to select vertical and horizontal directions
|
|
|
|
|
const MIN_REFLECTION_LINE_LENGTH = 10; // px
|
|
|
|
|
const MIN_TRANSLATION_DISTANCE = 1; // px
|
|
|
|
|
const MIN_ROTATION_ANGLE = Math.PI / 1000; // rad
|
|
|
|
|
const MIN_DRAWING_SIZE = 3; // px
|
2020-09-30 14:16:55 -03:00
|
|
|
const MIN_INTERMEDIATE_POINT_DISTANCE = 1; // px, the higher it is, the fewer points there will be
|
2021-02-18 05:07:12 -03:00
|
|
|
const MARK_COLOR = Clutter.Color.get_static(Clutter.StaticColor.BLUE);
|
2020-06-17 13:30:57 -03:00
|
|
|
|
2020-07-08 06:47:51 -03:00
|
|
|
var DrawingElement = function(params) {
|
2021-02-17 10:53:13 -03:00
|
|
|
return params.shape == Shape.TEXT ? new TextElement(params) :
|
|
|
|
|
params.shape == Shape.IMAGE ? new ImageElement(params) :
|
2020-07-30 06:13:23 -03:00
|
|
|
new _DrawingElement(params);
|
2020-07-08 06:47:51 -03:00
|
|
|
};
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
// DrawingElement represents a "brushstroke".
|
|
|
|
|
// It can be converted into a cairo path as well as a svg element.
|
|
|
|
|
// See DrawingArea._startDrawing() to know its params.
|
2020-07-08 06:47:51 -03:00
|
|
|
const _DrawingElement = new Lang.Class({
|
2020-10-04 05:01:55 -03:00
|
|
|
Name: `${UUID}-DrawingElement`,
|
2019-03-05 08:36:59 -03:00
|
|
|
|
|
|
|
|
_init: function(params) {
|
|
|
|
|
for (let key in params)
|
|
|
|
|
this[key] = params[key];
|
2020-06-08 15:50:23 -03:00
|
|
|
|
|
|
|
|
// compatibility with json generated by old extension versions
|
2020-06-11 22:45:51 -03:00
|
|
|
|
|
|
|
|
if (params.transformations === undefined)
|
|
|
|
|
this.transformations = [];
|
2020-09-10 10:19:17 -03:00
|
|
|
|
2020-09-14 19:29:02 -03:00
|
|
|
if (params.font && !(params.font instanceof Pango.FontDescription)) {
|
2020-09-04 19:03:05 -03:00
|
|
|
// compatibility with v6.2-
|
2020-09-10 10:19:17 -03:00
|
|
|
if (params.font.weight === 0)
|
|
|
|
|
this.font.weight = 400;
|
|
|
|
|
else if (params.font.weight === 1)
|
|
|
|
|
this.font.weight = 700;
|
2020-09-14 19:29:02 -03:00
|
|
|
this.font = new Pango.FontDescription();
|
2020-09-04 19:03:05 -03:00
|
|
|
['family', 'weight', 'style', 'stretch', 'variant'].forEach(attribute => {
|
|
|
|
|
if (params.font[attribute] !== undefined)
|
|
|
|
|
try {
|
2020-09-14 19:29:02 -03:00
|
|
|
this.font[`set_${attribute}`](params.font[attribute]);
|
2020-09-04 19:03:05 -03:00
|
|
|
} catch(e) {}
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-06-11 22:45:51 -03:00
|
|
|
|
|
|
|
|
if (params.transform && params.transform.center) {
|
|
|
|
|
let angle = (params.transform.angle || 0) + (params.transform.startAngle || 0);
|
|
|
|
|
if (angle)
|
2021-02-17 10:53:13 -03:00
|
|
|
this.transformations.push({ type: Transformation.ROTATION, angle: angle });
|
2020-06-11 22:45:51 -03:00
|
|
|
}
|
2021-02-17 10:53:13 -03:00
|
|
|
if (params.shape == Shape.ELLIPSE && params.transform && params.transform.ratio && params.transform.ratio != 1 && params.points.length >= 2) {
|
2020-06-11 22:45:51 -03:00
|
|
|
let [ratio, p0, p1] = [params.transform.ratio, params.points[0], params.points[1]];
|
|
|
|
|
// Add a fake point that will give the right ellipse ratio when building the element.
|
|
|
|
|
this.points.push([ratio * (p1[0] - p0[0]) + p0[0], ratio * (p1[1] - p0[1]) + p0[1]]);
|
|
|
|
|
}
|
|
|
|
|
delete this.transform;
|
2021-02-17 10:27:35 -03:00
|
|
|
|
|
|
|
|
// v10-
|
|
|
|
|
if (this.textRightAligned)
|
|
|
|
|
this.textAlignment = TextAlignment.RIGHT;
|
|
|
|
|
delete this.textRightAligned;
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-11 13:53:35 -03:00
|
|
|
// toJSON is called by JSON.stringify
|
|
|
|
|
toJSON: function() {
|
|
|
|
|
return {
|
|
|
|
|
shape: this.shape,
|
2020-09-22 12:31:48 -03:00
|
|
|
color: this.color,
|
2019-03-11 13:53:35 -03:00
|
|
|
line: this.line,
|
|
|
|
|
dash: this.dash,
|
|
|
|
|
fill: this.fill,
|
2020-06-08 15:50:23 -03:00
|
|
|
fillRule: this.fillRule,
|
2019-03-11 13:53:35 -03:00
|
|
|
eraser: this.eraser,
|
2021-02-17 10:53:13 -03:00
|
|
|
transformations: this.transformations.filter(transformation => transformation.type != Transformation.SMOOTH)
|
2020-10-04 17:17:13 -03:00
|
|
|
.map(transformation => Object.assign({}, transformation, { undoable: undefined })),
|
2019-03-11 13:53:35 -03:00
|
|
|
points: this.points.map((point) => [Math.round(point[0]*100)/100, Math.round(point[1]*100)/100])
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-15 17:13:03 -03:00
|
|
|
buildCairo: function(cr, params) {
|
2020-09-14 19:29:02 -03:00
|
|
|
if (this.color)
|
|
|
|
|
Clutter.cairo_set_source_color(cr, this.color);
|
2020-06-17 13:30:57 -03:00
|
|
|
|
2020-07-08 06:47:51 -03:00
|
|
|
if (this.line) {
|
|
|
|
|
cr.setLineCap(this.line.lineCap);
|
|
|
|
|
cr.setLineJoin(this.line.lineJoin);
|
|
|
|
|
cr.setLineWidth(this.line.lineWidth);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-27 08:40:34 -03:00
|
|
|
if (this.fillRule)
|
|
|
|
|
cr.setFillRule(this.fillRule);
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2020-06-27 08:40:34 -03:00
|
|
|
if (this.dash && this.dash.active && this.dash.array && this.dash.array[0] && this.dash.array[1])
|
2019-03-05 08:36:59 -03:00
|
|
|
cr.setDash(this.dash.array, this.dash.offset);
|
|
|
|
|
|
|
|
|
|
if (this.eraser)
|
|
|
|
|
cr.setOperator(Cairo.Operator.CLEAR);
|
|
|
|
|
else
|
|
|
|
|
cr.setOperator(Cairo.Operator.OVER);
|
|
|
|
|
|
2020-06-19 15:58:23 -03:00
|
|
|
if (params.dummyStroke)
|
|
|
|
|
setDummyStroke(cr);
|
|
|
|
|
|
2020-06-17 10:43:23 -03:00
|
|
|
if (SVG_DEBUG_SUPERPOSES_CAIRO) {
|
2020-06-17 13:30:57 -03:00
|
|
|
Clutter.cairo_set_source_color(cr, Clutter.Color.new(255, 0, 0, 255));
|
2020-06-17 10:43:23 -03:00
|
|
|
cr.setLineWidth(this.line.lineWidth / 2 || 1);
|
|
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2020-06-11 22:45:51 -03:00
|
|
|
this.transformations.slice(0).reverse().forEach(transformation => {
|
2021-02-17 10:53:13 -03:00
|
|
|
if (transformation.type == Transformation.TRANSLATION) {
|
2020-06-11 22:45:51 -03:00
|
|
|
cr.translate(transformation.slideX, transformation.slideY);
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.ROTATION) {
|
2020-06-18 15:21:56 -03:00
|
|
|
let center = this._getTransformedCenter(transformation);
|
2020-06-19 15:58:23 -03:00
|
|
|
cr.translate(center[0], center[1]);
|
|
|
|
|
cr.rotate(transformation.angle);
|
|
|
|
|
cr.translate(-center[0], -center[1]);
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.SCALE_PRESERVE || transformation.type == Transformation.STRETCH) {
|
2020-06-18 15:21:56 -03:00
|
|
|
let center = this._getTransformedCenter(transformation);
|
2020-06-19 15:58:23 -03:00
|
|
|
cr.translate(center[0], center[1]);
|
|
|
|
|
cr.rotate(transformation.angle);
|
|
|
|
|
cr.scale(transformation.scaleX, transformation.scaleY);
|
|
|
|
|
cr.rotate(-transformation.angle);
|
|
|
|
|
cr.translate(-center[0], -center[1]);
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.REFLECTION || transformation.type == Transformation.INVERSION) {
|
2020-06-17 13:30:57 -03:00
|
|
|
cr.translate(transformation.slideX, transformation.slideY);
|
|
|
|
|
cr.rotate(transformation.angle);
|
|
|
|
|
cr.scale(transformation.scaleX, transformation.scaleY);
|
|
|
|
|
cr.rotate(-transformation.angle);
|
|
|
|
|
cr.translate(-transformation.slideX, -transformation.slideY);
|
2020-06-13 08:53:52 -03:00
|
|
|
}
|
2020-06-11 22:45:51 -03:00
|
|
|
});
|
|
|
|
|
|
2020-07-08 06:47:51 -03:00
|
|
|
this._drawCairo(cr, params);
|
|
|
|
|
|
|
|
|
|
cr.identityMatrix();
|
|
|
|
|
},
|
|
|
|
|
|
2021-02-18 05:07:12 -03:00
|
|
|
_addMarks: function(cr) {
|
|
|
|
|
if (this.showSymmetryElement) {
|
|
|
|
|
setDummyStroke(cr);
|
|
|
|
|
Clutter.cairo_set_source_color(cr, MARK_COLOR);
|
|
|
|
|
|
|
|
|
|
let transformation = this.lastTransformation;
|
|
|
|
|
if (transformation.type == Transformation.REFLECTION) {
|
|
|
|
|
cr.moveTo(transformation.startX, transformation.startY);
|
|
|
|
|
cr.lineTo(transformation.endX, transformation.endY);
|
|
|
|
|
} else {
|
|
|
|
|
cr.arc(transformation.endX, transformation.endY, INVERSION_CIRCLE_RADIUS, 0, 2 * Math.PI);
|
|
|
|
|
}
|
|
|
|
|
cr.stroke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.showRotationCenter) {
|
|
|
|
|
setDummyStroke(cr);
|
|
|
|
|
Clutter.cairo_set_source_color(cr, MARK_COLOR);
|
|
|
|
|
|
|
|
|
|
let center = this._getTransformedCenter(this.lastTransformation);
|
|
|
|
|
cr.arc(center[0], center[1], INVERSION_CIRCLE_RADIUS, 0, 2 * Math.PI);
|
|
|
|
|
cr.stroke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.showStretchAxes) {
|
|
|
|
|
setDummyStroke(cr);
|
|
|
|
|
Clutter.cairo_set_source_color(cr, MARK_COLOR);
|
|
|
|
|
|
|
|
|
|
let center = this._getTransformedCenter(this.lastTransformation);
|
|
|
|
|
for (let i = 0; i <=1; i++) {
|
|
|
|
|
cr.moveTo(center[0] - 1000 * Math.cos(i * Math.PI / 2), center[1] - 1000 * Math.sin(i * Math.PI / 2));
|
|
|
|
|
cr.lineTo(center[0] + 1000 * Math.cos(i * Math.PI / 2), center[1] + 1000 * Math.sin(i * Math.PI / 2));
|
|
|
|
|
}
|
|
|
|
|
cr.stroke();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-08 06:47:51 -03:00
|
|
|
_drawCairo: function(cr, params) {
|
2020-06-11 22:45:51 -03:00
|
|
|
let [points, shape] = [this.points, this.shape];
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
if (shape == Shape.LINE && points.length == 3) {
|
2019-03-07 10:28:35 -03:00
|
|
|
cr.moveTo(points[0][0], points[0][1]);
|
|
|
|
|
cr.curveTo(points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1]);
|
2020-06-07 13:57:11 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (shape == Shape.LINE && points.length == 4) {
|
2020-06-19 10:52:12 -03:00
|
|
|
cr.moveTo(points[0][0], points[0][1]);
|
|
|
|
|
cr.curveTo(points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
|
|
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (shape == Shape.NONE || shape == Shape.LINE) {
|
2019-03-05 08:36:59 -03:00
|
|
|
cr.moveTo(points[0][0], points[0][1]);
|
|
|
|
|
for (let j = 1; j < points.length; j++) {
|
|
|
|
|
cr.lineTo(points[j][0], points[j][1]);
|
|
|
|
|
}
|
2019-03-07 10:28:35 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (shape == Shape.ELLIPSE && points.length >= 2) {
|
2020-06-19 15:58:23 -03:00
|
|
|
let radius = Math.hypot(points[1][0] - points[0][0], points[1][1] - points[0][1]);
|
2020-06-11 22:45:51 -03:00
|
|
|
let ratio = 1;
|
|
|
|
|
|
2020-06-19 15:58:23 -03:00
|
|
|
if (points[2]) {
|
|
|
|
|
ratio = Math.hypot(points[2][0] - points[0][0], points[2][1] - points[0][1]) / radius;
|
|
|
|
|
cr.translate(points[0][0], points[0][1]);
|
|
|
|
|
cr.scale(ratio, 1);
|
|
|
|
|
cr.translate(-points[0][0], -points[0][1]);
|
|
|
|
|
cr.arc(points[0][0], points[0][1], radius, 0, 2 * Math.PI);
|
|
|
|
|
cr.translate(points[0][0], points[0][1]);
|
|
|
|
|
cr.scale(1 / ratio, 1);
|
|
|
|
|
cr.translate(-points[0][0], -points[0][1]);
|
|
|
|
|
} else
|
|
|
|
|
cr.arc(points[0][0], points[0][1], radius, 0, 2 * Math.PI);
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (shape == Shape.RECTANGLE && points.length == 2) {
|
2019-03-05 08:36:59 -03:00
|
|
|
cr.rectangle(points[0][0], points[0][1], points[1][0] - points[0][0], points[1][1] - points[0][1]);
|
2020-06-07 13:57:11 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if ((shape == Shape.POLYGON || shape == Shape.POLYLINE) && points.length >= 2) {
|
2020-06-07 13:57:11 -03:00
|
|
|
cr.moveTo(points[0][0], points[0][1]);
|
|
|
|
|
for (let j = 1; j < points.length; j++) {
|
|
|
|
|
cr.lineTo(points[j][0], points[j][1]);
|
|
|
|
|
}
|
2021-02-17 10:53:13 -03:00
|
|
|
if (shape == Shape.POLYGON)
|
2020-06-07 13:57:11 -03:00
|
|
|
cr.closePath();
|
2019-03-07 10:28:35 -03:00
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
getContainsPoint: function(cr, x, y) {
|
2020-06-17 10:27:18 -03:00
|
|
|
cr.save();
|
2020-06-13 08:53:52 -03:00
|
|
|
cr.setLineWidth(Math.max(this.line.lineWidth, 25));
|
|
|
|
|
cr.setDash([], 0);
|
|
|
|
|
|
|
|
|
|
// Check whether the point is inside/on/near the element.
|
2020-06-17 10:27:18 -03:00
|
|
|
let inElement = cr.inStroke(x, y) || this.fill && cr.inFill(x, y);
|
|
|
|
|
cr.restore();
|
2020-06-13 08:53:52 -03:00
|
|
|
return inElement;
|
|
|
|
|
},
|
|
|
|
|
|
2020-09-14 19:29:02 -03:00
|
|
|
buildSVG: function(bgcolorString) {
|
2020-09-16 12:58:06 -03:00
|
|
|
let transforms = [];
|
2020-06-19 15:58:23 -03:00
|
|
|
this.transformations.slice(0).reverse().forEach(transformation => {
|
|
|
|
|
let center = this._getTransformedCenter(transformation);
|
2020-06-15 17:13:03 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
if (transformation.type == Transformation.TRANSLATION) {
|
2020-09-16 12:58:06 -03:00
|
|
|
transforms.push(['translate', transformation.slideX, transformation.slideY]);
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.ROTATION) {
|
2020-09-16 12:58:06 -03:00
|
|
|
transforms.push(['translate', center[0], center[1]]);
|
|
|
|
|
transforms.push(['rotate', transformation.angle * RADIAN]);
|
|
|
|
|
transforms.push(['translate', -center[0], -center[1]]);
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.SCALE_PRESERVE || transformation.type == Transformation.STRETCH) {
|
2020-09-16 12:58:06 -03:00
|
|
|
transforms.push(['translate', center[0], center[1]]);
|
|
|
|
|
transforms.push(['rotate', transformation.angle * RADIAN]);
|
|
|
|
|
transforms.push(['scale', transformation.scaleX, transformation.scaleY]);
|
|
|
|
|
transforms.push(['rotate', -transformation.angle * RADIAN]);
|
|
|
|
|
transforms.push(['translate', -center[0], -center[1]]);
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.REFLECTION || transformation.type == Transformation.INVERSION) {
|
2020-09-16 12:58:06 -03:00
|
|
|
transforms.push(['translate', transformation.slideX, transformation.slideY]);
|
|
|
|
|
transforms.push(['rotate', transformation.angle * RADIAN]);
|
|
|
|
|
transforms.push(['scale', transformation.scaleX, transformation.scaleY]);
|
|
|
|
|
transforms.push(['rotate', -transformation.angle * RADIAN]);
|
|
|
|
|
transforms.push(['translate', -transformation.slideX, -transformation.slideY]);
|
2020-06-15 17:13:03 -03:00
|
|
|
}
|
2020-06-11 22:45:51 -03:00
|
|
|
});
|
2020-09-16 12:58:06 -03:00
|
|
|
|
|
|
|
|
let grouped = [];
|
|
|
|
|
transforms.forEach((transform, index) => {
|
|
|
|
|
let [type, ...values] = transform;
|
|
|
|
|
|
|
|
|
|
if (grouped.length && grouped[grouped.length - 1][0] == type)
|
|
|
|
|
values.forEach((value, valueIndex) => grouped[grouped.length - 1][valueIndex + 1] += value);
|
|
|
|
|
else
|
|
|
|
|
grouped.push(transform);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let filtered = grouped.filter(transform => {
|
|
|
|
|
let [type, ...values] = transform;
|
|
|
|
|
|
|
|
|
|
if (type == 'scale')
|
|
|
|
|
return values.some(value => value != 1);
|
|
|
|
|
else
|
|
|
|
|
return values.some(value => value != 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let transAttribute = '';
|
|
|
|
|
if (filtered.length) {
|
|
|
|
|
transAttribute = ' transform="';
|
|
|
|
|
filtered.forEach((transform, index) => {
|
|
|
|
|
let [type, ...values] = transform;
|
|
|
|
|
transAttribute += `${index == 0 ? '' : ' '}${type}(${values.map(value => Number(value).toFixed(2))})`;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
transAttribute += '"';
|
|
|
|
|
}
|
2020-06-11 22:45:51 -03:00
|
|
|
|
2020-09-14 19:29:02 -03:00
|
|
|
return this._drawSvg(transAttribute, bgcolorString);
|
2020-07-08 06:47:51 -03:00
|
|
|
},
|
|
|
|
|
|
2020-09-14 19:29:02 -03:00
|
|
|
_drawSvg: function(transAttribute, bgcolorString) {
|
2020-07-08 06:47:51 -03:00
|
|
|
let row = "\n ";
|
|
|
|
|
let points = this.points.map((point) => [Math.round(point[0]*100)/100, Math.round(point[1]*100)/100]);
|
2020-09-22 12:31:48 -03:00
|
|
|
let color = this.eraser ? bgcolorString : this.color.toJSON();
|
2020-07-08 06:47:51 -03:00
|
|
|
let fill = this.fill && !this.isStraightLine;
|
2020-09-16 12:58:06 -03:00
|
|
|
let attributes = this.eraser ? `class="eraser" ` : '';
|
2020-07-08 06:47:51 -03:00
|
|
|
|
|
|
|
|
if (fill) {
|
2020-09-16 12:58:06 -03:00
|
|
|
attributes += `fill="${color}"`;
|
2020-07-08 06:47:51 -03:00
|
|
|
if (this.fillRule)
|
2020-09-07 13:56:48 -03:00
|
|
|
attributes += ` fill-rule="${getFillRuleSvgName(this.fillRule)}"`;
|
2020-07-08 06:47:51 -03:00
|
|
|
} else {
|
2020-09-16 12:58:06 -03:00
|
|
|
attributes += `fill="none"`;
|
2020-07-08 06:47:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.line && this.line.lineWidth) {
|
|
|
|
|
attributes += ` stroke="${color}"` +
|
|
|
|
|
` stroke-width="${this.line.lineWidth}"`;
|
|
|
|
|
if (this.line.lineCap)
|
2020-09-07 13:56:48 -03:00
|
|
|
attributes += ` stroke-linecap="${getLineCapSvgName(this.line.lineCap)}"`;
|
2020-07-08 06:47:51 -03:00
|
|
|
if (this.line.lineJoin && !this.isStraightLine)
|
2020-09-07 13:56:48 -03:00
|
|
|
attributes += ` stroke-linejoin="${getLineJoinSvgName(this.line.lineJoin)}"`;
|
2020-07-08 06:47:51 -03:00
|
|
|
if (this.dash && this.dash.active && this.dash.array && this.dash.array[0] && this.dash.array[1])
|
|
|
|
|
attributes += ` stroke-dasharray="${this.dash.array[0]} ${this.dash.array[1]}" stroke-dashoffset="${this.dash.offset}"`;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
if (this.shape == Shape.LINE && points.length == 4) {
|
2020-06-19 10:52:12 -03:00
|
|
|
row += `<path ${attributes} d="M${points[0][0]} ${points[0][1]}`;
|
|
|
|
|
row += ` C ${points[1][0]} ${points[1][1]}, ${points[2][0]} ${points[2][1]}, ${points[3][0]} ${points[3][1]}`;
|
|
|
|
|
row += `${fill ? 'z' : ''}"${transAttribute}/>`;
|
|
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.LINE && points.length == 3) {
|
2019-03-05 08:36:59 -03:00
|
|
|
row += `<path ${attributes} d="M${points[0][0]} ${points[0][1]}`;
|
2019-03-07 10:28:35 -03:00
|
|
|
row += ` C ${points[0][0]} ${points[0][1]}, ${points[1][0]} ${points[1][1]}, ${points[2][0]} ${points[2][1]}`;
|
2020-06-11 22:45:51 -03:00
|
|
|
row += `${fill ? 'z' : ''}"${transAttribute}/>`;
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.LINE) {
|
2020-06-11 22:45:51 -03:00
|
|
|
row += `<line ${attributes} x1="${points[0][0]}" y1="${points[0][1]}" x2="${points[1][0]}" y2="${points[1][1]}"${transAttribute}/>`;
|
2020-06-07 15:08:42 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.NONE) {
|
2019-03-07 10:28:35 -03:00
|
|
|
row += `<path ${attributes} d="M${points[0][0]} ${points[0][1]}`;
|
2020-06-07 13:57:11 -03:00
|
|
|
for (let i = 1; i < points.length; i++)
|
2019-03-05 08:36:59 -03:00
|
|
|
row += ` L ${points[i][0]} ${points[i][1]}`;
|
2020-06-11 22:45:51 -03:00
|
|
|
row += `${fill ? 'z' : ''}"${transAttribute}/>`;
|
2019-03-07 10:28:35 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.ELLIPSE && points.length == 3) {
|
2019-03-07 10:28:35 -03:00
|
|
|
let ry = Math.hypot(points[1][0] - points[0][0], points[1][1] - points[0][1]);
|
2020-06-11 22:45:51 -03:00
|
|
|
let rx = Math.hypot(points[2][0] - points[0][0], points[2][1] - points[0][1]);
|
|
|
|
|
row += `<ellipse ${attributes} cx="${points[0][0]}" cy="${points[0][1]}" rx="${rx}" ry="${ry}"${transAttribute}/>`;
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.ELLIPSE && points.length == 2) {
|
2019-03-05 08:36:59 -03:00
|
|
|
let r = Math.hypot(points[1][0] - points[0][0], points[1][1] - points[0][1]);
|
2020-06-11 22:45:51 -03:00
|
|
|
row += `<circle ${attributes} cx="${points[0][0]}" cy="${points[0][1]}" r="${r}"${transAttribute}/>`;
|
2019-03-07 10:28:35 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.RECTANGLE && points.length == 2) {
|
2019-03-05 08:36:59 -03:00
|
|
|
row += `<rect ${attributes} x="${Math.min(points[0][0], points[1][0])}" y="${Math.min(points[0][1], points[1][1])}" ` +
|
2019-03-07 10:28:35 -03:00
|
|
|
`width="${Math.abs(points[1][0] - points[0][0])}" height="${Math.abs(points[1][1] - points[0][1])}"${transAttribute}/>`;
|
|
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.POLYGON && points.length >= 3) {
|
2020-06-07 13:57:11 -03:00
|
|
|
row += `<polygon ${attributes} points="`;
|
|
|
|
|
for (let i = 0; i < points.length; i++)
|
|
|
|
|
row += ` ${points[i][0]},${points[i][1]}`;
|
|
|
|
|
row += `"${transAttribute}/>`;
|
|
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.POLYLINE && points.length >= 2) {
|
2020-06-07 15:05:30 -03:00
|
|
|
row += `<polyline ${attributes} points="`;
|
|
|
|
|
for (let i = 0; i < points.length; i++)
|
|
|
|
|
row += ` ${points[i][0]},${points[i][1]}`;
|
|
|
|
|
row += `"${transAttribute}/>`;
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return row;
|
2019-03-05 17:08:43 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-11 22:45:51 -03:00
|
|
|
get lastTransformation() {
|
|
|
|
|
if (!this.transformations.length)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return this.transformations[this.transformations.length - 1];
|
2019-03-05 17:08:43 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-17 20:03:50 -03:00
|
|
|
get isStraightLine() {
|
2021-02-17 10:53:13 -03:00
|
|
|
return this.shape == Shape.LINE && this.points.length == 2;
|
2019-03-05 17:08:43 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
smoothAll: function() {
|
2020-10-04 15:55:06 -03:00
|
|
|
let oldPoints = this.points.slice();
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this.points.length; i++)
|
2020-06-19 10:52:12 -03:00
|
|
|
this._smooth(i);
|
2020-10-04 15:55:06 -03:00
|
|
|
|
|
|
|
|
let newPoints = this.points.slice();
|
|
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
this.transformations.push({ type: Transformation.SMOOTH, undoable: true,
|
2020-10-04 15:55:06 -03:00
|
|
|
undo: () => this.points = oldPoints,
|
|
|
|
|
redo: () => this.points = newPoints });
|
2020-10-04 17:48:40 -03:00
|
|
|
|
|
|
|
|
if (this._undoneTransformations)
|
2021-02-17 10:53:13 -03:00
|
|
|
this._undoneTransformations = this._undoneTransformations.filter(transformation => transformation.type != Transformation.SMOOTH);
|
2019-03-05 17:08:43 -03:00
|
|
|
},
|
2019-03-07 10:28:35 -03:00
|
|
|
|
2020-06-19 10:52:12 -03:00
|
|
|
addPoint: function() {
|
2021-02-17 10:53:13 -03:00
|
|
|
if (this.shape == Shape.POLYGON || this.shape == Shape.POLYLINE) {
|
2020-06-19 10:52:12 -03:00
|
|
|
// copy last point
|
|
|
|
|
let [lastPoint, secondToLastPoint] = [this.points[this.points.length - 1], this.points[this.points.length - 2]];
|
|
|
|
|
if (!getNearness(secondToLastPoint, lastPoint, MIN_DRAWING_SIZE))
|
|
|
|
|
this.points.push([lastPoint[0], lastPoint[1]]);
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.LINE) {
|
2020-06-19 10:52:12 -03:00
|
|
|
if (this.points.length == 2) {
|
|
|
|
|
this.points[2] = this.points[1];
|
|
|
|
|
} else if (this.points.length == 3) {
|
|
|
|
|
this.points[3] = this.points[2];
|
|
|
|
|
this.points[2] = this.points[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-18 21:48:23 -03:00
|
|
|
},
|
|
|
|
|
|
2020-09-30 14:16:55 -03:00
|
|
|
// For free drawing only.
|
|
|
|
|
addIntermediatePoint: function(x, y, transform) {
|
|
|
|
|
let points = this.points;
|
|
|
|
|
if (getNearness(points[points.length - 1], [x, y], MIN_INTERMEDIATE_POINT_DISTANCE))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
points.push([x, y]);
|
|
|
|
|
if (transform)
|
|
|
|
|
this._smooth(points.length - 1);
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-11 22:45:51 -03:00
|
|
|
startDrawing: function(startX, startY) {
|
|
|
|
|
this.points.push([startX, startY]);
|
|
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
if (this.shape == Shape.POLYGON || this.shape == Shape.POLYLINE)
|
2020-06-11 22:45:51 -03:00
|
|
|
this.points.push([startX, startY]);
|
2019-03-07 10:28:35 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-11 22:45:51 -03:00
|
|
|
updateDrawing: function(x, y, transform) {
|
2019-03-07 10:28:35 -03:00
|
|
|
let points = this.points;
|
2020-06-11 22:45:51 -03:00
|
|
|
if (x == points[points.length - 1][0] && y == points[points.length - 1][1])
|
2019-03-07 10:28:35 -03:00
|
|
|
return;
|
2020-06-11 22:45:51 -03:00
|
|
|
|
2020-06-19 10:52:12 -03:00
|
|
|
transform = transform || this.transformations.length >= 1;
|
2020-06-11 22:45:51 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
if (this.shape == Shape.NONE) {
|
2020-06-19 10:52:12 -03:00
|
|
|
points.push([x, y]);
|
|
|
|
|
if (transform)
|
|
|
|
|
this._smooth(points.length - 1);
|
2020-06-11 22:45:51 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if ((this.shape == Shape.RECTANGLE || this.shape == Shape.POLYGON || this.shape == Shape.POLYLINE) && transform) {
|
2020-06-11 22:45:51 -03:00
|
|
|
if (points.length < 2)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-18 15:21:56 -03:00
|
|
|
let center = this._getOriginalCenter();
|
2021-02-17 10:53:13 -03:00
|
|
|
this.transformations[0] = { type: Transformation.ROTATION,
|
2020-06-18 21:48:23 -03:00
|
|
|
angle: getAngle(center[0], center[1], points[points.length - 1][0], points[points.length - 1][1], x, y) };
|
2020-06-11 22:45:51 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.ELLIPSE && transform) {
|
2020-06-11 22:45:51 -03:00
|
|
|
if (points.length < 2)
|
|
|
|
|
return;
|
2019-03-07 10:28:35 -03:00
|
|
|
|
2020-06-11 22:45:51 -03:00
|
|
|
points[2] = [x, y];
|
2020-06-18 15:21:56 -03:00
|
|
|
let center = this._getOriginalCenter();
|
2021-02-17 10:53:13 -03:00
|
|
|
this.transformations[0] = { type: Transformation.ROTATION,
|
2020-06-18 21:48:23 -03:00
|
|
|
angle: getAngle(center[0], center[1], center[0] + 1, center[1], x, y) };
|
2020-06-11 22:45:51 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (this.shape == Shape.POLYGON || this.shape == Shape.POLYLINE) {
|
2020-06-11 22:45:51 -03:00
|
|
|
points[points.length - 1] = [x, y];
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
points[1] = [x, y];
|
|
|
|
|
|
|
|
|
|
}
|
2019-03-07 10:28:35 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-11 22:45:51 -03:00
|
|
|
stopDrawing: function() {
|
|
|
|
|
// skip when the size is too small to be visible (3px) (except for free drawing)
|
2021-02-17 10:53:13 -03:00
|
|
|
if (this.shape != Shape.NONE && this.points.length >= 2) {
|
2020-06-11 22:45:51 -03:00
|
|
|
let lastPoint = this.points[this.points.length - 1];
|
|
|
|
|
let secondToLastPoint = this.points[this.points.length - 2];
|
2020-06-18 21:48:23 -03:00
|
|
|
if (getNearness(secondToLastPoint, lastPoint, MIN_DRAWING_SIZE))
|
2020-06-11 22:45:51 -03:00
|
|
|
this.points.pop();
|
|
|
|
|
}
|
2020-06-18 21:48:23 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
if (this.transformations[0] && this.transformations[0].type == Transformation.ROTATION &&
|
2020-06-18 21:48:23 -03:00
|
|
|
Math.abs(this.transformations[0].angle) < MIN_ROTATION_ANGLE)
|
|
|
|
|
this.transformations.shift();
|
2020-06-11 22:45:51 -03:00
|
|
|
},
|
|
|
|
|
|
2020-10-04 17:17:13 -03:00
|
|
|
startTransformation: function(startX, startY, type, undoable) {
|
2021-02-17 10:53:13 -03:00
|
|
|
if (type == Transformation.TRANSLATION)
|
2020-10-04 17:17:13 -03:00
|
|
|
this.transformations.push({ startX, startY, type, undoable, slideX: 0, slideY: 0 });
|
2021-02-17 10:53:13 -03:00
|
|
|
else if (type == Transformation.ROTATION)
|
2020-10-04 17:17:13 -03:00
|
|
|
this.transformations.push({ startX, startY, type, undoable, angle: 0 });
|
2021-02-17 10:53:13 -03:00
|
|
|
else if (type == Transformation.SCALE_PRESERVE || type == Transformation.STRETCH)
|
2020-10-04 17:17:13 -03:00
|
|
|
this.transformations.push({ startX, startY, type, undoable, scaleX: 1, scaleY: 1, angle: 0 });
|
2021-02-17 10:53:13 -03:00
|
|
|
else if (type == Transformation.REFLECTION)
|
2020-10-04 17:17:13 -03:00
|
|
|
this.transformations.push({ startX, startY, endX: startX, endY: startY, type, undoable,
|
2020-06-17 13:30:57 -03:00
|
|
|
scaleX: 1, scaleY: 1, slideX: 0, slideY: 0, angle: 0 });
|
2021-02-17 10:53:13 -03:00
|
|
|
else if (type == Transformation.INVERSION)
|
2020-10-04 17:17:13 -03:00
|
|
|
this.transformations.push({ startX, startY, endX: startX, endY: startY, type, undoable,
|
2020-06-17 13:30:57 -03:00
|
|
|
scaleX: -1, scaleY: -1, slideX: startX, slideY: startY,
|
|
|
|
|
angle: Math.PI + Math.atan(startY / (startX || 1)) });
|
|
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
if (type == Transformation.REFLECTION || type == Transformation.INVERSION)
|
2020-06-17 13:30:57 -03:00
|
|
|
this.showSymmetryElement = true;
|
2021-02-18 05:07:12 -03:00
|
|
|
else if (type == Transformation.ROTATION)
|
|
|
|
|
this.showRotationCenter = true;
|
|
|
|
|
else if (type == Transformation.STRETCH)
|
|
|
|
|
this.showStretchAxes = true;
|
2020-06-13 08:53:52 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updateTransformation: function(x, y) {
|
2020-06-15 17:13:03 -03:00
|
|
|
let transformation = this.lastTransformation;
|
2020-06-13 08:53:52 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
if (transformation.type == Transformation.TRANSLATION) {
|
2020-06-13 08:53:52 -03:00
|
|
|
transformation.slideX = x - transformation.startX;
|
|
|
|
|
transformation.slideY = y - transformation.startY;
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.ROTATION) {
|
2020-06-18 15:21:56 -03:00
|
|
|
let center = this._getTransformedCenter(transformation);
|
2020-06-15 17:13:03 -03:00
|
|
|
transformation.angle = getAngle(center[0], center[1], transformation.startX, transformation.startY, x, y);
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.SCALE_PRESERVE) {
|
2020-06-18 15:21:56 -03:00
|
|
|
let center = this._getTransformedCenter(transformation);
|
2020-06-19 11:55:18 -03:00
|
|
|
let scale = Math.hypot(x - center[0], y - center[1]) / Math.hypot(transformation.startX - center[0], transformation.startY - center[1]) || 1;
|
|
|
|
|
[transformation.scaleX, transformation.scaleY] = [scale, scale];
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.STRETCH) {
|
2020-06-18 15:21:56 -03:00
|
|
|
let center = this._getTransformedCenter(transformation);
|
2020-06-15 17:13:03 -03:00
|
|
|
let startAngle = getAngle(center[0], center[1], center[0] + 1, center[1], transformation.startX, transformation.startY);
|
2020-06-18 21:48:23 -03:00
|
|
|
let vertical = Math.abs(Math.sin(startAngle)) >= Math.sin(Math.PI / 2 - STRETCH_TOLERANCE);
|
|
|
|
|
let horizontal = Math.abs(Math.cos(startAngle)) >= Math.cos(STRETCH_TOLERANCE);
|
2020-06-19 11:55:18 -03:00
|
|
|
let scale = Math.hypot(x - center[0], y - center[1]) / Math.hypot(transformation.startX - center[0], transformation.startY - center[1]) || 1;
|
|
|
|
|
transformation.scaleX = vertical ? 1 : scale;
|
|
|
|
|
transformation.scaleY = !vertical ? 1 : scale;
|
|
|
|
|
transformation.angle = vertical || horizontal ? 0 : getAngle(center[0], center[1], center[0] + 1, center[1], x, y);
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.REFLECTION) {
|
2020-06-17 13:30:57 -03:00
|
|
|
[transformation.endX, transformation.endY] = [x, y];
|
|
|
|
|
if (getNearness([transformation.startX, transformation.startY], [x, y], MIN_REFLECTION_LINE_LENGTH)) {
|
|
|
|
|
// do nothing to avoid jumps (no transformation at starting and locked transformation after)
|
2020-06-18 21:48:23 -03:00
|
|
|
} else if (Math.abs(y - transformation.startY) <= REFLECTION_TOLERANCE && Math.abs(x - transformation.startX) > REFLECTION_TOLERANCE) {
|
2020-06-17 13:30:57 -03:00
|
|
|
[transformation.scaleX, transformation.scaleY] = [1, -1];
|
|
|
|
|
[transformation.slideX, transformation.slideY] = [0, transformation.startY];
|
|
|
|
|
transformation.angle = Math.PI;
|
2020-06-18 21:48:23 -03:00
|
|
|
} else if (Math.abs(x - transformation.startX) <= REFLECTION_TOLERANCE && Math.abs(y - transformation.startY) > REFLECTION_TOLERANCE) {
|
2020-06-17 13:30:57 -03:00
|
|
|
[transformation.scaleX, transformation.scaleY] = [-1, 1];
|
|
|
|
|
[transformation.slideX, transformation.slideY] = [transformation.startX, 0];
|
|
|
|
|
transformation.angle = Math.PI;
|
|
|
|
|
} else if (x != transformation.startX) {
|
|
|
|
|
let tan = (y - transformation.startY) / (x - transformation.startX);
|
|
|
|
|
[transformation.scaleX, transformation.scaleY] = [1, -1];
|
|
|
|
|
[transformation.slideX, transformation.slideY] = [0, transformation.startY - transformation.startX * tan];
|
|
|
|
|
transformation.angle = Math.PI + Math.atan(tan);
|
|
|
|
|
} else if (y != transformation.startY) {
|
|
|
|
|
let tan = (x - transformation.startX) / (y - transformation.startY);
|
|
|
|
|
[transformation.scaleX, transformation.scaleY] = [-1, 1];
|
|
|
|
|
[transformation.slideX, transformation.slideY] = [transformation.startX - transformation.startY * tan, 0];
|
|
|
|
|
transformation.angle = Math.PI - Math.atan(tan);
|
|
|
|
|
}
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.INVERSION) {
|
2020-06-17 13:30:57 -03:00
|
|
|
[transformation.endX, transformation.endY] = [x, y];
|
|
|
|
|
[transformation.scaleX, transformation.scaleY] = [-1, -1];
|
|
|
|
|
[transformation.slideX, transformation.slideY] = [x, y];
|
|
|
|
|
transformation.angle = Math.PI + Math.atan(y / (x || 1));
|
2020-06-13 08:53:52 -03:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-17 13:30:57 -03:00
|
|
|
stopTransformation: function() {
|
2020-10-09 03:48:42 -03:00
|
|
|
this.showSymmetryElement = false;
|
2021-02-18 05:07:12 -03:00
|
|
|
this.showRotationCenter = false;
|
|
|
|
|
this.showStretchAxes = false;
|
2020-10-09 03:48:42 -03:00
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
// Clean transformations
|
2020-06-15 17:13:03 -03:00
|
|
|
let transformation = this.lastTransformation;
|
2020-10-09 03:48:42 -03:00
|
|
|
if (!transformation)
|
|
|
|
|
return;
|
2020-06-17 13:30:57 -03:00
|
|
|
|
2021-02-17 10:53:13 -03:00
|
|
|
if (transformation.type == Transformation.REFLECTION &&
|
2020-06-17 13:30:57 -03:00
|
|
|
getNearness([transformation.startX, transformation.startY], [transformation.endX, transformation.endY], MIN_REFLECTION_LINE_LENGTH) ||
|
2021-02-17 10:53:13 -03:00
|
|
|
transformation.type == Transformation.TRANSLATION && Math.hypot(transformation.slideX, transformation.slideY) < MIN_TRANSLATION_DISTANCE ||
|
|
|
|
|
transformation.type == Transformation.ROTATION && Math.abs(transformation.angle) < MIN_ROTATION_ANGLE) {
|
2020-06-17 13:30:57 -03:00
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
this.transformations.pop();
|
|
|
|
|
} else {
|
|
|
|
|
delete transformation.startX;
|
|
|
|
|
delete transformation.startY;
|
2020-06-17 13:30:57 -03:00
|
|
|
delete transformation.endX;
|
|
|
|
|
delete transformation.endY;
|
2020-06-13 08:53:52 -03:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-10-04 12:20:03 -03:00
|
|
|
undoTransformation: function() {
|
|
|
|
|
if (this.transformations && this.transformations.length) {
|
|
|
|
|
// Do not undo initial transformations (transformations made during the drawing step).
|
2020-10-04 17:17:13 -03:00
|
|
|
if (!this.lastTransformation.undoable)
|
2020-10-04 12:20:03 -03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!this._undoneTransformations)
|
|
|
|
|
this._undoneTransformations = [];
|
2020-10-04 15:55:06 -03:00
|
|
|
|
|
|
|
|
let transformation = this.transformations.pop();
|
2021-02-17 10:53:13 -03:00
|
|
|
if (transformation.type == Transformation.SMOOTH)
|
2020-10-04 15:55:06 -03:00
|
|
|
transformation.undo();
|
|
|
|
|
|
|
|
|
|
this._undoneTransformations.push(transformation);
|
2020-10-04 12:20:03 -03:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
redoTransformation: function() {
|
|
|
|
|
if (this._undoneTransformations && this._undoneTransformations.length) {
|
|
|
|
|
if (!this.transformations)
|
|
|
|
|
this.transformations = [];
|
2020-10-04 15:55:06 -03:00
|
|
|
|
|
|
|
|
let transformation = this._undoneTransformations.pop();
|
2021-02-17 10:53:13 -03:00
|
|
|
if (transformation.type == Transformation.SMOOTH)
|
2020-10-04 15:55:06 -03:00
|
|
|
transformation.redo();
|
|
|
|
|
|
|
|
|
|
this.transformations.push(transformation);
|
2020-10-04 12:20:03 -03:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
resetUndoneTransformations: function() {
|
|
|
|
|
delete this._undoneTransformations;
|
|
|
|
|
},
|
|
|
|
|
|
2020-10-04 17:48:40 -03:00
|
|
|
get canUndo() {
|
|
|
|
|
return this._undoneTransformations && this._undoneTransformations.length ? true : false;
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
// The figure rotation center before transformations (original).
|
2020-06-18 15:21:56 -03:00
|
|
|
_getOriginalCenter: function() {
|
|
|
|
|
if (!this._originalCenter) {
|
|
|
|
|
let points = this.points;
|
2021-02-17 10:53:13 -03:00
|
|
|
this._originalCenter = this.shape == Shape.ELLIPSE ? [points[0][0], points[0][1]] :
|
|
|
|
|
this.shape == Shape.LINE && points.length == 4 ? getCurveCenter(points[0], points[1], points[2], points[3]) :
|
|
|
|
|
this.shape == Shape.LINE && points.length == 3 ? getCurveCenter(points[0], points[0], points[1], points[2]) :
|
2020-06-18 15:21:56 -03:00
|
|
|
points.length >= 3 ? getCentroid(points) :
|
|
|
|
|
getNaiveCenter(points);
|
|
|
|
|
}
|
2020-06-13 08:53:52 -03:00
|
|
|
|
2020-06-18 15:21:56 -03:00
|
|
|
return this._originalCenter;
|
2020-06-13 08:53:52 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-18 15:21:56 -03:00
|
|
|
// The figure rotation center, whose position is affected by all transformations done before 'transformation'.
|
|
|
|
|
_getTransformedCenter: function(transformation) {
|
|
|
|
|
if (!transformation.elementTransformedCenter) {
|
2020-06-26 07:19:53 -03:00
|
|
|
let matrix = new Pango.Matrix({ xx: 1, xy: 0, yx: 0, yy: 1, x0: 0, y0: 0 });
|
2020-06-18 15:21:56 -03:00
|
|
|
|
|
|
|
|
// Apply transformations to the matrice in reverse order
|
|
|
|
|
// because Pango multiply matrices by the left when applying a transformation
|
|
|
|
|
this.transformations.slice(0, this.transformations.indexOf(transformation)).reverse().forEach(transformation => {
|
2021-02-17 10:53:13 -03:00
|
|
|
if (transformation.type == Transformation.TRANSLATION) {
|
2020-06-18 15:21:56 -03:00
|
|
|
matrix.translate(transformation.slideX, transformation.slideY);
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.ROTATION) {
|
2020-06-18 15:21:56 -03:00
|
|
|
// nothing, the center position is preserved.
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.SCALE_PRESERVE || transformation.type == Transformation.STRETCH) {
|
2020-06-18 15:21:56 -03:00
|
|
|
// nothing, the center position is preserved.
|
2021-02-17 10:53:13 -03:00
|
|
|
} else if (transformation.type == Transformation.REFLECTION || transformation.type == Transformation.INVERSION) {
|
2020-06-18 15:21:56 -03:00
|
|
|
matrix.translate(transformation.slideX, transformation.slideY);
|
2020-06-19 15:58:23 -03:00
|
|
|
matrix.rotate(-transformation.angle * RADIAN);
|
2020-06-18 15:21:56 -03:00
|
|
|
matrix.scale(transformation.scaleX, transformation.scaleY);
|
2020-06-19 15:58:23 -03:00
|
|
|
matrix.rotate(transformation.angle * RADIAN);
|
2020-06-18 15:21:56 -03:00
|
|
|
matrix.translate(-transformation.slideX, -transformation.slideY);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let originalCenter = this._getOriginalCenter();
|
|
|
|
|
transformation.elementTransformedCenter = matrix.transform_point(originalCenter[0], originalCenter[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return transformation.elementTransformedCenter;
|
2020-06-15 17:13:03 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-19 10:52:12 -03:00
|
|
|
_smooth: function(i) {
|
|
|
|
|
if (i < 2)
|
|
|
|
|
return;
|
|
|
|
|
this.points[i-1] = [(this.points[i-2][0] + this.points[i][0]) / 2, (this.points[i-2][1] + this.points[i][1]) / 2];
|
2020-06-07 13:57:11 -03:00
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
});
|
|
|
|
|
|
2020-07-08 06:47:51 -03:00
|
|
|
const TextElement = new Lang.Class({
|
2020-10-04 05:01:55 -03:00
|
|
|
Name: `${UUID}-TextElement`,
|
2020-07-08 06:47:51 -03:00
|
|
|
Extends: _DrawingElement,
|
|
|
|
|
|
|
|
|
|
toJSON: function() {
|
2020-09-04 19:03:05 -03:00
|
|
|
// The font size is useless because it is always computed from the points during cairo/svg building.
|
|
|
|
|
this.font.unset_fields(Pango.FontMask.SIZE);
|
|
|
|
|
|
2020-07-08 06:47:51 -03:00
|
|
|
return {
|
|
|
|
|
shape: this.shape,
|
2020-09-22 12:31:48 -03:00
|
|
|
color: this.color,
|
2020-07-08 06:47:51 -03:00
|
|
|
eraser: this.eraser,
|
|
|
|
|
transformations: this.transformations,
|
|
|
|
|
text: this.text,
|
2021-02-17 10:27:35 -03:00
|
|
|
textAlignment: this.textAlignment,
|
2020-09-04 19:03:05 -03:00
|
|
|
font: this.font.to_string(),
|
2020-07-08 06:47:51 -03:00
|
|
|
points: this.points.map((point) => [Math.round(point[0]*100)/100, Math.round(point[1]*100)/100])
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-11 10:59:08 -03:00
|
|
|
get x() {
|
|
|
|
|
// this.textWidth is computed during Cairo building.
|
2021-02-17 10:27:35 -03:00
|
|
|
let offset = this.textAlignment == TextAlignment.RIGHT ? this.textWidth :
|
|
|
|
|
this.textAlignment == TextAlignment.CENTER ? this.textWidth / 2 :
|
|
|
|
|
0;
|
|
|
|
|
|
|
|
|
|
return this.points[1][0] - offset;
|
2020-07-11 10:59:08 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get y() {
|
|
|
|
|
return Math.max(this.points[0][1], this.points[1][1]);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get height() {
|
|
|
|
|
return Math.abs(this.points[1][1] - this.points[0][1]);
|
|
|
|
|
},
|
|
|
|
|
|
2021-02-17 07:33:32 -03:00
|
|
|
// this.lineWidths is computed during Cairo building.
|
|
|
|
|
_getLineX: function(index) {
|
2021-02-17 10:27:35 -03:00
|
|
|
let offset = this.textAlignment == TextAlignment.RIGHT && this.lineWidths && this.lineWidths[index] ? this.lineWidths[index] :
|
|
|
|
|
this.textAlignment == TextAlignment.CENTER && this.lineWidths && this.lineWidths[index] ? this.lineWidths[index] / 2 :
|
|
|
|
|
0;
|
|
|
|
|
|
|
|
|
|
return this.points[1][0] - offset;
|
2020-07-11 10:59:08 -03:00
|
|
|
},
|
|
|
|
|
|
2020-07-08 06:47:51 -03:00
|
|
|
_drawCairo: function(cr, params) {
|
2021-02-17 07:33:32 -03:00
|
|
|
if (this.points.length != 2)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
let layout = PangoCairo.create_layout(cr);
|
|
|
|
|
let fontSize = this.height * Pango.SCALE;
|
|
|
|
|
this.font.set_absolute_size(fontSize);
|
|
|
|
|
layout.set_font_description(this.font);
|
|
|
|
|
layout.set_text(this.text, -1);
|
|
|
|
|
this.textWidth = layout.get_pixel_size()[0];
|
2021-02-17 10:53:13 -03:00
|
|
|
this.lineWidths = layout.get_lines_readonly().map(layoutLine => layoutLine.get_pixel_extents()[1].width);
|
2021-02-17 07:33:32 -03:00
|
|
|
|
|
|
|
|
layout.get_lines_readonly().forEach((layoutLine, index) => {
|
|
|
|
|
cr.moveTo(this._getLineX(index), this.y + this.height * index);
|
|
|
|
|
PangoCairo.show_layout_line(cr, layoutLine);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Cannot use 'layout.index_to_line_x(cursorPosition, 0)' because character position != byte index
|
|
|
|
|
if (params.showTextCursor) {
|
|
|
|
|
let layoutCopy = layout.copy();
|
|
|
|
|
if (this.cursorPosition != -1)
|
|
|
|
|
layoutCopy.set_text(this.text.slice(0, this.cursorPosition), -1);
|
|
|
|
|
|
|
|
|
|
let cursorLineIndex = layoutCopy.get_line_count() - 1;
|
|
|
|
|
let cursorX = this._getLineX(cursorLineIndex) + layoutCopy.get_line_readonly(cursorLineIndex).get_pixel_extents()[1].width;
|
|
|
|
|
let cursorY = this.y + this.height * cursorLineIndex;
|
|
|
|
|
cr.rectangle(cursorX, cursorY, this.height / 25, - this.height);
|
|
|
|
|
cr.fill();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 19:17:16 -03:00
|
|
|
if (params.showElementBounds) {
|
2021-02-17 07:33:32 -03:00
|
|
|
cr.rectangle(this.x, this.y - this.height,
|
|
|
|
|
this.textWidth, this.height * layout.get_line_count());
|
|
|
|
|
setDummyStroke(cr);
|
2021-02-19 19:17:16 -03:00
|
|
|
} else if (params.drawElementBounds) {
|
2021-02-17 07:33:32 -03:00
|
|
|
cr.rectangle(this.x, this.y - this.height,
|
|
|
|
|
this.textWidth, this.height * layout.get_line_count());
|
|
|
|
|
// Only draw the rectangle to find the element, not to show it.
|
|
|
|
|
cr.setLineWidth(0);
|
2020-07-08 06:47:51 -03:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getContainsPoint: function(cr, x, y) {
|
|
|
|
|
return cr.inFill(x, y);
|
|
|
|
|
},
|
|
|
|
|
|
2020-09-14 19:29:02 -03:00
|
|
|
_drawSvg: function(transAttribute, bgcolorString) {
|
2021-02-17 07:33:32 -03:00
|
|
|
if (this.points.length != 2)
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
let row = "";
|
|
|
|
|
let height = Math.round(this.height * 100) / 100;
|
2020-09-22 12:31:48 -03:00
|
|
|
let color = this.eraser ? bgcolorString : this.color.toJSON();
|
2020-09-16 12:58:06 -03:00
|
|
|
let attributes = this.eraser ? `class="eraser" ` : '';
|
2021-02-17 07:33:32 -03:00
|
|
|
attributes += `fill="${color}" ` +
|
|
|
|
|
`font-size="${height}" ` +
|
|
|
|
|
`font-family="${this.font.get_family()}"`;
|
|
|
|
|
|
|
|
|
|
// this.font.to_string() is not valid to fill the svg 'font' shorthand property.
|
|
|
|
|
// Each property must be filled separately.
|
|
|
|
|
['Stretch', 'Style', 'Variant'].forEach(attribute => {
|
|
|
|
|
let lower = attribute.toLowerCase();
|
|
|
|
|
if (this.font[`get_${lower}`]() != Pango[attribute].NORMAL) {
|
|
|
|
|
let font = new Pango.FontDescription();
|
|
|
|
|
font[`set_${lower}`](this.font[`get_${lower}`]());
|
|
|
|
|
attributes += ` font-${lower}="${font.to_string()}"`;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (this.font.get_weight() != Pango.Weight.NORMAL)
|
|
|
|
|
attributes += ` font-weight="${this.font.get_weight()}"`;
|
|
|
|
|
|
|
|
|
|
// It is a fallback for thumbnails. The following layout is not the same than the Cairo one and
|
|
|
|
|
// layoutLine.get_pixel_extents does not return the correct value with non-latin fonts.
|
|
|
|
|
// An alternative would be to store this.lineWidths in the json.
|
2021-02-17 10:27:35 -03:00
|
|
|
if (this.textAlignment != TextAlignment.LEFT && !this.lineWidths) {
|
2021-02-17 07:33:32 -03:00
|
|
|
let clutterText = new Clutter.Text({ text: this.text });
|
|
|
|
|
let layout = clutterText.get_layout();
|
|
|
|
|
let fontSize = height * Pango.SCALE;
|
|
|
|
|
this.font.set_absolute_size(fontSize);
|
|
|
|
|
layout.set_font_description(this.font);
|
|
|
|
|
this.lineWidths = layout.get_lines_readonly().map(layoutLine => layoutLine.get_pixel_extents()[1].width);
|
2020-07-08 06:47:51 -03:00
|
|
|
}
|
|
|
|
|
|
2021-02-17 07:33:32 -03:00
|
|
|
this.text.split(/\r\n|\r|\n/).forEach((text, index) => {
|
|
|
|
|
let x = Math.round(this._getLineX(index) * 100) / 100;
|
|
|
|
|
let y = Math.round((this.y + this.height * index) * 100) / 100;
|
|
|
|
|
row += `\n <text ${attributes} x="${x}" y="${y}"${transAttribute}>${text}</text>`;
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-08 06:47:51 -03:00
|
|
|
return row;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updateDrawing: function(x, y, transform) {
|
|
|
|
|
let points = this.points;
|
|
|
|
|
if (x == points[points.length - 1][0] && y == points[points.length - 1][1])
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
transform = transform || this.transformations.length >= 1;
|
|
|
|
|
|
|
|
|
|
if (transform) {
|
|
|
|
|
if (points.length < 2)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
let [slideX, slideY] = [x - points[1][0], y - points[1][1]];
|
|
|
|
|
points[0] = [points[0][0] + slideX, points[0][1] + slideY];
|
|
|
|
|
points[1] = [x, y];
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
points[1] = [x, y];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_getOriginalCenter: function() {
|
|
|
|
|
if (!this._originalCenter) {
|
|
|
|
|
let points = this.points;
|
2021-02-17 07:33:32 -03:00
|
|
|
this._originalCenter = points.length == 2 ? [points[1][0], Math.max(points[0][1], points[1][1])] :
|
2020-07-08 06:47:51 -03:00
|
|
|
points.length >= 3 ? getCentroid(points) :
|
|
|
|
|
getNaiveCenter(points);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this._originalCenter;
|
|
|
|
|
},
|
|
|
|
|
});
|
2020-07-30 06:13:23 -03:00
|
|
|
|
|
|
|
|
const ImageElement = new Lang.Class({
|
2020-10-04 05:01:55 -03:00
|
|
|
Name: `${UUID}-ImageElement`,
|
2020-07-30 06:13:23 -03:00
|
|
|
Extends: _DrawingElement,
|
|
|
|
|
|
|
|
|
|
toJSON: function() {
|
|
|
|
|
return {
|
|
|
|
|
shape: this.shape,
|
2020-09-22 12:31:48 -03:00
|
|
|
color: this.color,
|
2020-09-22 18:21:48 -03:00
|
|
|
colored: this.colored,
|
2020-07-30 06:13:23 -03:00
|
|
|
transformations: this.transformations,
|
2020-09-22 13:22:41 -03:00
|
|
|
image: this.image,
|
2020-07-30 06:13:23 -03:00
|
|
|
preserveAspectRatio: this.preserveAspectRatio,
|
|
|
|
|
points: this.points.map((point) => [Math.round(point[0]*100)/100, Math.round(point[1]*100)/100])
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_drawCairo: function(cr, params) {
|
|
|
|
|
if (this.points.length < 2)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
let points = this.points;
|
|
|
|
|
let [x, y] = [Math.min(points[0][0], points[1][0]), Math.min(points[0][1], points[1][1])];
|
|
|
|
|
let [width, height] = [Math.abs(points[1][0] - points[0][0]), Math.abs(points[1][1] - points[0][1])];
|
|
|
|
|
|
|
|
|
|
if (width < 1 || height < 1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
cr.save();
|
2020-09-22 18:21:48 -03:00
|
|
|
this.image.setCairoSource(cr, x, y, width, height, this.preserveAspectRatio, this.colored ? this.color.toJSON() : null);
|
2020-07-30 06:13:23 -03:00
|
|
|
cr.rectangle(x, y, width, height);
|
|
|
|
|
cr.fill();
|
|
|
|
|
cr.restore();
|
|
|
|
|
|
2021-02-19 19:17:16 -03:00
|
|
|
if (params.showElementBounds) {
|
2020-07-30 06:13:23 -03:00
|
|
|
cr.rectangle(x, y, width, height);
|
|
|
|
|
setDummyStroke(cr);
|
2021-02-19 19:17:16 -03:00
|
|
|
} else if (params.drawElementBounds) {
|
2020-07-30 06:13:23 -03:00
|
|
|
cr.rectangle(x, y, width, height);
|
|
|
|
|
// Only draw the rectangle to find the element, not to show it.
|
|
|
|
|
cr.setLineWidth(0);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getContainsPoint: function(cr, x, y) {
|
|
|
|
|
return cr.inFill(x, y);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_drawSvg: function(transAttribute) {
|
|
|
|
|
let points = this.points;
|
|
|
|
|
let row = "\n ";
|
2020-09-22 18:21:48 -03:00
|
|
|
let attributes = '';
|
2020-07-30 06:13:23 -03:00
|
|
|
|
|
|
|
|
if (points.length == 2) {
|
2020-09-16 12:58:06 -03:00
|
|
|
attributes += `fill="none"`;
|
2020-09-22 18:21:48 -03:00
|
|
|
let base64 = this.image.getBase64ForColor(this.colored ? this.color.toJSON() : null);
|
2020-07-30 06:13:23 -03:00
|
|
|
row += `<image ${attributes} x="${Math.min(points[0][0], points[1][0])}" y="${Math.min(points[0][1], points[1][1])}" ` +
|
|
|
|
|
`width="${Math.abs(points[1][0] - points[0][0])}" height="${Math.abs(points[1][1] - points[0][1])}"${transAttribute} ` +
|
|
|
|
|
`preserveAspectRatio="${this.preserveAspectRatio ? 'xMinYMin' : 'none'}" ` +
|
2020-09-22 18:21:48 -03:00
|
|
|
`id="${this.image.displayName}" xlink:href="data:${this.image.contentType};base64,${base64}"/>`;
|
2020-07-30 06:13:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return row;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updateDrawing: function(x, y, transform) {
|
|
|
|
|
let points = this.points;
|
|
|
|
|
if (x == points[0][0] || y == points[0][1])
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
points[1] = [x, y];
|
|
|
|
|
this.preserveAspectRatio = !transform;
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-07-08 06:47:51 -03:00
|
|
|
|
2020-06-19 15:58:23 -03:00
|
|
|
const setDummyStroke = function(cr) {
|
2020-06-15 19:06:52 -03:00
|
|
|
cr.setLineWidth(2);
|
|
|
|
|
cr.setLineCap(0);
|
|
|
|
|
cr.setLineJoin(0);
|
|
|
|
|
cr.setDash([1, 2], 0);
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-07 13:57:11 -03:00
|
|
|
const getNearness = function(pointA, pointB, distance) {
|
|
|
|
|
return Math.hypot(pointB[0] - pointA[0], pointB[1] - pointA[1]) < distance;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// mean of the vertices, ok for regular polygons
|
|
|
|
|
const getNaiveCenter = function(points) {
|
|
|
|
|
return points.reduce((accumulator, point) => accumulator = [accumulator[0] + point[0], accumulator[1] + point[1]])
|
|
|
|
|
.map(coord => coord / points.length);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://en.wikipedia.org/wiki/Centroid#Of_a_polygon
|
|
|
|
|
const getCentroid = function(points) {
|
|
|
|
|
let n = points.length;
|
|
|
|
|
points.push(points[0]);
|
|
|
|
|
|
|
|
|
|
let [sA, sX, sY] = [0, 0, 0];
|
|
|
|
|
for (let i = 0; i <= n-1; i++) {
|
|
|
|
|
let a = points[i][0]*points[i+1][1] - points[i+1][0]*points[i][1];
|
|
|
|
|
sA += a;
|
|
|
|
|
sX += (points[i][0] + points[i+1][0]) * a;
|
|
|
|
|
sY += (points[i][1] + points[i+1][1]) * a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
points.pop();
|
2020-06-11 22:45:51 -03:00
|
|
|
if (sA == 0)
|
|
|
|
|
return getNaiveCenter(points);
|
2020-06-07 13:57:11 -03:00
|
|
|
return [sX / (3 * sA), sY / (3 * sA)];
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-11 22:45:51 -03:00
|
|
|
/*
|
|
|
|
|
Cubic Bézier:
|
|
|
|
|
[0, 1] -> ℝ², P(t) = (1-t)³P₀ + 3t(1-t)²P₁ + 3t²(1-t)P₂ + t³P₃
|
|
|
|
|
|
|
|
|
|
general case:
|
|
|
|
|
|
|
|
|
|
const cubicBezierCoord = function(x0, x1, x2, x3, t) {
|
|
|
|
|
return (1-t)**3*x0 + 3*t*(1-t)**2*x1 + 3*t**2*(1-t)*x2 + t**3*x3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cubicBezierPoint = function(p0, p1, p2, p3, t) {
|
|
|
|
|
return [cubicBezier(p0[0], p1[0], p2[0], p3[0], t), cubicBezier(p0[1], p1[1], p2[1], p3[1], t)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Approximatively:
|
|
|
|
|
control point: p0 ---- p1 ---- p2 ---- p3 (p2 is not on the curve)
|
|
|
|
|
t: 0 ---- 1/3 ---- 2/3 ---- 1
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-19 10:52:12 -03:00
|
|
|
// If the curve has a symmetry axis, it is truly a center (the intersection of the curve and the axis).
|
2020-06-11 22:45:51 -03:00
|
|
|
// In other cases, it is not a notable point, just a visual approximation.
|
2020-06-19 10:52:12 -03:00
|
|
|
const getCurveCenter = function(p0, p1, p2, p3) {
|
|
|
|
|
if (p0[0] == p1[0] && p0[1] == p1[1])
|
|
|
|
|
// p0 = p1, t = 2/3
|
|
|
|
|
return [(p1[0] + 6*p1[0] + 12*p2[0] + 8*p3[0]) / 27, (p1[1] + 6*p1[1] + 12*p2[1] + 8*p3[1]) / 27];
|
|
|
|
|
else
|
|
|
|
|
// t = 1/2
|
|
|
|
|
return [(p0[0] + 3*p1[0] + 3*p2[0] + p3[0]) / 8, (p0[1] + 3*p1[1] + 3*p2[1] + p3[1]) / 8];
|
2020-06-11 22:45:51 -03:00
|
|
|
};
|
|
|
|
|
|
2020-03-02 12:32:50 -03:00
|
|
|
const getAngle = function(xO, yO, xA, yA, xB, yB) {
|
2019-03-07 10:28:35 -03:00
|
|
|
// calculate angle of rotation in absolute value
|
|
|
|
|
// cos(AOB) = (OA.OB)/(||OA||*||OB||) where OA.OB = (xA-xO)*(xB-xO) + (yA-yO)*(yB-yO)
|
2020-06-11 22:45:51 -03:00
|
|
|
let cos = ((xA - xO)*(xB - xO) + (yA - yO)*(yB - yO)) / (Math.hypot(xA - xO, yA - yO) * Math.hypot(xB - xO, yB - yO));
|
|
|
|
|
|
|
|
|
|
// acos is defined on [-1, 1] but
|
|
|
|
|
// with A == B and imperfect computer calculations, cos may be equal to 1.00000001.
|
|
|
|
|
cos = Math.min(Math.max(-1, cos), 1);
|
|
|
|
|
let angle = Math.acos( cos );
|
2019-03-07 10:28:35 -03:00
|
|
|
|
|
|
|
|
// determine the sign of the angle
|
2020-06-11 22:45:51 -03:00
|
|
|
if (xA == xO) {
|
|
|
|
|
if (xB > xO)
|
|
|
|
|
angle = -angle;
|
|
|
|
|
} else {
|
|
|
|
|
// equation of OA: y = ax + b
|
|
|
|
|
let a = (yA - yO) / (xA - xO);
|
|
|
|
|
let b = yA - a*xA;
|
|
|
|
|
if (yB < a*xB + b)
|
|
|
|
|
angle = - angle;
|
|
|
|
|
if (xA < xO)
|
|
|
|
|
angle = - angle;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-07 10:28:35 -03:00
|
|
|
return angle;
|
2020-03-02 12:32:50 -03:00
|
|
|
};
|
2019-03-07 10:28:35 -03:00
|
|
|
|