font-xform.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. const ColorXform = require('./color-xform');
  3. const BooleanXform = require('../simple/boolean-xform');
  4. const IntegerXform = require('../simple/integer-xform');
  5. const StringXform = require('../simple/string-xform');
  6. const UnderlineXform = require('./underline-xform');
  7. const _ = require('../../../utils/under-dash');
  8. const BaseXform = require('../base-xform');
  9. // Font encapsulates translation from font model to xlsx
  10. class FontXform extends BaseXform {
  11. constructor(options) {
  12. super();
  13. this.options = options || FontXform.OPTIONS;
  14. this.map = {
  15. b: {prop: 'bold', xform: new BooleanXform({tag: 'b', attr: 'val'})},
  16. i: {prop: 'italic', xform: new BooleanXform({tag: 'i', attr: 'val'})},
  17. u: {prop: 'underline', xform: new UnderlineXform()},
  18. charset: {prop: 'charset', xform: new IntegerXform({tag: 'charset', attr: 'val'})},
  19. color: {prop: 'color', xform: new ColorXform()},
  20. condense: {prop: 'condense', xform: new BooleanXform({tag: 'condense', attr: 'val'})},
  21. extend: {prop: 'extend', xform: new BooleanXform({tag: 'extend', attr: 'val'})},
  22. family: {prop: 'family', xform: new IntegerXform({tag: 'family', attr: 'val'})},
  23. outline: {prop: 'outline', xform: new BooleanXform({tag: 'outline', attr: 'val'})},
  24. vertAlign: {prop: 'vertAlign', xform: new StringXform({tag: 'vertAlign', attr: 'val'})},
  25. scheme: {prop: 'scheme', xform: new StringXform({tag: 'scheme', attr: 'val'})},
  26. shadow: {prop: 'shadow', xform: new BooleanXform({tag: 'shadow', attr: 'val'})},
  27. strike: {prop: 'strike', xform: new BooleanXform({tag: 'strike', attr: 'val'})},
  28. sz: {prop: 'size', xform: new IntegerXform({tag: 'sz', attr: 'val'})},
  29. };
  30. this.map[this.options.fontNameTag] = {
  31. prop: 'name',
  32. xform: new StringXform({tag: this.options.fontNameTag, attr: 'val'}),
  33. };
  34. }
  35. get tag() {
  36. return this.options.tagName;
  37. }
  38. render(xmlStream, model) {
  39. const {map} = this;
  40. xmlStream.openNode(this.options.tagName);
  41. _.each(this.map, (defn, tag) => {
  42. map[tag].xform.render(xmlStream, model[defn.prop]);
  43. });
  44. xmlStream.closeNode();
  45. }
  46. parseOpen(node) {
  47. if (this.parser) {
  48. this.parser.parseOpen(node);
  49. return true;
  50. }
  51. if (this.map[node.name]) {
  52. this.parser = this.map[node.name].xform;
  53. return this.parser.parseOpen(node);
  54. }
  55. switch (node.name) {
  56. case this.options.tagName:
  57. this.model = {};
  58. return true;
  59. default:
  60. return false;
  61. }
  62. }
  63. parseText(text) {
  64. if (this.parser) {
  65. this.parser.parseText(text);
  66. }
  67. }
  68. parseClose(name) {
  69. if (this.parser && !this.parser.parseClose(name)) {
  70. const item = this.map[name];
  71. if (this.parser.model) {
  72. this.model[item.prop] = this.parser.model;
  73. }
  74. this.parser = undefined;
  75. return true;
  76. }
  77. switch (name) {
  78. case this.options.tagName:
  79. return false;
  80. default:
  81. return true;
  82. }
  83. }
  84. }
  85. FontXform.OPTIONS = {
  86. tagName: 'font',
  87. fontNameTag: 'name',
  88. };
  89. module.exports = FontXform;