服务:Spring Boot 3.2 + docx4j 8.3.9,POST /api/v3/server_office_file/file/parse
接受 .docx,返回 HTML(公式 → LaTeX,图片 → RustFS 外链)
公式支持三类来源:
- OMML(Word 2007+ 公式) → Python omml_mathml2latex
- MathType OLE 对象 → Ruby mathtype2mathml → MathML → LaTeX
- Word EQ 域(老教辅主力) → EqToLatexUtil(本次新增)
本次改动:
1. 新增 java-svc/.../core/EqToLatexUtil.java:
移植自 jby backend-services office-core,EQ 语法 → LaTeX
(\f→\dfrac、\r→\sqrt、\i→\int/\sum/\prod、\s→^{}/_{}、\b→\left\right、
\a→array、\x→\boxed/\overline、\o\al 堆叠、圈码、化学符号、极限、连等式)
品牌标记 JBYU* → WPU*,与采集端一致
2. CtoMathFinder:接上 EQ 分支(原为 not-ported 桩),域代码经
EqToLatexUtil.convert 转 LaTeX 写入 eqLatexMap(WPEQNUM 占位符)
3. WordParseUtil.dealPlaceholder:EQ 的 LaTeX 同样加 \( \) 包裹
(原先因 WPEQNUM 例外不加包裹,前端/MathJax 不渲染)
修前症状:教辅 Word 里分式/方程组/上下标整段丢失(如 {−1/3, 1} 变成 {-,1})
修后实测:46 份教辅 2908 处 EQ 域全部还原,公式总数 2954 处、零未包裹残留
安全:application.yml(含 RustFS 密钥)不入库,提供 application.yml.example;
OssService 的 @Value 默认密钥已清空
122 lines
4.9 KiB
Java
122 lines
4.9 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* javax.xml.bind.JAXBElement
|
|
* org.apache.commons.lang3.StringUtils
|
|
* org.docx4j.TextUtils
|
|
* org.docx4j.TraversalUtil$CallbackImpl
|
|
* org.docx4j.mce.AlternateContent
|
|
* org.docx4j.mce.AlternateContent$Fallback
|
|
* org.docx4j.openpackaging.packages.WordprocessingMLPackage
|
|
* org.docx4j.vml.CTGroup
|
|
* org.docx4j.vml.CTRoundRect
|
|
* org.docx4j.vml.CTShape
|
|
* org.docx4j.vml.CTTextbox
|
|
* org.docx4j.vml.VmlShapeElements
|
|
* org.docx4j.wml.CTTxbxContent
|
|
* org.docx4j.wml.ObjectFactory
|
|
* org.docx4j.wml.Pict
|
|
* org.docx4j.wml.R
|
|
* org.docx4j.wml.Text
|
|
* org.slf4j.Logger
|
|
* org.slf4j.LoggerFactory
|
|
*/
|
|
package com.jby.officecore.utils.finder;
|
|
|
|
import com.jby.officecore.domain.bo.ParseBO;
|
|
import java.util.List;
|
|
import javax.xml.bind.JAXBElement;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.docx4j.TextUtils;
|
|
import org.docx4j.TraversalUtil;
|
|
import org.docx4j.mce.AlternateContent;
|
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
|
import org.docx4j.vml.CTGroup;
|
|
import org.docx4j.vml.CTRoundRect;
|
|
import org.docx4j.vml.CTShape;
|
|
import org.docx4j.vml.CTTextbox;
|
|
import org.docx4j.vml.VmlShapeElements;
|
|
import org.docx4j.wml.CTTxbxContent;
|
|
import org.docx4j.wml.ObjectFactory;
|
|
import org.docx4j.wml.Pict;
|
|
import org.docx4j.wml.R;
|
|
import org.docx4j.wml.Text;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class CtTextBoxFinder
|
|
extends TraversalUtil.CallbackImpl {
|
|
private static final Logger log = LoggerFactory.getLogger(CtTextBoxFinder.class);
|
|
private WordprocessingMLPackage wordMlPackage;
|
|
private ParseBO parseBO;
|
|
|
|
public CtTextBoxFinder(WordprocessingMLPackage wordMlPackage, ParseBO parseBO) {
|
|
this.wordMlPackage = wordMlPackage;
|
|
this.parseBO = parseBO;
|
|
}
|
|
|
|
public List<Object> apply(Object o) {
|
|
if (o instanceof Pict) {
|
|
boolean isFirstRun = true;
|
|
ObjectFactory factory = new ObjectFactory();
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
R sourceR = factory.createR();
|
|
Pict pict = (Pict)o;
|
|
List anyAndAny = pict.getAnyAndAny();
|
|
for (Object object : anyAndAny) {
|
|
if (!(object instanceof JAXBElement)) continue;
|
|
JAXBElement jbe = (JAXBElement)object;
|
|
this.findTextBox(jbe.getValue(), stringBuilder, isFirstRun, sourceR);
|
|
}
|
|
if (StringUtils.isNotBlank((CharSequence)stringBuilder.toString())) {
|
|
AlternateContent.Fallback fallback = (AlternateContent.Fallback)pict.getParent();
|
|
AlternateContent alternateContent = (AlternateContent)fallback.getParent();
|
|
R r = (R)alternateContent.getParent();
|
|
Text text = factory.createText();
|
|
text.setValue(stringBuilder + "JBY_TEXTBOX_JBY");
|
|
r.getContent().set(0, text);
|
|
r.setRPr(sourceR.getRPr());
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private void findTextBox(Object object, StringBuilder sb, boolean isFirstRun, R sourceR) {
|
|
block8: {
|
|
List egShapeElements;
|
|
if (object instanceof CTShape || object instanceof CTRoundRect) {
|
|
VmlShapeElements vmlShapeElements = (VmlShapeElements)object;
|
|
egShapeElements = vmlShapeElements.getEGShapeElements();
|
|
for (JAXBElement egShapeElement : egShapeElements) {
|
|
if (!(egShapeElement.getValue() instanceof CTTextbox)) continue;
|
|
CTTextbox ctTextbox = (CTTextbox)egShapeElement.getValue();
|
|
CTTxbxContent txbxContent = ctTextbox.getTxbxContent();
|
|
for (int i = 0; i < txbxContent.getContent().size(); ++i) {
|
|
Object obj = txbxContent.getContent().get(i);
|
|
String text = TextUtils.getText(obj);
|
|
if (txbxContent.getContent().size() > 1 && i != txbxContent.getContent().size() - 1) {
|
|
text = text + "JBY_TEXTBOX_JBYBR";
|
|
}
|
|
sb.append(text);
|
|
}
|
|
}
|
|
}
|
|
if (!(object instanceof CTGroup)) break block8;
|
|
CTGroup ctGroup = (CTGroup)object;
|
|
egShapeElements = ctGroup.getEGShapeElements();
|
|
boolean isCtShape = egShapeElements.stream().anyMatch(f -> f.getValue() instanceof CTShape);
|
|
if (isCtShape) {
|
|
for (int i = egShapeElements.size() - 1; i >= 0; --i) {
|
|
this.findTextBox(((JAXBElement)egShapeElements.get(i)).getValue(), sb, isFirstRun, sourceR);
|
|
}
|
|
} else {
|
|
for (JAXBElement egShapeElement : egShapeElements) {
|
|
this.findTextBox(egShapeElement.getValue(), sb, isFirstRun, sourceR);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|