Skip to content

Commit 2c41840

Browse files
committed
Merge branch 'master' into fix/measurement-svg-crop
2 parents 2d6bc0b + 756d50e commit 2c41840

File tree

5 files changed

+405
-549
lines changed

5 files changed

+405
-549
lines changed

package-lock.json

Lines changed: 3 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/drawControllers/svg.drawController.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {Point} from '@flatten-js/core';
2-
import {DEFAULT_TEXT_OPTIONS, type DrawController} from './DrawController';
3-
import {SVG_MARGIN} from '../App.consts.ts';
1+
import {Point, Vector} from '@flatten-js/core';
42
import {toast} from 'react-toastify';
3+
import {SVG_MARGIN, TO_DEGREES} from '../App.consts.ts';
54
import type {TextOptions} from '../entities/TextEntity.ts';
6-
import {triggerReactUpdate} from '../state.ts';
7-
import {StateVariable} from '../helpers/undo-stack.ts';
85
import {isLengthEqual} from '../helpers/is-length-equal.ts';
6+
import {StateVariable} from '../helpers/undo-stack.ts';
7+
import {triggerReactUpdate} from '../state.ts';
8+
import {DEFAULT_TEXT_OPTIONS, type DrawController} from './DrawController';
99

1010
export class SvgDrawController implements DrawController {
1111
private lineColor = '#000';
@@ -211,17 +211,32 @@ export class SvgDrawController implements DrawController {
211211
...options,
212212
};
213213

214-
let finalTextColor = textOptions.textColor;
215-
const lowerCaseTextColor = textOptions.textColor.toLowerCase();
216-
if (lowerCaseTextColor === '#fff' || lowerCaseTextColor === '#ffffff' || lowerCaseTextColor === 'white') {
217-
finalTextColor = '#000'; // Change to black if current color is white
218-
}
219-
// No need to handle black to white, as SVG background is white.
220-
// Other colors will remain as they are.
214+
let finalTextColor = textOptions.textColor;
215+
const lowerCaseTextColor = textOptions.textColor.toLowerCase();
216+
if (
217+
lowerCaseTextColor === '#fff' ||
218+
lowerCaseTextColor === '#ffffff' ||
219+
lowerCaseTextColor === 'white'
220+
) {
221+
finalTextColor = '#000'; // Change to black if current color is white
222+
}
223+
// No need to handle black to white, as SVG background is white.
224+
// Other colors will remain as they are.
225+
226+
let transformAttribute = '';
227+
if (textOptions.textDirection) {
228+
const angle = textOptions.textDirection.angleTo(new Vector(1, 0)) * TO_DEGREES;
229+
transformAttribute = `transform="rotate(${angle}, ${canvasBasePoint.x}, ${canvasBasePoint.y})"`;
230+
}
231+
232+
let textAnchorAttribute = '';
233+
if (textOptions.textAlign === 'center') {
234+
textAnchorAttribute = 'text-anchor="middle"';
235+
}
221236

222237
this.svgStrings.push(
223-
// Use finalTextColor here
224-
`<text x="${canvasBasePoint.x}" y="${canvasBasePoint.y}" fill="${finalTextColor}" font-size="${textOptions.fontSize}" font-family="${textOptions.fontFamily}">${label}</text>`
238+
// Use finalTextColor here
239+
`<text x="${canvasBasePoint.x}" y="${canvasBasePoint.y}" fill="${finalTextColor}" font-size="${textOptions.fontSize}" font-family="${textOptions.fontFamily}" ${transformAttribute} ${textAnchorAttribute}>${label}</text>`
225240
);
226241
}
227242

0 commit comments

Comments
 (0)