Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { CanvasToolModule } from 'features/controlLayers/konva/CanvasTool/C
import { canvasToBlob, getPrefixedId } from 'features/controlLayers/konva/util';
import { type CanvasTextSettingsState, selectCanvasTextSlice } from 'features/controlLayers/store/canvasTextSlice';
import type { Coordinate, RgbaColor } from 'features/controlLayers/store/types';
import { imageDTOToImageObject } from 'features/controlLayers/store/util';
import { buildCommittedTextImageState, getCommittedTextImageDimensions } from 'features/controlLayers/text/textCommit';
import { getFontStackById, TEXT_RASTER_PADDING } from 'features/controlLayers/text/textConstants';
import {
buildFontDescriptor,
Expand Down Expand Up @@ -418,7 +418,8 @@ export class CanvasTextToolModule extends CanvasModuleBase {
is_intermediate: true,
silent: true,
});
const imageState = imageDTOToImageObject(imageDTO);
const { width: committedWidth, height: committedHeight } = getCommittedTextImageDimensions(totalWidth, totalHeight);
const imageState = buildCommittedTextImageState(imageDTO, totalWidth, totalHeight);

const extraLeftPadding = Math.ceil(textSettings.fontSize * 0.12);
const fallbackPosition = calculateLayerPosition(
Expand All @@ -434,8 +435,8 @@ export class CanvasTextToolModule extends CanvasModuleBase {
rotation === 0
? basePosition
: {
x: basePosition.x + baseSize.width / 2 - totalWidth / 2,
y: basePosition.y + baseSize.height / 2 - totalHeight / 2,
x: basePosition.x + baseSize.width / 2 - committedWidth / 2,
y: basePosition.y + baseSize.height / 2 - committedHeight / 2,
};

const selectedAdapter = this.manager.stateApi.getSelectedEntityAdapter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, it } from 'vitest';

import { buildCommittedTextImageState, getCommittedTextImageDimensions } from './textCommit';

describe('text commit helpers', () => {
it('preserves logical dimensions when committing a hidpi render', () => {
const imageDTO = {
image_name: 'canvas-text.png',
width: 300,
height: 150,
};

const imageState = buildCommittedTextImageState(imageDTO as never, 200, 100);

expect(imageState.image).toEqual({
image_name: 'canvas-text.png',
width: 200,
height: 100,
});
});

it('rounds up fractional logical dimensions and clamps to at least one pixel', () => {
expect(getCommittedTextImageDimensions(99.1, 0.2)).toEqual({
width: 100,
height: 1,
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { imageDTOToImageObject } from 'features/controlLayers/store/util';
import type { ImageDTO } from 'services/api/types';

export const getCommittedTextImageDimensions = (width: number, height: number) => ({
width: Math.max(1, Math.ceil(width)),
height: Math.max(1, Math.ceil(height)),
});

export const buildCommittedTextImageState = (imageDTO: ImageDTO, width: number, height: number) => {
const committedDimensions = getCommittedTextImageDimensions(width, height);
return imageDTOToImageObject(imageDTO, {
image: {
image_name: imageDTO.image_name,
width: committedDimensions.width,
height: committedDimensions.height,
},
});
};
Loading