bigtext.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * bigtext.js - bigtext element for blessed
  3. * Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
  4. * https://github.com/chjj/blessed
  5. */
  6. /**
  7. * Modules
  8. */
  9. var fs = require('fs');
  10. var Node = require('./node');
  11. var Box = require('./box');
  12. /**
  13. * BigText
  14. */
  15. function BigText(options) {
  16. if (!(this instanceof Node)) {
  17. return new BigText(options);
  18. }
  19. options = options || {};
  20. options.font = options.font
  21. || __dirname + '/../../usr/fonts/ter-u14n.json';
  22. options.fontBold = options.font
  23. || __dirname + '/../../usr/fonts/ter-u14b.json';
  24. this.fch = options.fch;
  25. this.ratio = {};
  26. this.font = this.loadFont(options.font);
  27. this.fontBold = this.loadFont(options.font);
  28. Box.call(this, options);
  29. if (this.style.bold) {
  30. this.font = this.fontBold;
  31. }
  32. }
  33. BigText.prototype.__proto__ = Box.prototype;
  34. BigText.prototype.type = 'bigtext';
  35. BigText.prototype.loadFont = function(filename) {
  36. var self = this
  37. , data
  38. , font;
  39. data = JSON.parse(fs.readFileSync(filename, 'utf8'));
  40. this.ratio.width = data.width;
  41. this.ratio.height = data.height;
  42. function convertLetter(ch, lines) {
  43. var line, i;
  44. while (lines.length > self.ratio.height) {
  45. lines.shift();
  46. lines.pop();
  47. }
  48. lines = lines.map(function(line) {
  49. var chs = line.split('');
  50. chs = chs.map(function(ch) {
  51. return ch === ' ' ? 0 : 1;
  52. });
  53. while (chs.length < self.ratio.width) {
  54. chs.push(0);
  55. }
  56. return chs;
  57. });
  58. while (lines.length < self.ratio.height) {
  59. line = [];
  60. for (i = 0; i < self.ratio.width; i++) {
  61. line.push(0);
  62. }
  63. lines.push(line);
  64. }
  65. return lines;
  66. }
  67. font = Object.keys(data.glyphs).reduce(function(out, ch) {
  68. var lines = data.glyphs[ch].map;
  69. out[ch] = convertLetter(ch, lines);
  70. return out;
  71. }, {});
  72. delete font[' '];
  73. return font;
  74. };
  75. BigText.prototype.setContent = function(content) {
  76. this.content = '';
  77. this.text = content || '';
  78. };
  79. BigText.prototype.render = function() {
  80. if (this.position.width == null || this._shrinkWidth) {
  81. // if (this.width - this.iwidth < this.ratio.width * this.text.length + 1) {
  82. this.position.width = this.ratio.width * this.text.length + 1;
  83. this._shrinkWidth = true;
  84. // }
  85. }
  86. if (this.position.height == null || this._shrinkHeight) {
  87. // if (this.height - this.iheight < this.ratio.height + 0) {
  88. this.position.height = this.ratio.height + 0;
  89. this._shrinkHeight = true;
  90. // }
  91. }
  92. var coords = this._render();
  93. if (!coords) return;
  94. var lines = this.screen.lines
  95. , left = coords.xi + this.ileft
  96. , top = coords.yi + this.itop
  97. , right = coords.xl - this.iright
  98. , bottom = coords.yl - this.ibottom;
  99. var dattr = this.sattr(this.style)
  100. , bg = dattr & 0x1ff
  101. , fg = (dattr >> 9) & 0x1ff
  102. , flags = (dattr >> 18) & 0x1ff
  103. , attr = (flags << 18) | (bg << 9) | fg;
  104. for (var x = left, i = 0; x < right; x += this.ratio.width, i++) {
  105. var ch = this.text[i];
  106. if (!ch) break;
  107. var map = this.font[ch];
  108. if (!map) continue;
  109. for (var y = top; y < Math.min(bottom, top + this.ratio.height); y++) {
  110. if (!lines[y]) continue;
  111. var mline = map[y - top];
  112. if (!mline) continue;
  113. for (var mx = 0; mx < this.ratio.width; mx++) {
  114. var mcell = mline[mx];
  115. if (mcell == null) break;
  116. if (this.fch && this.fch !== ' ') {
  117. lines[y][x + mx][0] = dattr;
  118. lines[y][x + mx][1] = mcell === 1 ? this.fch : this.ch;
  119. } else {
  120. lines[y][x + mx][0] = mcell === 1 ? attr : dattr;
  121. lines[y][x + mx][1] = mcell === 1 ? ' ' : this.ch;
  122. }
  123. }
  124. lines[y].dirty = true;
  125. }
  126. }
  127. return coords;
  128. };
  129. /**
  130. * Expose
  131. */
  132. module.exports = BigText;