Skip to content

Commit

Permalink
fix #1008 by adjusting font-size
Browse files Browse the repository at this point in the history
(allowing to keep the text keep his proportions to the font)
  • Loading branch information
jfhenon committed Dec 9, 2024
1 parent 28361b8 commit 8aa65d5
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 211 deletions.
142 changes: 72 additions & 70 deletions coverage/coverage-summary.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cypress/__svgSnapshots__/scenario-check tool_bold.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion cypress/__svgSnapshots__/scenario-check tool_clone.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion cypress/__svgSnapshots__/scenario-check tool_italic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 36 additions & 2 deletions packages/svgcanvas/core/coords.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const remapElement = (selected, changes, m) => {
case 'image': {
// Allow images to be inverted (give them matrix when flipped)
if (elName === 'image' && (m.a < 0 || m.d < 0)) {
// Convert to matrix
// Convert to matrix if flipped
const chlist = getTransformList(selected)
const mt = svgCanvas.getSvgRoot().createSVGTransform()
mt.setMatrix(matrixMultiply(transformListToTransform(chlist).matrix, m))
Expand Down Expand Up @@ -177,6 +177,19 @@ export const remapElement = (selected, changes, m) => {
const pt = remap(changes.x, changes.y)
changes.x = pt.x
changes.y = pt.y

// Scale font-size
let fontSize = selected.getAttribute('font-size')
if (!fontSize) {
// If not directly set, try computed style
fontSize = window.getComputedStyle(selected).fontSize
}
const fontSizeNum = parseFloat(fontSize)
if (!isNaN(fontSizeNum)) {
// Assume uniform scaling and use m.a
changes['font-size'] = fontSizeNum * Math.abs(m.a)
}

finishUp()

// Handle child 'tspan' elements
Expand All @@ -197,7 +210,17 @@ export const remapElement = (selected, changes, m) => {
const childPtY = remap(changes.x, childY).y
childChanges.y = childPtY
}
if (hasX || hasY) {

let tspanFS = child.getAttribute('font-size')
if (!tspanFS) {
tspanFS = window.getComputedStyle(child).fontSize
}
const tspanFSNum = parseFloat(tspanFS)
if (!isNaN(tspanFSNum)) {
childChanges['font-size'] = tspanFSNum * Math.abs(m.a)
}

if (hasX || hasY || childChanges['font-size']) {
assignAttributes(child, childChanges, 1000, true)
}
}
Expand All @@ -208,6 +231,17 @@ export const remapElement = (selected, changes, m) => {
const pt = remap(changes.x, changes.y)
changes.x = pt.x
changes.y = pt.y

// Handle tspan font-size scaling
let tspanFS = selected.getAttribute('font-size')
if (!tspanFS) {
tspanFS = window.getComputedStyle(selected).fontSize
}
const tspanFSNum = parseFloat(tspanFS)
if (!isNaN(tspanFSNum)) {
changes['font-size'] = tspanFSNum * Math.abs(m.a)
}

finishUp()
break
}
Expand Down
1 change: 1 addition & 0 deletions packages/svgcanvas/core/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ const mouseOutEvent = () => {
* @returns {void}
*/
const mouseUpEvent = (evt) => {
evt.preventDefault()
moveSelectionThresholdReached = false
if (evt.button === 2) { return }
if (!svgCanvas.getStarted()) { return }
Expand Down
Loading

0 comments on commit 8aa65d5

Please sign in to comment.