Skip to content

fix: 修复ofd格式解析异常 #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: js
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ export default {
that.loading = false;
},
fail(error) {
console.log(error)
that.loading = false;
that.$alert('OFD打开失败', error, {
confirmButtonText: '确定',
Expand Down
8 changes: 6 additions & 2 deletions src/utils/ofd/ofd_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,12 @@ const getMultiMediaRes = async function (zip, res, doc) {
const jbig2 = await parseJbig2ImageFromZip(zip, file);
multiMediaResObj[item['@_ID']] = jbig2;
} else {
const img = await parseOtherImageFromZip(zip, file);
multiMediaResObj[item['@_ID']] = {img, 'format': 'png'};
try {
const img = await parseOtherImageFromZip(zip, file);
multiMediaResObj[item['@_ID']] = {img, 'format': 'png'};
} catch (e) {
console.error(e, zip, file)
}
}
} else {
multiMediaResObj[item['@_ID']] = file;
Expand Down
18 changes: 9 additions & 9 deletions src/utils/ofd/ofd_render.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ const renderLayer = function (pageDiv, fontResObj, drawParamResObj, multiMediaRe
let drawParam = layer?.['@_DrawParam'];
if (drawParam && Object.keys(drawParamResObj).length > 0 && drawParamResObj[drawParam]) {
if (drawParamResObj[drawParam]['relative']) {
drawParam = drawParamResObj[drawParam]['relative'];
if (drawParamResObj[drawParam]['FillColor']) {
fillColor = parseColor(drawParamResObj[drawParam]['FillColor']);
const relaDrawParam = drawParamResObj[drawParam]['relative'];
if (drawParamResObj[relaDrawParam]['FillColor']) {
fillColor = parseColor(drawParamResObj[relaDrawParam]['FillColor']);
}
if (drawParamResObj[drawParam]['StrokeColor']) {
strokeColor = parseColor(drawParamResObj[drawParam]['StrokeColor']);
if (drawParamResObj[relaDrawParam]['StrokeColor']) {
strokeColor = parseColor(drawParamResObj[relaDrawParam]['StrokeColor']);
}
if (drawParamResObj[drawParam]['LineWidth']) {
lineWith = converterDpi(drawParamResObj[drawParam]['LineWidth']);
if (drawParamResObj[relaDrawParam]['LineWidth']) {
lineWith = converterDpi(drawParamResObj[relaDrawParam]['LineWidth']);
}
}
if (drawParamResObj[drawParam]['FillColor']) {
Expand Down Expand Up @@ -375,8 +375,8 @@ export const renderTextObject = function (fontResObj, textObject, defaultFillCol
for (const textCodePoint of textCodePointList) {
if (textCodePoint && !isNaN(textCodePoint.x)) {
let text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
text.setAttribute('x', textCodePoint.x);
text.setAttribute('y', textCodePoint.y);
text.setAttribute('x', textCodePoint.xList.join(' '));
text.setAttribute('y', textCodePoint.yList.join(' '));
text.innerHTML = textCodePoint.text;
if (ctm) {
const ctms = parseCtm(ctm);
Expand Down
38 changes: 21 additions & 17 deletions src/utils/ofd/ofd_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,24 +194,28 @@ export const calTextPoint = function (textCodes) {
textStr += '';
textStr = decodeHtml(textStr);
textStr = textStr.replace(/ /g, ' ');
for (let i = 0; i < textStr.length; i++) {
if (i > 0 && deltaXList.length > 0) {
x += deltaXList[(i - 1)];
}
if (i > 0 && deltaYList.length > 0) {
y += deltaYList[(i - 1)];
}
let text = textStr.substring(i, i + 1);
let filterPointY = textCodePointList.filter((textCodePoint) => {
return textCodePoint.y == converterDpi(y)
});
if (filterPointY && filterPointY.length) { // Y坐标相同,无需再创建text标签
filterPointY[0].text += text;
} else {
let textCodePoint = { 'x': converterDpi(x), 'y': converterDpi(y), 'text': text };
textCodePointList.push(textCodePoint);
}
const maxLen = Math.max(deltaXList.length, deltaYList.length)
let pointX = converterDpi(x)
let pointY = converterDpi(y)

// 修复字间距过宽的问题
const xList = [pointX]
const yList = [pointY]
let posX = parseFloat(pointX)
let posY = parseFloat(pointY)
for (let i = 0; i < maxLen; i++) {
posX += converterDpi(deltaXList[i] || 0)
posY += converterDpi(deltaYList[i] || 0)
xList.push(posX)
yList.push(posY)
}
textCodePointList.push({
x: pointX,
y: pointY,
xList,
yList,
text: textStr
});
}
}
return textCodePointList;
Expand Down