Skip to content

Commit 820f253

Browse files
author
hexingyu
committed
fix: 修复ofd格式解析异常
1 parent c426b69 commit 820f253

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

src/components/HelloWorld.vue

+1
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ export default {
488488
that.loading = false;
489489
},
490490
fail(error) {
491+
console.log(error)
491492
that.loading = false;
492493
that.$alert('OFD打开失败', error, {
493494
confirmButtonText: '确定',

src/utils/ofd/ofd_parser.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,12 @@ const getMultiMediaRes = async function (zip, res, doc) {
309309
const jbig2 = await parseJbig2ImageFromZip(zip, file);
310310
multiMediaResObj[item['@_ID']] = jbig2;
311311
} else {
312-
const img = await parseOtherImageFromZip(zip, file);
313-
multiMediaResObj[item['@_ID']] = {img, 'format': 'png'};
312+
try {
313+
const img = await parseOtherImageFromZip(zip, file);
314+
multiMediaResObj[item['@_ID']] = {img, 'format': 'png'};
315+
} catch (e) {
316+
console.error(e, zip, file)
317+
}
314318
}
315319
} else {
316320
multiMediaResObj[item['@_ID']] = file;

src/utils/ofd/ofd_render.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,15 @@ const renderLayer = function (pageDiv, fontResObj, drawParamResObj, multiMediaRe
241241
let drawParam = layer?.['@_DrawParam'];
242242
if (drawParam && Object.keys(drawParamResObj).length > 0 && drawParamResObj[drawParam]) {
243243
if (drawParamResObj[drawParam]['relative']) {
244-
drawParam = drawParamResObj[drawParam]['relative'];
245-
if (drawParamResObj[drawParam]['FillColor']) {
246-
fillColor = parseColor(drawParamResObj[drawParam]['FillColor']);
244+
const relaDrawParam = drawParamResObj[drawParam]['relative'];
245+
if (drawParamResObj[relaDrawParam]['FillColor']) {
246+
fillColor = parseColor(drawParamResObj[relaDrawParam]['FillColor']);
247247
}
248-
if (drawParamResObj[drawParam]['StrokeColor']) {
249-
strokeColor = parseColor(drawParamResObj[drawParam]['StrokeColor']);
248+
if (drawParamResObj[relaDrawParam]['StrokeColor']) {
249+
strokeColor = parseColor(drawParamResObj[relaDrawParam]['StrokeColor']);
250250
}
251-
if (drawParamResObj[drawParam]['LineWidth']) {
252-
lineWith = converterDpi(drawParamResObj[drawParam]['LineWidth']);
251+
if (drawParamResObj[relaDrawParam]['LineWidth']) {
252+
lineWith = converterDpi(drawParamResObj[relaDrawParam]['LineWidth']);
253253
}
254254
}
255255
if (drawParamResObj[drawParam]['FillColor']) {
@@ -358,7 +358,7 @@ export const renderTextObject = function (fontResObj, textObject, defaultFillCol
358358
const hScale = textObject['@_HScale'];
359359
const font = textObject['@_Font'];
360360
const weight = textObject['@_Weight'];
361-
const size = converterDpi(parseFloat(textObject['@_Size']));
361+
const size = converterDpi(parseFloat(textObject['@_Size'])) || 14;
362362
let array = [];
363363
array = array.concat(textObject['ofd:TextCode']);
364364
const textCodePointList = calTextPoint(array);
@@ -375,8 +375,8 @@ export const renderTextObject = function (fontResObj, textObject, defaultFillCol
375375
for (const textCodePoint of textCodePointList) {
376376
if (textCodePoint && !isNaN(textCodePoint.x)) {
377377
let text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
378-
text.setAttribute('x', textCodePoint.x);
379-
text.setAttribute('y', textCodePoint.y);
378+
text.setAttribute('x', textCodePoint.deltaX.join(' '));
379+
text.setAttribute('y', textCodePoint.deltaY.join(' '));
380380
text.innerHTML = textCodePoint.text;
381381
if (ctm) {
382382
const ctms = parseCtm(ctm);

src/utils/ofd/ofd_util.js

+21-17
Original file line numberDiff line numberDiff line change
@@ -194,24 +194,28 @@ export const calTextPoint = function (textCodes) {
194194
textStr += '';
195195
textStr = decodeHtml(textStr);
196196
textStr = textStr.replace(/ /g, ' ');
197-
for (let i = 0; i < textStr.length; i++) {
198-
if (i > 0 && deltaXList.length > 0) {
199-
x += deltaXList[(i - 1)];
200-
}
201-
if (i > 0 && deltaYList.length > 0) {
202-
y += deltaYList[(i - 1)];
203-
}
204-
let text = textStr.substring(i, i + 1);
205-
let filterPointY = textCodePointList.filter((textCodePoint) => {
206-
return textCodePoint.y == converterDpi(y)
207-
});
208-
if (filterPointY && filterPointY.length) { // Y坐标相同,无需再创建text标签
209-
filterPointY[0].text += text;
210-
} else {
211-
let textCodePoint = { 'x': converterDpi(x), 'y': converterDpi(y), 'text': text };
212-
textCodePointList.push(textCodePoint);
213-
}
197+
const maxLen = Math.max(deltaXList.length, deltaYList.length)
198+
let pointX = converterDpi(x)
199+
let pointY = converterDpi(y)
200+
201+
// 修复字间距过宽的问题
202+
const xList = [pointX]
203+
const yList = [pointY]
204+
let posX = parseFloat(pointX)
205+
let posY = parseFloat(pointY)
206+
for (let i = 0; i < maxLen; i++) {
207+
posX += converterDpi(deltaXList[i] || 0)
208+
posY += converterDpi(deltaYList[i] || 0)
209+
xList.push(posX)
210+
yList.push(posY)
214211
}
212+
textCodePointList.push({
213+
x: pointX,
214+
y: pointY,
215+
deltaX: xList,
216+
deltaY: yList,
217+
text: textStr
218+
});
215219
}
216220
}
217221
return textCodePointList;

0 commit comments

Comments
 (0)