@konoui/mjimage 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.md +83 -0
  2. package/dist/index.html +17763 -0
  3. package/dist/mjs/index.d.ts +8 -0
  4. package/dist/mjs/index.js +24 -0
  5. package/dist/mjs/lib/constants.d.ts +50 -0
  6. package/dist/mjs/lib/constants.js +48 -0
  7. package/dist/mjs/lib/context.d.ts +12 -0
  8. package/dist/mjs/lib/context.js +33 -0
  9. package/dist/mjs/lib/image.d.ts +39 -0
  10. package/dist/mjs/lib/image.js +303 -0
  11. package/dist/mjs/lib/lexer.d.ts +13 -0
  12. package/dist/mjs/lib/lexer.js +46 -0
  13. package/dist/mjs/lib/mjimage.d.ts +11 -0
  14. package/dist/mjs/lib/mjimage.js +90 -0
  15. package/dist/mjs/lib/parser.d.ts +30 -0
  16. package/dist/mjs/lib/parser.js +245 -0
  17. package/dist/mjs/lib/table-parser.d.ts +220 -0
  18. package/dist/mjs/lib/table-parser.js +114 -0
  19. package/dist/mjs/lib/table.d.ts +10 -0
  20. package/dist/mjs/lib/table.js +242 -0
  21. package/dist/mjs/svg/_0.svg +1 -0
  22. package/dist/mjs/svg/m0.svg +1 -0
  23. package/dist/mjs/svg/m1.svg +1 -0
  24. package/dist/mjs/svg/m2.svg +1 -0
  25. package/dist/mjs/svg/m3.svg +1 -0
  26. package/dist/mjs/svg/m4.svg +1 -0
  27. package/dist/mjs/svg/m5.svg +1 -0
  28. package/dist/mjs/svg/m6.svg +1 -0
  29. package/dist/mjs/svg/m7.svg +1 -0
  30. package/dist/mjs/svg/m8.svg +1 -0
  31. package/dist/mjs/svg/m9.svg +1 -0
  32. package/dist/mjs/svg/p0.svg +1 -0
  33. package/dist/mjs/svg/p1.svg +1 -0
  34. package/dist/mjs/svg/p2.svg +1 -0
  35. package/dist/mjs/svg/p3.svg +1 -0
  36. package/dist/mjs/svg/p4.svg +1 -0
  37. package/dist/mjs/svg/p5.svg +1 -0
  38. package/dist/mjs/svg/p6.svg +1 -0
  39. package/dist/mjs/svg/p7.svg +1 -0
  40. package/dist/mjs/svg/p8.svg +1 -0
  41. package/dist/mjs/svg/p9.svg +1 -0
  42. package/dist/mjs/svg/s0.svg +1 -0
  43. package/dist/mjs/svg/s1.svg +1 -0
  44. package/dist/mjs/svg/s2.svg +1 -0
  45. package/dist/mjs/svg/s3.svg +1 -0
  46. package/dist/mjs/svg/s4.svg +1 -0
  47. package/dist/mjs/svg/s5.svg +1 -0
  48. package/dist/mjs/svg/s6.svg +1 -0
  49. package/dist/mjs/svg/s7.svg +1 -0
  50. package/dist/mjs/svg/s8.svg +1 -0
  51. package/dist/mjs/svg/s9.svg +1 -0
  52. package/dist/mjs/svg/stick100.svg +1 -0
  53. package/dist/mjs/svg/stick1000.svg +1 -0
  54. package/dist/mjs/svg/z1.svg +1 -0
  55. package/dist/mjs/svg/z2.svg +1 -0
  56. package/dist/mjs/svg/z3.svg +1 -0
  57. package/dist/mjs/svg/z4.svg +1 -0
  58. package/dist/mjs/svg/z5.svg +1 -0
  59. package/dist/mjs/svg/z6.svg +1 -0
  60. package/dist/mjs/svg/z7.svg +1 -0
  61. package/dist/svg/_0.svg +1 -0
  62. package/dist/svg/m0.svg +1 -0
  63. package/dist/svg/m1.svg +1 -0
  64. package/dist/svg/m2.svg +1 -0
  65. package/dist/svg/m3.svg +1 -0
  66. package/dist/svg/m4.svg +1 -0
  67. package/dist/svg/m5.svg +1 -0
  68. package/dist/svg/m6.svg +1 -0
  69. package/dist/svg/m7.svg +1 -0
  70. package/dist/svg/m8.svg +1 -0
  71. package/dist/svg/m9.svg +1 -0
  72. package/dist/svg/p0.svg +1 -0
  73. package/dist/svg/p1.svg +1 -0
  74. package/dist/svg/p2.svg +1 -0
  75. package/dist/svg/p3.svg +1 -0
  76. package/dist/svg/p4.svg +1 -0
  77. package/dist/svg/p5.svg +1 -0
  78. package/dist/svg/p6.svg +1 -0
  79. package/dist/svg/p7.svg +1 -0
  80. package/dist/svg/p8.svg +1 -0
  81. package/dist/svg/p9.svg +1 -0
  82. package/dist/svg/s0.svg +1 -0
  83. package/dist/svg/s1.svg +1 -0
  84. package/dist/svg/s2.svg +1 -0
  85. package/dist/svg/s3.svg +1 -0
  86. package/dist/svg/s4.svg +1 -0
  87. package/dist/svg/s5.svg +1 -0
  88. package/dist/svg/s6.svg +1 -0
  89. package/dist/svg/s7.svg +1 -0
  90. package/dist/svg/s8.svg +1 -0
  91. package/dist/svg/s9.svg +1 -0
  92. package/dist/svg/stick100.svg +1 -0
  93. package/dist/svg/stick1000.svg +1 -0
  94. package/dist/svg/z1.svg +1 -0
  95. package/dist/svg/z2.svg +1 -0
  96. package/dist/svg/z3.svg +1 -0
  97. package/dist/svg/z4.svg +1 -0
  98. package/dist/svg/z5.svg +1 -0
  99. package/dist/svg/z6.svg +1 -0
  100. package/dist/svg/z7.svg +1 -0
  101. package/dist/tiles.1417fc37.svg +1 -0
  102. package/package.json +71 -0
@@ -0,0 +1,8 @@
1
+ export * from "./lib/mjimage";
2
+ export * from "./lib/constants";
3
+ export * from "./lib/context";
4
+ export * from "./lib/lexer";
5
+ export * from "./lib/image";
6
+ export * from "./lib/parser";
7
+ export * from "./lib/table";
8
+ export * from "./lib/table-parser";
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./lib/mjimage"), exports);
18
+ __exportStar(require("./lib/constants"), exports);
19
+ __exportStar(require("./lib/context"), exports);
20
+ __exportStar(require("./lib/lexer"), exports);
21
+ __exportStar(require("./lib/image"), exports);
22
+ __exportStar(require("./lib/parser"), exports);
23
+ __exportStar(require("./lib/table"), exports);
24
+ __exportStar(require("./lib/table-parser"), exports);
@@ -0,0 +1,50 @@
1
+ export declare const FONT_FAMILY = "MS Gothic, sans-serif";
2
+ export declare const TILE_CONTEXT: {
3
+ WIDTH: number;
4
+ HEIGHT: number;
5
+ };
6
+ export declare const TABLE_CONTEXT: {
7
+ BASE: number;
8
+ };
9
+ export declare const KIND: {
10
+ readonly M: "m";
11
+ readonly P: "p";
12
+ readonly S: "s";
13
+ readonly Z: "z";
14
+ readonly BACK: "_";
15
+ readonly SEPARATOR: ",";
16
+ };
17
+ export declare const OPERATOR: {
18
+ readonly TSUMO: "t";
19
+ readonly DORA: "d";
20
+ readonly HORIZONTAL: "-";
21
+ readonly COLOR_GRAYSCALE: "g";
22
+ };
23
+ export declare const BLOCK: {
24
+ readonly PON: "pon";
25
+ readonly CHI: "chi";
26
+ readonly SHO_KAN: "shokan";
27
+ readonly DAI_KAN: "daikan";
28
+ readonly AN_KAN: "ankan";
29
+ readonly DORA: "dora";
30
+ readonly TSUMO: "tsumo";
31
+ readonly HAND: "hand";
32
+ readonly DISCARD: "simple-discard";
33
+ readonly UNKNOWN: "unknown";
34
+ };
35
+ export declare const ROUND_MAP: {
36
+ readonly "1w1": "東1局";
37
+ readonly "1w2": "東2局";
38
+ readonly "1w3": "東3局";
39
+ readonly "1w4": "東4局";
40
+ readonly "2w1": "南1局";
41
+ readonly "2w2": "南2局";
42
+ readonly "2w3": "南3局";
43
+ readonly "2w4": "南4局";
44
+ };
45
+ export declare const WIND_MAP: {
46
+ readonly "1w": "東";
47
+ readonly "2w": "南";
48
+ readonly "3w": "西";
49
+ readonly "4w": "北";
50
+ };
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WIND_MAP = exports.ROUND_MAP = exports.BLOCK = exports.OPERATOR = exports.KIND = exports.TABLE_CONTEXT = exports.TILE_CONTEXT = exports.FONT_FAMILY = void 0;
4
+ exports.FONT_FAMILY = "MS Gothic, sans-serif";
5
+ exports.TILE_CONTEXT = { WIDTH: 66, HEIGHT: 90 };
6
+ exports.TABLE_CONTEXT = { BASE: 40 };
7
+ exports.KIND = {
8
+ M: "m",
9
+ P: "p",
10
+ S: "s",
11
+ Z: "z",
12
+ BACK: "_",
13
+ SEPARATOR: ",",
14
+ };
15
+ exports.OPERATOR = {
16
+ TSUMO: "t",
17
+ DORA: "d",
18
+ HORIZONTAL: "-",
19
+ COLOR_GRAYSCALE: "g",
20
+ };
21
+ exports.BLOCK = {
22
+ PON: "pon",
23
+ CHI: "chi",
24
+ SHO_KAN: "shokan",
25
+ DAI_KAN: "daikan",
26
+ AN_KAN: "ankan",
27
+ DORA: "dora",
28
+ TSUMO: "tsumo",
29
+ HAND: "hand",
30
+ DISCARD: "simple-discard",
31
+ UNKNOWN: "unknown",
32
+ };
33
+ exports.ROUND_MAP = {
34
+ "1w1": "東1局",
35
+ "1w2": "東2局",
36
+ "1w3": "東3局",
37
+ "1w4": "東4局",
38
+ "2w1": "南1局",
39
+ "2w2": "南2局",
40
+ "2w3": "南3局",
41
+ "2w4": "南4局",
42
+ };
43
+ exports.WIND_MAP = {
44
+ "1w": "東",
45
+ "2w": "南",
46
+ "3w": "西",
47
+ "4w": "北",
48
+ };
@@ -0,0 +1,12 @@
1
+ export interface FontContext {
2
+ font: {
3
+ family: string;
4
+ size: number;
5
+ };
6
+ textWidth: number;
7
+ textHeight: number;
8
+ numWidth: number;
9
+ numHeight: number;
10
+ }
11
+ export declare const getFontContext: (ctx: CanvasRenderingContext2D, fontFamily: string, fontSize: number) => FontContext;
12
+ export declare const getTableFontContext: (ctx: CanvasRenderingContext2D, scale: number) => FontContext;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTableFontContext = exports.getFontContext = void 0;
4
+ var constants_1 = require("./constants");
5
+ var getContext = function (ctx, str, font) {
6
+ ctx.font = font;
7
+ var metrics = ctx.measureText(str);
8
+ var width = metrics.width;
9
+ var height = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
10
+ return [width, height];
11
+ };
12
+ var getFontContext = function (ctx, fontFamily, fontSize) {
13
+ var font = { family: fontFamily, size: fontSize };
14
+ var fontString = "".concat(font.size, "px ").concat(font.family);
15
+ var _a = getContext(ctx, "東", fontString), textWidth = _a[0], textHeight = _a[1];
16
+ var _b = getContext(ctx, "2", fontString), numWidth = _b[0], numHeight = _b[1];
17
+ var ret = {
18
+ font: { family: fontFamily, size: fontSize },
19
+ textWidth: textWidth,
20
+ textHeight: textHeight,
21
+ numWidth: numWidth,
22
+ numHeight: numHeight,
23
+ };
24
+ return ret;
25
+ };
26
+ exports.getFontContext = getFontContext;
27
+ var getTableFontContext = function (ctx, scale) {
28
+ var fontCtx = (0, exports.getFontContext)(ctx, constants_1.FONT_FAMILY, constants_1.TABLE_CONTEXT.BASE * scale);
29
+ fontCtx.textHeight = fontCtx.textWidth;
30
+ fontCtx.numHeight = fontCtx.numWidth;
31
+ return fontCtx;
32
+ };
33
+ exports.getTableFontContext = getTableFontContext;
@@ -0,0 +1,39 @@
1
+ import { Tile, Block } from "./parser";
2
+ import { Svg, G, Image, Use } from "@svgdotjs/svg.js";
3
+ export interface ImageHelperConfig {
4
+ scale?: number;
5
+ imageHostPath?: string;
6
+ imageHostUrl?: string;
7
+ svgSprite?: boolean;
8
+ }
9
+ declare class BaseHelper {
10
+ readonly tileWidth: number;
11
+ readonly tileHeight: number;
12
+ readonly diffTileHeightWidth: number;
13
+ readonly textWidth: number;
14
+ readonly image_host_path: string;
15
+ readonly image_host_url: string;
16
+ readonly scale: number;
17
+ readonly svgSprite: boolean;
18
+ constructor(props?: ImageHelperConfig);
19
+ private image;
20
+ createImage(tile: Tile, x: number, y: number): Image | Use;
21
+ createTextImage(tile: Tile, x: number, y: number, t: string): G;
22
+ createStackImage(tile: Tile, x: number, y: number): G;
23
+ createRotate90Image(tile: Tile, x: number, y: number, adjustY?: boolean): G;
24
+ createStick(v: 100 | 1000): Image | Use;
25
+ buildID(tile: Tile | 100 | 1000): string;
26
+ buildURL(tile: Tile | 100 | 1000): string;
27
+ }
28
+ export declare class ImageHelper extends BaseHelper {
29
+ readonly blockMargin: number;
30
+ createBlockHandDiscard(tiles: Tile[]): G;
31
+ createBlockPonChiKan(block: Block): G;
32
+ }
33
+ export declare const createHand: (helper: ImageHelper, blocks: Block[]) => {
34
+ e: G;
35
+ width: number;
36
+ height: number;
37
+ };
38
+ export declare const drawBlocks: (svg: Svg, blocks: Block[], config?: ImageHelperConfig) => void;
39
+ export {};
@@ -0,0 +1,303 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.drawBlocks = exports.createHand = exports.ImageHelper = void 0;
22
+ var assert_1 = __importDefault(require("assert"));
23
+ var parser_1 = require("./parser");
24
+ var svg_js_1 = require("@svgdotjs/svg.js");
25
+ var constants_1 = require("./constants");
26
+ var BaseHelper = (function () {
27
+ function BaseHelper(props) {
28
+ if (props === void 0) { props = {}; }
29
+ var _a, _b, _c, _d;
30
+ this.scale = (_a = props.scale) !== null && _a !== void 0 ? _a : 1;
31
+ this.image_host_path = (_b = props.imageHostPath) !== null && _b !== void 0 ? _b : "";
32
+ this.image_host_url = (_c = props.imageHostUrl) !== null && _c !== void 0 ? _c : "";
33
+ this.tileWidth = constants_1.TILE_CONTEXT.WIDTH * this.scale;
34
+ this.tileHeight = constants_1.TILE_CONTEXT.HEIGHT * this.scale;
35
+ this.textWidth = this.tileWidth * 0.8;
36
+ this.diffTileHeightWidth = (this.tileHeight - this.tileWidth) / 2;
37
+ this.svgSprite = (_d = props.svgSprite) !== null && _d !== void 0 ? _d : false;
38
+ }
39
+ BaseHelper.prototype.image = function (tile) {
40
+ var img = new svg_js_1.Image().load(this.buildURL(tile));
41
+ if (this.svgSprite) {
42
+ img = new svg_js_1.Use().use(this.buildID(tile));
43
+ }
44
+ if (tile instanceof parser_1.Tile && tile.op == constants_1.OPERATOR.COLOR_GRAYSCALE) {
45
+ img.css({ filter: "contrast(65%)" });
46
+ }
47
+ return img;
48
+ };
49
+ BaseHelper.prototype.createImage = function (tile, x, y) {
50
+ var image = this.image(tile)
51
+ .dx(x)
52
+ .dy(y)
53
+ .size(this.tileWidth, this.tileHeight);
54
+ return image;
55
+ };
56
+ BaseHelper.prototype.createTextImage = function (tile, x, y, t) {
57
+ var image = this.createImage(tile, x, y);
58
+ var fontSize = this.tileHeight * 0.2;
59
+ var textX = this.tileWidth;
60
+ var textY = this.tileHeight;
61
+ var text = new svg_js_1.Text().text(t);
62
+ text
63
+ .width(this.tileWidth)
64
+ .height(this.tileHeight)
65
+ .font({
66
+ family: constants_1.FONT_FAMILY,
67
+ size: fontSize,
68
+ })
69
+ .dx(textX)
70
+ .dy(textY);
71
+ var g = new svg_js_1.G();
72
+ g.add(image).add(text).translate(x, y);
73
+ return g;
74
+ };
75
+ BaseHelper.prototype.createStackImage = function (tile, x, y) {
76
+ var base = this.createRotate90Image(tile, 0, 0, true);
77
+ var up = this.createRotate90Image(tile, 0, this.tileWidth, true);
78
+ var g = new svg_js_1.G().translate(x, y).add(base).add(up);
79
+ return g;
80
+ };
81
+ BaseHelper.prototype.createRotate90Image = function (tile, x, y, adjustY) {
82
+ if (adjustY === void 0) { adjustY = false; }
83
+ var image = this.createImage(tile, 0, 0);
84
+ var centerX = this.tileWidth / 2;
85
+ var centerY = this.tileHeight / 2;
86
+ var translatedX = x + this.diffTileHeightWidth;
87
+ var translatedY = adjustY ? y - this.diffTileHeightWidth : y;
88
+ var g = new svg_js_1.G();
89
+ g.add(image)
90
+ .translate(translatedX, translatedY)
91
+ .rotate(90, centerX, centerY);
92
+ return g;
93
+ };
94
+ BaseHelper.prototype.createStick = function (v) {
95
+ return this.image(v);
96
+ };
97
+ BaseHelper.prototype.buildID = function (tile) {
98
+ if (tile == 100 || tile == 1000) {
99
+ return tile == 100 ? "stick100" : "stick1000";
100
+ }
101
+ return "".concat(tile.k).concat(tile.n);
102
+ };
103
+ BaseHelper.prototype.buildURL = function (tile) {
104
+ var filename = "".concat(this.buildID(tile), ".svg");
105
+ if (this.image_host_url != "") {
106
+ return "".concat(this.image_host_url).concat(filename);
107
+ }
108
+ return "".concat(this.image_host_path).concat(filename);
109
+ };
110
+ return BaseHelper;
111
+ }());
112
+ var ImageHelper = (function (_super) {
113
+ __extends(ImageHelper, _super);
114
+ function ImageHelper() {
115
+ var _this = _super !== null && _super.apply(this, arguments) || this;
116
+ _this.blockMargin = _this.tileWidth * 0.3;
117
+ return _this;
118
+ }
119
+ ImageHelper.prototype.createBlockHandDiscard = function (tiles) {
120
+ var g = new svg_js_1.G();
121
+ var pos = 0;
122
+ for (var _i = 0, tiles_1 = tiles; _i < tiles_1.length; _i++) {
123
+ var t = tiles_1[_i];
124
+ if (t.op == constants_1.OPERATOR.HORIZONTAL) {
125
+ var img_1 = this.createRotate90Image(t, pos, this.diffTileHeightWidth);
126
+ g.add(img_1);
127
+ pos += this.tileHeight;
128
+ continue;
129
+ }
130
+ var img = this.createImage(t, pos, 0);
131
+ g.add(img);
132
+ pos += this.tileWidth;
133
+ }
134
+ return g;
135
+ };
136
+ ImageHelper.prototype.createBlockPonChiKan = function (block) {
137
+ var idx = block.tiles.findIndex(function (d) { return d.op === constants_1.OPERATOR.HORIZONTAL; });
138
+ var pos = 0;
139
+ var g = new svg_js_1.G();
140
+ if (block.type == constants_1.BLOCK.SHO_KAN) {
141
+ var diff = this.tileWidth * 2 - this.tileHeight;
142
+ for (var i = 0; i < block.tiles.length; i++) {
143
+ if (i == idx + 1)
144
+ continue;
145
+ if (i == idx) {
146
+ var img_2 = this.createStackImage(block.tiles[idx], pos, 0);
147
+ pos += this.tileHeight;
148
+ g.add(img_2);
149
+ continue;
150
+ }
151
+ var img = this.createImage(block.tiles[i], pos, diff);
152
+ pos += this.tileWidth;
153
+ g.add(img);
154
+ }
155
+ return g;
156
+ }
157
+ if (block.type == constants_1.BLOCK.CHI) {
158
+ var img = this.createRotate90Image(block.tiles[idx], pos, this.diffTileHeightWidth);
159
+ pos += this.tileHeight;
160
+ g.add(img);
161
+ for (var i = 0; i < block.tiles.length; i++) {
162
+ if (i == idx)
163
+ continue;
164
+ var img_3 = this.createImage(block.tiles[i], pos, 0);
165
+ pos += this.tileWidth;
166
+ g.add(img_3);
167
+ }
168
+ return g;
169
+ }
170
+ for (var i = 0; i < block.tiles.length; i++) {
171
+ if (i == idx) {
172
+ var img_4 = this.createRotate90Image(block.tiles[i], pos, this.diffTileHeightWidth);
173
+ pos += this.tileHeight;
174
+ g.add(img_4);
175
+ continue;
176
+ }
177
+ var img = this.createImage(block.tiles[1], pos, 0);
178
+ pos += this.tileWidth;
179
+ g.add(img);
180
+ }
181
+ return g;
182
+ };
183
+ return ImageHelper;
184
+ }(BaseHelper));
185
+ exports.ImageHelper = ImageHelper;
186
+ var getBlockCreators = function (h) {
187
+ var _a;
188
+ var lookup = (_a = {},
189
+ _a[constants_1.BLOCK.CHI] = function (block) {
190
+ var width = h.tileWidth * 2 + h.tileHeight;
191
+ var height = h.tileHeight;
192
+ var g = h.createBlockPonChiKan(block);
193
+ return { width: width, height: height, e: g };
194
+ },
195
+ _a[constants_1.BLOCK.PON] = function (block) {
196
+ var width = h.tileWidth * 2 + h.tileHeight;
197
+ var height = h.tileHeight;
198
+ var g = h.createBlockPonChiKan(block);
199
+ return { width: width, height: height, e: g };
200
+ },
201
+ _a[constants_1.BLOCK.DAI_KAN] = function (block) {
202
+ var width = h.tileWidth * 3 + h.tileHeight;
203
+ var height = h.tileHeight;
204
+ var g = h.createBlockPonChiKan(block);
205
+ return { width: width, height: height, e: g };
206
+ },
207
+ _a[constants_1.BLOCK.SHO_KAN] = function (block) {
208
+ var width = h.tileWidth * 2 + h.tileHeight;
209
+ var height = h.tileWidth * 2;
210
+ var g = h.createBlockPonChiKan(block);
211
+ return { width: width, height: height, e: g };
212
+ },
213
+ _a[constants_1.BLOCK.AN_KAN] = function (block) {
214
+ var width = h.tileWidth * 4;
215
+ var height = h.tileHeight;
216
+ var zp = block.tiles.filter(function (v) {
217
+ return v.k !== constants_1.KIND.BACK;
218
+ });
219
+ (0, assert_1.default)(zp != null && zp.length == 2);
220
+ var g = h.createBlockHandDiscard([
221
+ new parser_1.Tile(constants_1.KIND.BACK, 0),
222
+ zp[0],
223
+ zp[1],
224
+ new parser_1.Tile(constants_1.KIND.BACK, 0),
225
+ ]);
226
+ return { width: width, height: height, e: g };
227
+ },
228
+ _a[constants_1.BLOCK.DORA] = function (block) {
229
+ var width = h.tileWidth + h.textWidth;
230
+ var height = h.tileHeight;
231
+ var g = new svg_js_1.G();
232
+ var img = h.createTextImage(block.tiles[0], 0, 0, "(ドラ)");
233
+ g.add(img);
234
+ return { width: width, height: height, e: g };
235
+ },
236
+ _a[constants_1.BLOCK.TSUMO] = function (block) {
237
+ var width = h.tileWidth + h.textWidth;
238
+ var height = h.tileHeight;
239
+ var g = new svg_js_1.G();
240
+ var img = h.createTextImage(block.tiles[0], 0, 0, "(ツモ)");
241
+ g.add(img);
242
+ return { width: width, height: height, e: g };
243
+ },
244
+ _a[constants_1.BLOCK.HAND] = function (block) {
245
+ var width = h.tileWidth * block.tiles.length;
246
+ var height = h.tileHeight;
247
+ var g = h.createBlockHandDiscard(block.tiles);
248
+ return { width: width, height: height, e: g };
249
+ },
250
+ _a[constants_1.BLOCK.DISCARD] = function (block) {
251
+ var width = block.tiles
252
+ .map(function (v) { return (v.op == constants_1.OPERATOR.HORIZONTAL ? h.tileHeight : h.tileWidth); })
253
+ .reduce(function (prev, v) { return prev + v; });
254
+ var height = h.tileHeight;
255
+ var g = h.createBlockHandDiscard(block.tiles);
256
+ return { width: width, height: height, e: g };
257
+ },
258
+ _a[constants_1.BLOCK.UNKNOWN] = function (block) {
259
+ var width = 0;
260
+ var height = 0;
261
+ var g = new svg_js_1.G();
262
+ throw new Error("found unknown block");
263
+ },
264
+ _a);
265
+ return lookup;
266
+ };
267
+ var createHand = function (helper, blocks) {
268
+ var creators = getBlockCreators(helper);
269
+ var baseHeight = helper.tileWidth;
270
+ var sumOfWidth = 0;
271
+ var elms = [];
272
+ for (var _i = 0, blocks_1 = blocks; _i < blocks_1.length; _i++) {
273
+ var block = blocks_1[_i];
274
+ var fn = creators[block.type];
275
+ var elm = fn(block);
276
+ elms.push(elm);
277
+ sumOfWidth += elm.width;
278
+ if (elm.height > baseHeight)
279
+ baseHeight = elm.height;
280
+ }
281
+ var sizeHeight = baseHeight;
282
+ var sizeWidth = sumOfWidth + (blocks.length - 1) * helper.blockMargin;
283
+ var hand = new svg_js_1.G();
284
+ var pos = 0;
285
+ for (var _a = 0, elms_1 = elms; _a < elms_1.length; _a++) {
286
+ var elm = elms_1[_a];
287
+ var diff = baseHeight - elm.height;
288
+ var g = new svg_js_1.G().translate(pos, diff);
289
+ g.add(elm.e);
290
+ hand.add(g);
291
+ pos += elm.width + helper.blockMargin;
292
+ }
293
+ return { e: hand, width: sizeWidth, height: sizeHeight };
294
+ };
295
+ exports.createHand = createHand;
296
+ var drawBlocks = function (svg, blocks, config) {
297
+ if (config === void 0) { config = {}; }
298
+ var helper = new ImageHelper(config);
299
+ var hand = (0, exports.createHand)(helper, blocks);
300
+ svg.size(hand.width, hand.height);
301
+ svg.add(hand.e);
302
+ };
303
+ exports.drawBlocks = drawBlocks;
@@ -0,0 +1,13 @@
1
+ export declare class Lexer {
2
+ private input;
3
+ position: number;
4
+ private nextPosition;
5
+ char: string;
6
+ readonly eof = "\0";
7
+ constructor(input: string);
8
+ readChar(): string;
9
+ peekChar(): string;
10
+ prevChar(): string;
11
+ skipWhitespace(): void;
12
+ private isWhitespace;
13
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Lexer = void 0;
4
+ var Lexer = (function () {
5
+ function Lexer(input) {
6
+ this.eof = "\0";
7
+ this.input = input;
8
+ this.position = 0;
9
+ this.nextPosition = 0;
10
+ this.char = this.readChar();
11
+ }
12
+ Lexer.prototype.readChar = function () {
13
+ if (this.nextPosition >= this.input.length) {
14
+ this.char = this.eof;
15
+ return this.char;
16
+ }
17
+ else {
18
+ this.char = this.input[this.nextPosition];
19
+ }
20
+ this.position = this.nextPosition;
21
+ this.nextPosition++;
22
+ return this.char;
23
+ };
24
+ Lexer.prototype.peekChar = function () {
25
+ if (this.nextPosition >= this.input.length)
26
+ return this.eof;
27
+ return this.input[this.nextPosition];
28
+ };
29
+ Lexer.prototype.prevChar = function () {
30
+ if (this.position >= this.input.length)
31
+ return this.eof;
32
+ if (this.position > 0)
33
+ return this.input[this.position - 1];
34
+ return this.eof;
35
+ };
36
+ Lexer.prototype.skipWhitespace = function () {
37
+ while (this.isWhitespace(this.char)) {
38
+ this.readChar();
39
+ }
40
+ };
41
+ Lexer.prototype.isWhitespace = function (char) {
42
+ return /\s/.test(char);
43
+ };
44
+ return Lexer;
45
+ }());
46
+ exports.Lexer = Lexer;
@@ -0,0 +1,11 @@
1
+ import { ImageHelperConfig } from "./image";
2
+ interface InitializeConfig extends Omit<ImageHelperConfig, "scale"> {
3
+ querySelector?: string | string[];
4
+ scale?: number;
5
+ tableScale?: number;
6
+ }
7
+ export declare class mjimage {
8
+ static svgURL: () => string;
9
+ static initialize: (props?: InitializeConfig) => void;
10
+ }
11
+ export {};