TextStyle.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Created by admin on 2018-05-17.
  3. */
  4. import * as PIXI from './pixi';
  5. var TextStyle = (function () {
  6. function TextStyle() {
  7. }
  8. TextStyle.findStyle = function (data) {
  9. var style = null;
  10. var pass = true;
  11. for (var i = 0; i < this.styles.length; i++) {
  12. pass = true;
  13. if (data.font) {
  14. if (this.styles[i].fontFamily != data.font) {
  15. pass = false;
  16. break;
  17. }
  18. }
  19. if (data.size) {
  20. if (this.styles[i].fontSize != data.size + "px") {
  21. pass = false;
  22. break;
  23. }
  24. }
  25. if (data.fillCol) {
  26. if (this.styles[i].fill != data.fillCol) {
  27. pass = false;
  28. break;
  29. }
  30. }
  31. if (data.strokeCol) {
  32. if (this.styles[i].stroke != data.strokeCol) {
  33. pass = false;
  34. break;
  35. }
  36. }
  37. if (data.strokeThick) {
  38. if (this.styles[i].strokeThickness != data.strokeThick) {
  39. pass = false;
  40. break;
  41. }
  42. }
  43. if (data.fontWeight) {
  44. if (this.styles[i].fontWeight != data.fontWeight) {
  45. pass = false;
  46. break;
  47. }
  48. }
  49. if (data.fontAlign) {
  50. if (this.styles[i].fontAlign != data.fontAlign) {
  51. pass = false;
  52. break;
  53. }
  54. }
  55. if (data.wordWrapWidth) {
  56. if (this.styles[i].wordWrapWidth != data.wordWrapWidth) {
  57. pass = false;
  58. break;
  59. }
  60. }
  61. style = this.styles[i];
  62. break;
  63. }
  64. return style;
  65. };
  66. //font:string, size:string, fillCol:string, strokeCol:string, strokeThick:number, fontWeight:string, fontAlign:string, wordWrapWidth:number
  67. TextStyle.makeStyle = function (data) {
  68. var style = this.findStyle(data);
  69. if (!style) {
  70. style = new PIXI.TextStyle({
  71. fontFamily: (data.font) ? data.font : 'Arial',
  72. fontSize: (data.size) ? data.size + "px" : '20px',
  73. fill: (data.fillCol) ? data.fillCol : '#ffffff',
  74. stroke: (data.strokeCol) ? data.strokeCol : '#000000',
  75. strokeThickness: (data.strokeThick) ? data.strokeThick : 0,
  76. fontWeight: (data.fontWeight) ? data.fontWeight : 'normal',
  77. fontAlign: (data.fontAlign) ? data.fontAlign : 'center',
  78. wordWrap: (data.wordWrapWidth && data.wordWrapWidth > 0) ? true : false,
  79. wordWrapWidth: (data.wordWrapWidth && data.wordWrapWidth > 0) ? data.wordWrapWidth : 0
  80. });
  81. this.styles.push(style);
  82. }
  83. return style;
  84. };
  85. return TextStyle;
  86. }());
  87. TextStyle.styles = [];
  88. module.exports = TextStyle