@first-ch/tools-mcp 0.4.0 → 0.24.0
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.
- package/README.md +132 -30
- package/aspect-ratio.mjs +706 -0
- package/base64.mjs +356 -0
- package/color.mjs +629 -0
- package/cron.mjs +633 -0
- package/diff.mjs +322 -0
- package/hash.mjs +234 -0
- package/html-escape.mjs +308 -0
- package/json-yaml.mjs +1693 -0
- package/jwt.mjs +639 -0
- package/lib.mjs +103 -0
- package/markdown-table.mjs +641 -0
- package/marp-theme.css +51 -0
- package/marp.mjs +219 -0
- package/package.json +132 -6
- package/px-rem.mjs +488 -0
- package/qr.mjs +744 -0
- package/server.mjs +942 -2
- package/sql-format.mjs +694 -0
- package/testdata.mjs +608 -0
- package/unixtime.mjs +505 -0
- package/url.mjs +415 -0
- package/user-agent.mjs +633 -0
- package/uuid.mjs +238 -0
- package/vendor/jsquash-webp/CHECKSUMS.sha256 +10 -0
package/aspect-ratio.mjs
ADDED
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
// アスペクト比計算&レスポンシブサイズ算出(tools.first-ch.com/aspect-ratio/ と同一ロジック)
|
|
2
|
+
//
|
|
3
|
+
// 「計算コア」ブロックは site 側の site/aspect-ratio/app.js と同一の実装。
|
|
4
|
+
// 2箇所ルール: 片方を直したらもう片方も同じ内容で直す(site側が正本)。
|
|
5
|
+
// 使っているのは Number/String/RegExp だけなので、ブラウザ版のコードをそのまま持ってこられる。
|
|
6
|
+
|
|
7
|
+
/* ==================== ここから計算コア(site / MCP で同一) ==================== */
|
|
8
|
+
|
|
9
|
+
const AR_MAX_DECIMALS = 6; // 表示・丸めに使う小数の上限
|
|
10
|
+
const AR_MAX_SIZE = 1000000; // 寸法として受け付ける上限 px
|
|
11
|
+
const AR_APPROX_DEN = 50; // 「およそ N:M」を出すときの分母の上限
|
|
12
|
+
export const AR_ROUND_MODES = ['none', 'round', 'floor', 'ceil', 'even'];
|
|
13
|
+
export const AR_DEFAULT_WIDTHS = [320, 375, 414, 768, 1024, 1280, 1440, 1920];
|
|
14
|
+
|
|
15
|
+
// よく使う比率。key は表示側で日英に訳す
|
|
16
|
+
export const AR_PRESETS = [
|
|
17
|
+
{ key: 'HD', w: 16, h: 9 },
|
|
18
|
+
{ key: 'SD', w: 4, h: 3 },
|
|
19
|
+
{ key: 'PHOTO', w: 3, h: 2 },
|
|
20
|
+
{ key: 'SQUARE', w: 1, h: 1 },
|
|
21
|
+
{ key: 'FIVE_FOUR', w: 5, h: 4 },
|
|
22
|
+
{ key: 'PORTRAIT_45', w: 4, h: 5 },
|
|
23
|
+
{ key: 'PORTRAIT_23', w: 2, h: 3 },
|
|
24
|
+
{ key: 'STORY', w: 9, h: 16 },
|
|
25
|
+
{ key: 'ULTRA', w: 21, h: 9 },
|
|
26
|
+
{ key: 'ULTRA_REAL', w: 64, h: 27 },
|
|
27
|
+
{ key: 'VISTA', w: 37, h: 20 }, // 1.85:1
|
|
28
|
+
{ key: 'SCOPE', w: 239, h: 100 }, // 2.39:1
|
|
29
|
+
{ key: 'OGP', w: 40, h: 21 }, // 1.905:1(1200×630)
|
|
30
|
+
{ key: 'GOLDEN', w: 1618, h: 1000 }, // 黄金比
|
|
31
|
+
{ key: 'PAPER', w: 297, h: 210 }, // A判用紙(√2:1)
|
|
32
|
+
{ key: 'TWO_ONE', w: 2, h: 1 },
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
// 「その寸法には名前がある」と言えるもの。key は表示側で日英に訳す
|
|
36
|
+
export const AR_KNOWN = {
|
|
37
|
+
'1920x1080': 'FHD', '1280x720': 'HD', '3840x2160': 'UHD4K', '2560x1440': 'QHD',
|
|
38
|
+
'1080x1920': 'FHD_V', '720x1280': 'HD_V', '1080x1080': 'IG_SQUARE', '1080x1350': 'IG_PORTRAIT',
|
|
39
|
+
'1200x630': 'OGP_IMAGE', '640x480': 'VGA', '1024x768': 'XGA', '1366x768': 'LAPTOP',
|
|
40
|
+
'3440x1440': 'UW34', '2560x1080': 'UW25', '1920x1200': 'WUXGA', '4096x2160': 'DCI4K',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** 文字列・数値を有限な数へ。読めなければ NaN(0 と区別するため例外にはしない) */
|
|
44
|
+
export function arNum(v) {
|
|
45
|
+
if (typeof v === 'number') return Number.isFinite(v) ? v : NaN;
|
|
46
|
+
const s = String(v === undefined || v === null ? '' : v).trim().replace(/,/g, '').replace(/px$/i, '').trim();
|
|
47
|
+
if (!s) return NaN;
|
|
48
|
+
const n = Number(s);
|
|
49
|
+
return Number.isFinite(n) ? n : NaN;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 寸法として使える正の数か */
|
|
53
|
+
export function arIsSize(n) {
|
|
54
|
+
return Number.isFinite(n) && n > 0 && n <= AR_MAX_SIZE;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 数値を表示・CSS用の文字列へ。小数6桁まで見て末尾の0を落とす。
|
|
59
|
+
* 二進小数の誤差(0.1+0.2)が末尾に出ないよう、丸めてから文字列にする。
|
|
60
|
+
*/
|
|
61
|
+
export function arFormat(n) {
|
|
62
|
+
if (!Number.isFinite(n)) return '';
|
|
63
|
+
const s = n.toFixed(AR_MAX_DECIMALS).replace(/0+$/, '').replace(/\.$/, '');
|
|
64
|
+
return s === '-0' ? '0' : s;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 整数の最大公約数(0 のときは 1 を返して割り算を壊さない) */
|
|
68
|
+
export function arGcd(a, b) {
|
|
69
|
+
let x = Math.abs(Math.round(a));
|
|
70
|
+
let y = Math.abs(Math.round(b));
|
|
71
|
+
while (y) {
|
|
72
|
+
const t = x % y;
|
|
73
|
+
x = y;
|
|
74
|
+
y = t;
|
|
75
|
+
}
|
|
76
|
+
return x || 1;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** その数の小数桁数(表示上の桁数。1.850 → 2) */
|
|
80
|
+
export function arDecimals(n) {
|
|
81
|
+
const s = arFormat(n);
|
|
82
|
+
const i = s.indexOf('.');
|
|
83
|
+
return i === -1 ? 0 : s.length - i - 1;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 幅・高さを約分した比にする。
|
|
88
|
+
* 小数は「入力した桁数」ぶん10倍して整数にしてから約分する(1.85 : 1 → 37 : 20)。
|
|
89
|
+
*/
|
|
90
|
+
export function arSimplify(w, h) {
|
|
91
|
+
const p = Math.pow(10, Math.min(AR_MAX_DECIMALS, Math.max(arDecimals(w), arDecimals(h))));
|
|
92
|
+
const W = Math.round(w * p);
|
|
93
|
+
const H = Math.round(h * p);
|
|
94
|
+
const g = arGcd(W, H);
|
|
95
|
+
return { w: W / g, h: H / g };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 小数を「分母が maxDen 以下でいちばん近い分数」にする(連分数展開)。
|
|
100
|
+
* 1.7778 → 16:9 のように、割り切れない比を人が読める形へ寄せるのに使う。
|
|
101
|
+
*/
|
|
102
|
+
export function arApprox(value, maxDen) {
|
|
103
|
+
if (!(value > 0)) return null;
|
|
104
|
+
const limit = Math.max(1, Math.trunc(maxDen) || 1);
|
|
105
|
+
let n0 = 0, d0 = 1, n1 = 1, d1 = 0, x = value;
|
|
106
|
+
for (let i = 0; i < 32; i += 1) {
|
|
107
|
+
const a = Math.floor(x);
|
|
108
|
+
const n2 = a * n1 + n0;
|
|
109
|
+
const d2 = a * d1 + d0;
|
|
110
|
+
if (d2 > limit) break;
|
|
111
|
+
n0 = n1; d0 = d1; n1 = n2; d1 = d2;
|
|
112
|
+
const frac = x - a;
|
|
113
|
+
if (frac < 1e-12) break;
|
|
114
|
+
x = 1 / frac;
|
|
115
|
+
}
|
|
116
|
+
if (!(n1 > 0) || !(d1 > 0)) return null;
|
|
117
|
+
return { w: n1, h: d1, decimal: n1 / d1, error: Math.abs(n1 / d1 - value) / value * 100 };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* その小数と一致する定番比率(説明文に使う)。
|
|
122
|
+
* 21:9 は約分すると 7:3 になるので、整数の一致ではなく小数で比べる。
|
|
123
|
+
*/
|
|
124
|
+
export function arPresetFor(decimal) {
|
|
125
|
+
if (!(decimal > 0)) return null;
|
|
126
|
+
return AR_PRESETS.find((p) => Math.abs(p.w / p.h - decimal) < 1e-9) || null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** いちばん近い定番比率と、そこからのずれ(%) */
|
|
130
|
+
export function arNearestPreset(decimal) {
|
|
131
|
+
if (!(decimal > 0)) return null;
|
|
132
|
+
let best = null;
|
|
133
|
+
for (const p of AR_PRESETS) {
|
|
134
|
+
const d = p.w / p.h;
|
|
135
|
+
const error = Math.abs(d - decimal) / decimal * 100;
|
|
136
|
+
if (!best || error < best.error) best = { key: p.key, w: p.w, h: p.h, decimal: d, error };
|
|
137
|
+
}
|
|
138
|
+
return best;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 「16:9」「16/9」「16x9」「16 × 9」「1.85」「1.85:1」
|
|
142
|
+
const AR_NUM_SRC = '(\\d+(?:\\.\\d+)?|\\.\\d+)';
|
|
143
|
+
const AR_RATIO_RE = new RegExp('^' + AR_NUM_SRC + '\\s*(?::|/|x|×|by)\\s*' + AR_NUM_SRC + '$', 'i');
|
|
144
|
+
const AR_SINGLE_RE = new RegExp('^' + AR_NUM_SRC + '$');
|
|
145
|
+
|
|
146
|
+
/** 比率の文字列を {w, h} へ。読めなければ null */
|
|
147
|
+
export function arParseRatio(input) {
|
|
148
|
+
const s = String(input === undefined || input === null ? '' : input).trim().replace(/,/g, '');
|
|
149
|
+
if (!s) return null;
|
|
150
|
+
let w, h;
|
|
151
|
+
const m = AR_RATIO_RE.exec(s);
|
|
152
|
+
if (m) {
|
|
153
|
+
w = Number(m[1]);
|
|
154
|
+
h = Number(m[2]);
|
|
155
|
+
} else {
|
|
156
|
+
const one = AR_SINGLE_RE.exec(s);
|
|
157
|
+
if (!one) return null;
|
|
158
|
+
w = Number(one[1]);
|
|
159
|
+
h = 1;
|
|
160
|
+
}
|
|
161
|
+
if (!arIsSize(w) || !arIsSize(h)) return null;
|
|
162
|
+
return { w, h };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** 丸め方を適用する。none は二進小数の誤差だけ落とす */
|
|
166
|
+
export function arRound(v, mode) {
|
|
167
|
+
if (!Number.isFinite(v)) return NaN;
|
|
168
|
+
if (mode === 'round') return Math.round(v);
|
|
169
|
+
if (mode === 'floor') return Math.floor(v);
|
|
170
|
+
if (mode === 'ceil') return Math.ceil(v);
|
|
171
|
+
if (mode === 'even') return Math.max(2, Math.round(v / 2) * 2);
|
|
172
|
+
return Math.round(v * 1e6) / 1e6;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 比率と、幅または高さから、もう一方を求める。
|
|
177
|
+
* @param {object} opts
|
|
178
|
+
* @param {number} opts.ratioW 比の左(幅)側
|
|
179
|
+
* @param {number} opts.ratioH 比の右(高さ)側
|
|
180
|
+
* @param {number} [opts.width] 幅 px(height と併せてどちらか一方は必須)
|
|
181
|
+
* @param {number} [opts.height] 高さ px
|
|
182
|
+
* @param {'none'|'round'|'floor'|'ceil'|'even'} [opts.round='none'] 整数への丸め方
|
|
183
|
+
* @returns {object|null}
|
|
184
|
+
*/
|
|
185
|
+
export function arSize(opts) {
|
|
186
|
+
const o = opts || {};
|
|
187
|
+
const rw = arNum(o.ratioW);
|
|
188
|
+
const rh = arNum(o.ratioH);
|
|
189
|
+
if (!arIsSize(rw) || !arIsSize(rh)) return null;
|
|
190
|
+
const decimal = rw / rh;
|
|
191
|
+
|
|
192
|
+
let width = arNum(o.width);
|
|
193
|
+
let height = arNum(o.height);
|
|
194
|
+
const hasW = arIsSize(width);
|
|
195
|
+
const hasH = arIsSize(height);
|
|
196
|
+
if (!hasW && !hasH) return null;
|
|
197
|
+
if (hasW && !hasH) height = width / decimal;
|
|
198
|
+
else if (hasH && !hasW) width = height * decimal;
|
|
199
|
+
|
|
200
|
+
const mode = AR_ROUND_MODES.indexOf(o.round) !== -1 ? o.round : 'none';
|
|
201
|
+
const outW = arRound(width, mode);
|
|
202
|
+
const outH = arRound(height, mode);
|
|
203
|
+
const actual = outH > 0 ? outW / outH : NaN;
|
|
204
|
+
const ratio = arSimplify(rw, rh);
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
ratio,
|
|
208
|
+
ratioInput: { w: rw, h: rh },
|
|
209
|
+
decimal,
|
|
210
|
+
exactWidth: width,
|
|
211
|
+
exactHeight: height,
|
|
212
|
+
width: outW,
|
|
213
|
+
height: outH,
|
|
214
|
+
// 'none' は二進小数の誤差を落とすだけなので「丸めた」とは言わない
|
|
215
|
+
rounded: mode !== 'none' && (Math.abs(outW - width) > 1e-9 || Math.abs(outH - height) > 1e-9),
|
|
216
|
+
exact: Math.abs(width - Math.round(width)) < 1e-9 && Math.abs(height - Math.round(height)) < 1e-9,
|
|
217
|
+
actualRatio: actual,
|
|
218
|
+
errorPercent: Number.isFinite(actual) ? (actual - decimal) / decimal * 100 : NaN,
|
|
219
|
+
orientation: decimal > 1.0001 ? 'landscape' : decimal < 0.9999 ? 'portrait' : 'square',
|
|
220
|
+
megapixels: outW * outH / 1e6,
|
|
221
|
+
paddingTop: 100 / decimal,
|
|
222
|
+
css: 'aspect-ratio: ' + arFormat(ratio.w) + ' / ' + arFormat(ratio.h) + ';',
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* 幅と高さから比率を求める(約分・近い分数・近い定番比率・向き・画素数)。
|
|
228
|
+
* @returns {object|null}
|
|
229
|
+
*/
|
|
230
|
+
export function arRatioOf(width, height) {
|
|
231
|
+
const w = arNum(width);
|
|
232
|
+
const h = arNum(height);
|
|
233
|
+
if (!arIsSize(w) || !arIsSize(h)) return null;
|
|
234
|
+
const decimal = w / h;
|
|
235
|
+
const ratio = arSimplify(w, h);
|
|
236
|
+
const approx = arApprox(decimal, AR_APPROX_DEN);
|
|
237
|
+
const nearest = arNearestPreset(decimal);
|
|
238
|
+
// 約分した比が既に十分小さければ、近似分数はそれ自身(別掲する意味がない)
|
|
239
|
+
const approxSame = !approx || (approx.w === ratio.w && approx.h === ratio.h);
|
|
240
|
+
return {
|
|
241
|
+
width: w,
|
|
242
|
+
height: h,
|
|
243
|
+
decimal,
|
|
244
|
+
ratio,
|
|
245
|
+
approx: approxSame ? null : approx,
|
|
246
|
+
nearest,
|
|
247
|
+
orientation: decimal > 1.0001 ? 'landscape' : decimal < 0.9999 ? 'portrait' : 'square',
|
|
248
|
+
megapixels: w * h / 1e6,
|
|
249
|
+
known: AR_KNOWN[arFormat(w) + 'x' + arFormat(h)] || null,
|
|
250
|
+
paddingTop: 100 / decimal,
|
|
251
|
+
css: 'aspect-ratio: ' + arFormat(ratio.w) + ' / ' + arFormat(ratio.h) + ';',
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 元の寸法を表示枠へ contain / cover ではめ込んだ結果。
|
|
257
|
+
* contain は枠に収める(余白が出る)、cover は枠を埋める(はみ出しを切る)。
|
|
258
|
+
* @returns {object|null}
|
|
259
|
+
*/
|
|
260
|
+
export function arFit(opts) {
|
|
261
|
+
const o = opts || {};
|
|
262
|
+
const sw = arNum(o.srcW), sh = arNum(o.srcH);
|
|
263
|
+
const bw = arNum(o.boxW), bh = arNum(o.boxH);
|
|
264
|
+
if (![sw, sh, bw, bh].every(arIsSize)) return null;
|
|
265
|
+
const mode = o.mode === 'cover' ? 'cover' : 'contain';
|
|
266
|
+
const sx = bw / sw;
|
|
267
|
+
const sy = bh / sh;
|
|
268
|
+
const scale = mode === 'cover' ? Math.max(sx, sy) : Math.min(sx, sy);
|
|
269
|
+
const w = sw * scale;
|
|
270
|
+
const h = sh * scale;
|
|
271
|
+
return {
|
|
272
|
+
mode,
|
|
273
|
+
scale,
|
|
274
|
+
width: w,
|
|
275
|
+
height: h,
|
|
276
|
+
offsetX: (bw - w) / 2,
|
|
277
|
+
offsetY: (bh - h) / 2,
|
|
278
|
+
barX: Math.max(0, (bw - w) / 2), // 左右の余白(ピラーボックス)
|
|
279
|
+
barY: Math.max(0, (bh - h) / 2), // 上下の余白(レターボックス)
|
|
280
|
+
cropX: Math.max(0, (w - bw) / 2), // 左右の切り取り
|
|
281
|
+
cropY: Math.max(0, (h - bh) / 2),
|
|
282
|
+
// 元画像のうち枠に残る面積の割合(cover のとき 100% 未満になる)
|
|
283
|
+
visiblePercent: Math.min(100, (Math.min(w, bw) * Math.min(h, bh)) / (w * h) * 100),
|
|
284
|
+
srcRatio: sw / sh,
|
|
285
|
+
boxRatio: bw / bh,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 幅の一覧に対する高さの早見表。
|
|
291
|
+
* @returns {Array<object>}
|
|
292
|
+
*/
|
|
293
|
+
export function arTable(opts) {
|
|
294
|
+
const o = opts || {};
|
|
295
|
+
const rw = arNum(o.ratioW);
|
|
296
|
+
const rh = arNum(o.ratioH);
|
|
297
|
+
if (!arIsSize(rw) || !arIsSize(rh)) return [];
|
|
298
|
+
const decimal = rw / rh;
|
|
299
|
+
const mode = AR_ROUND_MODES.indexOf(o.round) !== -1 ? o.round : 'round';
|
|
300
|
+
const list = (Array.isArray(o.widths) && o.widths.length ? o.widths : AR_DEFAULT_WIDTHS)
|
|
301
|
+
.map(arNum)
|
|
302
|
+
.filter(arIsSize);
|
|
303
|
+
return list.map((w) => {
|
|
304
|
+
const exactHeight = w / decimal;
|
|
305
|
+
const height = arRound(exactHeight, mode);
|
|
306
|
+
return {
|
|
307
|
+
width: w,
|
|
308
|
+
exactHeight,
|
|
309
|
+
height,
|
|
310
|
+
exact: Math.abs(exactHeight - Math.round(exactHeight)) < 1e-9,
|
|
311
|
+
paddingTop: 100 / decimal,
|
|
312
|
+
};
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* CSS(と貼り付け用HTML)のスニペットを組み立てる。
|
|
318
|
+
* @param {object} opts ratioW / ratioH / selector / target / fit / fallback / width
|
|
319
|
+
*/
|
|
320
|
+
export function arSnippet(opts) {
|
|
321
|
+
const o = opts || {};
|
|
322
|
+
const rw = arNum(o.ratioW);
|
|
323
|
+
const rh = arNum(o.ratioH);
|
|
324
|
+
if (!arIsSize(rw) || !arIsSize(rh)) return null;
|
|
325
|
+
const ratio = arSimplify(rw, rh);
|
|
326
|
+
const decimal = ratio.w / ratio.h;
|
|
327
|
+
const sel = String(o.selector || '.media').trim() || '.media';
|
|
328
|
+
const target = ['img', 'video', 'iframe', 'background'].indexOf(o.target) !== -1 ? o.target : 'img';
|
|
329
|
+
const fit = ['cover', 'contain', 'fill', 'none'].indexOf(o.fit) !== -1 ? o.fit : 'cover';
|
|
330
|
+
const ar = arFormat(ratio.w) + ' / ' + arFormat(ratio.h);
|
|
331
|
+
const pad = arFormat(Math.round(100 / decimal * 1e4) / 1e4);
|
|
332
|
+
const width = arIsSize(arNum(o.width)) ? arNum(o.width) : 1280;
|
|
333
|
+
const height = arRound(width / decimal, 'round');
|
|
334
|
+
|
|
335
|
+
const css = [];
|
|
336
|
+
css.push(sel + ' {');
|
|
337
|
+
css.push(' aspect-ratio: ' + ar + ';');
|
|
338
|
+
css.push(' width: 100%;');
|
|
339
|
+
if (target === 'background') {
|
|
340
|
+
css.push(' background-image: url("/path/to/image.jpg");');
|
|
341
|
+
css.push(' background-size: ' + (fit === 'contain' ? 'contain' : 'cover') + ';');
|
|
342
|
+
css.push(' background-position: center;');
|
|
343
|
+
css.push(' background-repeat: no-repeat;');
|
|
344
|
+
} else {
|
|
345
|
+
css.push(' overflow: hidden;');
|
|
346
|
+
}
|
|
347
|
+
css.push('}');
|
|
348
|
+
if (target !== 'background') {
|
|
349
|
+
css.push('');
|
|
350
|
+
css.push(sel + ' > ' + target + ' {');
|
|
351
|
+
css.push(' display: block;');
|
|
352
|
+
css.push(' width: 100%;');
|
|
353
|
+
css.push(' height: 100%;');
|
|
354
|
+
if (target === 'iframe') css.push(' border: 0;');
|
|
355
|
+
else css.push(' object-fit: ' + fit + ';');
|
|
356
|
+
css.push('}');
|
|
357
|
+
}
|
|
358
|
+
if (o.fallback) {
|
|
359
|
+
css.push('');
|
|
360
|
+
css.push('/* aspect-ratio に対応しない古いブラウザ向け(padding-top の比率ハック) */');
|
|
361
|
+
css.push('@supports not (aspect-ratio: 1 / 1) {');
|
|
362
|
+
css.push(' ' + sel + ' {');
|
|
363
|
+
css.push(' height: 0;');
|
|
364
|
+
css.push(' padding-top: ' + pad + '%;');
|
|
365
|
+
css.push(' position: relative;');
|
|
366
|
+
css.push(' }');
|
|
367
|
+
if (target !== 'background') {
|
|
368
|
+
css.push(' ' + sel + ' > ' + target + ' {');
|
|
369
|
+
css.push(' position: absolute;');
|
|
370
|
+
css.push(' inset: 0;');
|
|
371
|
+
css.push(' }');
|
|
372
|
+
}
|
|
373
|
+
css.push('}');
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
let html;
|
|
377
|
+
if (target === 'background') {
|
|
378
|
+
html = '<div class="' + sel.replace(/^\./, '') + '"></div>';
|
|
379
|
+
} else if (target === 'iframe') {
|
|
380
|
+
html = '<div class="' + sel.replace(/^\./, '') + '">\n'
|
|
381
|
+
+ ' <iframe src="https://www.youtube.com/embed/VIDEO_ID"\n'
|
|
382
|
+
+ ' width="' + arFormat(width) + '" height="' + arFormat(height) + '"\n'
|
|
383
|
+
+ ' title="" loading="lazy" allowfullscreen></iframe>\n'
|
|
384
|
+
+ '</div>';
|
|
385
|
+
} else {
|
|
386
|
+
html = '<div class="' + sel.replace(/^\./, '') + '">\n'
|
|
387
|
+
+ ' <' + target + ' src="/path/to/file" width="' + arFormat(width) + '" height="' + arFormat(height) + '"'
|
|
388
|
+
+ (target === 'img' ? ' alt=""' : ' controls') + '>' + (target === 'img' ? '' : '</' + target + '>') + '\n'
|
|
389
|
+
+ '</div>';
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return {
|
|
393
|
+
ratio,
|
|
394
|
+
decimal,
|
|
395
|
+
css: css.join('\n'),
|
|
396
|
+
html,
|
|
397
|
+
paddingTop: Number(pad),
|
|
398
|
+
width,
|
|
399
|
+
height,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/* ==================== ここまで計算コア ==================== */
|
|
404
|
+
|
|
405
|
+
// 定番比率の説明(MCPの戻り値は日本語で返す。site側の T.presets と同じ内容)
|
|
406
|
+
const AR_PRESET_TEXT = {
|
|
407
|
+
HD: '16:9(HD・FHD・4K・YouTube・動画全般)',
|
|
408
|
+
SD: '4:3(旧テレビ・iPad・スライド)',
|
|
409
|
+
PHOTO: '3:2(一眼レフ・L判プリント)',
|
|
410
|
+
SQUARE: '1:1(正方形の投稿・アイコン)',
|
|
411
|
+
FIVE_FOUR: '5:4(中判・古いモニタ)',
|
|
412
|
+
PORTRAIT_45: '4:5(Instagram縦)',
|
|
413
|
+
PORTRAIT_23: '2:3(縦位置の写真プリント)',
|
|
414
|
+
STORY: '9:16(ストーリー・Reels・TikTok・スマホ全画面)',
|
|
415
|
+
ULTRA: '21:9(いわゆるウルトラワイド・通称)',
|
|
416
|
+
ULTRA_REAL: '64:27(2560×1080 のウルトラワイドの実際の比)',
|
|
417
|
+
VISTA: '1.85:1(映画のビスタサイズ)',
|
|
418
|
+
SCOPE: '2.39:1(シネマスコープ)',
|
|
419
|
+
OGP: '1.91:1(OGP・SNSシェア画像・1200×630)',
|
|
420
|
+
GOLDEN: '1.618:1(黄金比)',
|
|
421
|
+
PAPER: '√2:1(A判用紙・A4は297×210)',
|
|
422
|
+
TWO_ONE: '2:1(ユニビジウム)',
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
const AR_KNOWN_TEXT = {
|
|
426
|
+
FHD: 'FHD(1080p)', HD: 'HD(720p)', UHD4K: '4K UHD', QHD: 'QHD(1440p)',
|
|
427
|
+
FHD_V: '1080p の縦向き', HD_V: '720p の縦向き', IG_SQUARE: 'Instagramの正方形',
|
|
428
|
+
IG_PORTRAIT: 'Instagramの縦長', OGP_IMAGE: 'OGP・SNSシェア画像',
|
|
429
|
+
VGA: 'VGA', XGA: 'XGA', LAPTOP: 'ノートPCでよくある画面', UW34: '34インチのウルトラワイド',
|
|
430
|
+
UW25: '25/29インチのウルトラワイド', WUXGA: 'WUXGA', DCI4K: 'DCI 4K',
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
const AR_NOTE_TEXT = {
|
|
434
|
+
EXACT: (s) => `${s} は割り切れます(丸めは起きていません)。`,
|
|
435
|
+
NOT_INTEGER: (a) => `${a.from} のとき ${a.to} は整数になりません。ブラウザは小数のまま扱えますが、画像や動画として書き出すときは整数にする必要があります(いちばん近いのは ${a.nearest})。`,
|
|
436
|
+
ROUNDED: (a) => `${a.size} に丸めた場合の実際の比率は ${a.ratio} で、指定した比率から ${a.error}% ずれます。`,
|
|
437
|
+
ODD_DIMENSION: (s) => `${s} は奇数を含みます。H.264 / H.265 は色情報を縦横半分の解像度で持つ(YUV 4:2:0)ため、奇数の幅・高さはエンコーダに拒否されるか黙って引き伸ばされます。round="even" を指定してください。`,
|
|
438
|
+
KNOWN_SIZE: (a) => `${a.size} は ${a.name}です。`,
|
|
439
|
+
PRESET: (s) => s,
|
|
440
|
+
ULTRAWIDE: () => '「21:9」のモニタは実際には21:9ではありません。2560×1080 は 64:27(2.370)、3440×1440 は 43:18(2.389)です。CSSに 21:9 と書くと端に1px前後の隙間が出ます。',
|
|
441
|
+
NEAREST: (a) => `いちばん近い定番比率: ${a.label}(${a.error}% のずれ)。`,
|
|
442
|
+
NEAREST_EXACT: (s) => `ちょうど ${s}です。`,
|
|
443
|
+
APPROX: (a) => `これは ${a.ratio} と ${a.error}% しか違いません。CSSに書くならこちらの方が読みやすくなります。`,
|
|
444
|
+
BIG_RATIO: (s) => `約分しても ${s} にしかなりません。共通の約数がほとんどない寸法は、比率から決めたものではなく切り抜きの結果であることが多いです。`,
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
export class AspectRatioError extends Error {}
|
|
448
|
+
|
|
449
|
+
const arNote = (code, arg) => ({ code, message: AR_NOTE_TEXT[code] ? AR_NOTE_TEXT[code](arg) : code });
|
|
450
|
+
|
|
451
|
+
/** 表示用に小数2桁へ寄せる(誤差やパーセントの見た目を揃える) */
|
|
452
|
+
const arPct = (n) => arFormat(Math.round(n * 100) / 100);
|
|
453
|
+
/** 表示用に小数4桁へ寄せる(比率の小数) */
|
|
454
|
+
const arDec = (n) => arFormat(Math.round(n * 1e4) / 1e4);
|
|
455
|
+
|
|
456
|
+
const arRatioText = (r) => arFormat(r.w) + ':' + arFormat(r.h);
|
|
457
|
+
|
|
458
|
+
/** 早見表の行を戻り値の形へ */
|
|
459
|
+
function arTableRows(rows) {
|
|
460
|
+
return rows.map((r) => ({
|
|
461
|
+
width: r.width,
|
|
462
|
+
height: r.height,
|
|
463
|
+
exact_height: Math.round(r.exactHeight * 1e6) / 1e6,
|
|
464
|
+
exact: r.exact,
|
|
465
|
+
padding_top: arFormat(Math.round(r.paddingTop * 1e4) / 1e4) + '%',
|
|
466
|
+
css: `width: ${arFormat(r.width)}px; height: ${arFormat(r.height)}px;`,
|
|
467
|
+
}));
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** はめ込み結果を戻り値の形へ */
|
|
471
|
+
function arFitResult(f) {
|
|
472
|
+
const round2 = (n) => Math.round(n * 100) / 100;
|
|
473
|
+
const out = {
|
|
474
|
+
mode: f.mode,
|
|
475
|
+
scale: Math.round(f.scale * 1e6) / 1e6,
|
|
476
|
+
width: round2(f.width),
|
|
477
|
+
height: round2(f.height),
|
|
478
|
+
offset_x: round2(f.offsetX),
|
|
479
|
+
offset_y: round2(f.offsetY),
|
|
480
|
+
};
|
|
481
|
+
if (f.mode === 'contain') {
|
|
482
|
+
out.bar_x = round2(f.barX);
|
|
483
|
+
out.bar_y = round2(f.barY);
|
|
484
|
+
} else {
|
|
485
|
+
out.crop_x = round2(f.cropX);
|
|
486
|
+
out.crop_y = round2(f.cropY);
|
|
487
|
+
out.visible_percent = round2(f.visiblePercent);
|
|
488
|
+
}
|
|
489
|
+
out.same_ratio = Math.abs(f.srcRatio - f.boxRatio) < 1e-6;
|
|
490
|
+
return out;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* アスペクト比の計算。
|
|
495
|
+
*
|
|
496
|
+
* ratio と width / height の片方を渡すと、もう一方の寸法を求める(mode: 'size')。
|
|
497
|
+
* width と height を渡して ratio を渡さないと、その寸法の比率を求める(mode: 'measure')。
|
|
498
|
+
* ratio だけを渡すと、その比率そのものの情報を返す(mode: 'ratio')。
|
|
499
|
+
* box を渡すと、元の寸法をその枠へ contain / cover ではめ込んだ結果も返す。
|
|
500
|
+
*
|
|
501
|
+
* @param {object} opts
|
|
502
|
+
* @param {string|number} [opts.ratio] 比率('16:9' / '16/9' / '1.85' / 1.85)
|
|
503
|
+
* @param {number} [opts.width] 幅 px
|
|
504
|
+
* @param {number} [opts.height] 高さ px
|
|
505
|
+
* @param {'none'|'round'|'floor'|'ceil'|'even'} [opts.round='none'] 丸め方
|
|
506
|
+
* @param {number[]} [opts.widths] 早見表に並べる幅の一覧
|
|
507
|
+
* @param {boolean} [opts.table] 早見表を返すか(widths を渡すと自動で true)
|
|
508
|
+
* @param {string|object} [opts.box] はめ込む枠('1280x400' か {width,height})
|
|
509
|
+
* @param {'cover'|'contain'} [opts.fit='cover'] はめ方
|
|
510
|
+
* @param {boolean} [opts.snippet] CSS / HTML のスニペットを返すか
|
|
511
|
+
* @param {string} [opts.selector='.media'] スニペットのセレクタ
|
|
512
|
+
* @param {'img'|'video'|'iframe'|'background'} [opts.target='img'] スニペットの中身
|
|
513
|
+
* @param {'cover'|'contain'|'fill'|'none'} [opts.objectFit='cover'] object-fit
|
|
514
|
+
* @param {boolean} [opts.fallback] padding-top のフォールバックを付けるか
|
|
515
|
+
*/
|
|
516
|
+
export function aspectRatioCalc(opts = {}) {
|
|
517
|
+
const hasRatio = opts.ratio !== undefined && opts.ratio !== null && String(opts.ratio).trim() !== '';
|
|
518
|
+
const width = arNum(opts.width);
|
|
519
|
+
const height = arNum(opts.height);
|
|
520
|
+
const hasW = opts.width !== undefined && opts.width !== null && String(opts.width).trim() !== '';
|
|
521
|
+
const hasH = opts.height !== undefined && opts.height !== null && String(opts.height).trim() !== '';
|
|
522
|
+
|
|
523
|
+
if (hasW && !arIsSize(width)) throw new AspectRatioError(`width は 0 より大きい ${AR_MAX_SIZE} 以下の数値で指定してください: ${opts.width}`);
|
|
524
|
+
if (hasH && !arIsSize(height)) throw new AspectRatioError(`height は 0 より大きい ${AR_MAX_SIZE} 以下の数値で指定してください: ${opts.height}`);
|
|
525
|
+
if (!hasRatio && !hasW && !hasH) {
|
|
526
|
+
throw new AspectRatioError('ratio(比率)か width / height(寸法)のどちらかを渡してください');
|
|
527
|
+
}
|
|
528
|
+
if (!hasRatio && !(hasW && hasH)) {
|
|
529
|
+
throw new AspectRatioError('ratio を省略する場合は width と height の両方を渡してください(その2つから比率を求めます)');
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
let ratio = null;
|
|
533
|
+
if (hasRatio) {
|
|
534
|
+
ratio = arParseRatio(opts.ratio);
|
|
535
|
+
if (!ratio) throw new AspectRatioError(`ratio を読めません('16:9' / '16/9' / '1.85' のように指定してください): ${opts.ratio}`);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
const round = opts.round === undefined || opts.round === null ? 'none' : String(opts.round);
|
|
539
|
+
if (AR_ROUND_MODES.indexOf(round) === -1) {
|
|
540
|
+
throw new AspectRatioError(`round は ${AR_ROUND_MODES.join(' / ')} のいずれか: ${opts.round}`);
|
|
541
|
+
}
|
|
542
|
+
const fitMode = opts.fit === undefined || opts.fit === null ? 'cover' : String(opts.fit);
|
|
543
|
+
if (['cover', 'contain'].indexOf(fitMode) === -1) {
|
|
544
|
+
throw new AspectRatioError(`fit は cover / contain のいずれか: ${opts.fit}`);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
let box = null;
|
|
548
|
+
if (opts.box !== undefined && opts.box !== null && opts.box !== '') {
|
|
549
|
+
box = typeof opts.box === 'object'
|
|
550
|
+
? (arIsSize(arNum(opts.box.width)) && arIsSize(arNum(opts.box.height))
|
|
551
|
+
? { w: arNum(opts.box.width), h: arNum(opts.box.height) } : null)
|
|
552
|
+
: arParseRatio(opts.box);
|
|
553
|
+
if (!box) throw new AspectRatioError(`box を読めません('1280x400' か {width,height} で指定してください): ${JSON.stringify(opts.box)}`);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const wantTable = opts.table === true || (Array.isArray(opts.widths) && opts.widths.length > 0);
|
|
557
|
+
const wantSnippet = opts.snippet === true;
|
|
558
|
+
const notes = [];
|
|
559
|
+
const result = {};
|
|
560
|
+
|
|
561
|
+
if (hasRatio && (hasW || hasH)) {
|
|
562
|
+
/* ---- 比率+片側の寸法 → もう一方 ---- */
|
|
563
|
+
const r = arSize({
|
|
564
|
+
ratioW: ratio.w,
|
|
565
|
+
ratioH: ratio.h,
|
|
566
|
+
width: hasW ? width : undefined,
|
|
567
|
+
height: hasH ? height : undefined,
|
|
568
|
+
round,
|
|
569
|
+
});
|
|
570
|
+
if (!r) throw new AspectRatioError('寸法を計算できませんでした');
|
|
571
|
+
const sizeText = `${arFormat(r.width)} × ${arFormat(r.height)}`;
|
|
572
|
+
Object.assign(result, {
|
|
573
|
+
mode: 'size',
|
|
574
|
+
ratio: { w: r.ratio.w, h: r.ratio.h, text: arRatioText(r.ratio), decimal: arDec(r.decimal) },
|
|
575
|
+
width: r.width,
|
|
576
|
+
height: r.height,
|
|
577
|
+
exact_width: Math.round(r.exactWidth * 1e6) / 1e6,
|
|
578
|
+
exact_height: Math.round(r.exactHeight * 1e6) / 1e6,
|
|
579
|
+
rounding: round,
|
|
580
|
+
rounded: r.rounded,
|
|
581
|
+
actual_ratio: arDec(r.actualRatio),
|
|
582
|
+
error_percent: Math.round(r.errorPercent * 1e4) / 1e4,
|
|
583
|
+
orientation: r.orientation,
|
|
584
|
+
megapixels: Math.round(r.megapixels * 1000) / 1000,
|
|
585
|
+
padding_top: arFormat(Math.round(r.paddingTop * 1e4) / 1e4) + '%',
|
|
586
|
+
css: r.css,
|
|
587
|
+
formatted: { size: sizeText },
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
const known = AR_KNOWN[arFormat(r.width) + 'x' + arFormat(r.height)];
|
|
591
|
+
if (known) notes.push(arNote('KNOWN_SIZE', { size: sizeText, name: AR_KNOWN_TEXT[known] }));
|
|
592
|
+
if (r.exact) {
|
|
593
|
+
notes.push(arNote('EXACT', `${arFormat(r.exactWidth)} × ${arFormat(r.exactHeight)}`));
|
|
594
|
+
} else {
|
|
595
|
+
const from = hasW ? `幅 ${arFormat(r.exactWidth)}` : `高さ ${arFormat(r.exactHeight)}`;
|
|
596
|
+
const to = hasW ? `高さ ${arFormat(r.exactHeight)}` : `幅 ${arFormat(r.exactWidth)}`;
|
|
597
|
+
const other = hasW ? r.exactHeight : r.exactWidth;
|
|
598
|
+
notes.push(arNote('NOT_INTEGER', { from, to, nearest: arFormat(Math.round(other)) }));
|
|
599
|
+
}
|
|
600
|
+
if (r.rounded && Math.abs(r.errorPercent) > 1e-9) {
|
|
601
|
+
notes.push(arNote('ROUNDED', { size: sizeText, ratio: arDec(r.actualRatio), error: arPct(Math.abs(r.errorPercent)) }));
|
|
602
|
+
}
|
|
603
|
+
if (Number.isInteger(r.width) && Number.isInteger(r.height) && (r.width % 2 !== 0 || r.height % 2 !== 0)) {
|
|
604
|
+
notes.push(arNote('ODD_DIMENSION', sizeText));
|
|
605
|
+
}
|
|
606
|
+
const preset = arPresetFor(r.decimal);
|
|
607
|
+
if (preset && preset.key === 'ULTRA') notes.push(arNote('ULTRAWIDE'));
|
|
608
|
+
else if (preset) notes.push(arNote('PRESET', AR_PRESET_TEXT[preset.key]));
|
|
609
|
+
} else if (hasRatio) {
|
|
610
|
+
/* ---- 比率だけ ---- */
|
|
611
|
+
const simple = arSimplify(ratio.w, ratio.h);
|
|
612
|
+
const decimal = ratio.w / ratio.h;
|
|
613
|
+
const nearest = arNearestPreset(decimal);
|
|
614
|
+
Object.assign(result, {
|
|
615
|
+
mode: 'ratio',
|
|
616
|
+
ratio: { w: simple.w, h: simple.h, text: arRatioText(simple), decimal: arDec(decimal) },
|
|
617
|
+
orientation: decimal > 1.0001 ? 'landscape' : decimal < 0.9999 ? 'portrait' : 'square',
|
|
618
|
+
padding_top: arFormat(Math.round(100 / decimal * 1e4) / 1e4) + '%',
|
|
619
|
+
css: 'aspect-ratio: ' + arFormat(simple.w) + ' / ' + arFormat(simple.h) + ';',
|
|
620
|
+
});
|
|
621
|
+
const preset = arPresetFor(decimal);
|
|
622
|
+
if (preset && preset.key === 'ULTRA') notes.push(arNote('ULTRAWIDE'));
|
|
623
|
+
else if (preset) notes.push(arNote('PRESET', AR_PRESET_TEXT[preset.key]));
|
|
624
|
+
else if (nearest && nearest.error >= 0.05) {
|
|
625
|
+
notes.push(arNote('NEAREST', { label: AR_PRESET_TEXT[nearest.key], error: arPct(nearest.error) }));
|
|
626
|
+
}
|
|
627
|
+
} else {
|
|
628
|
+
/* ---- 寸法だけ → 比率を求める ---- */
|
|
629
|
+
const r = arRatioOf(width, height);
|
|
630
|
+
if (!r) throw new AspectRatioError('比率を計算できませんでした');
|
|
631
|
+
const sizeText = `${arFormat(r.width)} × ${arFormat(r.height)}`;
|
|
632
|
+
ratio = { w: r.ratio.w, h: r.ratio.h };
|
|
633
|
+
Object.assign(result, {
|
|
634
|
+
mode: 'measure',
|
|
635
|
+
width: r.width,
|
|
636
|
+
height: r.height,
|
|
637
|
+
ratio: { w: r.ratio.w, h: r.ratio.h, text: arRatioText(r.ratio), decimal: arDec(r.decimal) },
|
|
638
|
+
orientation: r.orientation,
|
|
639
|
+
megapixels: Math.round(r.megapixels * 1000) / 1000,
|
|
640
|
+
padding_top: arFormat(Math.round(r.paddingTop * 1e4) / 1e4) + '%',
|
|
641
|
+
css: r.css,
|
|
642
|
+
formatted: { size: sizeText },
|
|
643
|
+
});
|
|
644
|
+
if (r.approx) {
|
|
645
|
+
result.approx = { w: r.approx.w, h: r.approx.h, text: arRatioText(r.approx), error_percent: Math.round(r.approx.error * 1e4) / 1e4 };
|
|
646
|
+
}
|
|
647
|
+
if (r.nearest) {
|
|
648
|
+
result.nearest = {
|
|
649
|
+
w: r.nearest.w, h: r.nearest.h, text: arRatioText(r.nearest),
|
|
650
|
+
label: AR_PRESET_TEXT[r.nearest.key], error_percent: Math.round(r.nearest.error * 1e4) / 1e4,
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
if (r.known) {
|
|
654
|
+
result.known = AR_KNOWN_TEXT[r.known];
|
|
655
|
+
notes.push(arNote('KNOWN_SIZE', { size: sizeText, name: AR_KNOWN_TEXT[r.known] }));
|
|
656
|
+
}
|
|
657
|
+
if (r.nearest) {
|
|
658
|
+
if (r.nearest.error < 0.05) notes.push(arNote('NEAREST_EXACT', AR_PRESET_TEXT[r.nearest.key]));
|
|
659
|
+
else notes.push(arNote('NEAREST', { label: AR_PRESET_TEXT[r.nearest.key], error: arPct(r.nearest.error) }));
|
|
660
|
+
}
|
|
661
|
+
if (r.approx) {
|
|
662
|
+
notes.push(arNote('APPROX', { ratio: arRatioText(r.approx), error: arPct(r.approx.error) }));
|
|
663
|
+
}
|
|
664
|
+
if (r.ratio.w > 100 || r.ratio.h > 100) notes.push(arNote('BIG_RATIO', arRatioText(r.ratio)));
|
|
665
|
+
const preset = arPresetFor(r.decimal);
|
|
666
|
+
if (preset && preset.key === 'ULTRA') notes.push(arNote('ULTRAWIDE'));
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/* ---- 枠へのはめ込み ---- */
|
|
670
|
+
if (box) {
|
|
671
|
+
const srcW = result.width !== undefined ? result.width : ratio.w;
|
|
672
|
+
const srcH = result.height !== undefined ? result.height : ratio.h;
|
|
673
|
+
const f = arFit({ srcW, srcH, boxW: box.w, boxH: box.h, mode: fitMode });
|
|
674
|
+
if (f) {
|
|
675
|
+
result.fit = Object.assign({ box: { width: box.w, height: box.h } }, arFitResult(f));
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/* ---- 早見表 ---- */
|
|
680
|
+
if (wantTable) {
|
|
681
|
+
const rows = arTable({
|
|
682
|
+
ratioW: ratio.w,
|
|
683
|
+
ratioH: ratio.h,
|
|
684
|
+
widths: opts.widths,
|
|
685
|
+
round: round === 'none' ? 'round' : round,
|
|
686
|
+
});
|
|
687
|
+
result.table = arTableRows(rows);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/* ---- スニペット ---- */
|
|
691
|
+
if (wantSnippet) {
|
|
692
|
+
const s = arSnippet({
|
|
693
|
+
ratioW: ratio.w,
|
|
694
|
+
ratioH: ratio.h,
|
|
695
|
+
selector: opts.selector,
|
|
696
|
+
target: opts.target,
|
|
697
|
+
fit: opts.objectFit,
|
|
698
|
+
fallback: opts.fallback,
|
|
699
|
+
width: result.width,
|
|
700
|
+
});
|
|
701
|
+
if (s) result.snippet = { css: s.css, html: s.html };
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
result.notes = notes;
|
|
705
|
+
return result;
|
|
706
|
+
}
|