Skip to content
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

[background image] Add support for background image in the selection info panel #1804

Merged
merged 10 commits into from
Nov 17, 2024
28 changes: 23 additions & 5 deletions src/fontra/backends/designspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1820,14 +1820,29 @@ def unpackBackgroundImage(imageDict: dict | None) -> BackgroundImage | None:
return None

t = Transform(*(imageDict.get(k, dv) for k, dv in imageTransformFields))
colorChannels = [
float(ch.strip()) for ch in imageDict.get("color", "-1").split(",")
]
colorChannels = (
[float(ch.strip()) for ch in imageDict["color"].split(",")]
if "color" in imageDict
else None
)

opacity = 1.0

if colorChannels:
if len(colorChannels) == 4:
opacity = colorChannels[3]
if colorChannels[:3] != [0, 0, 0]:
colorChannels[3] = 1.0
else:
colorChannels = None
else:
colorChannels = None

return BackgroundImage(
identifier=imageDict["fileName"],
transformation=DecomposedTransform.fromTransform(t),
color=RGBAColor(*colorChannels) if len(colorChannels) == 4 else None,
opacity=opacity,
color=RGBAColor(*colorChannels) if colorChannels else None,
)


Expand All @@ -1842,8 +1857,11 @@ def packBackgroundImage(backgroundImage, imageFileName) -> dict:
if backgroundImage.color is not None:
c = backgroundImage.color
imageDict["color"] = ",".join(
_formatChannelValue(ch) for ch in [c.red, c.green, c.blue, c.alpha]
_formatChannelValue(ch)
for ch in [c.red, c.green, c.blue, backgroundImage.opacity]
)
elif backgroundImage.opacity != 1.0:
imageDict["color"] = f"0,0,0,{_formatChannelValue(backgroundImage.opacity)}"

return imageDict

Expand Down
3 changes: 3 additions & 0 deletions src/fontra/client/core/classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@
"transformation": {
"type": "DecomposedTransform"
},
"opacity": {
"type": "float"
},
"color": {
"type": "RGBAColor",
"optional": true
Expand Down
1 change: 1 addition & 0 deletions src/fontra/client/core/var-glyph.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export function copyBackgroundImage(image) {
return {
identifier: image.identifier,
transformation: { ...getDecomposedIdentity(), ...image.transformation },
opacity: image.opacity !== undefined ? image.opacity : 1.0,
color: image.color ? { ...image.color } : undefined,
customData: copyCustomData(image.customData || {}),
};
Expand Down
1 change: 1 addition & 0 deletions src/fontra/client/lang/de.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const strings = {
"axes.undo.add": "Achse %0 hinzufügen",
"axes.undo.delete": "Achse %0 entfernen",
"axes.undo.edit": "Achse %0 bearbeiten",
"background-image.labels.opacity": "Transparenz",
"canvas.clean-view-and-hand-tool": "Ungehinderte Sicht und Hand Werkzeug",
"cross-axis-mapping.axis-participates":
"Wenn markiert, dann ist diese Achse teil des Mappings",
Expand Down
1 change: 1 addition & 0 deletions src/fontra/client/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const strings = {
"axes.undo.add": "add axis %0",
"axes.undo.delete": "delete axis %0",
"axes.undo.edit": "edit axis %0",
"background-image.labels.opacity": "Opacity",
"canvas.clean-view-and-hand-tool": "Clean View and Hand Tool",
"cross-axis-mapping.axis-participates":
"When checked, this axis participates in the mapping",
Expand Down
1 change: 1 addition & 0 deletions src/fontra/client/lang/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const strings = {
"axes.undo.add": "add axis %0",
"axes.undo.delete": "delete axis %0",
"axes.undo.edit": "edit axis %0",
"background-image.labels.opacity": "Opacity",
"canvas.clean-view-and-hand-tool": "Prévisualisation et outil de déplacement",
"cross-axis-mapping.axis-participates":
"When checked, this axis participates in the mapping",
Expand Down
1 change: 1 addition & 0 deletions src/fontra/client/lang/ja.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const strings = {
"axes.undo.add": "補完軸%0を追加",
"axes.undo.delete": "補完軸%0を削除",
"axes.undo.edit": "補完軸%0を編集",
"background-image.labels.opacity": "Opacity",
"canvas.clean-view-and-hand-tool": "塗りのプレビューと手のひらツール",
"cross-axis-mapping.axis-participates":
"チェックすると、この補完軸がマッピング内で有効になります",
Expand Down
1 change: 1 addition & 0 deletions src/fontra/client/lang/nl.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const strings = {
"axes.undo.add": "add axis %0",
"axes.undo.delete": "delete axis %0",
"axes.undo.edit": "edit axis %0",
"background-image.labels.opacity": "Opacity",
"canvas.clean-view-and-hand-tool": "Schone weergave en Hand gereedschap",
"cross-axis-mapping.axis-participates":
"When checked, this axis participates in the mapping",
Expand Down
1 change: 1 addition & 0 deletions src/fontra/client/lang/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const strings = {
"axes.undo.add": "添加参数轴 %0",
"axes.undo.delete": "删除参数轴 %0",
"axes.undo.edit": "编辑参数轴 %0",
"background-image.labels.opacity": "透明度",
"canvas.clean-view-and-hand-tool": "预览与拖拽工具",
"cross-axis-mapping.axis-participates": "选中后,该参数轴参与映射",
"cross-axis-mapping.delete": "删除跨轴映射",
Expand Down
9 changes: 9 additions & 0 deletions src/fontra/client/web-components/ui-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export class Form extends SimpleElement {
grid-column: 1 / span 2;
}

.ui-form-line-spacer {
grid-column: 1 / span 2;
height: 0.2em;
}

.ui-form-label.header {
overflow-x: unset;
font-weight: bold;
Expand Down Expand Up @@ -140,6 +145,10 @@ export class Form extends SimpleElement {
this.contentElement.appendChild(html.hr());
continue;
}
if (fieldItem.type === "line-spacer") {
this.contentElement.appendChild(html.div({ class: "ui-form-line-spacer" }));
continue;
}
if (fieldItem.type === "spacer") {
this.contentElement.appendChild(html.br());
continue;
Expand Down
1 change: 1 addition & 0 deletions src/fontra/core/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class RGBAColor:
class BackgroundImage:
identifier: str
transformation: DecomposedTransform = field(default_factory=DecomposedTransform)
opacity: float = 1.0
color: Optional[RGBAColor] = None
customData: CustomData = field(default_factory=dict)

Expand Down
1 change: 1 addition & 0 deletions src/fontra/views/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2234,6 +2234,7 @@ export class EditorController {
layerGlyph.backgroundImage = {
identifier: imageIdentifier,
transformation: getDecomposedIdentity(),
opacity: 1.0,
};
imageIdentifiers.push(imageIdentifier);
}
Expand Down
178 changes: 112 additions & 66 deletions src/fontra/views/editor/panel-selection-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { translate } from "/core/localization.js";
import { rectFromPoints, rectSize, unionRect } from "/core/rectangle.js";
import { getDecomposedIdentity } from "/core/transform.js";
import {
assert,
enumerate,
getCharFromCodePoint,
makeUPlusStringFromCodePoint,
Expand Down Expand Up @@ -114,9 +115,10 @@ export default class SelectionInfoPanel extends Panel {
async update(senderInfo) {
if (
senderInfo?.senderID === this &&
senderInfo?.fieldKeyPath?.length !== 3 &&
senderInfo?.fieldKeyPath?.[0] !== "component" &&
senderInfo?.fieldKeyPath?.[2] !== "name"
((senderInfo?.fieldKeyPath?.length !== 3 &&
senderInfo?.fieldKeyPath?.[0] !== "component" &&
senderInfo?.fieldKeyPath?.[2] !== "name") ||
senderInfo?.fieldKeyPath?.[0] === "backgroundImage")
) {
// Don't rebuild, just update the Dimensions field
await this.updateDimensions();
Expand Down Expand Up @@ -255,14 +257,51 @@ export default class SelectionInfoPanel extends Panel {
}
}

const { pointIndices, componentIndices } = this._getSelection();
const { pointIndices, componentIndices, backgroundImageIndices } =
this._getSelection();

if (glyphController) {
formContents.push(
...this._setupDimensionsInfo(glyphController, pointIndices, componentIndices)
);
}

for (const index of backgroundImageIndices) {
assert(index === 0, "only a single bg image is supported");

const backgroundImage = instance?.backgroundImage;
if (!backgroundImage) {
continue;
}

const backgroundImageKey = (...path) =>
JSON.stringify(["backgroundImage", ...path]);

formContents.push({ type: "divider" });
formContents.push({
type: "header",
label: translate("sidebar.user-settings.glyph.background-image"),
});

formContents.push({
type: "edit-number-slider",
key: backgroundImageKey("opacity"),
label: translate("background-image.labels.opacity"),
value: backgroundImage.opacity,
minValue: 0,
defaultValue: 1.0,
maxValue: 1.0,
});

formContents.push({ type: "line-spacer" });

addTransformationItems(
formContents,
backgroundImageKey,
backgroundImage.transformation
);
}

for (const index of componentIndices) {
if (!instance) {
break;
Expand Down Expand Up @@ -299,64 +338,7 @@ export default class SelectionInfoPanel extends Panel {
}),
});

formContents.push({
type: "edit-number-x-y",
label: translate("sidebar.selection-info.component.translate"),
fieldX: {
key: componentKey("transformation", "translateX"),
value: component.transformation.translateX,
},
fieldY: {
key: componentKey("transformation", "translateY"),
value: component.transformation.translateY,
},
});

formContents.push({
type: "edit-angle",
key: componentKey("transformation", "rotation"),
label: translate("sidebar.selection-info.component.rotation"),
value: component.transformation.rotation,
});

formContents.push({
type: "edit-number-x-y",
label: translate("sidebar.selection-info.component.scale"),
fieldX: {
key: componentKey("transformation", "scaleX"),
value: component.transformation.scaleX,
},
fieldY: {
key: componentKey("transformation", "scaleY"),
value: component.transformation.scaleY,
},
});

formContents.push({
type: "edit-number-x-y",
label: translate("sidebar.selection-info.component.skew"),
fieldX: {
key: componentKey("transformation", "skewX"),
value: component.transformation.skewX,
},
fieldY: {
key: componentKey("transformation", "skewY"),
value: component.transformation.skewY,
},
});

formContents.push({
type: "edit-number-x-y",
label: translate("sidebar.selection-info.component.center"),
fieldX: {
key: componentKey("transformation", "tCenterX"),
value: component.transformation.tCenterX,
},
fieldY: {
key: componentKey("transformation", "tCenterY"),
value: component.transformation.tCenterY,
},
});
addTransformationItems(formContents, componentKey, component.transformation);

const baseGlyph = await this.fontController.getGlyph(component.name);
if (baseGlyph && component.location) {
Expand Down Expand Up @@ -561,9 +543,8 @@ export default class SelectionInfoPanel extends Panel {
}

_getSelection() {
const { point, component, componentOrigin, componentTCenter } = parseSelection(
this.sceneController.selection
);
const { point, component, componentOrigin, componentTCenter, backgroundImage } =
parseSelection(this.sceneController.selection);

const componentIndices = [
...new Set([
Expand All @@ -572,7 +553,11 @@ export default class SelectionInfoPanel extends Panel {
...(componentTCenter || []),
]),
].sort((a, b) => a - b);
return { pointIndices: point || [], componentIndices };
return {
pointIndices: point || [],
componentIndices,
backgroundImageIndices: backgroundImage || [],
};
}

_getDimensionsString(glyphController, pointIndices, componentIndices) {
Expand Down Expand Up @@ -705,6 +690,67 @@ export default class SelectionInfoPanel extends Panel {
}
}

function addTransformationItems(formContents, keyFunc, transformation) {
formContents.push({
type: "edit-number-x-y",
label: translate("sidebar.selection-info.component.translate"),
fieldX: {
key: keyFunc("transformation", "translateX"),
value: transformation.translateX,
},
fieldY: {
key: keyFunc("transformation", "translateY"),
value: transformation.translateY,
},
});

formContents.push({
type: "edit-angle",
key: keyFunc("transformation", "rotation"),
label: translate("sidebar.selection-info.component.rotation"),
value: transformation.rotation,
});

formContents.push({
type: "edit-number-x-y",
label: translate("sidebar.selection-info.component.scale"),
fieldX: {
key: keyFunc("transformation", "scaleX"),
value: transformation.scaleX,
},
fieldY: {
key: keyFunc("transformation", "scaleY"),
value: transformation.scaleY,
},
});

formContents.push({
type: "edit-number-x-y",
label: translate("sidebar.selection-info.component.skew"),
fieldX: {
key: keyFunc("transformation", "skewX"),
value: transformation.skewX,
},
fieldY: {
key: keyFunc("transformation", "skewY"),
value: transformation.skewY,
},
});

formContents.push({
type: "edit-number-x-y",
label: translate("sidebar.selection-info.component.center"),
fieldX: {
key: keyFunc("transformation", "tCenterX"),
value: transformation.tCenterX,
},
fieldY: {
key: keyFunc("transformation", "tCenterY"),
value: transformation.tCenterY,
},
});
}

function defaultGetFieldValue(glyph, glyphController, fieldItem) {
const changePath = JSON.parse(fieldItem.key);
return getNestedValue(glyph, changePath);
Expand Down
Loading