@egova-mobile/app-media-utils 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.
@@ -0,0 +1,1522 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __commonJS = (cb, mod) => function __require() {
9
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
+ };
11
+ var __export = (target, all) => {
12
+ for (var name in all)
13
+ __defProp(target, name, { get: all[name], enumerable: true });
14
+ };
15
+ var __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from === "object" || typeof from === "function") {
17
+ for (let key of __getOwnPropNames(from))
18
+ if (!__hasOwnProp.call(to, key) && key !== except)
19
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
24
+ // If the importer is in node compatibility mode or this is not an ESM
25
+ // file that has been converted to a CommonJS file using a Babel-
26
+ // compatible transform (i.e. "__esModule" has not been set), then set
27
+ // "default" to the CommonJS "module.exports" for node compatibility.
28
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
29
+ mod
30
+ ));
31
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
+ var __async = (__this, __arguments, generator) => {
33
+ return new Promise((resolve, reject) => {
34
+ var fulfilled = (value) => {
35
+ try {
36
+ step(generator.next(value));
37
+ } catch (e) {
38
+ reject(e);
39
+ }
40
+ };
41
+ var rejected = (value) => {
42
+ try {
43
+ step(generator.throw(value));
44
+ } catch (e) {
45
+ reject(e);
46
+ }
47
+ };
48
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
49
+ step((generator = generator.apply(__this, __arguments)).next());
50
+ });
51
+ };
52
+
53
+ // ../../node_modules/.pnpm/dayjs@1.11.10/node_modules/dayjs/dayjs.min.js
54
+ var require_dayjs_min = __commonJS({
55
+ "../../node_modules/.pnpm/dayjs@1.11.10/node_modules/dayjs/dayjs.min.js"(exports, module2) {
56
+ !function(t, e) {
57
+ "object" == typeof exports && "undefined" != typeof module2 ? module2.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e();
58
+ }(exports, function() {
59
+ "use strict";
60
+ var t = 1e3, e = 6e4, n = 36e5, r = "millisecond", i = "second", s = "minute", u = "hour", a = "day", o = "week", c = "month", f = "quarter", h = "year", d = "date", l = "Invalid Date", $ = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, M = { name: "en", weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"), months: "January_February_March_April_May_June_July_August_September_October_November_December".split("_"), ordinal: function(t2) {
61
+ var e2 = ["th", "st", "nd", "rd"], n2 = t2 % 100;
62
+ return "[" + t2 + (e2[(n2 - 20) % 10] || e2[n2] || e2[0]) + "]";
63
+ } }, m = function(t2, e2, n2) {
64
+ var r2 = String(t2);
65
+ return !r2 || r2.length >= e2 ? t2 : "" + Array(e2 + 1 - r2.length).join(n2) + t2;
66
+ }, v = { s: m, z: function(t2) {
67
+ var e2 = -t2.utcOffset(), n2 = Math.abs(e2), r2 = Math.floor(n2 / 60), i2 = n2 % 60;
68
+ return (e2 <= 0 ? "+" : "-") + m(r2, 2, "0") + ":" + m(i2, 2, "0");
69
+ }, m: function t2(e2, n2) {
70
+ if (e2.date() < n2.date())
71
+ return -t2(n2, e2);
72
+ var r2 = 12 * (n2.year() - e2.year()) + (n2.month() - e2.month()), i2 = e2.clone().add(r2, c), s2 = n2 - i2 < 0, u2 = e2.clone().add(r2 + (s2 ? -1 : 1), c);
73
+ return +(-(r2 + (n2 - i2) / (s2 ? i2 - u2 : u2 - i2)) || 0);
74
+ }, a: function(t2) {
75
+ return t2 < 0 ? Math.ceil(t2) || 0 : Math.floor(t2);
76
+ }, p: function(t2) {
77
+ return { M: c, y: h, w: o, d: a, D: d, h: u, m: s, s: i, ms: r, Q: f }[t2] || String(t2 || "").toLowerCase().replace(/s$/, "");
78
+ }, u: function(t2) {
79
+ return void 0 === t2;
80
+ } }, g = "en", D = {};
81
+ D[g] = M;
82
+ var p = "$isDayjsObject", S = function(t2) {
83
+ return t2 instanceof _ || !(!t2 || !t2[p]);
84
+ }, w = function t2(e2, n2, r2) {
85
+ var i2;
86
+ if (!e2)
87
+ return g;
88
+ if ("string" == typeof e2) {
89
+ var s2 = e2.toLowerCase();
90
+ D[s2] && (i2 = s2), n2 && (D[s2] = n2, i2 = s2);
91
+ var u2 = e2.split("-");
92
+ if (!i2 && u2.length > 1)
93
+ return t2(u2[0]);
94
+ } else {
95
+ var a2 = e2.name;
96
+ D[a2] = e2, i2 = a2;
97
+ }
98
+ return !r2 && i2 && (g = i2), i2 || !r2 && g;
99
+ }, O = function(t2, e2) {
100
+ if (S(t2))
101
+ return t2.clone();
102
+ var n2 = "object" == typeof e2 ? e2 : {};
103
+ return n2.date = t2, n2.args = arguments, new _(n2);
104
+ }, b = v;
105
+ b.l = w, b.i = S, b.w = function(t2, e2) {
106
+ return O(t2, { locale: e2.$L, utc: e2.$u, x: e2.$x, $offset: e2.$offset });
107
+ };
108
+ var _ = function() {
109
+ function M2(t2) {
110
+ this.$L = w(t2.locale, null, true), this.parse(t2), this.$x = this.$x || t2.x || {}, this[p] = true;
111
+ }
112
+ var m2 = M2.prototype;
113
+ return m2.parse = function(t2) {
114
+ this.$d = function(t3) {
115
+ var e2 = t3.date, n2 = t3.utc;
116
+ if (null === e2)
117
+ return /* @__PURE__ */ new Date(NaN);
118
+ if (b.u(e2))
119
+ return /* @__PURE__ */ new Date();
120
+ if (e2 instanceof Date)
121
+ return new Date(e2);
122
+ if ("string" == typeof e2 && !/Z$/i.test(e2)) {
123
+ var r2 = e2.match($);
124
+ if (r2) {
125
+ var i2 = r2[2] - 1 || 0, s2 = (r2[7] || "0").substring(0, 3);
126
+ return n2 ? new Date(Date.UTC(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2)) : new Date(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2);
127
+ }
128
+ }
129
+ return new Date(e2);
130
+ }(t2), this.init();
131
+ }, m2.init = function() {
132
+ var t2 = this.$d;
133
+ this.$y = t2.getFullYear(), this.$M = t2.getMonth(), this.$D = t2.getDate(), this.$W = t2.getDay(), this.$H = t2.getHours(), this.$m = t2.getMinutes(), this.$s = t2.getSeconds(), this.$ms = t2.getMilliseconds();
134
+ }, m2.$utils = function() {
135
+ return b;
136
+ }, m2.isValid = function() {
137
+ return !(this.$d.toString() === l);
138
+ }, m2.isSame = function(t2, e2) {
139
+ var n2 = O(t2);
140
+ return this.startOf(e2) <= n2 && n2 <= this.endOf(e2);
141
+ }, m2.isAfter = function(t2, e2) {
142
+ return O(t2) < this.startOf(e2);
143
+ }, m2.isBefore = function(t2, e2) {
144
+ return this.endOf(e2) < O(t2);
145
+ }, m2.$g = function(t2, e2, n2) {
146
+ return b.u(t2) ? this[e2] : this.set(n2, t2);
147
+ }, m2.unix = function() {
148
+ return Math.floor(this.valueOf() / 1e3);
149
+ }, m2.valueOf = function() {
150
+ return this.$d.getTime();
151
+ }, m2.startOf = function(t2, e2) {
152
+ var n2 = this, r2 = !!b.u(e2) || e2, f2 = b.p(t2), l2 = function(t3, e3) {
153
+ var i2 = b.w(n2.$u ? Date.UTC(n2.$y, e3, t3) : new Date(n2.$y, e3, t3), n2);
154
+ return r2 ? i2 : i2.endOf(a);
155
+ }, $2 = function(t3, e3) {
156
+ return b.w(n2.toDate()[t3].apply(n2.toDate("s"), (r2 ? [0, 0, 0, 0] : [23, 59, 59, 999]).slice(e3)), n2);
157
+ }, y2 = this.$W, M3 = this.$M, m3 = this.$D, v2 = "set" + (this.$u ? "UTC" : "");
158
+ switch (f2) {
159
+ case h:
160
+ return r2 ? l2(1, 0) : l2(31, 11);
161
+ case c:
162
+ return r2 ? l2(1, M3) : l2(0, M3 + 1);
163
+ case o:
164
+ var g2 = this.$locale().weekStart || 0, D2 = (y2 < g2 ? y2 + 7 : y2) - g2;
165
+ return l2(r2 ? m3 - D2 : m3 + (6 - D2), M3);
166
+ case a:
167
+ case d:
168
+ return $2(v2 + "Hours", 0);
169
+ case u:
170
+ return $2(v2 + "Minutes", 1);
171
+ case s:
172
+ return $2(v2 + "Seconds", 2);
173
+ case i:
174
+ return $2(v2 + "Milliseconds", 3);
175
+ default:
176
+ return this.clone();
177
+ }
178
+ }, m2.endOf = function(t2) {
179
+ return this.startOf(t2, false);
180
+ }, m2.$set = function(t2, e2) {
181
+ var n2, o2 = b.p(t2), f2 = "set" + (this.$u ? "UTC" : ""), l2 = (n2 = {}, n2[a] = f2 + "Date", n2[d] = f2 + "Date", n2[c] = f2 + "Month", n2[h] = f2 + "FullYear", n2[u] = f2 + "Hours", n2[s] = f2 + "Minutes", n2[i] = f2 + "Seconds", n2[r] = f2 + "Milliseconds", n2)[o2], $2 = o2 === a ? this.$D + (e2 - this.$W) : e2;
182
+ if (o2 === c || o2 === h) {
183
+ var y2 = this.clone().set(d, 1);
184
+ y2.$d[l2]($2), y2.init(), this.$d = y2.set(d, Math.min(this.$D, y2.daysInMonth())).$d;
185
+ } else
186
+ l2 && this.$d[l2]($2);
187
+ return this.init(), this;
188
+ }, m2.set = function(t2, e2) {
189
+ return this.clone().$set(t2, e2);
190
+ }, m2.get = function(t2) {
191
+ return this[b.p(t2)]();
192
+ }, m2.add = function(r2, f2) {
193
+ var d2, l2 = this;
194
+ r2 = Number(r2);
195
+ var $2 = b.p(f2), y2 = function(t2) {
196
+ var e2 = O(l2);
197
+ return b.w(e2.date(e2.date() + Math.round(t2 * r2)), l2);
198
+ };
199
+ if ($2 === c)
200
+ return this.set(c, this.$M + r2);
201
+ if ($2 === h)
202
+ return this.set(h, this.$y + r2);
203
+ if ($2 === a)
204
+ return y2(1);
205
+ if ($2 === o)
206
+ return y2(7);
207
+ var M3 = (d2 = {}, d2[s] = e, d2[u] = n, d2[i] = t, d2)[$2] || 1, m3 = this.$d.getTime() + r2 * M3;
208
+ return b.w(m3, this);
209
+ }, m2.subtract = function(t2, e2) {
210
+ return this.add(-1 * t2, e2);
211
+ }, m2.format = function(t2) {
212
+ var e2 = this, n2 = this.$locale();
213
+ if (!this.isValid())
214
+ return n2.invalidDate || l;
215
+ var r2 = t2 || "YYYY-MM-DDTHH:mm:ssZ", i2 = b.z(this), s2 = this.$H, u2 = this.$m, a2 = this.$M, o2 = n2.weekdays, c2 = n2.months, f2 = n2.meridiem, h2 = function(t3, n3, i3, s3) {
216
+ return t3 && (t3[n3] || t3(e2, r2)) || i3[n3].slice(0, s3);
217
+ }, d2 = function(t3) {
218
+ return b.s(s2 % 12 || 12, t3, "0");
219
+ }, $2 = f2 || function(t3, e3, n3) {
220
+ var r3 = t3 < 12 ? "AM" : "PM";
221
+ return n3 ? r3.toLowerCase() : r3;
222
+ };
223
+ return r2.replace(y, function(t3, r3) {
224
+ return r3 || function(t4) {
225
+ switch (t4) {
226
+ case "YY":
227
+ return String(e2.$y).slice(-2);
228
+ case "YYYY":
229
+ return b.s(e2.$y, 4, "0");
230
+ case "M":
231
+ return a2 + 1;
232
+ case "MM":
233
+ return b.s(a2 + 1, 2, "0");
234
+ case "MMM":
235
+ return h2(n2.monthsShort, a2, c2, 3);
236
+ case "MMMM":
237
+ return h2(c2, a2);
238
+ case "D":
239
+ return e2.$D;
240
+ case "DD":
241
+ return b.s(e2.$D, 2, "0");
242
+ case "d":
243
+ return String(e2.$W);
244
+ case "dd":
245
+ return h2(n2.weekdaysMin, e2.$W, o2, 2);
246
+ case "ddd":
247
+ return h2(n2.weekdaysShort, e2.$W, o2, 3);
248
+ case "dddd":
249
+ return o2[e2.$W];
250
+ case "H":
251
+ return String(s2);
252
+ case "HH":
253
+ return b.s(s2, 2, "0");
254
+ case "h":
255
+ return d2(1);
256
+ case "hh":
257
+ return d2(2);
258
+ case "a":
259
+ return $2(s2, u2, true);
260
+ case "A":
261
+ return $2(s2, u2, false);
262
+ case "m":
263
+ return String(u2);
264
+ case "mm":
265
+ return b.s(u2, 2, "0");
266
+ case "s":
267
+ return String(e2.$s);
268
+ case "ss":
269
+ return b.s(e2.$s, 2, "0");
270
+ case "SSS":
271
+ return b.s(e2.$ms, 3, "0");
272
+ case "Z":
273
+ return i2;
274
+ }
275
+ return null;
276
+ }(t3) || i2.replace(":", "");
277
+ });
278
+ }, m2.utcOffset = function() {
279
+ return 15 * -Math.round(this.$d.getTimezoneOffset() / 15);
280
+ }, m2.diff = function(r2, d2, l2) {
281
+ var $2, y2 = this, M3 = b.p(d2), m3 = O(r2), v2 = (m3.utcOffset() - this.utcOffset()) * e, g2 = this - m3, D2 = function() {
282
+ return b.m(y2, m3);
283
+ };
284
+ switch (M3) {
285
+ case h:
286
+ $2 = D2() / 12;
287
+ break;
288
+ case c:
289
+ $2 = D2();
290
+ break;
291
+ case f:
292
+ $2 = D2() / 3;
293
+ break;
294
+ case o:
295
+ $2 = (g2 - v2) / 6048e5;
296
+ break;
297
+ case a:
298
+ $2 = (g2 - v2) / 864e5;
299
+ break;
300
+ case u:
301
+ $2 = g2 / n;
302
+ break;
303
+ case s:
304
+ $2 = g2 / e;
305
+ break;
306
+ case i:
307
+ $2 = g2 / t;
308
+ break;
309
+ default:
310
+ $2 = g2;
311
+ }
312
+ return l2 ? $2 : b.a($2);
313
+ }, m2.daysInMonth = function() {
314
+ return this.endOf(c).$D;
315
+ }, m2.$locale = function() {
316
+ return D[this.$L];
317
+ }, m2.locale = function(t2, e2) {
318
+ if (!t2)
319
+ return this.$L;
320
+ var n2 = this.clone(), r2 = w(t2, e2, true);
321
+ return r2 && (n2.$L = r2), n2;
322
+ }, m2.clone = function() {
323
+ return b.w(this.$d, this);
324
+ }, m2.toDate = function() {
325
+ return new Date(this.valueOf());
326
+ }, m2.toJSON = function() {
327
+ return this.isValid() ? this.toISOString() : null;
328
+ }, m2.toISOString = function() {
329
+ return this.$d.toISOString();
330
+ }, m2.toString = function() {
331
+ return this.$d.toUTCString();
332
+ }, M2;
333
+ }(), k = _.prototype;
334
+ return O.prototype = k, [["$ms", r], ["$s", i], ["$m", s], ["$H", u], ["$W", a], ["$M", c], ["$y", h], ["$D", d]].forEach(function(t2) {
335
+ k[t2[1]] = function(e2) {
336
+ return this.$g(e2, t2[0], t2[1]);
337
+ };
338
+ }), O.extend = function(t2, e2) {
339
+ return t2.$i || (t2(e2, _, O), t2.$i = true), O;
340
+ }, O.locale = w, O.isDayjs = S, O.unix = function(t2) {
341
+ return O(1e3 * t2);
342
+ }, O.en = D[g], O.Ls = D, O.p = {}, O;
343
+ });
344
+ }
345
+ });
346
+
347
+ // ../../node_modules/.pnpm/dayjs@1.11.10/node_modules/dayjs/locale/zh-cn.js
348
+ var require_zh_cn = __commonJS({
349
+ "../../node_modules/.pnpm/dayjs@1.11.10/node_modules/dayjs/locale/zh-cn.js"(exports, module2) {
350
+ !function(e, _) {
351
+ "object" == typeof exports && "undefined" != typeof module2 ? module2.exports = _(require_dayjs_min()) : "function" == typeof define && define.amd ? define(["dayjs"], _) : (e = "undefined" != typeof globalThis ? globalThis : e || self).dayjs_locale_zh_cn = _(e.dayjs);
352
+ }(exports, function(e) {
353
+ "use strict";
354
+ function _(e2) {
355
+ return e2 && "object" == typeof e2 && "default" in e2 ? e2 : { default: e2 };
356
+ }
357
+ var t = _(e), d = { name: "zh-cn", weekdays: "星期日_星期一_星期二_星期三_星期四_星期五_星期六".split("_"), weekdaysShort: "周日_周一_周二_周三_周四_周五_周六".split("_"), weekdaysMin: "日_一_二_三_四_五_六".split("_"), months: "一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月".split("_"), monthsShort: "1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月".split("_"), ordinal: function(e2, _2) {
358
+ return "W" === _2 ? e2 + "周" : e2 + "日";
359
+ }, weekStart: 1, yearStart: 4, formats: { LT: "HH:mm", LTS: "HH:mm:ss", L: "YYYY/MM/DD", LL: "YYYY年M月D日", LLL: "YYYY年M月D日Ah点mm分", LLLL: "YYYY年M月D日ddddAh点mm分", l: "YYYY/M/D", ll: "YYYY年M月D日", lll: "YYYY年M月D日 HH:mm", llll: "YYYY年M月D日dddd HH:mm" }, relativeTime: { future: "%s内", past: "%s前", s: "几秒", m: "1 分钟", mm: "%d 分钟", h: "1 小时", hh: "%d 小时", d: "1 天", dd: "%d 天", M: "1 个月", MM: "%d 个月", y: "1 年", yy: "%d 年" }, meridiem: function(e2, _2) {
360
+ var t2 = 100 * e2 + _2;
361
+ return t2 < 600 ? "凌晨" : t2 < 900 ? "早上" : t2 < 1100 ? "上午" : t2 < 1300 ? "中午" : t2 < 1800 ? "下午" : "晚上";
362
+ } };
363
+ return t.default.locale(d, null, true), d;
364
+ });
365
+ }
366
+ });
367
+
368
+ // src/index.ts
369
+ var src_exports = {};
370
+ __export(src_exports, {
371
+ CompressOptions: () => CompressOptions,
372
+ ImageProcessor: () => ImageProcessor,
373
+ ResizeOptions: () => ResizeOptions,
374
+ WatermarkContext: () => WatermarkContext,
375
+ WatermarkOptions: () => WatermarkOptions,
376
+ canvas2File: () => canvas2File,
377
+ canvas2Image: () => canvas2Image,
378
+ checkWatermarkConfigSupported: () => checkWatermarkConfigSupported,
379
+ file2DataURL: () => file2DataURL,
380
+ file2Image: () => file2Image,
381
+ image2Canvas: () => image2Canvas,
382
+ image2file: () => image2file,
383
+ resize: () => resize
384
+ });
385
+ module.exports = __toCommonJS(src_exports);
386
+
387
+ // src/utils/type-check.ts
388
+ function checkDataType(tgt, type) {
389
+ const dataType = Object.prototype.toString.call(tgt).replace(/\[object (\w+)\]/, "$1").toLowerCase();
390
+ return type ? dataType === type : dataType;
391
+ }
392
+ function checkIsUndefined(tgt) {
393
+ return checkDataType(tgt, "undefined");
394
+ }
395
+ function checkIsNull(tgt) {
396
+ return checkDataType(tgt, "null");
397
+ }
398
+ function checkIsUndefinedOrNull(tgt) {
399
+ return checkIsUndefined(tgt) || checkIsNull(tgt);
400
+ }
401
+ function checkIsString(tgt) {
402
+ return checkDataType(tgt, "string");
403
+ }
404
+ function checkIsStringValid(tgt) {
405
+ if (checkDataType(tgt, "string")) {
406
+ return tgt !== "";
407
+ } else {
408
+ return false;
409
+ }
410
+ }
411
+ function checkIsRegExp(tgt) {
412
+ return checkDataType(tgt, "regexp");
413
+ }
414
+
415
+ // src/utils/type-parse.ts
416
+ function parseInteger(obj, defaultValue) {
417
+ let result = parseInt(obj, 10);
418
+ if (isNaN(result)) {
419
+ result = defaultValue || 0;
420
+ }
421
+ return result;
422
+ }
423
+ function parseDecimal(obj, defaultValue) {
424
+ let result = parseFloat(obj);
425
+ if (isNaN(result)) {
426
+ result = defaultValue || 0;
427
+ }
428
+ return result;
429
+ }
430
+
431
+ // src/utils/date.ts
432
+ var FORMAT = {
433
+ //英文简写如:12:01
434
+ FORMAT_HM: "HH:mm",
435
+ //英文简写如:1-12 12:01
436
+ FORMAT_MDHM: "MM-dd HH:mm",
437
+ //中文简写如:1月12日
438
+ FORMAT_MD_CN: "MM月dd日",
439
+ //中文简写如:1月12日
440
+ FORMAT_MD: "MM-dd",
441
+ //英文简写(默认)如:2010-12-01
442
+ FORMAT_YMD: "yyyy-MM-dd",
443
+ FORMAT_YMD_SLASH: "yyyy/MM/dd",
444
+ //英文简写(默认)如:2010-12
445
+ FORMAT_YM: "yyyy-MM",
446
+ //英文全称 如:2010-12-01 23:15
447
+ FORMAT_YMDHM: "yyyy-MM-dd HH:mm",
448
+ //英文全称 如:2010-12-01 23:15:06
449
+ FORMAT_YMDHMS: "yyyy-MM-dd HH:mm:ss",
450
+ //英文全称 如:2010-12-01 23:15:06
451
+ FORMAT_YMDHMSMS: "yyyy-MM-dd HH:mm:ss msms",
452
+ //中文简写 如:2010年12月
453
+ FORMAT_YM_CN: "yyyy年MM月",
454
+ //中文简写 如:2010年12月01日
455
+ FORMAT_YMD_CN: "yyyy年MM月dd日",
456
+ //中文简写 如:2010年12月01日 12时
457
+ FORMAT_YMDH_CN: "yyyy年MM月dd日 HH时",
458
+ //中文简写 如:12月01日 12时
459
+ FORMAT_MDH_CN: "MM月dd日 HH时",
460
+ //中文简写 如:2010年12月01日 12时12分
461
+ FORMAT_YMDHM_CN: "yyyy年MM月dd日 HH时mm分",
462
+ //中文全称 如:2010年12月01日 23时15分06秒
463
+ FORMAT_YMDHMS_CN: "yyyy年MM月dd日 HH时mm分ss秒"
464
+ };
465
+
466
+ // src/utils/system-check.ts
467
+ function checkIsAndroid() {
468
+ return /android/.test(navigator.userAgent.toLowerCase());
469
+ }
470
+ function checkIsIOS() {
471
+ return /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
472
+ }
473
+ function checkIsWindows() {
474
+ return navigator.platform.indexOf("Win") > -1;
475
+ }
476
+
477
+ // src/image/watermark.ts
478
+ var import_dayjs = __toESM(require_dayjs_min());
479
+ var import_zh_cn = __toESM(require_zh_cn());
480
+ import_dayjs.default.locale("zh-cn");
481
+ var Point = class {
482
+ constructor(x, y) {
483
+ this.x = x;
484
+ this.y = y;
485
+ }
486
+ };
487
+ var Size = class {
488
+ constructor(width, height) {
489
+ this.width = width;
490
+ this.height = height;
491
+ }
492
+ };
493
+ var clamp = (value, min, max) => Math.min(Math.max(value, min), max);
494
+ var subString = (str, startIndex, length) => {
495
+ if (checkIsUndefinedOrNull(length)) {
496
+ return str.substring(startIndex);
497
+ } else {
498
+ return str.substring(startIndex, startIndex + length);
499
+ }
500
+ };
501
+ var DEFAULT_ADDRESS_ICON = "data:image/png;base64,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";
502
+ var ColorMap = {
503
+ transparent: "#00000000",
504
+ white: "#FFFFFF",
505
+ black: "#000000",
506
+ darkgray: "#444444",
507
+ gray: "#888888",
508
+ lightgray: "#CCCCCC",
509
+ darkgrey: "#444444",
510
+ grey: "#888888",
511
+ lightgrey: "#CCCCCC",
512
+ red: "#FF0000",
513
+ green: "#00FF00",
514
+ blue: "#0000FF",
515
+ yellow: "#FFFF00",
516
+ cyan: "#00FFFF",
517
+ magenta: "#FF00FF",
518
+ aqua: "#00FFFF",
519
+ fuchsia: "#FF00FF",
520
+ lime: "#00FF00",
521
+ maroon: "#800000",
522
+ navy: "#000080",
523
+ olive: "#808000",
524
+ purple: "#800080",
525
+ silver: "#C0C0C0",
526
+ teal: "#008080",
527
+ orange: "#FF9933"
528
+ };
529
+ function getColorValue(key) {
530
+ for (const k of Object.getOwnPropertyNames(ColorMap)) {
531
+ if (k === key) {
532
+ return ColorMap[k];
533
+ }
534
+ }
535
+ return null;
536
+ }
537
+ var CONTENT_PADDING = 10;
538
+ var DEFAULT_PADDING = 2;
539
+ var DEFAULT_FONT_SIZE = 25;
540
+ var DEFAULT_CANVAS_SIZE = 480;
541
+ var DEFAULT_BACKGROUND_OPACITY = 128;
542
+ var LOGO_DEFAULT_PADDING = 4;
543
+ var WatermarkStyle = class {
544
+ //是否是简化配置
545
+ /**
546
+ * Watermark Style
547
+ * @param fontName
548
+ * @param fontWeight
549
+ * @param fontColor
550
+ * @param fontSize
551
+ * @param backgroundColor
552
+ * @param backgroundOpacity
553
+ * @param paddingTop
554
+ * @param paddingBottom
555
+ * @param position
556
+ * @param scale
557
+ * @param simplified
558
+ */
559
+ constructor(fontName, fontWeight, fontColor, fontSize, backgroundColor, backgroundOpacity, paddingTop, paddingBottom, position, scale, simplified) {
560
+ this.fontName = fontName;
561
+ this.fontWeight = fontWeight;
562
+ this.fontColor = fontColor;
563
+ this.fontSize = fontSize;
564
+ this.backgroundColor = backgroundColor;
565
+ this.backgroundOpacity = backgroundOpacity;
566
+ this.paddingTop = paddingTop;
567
+ this.paddingBottom = paddingBottom;
568
+ this.position = position;
569
+ this.scale = scale;
570
+ this.simplified = simplified;
571
+ }
572
+ };
573
+ var WatermarkImage = class {
574
+ constructor(image = "", width = 20, height = 20, position = 0 /* POSITION_LEFT */) {
575
+ this.image = image;
576
+ this.width = width;
577
+ this.height = height;
578
+ this.position = position;
579
+ }
580
+ };
581
+ var Watermark = class {
582
+ /**
583
+ * 水印对象,包含需要渲染的文本、图像,以及相应的style
584
+ * @param content
585
+ * @param watermarkStyle
586
+ */
587
+ constructor(content, watermarkStyle, watermarkImage) {
588
+ this.content = content;
589
+ this.style = watermarkStyle;
590
+ this.watermarkImage = watermarkImage;
591
+ }
592
+ };
593
+ var WatermarkContext = class {
594
+ /**
595
+ * 打水印需要的环境信息
596
+ * @param address
597
+ * @param watermarkTime
598
+ * @param humanName
599
+ * @param projectName
600
+ * @param logo
601
+ */
602
+ constructor(params) {
603
+ this.address = params.address;
604
+ this.watermarkTime = params.watermarkTime;
605
+ this.humanName = params.humanName;
606
+ this.projectName = params.projectName;
607
+ this.logo = params.logo;
608
+ this.applicationName = params.applicationName;
609
+ this.pixel = params.pixel;
610
+ this.width = params.width;
611
+ this.height = params.height;
612
+ }
613
+ };
614
+ var WatermarkOptions = class {
615
+ /**
616
+ * 水印配置项
617
+ * @param enabled, 是否开启水印
618
+ * @param watermarkConfigString,水印配置的字符串
619
+ * @param context,打水印需要的环境信息
620
+ */
621
+ constructor(params) {
622
+ this.enabled = params.enabled;
623
+ this.watermarkConfigString = params.watermarkConfigString;
624
+ this.context = params.context;
625
+ }
626
+ };
627
+ function getHeitiFontFamily(fontFamily = "") {
628
+ let defaultFontFamily;
629
+ if (checkIsWindows()) {
630
+ if (fontFamily.includes("黑体")) {
631
+ fontFamily = "";
632
+ }
633
+ defaultFontFamily = "'Roboto Black', 'Droid Sans'";
634
+ } else if (checkIsIOS()) {
635
+ if (fontFamily.includes("黑体")) {
636
+ fontFamily = "";
637
+ }
638
+ defaultFontFamily = "'PingFang SC Medium'";
639
+ } else if (checkIsAndroid()) {
640
+ defaultFontFamily = "'Roboto Black', 'Droid Sans'";
641
+ } else {
642
+ defaultFontFamily = '"PingFang SC Medium", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif';
643
+ }
644
+ return fontFamily ? `${fontFamily}, ${defaultFontFamily}` : defaultFontFamily;
645
+ }
646
+ function parseWatermarkStyle(styleString, canvasSize) {
647
+ if (!checkIsStringValid(styleString)) {
648
+ return null;
649
+ }
650
+ const styleParts = styleString.split("$");
651
+ if (styleParts.length <= 0) {
652
+ return null;
653
+ }
654
+ const scale = Math.min(canvasSize.width, canvasSize.height) / DEFAULT_CANVAS_SIZE;
655
+ if (parseInteger(styleParts[0], -1) !== -1) {
656
+ const fontSize = Math.round(
657
+ parseDecimal(styleParts[0], DEFAULT_FONT_SIZE) * scale
658
+ );
659
+ let position = "lefttop" /* LeftTop */;
660
+ if (styleParts[1]) {
661
+ position = styleParts[1];
662
+ }
663
+ let paddingTop = DEFAULT_PADDING * scale;
664
+ let paddingBottom = DEFAULT_PADDING * scale;
665
+ if (styleParts[2]) {
666
+ const paddingParts = styleParts[2].split("_");
667
+ paddingTop = parseInteger(paddingParts[0], DEFAULT_PADDING) * scale;
668
+ if (paddingParts[1]) {
669
+ paddingBottom = parseInteger(paddingParts[1], DEFAULT_PADDING) * scale;
670
+ }
671
+ }
672
+ const fontName = getHeitiFontFamily();
673
+ const fontWeight = "normal" /* Normal */;
674
+ const fontColor = ColorMap.transparent;
675
+ const backgroundColor = ColorMap.transparent;
676
+ const backgroundOpacity = 128;
677
+ return new WatermarkStyle(
678
+ fontName,
679
+ fontWeight,
680
+ fontColor,
681
+ fontSize,
682
+ backgroundColor,
683
+ backgroundOpacity,
684
+ paddingTop,
685
+ paddingBottom,
686
+ position,
687
+ scale,
688
+ true
689
+ );
690
+ } else {
691
+ const fontName = getHeitiFontFamily(styleParts[0]);
692
+ let fontWeight = "normal" /* Normal */;
693
+ if (styleParts[1]) {
694
+ fontWeight = styleParts[1];
695
+ }
696
+ let fontColor = ColorMap.white;
697
+ if (styleParts[2]) {
698
+ const color = getColorValue(styleParts[2]);
699
+ if (color) {
700
+ fontColor = color;
701
+ }
702
+ }
703
+ let fontSize = Math.round(DEFAULT_FONT_SIZE * scale);
704
+ if (styleParts[3]) {
705
+ fontSize = Math.round(
706
+ parseDecimal(styleParts[3], DEFAULT_FONT_SIZE) * scale
707
+ );
708
+ }
709
+ let backgroundColor = ColorMap.blue;
710
+ if (styleParts[4]) {
711
+ const color = getColorValue(styleParts[4]);
712
+ if (color) {
713
+ backgroundColor = color;
714
+ }
715
+ }
716
+ let position = "lefttop" /* LeftTop */;
717
+ if (styleParts[5]) {
718
+ position = styleParts[5];
719
+ }
720
+ let backgroundOpacity = 255;
721
+ if (styleParts[6]) {
722
+ let opacity = parseInteger(styleParts[6], DEFAULT_BACKGROUND_OPACITY);
723
+ opacity = clamp(opacity, 0, 255);
724
+ backgroundOpacity = opacity;
725
+ }
726
+ let paddingTop = DEFAULT_PADDING * scale;
727
+ let paddingBottom = DEFAULT_PADDING * scale;
728
+ if (styleParts[7]) {
729
+ const paddingParts = styleParts[7].split("_");
730
+ paddingTop = parseInteger(paddingParts[0], DEFAULT_PADDING) * scale;
731
+ if (paddingParts[1]) {
732
+ paddingBottom = parseInteger(paddingParts[1], DEFAULT_PADDING) * scale;
733
+ }
734
+ }
735
+ return new WatermarkStyle(
736
+ fontName,
737
+ fontWeight,
738
+ fontColor,
739
+ fontSize,
740
+ backgroundColor,
741
+ backgroundOpacity,
742
+ paddingTop,
743
+ paddingBottom,
744
+ position,
745
+ scale,
746
+ false
747
+ );
748
+ }
749
+ }
750
+ var TextScanner = class {
751
+ /**
752
+ * The content to scan
753
+ * @param text
754
+ */
755
+ constructor(text) {
756
+ this.text = text;
757
+ this.cursor = 0;
758
+ }
759
+ hasNext(obj) {
760
+ if (checkIsUndefinedOrNull(obj)) {
761
+ return this.cursor < this.text.length;
762
+ } else if (checkIsString(obj)) {
763
+ const str = obj;
764
+ const current = subString(this.text, this.cursor, str.length);
765
+ if (current === str) {
766
+ return true;
767
+ }
768
+ } else if (checkIsRegExp(obj)) {
769
+ const regex = obj;
770
+ const subText = subString(this.text, this.cursor);
771
+ const matchResults = subText.match(regex);
772
+ if (matchResults) {
773
+ if (matchResults.index === 0) {
774
+ return true;
775
+ }
776
+ }
777
+ }
778
+ return false;
779
+ }
780
+ next(obj) {
781
+ if (checkIsUndefinedOrNull(obj)) {
782
+ return subString(this.text, this.cursor, 1);
783
+ } else if (checkIsString(obj)) {
784
+ const str = obj;
785
+ if (subString(this.text, this.cursor, str.length) === str) {
786
+ return str;
787
+ }
788
+ } else if (checkIsRegExp(obj)) {
789
+ const regex = obj;
790
+ const subText = subString(this.text, this.cursor);
791
+ const matchResults = subText.match(regex);
792
+ if (matchResults) {
793
+ if (matchResults.index === 0) {
794
+ return matchResults;
795
+ }
796
+ }
797
+ }
798
+ return null;
799
+ }
800
+ skip(obj) {
801
+ if (checkIsUndefinedOrNull(obj)) {
802
+ this.cursor += 1;
803
+ } else if (checkIsStringValid(obj)) {
804
+ const str = obj;
805
+ this.cursor += str.length;
806
+ }
807
+ return this;
808
+ }
809
+ };
810
+ function transformFormatString(format) {
811
+ let transformString = format;
812
+ if (format.includes("d")) {
813
+ transformString = transformString.replace(/d/g, "D");
814
+ }
815
+ if (format.includes("y")) {
816
+ transformString = transformString.replace(/y/g, "Y");
817
+ }
818
+ if (format.includes("E")) {
819
+ transformString = transformString.replace(/E/g, "d");
820
+ }
821
+ return transformString;
822
+ }
823
+ function buildContent(contentConfigString, watermarkContext, style, canvasSize) {
824
+ let content = "";
825
+ const watermarkImage = new WatermarkImage();
826
+ if (contentConfigString.includes("{logo")) {
827
+ const start = contentConfigString.indexOf("{logo");
828
+ const end = contentConfigString.indexOf("}", start);
829
+ if (end != -1) {
830
+ const logoConfigWithBrace = contentConfigString.substring(start, end + 1);
831
+ if (contentConfigString.startsWith(logoConfigWithBrace) || contentConfigString.endsWith(logoConfigWithBrace)) {
832
+ if (contentConfigString.startsWith(logoConfigWithBrace)) {
833
+ watermarkImage.position = 0 /* POSITION_LEFT */;
834
+ } else {
835
+ watermarkImage.position = 1 /* POSITION_RIGHT */;
836
+ }
837
+ const imgWidthHeight = (style == null ? void 0 : style.fontSize) || 20;
838
+ watermarkImage.image = watermarkContext.logo;
839
+ watermarkImage.height = imgWidthHeight;
840
+ watermarkImage.width = imgWidthHeight;
841
+ }
842
+ contentConfigString = contentConfigString.replace(
843
+ logoConfigWithBrace,
844
+ ""
845
+ );
846
+ }
847
+ }
848
+ const scanner = new TextScanner(contentConfigString);
849
+ let steps = 0;
850
+ while (scanner.hasNext() && /* 防止scanner代码错误产生无限循环,设置一个最大scan步数*/
851
+ steps < contentConfigString.length) {
852
+ const dateRegex = new RegExp("date{(.*?)}|date" /* Date */);
853
+ if (scanner.hasNext(dateRegex)) {
854
+ const regexResult = scanner.next(dateRegex);
855
+ if (regexResult) {
856
+ const matchResults = regexResult;
857
+ let format = FORMAT.FORMAT_YMD;
858
+ if (matchResults && matchResults.length >= 2) {
859
+ if (checkIsStringValid(matchResults[1])) {
860
+ format = matchResults[1];
861
+ }
862
+ }
863
+ content += (0, import_dayjs.default)(watermarkContext.watermarkTime).format(
864
+ transformFormatString(format)
865
+ );
866
+ scanner.skip(matchResults.input);
867
+ }
868
+ } else if (scanner.hasNext("time" /* Time */)) {
869
+ const value = (0, import_dayjs.default)(watermarkContext.watermarkTime).format(
870
+ FORMAT.FORMAT_HM
871
+ );
872
+ content += value;
873
+ scanner.skip("time" /* Time */);
874
+ } else if (scanner.hasNext("address" /* Address */)) {
875
+ content += watermarkContext.address;
876
+ if (!watermarkImage.image) {
877
+ const imgWidthHeight = (style == null ? void 0 : style.fontSize) || 20;
878
+ watermarkImage.image = DEFAULT_ADDRESS_ICON;
879
+ watermarkImage.height = imgWidthHeight;
880
+ watermarkImage.width = imgWidthHeight;
881
+ }
882
+ scanner.skip("address" /* Address */);
883
+ } else if (scanner.hasNext("humanname" /* HumanName */)) {
884
+ content += watermarkContext.humanName;
885
+ scanner.skip("humanname" /* HumanName */);
886
+ } else if (scanner.hasNext("projectName" /* ProjectName */)) {
887
+ content += watermarkContext.projectName;
888
+ scanner.skip("projectName" /* ProjectName */);
889
+ } else if (scanner.hasNext("applicationName" /* ApplicationName */)) {
890
+ content += watermarkContext.applicationName;
891
+ scanner.skip("applicationName" /* ApplicationName */);
892
+ } else if (scanner.hasNext("pixel" /* Pixel */)) {
893
+ content += `${canvasSize.width} * ${canvasSize.height}`;
894
+ scanner.skip("pixel" /* Pixel */);
895
+ } else if (scanner.hasNext("width" /* Width */)) {
896
+ content += canvasSize.width;
897
+ scanner.skip("width" /* Width */);
898
+ } else if (scanner.hasNext("height" /* Height */)) {
899
+ content += canvasSize.height;
900
+ scanner.skip("height" /* Height */);
901
+ } else {
902
+ const nextChar = scanner.next();
903
+ if (checkIsString(nextChar)) {
904
+ content += nextChar;
905
+ }
906
+ scanner.skip();
907
+ }
908
+ steps++;
909
+ }
910
+ return { content, watermarkImage };
911
+ }
912
+ function parseWatermarks(watermarkConfigString, canvasSize, watermarkContext) {
913
+ const watermarks = [];
914
+ const configStringArray = watermarkConfigString.split("###");
915
+ for (let i = 0; i < configStringArray.length; i++) {
916
+ const configString = configStringArray[i];
917
+ const configParts = configString.split("*#*");
918
+ if (configParts.length === 2) {
919
+ const styleString = configParts[0];
920
+ const contentString = configParts[1];
921
+ const style = parseWatermarkStyle(styleString, canvasSize);
922
+ const data = buildContent(
923
+ contentString,
924
+ watermarkContext,
925
+ style,
926
+ canvasSize
927
+ );
928
+ if (style && (data.content || data.watermarkImage.image)) {
929
+ const watermark = new Watermark(
930
+ data.content,
931
+ style,
932
+ data.watermarkImage
933
+ );
934
+ watermarks.push(watermark);
935
+ }
936
+ }
937
+ }
938
+ return watermarks;
939
+ }
940
+ var Tile = class {
941
+ constructor(origin, size, lineHeight, lineWidths, lines, style, watermarkImage) {
942
+ this.origin = origin;
943
+ this.size = size;
944
+ this.lineHeight = lineHeight;
945
+ this.lineWidths = lineWidths;
946
+ this.lines = lines;
947
+ this.style = style;
948
+ this.watermarkImage = watermarkImage;
949
+ }
950
+ };
951
+ function getCanvasScale(canvas, len) {
952
+ const min = Math.min(canvas.width, canvas.height);
953
+ return min / DEFAULT_CANVAS_SIZE * len;
954
+ }
955
+ function buildTile(canvas, watermark) {
956
+ const { content, style, watermarkImage } = watermark;
957
+ const origin = new Point(0, 0);
958
+ const size = new Size(0, 0);
959
+ const lines = [];
960
+ const ctx = canvas.getContext("2d");
961
+ if (!ctx) {
962
+ return null;
963
+ }
964
+ let imageWidth = 0;
965
+ if (watermarkImage.image) {
966
+ imageWidth = watermarkImage.width + LOGO_DEFAULT_PADDING * style.scale;
967
+ }
968
+ ctx.font = `${style.fontWeight} ${style.fontSize}px ${style.fontName}`;
969
+ const contentPadding = getCanvasScale(canvas, CONTENT_PADDING);
970
+ const contentWidth = canvas.width - contentPadding * 2 - imageWidth;
971
+ const lineHeight = ctx.measureText("M").width;
972
+ const lineWidths = [];
973
+ let temp = "";
974
+ let lastWidth = 0;
975
+ for (let i = 0; i < content.length; i++) {
976
+ const lineWidth = ctx.measureText(temp).width;
977
+ if (lineWidth >= contentWidth) {
978
+ const line = temp.substring(0, temp.length - 1);
979
+ const preLineWidth = ctx.measureText(line).width;
980
+ lines.push(line);
981
+ lineWidths.push(preLineWidth);
982
+ temp = temp.substring(temp.length - 1);
983
+ }
984
+ lastWidth = lineWidth;
985
+ temp += content[i];
986
+ }
987
+ lines.push(temp);
988
+ lineWidths.push(lastWidth);
989
+ size.width = contentWidth;
990
+ size.height = lines.length * lineHeight;
991
+ return new Tile(
992
+ origin,
993
+ size,
994
+ lineHeight,
995
+ lineWidths,
996
+ lines,
997
+ style,
998
+ watermarkImage
999
+ );
1000
+ }
1001
+ function getAxisPoint(canvasWidth, width, tile, origin, line) {
1002
+ const { style, watermarkImage, lines, lineHeight } = tile;
1003
+ const padding = (CONTENT_PADDING + DEFAULT_PADDING) * style.scale;
1004
+ let axisPoint = new Point(padding, origin.y);
1005
+ if (style.position.startsWith("center")) {
1006
+ axisPoint = new Point(canvasWidth / 2, origin.y);
1007
+ } else if (style.position.startsWith("right")) {
1008
+ axisPoint = new Point(canvasWidth - padding, origin.y);
1009
+ }
1010
+ const imagePoint = new Point(axisPoint.x, axisPoint.y);
1011
+ if (watermarkImage.image) {
1012
+ const maxWidth = Math.max(...tile.lineWidths);
1013
+ if (watermarkImage.position === 0 /* POSITION_LEFT */) {
1014
+ if (style.position.startsWith("left")) {
1015
+ axisPoint.x = axisPoint.x + watermarkImage.width + LOGO_DEFAULT_PADDING * style.scale;
1016
+ } else if (style.position.startsWith("center")) {
1017
+ if (line) {
1018
+ axisPoint.x = axisPoint.x + watermarkImage.width / 2 + LOGO_DEFAULT_PADDING / 2 * style.scale;
1019
+ imagePoint.x = lines.length === 1 ? imagePoint.x - width / 2 - watermarkImage.width - LOGO_DEFAULT_PADDING / 2 * style.scale : imagePoint.x - maxWidth / 2 - watermarkImage.width / 2 - LOGO_DEFAULT_PADDING / 2 * style.scale;
1020
+ } else {
1021
+ imagePoint.x = imagePoint.x - watermarkImage.width / 2;
1022
+ }
1023
+ } else {
1024
+ if (line) {
1025
+ imagePoint.x = lines.length === 1 ? imagePoint.x - width - 2 * watermarkImage.width - LOGO_DEFAULT_PADDING / 2 * style.scale : imagePoint.x - maxWidth - watermarkImage.width - LOGO_DEFAULT_PADDING / 2 * style.scale;
1026
+ } else {
1027
+ imagePoint.x = imagePoint.x - watermarkImage.width;
1028
+ }
1029
+ }
1030
+ } else {
1031
+ if (style.position.startsWith("left")) {
1032
+ imagePoint.x = lines.length === 1 ? axisPoint.x + width + watermarkImage.width + LOGO_DEFAULT_PADDING * style.scale : axisPoint.x + maxWidth + LOGO_DEFAULT_PADDING * style.scale;
1033
+ } else if (style.position.startsWith("center")) {
1034
+ axisPoint.x = axisPoint.x - watermarkImage.width / 2;
1035
+ imagePoint.x = lines.length === 1 ? imagePoint.x + width / 2 + LOGO_DEFAULT_PADDING * style.scale : imagePoint.x + maxWidth / 2 - watermarkImage.width / 2 + LOGO_DEFAULT_PADDING * style.scale;
1036
+ } else {
1037
+ axisPoint.x = axisPoint.x - watermarkImage.width;
1038
+ imagePoint.x = imagePoint.x - watermarkImage.width + LOGO_DEFAULT_PADDING / 2 * style.scale;
1039
+ }
1040
+ }
1041
+ if (lines.length > 1) {
1042
+ imagePoint.y = imagePoint.y + lines.length * lineHeight / 2 - watermarkImage.height / 2;
1043
+ }
1044
+ }
1045
+ return {
1046
+ text: axisPoint,
1047
+ image: imagePoint
1048
+ };
1049
+ }
1050
+ function getTextAlign(style) {
1051
+ let align = "left";
1052
+ if (style.position.startsWith("center")) {
1053
+ align = "center";
1054
+ } else if (style.position.startsWith("right")) {
1055
+ align = "right";
1056
+ }
1057
+ return align;
1058
+ }
1059
+ function loadImage(url) {
1060
+ return new Promise((resolve, reject) => {
1061
+ const img = new Image();
1062
+ img.src = url;
1063
+ img.crossOrigin = "anonymous";
1064
+ img.onload = function(e) {
1065
+ if (img.complete) {
1066
+ resolve(img);
1067
+ } else {
1068
+ console.error(e);
1069
+ reject(new Error("文件加载失败"));
1070
+ }
1071
+ };
1072
+ });
1073
+ }
1074
+ function drawLine(canvas, line, width, origin, tile, drawImage) {
1075
+ return __async(this, null, function* () {
1076
+ const ctx = canvas.getContext("2d");
1077
+ if (!ctx) {
1078
+ return;
1079
+ }
1080
+ const { style, watermarkImage } = tile;
1081
+ ctx.textBaseline = "top";
1082
+ ctx.font = `${style.fontWeight} ${style.fontSize}px ${style.fontName}`;
1083
+ const point = getAxisPoint(canvas.width, width, tile, origin, line);
1084
+ ctx.textAlign = getTextAlign(style);
1085
+ if (watermarkImage.image && drawImage) {
1086
+ const image = yield loadImage(watermarkImage.image);
1087
+ ctx.drawImage(
1088
+ image,
1089
+ Math.round(point.image.x),
1090
+ Math.round(point.image.y),
1091
+ watermarkImage.width,
1092
+ watermarkImage.height
1093
+ );
1094
+ }
1095
+ if (style.simplified) {
1096
+ ctx.shadowBlur = 4;
1097
+ ctx.shadowColor = "rgba(0, 0, 0, 0.5)";
1098
+ const offset = clamp(style.fontSize / 40, 2, 4);
1099
+ ctx.shadowOffsetY = offset;
1100
+ ctx.shadowOffsetX = offset;
1101
+ ctx.fillStyle = "rgba(255,255,255, 0.5)";
1102
+ } else {
1103
+ ctx.fillStyle = style.fontColor;
1104
+ }
1105
+ ctx.fillText(line, Math.round(point.text.x), Math.round(point.text.y));
1106
+ });
1107
+ }
1108
+ function hexToRGB(hex, alpha) {
1109
+ const r = parseInt(hex.slice(1, 3), 16), g = parseInt(hex.slice(3, 5), 16), b = parseInt(hex.slice(5, 7), 16);
1110
+ if (alpha) {
1111
+ return "rgba(" + r + ", " + g + ", " + b + ", " + alpha + ")";
1112
+ } else {
1113
+ return "rgb(" + r + ", " + g + ", " + b + ")";
1114
+ }
1115
+ }
1116
+ function drawBackground(canvas, tile) {
1117
+ const ctx = canvas.getContext("2d");
1118
+ if (!ctx) {
1119
+ return;
1120
+ }
1121
+ let { backgroundColor } = tile.style;
1122
+ let backgroundOpacity = parseDecimal(
1123
+ String(tile.style.backgroundOpacity),
1124
+ -1
1125
+ );
1126
+ if (backgroundOpacity > 255) {
1127
+ backgroundOpacity = 255;
1128
+ }
1129
+ const { style } = tile;
1130
+ const { paddingTop, paddingBottom } = style;
1131
+ if (backgroundColor !== "transparent") {
1132
+ if (backgroundColor.startsWith("#") && backgroundOpacity >= 0) {
1133
+ backgroundColor = hexToRGB(backgroundColor, backgroundOpacity / 255);
1134
+ }
1135
+ ctx.fillStyle = backgroundColor;
1136
+ const padding = (CONTENT_PADDING + DEFAULT_PADDING) * style.scale;
1137
+ const paddingScale = tile.style.fontSize / DEFAULT_FONT_SIZE;
1138
+ for (let j = 0; j < tile.lineWidths.length; j++) {
1139
+ const { lineHeight } = tile;
1140
+ const lineWidth = tile.lineWidths[j];
1141
+ let paddingRight = 30 * paddingScale;
1142
+ let paddingLeft = 10 * paddingScale;
1143
+ let axisPoint = new Point(padding - paddingLeft, tile.origin.y);
1144
+ if (style.position.startsWith("center")) {
1145
+ paddingLeft = 20 * paddingScale;
1146
+ paddingRight = 20 * paddingScale;
1147
+ axisPoint = new Point(
1148
+ (canvas.width - lineWidth) / 2 - paddingLeft,
1149
+ tile.origin.y
1150
+ );
1151
+ } else if (style.position.startsWith("right")) {
1152
+ paddingLeft = 30 * paddingScale;
1153
+ paddingRight = 10 * paddingScale;
1154
+ axisPoint = new Point(
1155
+ canvas.width - lineWidth - padding - paddingLeft,
1156
+ tile.origin.y
1157
+ );
1158
+ }
1159
+ if (j === 0) {
1160
+ const top = checkIsWindows() ? paddingTop + 6 * style.scale : paddingTop;
1161
+ ctx.fillRect(
1162
+ axisPoint.x,
1163
+ axisPoint.y - paddingTop > 0 ? axisPoint.y - top : 0,
1164
+ paddingLeft + lineWidth + paddingRight,
1165
+ top
1166
+ );
1167
+ }
1168
+ ctx.fillRect(
1169
+ axisPoint.x,
1170
+ axisPoint.y + j * lineHeight,
1171
+ paddingLeft + lineWidth + paddingRight,
1172
+ lineHeight
1173
+ );
1174
+ if (j === tile.lineWidths.length - 1) {
1175
+ const bottom = checkIsIOS() ? paddingBottom + 4 * style.scale : paddingBottom;
1176
+ ctx.fillRect(
1177
+ axisPoint.x,
1178
+ axisPoint.y + (j + 1) * lineHeight,
1179
+ paddingLeft + lineWidth + paddingRight,
1180
+ bottom
1181
+ );
1182
+ }
1183
+ }
1184
+ }
1185
+ }
1186
+ function drawTile(canvas, tile) {
1187
+ return __async(this, null, function* () {
1188
+ if (!tile.style.simplified) {
1189
+ drawBackground(canvas, tile);
1190
+ }
1191
+ for (let i = 0; i < tile.lines.length; i++) {
1192
+ const origin = new Point(
1193
+ tile.origin.x,
1194
+ i * tile.lineHeight + tile.origin.y
1195
+ );
1196
+ yield drawLine(
1197
+ canvas,
1198
+ tile.lines[i],
1199
+ tile.lineWidths[i],
1200
+ origin,
1201
+ tile,
1202
+ i === 0
1203
+ );
1204
+ }
1205
+ });
1206
+ }
1207
+ function drawGroupedWatermarks(canvas, watermarks, groupPosition) {
1208
+ return __async(this, null, function* () {
1209
+ if (!watermarks || watermarks.length === 0) {
1210
+ return;
1211
+ }
1212
+ const tiles = [];
1213
+ let totalHeight = 0;
1214
+ for (let i = 0; i < watermarks.length; i++) {
1215
+ const watermark = watermarks[i];
1216
+ const tile = buildTile(canvas, watermark);
1217
+ if (tile) {
1218
+ tiles.push(tile);
1219
+ totalHeight += tile.size.height + CONTENT_PADDING;
1220
+ }
1221
+ }
1222
+ if (totalHeight <= 0) {
1223
+ console.error("watermarks height " + totalHeight);
1224
+ console.error(JSON.stringify(watermarks));
1225
+ return;
1226
+ }
1227
+ const contentPadding = getCanvasScale(canvas, CONTENT_PADDING);
1228
+ let baseOrigin = new Point(contentPadding, contentPadding);
1229
+ if (groupPosition === "center" /* Center */) {
1230
+ baseOrigin = new Point(
1231
+ contentPadding,
1232
+ Math.round((canvas.height - totalHeight) / 2)
1233
+ );
1234
+ } else if (groupPosition === "bottom" /* Bottom */) {
1235
+ baseOrigin = new Point(
1236
+ contentPadding,
1237
+ canvas.height - totalHeight - contentPadding
1238
+ );
1239
+ } else if (groupPosition === "top" /* Top */) {
1240
+ baseOrigin = new Point(contentPadding, contentPadding);
1241
+ }
1242
+ let lastMarginTop = 0;
1243
+ for (let i = 0; i < tiles.length; i++) {
1244
+ const tile = tiles[i];
1245
+ lastMarginTop += tile.style.paddingTop;
1246
+ tile.origin = new Point(
1247
+ tile.origin.x + baseOrigin.x,
1248
+ lastMarginTop + baseOrigin.y
1249
+ );
1250
+ lastMarginTop += tile.size.height;
1251
+ lastMarginTop += tile.style.paddingBottom;
1252
+ }
1253
+ for (let i = 0; i < tiles.length; i++) {
1254
+ const tile = tiles[i];
1255
+ yield drawTile(canvas, tile);
1256
+ }
1257
+ });
1258
+ }
1259
+ function groupingWatermarks(watermarks) {
1260
+ const topList = [];
1261
+ const centerList = [];
1262
+ const bottomList = [];
1263
+ for (let i = 0; i < watermarks.length; i++) {
1264
+ const watermark = watermarks[i];
1265
+ if (watermark.style.position.endsWith("top")) {
1266
+ topList.push(watermark);
1267
+ } else if (watermark.style.position.endsWith("center")) {
1268
+ centerList.push(watermark);
1269
+ } else if (watermark.style.position.endsWith("bottom")) {
1270
+ bottomList.push(watermark);
1271
+ }
1272
+ }
1273
+ return {
1274
+ ["top" /* Top */]: topList,
1275
+ ["center" /* Center */]: centerList,
1276
+ ["bottom" /* Bottom */]: bottomList
1277
+ };
1278
+ }
1279
+ function renderWatermark(canvas, options) {
1280
+ return __async(this, null, function* () {
1281
+ if (!options.enabled || options.watermarkConfigString === "no") {
1282
+ return Promise.resolve(canvas);
1283
+ }
1284
+ let { watermarkConfigString } = options;
1285
+ if (watermarkConfigString === "default") {
1286
+ watermarkConfigString = "50$lefttop$50*#*time###25$lefttop*#*date";
1287
+ }
1288
+ const watermarks = parseWatermarks(
1289
+ watermarkConfigString,
1290
+ new Size(canvas.width, canvas.height),
1291
+ options.context
1292
+ );
1293
+ const groupedWatermarks = groupingWatermarks(watermarks);
1294
+ yield drawGroupedWatermarks(
1295
+ canvas,
1296
+ groupedWatermarks["top" /* Top */],
1297
+ "top" /* Top */
1298
+ );
1299
+ yield drawGroupedWatermarks(
1300
+ canvas,
1301
+ groupedWatermarks["center" /* Center */],
1302
+ "center" /* Center */
1303
+ );
1304
+ yield drawGroupedWatermarks(
1305
+ canvas,
1306
+ groupedWatermarks["bottom" /* Bottom */],
1307
+ "bottom" /* Bottom */
1308
+ );
1309
+ return Promise.resolve(canvas);
1310
+ });
1311
+ }
1312
+ function checkWatermarkConfigSupported(watermarkConfigString) {
1313
+ const configStringArray = watermarkConfigString.split("###");
1314
+ for (let i = 0; i < configStringArray.length; i++) {
1315
+ const configString = configStringArray[i];
1316
+ const configParts = configString.split("*#*");
1317
+ if (configParts.length === 2) {
1318
+ const styleString = configParts[0];
1319
+ const styleParts = styleString.split("$");
1320
+ if (styleParts.length <= 0) {
1321
+ console.error("水印配置不合法");
1322
+ return false;
1323
+ }
1324
+ }
1325
+ }
1326
+ return true;
1327
+ }
1328
+
1329
+ // src/image/image-util.ts
1330
+ function file2DataURL(file) {
1331
+ return new Promise((resolve, reject) => {
1332
+ const a = new FileReader();
1333
+ a.onload = function(res) {
1334
+ if (res && res.target) {
1335
+ resolve(res.target.result);
1336
+ } else {
1337
+ reject(new Error("读取文件url失败"));
1338
+ }
1339
+ };
1340
+ a.readAsDataURL(file);
1341
+ });
1342
+ }
1343
+ function file2Image(file) {
1344
+ return new Promise((resolve, reject) => {
1345
+ file2DataURL(file).then((url) => {
1346
+ const img = new Image();
1347
+ img.src = url;
1348
+ img.onload = function() {
1349
+ if (img.complete) {
1350
+ resolve(img);
1351
+ } else {
1352
+ reject(new Error("文件加载失败"));
1353
+ }
1354
+ };
1355
+ }).catch(reject);
1356
+ });
1357
+ }
1358
+ function image2file(img, fileName, lastModified, type = "image/jpeg", quality = 0.8) {
1359
+ return image2Canvas(img, type).then((canvas) => {
1360
+ return canvas2File(canvas, fileName, lastModified, type, quality);
1361
+ });
1362
+ }
1363
+ function image2Canvas(img, type, width, height) {
1364
+ const canvas = document.createElement("canvas");
1365
+ const _width = width || img.width;
1366
+ const _height = height || img.height;
1367
+ canvas.width = _width;
1368
+ canvas.height = _height;
1369
+ const ctx = canvas.getContext("2d");
1370
+ if (ctx) {
1371
+ ctx.fillStyle = "transparent";
1372
+ if (type === "image/jpeg") {
1373
+ ctx.fillStyle = "#fff";
1374
+ }
1375
+ ctx.drawImage(img, 0, 0, _width, _height);
1376
+ return Promise.resolve(canvas);
1377
+ } else {
1378
+ return Promise.reject(null);
1379
+ }
1380
+ }
1381
+ function canvas2Image(canvas, type = "image/jpeg", quality = 0.8) {
1382
+ const image = new Image();
1383
+ image.src = canvas.toDataURL(type, quality);
1384
+ return Promise.resolve(image);
1385
+ }
1386
+ function canvas2File(canvas, fileName, lastModified, type = "image/jpeg", quality = 0.8) {
1387
+ return new Promise((resolve, reject) => {
1388
+ canvas.toBlob(
1389
+ (blob) => {
1390
+ if (!blob) {
1391
+ reject(new Error("canvas文件导出失败"));
1392
+ } else {
1393
+ const file = new File([blob], fileName, { lastModified, type });
1394
+ resolve(file);
1395
+ }
1396
+ },
1397
+ type,
1398
+ quality
1399
+ );
1400
+ });
1401
+ }
1402
+ function resize(img, {
1403
+ inputType = "image/jpeg",
1404
+ imageWidth = -1,
1405
+ imageHeight = -1,
1406
+ sideLengthLimit = 640,
1407
+ baseOnShortSide = true,
1408
+ // 是否以短边为基准缩放
1409
+ resultType = "image"
1410
+ }) {
1411
+ let scale = 1;
1412
+ const scaleBaseFunc = baseOnShortSide ? Math.min : Math.max;
1413
+ if (imageWidth > 0 && imageHeight > 0) {
1414
+ scale = sideLengthLimit / scaleBaseFunc(imageWidth, imageHeight);
1415
+ } else if (img.width > 0 && img.height > 0) {
1416
+ scale = sideLengthLimit / scaleBaseFunc(img.width, img.height);
1417
+ imageWidth = img.width;
1418
+ imageHeight = img.height;
1419
+ } else {
1420
+ return Promise.reject(new Error("image width/height 无效"));
1421
+ }
1422
+ const targetWidth = scale > 1 ? imageWidth : imageWidth * scale;
1423
+ const targetHeight = scale > 1 ? imageHeight : imageHeight * scale;
1424
+ if (resultType === "image") {
1425
+ return image2Canvas(img, inputType, targetWidth, targetHeight).then(
1426
+ (canvas) => {
1427
+ return canvas2Image(canvas);
1428
+ }
1429
+ );
1430
+ } else {
1431
+ return image2Canvas(img, inputType, targetWidth, targetHeight);
1432
+ }
1433
+ }
1434
+
1435
+ // src/image/image-processor.ts
1436
+ var MAX_SIDE_LENGTH = 1920;
1437
+ var MIN_SIDE_LENGTH = 480;
1438
+ var PHOTO_FILE_SIZE_LIMIT = 1e3;
1439
+ var DEFAULT_JPEG_QUALITY = 0.8;
1440
+ var MIN_JPEG_QUALITY = 0.1;
1441
+ var JPEG_QUALITY_STEP = 0.05;
1442
+ var ResizeOptions = class {
1443
+ // 最大边长
1444
+ constructor(maxSideLength) {
1445
+ if (checkIsUndefinedOrNull(maxSideLength)) {
1446
+ this.maxSideLength = MAX_SIDE_LENGTH;
1447
+ } else {
1448
+ this.maxSideLength = maxSideLength;
1449
+ if (this.maxSideLength > MAX_SIDE_LENGTH) {
1450
+ this.maxSideLength = MAX_SIDE_LENGTH;
1451
+ } else if (this.maxSideLength < MIN_SIDE_LENGTH) {
1452
+ this.maxSideLength = MIN_SIDE_LENGTH;
1453
+ }
1454
+ }
1455
+ }
1456
+ };
1457
+ var CompressOptions = class {
1458
+ // 图片大小限制,单位kb
1459
+ constructor(photoFileSizeLimit) {
1460
+ if (checkIsUndefinedOrNull(photoFileSizeLimit)) {
1461
+ this.photoFileSizeLimit = PHOTO_FILE_SIZE_LIMIT;
1462
+ } else {
1463
+ this.photoFileSizeLimit = photoFileSizeLimit;
1464
+ }
1465
+ }
1466
+ };
1467
+ var ImageProcessor = class {
1468
+ // 压缩图片的配置
1469
+ constructor(file, resizeOptions, watermarkOptions, compressOptions) {
1470
+ this.file = file;
1471
+ this.resizeOptions = resizeOptions;
1472
+ this.watermarkOptions = watermarkOptions;
1473
+ this.compressOptions = compressOptions;
1474
+ }
1475
+ process() {
1476
+ const filename = this.file.name;
1477
+ const { lastModified } = this.file;
1478
+ let fileType = this.file.type;
1479
+ if (this.file.size > this.compressOptions.photoFileSizeLimit * 1024) {
1480
+ fileType = "image/jpeg";
1481
+ }
1482
+ if (fileType === "image/gif") {
1483
+ fileType = "image/png";
1484
+ }
1485
+ return file2Image(this.file).then((img) => {
1486
+ return resize(img, {
1487
+ inputType: fileType,
1488
+ sideLengthLimit: this.resizeOptions.maxSideLength,
1489
+ resultType: "canvas"
1490
+ });
1491
+ }).then((result) => __async(this, null, function* () {
1492
+ const canvas = result;
1493
+ return yield renderWatermark(canvas, this.watermarkOptions);
1494
+ })).then((canvas) => {
1495
+ const exportFilename = filename.replace(/\./g, "-") + ".jpg";
1496
+ return this.compress(
1497
+ canvas,
1498
+ exportFilename,
1499
+ lastModified,
1500
+ fileType,
1501
+ DEFAULT_JPEG_QUALITY
1502
+ );
1503
+ });
1504
+ }
1505
+ compress(canvas, filename, lastModified, type, quality) {
1506
+ return canvas2File(canvas, filename, lastModified, type, quality).then(
1507
+ (file) => {
1508
+ if (file.size > this.compressOptions.photoFileSizeLimit * 1024 && quality - JPEG_QUALITY_STEP > MIN_JPEG_QUALITY) {
1509
+ return this.compress(
1510
+ canvas,
1511
+ filename,
1512
+ lastModified,
1513
+ type,
1514
+ quality - JPEG_QUALITY_STEP
1515
+ );
1516
+ } else {
1517
+ return Promise.resolve(file);
1518
+ }
1519
+ }
1520
+ );
1521
+ }
1522
+ };