@wapri/preview 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +78 -2
- package/dist/constant/index.d.ts +51 -0
- package/dist/constant/index.js +58 -0
- package/dist/core/context.d.ts +21 -0
- package/dist/core/context.js +78 -0
- package/dist/core/lazy.d.ts +43 -0
- package/dist/core/lazy.js +175 -0
- package/dist/core/source.d.ts +20 -0
- package/dist/core/source.js +118 -0
- package/dist/core/style.d.ts +8 -0
- package/dist/core/style.js +20 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +17 -0
- package/dist/pdf/index.d.ts +8 -0
- package/dist/pdf/index.js +364 -0
- package/dist/pdf/load.d.ts +15 -0
- package/dist/pdf/load.js +78 -0
- package/dist/previewFile.d.ts +18 -0
- package/dist/previewFile.js +54 -0
- package/dist/text/index.d.ts +8 -0
- package/dist/text/index.js +118 -0
- package/dist/types/common.d.ts +68 -0
- package/dist/types/common.js +1 -0
- package/dist/types/pdf.d.ts +23 -0
- package/dist/types/pdf.js +1 -0
- package/dist/types/text.d.ts +14 -0
- package/dist/types/text.js +1 -0
- package/dist/types/word.d.ts +57 -0
- package/dist/types/word.js +1 -0
- package/dist/types/xlsx.d.ts +45 -0
- package/dist/types/xlsx.js +1 -0
- package/dist/vendor/pdf.worker.min.mjs +21 -0
- package/dist/vendor/xlsx.full.min.js +22 -0
- package/dist/word/font.d.ts +20 -0
- package/dist/word/font.js +132 -0
- package/dist/word/index.d.ts +8 -0
- package/dist/word/index.js +237 -0
- package/dist/word/layout.d.ts +56 -0
- package/dist/word/layout.js +365 -0
- package/dist/word/parse.d.ts +6 -0
- package/dist/word/parse.js +315 -0
- package/dist/word/render.d.ts +17 -0
- package/dist/word/render.js +210 -0
- package/dist/xlsx/index.d.ts +8 -0
- package/dist/xlsx/index.js +195 -0
- package/dist/xlsx/parse.d.ts +14 -0
- package/dist/xlsx/parse.js +177 -0
- package/dist/xlsx/render.d.ts +17 -0
- package/dist/xlsx/render.js +144 -0
- package/dist/xlsx/workerSource.d.ts +7 -0
- package/dist/xlsx/workerSource.js +8 -0
- package/package.json +28 -3
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文本度量与绘制:优先用 opentype.js 读取内嵌字体做精确测量,缺失时回退 Canvas measureText。
|
|
3
|
+
*/
|
|
4
|
+
export interface FontStyle {
|
|
5
|
+
fontSize: number;
|
|
6
|
+
bold?: boolean;
|
|
7
|
+
italic?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface TextMetricsProvider {
|
|
10
|
+
/** 文本宽度,px */
|
|
11
|
+
measure(text: string, style: FontStyle): number;
|
|
12
|
+
/** 绘制文本,(x, y) 为基线起点 */
|
|
13
|
+
draw(ctx: CanvasRenderingContext2D, text: string, x: number, y: number, style: FontStyle): void;
|
|
14
|
+
/** 字体是否来自内嵌字体 */
|
|
15
|
+
readonly hasEmbeddedFont: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 加载内嵌字体;解析失败的字体直接忽略,整体回退 Canvas 测量。
|
|
19
|
+
*/
|
|
20
|
+
export declare const createFontMetrics: (fontBuffers?: ArrayBuffer[], family?: string) => Promise<TextMetricsProvider>;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
4
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
5
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
6
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
7
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
8
|
+
/**
|
|
9
|
+
* 文本度量与绘制:优先用 opentype.js 读取内嵌字体做精确测量,缺失时回退 Canvas measureText。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { parse as parseFont } from 'opentype.js';
|
|
13
|
+
var isBoldFont = function isBoldFont(font) {
|
|
14
|
+
var _ref, _font$names$fontSubfa, _font$names$fontSubfa2, _font$names$fullName;
|
|
15
|
+
var name = (_ref = (_font$names$fontSubfa = (_font$names$fontSubfa2 = font.names.fontSubfamily) === null || _font$names$fontSubfa2 === void 0 ? void 0 : _font$names$fontSubfa2.en) !== null && _font$names$fontSubfa !== void 0 ? _font$names$fontSubfa : (_font$names$fullName = font.names.fullName) === null || _font$names$fullName === void 0 ? void 0 : _font$names$fullName.en) !== null && _ref !== void 0 ? _ref : '';
|
|
16
|
+
return /bold|heavy|black|semibold/i.test(name);
|
|
17
|
+
};
|
|
18
|
+
var toFontString = function toFontString(style, family) {
|
|
19
|
+
return "".concat(style.italic ? 'italic ' : '').concat(style.bold ? 'bold ' : '').concat(style.fontSize, "px ").concat(family);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** 用 Canvas 测量,作为没有内嵌字体时的兜底 */
|
|
23
|
+
var createCanvasMetrics = function createCanvasMetrics(family) {
|
|
24
|
+
var canvas = document.createElement('canvas');
|
|
25
|
+
var context = canvas.getContext('2d');
|
|
26
|
+
return {
|
|
27
|
+
hasEmbeddedFont: false,
|
|
28
|
+
measure: function measure(text, style) {
|
|
29
|
+
if (!context) return text.length * style.fontSize * 0.6;
|
|
30
|
+
context.font = toFontString(style, family);
|
|
31
|
+
return context.measureText(text).width;
|
|
32
|
+
},
|
|
33
|
+
draw: function draw(ctx, text, x, y, style) {
|
|
34
|
+
ctx.font = toFontString(style, family);
|
|
35
|
+
ctx.fillText(text, x, y);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 加载内嵌字体;解析失败的字体直接忽略,整体回退 Canvas 测量。
|
|
42
|
+
*/
|
|
43
|
+
export var createFontMetrics = /*#__PURE__*/function () {
|
|
44
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
45
|
+
var fontBuffers,
|
|
46
|
+
family,
|
|
47
|
+
loaded,
|
|
48
|
+
cache,
|
|
49
|
+
_iterator,
|
|
50
|
+
_step,
|
|
51
|
+
buffer,
|
|
52
|
+
font,
|
|
53
|
+
fallback,
|
|
54
|
+
pick,
|
|
55
|
+
_args = arguments;
|
|
56
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
57
|
+
while (1) switch (_context.prev = _context.next) {
|
|
58
|
+
case 0:
|
|
59
|
+
fontBuffers = _args.length > 0 && _args[0] !== undefined ? _args[0] : [];
|
|
60
|
+
family = _args.length > 1 && _args[1] !== undefined ? _args[1] : 'sans-serif';
|
|
61
|
+
loaded = [];
|
|
62
|
+
cache = new Map();
|
|
63
|
+
_iterator = _createForOfIteratorHelper(fontBuffers);
|
|
64
|
+
try {
|
|
65
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
66
|
+
buffer = _step.value;
|
|
67
|
+
try {
|
|
68
|
+
font = parseFont(buffer);
|
|
69
|
+
loaded.push({
|
|
70
|
+
font: font,
|
|
71
|
+
bold: isBoldFont(font)
|
|
72
|
+
});
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.warn('[preview] 字体解析失败,已忽略', error);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} catch (err) {
|
|
78
|
+
_iterator.e(err);
|
|
79
|
+
} finally {
|
|
80
|
+
_iterator.f();
|
|
81
|
+
}
|
|
82
|
+
if (!(loaded.length === 0)) {
|
|
83
|
+
_context.next = 8;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
return _context.abrupt("return", createCanvasMetrics(family));
|
|
87
|
+
case 8:
|
|
88
|
+
fallback = createCanvasMetrics(family);
|
|
89
|
+
pick = function pick(style) {
|
|
90
|
+
var target = loaded.find(function (item) {
|
|
91
|
+
return item.bold === Boolean(style.bold);
|
|
92
|
+
});
|
|
93
|
+
return target !== null && target !== void 0 ? target : loaded[0];
|
|
94
|
+
};
|
|
95
|
+
return _context.abrupt("return", {
|
|
96
|
+
hasEmbeddedFont: true,
|
|
97
|
+
measure: function measure(text, style) {
|
|
98
|
+
var key = "".concat(style.bold ? 1 : 0, "|").concat(style.fontSize, "|").concat(text);
|
|
99
|
+
var cached = cache.get(key);
|
|
100
|
+
if (cached !== undefined) return cached;
|
|
101
|
+
var _pick = pick(style),
|
|
102
|
+
font = _pick.font;
|
|
103
|
+
var width = 0;
|
|
104
|
+
try {
|
|
105
|
+
width = font.getAdvanceWidth(text, style.fontSize);
|
|
106
|
+
} catch (_unused) {
|
|
107
|
+
width = fallback.measure(text, style);
|
|
108
|
+
}
|
|
109
|
+
if (cache.size > 5000) cache.clear();
|
|
110
|
+
cache.set(key, width);
|
|
111
|
+
return width;
|
|
112
|
+
},
|
|
113
|
+
draw: function draw(ctx, text, x, y, style) {
|
|
114
|
+
var _pick2 = pick(style),
|
|
115
|
+
font = _pick2.font;
|
|
116
|
+
try {
|
|
117
|
+
font.draw(ctx, text, x, y, style.fontSize);
|
|
118
|
+
} catch (_unused2) {
|
|
119
|
+
fallback.draw(ctx, text, x, y, style);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
case 11:
|
|
124
|
+
case "end":
|
|
125
|
+
return _context.stop();
|
|
126
|
+
}
|
|
127
|
+
}, _callee);
|
|
128
|
+
}));
|
|
129
|
+
return function createFontMetrics() {
|
|
130
|
+
return _ref2.apply(this, arguments);
|
|
131
|
+
};
|
|
132
|
+
}();
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Word 预览:mammoth 解析、opentype 排版、Canvas 分页绘制。
|
|
3
|
+
*/
|
|
4
|
+
import type { ResolvedSource } from '../core/source';
|
|
5
|
+
import type { PreviewHandle, PreviewInput } from '../types/common';
|
|
6
|
+
import type { WordPreviewOptions } from '../types/word';
|
|
7
|
+
export declare const renderWord: (source: ResolvedSource, options: WordPreviewOptions) => Promise<PreviewHandle<'word'>>;
|
|
8
|
+
export declare const previewWord: (input: PreviewInput, options: WordPreviewOptions) => Promise<PreviewHandle<'word'>>;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
+
/**
|
|
6
|
+
* Word 预览:mammoth 解析、opentype 排版、Canvas 分页绘制。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { PREVIEW_CLASS, WORD_PAGE_HEIGHT, WORD_PAGE_PADDING, WORD_PAGE_WIDTH } from "../constant";
|
|
10
|
+
import { createPreviewContext } from "../core/context";
|
|
11
|
+
import { LazyRenderPool } from "../core/lazy";
|
|
12
|
+
import { resolvePreviewSource } from "../core/source";
|
|
13
|
+
import { createFontMetrics } from "./font";
|
|
14
|
+
import { layoutDocument } from "./layout";
|
|
15
|
+
import { parseWord } from "./parse";
|
|
16
|
+
import { renderPageToCanvas, renderThumbnail } from "./render";
|
|
17
|
+
var THUMBNAIL_WIDTH = 72;
|
|
18
|
+
export var renderWord = /*#__PURE__*/function () {
|
|
19
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(source, options) {
|
|
20
|
+
var _options$pageWidth, _options$pageHeight, _options$padding, _options$maxPixelRati;
|
|
21
|
+
var context, metrics, model, _options$fontSize, baseWidth, baseHeight, basePadding, pixelRatio, slots, pool, thumbPool, thumbBar, paintOptions, buildPages, buildThumbnails;
|
|
22
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
23
|
+
while (1) switch (_context.prev = _context.next) {
|
|
24
|
+
case 0:
|
|
25
|
+
context = createPreviewContext(options);
|
|
26
|
+
context.emitProgress({
|
|
27
|
+
stage: 'parsing',
|
|
28
|
+
loaded: 0,
|
|
29
|
+
total: 0
|
|
30
|
+
});
|
|
31
|
+
_context.prev = 2;
|
|
32
|
+
_context.next = 5;
|
|
33
|
+
return createFontMetrics(options.fonts, context.theme.fontFamily);
|
|
34
|
+
case 5:
|
|
35
|
+
metrics = _context.sent;
|
|
36
|
+
_context.next = 12;
|
|
37
|
+
break;
|
|
38
|
+
case 8:
|
|
39
|
+
_context.prev = 8;
|
|
40
|
+
_context.t0 = _context["catch"](2);
|
|
41
|
+
context.fail(_context.t0);
|
|
42
|
+
throw _context.t0;
|
|
43
|
+
case 12:
|
|
44
|
+
_context.prev = 12;
|
|
45
|
+
_context.next = 15;
|
|
46
|
+
return parseWord(source.buffer, (_options$fontSize = options.fontSize) !== null && _options$fontSize !== void 0 ? _options$fontSize : context.theme.fontSize + 2);
|
|
47
|
+
case 15:
|
|
48
|
+
model = _context.sent;
|
|
49
|
+
_context.next = 22;
|
|
50
|
+
break;
|
|
51
|
+
case 18:
|
|
52
|
+
_context.prev = 18;
|
|
53
|
+
_context.t1 = _context["catch"](12);
|
|
54
|
+
context.fail(_context.t1);
|
|
55
|
+
throw _context.t1;
|
|
56
|
+
case 22:
|
|
57
|
+
baseWidth = (_options$pageWidth = options.pageWidth) !== null && _options$pageWidth !== void 0 ? _options$pageWidth : WORD_PAGE_WIDTH;
|
|
58
|
+
baseHeight = (_options$pageHeight = options.pageHeight) !== null && _options$pageHeight !== void 0 ? _options$pageHeight : WORD_PAGE_HEIGHT;
|
|
59
|
+
basePadding = (_options$padding = options.padding) !== null && _options$padding !== void 0 ? _options$padding : WORD_PAGE_PADDING;
|
|
60
|
+
pixelRatio = (_options$maxPixelRati = options.maxPixelRatio) !== null && _options$maxPixelRati !== void 0 ? _options$maxPixelRati : 2;
|
|
61
|
+
slots = [];
|
|
62
|
+
pool = new LazyRenderPool({
|
|
63
|
+
root: context.viewport,
|
|
64
|
+
keepAlive: false,
|
|
65
|
+
maxConcurrent: 1
|
|
66
|
+
});
|
|
67
|
+
paintOptions = {
|
|
68
|
+
pixelRatio: pixelRatio,
|
|
69
|
+
paper: context.theme.paper,
|
|
70
|
+
color: context.theme.color,
|
|
71
|
+
metrics: metrics
|
|
72
|
+
};
|
|
73
|
+
buildPages = function buildPages() {
|
|
74
|
+
var availableWidth = context.viewport.clientWidth || context.container.clientWidth || 360;
|
|
75
|
+
var pageWidth = Math.max(240, Math.min(baseWidth, availableWidth - 24));
|
|
76
|
+
var scale = pageWidth / baseWidth;
|
|
77
|
+
var pageHeight = baseHeight * scale;
|
|
78
|
+
var pages = layoutDocument(model, {
|
|
79
|
+
pageWidth: pageWidth,
|
|
80
|
+
pageHeight: pageHeight,
|
|
81
|
+
padding: basePadding * scale,
|
|
82
|
+
fontSize: model.fontSize * scale,
|
|
83
|
+
lineHeight: context.theme.lineHeight,
|
|
84
|
+
color: context.theme.color,
|
|
85
|
+
mutedColor: context.theme.mutedColor,
|
|
86
|
+
metrics: metrics
|
|
87
|
+
});
|
|
88
|
+
pool.destroy();
|
|
89
|
+
pool = new LazyRenderPool({
|
|
90
|
+
root: context.viewport,
|
|
91
|
+
keepAlive: false,
|
|
92
|
+
maxConcurrent: 1
|
|
93
|
+
});
|
|
94
|
+
context.viewport.textContent = '';
|
|
95
|
+
slots = pages.map(function (page, index) {
|
|
96
|
+
var element = document.createElement('div');
|
|
97
|
+
var canvas = document.createElement('canvas');
|
|
98
|
+
element.className = PREVIEW_CLASS.page;
|
|
99
|
+
element.style.width = "".concat(page.width, "px");
|
|
100
|
+
element.style.height = "".concat(page.height, "px");
|
|
101
|
+
canvas.className = PREVIEW_CLASS.pageCanvas;
|
|
102
|
+
element.appendChild(canvas);
|
|
103
|
+
var label = document.createElement('span');
|
|
104
|
+
label.className = PREVIEW_CLASS.pageLabel;
|
|
105
|
+
label.textContent = "".concat(index + 1, " / ").concat(pages.length);
|
|
106
|
+
element.appendChild(label);
|
|
107
|
+
context.viewport.appendChild(element);
|
|
108
|
+
return {
|
|
109
|
+
element: element,
|
|
110
|
+
canvas: canvas,
|
|
111
|
+
page: page
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
slots.forEach(function (slot) {
|
|
115
|
+
pool.register({
|
|
116
|
+
element: slot.element,
|
|
117
|
+
render: function render() {
|
|
118
|
+
return renderPageToCanvas(slot.page, slot.canvas, paintOptions);
|
|
119
|
+
},
|
|
120
|
+
release: function release() {
|
|
121
|
+
slot.canvas.width = 0;
|
|
122
|
+
slot.canvas.height = 0;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
buildThumbnails(pages);
|
|
127
|
+
context.emitProgress({
|
|
128
|
+
stage: 'ready',
|
|
129
|
+
loaded: pages.length,
|
|
130
|
+
total: pages.length
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
buildThumbnails = function buildThumbnails(pages) {
|
|
134
|
+
var _thumbPool, _thumbBar;
|
|
135
|
+
if (!options.thumbnails) return;
|
|
136
|
+
(_thumbPool = thumbPool) === null || _thumbPool === void 0 || _thumbPool.destroy();
|
|
137
|
+
(_thumbBar = thumbBar) === null || _thumbBar === void 0 || _thumbBar.remove();
|
|
138
|
+
var bar = document.createElement('div');
|
|
139
|
+
bar.className = PREVIEW_CLASS.thumbnails;
|
|
140
|
+
context.root.appendChild(bar);
|
|
141
|
+
thumbBar = bar;
|
|
142
|
+
thumbPool = new LazyRenderPool({
|
|
143
|
+
root: bar,
|
|
144
|
+
keepAlive: true,
|
|
145
|
+
maxConcurrent: 1
|
|
146
|
+
});
|
|
147
|
+
pages.forEach(function (page, index) {
|
|
148
|
+
var _thumbPool2;
|
|
149
|
+
var item = document.createElement('button');
|
|
150
|
+
var canvas = document.createElement('canvas');
|
|
151
|
+
item.type = 'button';
|
|
152
|
+
item.className = PREVIEW_CLASS.thumbnail;
|
|
153
|
+
item.title = "\u7B2C ".concat(index + 1, " \u9875");
|
|
154
|
+
canvas.style.width = "".concat(THUMBNAIL_WIDTH, "px");
|
|
155
|
+
item.appendChild(canvas);
|
|
156
|
+
bar.appendChild(item);
|
|
157
|
+
item.addEventListener('click', function () {
|
|
158
|
+
var _slots$index;
|
|
159
|
+
(_slots$index = slots[index]) === null || _slots$index === void 0 || _slots$index.element.scrollIntoView({
|
|
160
|
+
block: 'start'
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
(_thumbPool2 = thumbPool) === null || _thumbPool2 === void 0 || _thumbPool2.register({
|
|
164
|
+
element: item,
|
|
165
|
+
render: function render() {
|
|
166
|
+
return renderThumbnail(page, canvas, paintOptions, THUMBNAIL_WIDTH);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
buildPages();
|
|
172
|
+
context.onDestroy(function () {
|
|
173
|
+
var _thumbPool3;
|
|
174
|
+
pool.destroy();
|
|
175
|
+
(_thumbPool3 = thumbPool) === null || _thumbPool3 === void 0 || _thumbPool3.destroy();
|
|
176
|
+
});
|
|
177
|
+
_context.t2 = context.container;
|
|
178
|
+
_context.t3 = function renderAll() {
|
|
179
|
+
pool.renderAll();
|
|
180
|
+
};
|
|
181
|
+
_context.t4 = function scrollTo(index) {
|
|
182
|
+
var _slots$index2;
|
|
183
|
+
(_slots$index2 = slots[index]) === null || _slots$index2 === void 0 || _slots$index2.element.scrollIntoView({
|
|
184
|
+
block: 'start'
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
_context.t5 = function refresh() {
|
|
188
|
+
buildPages();
|
|
189
|
+
};
|
|
190
|
+
_context.t6 = function destroy() {
|
|
191
|
+
context.destroy();
|
|
192
|
+
};
|
|
193
|
+
return _context.abrupt("return", {
|
|
194
|
+
kind: 'word',
|
|
195
|
+
container: _context.t2,
|
|
196
|
+
get unitCount() {
|
|
197
|
+
return slots.length;
|
|
198
|
+
},
|
|
199
|
+
get renderedCount() {
|
|
200
|
+
return pool.renderedCount;
|
|
201
|
+
},
|
|
202
|
+
renderAll: _context.t3,
|
|
203
|
+
scrollTo: _context.t4,
|
|
204
|
+
refresh: _context.t5,
|
|
205
|
+
destroy: _context.t6
|
|
206
|
+
});
|
|
207
|
+
case 39:
|
|
208
|
+
case "end":
|
|
209
|
+
return _context.stop();
|
|
210
|
+
}
|
|
211
|
+
}, _callee, null, [[2, 8], [12, 18]]);
|
|
212
|
+
}));
|
|
213
|
+
return function renderWord(_x, _x2) {
|
|
214
|
+
return _ref.apply(this, arguments);
|
|
215
|
+
};
|
|
216
|
+
}();
|
|
217
|
+
export var previewWord = /*#__PURE__*/function () {
|
|
218
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(input, options) {
|
|
219
|
+
var source;
|
|
220
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
221
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
222
|
+
case 0:
|
|
223
|
+
_context2.next = 2;
|
|
224
|
+
return resolvePreviewSource(input);
|
|
225
|
+
case 2:
|
|
226
|
+
source = _context2.sent;
|
|
227
|
+
return _context2.abrupt("return", renderWord(source, options));
|
|
228
|
+
case 4:
|
|
229
|
+
case "end":
|
|
230
|
+
return _context2.stop();
|
|
231
|
+
}
|
|
232
|
+
}, _callee2);
|
|
233
|
+
}));
|
|
234
|
+
return function previewWord(_x3, _x4) {
|
|
235
|
+
return _ref2.apply(this, arguments);
|
|
236
|
+
};
|
|
237
|
+
}();
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Word 精简排版引擎:断行、表格布局、分页,产出可绘制到 Canvas 的盒模型。
|
|
3
|
+
*
|
|
4
|
+
* 字体度量来自 opentype.js(内嵌字体)或 Canvas measureText,两者都通过 TextMetricsProvider 抽象。
|
|
5
|
+
*/
|
|
6
|
+
import type { WordDocumentModel } from '../types/word';
|
|
7
|
+
import type { TextMetricsProvider } from './font';
|
|
8
|
+
export interface TextBox {
|
|
9
|
+
type: 'text';
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
text: string;
|
|
15
|
+
fontSize: number;
|
|
16
|
+
bold?: boolean;
|
|
17
|
+
italic?: boolean;
|
|
18
|
+
underline?: boolean;
|
|
19
|
+
color?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface ImageBox {
|
|
22
|
+
type: 'image';
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
width: number;
|
|
26
|
+
height: number;
|
|
27
|
+
src: string;
|
|
28
|
+
}
|
|
29
|
+
export interface RectBox {
|
|
30
|
+
type: 'rect';
|
|
31
|
+
x: number;
|
|
32
|
+
y: number;
|
|
33
|
+
width: number;
|
|
34
|
+
height: number;
|
|
35
|
+
stroke?: string;
|
|
36
|
+
fill?: string;
|
|
37
|
+
lineWidth?: number;
|
|
38
|
+
}
|
|
39
|
+
export type LayoutBox = TextBox | ImageBox | RectBox;
|
|
40
|
+
export interface LayoutPage {
|
|
41
|
+
width: number;
|
|
42
|
+
height: number;
|
|
43
|
+
boxes: LayoutBox[];
|
|
44
|
+
}
|
|
45
|
+
export interface LayoutConfig {
|
|
46
|
+
pageWidth: number;
|
|
47
|
+
pageHeight: number;
|
|
48
|
+
padding: number;
|
|
49
|
+
fontSize: number;
|
|
50
|
+
lineHeight: number;
|
|
51
|
+
color: string;
|
|
52
|
+
mutedColor: string;
|
|
53
|
+
metrics: TextMetricsProvider;
|
|
54
|
+
}
|
|
55
|
+
/** 把文档模型排版成页面盒模型 */
|
|
56
|
+
export declare const layoutDocument: (model: WordDocumentModel, config: LayoutConfig) => LayoutPage[];
|