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

Adding in font-family for text #66

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
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
1 change: 1 addition & 0 deletions assets/help/help.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"text": {
"type": "Sets the text type. The text may be automatically generated depending of the setting.",
"text_content": "Sets the text to display, when allowed.",
"font_family": "Sets the font family of the text",
"font_size": "Sets the font size in pixels.",
"decoration": "Respectively toggle italic, bold, underline, overline, and line through on the text.",
"text_align": "Sets how the text should align itself in the container."
Expand Down
5 changes: 5 additions & 0 deletions docs/save.md
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ This is documented from a created save with default values. For more information
"visual_object_type": "text",
"text_type": "any",
"text_content": "text",
"font_family" : "verdana",
"font_size": 20,
"color": "#ffffff",
"text_decoration": {
Expand Down Expand Up @@ -1014,6 +1015,10 @@ This is documented from a created save with default values. For more information
- **type:** String
- **allowed values:** Any string without any "\".
- **description:** Text content displayed by the object.
- `font_family`:
- **type:** String
- **allowed values:** serif, sans-serif, monospace, cursive, fantasy...
- **description:** Font family of the displayed text.
- `font_size`:
- **type:** integer
- **allowed values:** value >= 0
Expand Down
1 change: 1 addition & 0 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ class SaveHandler {
object.svg_filter = old_object.svg_filters;
object.text_type = old_object.type;
object.text_content = old_object.text;
object.font_family = old_object.font_family;
object.font_size = old_object.font_size;
object.color = old_object.color;
object.text_decoration = {
Expand Down
7 changes: 6 additions & 1 deletion js/visual_objects/visual_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ export class VText extends VisualObject {

this._properties["text_type"] = new property.VPTextType(this._save_handler, this);
this._properties["text_content"] = new property.VPTextContent(this._save_handler, this);
this._properties["font_family"] = new property.VPFontFamily(this._save_handler, this);
this._properties["font_size"] = new property.VPFontSize(this._save_handler, this);
this._properties["color"] = new property.VPColor(this._save_handler, this);
this._properties["text_decoration"] = new property.VPTextDecoration(this._save_handler, this);
Expand Down Expand Up @@ -345,6 +346,10 @@ export class VText extends VisualObject {
this._element.innerHTML = value;
});

this._properties["font_family"].subscribeToEvent("value_changed", (value) => {
this._element.style.fontFamily = value;
});

this._properties["font_size"].subscribeToEvent("value_changed", (value) => {
this._element.style.fontSize = value + "px";
});
Expand Down Expand Up @@ -1815,4 +1820,4 @@ export class VShape extends VisualObject {
return true;
}

}
}
59 changes: 59 additions & 0 deletions js/visual_objects/visual_object_property.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const DEFAULTS = {

TEXT_TYPE: "any",
TEXT_CONTENT: "text",
FONT_FAMILY: "verdana",
FONT_SIZE: 20,
TEXT_DECORATION: {
italic: false,
Expand Down Expand Up @@ -1332,7 +1333,65 @@ export class VPTextContent extends VisualObjectProperty {
}
}

/**
* font family property, font family for the object.
*
* @export
* @class VPFontFamily
* @extends {VisualObjectProperty}
*/
export class VPFontFamily extends VisualObjectProperty {
/**
* Creates an instance of VPFontFamily.
* @param {SaveHandler} save_handler The SaveHandler to read and write from.
* @param {object.VisualObject} visual_object The VisualObject that it manipulates.
* @memberof VPFontFamily
*/
constructor(save_handler, visual_object) {
super(save_handler, visual_object, "font_family");

// create associated UI
if (!this._save_handler.owner_project.export_mode) {
this._ui_parameter = new ui_components.UIParameterString(
this._visual_object.parameter_rack,
"Font family",
this.getCurrentValue(),
() => {
this.setSaveUISafe(this._ui_parameter.value);
}
);
this._ui_parameter.help_string = help.parameter.object.text.font_family;
}
}

/**
* Get the default value of the visual object property.
* If it is undefined, assign a value.
*
* @memberof VPFontFamily
* @override
* @return {String}
*/
getDefaultValue() {
if (this._default_value) return this._default_value;
else {
this._default_value = DEFAULTS.FONT_FAMILY;
return this._default_value;
};
}

/**
* Only checking for \ like the text_content. Could possibly add validation to state if the font exists.
*
* @override
* @param {*} value
* @return {Boolean} is valid
* @memberof VPFontFamily
*/
hasValidValue(value) {
return (!utils.IsUndefined(value) && utils.IsAString(value) && !(value.indexOf("\\") > -1));
}
}

/**
* Visual property to set the font size of a text based object
Expand Down