Skip to content

Fix SVG alignment and click box #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2021
Merged
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
62 changes: 29 additions & 33 deletions src/core/Decorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,36 @@ type IndividualHatAdjustmentSetting = Record<HatShape, IndividualHatAdjustment>;

const defaultShapeMeasurements: Record<HatShape, ShapeMeasurements> = {
default: {
hatWidthToCharacterWidthRatio: 0.507,
verticalOffsetEm: -0.05,
hatWidthToCharacterWidthRatio: 0.6362522386652841,
verticalOffsetEm: -0.06274676909914044,
},
fourPointStar: {
hatWidthToCharacterWidthRatio: 0.6825,
verticalOffsetEm: -0.105,
hatWidthToCharacterWidthRatio: 0.8564933982032671,
verticalOffsetEm: -0.13176821510819492,
},
threePointStar: {
hatWidthToCharacterWidthRatio: 0.9555,
verticalOffsetEm: -0.055,
hatWidthToCharacterWidthRatio: 1.199090757484574,
verticalOffsetEm: -0.06902144600905448,
},
chevron: {
hatWidthToCharacterWidthRatio: 0.6825,
verticalOffsetEm: -0.02,
hatWidthToCharacterWidthRatio: 0.8564933982032671,
verticalOffsetEm: -0.025098707639656177,
},
hole: {
hatWidthToCharacterWidthRatio: 0.819,
verticalOffsetEm: -0.07,
hatWidthToCharacterWidthRatio: 1.0277920778439205,
verticalOffsetEm: -0.08784547673879664,
},
frame: {
hatWidthToCharacterWidthRatio: 0.61425,
verticalOffsetEm: -0.02,
hatWidthToCharacterWidthRatio: 0.7708440583829403,
verticalOffsetEm: -0.025098707639656177,
},
curve: {
hatWidthToCharacterWidthRatio: 0.6825,
verticalOffsetEm: -0.07,
hatWidthToCharacterWidthRatio: 0.8564933982032671,
verticalOffsetEm: -0.08784547673879664,
},
eye: {
hatWidthToCharacterWidthRatio: 0.921375,
verticalOffsetEm: -0.12,
hatWidthToCharacterWidthRatio: 1.1562660875744104,
verticalOffsetEm: -0.15059224583793704,
},
};

Expand Down Expand Up @@ -138,9 +138,6 @@ export default class Decorations {
const { color, shape } = this.hatStyleMap[styleName];
const { svg, svgWidthPx, svgHeightPx } = hatSvgMap[shape];

const spanWidthPx =
svgWidthPx + (fontMeasurements.characterWidth - svgWidthPx) / 2;

const colorSetting = vscode.workspace
.getConfiguration("cursorless.colors")
.get<DecorationColorSetting>(color)!;
Expand All @@ -150,24 +147,24 @@ export default class Decorations {
decoration: vscode.window.createTextEditorDecorationType({
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
light: {
after: {
before: {
contentIconPath: this.constructColoredSvgDataUri(
svg,
colorSetting.light
),
},
},
dark: {
after: {
before: {
contentIconPath: this.constructColoredSvgDataUri(
svg,
colorSetting.dark
),
},
},
after: {
margin: `-${svgHeightPx}px 0 0 -${spanWidthPx}px`,
width: `${spanWidthPx}px`,
before: {
margin: `-${svgHeightPx}px -${svgWidthPx}px 0 0`,
width: `${svgWidthPx}px`,
Comment on lines +166 to +167
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to play games with span width anymore because we now align to the left side of the char

height: `${svgHeightPx}px`,
},
}),
Expand Down Expand Up @@ -261,25 +258,24 @@ export default class Decorations {
) {
const iconPath = join(this.extensionPath, "images", "hats", `${shape}.svg`);
const rawSvg = readFileSync(iconPath, "utf8");
const { characterWidth, characterHeight } = fontMeasurements;

const { originalViewBoxHeight, originalViewBoxWidth } =
this.getViewBoxDimensions(rawSvg);

const hatWidthPx =
hatWidthToCharacterWidthRatio * fontMeasurements.characterWidth;
const hatWidthPx = hatWidthToCharacterWidthRatio * characterWidth;
const hatHeightPx =
(originalViewBoxHeight / originalViewBoxWidth) * hatWidthPx;

const svgWidthPx = Math.ceil(fontMeasurements.characterWidth);
const svgHeightPx =
fontMeasurements.characterHeight + hatHeightPx + hatVerticalOffset;
const svgWidthPx = Math.ceil(characterWidth);
const svgHeightPx = characterHeight + hatHeightPx + hatVerticalOffset;

const newViewBoxWidth =
((originalViewBoxWidth / hatWidthToCharacterWidthRatio) *
fontMeasurements.characterWidth) /
svgWidthPx;
Comment on lines -278 to -280
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the dumb mistake. We need to adjust from char width to svg width, not the other way around 😅

((originalViewBoxWidth / hatWidthToCharacterWidthRatio) * svgWidthPx) /
characterWidth;
const newViewBoxHeight = (newViewBoxWidth * svgHeightPx) / svgWidthPx;
const newViewBoxX = -(newViewBoxWidth - originalViewBoxWidth) / 2;
const newViewBoxX =
(-(characterWidth - hatWidthPx) * (newViewBoxWidth / svgWidthPx)) / 2;
Comment on lines +277 to +278
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're left-aligned, we basically need to shift right by (characterWidth - hatWidthPx) / 2, translated to svg view box space (newViewBoxWidth / svgWidthPx)

const newViewBoxY = 0;

const newViewBoxString = `${newViewBoxX} ${newViewBoxY} ${newViewBoxWidth} ${newViewBoxHeight}`;
Expand Down