reader.go 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  1. package docx
  2. /*
  3. reader.go - Parse a Word (.docx) file into a Document.
  4. The goal is that an imported document LOOKS like it did where it came
  5. from (Word, Google Docs), so formatting is resolved the way Word
  6. resolves it - docDefaults, style chains, numbering definitions, table
  7. styles - and written out as explicit inline CSS on the editor HTML
  8. (see props.go for the inheritance and docs_layout.js for the half
  9. of the layout that can only be computed in the browser).
  10. The HTML vocabulary this produces is the Docs "rich model", also what
  11. the docx writer consumes:
  12. blocks p / h1-h6 (.doc-title, .doc-subtitle) with
  13. style: margin-bottom (spacing after), margin-top on headings
  14. and padding-top elsewhere (spacing before: Google
  15. Docs collapses a heading's with the spacing above it
  16. the way CSS margins collapse, and adds a body
  17. paragraph's), margin-left/-right (indents),
  18. text-indent, text-align, font-*, color,
  19. background-color, white-space:pre-wrap when the text
  20. relies on repeated spaces
  21. data-ls="1.15" auto line spacing (multiple of single)
  22. data-lsexact="15pt" exact line height
  23. data-lsmin="15pt" at-least line height
  24. data-keep-next / data-keep-lines / data-widow = "1"
  25. data-page-break-before="1"
  26. data-tabs="right:451.28:dot;left:36:none" (pt from the
  27. text column's left edge)
  28. lists ol / ul .doc-list with data-fmt, data-lvltext, start,
  29. style padding-left + --doc-hang; nested lists sit directly
  30. inside their parent list (what execCommand("indent") makes)
  31. runs span style=font-*, color, background-color, text-decoration,
  32. font-variant, text-transform; sup / sub; a (links, with
  33. color/decoration inherited so the runs decide),
  34. span.doc-tab (a real tab character), br,
  35. sup.doc-fnref[data-fn] (footnote reference),
  36. span.doc-field[data-field=PAGE|NUMPAGES]
  37. images img style=width/height in pt, object-view-box for a crop,
  38. border for a picture outline; img.doc-anchor (display:block,
  39. margin offsets) for anchored top-and-bottom pictures
  40. tables table.of-table style=width/margin-left, colgroup of pt
  41. widths, td/th with explicit border-*, padding,
  42. vertical-align, background-color, colspan/rowspan
  43. breaks div.doc-pagebreak
  44. Headers/footers come back as HTML (headerHtml/footerHtml) plus plain
  45. text for older consumers, footnotes as body.footnotes. Legacy binary
  46. .doc is rejected.
  47. */
  48. import (
  49. "archive/zip"
  50. "bytes"
  51. "errors"
  52. "fmt"
  53. "io"
  54. "path"
  55. "regexp"
  56. "sort"
  57. "strconv"
  58. "strings"
  59. "imuslab.com/arozos/mod/office/internal/fonts"
  60. "imuslab.com/arozos/mod/office/internal/htmlutil"
  61. "imuslab.com/arozos/mod/office/internal/media"
  62. "imuslab.com/arozos/mod/office/internal/units"
  63. "imuslab.com/arozos/mod/office/internal/xmltree"
  64. "imuslab.com/arozos/mod/office/metafile"
  65. "imuslab.com/arozos/mod/office/model"
  66. )
  67. // Parse converts raw .docx bytes into a Document
  68. func Parse(data []byte) (*model.Document, error) {
  69. if len(data) > 8 && data[0] == 0xD0 && data[1] == 0xCF {
  70. // a Word 97-2003 file: package doc converts it into a .docx first
  71. return nil, errors.New("this is a Word 97-2003 (.doc) document; open it with doc.Parse")
  72. }
  73. zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
  74. if err != nil {
  75. return nil, errors.New("not a valid docx (zip) file")
  76. }
  77. files := map[string][]byte{}
  78. for _, f := range zr.File {
  79. name := path.Clean(f.Name)
  80. if strings.HasSuffix(name, ".xml") || strings.HasSuffix(name, ".rels") ||
  81. strings.HasPrefix(name, "word/media/") || strings.HasPrefix(name, "media/") {
  82. rc, err := f.Open()
  83. if err != nil {
  84. continue
  85. }
  86. b, err := io.ReadAll(rc)
  87. rc.Close()
  88. if err != nil {
  89. continue
  90. }
  91. files[name] = b
  92. }
  93. }
  94. docXML, ok := files["word/document.xml"]
  95. if !ok {
  96. return nil, errors.New("docx is missing word/document.xml")
  97. }
  98. tree, err := xmltree.Parse(docXML)
  99. if err != nil {
  100. return nil, errors.New("cannot parse document.xml: " + err.Error())
  101. }
  102. body := tree.First("body")
  103. if body == nil {
  104. return nil, errors.New("document has no body")
  105. }
  106. cv := &docxConv{
  107. files: files,
  108. ss: parseStyleSheet(files["word/styles.xml"]),
  109. nb: parseNumbering(files["word/numbering.xml"]),
  110. bodyNode: body,
  111. fnNumber: map[string]int{},
  112. rv: docxReview{used: map[string]bool{}},
  113. }
  114. docPart := cv.part("word/document.xml")
  115. // Google Docs writes every rsid as zeros and numbers paragraphs from 1
  116. cv.gdocs = bytes.Contains(docXML, []byte(`w:rsidR="00000000"`)) &&
  117. bytes.Contains(docXML, []byte(`w14:paraId="00000001"`))
  118. doc := &model.Document{}
  119. // the document-wide line spacing every paragraph inherits unless it
  120. // says otherwise (Google Docs: 1.15, Word: 1.08 or single)
  121. cv.defaultLS = 1
  122. if dp, _ := cv.ss.paraStyle(""); dp != nil && dp.line.set && (dp.lineRule == "" || dp.lineRule == "auto") && dp.line.v > 0 {
  123. cv.defaultLS = round3(dp.line.v / 240)
  124. }
  125. doc.LineSpacing = cv.defaultLS
  126. // page geometry (parsed first: margins place anchored pictures and
  127. // multi-column layouts change the HTML conversion)
  128. sect := body.First("sectPr")
  129. if sect != nil {
  130. doc.Page = parseSectPage(sect)
  131. cv.marginL = twipsAttr(sect.First("pgMar"), "left", 1440) / 20
  132. cv.textW = (twipsAttr(sect.First("pgSz"), "w", 11906) -
  133. twipsAttr(sect.First("pgMar"), "left", 1440) -
  134. twipsAttr(sect.First("pgMar"), "right", 1440)) / 20
  135. if sect.First("titlePg") != nil && onOff(sect.First("titlePg")).v {
  136. doc.HFMode = model.HFModeExceptFirst
  137. }
  138. } else {
  139. cv.marginL, cv.textW = 72, 451.3
  140. }
  141. // Word writes IEEE-style spanning titles as leading single-column
  142. // sections; map those blocks back to .col-span-all
  143. if doc.Page != nil && doc.Page.Columns > 1 {
  144. cv.markSpanSections(body)
  145. }
  146. cv.collectSectionBreaks(body)
  147. doc.HTML = resolveTocLevels(cv.blocks(body, docPart, blockCtx{top: true}))
  148. // footnotes, in the order the text references them
  149. if len(cv.fnOrder) > 0 {
  150. if raw, ok := files["word/footnotes.xml"]; ok {
  151. if ft, err := xmltree.Parse(raw); err == nil {
  152. fnPart := cv.part("word/footnotes.xml")
  153. byID := map[string]*xmltree.Node{}
  154. for _, fn := range ft.All("footnote") {
  155. byID[fn.Attr("id")] = fn
  156. }
  157. for _, id := range cv.fnOrder {
  158. fn := byID[id]
  159. if fn == nil {
  160. continue
  161. }
  162. doc.Footnotes = append(doc.Footnotes, model.Footnote{
  163. ID: id,
  164. HTML: cv.blocks(fn, fnPart, blockCtx{footnote: true}),
  165. })
  166. }
  167. }
  168. }
  169. }
  170. // header / footer: the section's default parts, plus "different first
  171. // page" when it is on
  172. if sect != nil {
  173. hdr := cv.hfPart(sect, "headerReference", "default")
  174. ftr := cv.hfPart(sect, "footerReference", "default")
  175. if hdr != "" {
  176. doc.HeaderHTML, doc.Header = cv.hfHTML(hdr)
  177. }
  178. if ftr != "" {
  179. doc.FooterHTML, doc.Footer = cv.hfHTML(ftr)
  180. doc.PageNumbers = doc.PageNumbers || strings.Contains(doc.FooterHTML, `data-field="PAGE"`)
  181. }
  182. doc.PageNumbers = doc.PageNumbers || cv.autoPageNumber
  183. if doc.HFMode == model.HFModeExceptFirst {
  184. // a first-page part with real content is a different header,
  185. // not a missing one; the editor only models "blank on page one"
  186. if fh := cv.hfPart(sect, "headerReference", "first"); fh != "" {
  187. if h, txt := cv.hfHTML(fh); txt != "" || strings.Contains(h, "<img") {
  188. doc.HFMode = ""
  189. }
  190. }
  191. }
  192. }
  193. // review: the comments the text anchors, and whether Word was tracking
  194. doc.Comments = parseDocxComments(files, cv.rv.used)
  195. if raw, ok := files["word/settings.xml"]; ok {
  196. if st, err := xmltree.Parse(raw); err == nil {
  197. if tr := st.First("trackRevisions"); tr != nil && onOff(tr).v {
  198. doc.TrackChanges = true
  199. }
  200. }
  201. }
  202. return doc, nil
  203. }
  204. func round1(v float64) float64 {
  205. return float64(int(v*10+0.5)) / 10
  206. }
  207. func round3(v float64) float64 {
  208. if v < 0 {
  209. return -round3(-v)
  210. }
  211. return float64(int(v*1000+0.5)) / 1000
  212. }
  213. func abs(v int) int {
  214. if v < 0 {
  215. return -v
  216. }
  217. return v
  218. }
  219. func twipsAttr(n *xmltree.Node, name string, def float64) float64 {
  220. if v := numAttr(n, name); v.set {
  221. return v.v
  222. }
  223. return def
  224. }
  225. // parseSectPage reads page size, orientation, margins, header/footer
  226. // distances and columns from a sectPr
  227. func parseSectPage(sect *xmltree.Node) *model.PageConf {
  228. pc := &model.PageConf{Size: "A4", Orientation: "portrait"}
  229. if sz := sect.First("pgSz"); sz != nil {
  230. w := int(twipsAttr(sz, "w", 11906))
  231. h := int(twipsAttr(sz, "h", 16838))
  232. if sz.Attr("orient") == "landscape" || w > h {
  233. pc.Orientation = "landscape"
  234. w, h = h, w
  235. }
  236. best := "A4"
  237. bestD := 1 << 30
  238. for name, dim := range pageSizesTwips {
  239. d := abs(dim[0]-w) + abs(dim[1]-h)
  240. if d < bestD {
  241. bestD = d
  242. best = name
  243. }
  244. }
  245. pc.Size = best
  246. }
  247. if mar := sect.First("pgMar"); mar != nil {
  248. pc.Margins = &model.MarginsMM{
  249. Top: round1(twipsToMmF(twipsAttr(mar, "top", 1440))),
  250. Right: round1(twipsToMmF(twipsAttr(mar, "right", 1440))),
  251. Bottom: round1(twipsToMmF(twipsAttr(mar, "bottom", 1440))),
  252. Left: round1(twipsToMmF(twipsAttr(mar, "left", 1440))),
  253. }
  254. // Word's top/bottom margin may be negative ("do not move the text
  255. // for the header") - the editor has no such notion
  256. if pc.Margins.Top < 0 {
  257. pc.Margins.Top = -pc.Margins.Top
  258. }
  259. if pc.Margins.Bottom < 0 {
  260. pc.Margins.Bottom = -pc.Margins.Bottom
  261. }
  262. if v := numAttr(mar, "header"); v.set {
  263. d := round1(twipsToMmF(v.v))
  264. pc.HeaderDist = &d
  265. }
  266. if v := numAttr(mar, "footer"); v.set {
  267. d := round1(twipsToMmF(v.v))
  268. pc.FooterDist = &d
  269. }
  270. }
  271. if cols := sect.First("cols"); cols != nil {
  272. if n, err := strconv.Atoi(cols.Attr("num")); err == nil && n > 1 {
  273. pc.Columns = n
  274. if sp := numAttr(cols, "space"); sp.set && sp.v > 0 {
  275. pc.ColGap = round1(twipsToMmF(sp.v))
  276. }
  277. }
  278. }
  279. return pc
  280. }
  281. func twipsToMmF(tw float64) float64 { return tw * 25.4 / 1440.0 }
  282. /* ---------------- conversion state ---------------- */
  283. type docxPartCtx struct {
  284. name string
  285. rels map[string]string
  286. ext map[string]bool // rId -> TargetMode External
  287. }
  288. type blockCtx struct {
  289. top bool // direct children of w:body
  290. cell bool
  291. hf bool
  292. footnote bool
  293. }
  294. type fieldFrame struct {
  295. instr string
  296. inResult bool
  297. shown bool // the field's result put something on the page
  298. }
  299. type docxConv struct {
  300. files map[string][]byte
  301. ss *docxStyleSheet
  302. nb *docxNumDefs
  303. bodyNode *xmltree.Node
  304. spanIdx map[int]bool // top-level block indexes that span all columns
  305. skipIdx map[int]bool // empty section-divider paragraphs to drop
  306. breakIdx map[int]bool // paragraphs ending a section that starts a new page
  307. defaultLS float64
  308. marginL float64 // pt
  309. textW float64 // pt
  310. fields []fieldFrame
  311. // a TOC field was shown or ended in the paragraph at hand, and the
  312. // heading levels the last one lists (toc.go)
  313. tocTouched bool
  314. tocMax int
  315. fnNumber map[string]int
  316. fnOrder []string
  317. parts map[string]*docxPartCtx
  318. // Google Docs exports: a handful of its layout habits are imitated so
  319. // the import matches its own PDF (see the gdocs uses)
  320. gdocs bool
  321. // spacing-before the next paragraph gives back (see paragraph)
  322. reduceBefore float64
  323. // the paragraph before ended with "auto" (HTML) spacing after
  324. prevAutoAfter bool
  325. // a footer carried the page number our export adds (see hfHTML)
  326. autoPageNumber bool
  327. // review state: open comment ranges and the revision mark being walked
  328. // (review.go)
  329. rv docxReview
  330. curRev string // "" | "ins" | "del"
  331. }
  332. // editorTableStyle marks a table Build wrote from an editor-made one
  333. const editorTableStyle = "ArozEditorTable"
  334. // autoRuleStyle marks the paragraph Build writes an <hr> as
  335. const autoRuleStyle = "ArozHorizontalRule"
  336. // isAutoRule reports a horizontal rule written by Build: its style and
  337. // no text
  338. func isAutoRule(p *xmltree.Node) bool {
  339. pPr := p.First("pPr")
  340. if pPr == nil {
  341. return false
  342. }
  343. ps := pPr.First("pStyle")
  344. if ps == nil || ps.Attr("val") != autoRuleStyle {
  345. return false
  346. }
  347. var texts []string
  348. xmltree.CollectText(p, &texts)
  349. return strings.TrimSpace(strings.Join(texts, "")) == ""
  350. }
  351. // autoPageNumberStyle marks the page-number paragraph Build adds to a
  352. // footer, so an import turns it back into Document.PageNumbers
  353. const autoPageNumberStyle = "ArozPageNumber"
  354. // part loads the relationships of one XML part
  355. func (cv *docxConv) part(name string) *docxPartCtx {
  356. if cv.parts == nil {
  357. cv.parts = map[string]*docxPartCtx{}
  358. }
  359. if p, ok := cv.parts[name]; ok {
  360. return p
  361. }
  362. dir, file := path.Split(name)
  363. relsName := dir + "_rels/" + file + ".rels"
  364. p := &docxPartCtx{name: name, rels: map[string]string{}, ext: map[string]bool{}}
  365. if raw, ok := cv.files[relsName]; ok {
  366. if tree, err := xmltree.Parse(raw); err == nil {
  367. for _, r := range tree.All("Relationship") {
  368. p.rels[r.Attr("Id")] = r.Attr("Target")
  369. if strings.EqualFold(r.Attr("TargetMode"), "External") {
  370. p.ext[r.Attr("Id")] = true
  371. }
  372. }
  373. }
  374. }
  375. cv.parts[name] = p
  376. return p
  377. }
  378. // hfPart finds the part path a header/footer reference points at
  379. func (cv *docxConv) hfPart(sect *xmltree.Node, kind, typ string) string {
  380. docPart := cv.part("word/document.xml")
  381. for _, ref := range sect.All(kind) {
  382. t := ref.Attr("type")
  383. if t == "" {
  384. t = "default"
  385. }
  386. if t != typ {
  387. continue
  388. }
  389. target := docPart.rels[ref.AttrNS("relationships", "id")]
  390. if target == "" {
  391. continue
  392. }
  393. return xmltree.ResolvePart("word", target)
  394. }
  395. return ""
  396. }
  397. // hfHTML converts a header/footer part; returns its HTML and plain text
  398. func (cv *docxConv) hfHTML(partName string) (string, string) {
  399. raw, ok := cv.files[partName]
  400. if !ok {
  401. return "", ""
  402. }
  403. tree, err := xmltree.Parse(raw)
  404. if err != nil {
  405. return "", ""
  406. }
  407. // the page number our own export adds is the editor's page-number
  408. // switch, not footer content
  409. kept := tree.Nodes[:0]
  410. for _, c := range tree.Nodes {
  411. if c.XMLName.Local == "p" {
  412. if pPr := c.First("pPr"); pPr != nil {
  413. if ps := pPr.First("pStyle"); ps != nil && ps.Attr("val") == autoPageNumberStyle {
  414. cv.autoPageNumber = true
  415. continue
  416. }
  417. }
  418. }
  419. kept = append(kept, c)
  420. }
  421. tree.Nodes = kept
  422. saved := cv.fields
  423. cv.fields = nil
  424. htmlOut := cv.blocks(tree, cv.part(partName), blockCtx{hf: true})
  425. cv.fields = saved
  426. var texts []string
  427. xmltree.CollectText(tree, &texts)
  428. txt := strings.TrimSpace(strings.Join(texts, ""))
  429. // a header that is only empty paragraphs is no header - but one that is
  430. // only a page number is one
  431. if txt == "" && !strings.Contains(htmlOut, "<img") && !strings.Contains(htmlOut, `data-field="`) {
  432. return "", ""
  433. }
  434. // the editor's plain header, as Build writes it, stays plain
  435. if m := plainHFRe.FindStringSubmatch(htmlOut); m != nil && !strings.Contains(m[1], "<") {
  436. return "", txt
  437. }
  438. return htmlOut, txt
  439. }
  440. var plainHFRe = regexp.MustCompile(`^<p><span style="font-size:9pt;color:#6b7078;">(.*)</span></p>$`)
  441. // markSpanSections finds paragraph-embedded sectPr elements (section
  442. // dividers). Blocks belonging to a single-column section of a multi-column
  443. // document are IEEE-style spanning blocks.
  444. func (cv *docxConv) markSpanSections(body *xmltree.Node) {
  445. cv.spanIdx = map[int]bool{}
  446. cv.skipIdx = map[int]bool{}
  447. var pending []int
  448. for i := range body.Nodes {
  449. n := &body.Nodes[i]
  450. local := n.XMLName.Local
  451. if local != "p" && local != "tbl" {
  452. continue
  453. }
  454. if local == "p" {
  455. if pPr := n.First("pPr"); pPr != nil {
  456. if sp := pPr.First("sectPr"); sp != nil {
  457. single := true
  458. if cols := sp.First("cols"); cols != nil {
  459. if num, err := strconv.Atoi(cols.Attr("num")); err == nil && num > 1 {
  460. single = false
  461. }
  462. }
  463. if single {
  464. for _, j := range pending {
  465. cv.spanIdx[j] = true
  466. }
  467. cv.spanIdx[i] = true
  468. }
  469. if paragraphIsEmpty(n) {
  470. cv.skipIdx[i] = true // pure divider paragraph
  471. }
  472. pending = nil
  473. continue
  474. }
  475. }
  476. }
  477. pending = append(pending, i)
  478. }
  479. }
  480. // collectSectionBreaks marks the paragraphs that close a section whose
  481. // successor starts on a new page (single-column documents only - the
  482. // multi-column mapping above owns those)
  483. func (cv *docxConv) collectSectionBreaks(body *xmltree.Node) {
  484. if cv.spanIdx != nil {
  485. return
  486. }
  487. type sp struct {
  488. idx int
  489. typ string
  490. }
  491. var list []sp
  492. for i := range body.Nodes {
  493. n := &body.Nodes[i]
  494. if n.XMLName.Local == "p" {
  495. if s := n.Path("pPr", "sectPr"); s != nil {
  496. list = append(list, sp{i, sectType(s)})
  497. }
  498. }
  499. }
  500. if len(list) == 0 {
  501. return
  502. }
  503. cv.breakIdx = map[int]bool{}
  504. for k, s := range list {
  505. nextType := "nextPage"
  506. if k+1 < len(list) {
  507. nextType = list[k+1].typ
  508. } else if bs := body.First("sectPr"); bs != nil {
  509. nextType = sectType(bs)
  510. }
  511. if nextType != "continuous" {
  512. cv.breakIdx[s.idx] = true
  513. }
  514. }
  515. }
  516. func sectType(s *xmltree.Node) string {
  517. if t := s.First("type"); t != nil && t.Attr("val") != "" {
  518. return t.Attr("val")
  519. }
  520. return "nextPage"
  521. }
  522. func paragraphIsEmpty(p *xmltree.Node) bool {
  523. var texts []string
  524. xmltree.CollectText(p, &texts)
  525. if strings.TrimSpace(strings.Join(texts, "")) != "" {
  526. return false
  527. }
  528. var d []*xmltree.Node
  529. p.FindAll("drawing", &d)
  530. return len(d) == 0
  531. }
  532. /* ---------------- blocks ---------------- */
  533. type listFrame struct {
  534. tag string // ol | ul
  535. numID string
  536. ilvl int
  537. indL float64 // pt from the container's left edge
  538. }
  539. // blocks renders the block children of w:body, a table cell, a header,
  540. // footer or footnote
  541. func (cv *docxConv) blocks(parent *xmltree.Node, part *docxPartCtx, ctx blockCtx) string {
  542. var sb strings.Builder
  543. var lists []listFrame
  544. lastPara := -1 // where the last plain paragraph starts in sb
  545. closeLists := func(depth int) {
  546. for len(lists) > depth {
  547. sb.WriteString("</" + lists[len(lists)-1].tag + ">")
  548. lists = lists[:len(lists)-1]
  549. }
  550. }
  551. var walk func(children []xmltree.Node, top bool)
  552. walk = func(children []xmltree.Node, top bool) {
  553. for i := range children {
  554. n := &children[i]
  555. if top && ctx.top {
  556. if cv.skipIdx != nil && cv.skipIdx[i] {
  557. continue
  558. }
  559. }
  560. switch n.XMLName.Local {
  561. case "p":
  562. if isAutoRule(n) {
  563. closeLists(0)
  564. lastPara = -1
  565. sb.WriteString("<hr>")
  566. continue
  567. }
  568. spanAll := top && ctx.top && cv.spanIdx != nil && cv.spanIdx[i]
  569. item := cv.paragraph(n, part, ctx, spanAll)
  570. if item.list != nil {
  571. cv.placeListItem(&sb, &lists, item)
  572. } else {
  573. closeLists(0)
  574. lastPara = sb.Len()
  575. sb.WriteString(item.html)
  576. }
  577. if top && ctx.top && cv.breakIdx != nil && cv.breakIdx[i] {
  578. closeLists(0)
  579. sb.WriteString(pageBreakDiv)
  580. }
  581. case "tbl":
  582. closeLists(0)
  583. spanAll := top && ctx.top && cv.spanIdx != nil && cv.spanIdx[i]
  584. cv.reduceBefore = 0
  585. cv.prevAutoAfter = false
  586. // Google Docs gives no spacing-after to an empty paragraph
  587. // right above a table
  588. if cv.gdocs && lastPara >= 0 && emptyBlockHTML(sb.String()[lastPara:]) {
  589. cur := sb.String()
  590. tail := dropMarginBottom(cur[lastPara:])
  591. if tail != cur[lastPara:] {
  592. sb.Reset()
  593. sb.WriteString(cur[:lastPara])
  594. sb.WriteString(tail)
  595. }
  596. }
  597. sb.WriteString(cv.table(n, part, spanAll))
  598. case "sdt":
  599. if c := n.First("sdtContent"); c != nil {
  600. walk(c.Nodes, false)
  601. }
  602. case "customXml", "ins", "moveTo", "smartTag":
  603. walk(n.Nodes, false)
  604. case "AlternateContent":
  605. if c := n.First("Choice"); c != nil {
  606. walk(c.Nodes, false)
  607. } else if f := n.First("Fallback"); f != nil {
  608. walk(f.Nodes, false)
  609. }
  610. }
  611. }
  612. }
  613. walk(parent.Nodes, true)
  614. closeLists(0)
  615. return sb.String()
  616. }
  617. var marginBottomRe = regexp.MustCompile(`margin-bottom:[^;"]*;`)
  618. var emptyBlockRe = regexp.MustCompile(`^<[a-z0-9]+[^>]*><br></[a-z0-9]+>$`)
  619. // emptyBlockHTML reports whether s is one block holding nothing but its
  620. // empty line
  621. func emptyBlockHTML(s string) bool {
  622. return emptyBlockRe.MatchString(s)
  623. }
  624. // dropMarginBottom removes the spacing-after from the opening tag of the
  625. // block HTML that starts s (only when s is a single block)
  626. func dropMarginBottom(s string) string {
  627. end := strings.Index(s, ">")
  628. if end < 0 {
  629. return s
  630. }
  631. return marginBottomRe.ReplaceAllString(s[:end], "margin-bottom:0pt;") + s[end:]
  632. }
  633. const pageBreakDiv = `<div class="doc-pagebreak" contenteditable="false"></div>`
  634. type paraOutput struct {
  635. html string // complete block HTML (non-list paragraphs)
  636. list *docxListItem
  637. liAttrs string
  638. liInner string
  639. }
  640. type docxListItem struct {
  641. numID string
  642. ilvl int
  643. fmt string
  644. text string
  645. start int
  646. value int
  647. indL float64 // pt
  648. hang float64 // pt
  649. ordered bool
  650. }
  651. // placeListItem opens/closes list elements around one list paragraph
  652. func (cv *docxConv) placeListItem(sb *strings.Builder, lists *[]listFrame, it paraOutput) {
  653. li := it.list
  654. // a different list instance closes the current one entirely
  655. if len(*lists) > 0 && (*lists)[0].numID != li.numID {
  656. for len(*lists) > 0 {
  657. sb.WriteString("</" + (*lists)[len(*lists)-1].tag + ">")
  658. *lists = (*lists)[:len(*lists)-1]
  659. }
  660. }
  661. for len(*lists) > 0 && (*lists)[len(*lists)-1].ilvl > li.ilvl {
  662. sb.WriteString("</" + (*lists)[len(*lists)-1].tag + ">")
  663. *lists = (*lists)[:len(*lists)-1]
  664. }
  665. tag := "ul"
  666. if li.ordered {
  667. tag = "ol"
  668. }
  669. if n := len(*lists); n > 0 && (*lists)[n-1].ilvl == li.ilvl && (*lists)[n-1].tag != tag {
  670. sb.WriteString("</" + (*lists)[n-1].tag + ">")
  671. *lists = (*lists)[:n-1]
  672. }
  673. if len(*lists) == 0 || (*lists)[len(*lists)-1].ilvl < li.ilvl {
  674. parentL := 0.0
  675. if len(*lists) > 0 {
  676. parentL = (*lists)[len(*lists)-1].indL
  677. }
  678. pad := li.indL - parentL
  679. attrs := ` class="doc-list" data-num="` + xmltree.Escape(li.numID) + `" data-fmt="` + li.fmt + `"`
  680. if li.text != "" {
  681. attrs += ` data-lvltext="` + xmltree.Escape(li.text) + `"`
  682. }
  683. if li.ordered && li.value != 1 {
  684. attrs += ` start="` + strconv.Itoa(li.value) + `"`
  685. }
  686. attrs += ` style="padding-left:` + ptStr(pad) + `;--doc-hang:` + ptStr(li.hang) + `;"`
  687. sb.WriteString("<" + tag + attrs + ">")
  688. *lists = append(*lists, listFrame{tag: tag, numID: li.numID, ilvl: li.ilvl, indL: li.indL})
  689. }
  690. sb.WriteString("<li" + it.liAttrs + ">" + it.liInner + "</li>")
  691. }
  692. // ptStr formats a length in points
  693. func ptStr(v float64) string {
  694. return units.TrimFloat(units.Round2(v)) + "pt"
  695. }
  696. /* ---------------- paragraphs ---------------- */
  697. // paragraph converts one w:p
  698. func (cv *docxConv) paragraph(p *xmltree.Node, part *docxPartCtx, ctx blockCtx, spanAll bool) paraOutput {
  699. direct := parsePPr(p.First("pPr"))
  700. styleP, styleR := cv.ss.paraStyle(direct.style)
  701. eff := *styleP
  702. eff.merge(direct)
  703. baseR := *styleR
  704. // the mark's look reaches the block (an empty line's height, a list
  705. // number's colour) but not its decoration: an underline or highlight
  706. // on a block would run under every character in it, where Word puts
  707. // it on the pilcrow alone
  708. mark := direct.mark
  709. mark.u, mark.strike, mark.dstrike = "", optBool{}, optBool{}
  710. mark.highlight, mark.shd, mark.vanish = "", "", optBool{}
  711. baseR.merge(mark)
  712. if cv.reduceBefore > 0 && !ctx.cell {
  713. b := 0.0
  714. if eff.before.set {
  715. b = eff.before.v - cv.reduceBefore*20
  716. }
  717. if b < 0 {
  718. b = 0
  719. }
  720. eff.before = optNum{set: true, v: b}
  721. }
  722. cv.reduceBefore = 0
  723. // two "auto"-spaced paragraphs meet with one auto space between them,
  724. // the way HTML margins collapse - not the after and the before added
  725. if cv.prevAutoAfter && eff.autoBefore.v && !ctx.cell {
  726. eff.before = optNum{set: true, v: 0}
  727. }
  728. cv.prevAutoAfter = eff.autoAfter.v && !ctx.cell
  729. level := cv.ss.headingLevel(direct.style)
  730. if direct.style == "" {
  731. level = cv.ss.headingLevel(cv.ss.defPara)
  732. }
  733. // a table of contents line; Word's TOC Heading is based on Heading 1
  734. // but titles the table, it is no heading of the document
  735. tocLvl := cv.ss.tocLevel(direct.style)
  736. if tocLvl != 0 {
  737. level = 0
  738. }
  739. // list membership (numbering from the paragraph or its style)
  740. var li *docxListItem
  741. if eff.numSet && cv.nb.exists(eff.numID) && !ctx.hf {
  742. ilvl := 0
  743. if eff.ilvl.set {
  744. ilvl = int(eff.ilvl.v)
  745. }
  746. def := cv.nb.level(eff.numID, ilvl)
  747. li = &docxListItem{numID: eff.numID, ilvl: ilvl, fmt: "bullet", start: 1}
  748. // "auto" (HTML) spacing is the space around a list, not between its
  749. // items: Word sets list paragraphs with it close together, as a
  750. // browser sets <li>s
  751. if eff.autoBefore.v {
  752. eff.before = optNum{set: true, v: 0}
  753. }
  754. if eff.autoAfter.v {
  755. eff.after = optNum{set: true, v: 0}
  756. cv.prevAutoAfter = false
  757. }
  758. if def != nil {
  759. li.fmt = htmlListFormat(def.fmt)
  760. li.text = def.text
  761. li.start = def.start
  762. // numbering indents sit between the style's and the paragraph's own
  763. ind := *styleP
  764. if def.indL.set {
  765. ind.indL = def.indL
  766. }
  767. if def.indHang.set || def.indFirst.set {
  768. ind.indHang, ind.indFirst = def.indHang, def.indFirst
  769. }
  770. ind.merge(direct)
  771. eff.indL, eff.indHang, eff.indFirst = ind.indL, ind.indHang, ind.indFirst
  772. }
  773. li.ordered = li.fmt != "bullet"
  774. li.value = cv.nb.next(eff.numID, ilvl)
  775. li.indL = eff.indL.v / 20
  776. if eff.indHang.set && eff.indHang.v > 0 {
  777. li.hang = eff.indHang.v / 20
  778. } else if eff.indFirst.set && eff.indFirst.v < 0 {
  779. li.hang = -eff.indFirst.v / 20
  780. } else {
  781. li.hang = 18
  782. }
  783. }
  784. // a run takes its look from the paragraph style and its own rPr; the
  785. // paragraph mark's rPr formats only the mark (the empty line's height,
  786. // a list number), so runs must not inherit it - but they are written
  787. // against the block, which does
  788. inTocField := cv.tocFieldOpen()
  789. cv.tocTouched = false
  790. runs := cv.runs(p, part, *styleR, baseR)
  791. inTocField = inTocField || cv.tocTouched || cv.tocFieldOpen()
  792. tag := "p"
  793. var classes []string
  794. switch {
  795. case level >= 1 && level <= 6 && li == nil:
  796. tag = "h" + strconv.Itoa(level)
  797. case level == -1 && li == nil:
  798. tag = "h1"
  799. classes = append(classes, "doc-title")
  800. case level == -2 && li == nil:
  801. classes = append(classes, "doc-subtitle")
  802. }
  803. if spanAll {
  804. classes = append(classes, "col-span-all")
  805. }
  806. // a line of a TOC field's result that names no TOC style (Google Docs
  807. // writes none): an entry, when it reads like one
  808. if tocLvl == 0 && inTocField && level == 0 && looksLikeTocEntry(strings.Join(runs.segments, "")) {
  809. tocLvl = tocLevelUnknown
  810. }
  811. isToc := tocLvl != 0 && li == nil && !ctx.cell && !ctx.hf
  812. if isToc {
  813. classes = append(classes, "doc-toc")
  814. if tocLvl < 0 {
  815. classes = append(classes, "doc-toc-title")
  816. }
  817. }
  818. style, data := cv.blockStyle(tag, eff, baseR, li != nil, level, runs.preWrap)
  819. // CJK text whose East Asian font is a Latin face falls back to a
  820. // proportional CJK face in Google Docs (MS PGothic / PMincho, as its
  821. // PDFs show) - narrow punctuation, which the shipped Noto Sans CJK draws
  822. // through its proportional alternates
  823. if ea := baseR.fontEA; !isCJKFontName(ea) && (ea != "" || !isCJKFontName(baseR.fontASCII)) &&
  824. hasCJK(strings.Join(runs.segments, "")) {
  825. style += `font-feature-settings:&quot;palt&quot;;`
  826. }
  827. attrs := ""
  828. if runs.bookmark != "" {
  829. attrs += ` id="` + xmltree.Escape(runs.bookmark) + `"`
  830. }
  831. if len(classes) > 0 {
  832. attrs += ` class="` + strings.Join(classes, " ") + `"`
  833. }
  834. if style != "" {
  835. attrs += ` style="` + style + `"`
  836. }
  837. attrs += data
  838. if isToc && tocLvl > 0 && tocLvl != tocLevelUnknown {
  839. attrs += ` data-toc-level="` + strconv.Itoa(tocLvl) + `"`
  840. }
  841. if isToc && tocLvl > 0 && inTocField && cv.tocMax > 0 {
  842. attrs += ` data-toc-max="` + strconv.Itoa(cv.tocMax) + `"`
  843. }
  844. if li != nil {
  845. inner := strings.Join(runs.segments, "")
  846. if strings.TrimSpace(stripTags(inner)) == "" && !strings.Contains(inner, "<img") {
  847. inner += "<br>"
  848. }
  849. return paraOutput{list: li, liAttrs: attrs, liInner: inner}
  850. }
  851. var sb strings.Builder
  852. for i, seg := range runs.segments {
  853. if i > 0 {
  854. sb.WriteString(pageBreakDiv)
  855. }
  856. // an anchored picture sits above/below the text, so a paragraph
  857. // holding only that still has its own (empty) line
  858. flow := anchorImgRe.ReplaceAllString(seg, "")
  859. empty := strings.TrimSpace(stripTags(flow)) == "" && !strings.Contains(flow, "<img") &&
  860. !strings.Contains(flow, "doc-tab")
  861. anchorsOnly := empty && flow != seg
  862. if anchorsOnly {
  863. seg += "<br>"
  864. empty = false
  865. }
  866. if len(runs.segments) > 1 && empty {
  867. // the part of a paragraph before or after a page break that
  868. // holds nothing takes no line (how Google Docs lays it out)
  869. continue
  870. }
  871. if empty && !strings.Contains(seg, "<br>") {
  872. seg += "<br>"
  873. } else if strings.HasSuffix(seg, "<br>") && !anchorsOnly {
  874. // a trailing line break opens one more (empty) line
  875. seg += "<br>"
  876. }
  877. segAttrs := attrs
  878. if i > 0 {
  879. segAttrs = strings.Replace(segAttrs, ` id="`+xmltree.Escape(runs.bookmark)+`"`, "", 1)
  880. // the text after a page break carries on the paragraph that
  881. // began on the page before: its spacing-before was spent there
  882. noBefore := eff
  883. noBefore.before = optNum{set: true, v: 0}
  884. st2, _ := cv.blockStyle(tag, noBefore, baseR, false, level, runs.preWrap)
  885. if style != "" {
  886. segAttrs = strings.Replace(segAttrs, ` style="`+style+`"`, ` style="`+st2+`"`, 1)
  887. }
  888. }
  889. sb.WriteString("<" + tag + segAttrs + ">" + seg + "</" + tag + ">")
  890. }
  891. // A paragraph that ends in a page break leaves an empty remainder on
  892. // the next page that takes no line - but Google Docs still counts its
  893. // spacing-after against the spacing-before of what follows (a heading
  894. // after a page-break heading starts 4pt higher than one after a
  895. // page-break body paragraph)
  896. if n := len(runs.segments); n > 1 && !ctx.cell && eff.after.set {
  897. last := runs.segments[n-1]
  898. if strings.TrimSpace(stripTags(last)) == "" && !strings.Contains(last, "<img") {
  899. cv.reduceBefore = eff.after.v / 20
  900. }
  901. }
  902. return paraOutput{html: sb.String()}
  903. }
  904. // the editor's own defaults for body text (docs.css) - anything else is
  905. // stated inline
  906. const (
  907. editorBodyPt = 11.0
  908. editorBodyColor = "000000"
  909. )
  910. var editorFontStack = docxFontStack("Arial", "")
  911. var anchorImgRe = regexp.MustCompile(`<img[^>]*class="doc-anchor"[^>]*>`)
  912. // font names that stand for another face in practice: Google Docs writes
  913. // "Arial Unicode MS" for runs holding symbols and lays them out in Arial,
  914. // while a Windows machine that has the old Office font installed would draw
  915. // them 20% taller and wider
  916. var docxFontAlias = map[string]string{
  917. "arial unicode ms": "Arial",
  918. }
  919. // metric twins a word processor substitutes for a font it does not have
  920. var docxMetricFallback = map[string]string{
  921. "sans": "Arial", "serif": "'Times New Roman'", "mono": "'Courier New'",
  922. }
  923. // docxFontStack is fonts.StackFor with the substitution Google Docs and Word
  924. // make for a font they do not have: a metric-compatible core font right
  925. // behind it. Without that the browser falls through to the shipped Noto
  926. // faces, which are wider and taller, and every line wraps and stacks
  927. // differently from the source.
  928. func docxFontStack(latin, ea string) string {
  929. if alias, ok := docxFontAlias[strings.ToLower(latin)]; ok {
  930. latin = alias
  931. }
  932. if alias, ok := docxFontAlias[strings.ToLower(ea)]; ok {
  933. ea = alias
  934. }
  935. if ea == latin {
  936. ea = ""
  937. }
  938. stack := fonts.StackFor(latin, ea)
  939. l := strings.ToLower(latin)
  940. kind := "sans"
  941. switch {
  942. case strings.Contains(l, "courier"), strings.Contains(l, "mono"), strings.Contains(l, "consolas"):
  943. kind = "mono"
  944. case strings.Contains(l, "times"), strings.Contains(l, "georgia"), strings.Contains(l, "garamond"),
  945. strings.Contains(l, "cambria"), strings.Contains(l, "book antiqua"),
  946. strings.Contains(l, "serif") && !strings.Contains(l, "sans"):
  947. kind = "serif"
  948. }
  949. fb := docxMetricFallback[kind]
  950. if strings.EqualFold(strings.Trim(fb, "'"), latin) {
  951. return stack
  952. }
  953. first := fonts.QuoteName(latin)
  954. if ea != "" && ea != latin {
  955. first += "," + fonts.QuoteName(ea)
  956. }
  957. if strings.HasPrefix(stack, first+",") {
  958. return first + "," + fb + stack[len(first):]
  959. }
  960. return stack
  961. }
  962. // blockStyle renders a paragraph's layout properties as inline CSS plus
  963. // data attributes
  964. func (cv *docxConv) blockStyle(tag string, eff docxPPr, r docxRPr, isList bool, level int, preWrap bool) (string, string) {
  965. var css []string
  966. add := func(k, v string) { css = append(css, k+":"+v) }
  967. heading := tag != "p"
  968. // a paragraph border sits between the spacing and the text: the text
  969. // keeps its indent and the rule is drawn "space" points outside it
  970. edge := func(side string) (float64, bool) {
  971. b, ok := eff.borders[side]
  972. if !ok || !b.visible() {
  973. return 0, false
  974. }
  975. return borderWidthPt(b) + b.space, true
  976. }
  977. bordered := false
  978. for _, side := range []string{"top", "right", "bottom", "left"} {
  979. if _, ok := edge(side); ok {
  980. bordered = true
  981. }
  982. }
  983. before, after := 0.0, 0.0
  984. if eff.before.set {
  985. before = eff.before.v / 20
  986. }
  987. if eff.after.set {
  988. after = eff.after.v / 20
  989. }
  990. if before != 0 || heading {
  991. // Google Docs collapses a heading's spacing-before with the
  992. // spacing-after above it (the larger wins) but adds a body
  993. // paragraph's to it - margin collapses in CSS, padding does not
  994. if heading || bordered {
  995. // (a bordered paragraph's spacing is outside its rule)
  996. add("margin-top", ptStr(before))
  997. } else {
  998. add("padding-top", ptStr(before))
  999. }
  1000. }
  1001. if after != 0 || heading {
  1002. add("margin-bottom", ptStr(after))
  1003. }
  1004. if !isList {
  1005. left := 0.0
  1006. if eff.indL.set {
  1007. left = eff.indL.v / 20
  1008. }
  1009. if shift, ok := edge("left"); ok {
  1010. left -= shift
  1011. }
  1012. if left != 0 {
  1013. add("margin-left", ptStr(left))
  1014. }
  1015. first := 0.0
  1016. if eff.indHang.set && eff.indHang.v != 0 {
  1017. first = -eff.indHang.v / 20
  1018. } else if eff.indFirst.set {
  1019. first = eff.indFirst.v / 20
  1020. }
  1021. if first != 0 {
  1022. add("text-indent", ptStr(first))
  1023. }
  1024. }
  1025. right := 0.0
  1026. if eff.indR.set {
  1027. right = eff.indR.v / 20
  1028. }
  1029. if shift, ok := edge("right"); ok && !isList {
  1030. right -= shift
  1031. }
  1032. if right != 0 {
  1033. add("margin-right", ptStr(right))
  1034. }
  1035. switch eff.jc {
  1036. case "center":
  1037. add("text-align", "center")
  1038. case "right", "end":
  1039. add("text-align", "right")
  1040. case "both", "distribute":
  1041. add("text-align", "justify")
  1042. }
  1043. // run defaults of the block
  1044. fc := rPrCSS(r)
  1045. if fc["font-family"] != editorFontStack || heading {
  1046. add("font-family", fc["font-family"])
  1047. }
  1048. if fc["font-size"] != ptStr(editorBodyPt) || heading {
  1049. add("font-size", fc["font-size"])
  1050. }
  1051. if fc["font-weight"] != "400" || heading {
  1052. add("font-weight", fc["font-weight"])
  1053. }
  1054. if fc["font-style"] != "normal" || heading {
  1055. add("font-style", fc["font-style"])
  1056. }
  1057. if fc["color"] != "#"+strings.ToLower(editorBodyColor) || heading {
  1058. add("color", fc["color"])
  1059. }
  1060. for _, k := range []string{"text-decoration", "font-variant", "text-transform", "background-color"} {
  1061. if v := fc[k]; v != "" && v != "none" && v != "normal" {
  1062. add(k, v)
  1063. }
  1064. }
  1065. if eff.shd != "" && eff.shd != "auto" {
  1066. add("background-color", "#"+strings.ToLower(eff.shd))
  1067. }
  1068. for _, side := range []string{"top", "right", "bottom", "left"} {
  1069. b, ok := eff.borders[side]
  1070. if !ok || !b.visible() {
  1071. continue
  1072. }
  1073. add("border-"+side, borderCSS(b))
  1074. if b.space > 0 {
  1075. add("padding-"+side, ptStr(b.space))
  1076. }
  1077. }
  1078. if preWrap {
  1079. add("white-space", "pre-wrap")
  1080. }
  1081. var data strings.Builder
  1082. switch {
  1083. case eff.line.set && eff.lineRule == "exact":
  1084. data.WriteString(` data-lsexact="` + ptStr(eff.line.v/20) + `"`)
  1085. case eff.line.set && eff.lineRule == "atLeast":
  1086. data.WriteString(` data-lsmin="` + ptStr(eff.line.v/20) + `"`)
  1087. case eff.line.set && eff.line.v > 0:
  1088. if ls := round3(eff.line.v / 240); ls != cv.defaultLS {
  1089. data.WriteString(` data-ls="` + units.TrimFloat(ls) + `"`)
  1090. }
  1091. case cv.defaultLS != 1:
  1092. data.WriteString(` data-ls="1"`)
  1093. }
  1094. if eff.keepNext.v {
  1095. data.WriteString(` data-keep-next="1"`)
  1096. }
  1097. if eff.keepLines.v {
  1098. data.WriteString(` data-keep-lines="1"`)
  1099. }
  1100. // widow/orphan control is on unless a paragraph turns it off (Word's
  1101. // Normal style and Google Docs both keep two lines together)
  1102. if eff.widow.set && !eff.widow.v {
  1103. data.WriteString(` data-widow="0"`)
  1104. }
  1105. if eff.pageBreakBefore.v {
  1106. data.WriteString(` data-page-break-before="1"`)
  1107. }
  1108. if len(eff.tabs) > 0 {
  1109. tabs := append([]docxTab(nil), eff.tabs...)
  1110. sort.Slice(tabs, func(i, j int) bool { return tabs[i].pos < tabs[j].pos })
  1111. var parts []string
  1112. for _, t := range tabs {
  1113. if t.align == "clear" {
  1114. continue
  1115. }
  1116. align := t.align
  1117. switch align {
  1118. case "start", "":
  1119. align = "left"
  1120. case "end":
  1121. align = "right"
  1122. }
  1123. leader := t.leader
  1124. if leader == "" {
  1125. leader = "none"
  1126. }
  1127. parts = append(parts, align+":"+units.TrimFloat(units.Round2(t.pos/20))+":"+leader)
  1128. }
  1129. if len(parts) > 0 {
  1130. data.WriteString(` data-tabs="` + strings.Join(parts, ";") + `"`)
  1131. }
  1132. }
  1133. if len(css) == 0 {
  1134. return "", data.String()
  1135. }
  1136. return xmltree.Escape(strings.Join(css, ";") + ";"), data.String()
  1137. }
  1138. // borderWidthPt is the width a border is drawn at, in points
  1139. func borderWidthPt(b docxBorder) float64 {
  1140. w := b.sz / 8
  1141. if w <= 0 {
  1142. w = 0.5
  1143. }
  1144. if b.val == "double" && w < 2.25 {
  1145. w = 2.25
  1146. }
  1147. return w
  1148. }
  1149. func borderCSS(b docxBorder) string {
  1150. w := borderWidthPt(b)
  1151. style := "solid"
  1152. switch b.val {
  1153. case "dotted":
  1154. style = "dotted"
  1155. case "dashed", "dashSmallGap", "dotDash", "dotDotDash":
  1156. style = "dashed"
  1157. case "double":
  1158. style = "double"
  1159. }
  1160. col := b.color
  1161. if len(col) != 6 {
  1162. col = "000000"
  1163. }
  1164. return ptStr(w) + " " + style + " #" + strings.ToLower(col)
  1165. }
  1166. var highlightColors = map[string]string{
  1167. "yellow": "ffff00", "green": "00ff00", "cyan": "00ffff", "magenta": "ff00ff",
  1168. "blue": "0000ff", "red": "ff0000", "darkBlue": "000080", "darkCyan": "008080",
  1169. "darkGreen": "008000", "darkMagenta": "800080", "darkRed": "800000",
  1170. "darkYellow": "808000", "darkGray": "808080", "lightGray": "c0c0c0",
  1171. "black": "000000", "white": "ffffff",
  1172. }
  1173. // rPrCSS renders a fully resolved run's formatting as CSS values
  1174. func rPrCSS(r docxRPr) map[string]string {
  1175. out := map[string]string{}
  1176. font := r.fontASCII
  1177. if font == "" {
  1178. font = "Arial"
  1179. }
  1180. ea := r.fontEA
  1181. if ea == font {
  1182. ea = ""
  1183. }
  1184. out["font-family"] = docxFontStack(font, ea)
  1185. sz := 20.0 // Word's built-in default is 10pt
  1186. if r.sz.set && r.sz.v > 0 {
  1187. sz = r.sz.v
  1188. }
  1189. out["font-size"] = ptStr(sz / 2)
  1190. out["font-weight"] = "400"
  1191. if r.b.v {
  1192. out["font-weight"] = "700"
  1193. }
  1194. out["font-style"] = "normal"
  1195. if r.i.v {
  1196. out["font-style"] = "italic"
  1197. }
  1198. var deco []string
  1199. if r.u != "" && r.u != "none" {
  1200. deco = append(deco, "underline")
  1201. }
  1202. if r.strike.v || r.dstrike.v {
  1203. deco = append(deco, "line-through")
  1204. }
  1205. out["text-decoration"] = "none"
  1206. if len(deco) > 0 {
  1207. out["text-decoration"] = strings.Join(deco, " ")
  1208. }
  1209. col := r.color
  1210. if len(col) != 6 {
  1211. col = editorBodyColor
  1212. }
  1213. out["color"] = "#" + strings.ToLower(col)
  1214. if r.highlight != "" && r.highlight != "none" {
  1215. if h, ok := highlightColors[r.highlight]; ok {
  1216. out["background-color"] = "#" + h
  1217. }
  1218. } else if r.shd != "" && r.shd != "auto" {
  1219. out["background-color"] = "#" + strings.ToLower(r.shd)
  1220. }
  1221. if r.smallCaps.v {
  1222. out["font-variant"] = "small-caps"
  1223. }
  1224. if r.caps.v {
  1225. out["text-transform"] = "uppercase"
  1226. }
  1227. return out
  1228. }
  1229. var cssRunKeys = []string{"font-family", "font-size", "font-weight", "font-style",
  1230. "text-decoration", "color", "background-color", "font-variant", "text-transform"}
  1231. // hasCJK reports whether text holds Chinese, Japanese or Korean characters
  1232. func hasCJK(s string) bool {
  1233. for _, r := range s {
  1234. if (r >= 0x3000 && r <= 0x9fff) || (r >= 0xac00 && r <= 0xd7af) || (r >= 0xff00 && r <= 0xffef) {
  1235. return true
  1236. }
  1237. }
  1238. return false
  1239. }
  1240. // isCJKFontName reports whether a typeface is a Chinese, Japanese or Korean
  1241. // one (by its name, which is all a document states)
  1242. func isCJKFontName(name string) bool {
  1243. l := strings.ToLower(name)
  1244. for _, r := range l {
  1245. if (r >= 0x2e80 && r <= 0x9fff) || (r >= 0xac00 && r <= 0xd7af) || (r >= 0xf900 && r <= 0xfaff) {
  1246. return true
  1247. }
  1248. }
  1249. for _, k := range []string{"mingliu", "mincho", "gothic", "simsun", "simhei", "yahei", "jhenghei",
  1250. "meiryo", "gulim", "batang", "dotum", "malgun", "gungsuh", "kaiti", "fangsong", "biaukai",
  1251. "dfkai", "hiragino", "pingfang", "heiti", "songti", "noto sans cjk", "noto serif cjk",
  1252. "noto sans tc", "noto sans sc", "noto sans jp", "noto sans kr", "source han", "yu gothic", "yu mincho"} {
  1253. if strings.Contains(l, k) {
  1254. return true
  1255. }
  1256. }
  1257. return false
  1258. }
  1259. // runCSSDiff renders the properties in which a run differs from its block
  1260. func runCSSDiff(run, block map[string]string) string {
  1261. var parts []string
  1262. for _, k := range cssRunKeys {
  1263. rv, bv := run[k], block[k]
  1264. if rv == bv {
  1265. continue
  1266. }
  1267. if rv == "" {
  1268. switch k {
  1269. case "background-color":
  1270. rv = "transparent"
  1271. case "font-variant", "text-transform":
  1272. rv = "normal"
  1273. if k == "text-transform" {
  1274. rv = "none"
  1275. }
  1276. default:
  1277. continue
  1278. }
  1279. }
  1280. parts = append(parts, k+":"+rv)
  1281. }
  1282. if len(parts) == 0 {
  1283. return ""
  1284. }
  1285. return strings.Join(parts, ";") + ";"
  1286. }
  1287. /* ---------------- runs ---------------- */
  1288. type runsResult struct {
  1289. segments []string // inline HTML, split at page breaks
  1290. preWrap bool
  1291. bookmark string
  1292. midText bool // the text so far ends in a non-space, so a leading space is kept
  1293. }
  1294. type inlinePiece struct {
  1295. css string // run style diff
  1296. vert string
  1297. link string
  1298. rev string // "ins" | "del": a tracked change
  1299. cmt string // the comment anchored here (editor id)
  1300. html string
  1301. raw bool // html is a complete element (no span wrapping)
  1302. }
  1303. // runs renders the inline content of a paragraph
  1304. func (cv *docxConv) runs(p *xmltree.Node, part *docxPartCtx, baseR, blockR docxRPr) runsResult {
  1305. res := runsResult{}
  1306. blockCSS := rPrCSS(blockR)
  1307. var pieces []inlinePiece
  1308. var segments []string
  1309. flush := func() {
  1310. segments = append(segments, joinPieces(pieces))
  1311. pieces = nil
  1312. }
  1313. var walk func(n *xmltree.Node, link string)
  1314. walk = func(n *xmltree.Node, link string) {
  1315. for i := range n.Nodes {
  1316. c := &n.Nodes[i]
  1317. switch c.XMLName.Local {
  1318. case "r":
  1319. cv.run(c, part, baseR, blockCSS, link, &pieces, &res, flush)
  1320. case "hyperlink":
  1321. href := ""
  1322. if id := c.AttrNS("relationships", "id"); id != "" {
  1323. href = part.rels[id]
  1324. }
  1325. if a := c.Attr("anchor"); a != "" && href == "" {
  1326. href = "#" + a
  1327. }
  1328. walk(c, href)
  1329. case "fldSimple":
  1330. instr := strings.ToUpper(strings.TrimSpace(c.Attr("instr")))
  1331. cv.fields = append(cv.fields, fieldFrame{instr: instr, inResult: true})
  1332. walk(c, link)
  1333. cv.fields = cv.fields[:len(cv.fields)-1]
  1334. case "smartTag", "customXml", "bdo", "dir":
  1335. walk(c, link)
  1336. case "ins", "moveTo", "del", "moveFrom":
  1337. prev := cv.curRev
  1338. if cv.curRev == "" {
  1339. cv.curRev = "ins"
  1340. if c.XMLName.Local == "del" || c.XMLName.Local == "moveFrom" {
  1341. cv.curRev = "del"
  1342. }
  1343. }
  1344. walk(c, link)
  1345. cv.curRev = prev
  1346. case "commentRangeStart":
  1347. cv.rv.start(c.Attr("id"))
  1348. case "commentRangeEnd":
  1349. cv.rv.end(c.Attr("id"))
  1350. case "sdt":
  1351. if sc := c.First("sdtContent"); sc != nil {
  1352. walk(sc, link)
  1353. }
  1354. case "AlternateContent":
  1355. if ch := c.First("Choice"); ch != nil {
  1356. walk(ch, link)
  1357. } else if fb := c.First("Fallback"); fb != nil {
  1358. walk(fb, link)
  1359. }
  1360. case "bookmarkStart":
  1361. name := c.Attr("name")
  1362. if res.bookmark == "" && name != "" && name != "_GoBack" {
  1363. res.bookmark = name
  1364. }
  1365. }
  1366. }
  1367. }
  1368. walk(p, "")
  1369. flush()
  1370. res.segments = segments
  1371. return res
  1372. }
  1373. // fieldHidden reports whether we are inside a field's instruction text
  1374. func (cv *docxConv) fieldHidden() bool {
  1375. for _, f := range cv.fields {
  1376. if !f.inResult {
  1377. return true
  1378. }
  1379. }
  1380. return false
  1381. }
  1382. // fieldKind names the innermost PAGE / NUMPAGES field being shown
  1383. func (cv *docxConv) fieldKind() string {
  1384. for i := len(cv.fields) - 1; i >= 0; i-- {
  1385. w := strings.Fields(cv.fields[i].instr)
  1386. if len(w) > 0 && (w[0] == "PAGE" || w[0] == "NUMPAGES") {
  1387. return w[0]
  1388. }
  1389. }
  1390. return ""
  1391. }
  1392. func (cv *docxConv) run(r *xmltree.Node, part *docxPartCtx, baseR docxRPr, blockCSS map[string]string,
  1393. link string, pieces *[]inlinePiece, res *runsResult, pageBreak func()) {
  1394. direct := parseRPr(r.First("rPr"))
  1395. eff := baseR
  1396. if direct.rStyle != "" {
  1397. eff.merge(cv.ss.charStyle(direct.rStyle))
  1398. }
  1399. eff.merge(direct)
  1400. if eff.vanish.v {
  1401. return
  1402. }
  1403. css := runCSSDiff(rPrCSS(eff), blockCSS)
  1404. vert := ""
  1405. if eff.vert == "superscript" || eff.vert == "subscript" {
  1406. vert = eff.vert
  1407. }
  1408. emit := func(html string, raw bool) {
  1409. if n := len(cv.fields); n > 0 && cv.fields[n-1].inResult {
  1410. cv.fields[n-1].shown = true
  1411. }
  1412. if kind := cv.fieldKind(); kind != "" && !raw {
  1413. html = `<span class="doc-field" data-field="` + kind + `">` + html + `</span>`
  1414. }
  1415. *pieces = append(*pieces, cv.reviewed(inlinePiece{css: css, vert: vert, link: link, html: html, raw: raw}))
  1416. }
  1417. for i := range r.Nodes {
  1418. c := &r.Nodes[i]
  1419. switch c.XMLName.Local {
  1420. case "fldChar":
  1421. switch c.Attr("fldCharType") {
  1422. case "begin":
  1423. cv.fields = append(cv.fields, fieldFrame{})
  1424. case "separate":
  1425. if n := len(cv.fields); n > 0 {
  1426. cv.fields[n-1].inResult = true
  1427. if isTocInstr(cv.fields[n-1].instr) {
  1428. cv.tocTouched = true
  1429. cv.tocMax = tocInstrMaxLevel(cv.fields[n-1].instr)
  1430. }
  1431. }
  1432. case "end":
  1433. if n := len(cv.fields); n > 0 {
  1434. if isTocInstr(cv.fields[n-1].instr) {
  1435. cv.tocTouched = true
  1436. }
  1437. // a page number with no stored result (Google Docs
  1438. // writes none) is still a page number: the layout fills
  1439. // in each page's own
  1440. if !cv.fields[n-1].shown && cv.fieldKind() != "" {
  1441. emit("1", false)
  1442. }
  1443. cv.fields = cv.fields[:n-1]
  1444. }
  1445. }
  1446. continue
  1447. case "instrText":
  1448. if n := len(cv.fields); n > 0 && !cv.fields[n-1].inResult {
  1449. cv.fields[n-1].instr += strings.ToUpper(c.Text)
  1450. cv.fields[n-1].instr = strings.TrimSpace(cv.fields[n-1].instr)
  1451. }
  1452. continue
  1453. }
  1454. if cv.fieldHidden() {
  1455. continue
  1456. }
  1457. switch c.XMLName.Local {
  1458. case "t", "delText":
  1459. t := c.Text
  1460. if t == "" {
  1461. continue
  1462. }
  1463. if strings.Contains(t, " ") || strings.Contains(t, "\t") || (strings.HasPrefix(t, " ") && !res.midText) {
  1464. res.preWrap = true
  1465. }
  1466. res.midText = !strings.HasSuffix(t, " ")
  1467. emit(xmltree.Escape(t), false)
  1468. case "tab":
  1469. emit(`<span class="doc-tab">`+"\t"+`</span>`, false)
  1470. case "ptab":
  1471. emit(`<span class="doc-tab">`+"\t"+`</span>`, false)
  1472. case "br", "cr":
  1473. if c.Attr("type") == "page" {
  1474. pageBreak()
  1475. continue
  1476. }
  1477. emit("<br>", true)
  1478. res.midText = false
  1479. case "noBreakHyphen":
  1480. emit("\u2011", false)
  1481. case "softHyphen":
  1482. emit("\u00ad", false)
  1483. case "sym":
  1484. if v, err := strconv.ParseUint(c.Attr("char"), 16, 32); err == nil {
  1485. // symbol fonts map glyphs into the private use area
  1486. if v >= 0xF000 && v <= 0xF0FF {
  1487. v -= 0xF000
  1488. }
  1489. if v >= 32 {
  1490. emit(xmltree.Escape(string(rune(v))), false)
  1491. }
  1492. }
  1493. case "footnoteReference":
  1494. id := c.Attr("id")
  1495. if onOff2(c.Attr("customMarkFollows")) {
  1496. continue
  1497. }
  1498. num, ok := cv.fnNumber[id]
  1499. if !ok {
  1500. num = len(cv.fnOrder) + 1
  1501. cv.fnNumber[id] = num
  1502. cv.fnOrder = append(cv.fnOrder, id)
  1503. }
  1504. // the reference is its own superscript: the run's vertAlign
  1505. // must not wrap it in a second one
  1506. *pieces = append(*pieces, cv.reviewed(inlinePiece{css: css, link: link, raw: true,
  1507. html: `<sup class="doc-fnref" data-fn="` + xmltree.Escape(id) + `" contenteditable="false">` + strconv.Itoa(num) + `</sup>`}))
  1508. case "footnoteRef":
  1509. // the number inside the footnote itself - the editor draws it
  1510. continue
  1511. case "drawing":
  1512. if img := cv.drawing(c, part); img != "" {
  1513. emit(img, true)
  1514. }
  1515. case "pict", "object":
  1516. if img := cv.vmlImage(c, part); img != "" {
  1517. emit(img, true)
  1518. }
  1519. case "AlternateContent":
  1520. if ch := c.First("Choice"); ch != nil {
  1521. if d := ch.First("drawing"); d != nil {
  1522. if img := cv.drawing(d, part); img != "" {
  1523. emit(img, true)
  1524. }
  1525. }
  1526. }
  1527. case "ruby":
  1528. var texts []string
  1529. xmltree.CollectText(c.First("rubyBase"), &texts)
  1530. if s := strings.Join(texts, ""); s != "" {
  1531. emit(xmltree.Escape(s), false)
  1532. }
  1533. }
  1534. }
  1535. }
  1536. // reviewed stamps the review state in force onto a piece
  1537. func (cv *docxConv) reviewed(p inlinePiece) inlinePiece {
  1538. p.rev = cv.curRev
  1539. p.cmt = cv.rv.current()
  1540. if p.cmt != "" {
  1541. cv.rv.used[p.cmt] = true
  1542. }
  1543. return p
  1544. }
  1545. // joinPieces merges adjacent pieces sharing one format into one span.
  1546. // Nesting, outermost first: comment anchor, link, tracked change, style.
  1547. func joinPieces(pieces []inlinePiece) string {
  1548. var sb strings.Builder
  1549. groupBy(len(pieces), func(a, b int) bool { return pieces[a].cmt == pieces[b].cmt }, func(i, j int) {
  1550. inner := joinLinks(pieces[i:j])
  1551. if cmt := pieces[i].cmt; cmt != "" {
  1552. sb.WriteString(`<span class="doc-cmt" data-cid="` + xmltree.Escape(cmt) + `">` + inner + `</span>`)
  1553. } else {
  1554. sb.WriteString(inner)
  1555. }
  1556. })
  1557. return sb.String()
  1558. }
  1559. // groupBy calls emit(i, j) for every run [i, j) of neighbours same says
  1560. // belong together
  1561. func groupBy(n int, same func(a, b int) bool, emit func(i, j int)) {
  1562. i := 0
  1563. for i < n {
  1564. j := i + 1
  1565. for j < n && same(i, j) {
  1566. j++
  1567. }
  1568. emit(i, j)
  1569. i = j
  1570. }
  1571. }
  1572. func joinLinks(pieces []inlinePiece) string {
  1573. var sb strings.Builder
  1574. groupBy(len(pieces), func(a, b int) bool { return pieces[a].link == pieces[b].link }, func(i, j int) {
  1575. var inner strings.Builder
  1576. groupBy(j-i, func(a, b int) bool { return pieces[i+a].rev == pieces[i+b].rev }, func(ri, rj int) {
  1577. h := joinStyled(pieces[i+ri : i+rj])
  1578. switch pieces[i+ri].rev {
  1579. case "ins":
  1580. h = `<ins class="doc-ins">` + h + `</ins>`
  1581. case "del":
  1582. h = `<del class="doc-del">` + h + `</del>`
  1583. }
  1584. inner.WriteString(h)
  1585. })
  1586. if link := pieces[i].link; link != "" {
  1587. sb.WriteString(`<a href="` + xmltree.Escape(link) + `" style="color:inherit;text-decoration:inherit;">` + inner.String() + `</a>`)
  1588. } else {
  1589. sb.WriteString(inner.String())
  1590. }
  1591. })
  1592. return sb.String()
  1593. }
  1594. func joinStyled(pieces []inlinePiece) string {
  1595. var sb strings.Builder
  1596. groupBy(len(pieces), func(a, b int) bool {
  1597. return pieces[a].css == pieces[b].css && pieces[a].vert == pieces[b].vert
  1598. }, func(i, j int) {
  1599. var text strings.Builder
  1600. for k := i; k < j; k++ {
  1601. text.WriteString(pieces[k].html)
  1602. }
  1603. h := text.String()
  1604. if css := pieces[i].css; css != "" {
  1605. h = `<span style="` + xmltree.Escape(css) + `">` + h + `</span>`
  1606. }
  1607. switch pieces[i].vert {
  1608. case "superscript":
  1609. h = "<sup>" + h + "</sup>"
  1610. case "subscript":
  1611. h = "<sub>" + h + "</sub>"
  1612. }
  1613. sb.WriteString(h)
  1614. })
  1615. return sb.String()
  1616. }
  1617. func stripTags(s string) string {
  1618. return tagRe.ReplaceAllString(s, "")
  1619. }
  1620. /* ---------------- pictures ---------------- */
  1621. func (cv *docxConv) mediaData(part *docxPartCtx, rid string) (string, bool) {
  1622. data, ext, ok := cv.mediaBytes(part, rid)
  1623. if !ok {
  1624. return "", false
  1625. }
  1626. return media.EncodeDataURL(data, ext), true
  1627. }
  1628. // mediaBytes resolves an embedded picture to its bytes and image format
  1629. func (cv *docxConv) mediaBytes(part *docxPartCtx, rid string) ([]byte, string, bool) {
  1630. target, ok := part.rels[rid]
  1631. if !ok || part.ext[rid] {
  1632. return nil, "", false
  1633. }
  1634. dir := path.Dir(part.name)
  1635. mediaPath := xmltree.ResolvePart(dir, target)
  1636. data, ok := cv.files[mediaPath]
  1637. if !ok {
  1638. return nil, "", false
  1639. }
  1640. ext := strings.TrimPrefix(strings.ToLower(path.Ext(mediaPath)), ".")
  1641. switch ext {
  1642. case "jpg":
  1643. ext = "jpeg"
  1644. case "emf", "wmf":
  1645. // a Windows metafile is rendered to a bitmap the browser can show
  1646. return metafile.BrowserPicture(data, ext)
  1647. case "tif", "tiff":
  1648. // browsers cannot show these - keep the space, lose the picture
  1649. return nil, "", false
  1650. }
  1651. return data, ext, true
  1652. }
  1653. // drawing converts a DrawingML picture (inline or anchored)
  1654. func (cv *docxConv) drawing(n *xmltree.Node, part *docxPartCtx) string {
  1655. holder := n.First("inline")
  1656. anchor := false
  1657. if holder == nil {
  1658. holder = n.First("anchor")
  1659. anchor = holder != nil
  1660. }
  1661. if holder == nil {
  1662. return ""
  1663. }
  1664. var blips []*xmltree.Node
  1665. holder.FindAll("blip", &blips)
  1666. if len(blips) == 0 {
  1667. return ""
  1668. }
  1669. rid := blips[0].AttrNS("relationships", "embed")
  1670. if rid == "" {
  1671. rid = blips[0].Attr("embed")
  1672. }
  1673. data, format, ok := cv.mediaBytes(part, rid)
  1674. if !ok {
  1675. return ""
  1676. }
  1677. wPt, hPt := 0.0, 0.0
  1678. if ext := holder.First("extent"); ext != nil {
  1679. wPt = twipsAttr(ext, "cx", 0) / units.EmuPerPt
  1680. hPt = twipsAttr(ext, "cy", 0) / units.EmuPerPt
  1681. }
  1682. // a picture turned by quarter turns or mirrored: bake it into the bitmap
  1683. turns, flipH, flipV := 0, false, false
  1684. if spPr := findFirst(holder, "spPr"); spPr != nil {
  1685. if xf := spPr.First("xfrm"); xf != nil {
  1686. if t, ok := pictureTurns(twipsAttr(xf, "rot", 0)); ok {
  1687. turns = t
  1688. }
  1689. flipH, flipV = onOff2(xf.Attr("flipH")), onOff2(xf.Attr("flipV"))
  1690. }
  1691. }
  1692. if turns != 0 || flipH || flipV {
  1693. if d, f, ok := orientPicture(data, format, turns, flipH, flipV); ok {
  1694. data, format = d, f
  1695. if turns%2 == 1 {
  1696. wPt, hPt = hPt, wPt
  1697. }
  1698. } else {
  1699. turns, flipH, flipV = 0, false, false
  1700. }
  1701. }
  1702. src := media.EncodeDataURL(data, format)
  1703. var css []string
  1704. if wPt > 0 && hPt > 0 {
  1705. css = append(css, "width:"+ptStr(wPt), "height:"+ptStr(hPt))
  1706. }
  1707. // crop: srcRect is in thousandths of a percent of the source
  1708. var rects []*xmltree.Node
  1709. holder.FindAll("srcRect", &rects)
  1710. if len(rects) > 0 {
  1711. t := twipsAttr(rects[0], "t", 0) / 1000
  1712. r := twipsAttr(rects[0], "r", 0) / 1000
  1713. b := twipsAttr(rects[0], "b", 0) / 1000
  1714. l := twipsAttr(rects[0], "l", 0) / 1000
  1715. if t != 0 || r != 0 || b != 0 || l != 0 {
  1716. o := orientInsets([4]float64{t, r, b, l}, turns, flipH, flipV)
  1717. t, r, b, l = o[0], o[1], o[2], o[3]
  1718. css = append(css, fmt.Sprintf("object-fit:fill;object-view-box:inset(%s%% %s%% %s%% %s%%)",
  1719. units.TrimFloat(round3(t)), units.TrimFloat(round3(r)), units.TrimFloat(round3(b)), units.TrimFloat(round3(l))))
  1720. }
  1721. }
  1722. // a picture outline
  1723. borderPt := 0.0
  1724. if spPr := findFirst(holder, "spPr"); spPr != nil {
  1725. if ln := spPr.First("ln"); ln != nil && ln.First("noFill") == nil {
  1726. if fill := ln.First("solidFill"); fill != nil {
  1727. col := "000000"
  1728. if c := fill.First("srgbClr"); c != nil && len(c.Attr("val")) == 6 {
  1729. col = strings.ToLower(c.Attr("val"))
  1730. }
  1731. w := twipsAttr(ln, "w", 9525) / units.EmuPerPt
  1732. css = append(css, "border:"+ptStr(w)+" solid #"+col)
  1733. borderPt = w
  1734. }
  1735. }
  1736. }
  1737. if !anchor {
  1738. // room beside the frame: the effect extent beyond the outline (what
  1739. // the docx writer stores a picture's side margins in), else Google
  1740. // Docs' own 1.5pt (the space above and below a picture is the
  1741. // layout engine's business - see pictureLines in docs_layout.js)
  1742. ml, mr := 0.0, 0.0
  1743. if ee := holder.First("effectExtent"); ee != nil && turns%2 == 0 {
  1744. // (a quarter-turned frame keeps its turn in the extent instead)
  1745. ml = twipsAttr(ee, "l", 0)/units.EmuPerPt - borderPt
  1746. mr = twipsAttr(ee, "r", 0)/units.EmuPerPt - borderPt
  1747. }
  1748. if ml < 0.05 && mr < 0.05 && cv.gdocs {
  1749. ml, mr = 1.5, 1.5
  1750. }
  1751. if ml >= 0.05 {
  1752. css = append(css, "margin-left:"+ptStr(ml))
  1753. }
  1754. if mr >= 0.05 {
  1755. css = append(css, "margin-right:"+ptStr(mr))
  1756. }
  1757. }
  1758. alt := ""
  1759. if dp := holder.First("docPr"); dp != nil {
  1760. alt = dp.Attr("descr")
  1761. }
  1762. cls := ""
  1763. if anchor {
  1764. x, y := 0.0, 0.0
  1765. if ph := holder.First("positionH"); ph != nil {
  1766. if off := ph.First("posOffset"); off != nil {
  1767. if v, err := strconv.ParseFloat(strings.TrimSpace(off.Text), 64); err == nil {
  1768. x = v / units.EmuPerPt
  1769. }
  1770. }
  1771. switch ph.Attr("relativeFrom") {
  1772. case "page":
  1773. x -= cv.marginL
  1774. }
  1775. if al := ph.First("align"); al != nil {
  1776. switch strings.TrimSpace(al.Text) {
  1777. case "center":
  1778. x = (cv.textW - wPt) / 2
  1779. case "right":
  1780. x = cv.textW - wPt
  1781. }
  1782. }
  1783. }
  1784. if pv := holder.First("positionV"); pv != nil {
  1785. if off := pv.First("posOffset"); off != nil && (pv.Attr("relativeFrom") == "paragraph" || pv.Attr("relativeFrom") == "line") {
  1786. if v, err := strconv.ParseFloat(strings.TrimSpace(off.Text), 64); err == nil {
  1787. y = v / units.EmuPerPt
  1788. }
  1789. }
  1790. }
  1791. square := holder.First("wrapSquare") != nil || holder.First("wrapTight") != nil ||
  1792. holder.First("wrapThrough") != nil
  1793. if square {
  1794. // text flows around it: float to the side it sits on
  1795. side := "left"
  1796. if x+wPt/2 > cv.textW/2 {
  1797. side = "right"
  1798. }
  1799. css = append(css, "float:"+side, "max-width:none")
  1800. if side == "left" && x > 0 {
  1801. css = append(css, "margin-left:"+ptStr(x))
  1802. }
  1803. css = append(css, "margin-right:9pt", "margin-left:9pt")
  1804. } else {
  1805. css = append(css, "display:block", "max-width:none")
  1806. if x != 0 {
  1807. css = append(css, "margin-left:"+ptStr(x))
  1808. }
  1809. if y != 0 {
  1810. css = append(css, "margin-top:"+ptStr(y))
  1811. }
  1812. }
  1813. cls = ` class="doc-anchor"`
  1814. }
  1815. out := `<img src="` + src + `"` + cls
  1816. if alt != "" {
  1817. out += ` alt="` + xmltree.Escape(alt) + `"`
  1818. }
  1819. if len(css) > 0 {
  1820. out += ` style="` + strings.Join(css, ";") + `;"`
  1821. }
  1822. return out + ">"
  1823. }
  1824. func findFirst(n *xmltree.Node, local string) *xmltree.Node {
  1825. var all []*xmltree.Node
  1826. n.FindAll(local, &all)
  1827. if len(all) == 0 {
  1828. return nil
  1829. }
  1830. return all[0]
  1831. }
  1832. // vmlImage converts a legacy VML picture (w:pict / w:object)
  1833. func (cv *docxConv) vmlImage(n *xmltree.Node, part *docxPartCtx) string {
  1834. var datas []*xmltree.Node
  1835. n.FindAll("imagedata", &datas)
  1836. if len(datas) == 0 {
  1837. return ""
  1838. }
  1839. rid := datas[0].AttrNS("relationships", "id")
  1840. if rid == "" {
  1841. rid = datas[0].Attr("id")
  1842. }
  1843. src, ok := cv.mediaData(part, rid)
  1844. if !ok {
  1845. return ""
  1846. }
  1847. style := ""
  1848. var shapes []*xmltree.Node
  1849. n.FindAll("shape", &shapes)
  1850. if len(shapes) > 0 {
  1851. st := shapes[0].Attr("style")
  1852. w := cssLengthPt(htmlutil.StyleProp(st, "width"))
  1853. h := cssLengthPt(htmlutil.StyleProp(st, "height"))
  1854. if w > 0 && h > 0 {
  1855. style = ` style="width:` + ptStr(w) + `;height:` + ptStr(h) + `;"`
  1856. }
  1857. }
  1858. return `<img src="` + src + `"` + style + `>`
  1859. }
  1860. // cssLengthPt converts a CSS length (pt, px, in, cm, mm) to points
  1861. func cssLengthPt(s string) float64 {
  1862. s = strings.TrimSpace(strings.ToLower(s))
  1863. units := []struct {
  1864. suf string
  1865. k float64
  1866. }{{"pt", 1}, {"px", 0.75}, {"in", 72}, {"cm", 72 / 2.54}, {"mm", 72 / 25.4}}
  1867. for _, u := range units {
  1868. if strings.HasSuffix(s, u.suf) {
  1869. if v, err := strconv.ParseFloat(strings.TrimSuffix(s, u.suf), 64); err == nil {
  1870. return v * u.k
  1871. }
  1872. }
  1873. }
  1874. if v, err := strconv.ParseFloat(s, 64); err == nil {
  1875. return v * 0.75
  1876. }
  1877. return 0
  1878. }
  1879. /* ---------------- tables ---------------- */
  1880. type docxCell struct {
  1881. node *xmltree.Node
  1882. tcPr *xmltree.Node
  1883. gridCol int
  1884. span int
  1885. vMerge string // restart | continue | ""
  1886. rowspan int
  1887. }
  1888. func (cv *docxConv) table(tbl *xmltree.Node, part *docxPartCtx, spanAll bool) string {
  1889. direct := parseTblPr(tbl.First("tblPr"))
  1890. tp := *cv.ss.tableStyle(direct.style)
  1891. tp.merge(direct)
  1892. // grid
  1893. var grid []float64
  1894. if g := tbl.First("tblGrid"); g != nil {
  1895. for _, gc := range g.All("gridCol") {
  1896. grid = append(grid, twipsAttr(gc, "w", 0)/20)
  1897. }
  1898. }
  1899. // rows and cells with their grid positions
  1900. type rowInfo struct {
  1901. node *xmltree.Node
  1902. cells []*docxCell
  1903. }
  1904. var rows []*rowInfo
  1905. for i := range tbl.Nodes {
  1906. tr := &tbl.Nodes[i]
  1907. if tr.XMLName.Local != "tr" {
  1908. continue
  1909. }
  1910. ri := &rowInfo{node: tr}
  1911. col := 0
  1912. if trPr := tr.First("trPr"); trPr != nil {
  1913. if gb := trPr.First("gridBefore"); gb != nil {
  1914. if v, err := strconv.Atoi(gb.Attr("val")); err == nil {
  1915. col += v
  1916. }
  1917. }
  1918. }
  1919. var addCells func(parent *xmltree.Node)
  1920. addCells = func(parent *xmltree.Node) {
  1921. for j := range parent.Nodes {
  1922. tc := &parent.Nodes[j]
  1923. switch tc.XMLName.Local {
  1924. case "tc":
  1925. c := &docxCell{node: tc, tcPr: tc.First("tcPr"), gridCol: col, span: 1, rowspan: 1}
  1926. if c.tcPr != nil {
  1927. if gs := c.tcPr.First("gridSpan"); gs != nil {
  1928. if v, err := strconv.Atoi(gs.Attr("val")); err == nil && v > 1 {
  1929. c.span = v
  1930. }
  1931. }
  1932. if vm := c.tcPr.First("vMerge"); vm != nil {
  1933. c.vMerge = vm.Attr("val")
  1934. if c.vMerge == "" {
  1935. c.vMerge = "continue"
  1936. }
  1937. }
  1938. }
  1939. ri.cells = append(ri.cells, c)
  1940. col += c.span
  1941. case "sdt":
  1942. if sc := tc.First("sdtContent"); sc != nil {
  1943. addCells(sc)
  1944. }
  1945. case "customXml":
  1946. addCells(tc)
  1947. }
  1948. }
  1949. }
  1950. addCells(tr)
  1951. rows = append(rows, ri)
  1952. }
  1953. // vertical merges -> rowspan on the restarting cell
  1954. for r, ri := range rows {
  1955. for _, c := range ri.cells {
  1956. if c.vMerge != "restart" {
  1957. continue
  1958. }
  1959. for r2 := r + 1; r2 < len(rows); r2++ {
  1960. found := false
  1961. for _, c2 := range rows[r2].cells {
  1962. if c2.gridCol == c.gridCol && c2.vMerge == "continue" {
  1963. found = true
  1964. c.rowspan++
  1965. break
  1966. }
  1967. }
  1968. if !found {
  1969. break
  1970. }
  1971. }
  1972. }
  1973. }
  1974. nCols := len(grid)
  1975. for _, ri := range rows {
  1976. n := 0
  1977. for _, c := range ri.cells {
  1978. n = maxInt(n, c.gridCol+c.span)
  1979. }
  1980. nCols = maxInt(nCols, n)
  1981. }
  1982. for len(grid) < nCols {
  1983. grid = append(grid, cv.textW/float64(maxInt(nCols, 1)))
  1984. }
  1985. totalW := 0.0
  1986. for _, w := range grid {
  1987. totalW += w
  1988. }
  1989. if tp.width.set && tp.widthType == "dxa" && tp.width.v > 0 && totalW == 0 {
  1990. totalW = tp.width.v / 20
  1991. }
  1992. var css []string
  1993. if totalW > 0 {
  1994. // the grid decides the columns, not the content (Word's fixed
  1995. // layout, and what Google Docs always does)
  1996. css = append(css, "width:"+ptStr(totalW), "table-layout:fixed")
  1997. }
  1998. switch tp.jc {
  1999. case "center":
  2000. css = append(css, "margin-left:auto", "margin-right:auto")
  2001. case "right", "end":
  2002. css = append(css, "margin-left:auto", "margin-right:0")
  2003. default:
  2004. if tp.ind.set && tp.ind.v != 0 {
  2005. css = append(css, "margin-left:"+ptStr(tp.ind.v/20))
  2006. }
  2007. }
  2008. cls := "of-table"
  2009. if spanAll {
  2010. cls += " col-span-all"
  2011. }
  2012. // data-docx: the table lays out as Word's (no spacing around it) -
  2013. // except one the editor made, which keeps the editor's own
  2014. docxAttr := ` data-docx="1"`
  2015. if pr := tbl.First("tblPr"); pr != nil {
  2016. if st := pr.First("tblStyle"); st != nil && st.Attr("val") == editorTableStyle {
  2017. docxAttr = ""
  2018. }
  2019. }
  2020. var sb strings.Builder
  2021. sb.WriteString(`<table class="` + cls + `"` + docxAttr + ` style="` + strings.Join(css, ";") + `;">`)
  2022. if len(grid) > 0 {
  2023. sb.WriteString("<colgroup>")
  2024. for _, w := range grid {
  2025. sb.WriteString(`<col style="width:` + ptStr(w) + `">`)
  2026. }
  2027. sb.WriteString("</colgroup>")
  2028. }
  2029. sb.WriteString("<tbody>")
  2030. for r, ri := range rows {
  2031. trStyle := ""
  2032. trData := ""
  2033. if trPr := ri.node.First("trPr"); trPr != nil {
  2034. if th := trPr.First("trHeight"); th != nil {
  2035. if v := numAttr(th, "val"); v.set && v.v > 0 {
  2036. trStyle = ` style="height:` + ptStr(v.v/20) + `;"`
  2037. if th.Attr("hRule") == "exact" {
  2038. trData += ` data-exact="1"`
  2039. }
  2040. }
  2041. }
  2042. if onOff(trPr.First("cantSplit")).v {
  2043. trData += ` data-cant-split="1"`
  2044. }
  2045. if onOff(trPr.First("tblHeader")).v {
  2046. trData += ` data-header-row="1"`
  2047. }
  2048. }
  2049. sb.WriteString("<tr" + trStyle + trData + ">")
  2050. for _, c := range ri.cells {
  2051. if c.vMerge == "continue" {
  2052. continue
  2053. }
  2054. sb.WriteString(cv.cell(c, tp, r, len(rows), nCols, c.rowspan, part))
  2055. }
  2056. sb.WriteString("</tr>")
  2057. }
  2058. sb.WriteString("</tbody></table>")
  2059. return sb.String()
  2060. }
  2061. func (cv *docxConv) cell(c *docxCell, tp docxTblPr, row, nRows, nCols, rowspan int, part *docxPartCtx) string {
  2062. var tcBorders map[string]docxBorder
  2063. var tcMar map[string]optNum
  2064. shd, vAlign := "", ""
  2065. if c.tcPr != nil {
  2066. tcBorders = parseBorderSet(c.tcPr.First("tcBorders"))
  2067. tcMar = parseMarginSet(c.tcPr.First("tcMar"))
  2068. if s := c.tcPr.First("shd"); s != nil {
  2069. if f := strings.ToLower(s.Attr("fill")); len(f) == 6 {
  2070. shd = f
  2071. }
  2072. }
  2073. if va := c.tcPr.First("vAlign"); va != nil {
  2074. vAlign = va.Attr("val")
  2075. }
  2076. }
  2077. lastRow := row+rowspan >= nRows
  2078. firstCol := c.gridCol == 0
  2079. lastCol := c.gridCol+c.span >= nCols
  2080. side := func(name, outer, inner string, isOuter bool) string {
  2081. if b, ok := tcBorders[name]; ok {
  2082. if !b.visible() {
  2083. return "none"
  2084. }
  2085. return borderCSS(b)
  2086. }
  2087. key := inner
  2088. if isOuter {
  2089. key = outer
  2090. }
  2091. if b, ok := tp.borders[key]; ok && b.visible() {
  2092. return borderCSS(b)
  2093. }
  2094. return "none"
  2095. }
  2096. var css []string
  2097. css = append(css,
  2098. "border-top:"+side("top", "top", "insideH", row == 0),
  2099. "border-right:"+side("right", "right", "insideV", lastCol),
  2100. "border-bottom:"+side("bottom", "bottom", "insideH", lastRow),
  2101. "border-left:"+side("left", "left", "insideV", firstCol))
  2102. mar := func(name string, def float64) float64 {
  2103. if v, ok := tcMar[name]; ok {
  2104. return v.v / 20
  2105. }
  2106. if v, ok := tp.cellMar[name]; ok {
  2107. return v.v / 20
  2108. }
  2109. return def
  2110. }
  2111. // Word's defaults when nothing states a margin: 0.08" left and right
  2112. css = append(css, "padding:"+ptStr(mar("top", 0))+" "+ptStr(mar("right", 5.4))+" "+
  2113. ptStr(mar("bottom", 0))+" "+ptStr(mar("left", 5.4)))
  2114. switch vAlign {
  2115. case "center":
  2116. css = append(css, "vertical-align:middle")
  2117. case "bottom":
  2118. css = append(css, "vertical-align:bottom")
  2119. default:
  2120. css = append(css, "vertical-align:top")
  2121. }
  2122. if shd != "" && shd != "auto" {
  2123. css = append(css, "background-color:#"+shd)
  2124. }
  2125. attrs := ""
  2126. if c.span > 1 {
  2127. attrs += ` colspan="` + strconv.Itoa(c.span) + `"`
  2128. }
  2129. if rowspan > 1 {
  2130. attrs += ` rowspan="` + strconv.Itoa(rowspan) + `"`
  2131. }
  2132. inner := cv.blocks(c.node, part, blockCtx{cell: true})
  2133. if inner == "" {
  2134. inner = "<p><br></p>"
  2135. }
  2136. return "<td" + attrs + ` style="` + strings.Join(css, ";") + `;">` + inner + "</td>"
  2137. }