@konoui/mjimage 0.0.13 → 0.0.14

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 (51) hide show
  1. package/dist/mjs/lib/constants.d.ts +2 -0
  2. package/dist/mjs/lib/constants.js +6 -1
  3. package/dist/mjs/lib/image.d.ts +5 -5
  4. package/dist/mjs/lib/image.js +109 -111
  5. package/dist/mjs/lib/mjimage.d.ts +1 -0
  6. package/dist/mjs/lib/mjimage.js +6 -4
  7. package/dist/mjs/lib/parser.d.ts +10 -1
  8. package/dist/mjs/lib/parser.js +28 -17
  9. package/dist/mjs/lib/table.d.ts +3 -1
  10. package/dist/mjs/lib/table.js +10 -3
  11. package/dist/mjs/svg/_0.svg +1 -1
  12. package/dist/mjs/svg/m0.svg +1 -1
  13. package/dist/mjs/svg/m1.svg +1 -1
  14. package/dist/mjs/svg/m2.svg +1 -1
  15. package/dist/mjs/svg/m3.svg +1 -1
  16. package/dist/mjs/svg/m4.svg +1 -1
  17. package/dist/mjs/svg/m5.svg +1 -1
  18. package/dist/mjs/svg/m6.svg +1 -1
  19. package/dist/mjs/svg/m7.svg +1 -1
  20. package/dist/mjs/svg/m8.svg +1 -1
  21. package/dist/mjs/svg/m9.svg +1 -1
  22. package/dist/mjs/svg/p0.svg +1 -1
  23. package/dist/mjs/svg/p1.svg +1 -1
  24. package/dist/mjs/svg/p2.svg +1 -1
  25. package/dist/mjs/svg/p3.svg +1 -1
  26. package/dist/mjs/svg/p4.svg +1 -1
  27. package/dist/mjs/svg/p5.svg +1 -1
  28. package/dist/mjs/svg/p6.svg +1 -1
  29. package/dist/mjs/svg/p7.svg +1 -1
  30. package/dist/mjs/svg/p8.svg +1 -1
  31. package/dist/mjs/svg/p9.svg +1 -1
  32. package/dist/mjs/svg/s0.svg +1 -1
  33. package/dist/mjs/svg/s1.svg +1 -1
  34. package/dist/mjs/svg/s2.svg +1 -1
  35. package/dist/mjs/svg/s3.svg +1 -1
  36. package/dist/mjs/svg/s4.svg +1 -1
  37. package/dist/mjs/svg/s5.svg +1 -1
  38. package/dist/mjs/svg/s6.svg +1 -1
  39. package/dist/mjs/svg/s7.svg +1 -1
  40. package/dist/mjs/svg/s8.svg +1 -1
  41. package/dist/mjs/svg/s9.svg +1 -1
  42. package/dist/mjs/svg/stick100.svg +1 -1
  43. package/dist/mjs/svg/stick1000.svg +1 -1
  44. package/dist/mjs/svg/z1.svg +1 -1
  45. package/dist/mjs/svg/z2.svg +1 -1
  46. package/dist/mjs/svg/z3.svg +1 -1
  47. package/dist/mjs/svg/z4.svg +1 -1
  48. package/dist/mjs/svg/z5.svg +1 -1
  49. package/dist/mjs/svg/z6.svg +1 -1
  50. package/dist/mjs/svg/z7.svg +1 -1
  51. package/package.json +2 -2
@@ -2,6 +2,8 @@ export declare const FONT_FAMILY = "MS Gothic, sans-serif";
2
2
  export declare const TILE_CONTEXT: {
3
3
  WIDTH: number;
4
4
  HEIGHT: number;
5
+ TEXT_SCALE: number;
6
+ BLOCK_MARGIN_SCALE: number;
5
7
  };
6
8
  export declare const TABLE_CONTEXT: {
7
9
  BASE: number;
@@ -3,7 +3,12 @@ var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.WIND_MAP = exports.ROUND_MAP = exports.WIND = exports.BLOCK = exports.OPERATOR = exports.TYPE = exports.INPUT_SEPARATOR = exports.TABLE_CONTEXT = exports.TILE_CONTEXT = exports.FONT_FAMILY = void 0;
5
5
  exports.FONT_FAMILY = "MS Gothic, sans-serif";
6
- exports.TILE_CONTEXT = { WIDTH: 66, HEIGHT: 90 };
6
+ exports.TILE_CONTEXT = {
7
+ WIDTH: 66,
8
+ HEIGHT: 90,
9
+ TEXT_SCALE: 0.8,
10
+ BLOCK_MARGIN_SCALE: 0.3,
11
+ };
7
12
  exports.TABLE_CONTEXT = { BASE: 40 };
8
13
  exports.INPUT_SEPARATOR = ",";
9
14
  exports.TYPE = {
@@ -9,17 +9,15 @@ export interface ImageHelperConfig {
9
9
  declare class BaseHelper {
10
10
  readonly tileWidth: number;
11
11
  readonly tileHeight: number;
12
- readonly diffTileHeightWidth: number;
13
- readonly textWidth: number;
14
12
  readonly image_host_path: string;
15
13
  readonly image_host_url: string;
16
14
  readonly scale: number;
17
15
  readonly svgSprite: boolean;
18
16
  constructor(props?: ImageHelperConfig);
17
+ protected getDiffTileHeightWidth(t: Tile): number;
19
18
  private image;
20
19
  createImage(tile: Tile, x: number, y: number): Image | Use;
21
20
  createTextImage(tile: Tile, x: number, y: number, t: string): G;
22
- createStackImage(baseTile: Tile, upTile: Tile, x: number, y: number): G;
23
21
  createRotate90Image(tile: Tile, x: number, y: number, adjustY?: boolean): G;
24
22
  createStick(v: 100 | 1000): Image | Use;
25
23
  static buildID(tile: Tile | 100 | 1000): string;
@@ -27,7 +25,7 @@ declare class BaseHelper {
27
25
  }
28
26
  export declare class ImageHelper extends BaseHelper {
29
27
  readonly blockMargin: number;
30
- createBlockHandDiscard(tiles: Tile[]): G;
28
+ createBlockHandDiscard(block: Block): G;
31
29
  createBlockPonChiKan(block: Block): G;
32
30
  }
33
31
  export declare const createHand: (helper: ImageHelper, blocks: Block[]) => {
@@ -35,6 +33,8 @@ export declare const createHand: (helper: ImageHelper, blocks: Block[]) => {
35
33
  width: number;
36
34
  height: number;
37
35
  };
38
- export declare const drawBlocks: (svg: Svg, blocks: Block[], config?: ImageHelperConfig) => void;
36
+ export declare const drawBlocks: (svg: Svg, blocks: Block[], config?: ImageHelperConfig, params?: {
37
+ responsive: boolean;
38
+ }) => void;
39
39
  export declare const optimizeSVG: (draw: Svg) => void;
40
40
  export {};
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
17
28
  var __importDefault = (this && this.__importDefault) || function (mod) {
18
29
  return (mod && mod.__esModule) ? mod : { "default": mod };
19
30
  };
@@ -32,10 +43,12 @@ var BaseHelper = (function () {
32
43
  this.image_host_url = (_c = props.imageHostUrl) !== null && _c !== void 0 ? _c : "";
33
44
  this.tileWidth = constants_1.TILE_CONTEXT.WIDTH * this.scale;
34
45
  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
46
  this.svgSprite = (_d = props.svgSprite) !== null && _d !== void 0 ? _d : false;
38
47
  }
48
+ BaseHelper.prototype.getDiffTileHeightWidth = function (t) {
49
+ var size = t.imageSize(this.scale);
50
+ return (size.baseHeight - size.baseWidth) / 2;
51
+ };
39
52
  BaseHelper.prototype.image = function (tile) {
40
53
  var img = new svg_js_1.Image().load(this.buildURL(tile));
41
54
  if (this.svgSprite) {
@@ -47,21 +60,22 @@ var BaseHelper = (function () {
47
60
  return img;
48
61
  };
49
62
  BaseHelper.prototype.createImage = function (tile, x, y) {
63
+ var size = tile.imageSize(this.scale);
50
64
  var image = this.image(tile)
51
65
  .dx(x)
52
66
  .dy(y)
53
- .size(this.tileWidth, this.tileHeight);
67
+ .size(size.baseWidth, size.baseHeight);
54
68
  return image;
55
69
  };
56
70
  BaseHelper.prototype.createTextImage = function (tile, x, y, t) {
57
71
  var image = this.createImage(tile, x, y);
58
- var fontSize = this.tileHeight * 0.2;
59
- var textX = this.tileWidth;
60
- var textY = this.tileHeight;
72
+ var size = tile.imageSize(this.scale);
73
+ var fontSize = size.baseHeight * 0.2;
74
+ var textX = size.baseWidth;
75
+ var textY = size.baseHeight;
61
76
  var text = new svg_js_1.Text().text(t);
62
77
  text
63
- .width(this.tileWidth)
64
- .height(this.tileHeight)
78
+ .size(size.baseWidth, size.baseHeight)
65
79
  .font({
66
80
  family: constants_1.FONT_FAMILY,
67
81
  size: fontSize,
@@ -72,19 +86,14 @@ var BaseHelper = (function () {
72
86
  g.add(image).add(text).translate(x, y);
73
87
  return g;
74
88
  };
75
- BaseHelper.prototype.createStackImage = function (baseTile, upTile, x, y) {
76
- var base = this.createRotate90Image(baseTile, 0, 0, true);
77
- var up = this.createRotate90Image(upTile, 0, this.tileWidth, true);
78
- var g = new svg_js_1.G().translate(x, y).add(base).add(up);
79
- return g;
80
- };
81
89
  BaseHelper.prototype.createRotate90Image = function (tile, x, y, adjustY) {
82
90
  if (adjustY === void 0) { adjustY = false; }
83
91
  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;
92
+ var size = tile.imageSize(this.scale);
93
+ var centerX = size.baseWidth / 2;
94
+ var centerY = size.baseHeight / 2;
95
+ var translatedX = x + this.getDiffTileHeightWidth(tile);
96
+ var translatedY = adjustY ? y - this.getDiffTileHeightWidth(tile) : y;
88
97
  var g = new svg_js_1.G();
89
98
  g.add(image)
90
99
  .translate(translatedX, translatedY)
@@ -114,23 +123,22 @@ var ImageHelper = (function (_super) {
114
123
  __extends(ImageHelper, _super);
115
124
  function ImageHelper() {
116
125
  var _this = _super !== null && _super.apply(this, arguments) || this;
117
- _this.blockMargin = _this.tileWidth * 0.3;
126
+ _this.blockMargin = constants_1.TILE_CONTEXT.WIDTH * constants_1.TILE_CONTEXT.BLOCK_MARGIN_SCALE * _this.scale;
118
127
  return _this;
119
128
  }
120
- ImageHelper.prototype.createBlockHandDiscard = function (tiles) {
129
+ ImageHelper.prototype.createBlockHandDiscard = function (block) {
121
130
  var g = new svg_js_1.G();
122
131
  var pos = 0;
123
- for (var _i = 0, tiles_1 = tiles; _i < tiles_1.length; _i++) {
124
- var t = tiles_1[_i];
125
- if (t.has(constants_1.OPERATOR.HORIZONTAL)) {
126
- var img_1 = this.createRotate90Image(t, pos, this.diffTileHeightWidth);
127
- g.add(img_1);
128
- pos += this.tileHeight;
129
- continue;
130
- }
131
- var img = this.createImage(t, pos, 0);
132
+ for (var _i = 0, _a = block.tiles; _i < _a.length; _i++) {
133
+ var t = _a[_i];
134
+ var size = t.imageSize(this.scale);
135
+ var f = t.has(constants_1.OPERATOR.HORIZONTAL)
136
+ ? this.createRotate90Image.bind(this)
137
+ : this.createImage.bind(this);
138
+ var y = t.has(constants_1.OPERATOR.HORIZONTAL) ? this.getDiffTileHeightWidth(t) : 0;
139
+ var img = f(t, pos, y);
132
140
  g.add(img);
133
- pos += this.tileWidth;
141
+ pos += size.width;
134
142
  }
135
143
  return g;
136
144
  };
@@ -139,52 +147,53 @@ var ImageHelper = (function (_super) {
139
147
  var pos = 0;
140
148
  var g = new svg_js_1.G();
141
149
  if (block.type == constants_1.BLOCK.SHO_KAN) {
142
- var diff = this.tileWidth * 2 - this.tileHeight;
143
150
  var lastIdx = idx;
144
- var i = 0;
145
- for (var _i = 0, _a = block.tiles; _i < _a.length; _i++) {
146
- var t = _a[_i];
147
- if (t.has(constants_1.OPERATOR.HORIZONTAL))
151
+ for (var i = 0; i < block.tiles.length; i++)
152
+ if (block.tiles[i].has(constants_1.OPERATOR.HORIZONTAL))
148
153
  lastIdx = i;
149
- i++;
150
- }
151
- for (var i_1 = 0; i_1 < block.tiles.length; i_1++) {
152
- if (i_1 == lastIdx)
154
+ for (var i = 0; i < block.tiles.length; i++) {
155
+ var size = block.tiles[i].imageSize(this.scale);
156
+ if (i == lastIdx)
153
157
  continue;
154
- if (i_1 == idx) {
155
- var img_2 = this.createStackImage(block.tiles[idx], block.tiles[lastIdx], pos, 0);
156
- pos += this.tileHeight;
157
- g.add(img_2);
158
+ if (i == idx) {
159
+ var baseTile = block.tiles[idx];
160
+ var upperTile = block.tiles[lastIdx];
161
+ var size_1 = baseTile.imageSize(this.scale);
162
+ var baseImg = this.createRotate90Image(baseTile, 0, 0, true);
163
+ var upImg = this.createRotate90Image(upperTile, 0, size_1.height, true);
164
+ g.add(new svg_js_1.G().translate(pos, 0).add(baseImg).add(upImg));
165
+ pos += size_1.width;
158
166
  continue;
159
167
  }
160
- var img = this.createImage(block.tiles[i_1], pos, diff);
161
- pos += this.tileWidth;
168
+ var diff = size.width * 2 - size.height;
169
+ var img = this.createImage(block.tiles[i], pos, diff);
170
+ pos += size.width;
162
171
  g.add(img);
163
172
  }
164
173
  return g;
165
174
  }
166
175
  if (block.type == constants_1.BLOCK.CHI) {
167
- var img = this.createRotate90Image(block.tiles[idx], pos, this.diffTileHeightWidth);
168
- pos += this.tileHeight;
176
+ var img = this.createRotate90Image(block.tiles[idx], pos, this.getDiffTileHeightWidth(block.tiles[idx]));
177
+ pos += block.tiles[idx].imageSize(this.scale).width;
169
178
  g.add(img);
170
179
  for (var i = 0; i < block.tiles.length; i++) {
171
180
  if (i == idx)
172
181
  continue;
173
- var img_3 = this.createImage(block.tiles[i], pos, 0);
174
- pos += this.tileWidth;
175
- g.add(img_3);
182
+ var size = block.tiles[i].imageSize(this.scale);
183
+ var img_1 = this.createImage(block.tiles[i], pos, 0);
184
+ pos += size.width;
185
+ g.add(img_1);
176
186
  }
177
187
  return g;
178
188
  }
179
189
  for (var i = 0; i < block.tiles.length; i++) {
180
- if (i == idx) {
181
- var img_4 = this.createRotate90Image(block.tiles[i], pos, this.diffTileHeightWidth);
182
- pos += this.tileHeight;
183
- g.add(img_4);
184
- continue;
185
- }
186
- var img = this.createImage(block.tiles[1], pos, 0);
187
- pos += this.tileWidth;
190
+ var f = i == idx
191
+ ? this.createRotate90Image.bind(this)
192
+ : this.createImage.bind(this);
193
+ var y = i == idx ? this.getDiffTileHeightWidth(block.tiles[i]) : 0;
194
+ var size = block.tiles[i].imageSize(this.scale);
195
+ var img = f(block.tiles[i], pos, y);
196
+ pos += size.width;
188
197
  g.add(img);
189
198
  }
190
199
  return g;
@@ -194,80 +203,62 @@ var ImageHelper = (function (_super) {
194
203
  exports.ImageHelper = ImageHelper;
195
204
  var getBlockCreators = function (h) {
196
205
  var _a;
206
+ var scale = h.scale;
197
207
  var lookup = (_a = {},
198
208
  _a[constants_1.BLOCK.CHI] = function (block) {
199
- var width = h.tileWidth * 2 + h.tileHeight;
200
- var height = h.tileHeight;
209
+ var size = block.imageSize(scale);
201
210
  var g = h.createBlockPonChiKan(block);
202
- return { width: width, height: height, e: g };
211
+ return __assign(__assign({}, size), { e: g });
203
212
  },
204
213
  _a[constants_1.BLOCK.PON] = function (block) {
205
- var width = h.tileWidth * 2 + h.tileHeight;
206
- var height = h.tileHeight;
214
+ var size = block.imageSize(scale);
207
215
  var g = h.createBlockPonChiKan(block);
208
- return { width: width, height: height, e: g };
216
+ return __assign(__assign({}, size), { e: g });
209
217
  },
210
218
  _a[constants_1.BLOCK.DAI_KAN] = function (block) {
211
- var width = h.tileWidth * 3 + h.tileHeight;
212
- var height = h.tileHeight;
219
+ var size = block.imageSize(scale);
213
220
  var g = h.createBlockPonChiKan(block);
214
- return { width: width, height: height, e: g };
221
+ return __assign(__assign({}, size), { e: g });
215
222
  },
216
223
  _a[constants_1.BLOCK.SHO_KAN] = function (block) {
217
- var width = h.tileWidth * 2 + h.tileHeight;
218
- var height = h.tileWidth * 2;
224
+ var size = block.imageSize(scale);
219
225
  var g = h.createBlockPonChiKan(block);
220
- return { width: width, height: height, e: g };
226
+ return __assign(__assign({}, size), { e: g });
221
227
  },
222
228
  _a[constants_1.BLOCK.AN_KAN] = function (block) {
223
- var width = h.tileWidth * 4;
224
- var height = h.tileHeight;
229
+ var size = block.imageSize(scale);
225
230
  var zp = block.tiles.filter(function (v) {
226
231
  return v.t !== constants_1.TYPE.BACK;
227
232
  });
228
233
  (0, assert_1.default)(zp != null && zp.length == 2);
229
- var g = h.createBlockHandDiscard([
230
- new parser_1.Tile(constants_1.TYPE.BACK, 0),
231
- zp[0],
232
- zp[1],
233
- new parser_1.Tile(constants_1.TYPE.BACK, 0),
234
- ]);
235
- return { width: width, height: height, e: g };
234
+ var g = h.createBlockHandDiscard(new parser_1.BlockAnKan([zp[0], zp[1], zp[0], zp[1]]));
235
+ return __assign(__assign({}, size), { e: g });
236
236
  },
237
237
  _a[constants_1.BLOCK.DORA] = function (block) {
238
- var width = h.tileWidth + h.textWidth;
239
- var height = h.tileHeight;
238
+ var size = block.imageSize(scale);
240
239
  var g = new svg_js_1.G();
241
240
  var img = h.createTextImage(block.tiles[0], 0, 0, "(ドラ)");
242
241
  g.add(img);
243
- return { width: width, height: height, e: g };
242
+ return __assign(__assign({}, size), { e: g });
244
243
  },
245
244
  _a[constants_1.BLOCK.TSUMO] = function (block) {
246
- var width = h.tileWidth + h.textWidth;
247
- var height = h.tileHeight;
245
+ var size = block.imageSize(scale);
248
246
  var g = new svg_js_1.G();
249
247
  var img = h.createTextImage(block.tiles[0], 0, 0, "(ツモ)");
250
248
  g.add(img);
251
- return { width: width, height: height, e: g };
249
+ return __assign(__assign({}, size), { e: g });
252
250
  },
253
251
  _a[constants_1.BLOCK.HAND] = function (block) {
254
- var width = h.tileWidth * block.tiles.length;
255
- var height = h.tileHeight;
256
- var g = h.createBlockHandDiscard(block.tiles);
257
- return { width: width, height: height, e: g };
252
+ var size = block.imageSize(scale);
253
+ var g = h.createBlockHandDiscard(block);
254
+ return __assign(__assign({}, size), { e: g });
258
255
  },
259
256
  _a[constants_1.BLOCK.DISCARD] = function (block) {
260
- var width = block.tiles
261
- .map(function (v) { return (v.has(constants_1.OPERATOR.HORIZONTAL) ? h.tileHeight : h.tileWidth); })
262
- .reduce(function (prev, v) { return prev + v; });
263
- var height = h.tileHeight;
264
- var g = h.createBlockHandDiscard(block.tiles);
265
- return { width: width, height: height, e: g };
257
+ var size = block.imageSize(scale);
258
+ var g = h.createBlockHandDiscard(block);
259
+ return __assign(__assign({}, size), { e: g });
266
260
  },
267
261
  _a[constants_1.BLOCK.UNKNOWN] = function (block) {
268
- var width = 0;
269
- var height = 0;
270
- var g = new svg_js_1.G();
271
262
  throw new Error("found unknown block");
272
263
  },
273
264
  _a[constants_1.BLOCK.PAIR] = function (block) {
@@ -283,39 +274,46 @@ var getBlockCreators = function (h) {
283
274
  return lookup;
284
275
  };
285
276
  var createHand = function (helper, blocks) {
277
+ var _a = blocks.reduce(function (acc, b) {
278
+ var size = b.imageSize(helper.scale);
279
+ var v = size.height > acc.maxHeight ? size.height : acc.maxHeight;
280
+ return { maxHeight: v, sumWidth: acc.sumWidth + size.width };
281
+ }, { maxHeight: 0, sumWidth: 0 }), maxHeight = _a.maxHeight, sumWidth = _a.sumWidth;
282
+ var viewBoxHeight = maxHeight;
283
+ var viewBoxWidth = sumWidth + (blocks.length - 1) * helper.blockMargin;
284
+ var params = {
285
+ viewBoxWidth: viewBoxWidth,
286
+ viewBoxHeight: viewBoxHeight,
287
+ };
286
288
  var creators = getBlockCreators(helper);
287
- var baseHeight = helper.tileWidth;
288
- var sumOfWidth = 0;
289
289
  var elms = [];
290
290
  for (var _i = 0, blocks_1 = blocks; _i < blocks_1.length; _i++) {
291
291
  var block = blocks_1[_i];
292
292
  var fn = creators[block.type];
293
293
  var elm = fn(block);
294
294
  elms.push(elm);
295
- sumOfWidth += elm.width;
296
- if (elm.height > baseHeight)
297
- baseHeight = elm.height;
298
295
  }
299
- var sizeHeight = baseHeight;
300
- var sizeWidth = sumOfWidth + (blocks.length - 1) * helper.blockMargin;
301
296
  var hand = new svg_js_1.G();
302
297
  var pos = 0;
303
- for (var _a = 0, elms_1 = elms; _a < elms_1.length; _a++) {
304
- var elm = elms_1[_a];
305
- var diff = baseHeight - elm.height;
298
+ for (var _b = 0, elms_1 = elms; _b < elms_1.length; _b++) {
299
+ var elm = elms_1[_b];
300
+ var diff = viewBoxHeight - elm.height;
306
301
  var g = new svg_js_1.G().translate(pos, diff);
307
302
  g.add(elm.e);
308
303
  hand.add(g);
309
304
  pos += elm.width + helper.blockMargin;
310
305
  }
311
- return { e: hand, width: sizeWidth, height: sizeHeight };
306
+ return { e: hand, width: viewBoxWidth, height: viewBoxHeight };
312
307
  };
313
308
  exports.createHand = createHand;
314
- var drawBlocks = function (svg, blocks, config) {
309
+ var drawBlocks = function (svg, blocks, config, params) {
315
310
  if (config === void 0) { config = {}; }
311
+ if (params === void 0) { params = { responsive: false }; }
316
312
  var helper = new ImageHelper(config);
317
313
  var hand = (0, exports.createHand)(helper, blocks);
318
- svg.size(hand.width, hand.height);
314
+ if (!params.responsive)
315
+ svg.size(hand.width, hand.height);
316
+ svg.viewbox(0, 0, hand.width, hand.height);
319
317
  svg.add(hand.e);
320
318
  };
321
319
  exports.drawBlocks = drawBlocks;
@@ -3,6 +3,7 @@ interface InitializeConfig extends Omit<ImageHelperConfig, "scale"> {
3
3
  querySelector?: string | string[];
4
4
  scale?: number;
5
5
  tableScale?: number;
6
+ responsive?: boolean;
6
7
  }
7
8
  export declare class mjimage {
8
9
  static initialize: (props?: InitializeConfig) => void;
@@ -20,6 +20,7 @@ var constants_1 = require("./constants");
20
20
  var svg_js_1 = require("@svgdotjs/svg.js");
21
21
  var defaultQuerySelector = ".mjimage";
22
22
  var defaultScale = 1.6;
23
+ var defaultResponsive = false;
23
24
  var defaultSvgSprite = false;
24
25
  var tableRegex = /^\s*table/;
25
26
  var minPaiHeight = Math.min(constants_1.TILE_CONTEXT.WIDTH, constants_1.TILE_CONTEXT.HEIGHT);
@@ -30,13 +31,14 @@ var mjimage = (function () {
30
31
  function mjimage() {
31
32
  }
32
33
  mjimage.initialize = function (props) {
33
- var _a, _b, _c, _d;
34
+ var _a, _b, _c, _d, _e;
34
35
  if (props === void 0) { props = {}; }
35
36
  console.debug("initializing....");
36
37
  var querySelector = (_a = props.querySelector) !== null && _a !== void 0 ? _a : defaultQuerySelector;
37
38
  var handScale = (_b = props.scale) !== null && _b !== void 0 ? _b : defaultScale;
38
39
  var tableScale = (_c = props.tableScale) !== null && _c !== void 0 ? _c : handScale;
39
- var svgSprite = (_d = props.svgSprite) !== null && _d !== void 0 ? _d : defaultSvgSprite;
40
+ var responsive = (_d = props.responsive) !== null && _d !== void 0 ? _d : defaultResponsive;
41
+ var svgSprite = (_e = props.svgSprite) !== null && _e !== void 0 ? _e : defaultSvgSprite;
40
42
  if (typeof querySelector === "string")
41
43
  querySelector = [querySelector];
42
44
  var mtext = new measure_text_1.MeasureText();
@@ -60,12 +62,12 @@ var mjimage = (function () {
60
62
  if (tableRegex.test(input)) {
61
63
  var scale = calculateScale(tableScale, textHeight);
62
64
  var fontCtx = mtext.measureTableFontContext(scale);
63
- (0, table_1.drawTable)(svg, input, __assign(__assign({}, props), { svgSprite: svgSprite, scale: scale }), fontCtx);
65
+ (0, table_1.drawTable)(svg, input, __assign(__assign({}, props), { svgSprite: svgSprite, scale: scale }), fontCtx, { responsive: responsive });
64
66
  }
65
67
  else {
66
68
  var scale = calculateScale(handScale, textHeight);
67
69
  var blocks = new parser_1.Parser(input).parse();
68
- (0, image_1.drawBlocks)(svg, blocks, __assign(__assign({}, props), { svgSprite: svgSprite, scale: scale }));
70
+ (0, image_1.drawBlocks)(svg, blocks, __assign(__assign({}, props), { svgSprite: svgSprite, scale: scale }), { responsive: responsive });
69
71
  }
70
72
  svg.addTo(target);
71
73
  }
@@ -16,6 +16,12 @@ export declare class Tile {
16
16
  remove(op: Operator): Tile;
17
17
  isNum(): boolean;
18
18
  equals(t: Tile, ignoreRed?: boolean): boolean;
19
+ imageSize(scale: number): {
20
+ width: number;
21
+ height: number;
22
+ baseWidth: number;
23
+ baseHeight: number;
24
+ };
19
25
  }
20
26
  type BLOCK = (typeof BLOCK)[keyof typeof BLOCK];
21
27
  export declare class Block {
@@ -25,9 +31,12 @@ export declare class Block {
25
31
  toString(): string;
26
32
  is(type: BLOCK): boolean;
27
33
  isCalled(): boolean;
28
- equals(b: Block): boolean;
29
34
  minTile(): Tile;
30
35
  clone(): Block | BlockChi | BlockPon | BlockAnKan | BlockDaiKan | BlockShoKan | BlockPair | BlockSet | BlockIsolated;
36
+ imageSize(scale: number): {
37
+ width: number;
38
+ height: number;
39
+ };
31
40
  }
32
41
  export declare class BlockChi extends Block {
33
42
  constructor(tiles: [Tile, Tile, Tile]);
@@ -97,6 +97,16 @@ var Tile = (function () {
97
97
  ok || (ok = (this.n == 5 && t.n == 0) || (this.n == 0 && t.n == 5));
98
98
  return this.t == t.t && ok;
99
99
  };
100
+ Tile.prototype.imageSize = function (scale) {
101
+ var h = parseFloat((constants_1.TILE_CONTEXT.HEIGHT * scale).toPrecision(5));
102
+ var w = parseFloat((constants_1.TILE_CONTEXT.WIDTH * scale).toPrecision(5));
103
+ var size = this.has(constants_1.OPERATOR.HORIZONTAL)
104
+ ? { width: h, height: w, baseWidth: w, baseHeight: h }
105
+ : { width: w, height: h, w: w, baseWidth: w, baseHeight: h };
106
+ if (this.has(constants_1.OPERATOR.TSUMO) || this.has(constants_1.OPERATOR.DORA))
107
+ size.width += w * constants_1.TILE_CONTEXT.TEXT_SCALE;
108
+ return size;
109
+ };
100
110
  return Tile;
101
111
  }());
102
112
  exports.Tile = Tile;
@@ -152,21 +162,6 @@ var Block = (function () {
152
162
  constants_1.BLOCK.SHO_KAN.toString(),
153
163
  ].includes(this.type.toString());
154
164
  };
155
- Block.prototype.equals = function (b) {
156
- if (this.tiles.length != b.tiles.length)
157
- return false;
158
- var ab = this.tiles;
159
- var bb = b.tiles;
160
- if (this.is(constants_1.BLOCK.CHI) || b.is(constants_1.BLOCK.CHI)) {
161
- ab = b.clone().tiles.sort(exports.tileSortFunc);
162
- bb = this.clone().tiles.sort(exports.tileSortFunc);
163
- }
164
- for (var i = 0; i < b.tiles.length; i++) {
165
- if (!ab[i].equals(bb[i], true))
166
- return false;
167
- }
168
- return true;
169
- };
170
165
  Block.prototype.minTile = function () {
171
166
  if (this.is(constants_1.BLOCK.CHI))
172
167
  return this.clone().tiles.sort(exports.tileSortFunc)[0];
@@ -177,6 +172,20 @@ var Block = (function () {
177
172
  var tiles = this.tiles.map(function (t) { return new Tile(t.t, t.n, __spreadArray([], t.ops, true)); });
178
173
  return (0, exports.blockWrapper)(tiles, this.type);
179
174
  };
175
+ Block.prototype.imageSize = function (scale) {
176
+ var bh = this.tiles[0].imageSize(scale).baseHeight;
177
+ var bw = this.tiles[0].imageSize(scale).baseWidth;
178
+ if (this.is(constants_1.BLOCK.SHO_KAN))
179
+ return { width: bw * 2 + bh, height: Math.max(bw * 2, bh) };
180
+ var maxHeight = this.tiles.reduce(function (max, t) {
181
+ var h = t.imageSize(scale).height;
182
+ return h > max ? h : max;
183
+ }, 0);
184
+ var sumWidth = this.tiles.reduce(function (sum, t) {
185
+ return sum + t.imageSize(scale).width;
186
+ }, 0);
187
+ return { width: sumWidth, height: maxHeight };
188
+ };
180
189
  return Block;
181
190
  }());
182
191
  exports.Block = Block;
@@ -203,8 +212,10 @@ var BlockAnKan = (function (_super) {
203
212
  }
204
213
  BlockAnKan.prototype.toString = function () {
205
214
  var tiles = this.tiles.map(function (t) { return t.clone(); });
206
- tiles[1] = new Tile(constants_1.TYPE.BACK, 0);
207
- tiles[2] = new Tile(constants_1.TYPE.BACK, 0);
215
+ if (!tiles.some(function (v) { return v.t == constants_1.TYPE.BACK; })) {
216
+ tiles[0] = new Tile(constants_1.TYPE.BACK, 0);
217
+ tiles[3] = new Tile(constants_1.TYPE.BACK, 0);
218
+ }
208
219
  return tiles.reduce(function (s, t) {
209
220
  return "".concat(s).concat(t.toString());
210
221
  }, "");
@@ -7,4 +7,6 @@ export declare const createTable: (helper: ImageHelper, fontCtx: FontContext, ha
7
7
  width: number;
8
8
  height: number;
9
9
  };
10
- export declare const drawTable: (svg: Svg, tableInput: string, config: ImageHelperConfig | undefined, fontCtx: FontContext) => void;
10
+ export declare const drawTable: (svg: Svg, tableInput: string, config: ImageHelperConfig | undefined, fontCtx: FontContext, params?: {
11
+ responsive: boolean;
12
+ }) => void;
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.drawTable = exports.createTable = void 0;
4
+ var parser_1 = require("./parser");
4
5
  var image_1 = require("./image");
5
6
  var svg_js_1 = require("@svgdotjs/svg.js");
6
7
  var table_parser_1 = require("./table-parser");
8
+ var constants_1 = require("./constants");
7
9
  var splitTiles = function (input) {
8
10
  var chunkSize = 6;
9
11
  var result = [];
@@ -43,7 +45,9 @@ var handleDiscard = function (tiles, helper) {
43
45
  for (var i = 0; i < chunks.length; i++) {
44
46
  var tiles_1 = chunks[i];
45
47
  var posY = i * helper.tileHeight;
46
- var e = helper.createBlockHandDiscard(tiles_1).translate(0, posY);
48
+ var e = helper
49
+ .createBlockHandDiscard(new parser_1.Block(tiles_1, constants_1.BLOCK.DISCARD))
50
+ .translate(0, posY);
47
51
  g.add(e);
48
52
  }
49
53
  return {
@@ -230,13 +234,16 @@ var createTable = function (helper, fontCtx, handsProps, discardsProps, scoreBoa
230
234
  return { e: g, width: hands.width, height: hands.height };
231
235
  };
232
236
  exports.createTable = createTable;
233
- var drawTable = function (svg, tableInput, config, fontCtx) {
237
+ var drawTable = function (svg, tableInput, config, fontCtx, params) {
234
238
  if (config === void 0) { config = {}; }
239
+ if (params === void 0) { params = { responsive: false }; }
235
240
  var helper = new image_1.ImageHelper(config);
236
241
  var ctx = fontCtx;
237
242
  var _a = (0, table_parser_1.parse)(tableInput), discards = _a.discards, hands = _a.hands, scoreBoard = _a.scoreBoard;
238
243
  var table = (0, exports.createTable)(helper, ctx, hands, discards, scoreBoard);
239
- svg.size(table.width, table.height);
244
+ if (!params.responsive)
245
+ svg.size(table.width, table.height);
246
+ svg.viewbox(0, 0, table.width, table.height);
240
247
  svg.add(table.e);
241
248
  };
242
249
  exports.drawTable = drawTable;
@@ -1 +1 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="66" height="90" baseProfile="tiny" version="1.2"><path fill="#c73" d="M0 0h66v90H0z"/></svg>
1
+ <svg xmlns="http://www.w3.org/2000/svg" baseProfile="tiny" version="1.2" viewBox="0 0 66 90"><path fill="#c73" d="M0 0h66v90H0z"/></svg>