Skip to content

Commit

Permalink
feat(shape): Implement simple text shape with FIG fonts support
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaiklor committed Dec 17, 2015
1 parent 56d62c0 commit e70fe29
Showing 1 changed file with 104 additions and 2 deletions.
106 changes: 104 additions & 2 deletions src/FigText.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,109 @@
import Text from 'kittik-shape-text';
import Shape from 'kittik-shape-basic';
import figlet from 'figlet';

export default class FigText extends Text {
export default class FigText extends Shape {
constructor(options = {}) {
super(options);

this.setFont(options.font);
this.setHorizontalLayout(options.horizontalLayout);
this.setVerticalLayout(options.verticalLayout);
}

/**
* Get font that uses for rendering text.
*
* @returns {String}
*/
getFont() {
return this.get('font');
}

/**
* Set font that will be used for rendering the text.
*
* @param {String} [font=Standard]
* @returns {FigText}
*/
setFont(font = 'Standard') {
return this.set('font', font);
}

/**
* Get horizontal layout.
*
* @returns {String}
*/
getHorizontalLayout() {
return this.get('horizontalLayout');
}

/**
* Set horizontal layout.
*
* @param {String} [layout=default]
* @returns {FigText}
*/
setHorizontalLayout(layout = 'default') {
if (['default', 'full', 'fitted'].indexOf(layout) === -1) throw new Error(`Unrecognized layout: ${layout}`);
return this.set('horizontalLayout', layout);
}

/**
* Get vertical layout.
*
* @returns {String}
*/
getVerticalLayout() {
return this.get('verticalLayout');
}

/**
* Set vertical layout.
*
* @param {String} [layout=default]
* @returns {FigText}
*/
setVerticalLayout(layout = 'default') {
if (['default', 'full', 'fitted'].indexOf(layout) === -1) throw new Error(`Unrecognized layout: ${layout}`);
return this.set('verticalLayout', layout);
}

/**
* Render the shape.
*
* @param {Cursor} cursor
*/
render(cursor) {
let x = this.getX();
let y = this.getY();
let background = this.getBackground();
let foreground = this.getForeground();
let font = this.getFont();
let horizontalLayout = this.getHorizontalLayout();
let verticalLayout = this.getVerticalLayout();
let text = figlet.textSync(this.getText(), {font, horizontalLayout, verticalLayout}).split('\n');

if (typeof background !== 'undefined') cursor.background(background);
if (typeof foreground !== 'undefined') cursor.foreground(foreground);

text.forEach((item, index) => cursor.moveTo(x, y + index).write(item));
}

/**
* Serialize shape to object representation.
*
* @returns {{name, options}|*}
*/
toObject() {
let obj = super.toObject();

Object.assign(obj.options, {
font: this.getFont(),
horizontalLayout: this.getHorizontalLayout(),
verticalLayout: this.getVerticalLayout()
});

return obj;
}
}

0 comments on commit e70fe29

Please sign in to comment.