odp_reader.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. package odf
  2. /*
  3. odp_reader.go - Parse an OpenDocument Presentation (.odp) into a
  4. Presentation, scaled into the editor space (960 wide, the page's shape).
  5. ODF states appearance the same way PresentationML does: almost never on
  6. the element itself. A paragraph names a style, that style names a parent,
  7. the parent lives in styles.xml, and a frame with presentation:class="title"
  8. takes its position and its typography from the matching placeholder on the
  9. master page. Reading only what a shape spells out - which is what the
  10. first version of this reader did - gives every text box the same 24px
  11. default and drops the deck's whole design.
  12. So the reader resolves, lowest priority first:
  13. styles.xml office:styles the named style and its parents
  14. styles.xml automatic-styles the master page's own styles
  15. content.xml automatic-styles the per-document generated styles
  16. the paragraph's style, then the span's
  17. and draws each page on top of its master page's decoration, the way an
  18. ODF viewer does.
  19. */
  20. import (
  21. "encoding/base64"
  22. "errors"
  23. "fmt"
  24. "math"
  25. "strconv"
  26. "strings"
  27. "imuslab.com/arozos/mod/office/internal/fonts"
  28. "imuslab.com/arozos/mod/office/internal/slidetext"
  29. "imuslab.com/arozos/mod/office/internal/units"
  30. "imuslab.com/arozos/mod/office/internal/xmltree"
  31. "imuslab.com/arozos/mod/office/metafile"
  32. "imuslab.com/arozos/mod/office/model"
  33. )
  34. // odpStyle is a resolved ODF style: the graphic, paragraph and text
  35. // properties that matter to the editor, folded down the parent chain
  36. type odpStyle struct {
  37. // text
  38. SizePt float64
  39. Font string
  40. Bold bool
  41. Italic bool
  42. Underline bool
  43. Strike bool
  44. Color string
  45. Highlight string
  46. // paragraph
  47. Align string
  48. LineHeight float64 // unitless
  49. SpaceTop float64 // px
  50. SpaceBot float64 // px
  51. MarginLeft float64 // px
  52. Indent float64 // px
  53. // graphic
  54. Fill string // "" unset, "none", or "#rrggbb"
  55. Stroke string
  56. StrokeW float64
  57. Dash bool
  58. VAlign string // top | middle | bottom
  59. PadT float64
  60. PadR float64
  61. PadB float64
  62. PadL float64
  63. HasPad bool
  64. AutoGrow bool
  65. Rotation float64
  66. ArrowEnd bool
  67. ArrowHead bool
  68. }
  69. var odpDefaultStyle = odpStyle{SizePt: 18, Align: "left", Color: "#000000"}
  70. // ParseOdp converts raw .odp bytes into a Presentation
  71. func ParseOdp(data []byte) (*model.Presentation, error) {
  72. files, mime, err := readOdfZip(data)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if mime != "" && mime != odpMime {
  77. return nil, errors.New("not an OpenDocument presentation (mimetype " + mime + ")")
  78. }
  79. content, ok := files["content.xml"]
  80. if !ok {
  81. return nil, errors.New("odp is missing content.xml")
  82. }
  83. tree, err := parseOdfXML(content)
  84. if err != nil {
  85. return nil, errors.New("cannot parse content.xml: " + err.Error())
  86. }
  87. root := tree.first("document-content")
  88. if root == nil {
  89. return nil, errors.New("content.xml has no document-content root")
  90. }
  91. cv := &odpConverter{
  92. files: files,
  93. sx: 1,
  94. sy: 1,
  95. styles: map[string]*onode{},
  96. lists: map[string]*onode{},
  97. pageBg: map[string]string{},
  98. }
  99. pageW, pageH := model.SlidePxW, model.SlidePxH
  100. var stylesRoot *onode
  101. if raw, ok := files["styles.xml"]; ok {
  102. if st, err := parseOdfXML(raw); err == nil {
  103. stylesRoot = st.first("document-styles")
  104. }
  105. }
  106. if stylesRoot != nil {
  107. // source page size -> the editor's: 960 wide, the page's shape
  108. for _, auto := range []*onode{stylesRoot.first("automatic-styles")} {
  109. if auto == nil {
  110. continue
  111. }
  112. for _, pl := range auto.all("page-layout") {
  113. pp := pl.first("page-layout-properties")
  114. if pp == nil {
  115. continue
  116. }
  117. wPx := odfLenToPx(pp.attr("page-width"))
  118. hPx := odfLenToPx(pp.attr("page-height"))
  119. if wPx > 0 && hPx > 0 {
  120. pageW, pageH = model.SlideSizeFor(wPx, hPx)
  121. cv.sx = float64(pageW) / wPx
  122. cv.sy = float64(pageH) / hPx
  123. }
  124. }
  125. }
  126. cv.collectStyles(stylesRoot.first("styles"))
  127. cv.collectStyles(stylesRoot.first("automatic-styles"))
  128. if ms := stylesRoot.first("master-styles"); ms != nil {
  129. cv.masters = map[string]*onode{}
  130. for _, mp := range ms.all("master-page") {
  131. cv.masters[mp.attr("name")] = mp
  132. }
  133. }
  134. }
  135. cv.collectStyles(root.first("automatic-styles"))
  136. pres := root.path("body", "presentation")
  137. if pres == nil {
  138. return nil, errors.New("odp has no presentation body")
  139. }
  140. out := &model.Presentation{Size: []int{pageW, pageH}, Slides: []*model.Slide{}}
  141. for pi, page := range pres.all("page") {
  142. slide := &model.Slide{ID: fmt.Sprintf("s-odp-%d", pi+1), Objects: []*model.Object{}}
  143. master := cv.masterOf(page)
  144. slide.Bg = cv.backgroundOf(page, master)
  145. slide.Hidden = cv.hiddenPage(page)
  146. cv.master = master
  147. // the master page's own drawing sits underneath the slide's
  148. if master != nil {
  149. cv.walkPage(master, slide, true)
  150. }
  151. cv.walkPage(page, slide, false)
  152. for _, c := range page.children {
  153. if c.el != nil && c.el.name == "notes" {
  154. slide.Notes = strings.TrimSpace(c.el.allText())
  155. }
  156. }
  157. out.Slides = append(out.Slides, slide)
  158. }
  159. if len(out.Slides) == 0 {
  160. return nil, errors.New("no slides found in odp")
  161. }
  162. return out, nil
  163. }
  164. type odpConverter struct {
  165. files map[string][]byte
  166. sx, sy float64
  167. styles map[string]*onode
  168. lists map[string]*onode
  169. masters map[string]*onode
  170. master *onode
  171. pageBg map[string]string
  172. seq int
  173. // runs of the text body being built (see buildText)
  174. runs *odpRunFlags
  175. }
  176. // odpRunFlags gathers whether every run of a text body is bold / italic /
  177. // underlined - the only case in which the box itself may say so
  178. type odpRunFlags struct {
  179. any, bold, italic, under bool
  180. }
  181. // collectStyles indexes every style:style and text:list-style in a container
  182. func (cv *odpConverter) collectStyles(container *onode) {
  183. if container == nil {
  184. return
  185. }
  186. for _, st := range container.all("style") {
  187. if n := st.attr("name"); n != "" {
  188. cv.styles[n] = st
  189. }
  190. }
  191. for _, st := range container.all("list-style") {
  192. if n := st.attr("name"); n != "" {
  193. cv.lists[n] = st
  194. }
  195. }
  196. for _, st := range container.all("default-style") {
  197. if f := st.attr("family"); f != "" {
  198. cv.styles["#default-"+f] = st
  199. }
  200. }
  201. for _, dp := range container.all("style") {
  202. if dp.attr("family") != "drawing-page" {
  203. continue
  204. }
  205. if props := dp.first("drawing-page-properties"); props != nil {
  206. if c := props.attr("fill-color"); strings.HasPrefix(c, "#") {
  207. cv.pageBg[dp.attr("name")] = strings.ToLower(c)
  208. }
  209. }
  210. }
  211. }
  212. func (cv *odpConverter) masterOf(page *onode) *onode {
  213. if cv.masters == nil {
  214. return nil
  215. }
  216. return cv.masters[page.attr("master-page-name")]
  217. }
  218. // backgroundOf resolves a page's fill, falling back to its master's
  219. func (cv *odpConverter) backgroundOf(page, master *onode) string {
  220. if bg, ok := cv.pageBg[page.attr("style-name")]; ok {
  221. return bg
  222. }
  223. if master != nil {
  224. if bg, ok := cv.pageBg[master.attr("style-name")]; ok {
  225. return bg
  226. }
  227. if bg, ok := cv.pageBg[master.attr("page-layout-name")]; ok {
  228. return bg
  229. }
  230. }
  231. return ""
  232. }
  233. // hiddenPage reports a slide the show skips: presentation:visibility
  234. // "hidden" on the page's drawing-page style (LibreOffice's Hide Slide)
  235. func (cv *odpConverter) hiddenPage(page *onode) bool {
  236. st, ok := cv.styles[page.attr("style-name")]
  237. if !ok || st == nil {
  238. return false
  239. }
  240. props := st.first("drawing-page-properties")
  241. return props != nil && props.attr("visibility") == "hidden"
  242. }
  243. // walkPage draws every shape on a page. When masterOnly is set the
  244. // placeholders are skipped: on a master page they are prototypes that
  245. // state where a slide's title and body go, not content of their own.
  246. func (cv *odpConverter) walkPage(page *onode, slide *model.Slide, masterOnly bool) {
  247. for _, c := range page.children {
  248. if c.el == nil {
  249. continue
  250. }
  251. if masterOnly && c.el.attr("class") != "" {
  252. continue
  253. }
  254. cv.drawShape(c.el, slide, 0, 0, 1, 1)
  255. }
  256. }
  257. // drawShape converts one drawing element, recursing into groups. ox/oy and
  258. // kx/ky carry the transform a draw:g imposes on its children.
  259. func (cv *odpConverter) drawShape(n *onode, slide *model.Slide, ox, oy, kx, ky float64) {
  260. switch n.name {
  261. case "frame":
  262. cv.frame(n, slide, ox, oy, kx, ky)
  263. case "rect", "ellipse", "circle", "custom-shape", "polygon", "regular-polygon":
  264. cv.shape(n, slide, ox, oy, kx, ky)
  265. case "line", "connector":
  266. cv.line(n, slide, ox, oy, kx, ky)
  267. case "g":
  268. // a group has no transform of its own in ODF - its children carry
  269. // absolute coordinates - so it is simply flattened
  270. for _, c := range n.children {
  271. if c.el != nil {
  272. cv.drawShape(c.el, slide, ox, oy, kx, ky)
  273. }
  274. }
  275. }
  276. }
  277. /* ---------------- style resolution ---------------- */
  278. // resolveStyle folds a style and its parent chain into one odpStyle
  279. func (cv *odpConverter) resolveStyle(name string, base odpStyle) odpStyle {
  280. if name == "" {
  281. return base
  282. }
  283. // walk to the root of the parent chain first, then apply downwards so
  284. // the most specific style wins
  285. var chain []*onode
  286. seen := map[string]bool{}
  287. for cur := name; cur != "" && !seen[cur]; {
  288. seen[cur] = true
  289. st, ok := cv.styles[cur]
  290. if !ok {
  291. break
  292. }
  293. chain = append([]*onode{st}, chain...)
  294. cur = st.attr("parent-style-name")
  295. }
  296. out := base
  297. for _, st := range chain {
  298. cv.applyStyleNode(&out, st)
  299. }
  300. return out
  301. }
  302. // applyStyleNode folds one style:style element's properties into dst
  303. func (cv *odpConverter) applyStyleNode(dst *odpStyle, st *onode) {
  304. if tp := st.first("text-properties"); tp != nil {
  305. applyOdfTextProps(dst, tp)
  306. }
  307. if pp := st.first("paragraph-properties"); pp != nil {
  308. cv.applyOdfParaProps(dst, pp)
  309. }
  310. if gp := st.first("graphic-properties"); gp != nil {
  311. cv.applyOdfGraphicProps(dst, gp)
  312. }
  313. }
  314. func applyOdfTextProps(dst *odpStyle, tp *onode) {
  315. if v := tp.attr("font-size"); v != "" {
  316. if pt := odfFontSizePt(v, dst.SizePt); pt > 0 {
  317. dst.SizePt = pt
  318. }
  319. }
  320. if v := tp.attr("font-name"); v != "" {
  321. dst.Font = v
  322. }
  323. if v := tp.attr("font-family"); v != "" {
  324. dst.Font = strings.Trim(v, "'\"")
  325. }
  326. if v := tp.attr("font-weight"); v != "" {
  327. dst.Bold = v == "bold" || units.ParseNum(v) >= 600
  328. }
  329. if v := tp.attr("font-style"); v != "" {
  330. dst.Italic = v == "italic" || v == "oblique"
  331. }
  332. if v := tp.attr("text-underline-style"); v != "" {
  333. dst.Underline = v != "none"
  334. }
  335. if v := tp.attr("text-line-through-style"); v != "" {
  336. dst.Strike = v != "none"
  337. }
  338. if v := tp.attr("color"); strings.HasPrefix(v, "#") {
  339. dst.Color = strings.ToLower(v)
  340. }
  341. if v := tp.attr("background-color"); strings.HasPrefix(v, "#") {
  342. dst.Highlight = strings.ToLower(v)
  343. }
  344. }
  345. func (cv *odpConverter) applyOdfParaProps(dst *odpStyle, pp *onode) {
  346. switch pp.attr("text-align") {
  347. case "center":
  348. dst.Align = "center"
  349. case "end", "right":
  350. dst.Align = "right"
  351. case "justify":
  352. dst.Align = "justify"
  353. case "start", "left":
  354. dst.Align = "left"
  355. }
  356. if v := pp.attr("line-height"); v != "" {
  357. if strings.HasSuffix(v, "%") {
  358. dst.LineHeight = units.ParseNum(v) / 100 * model.LineHeightFactor
  359. } else if px := odfLenToPx(v); px > 0 && dst.SizePt > 0 {
  360. dst.LineHeight = px / units.PtToPx(dst.SizePt)
  361. }
  362. }
  363. if v := pp.attr("margin-top"); v != "" {
  364. dst.SpaceTop = odfLenToPx(v) * cv.sy
  365. }
  366. if v := pp.attr("margin-bottom"); v != "" {
  367. dst.SpaceBot = odfLenToPx(v) * cv.sy
  368. }
  369. if v := pp.attr("margin-left"); v != "" {
  370. dst.MarginLeft = odfLenToPx(v) * cv.sx
  371. }
  372. if v := pp.attr("text-indent"); v != "" {
  373. dst.Indent = odfLenToPx(v) * cv.sx
  374. }
  375. }
  376. func (cv *odpConverter) applyOdfGraphicProps(dst *odpStyle, gp *onode) {
  377. switch gp.attr("fill") {
  378. case "none":
  379. dst.Fill = "none"
  380. case "solid", "bitmap", "gradient", "hatch":
  381. if c := gp.attr("fill-color"); strings.HasPrefix(c, "#") {
  382. dst.Fill = strings.ToLower(c)
  383. }
  384. default:
  385. if c := gp.attr("fill-color"); strings.HasPrefix(c, "#") {
  386. dst.Fill = strings.ToLower(c)
  387. }
  388. }
  389. switch gp.attr("stroke") {
  390. case "none":
  391. dst.Stroke = "none"
  392. dst.StrokeW = 0
  393. case "dash":
  394. dst.Dash = true
  395. fallthrough
  396. case "solid":
  397. if c := gp.attr("stroke-color"); strings.HasPrefix(c, "#") {
  398. dst.Stroke = strings.ToLower(c)
  399. } else if dst.Stroke == "" || dst.Stroke == "none" {
  400. dst.Stroke = "#000000"
  401. }
  402. if w := odfLenToPx(gp.attr("stroke-width")) * cv.sx; w > 0 {
  403. dst.StrokeW = w
  404. } else if dst.StrokeW <= 0 {
  405. dst.StrokeW = 1
  406. }
  407. }
  408. switch gp.attr("textarea-vertical-align") {
  409. case "middle":
  410. dst.VAlign = "middle"
  411. case "bottom":
  412. dst.VAlign = "bottom"
  413. case "top":
  414. dst.VAlign = "top"
  415. }
  416. for attr, field := range map[string]*float64{
  417. "padding-top": &dst.PadT, "padding-right": &dst.PadR,
  418. "padding-bottom": &dst.PadB, "padding-left": &dst.PadL,
  419. } {
  420. if v := gp.attr(attr); v != "" {
  421. *field = odfLenToPx(v) * cv.sx
  422. dst.HasPad = true
  423. }
  424. }
  425. if v := gp.attr("padding"); v != "" {
  426. p := odfLenToPx(v) * cv.sx
  427. dst.PadT, dst.PadR, dst.PadB, dst.PadL = p, p, p, p
  428. dst.HasPad = true
  429. }
  430. if v := gp.attr("marker-end"); v != "" && v != "none" {
  431. dst.ArrowEnd = true
  432. }
  433. if v := gp.attr("marker-start"); v != "" && v != "none" {
  434. dst.ArrowHead = true
  435. }
  436. }
  437. // odfFontSizePt reads a font size, which may be relative ("120%")
  438. func odfFontSizePt(v string, current float64) float64 {
  439. if strings.HasSuffix(v, "%") {
  440. return current * units.ParseNum(v) / 100
  441. }
  442. if strings.HasSuffix(v, "pt") {
  443. return units.ParseNum(v)
  444. }
  445. if px := odfLenToPx(v); px > 0 {
  446. return px * 0.75
  447. }
  448. return 0
  449. }
  450. /* ---------------- geometry ---------------- */
  451. func (cv *odpConverter) geom(n *onode, ox, oy, kx, ky float64) (x, y, w, h float64) {
  452. x = ox + odfLenToPx(n.attr("x"))*cv.sx*kx
  453. y = oy + odfLenToPx(n.attr("y"))*cv.sy*ky
  454. w = odfLenToPx(n.attr("width")) * cv.sx * kx
  455. h = odfLenToPx(n.attr("height")) * cv.sy * ky
  456. return x, y, w, h
  457. }
  458. // odfRotation reads the rotation out of a draw:transform attribute, in
  459. // degrees clockwise (ODF states radians counter-clockwise)
  460. func odfRotation(n *onode) (deg float64, tx, ty float64) {
  461. t := n.attr("transform")
  462. if t == "" {
  463. return 0, 0, 0
  464. }
  465. if i := strings.Index(t, "rotate"); i >= 0 {
  466. if open := strings.Index(t[i:], "("); open >= 0 {
  467. rest := t[i+open+1:]
  468. if close := strings.Index(rest, ")"); close >= 0 {
  469. deg = -units.ParseNum(strings.TrimSpace(rest[:close])) * 180 / math.Pi
  470. }
  471. }
  472. }
  473. if i := strings.Index(t, "translate"); i >= 0 {
  474. if open := strings.Index(t[i:], "("); open >= 0 {
  475. rest := t[i+open+1:]
  476. if close := strings.Index(rest, ")"); close >= 0 {
  477. parts := strings.Fields(strings.TrimSpace(rest[:close]))
  478. if len(parts) == 2 {
  479. tx, ty = odfLenToPx(parts[0]), odfLenToPx(parts[1])
  480. }
  481. }
  482. }
  483. }
  484. return deg, tx, ty
  485. }
  486. func (cv *odpConverter) nextID() string {
  487. cv.seq++
  488. return fmt.Sprintf("o-odp-%d", cv.seq)
  489. }
  490. func (cv *odpConverter) add(slide *model.Slide, o *model.Object) {
  491. o.ID = cv.nextID()
  492. o.Z = len(slide.Objects) + 1
  493. slide.Objects = append(slide.Objects, o)
  494. }
  495. // styleOf resolves the drawing style of a shape, including the
  496. // presentation style a placeholder inherits
  497. func (cv *odpConverter) styleOf(n *onode) odpStyle {
  498. st := odpDefaultStyle
  499. st = cv.resolveStyle("#default-graphic", st)
  500. if ps := n.attr("class"); ps != "" && cv.master != nil {
  501. // a placeholder takes the master's matching frame's style
  502. if mf := cv.masterFrame(ps); mf != nil {
  503. st = cv.resolveStyle(mf.attr("style-name"), st)
  504. st = cv.resolveStyle(mf.attr("text-style-name"), st)
  505. }
  506. }
  507. st = cv.resolveStyle(n.attr("style-name"), st)
  508. st = cv.resolveStyle(n.attr("text-style-name"), st)
  509. return st
  510. }
  511. // masterFrame finds the placeholder frame of a given class on the master
  512. func (cv *odpConverter) masterFrame(class string) *onode {
  513. if cv.master == nil {
  514. return nil
  515. }
  516. for _, c := range cv.master.children {
  517. if c.el != nil && c.el.name == "frame" && c.el.attr("class") == class {
  518. return c.el
  519. }
  520. }
  521. return nil
  522. }
  523. /* ---------------- shapes ---------------- */
  524. func (cv *odpConverter) frame(n *onode, slide *model.Slide, ox, oy, kx, ky float64) {
  525. x, y, w, h := cv.geom(n, ox, oy, kx, ky)
  526. if (w <= 0 || h <= 0) && n.attr("class") != "" {
  527. // a placeholder with no geometry of its own takes the master's
  528. if mf := cv.masterFrame(n.attr("class")); mf != nil {
  529. x, y, w, h = cv.geom(mf, ox, oy, kx, ky)
  530. }
  531. }
  532. if w <= 0 || h <= 0 {
  533. return
  534. }
  535. rot, _, _ := odfRotation(n)
  536. st := cv.styleOf(n)
  537. if img := n.first("image"); img != nil {
  538. href := strings.TrimPrefix(img.attr("href"), "./")
  539. raw, ok := cv.files[href]
  540. if !ok {
  541. return
  542. }
  543. ext := strings.TrimPrefix(strings.ToLower(pathExtOf(href)), ".")
  544. if ext == "jpg" {
  545. ext = "jpeg"
  546. }
  547. if ext == "wmf" || ext == "emf" {
  548. if raw, ext, ok = metafile.BrowserPicture(raw, ext); !ok {
  549. return
  550. }
  551. }
  552. if ext != "png" && ext != "jpeg" && ext != "gif" {
  553. return
  554. }
  555. props := model.Props{Src: "data:image/" + ext + ";base64," +
  556. base64.StdEncoding.EncodeToString(raw), Fit: "fill"}
  557. if c := odfClipFractions(n.attr("clip")); c != nil {
  558. props.Crop = c
  559. }
  560. cv.add(slide, &model.Object{Type: "image", X: x, Y: y, W: w, H: h, Rot: rot, Props: props})
  561. return
  562. }
  563. if tbl := n.first("table"); tbl != nil {
  564. cv.table(tbl, slide, x, y, w, h, st)
  565. return
  566. }
  567. if tb := n.first("text-box"); tb != nil {
  568. res := cv.buildText(tb, st)
  569. if res.HTML == "" {
  570. return
  571. }
  572. cv.add(slide, &model.Object{Type: "text", X: x, Y: y, W: w, H: h, Rot: rot,
  573. Props: cv.textProps(res, st)})
  574. }
  575. }
  576. // textProps assembles the editor properties of a text body
  577. func (cv *odpConverter) textProps(res odpText, st odpStyle) model.Props {
  578. p := model.Props{
  579. HTML: res.HTML, FontSize: units.Round2(units.PtToPx(res.First.SizePt)),
  580. Color: res.First.Color, Align: res.First.Align,
  581. Bold: res.Bold, Italic: res.Italic,
  582. Underline: res.Underline,
  583. FontFamily: fonts.StackFor(res.First.Font, res.First.Font),
  584. VAlign: st.VAlign,
  585. LineHeight: units.Round2(odpLineHeight(res.First)),
  586. }
  587. if p.VAlign == "" {
  588. p.VAlign = "top"
  589. }
  590. if st.HasPad {
  591. p.Pad = []float64{units.Round2(st.PadT), units.Round2(st.PadR), units.Round2(st.PadB), units.Round2(st.PadL)}
  592. }
  593. return p
  594. }
  595. func odpLineHeight(st odpStyle) float64 {
  596. if st.LineHeight > 0 {
  597. return st.LineHeight
  598. }
  599. return model.LineHeightFactor
  600. }
  601. func (cv *odpConverter) shape(n *onode, slide *model.Slide, ox, oy, kx, ky float64) {
  602. x, y, w, h := cv.geom(n, ox, oy, kx, ky)
  603. if w <= 0 || h <= 0 {
  604. return
  605. }
  606. rot, _, _ := odfRotation(n)
  607. st := cv.styleOf(n)
  608. kind := "rect"
  609. switch n.name {
  610. case "ellipse", "circle":
  611. kind = "ellipse"
  612. case "polygon", "regular-polygon":
  613. kind = "diamond"
  614. case "custom-shape":
  615. kind = odpCustomShapeKind(n)
  616. default:
  617. if n.attr("corner-radius") != "" {
  618. kind = "roundRect"
  619. }
  620. }
  621. fill := st.Fill
  622. if fill == "" {
  623. fill = "#e07b1f"
  624. }
  625. stroke := st.Stroke
  626. if stroke == "none" {
  627. stroke = ""
  628. }
  629. res := cv.buildText(n, st)
  630. props := model.Props{Kind: kind, Fill: fill, Stroke: stroke, StrokeW: units.Round2(st.StrokeW),
  631. Dash: st.Dash, HTML: res.HTML, Text: res.Plain,
  632. TextColor: res.First.Color, FontSize: units.Round2(units.PtToPx(res.First.SizePt)),
  633. FontFamily: fonts.StackFor(res.First.Font, res.First.Font),
  634. Bold: res.Bold, Italic: res.Italic,
  635. Align: res.First.Align, VAlign: st.VAlign,
  636. LineHeight: units.Round2(odpLineHeight(res.First)),
  637. }
  638. if props.VAlign == "" {
  639. props.VAlign = "middle"
  640. }
  641. if st.HasPad {
  642. props.Pad = []float64{units.Round2(st.PadT), units.Round2(st.PadR), units.Round2(st.PadB), units.Round2(st.PadL)}
  643. }
  644. if r := odfLenToPx(n.attr("corner-radius")) * cv.sx; r > 0 {
  645. props.Radius = units.Round2(r)
  646. }
  647. cv.add(slide, &model.Object{Type: "shape", X: x, Y: y, W: w, H: h, Rot: rot, Props: props})
  648. }
  649. // odpCustomShapeKind maps a draw:custom-shape's enhanced geometry type onto
  650. // the shape the editor draws for it - the inverse of odpShapeTypes
  651. // (odp_writer.go), plus the spellings other producers use.
  652. func odpCustomShapeKind(n *onode) string {
  653. eg := n.first("enhanced-geometry")
  654. if eg == nil {
  655. return "rect"
  656. }
  657. switch eg.attr("type") {
  658. case "ellipse", "circle":
  659. return "ellipse"
  660. case "round-rectangle", "rounded-rectangle":
  661. return "roundRect"
  662. case "triangle":
  663. return "triangle"
  664. case "star":
  665. return "star5"
  666. case "isosceles-triangle":
  667. return "triangle"
  668. case "right-triangle":
  669. return "rtTriangle"
  670. case "diamond":
  671. return "diamond"
  672. case "pentagon":
  673. return "pentagon"
  674. case "hexagon":
  675. return "hexagon"
  676. case "octagon":
  677. return "octagon"
  678. case "parallelogram":
  679. return "parallelogram"
  680. case "trapezoid":
  681. return "trapezoid"
  682. case "cross":
  683. return "plus"
  684. case "pentagon-right":
  685. return "chevron"
  686. case "can":
  687. return "can"
  688. case "cube":
  689. return "cube"
  690. case "ring":
  691. return "donut"
  692. case "forbidden":
  693. return "noSmoking"
  694. case "heart":
  695. return "heart"
  696. case "lightning":
  697. return "lightningBolt"
  698. case "sun":
  699. return "sun"
  700. case "moon":
  701. return "moon"
  702. case "cloud":
  703. return "cloud"
  704. case "smiley":
  705. return "smileyFace"
  706. case "paper":
  707. return "foldedCorner"
  708. case "block-arc":
  709. return "blockArc"
  710. case "arc":
  711. return "arc"
  712. case "pie":
  713. return "pie"
  714. case "frame":
  715. return "frame"
  716. case "right-arrow":
  717. return "rightArrow"
  718. case "left-arrow":
  719. return "leftArrow"
  720. case "up-arrow":
  721. return "upArrow"
  722. case "down-arrow":
  723. return "downArrow"
  724. case "left-right-arrow":
  725. return "leftRightArrow"
  726. case "up-down-arrow":
  727. return "upDownArrow"
  728. case "quad-arrow":
  729. return "quadArrow"
  730. case "notched-right-arrow":
  731. return "notchedRightArrow"
  732. case "striped-right-arrow":
  733. return "stripedRightArrow"
  734. case "circular-arrow":
  735. return "circularArrow"
  736. case "right-arrow-callout":
  737. return "rightArrowCallout"
  738. case "left-arrow-callout":
  739. return "leftArrowCallout"
  740. case "up-arrow-callout":
  741. return "upArrowCallout"
  742. case "down-arrow-callout":
  743. return "downArrowCallout"
  744. case "left-right-arrow-callout":
  745. return "leftRightArrowCallout"
  746. case "star4":
  747. return "star4"
  748. case "star5":
  749. return "star5"
  750. case "star6":
  751. return "star6"
  752. case "star8":
  753. return "star8"
  754. case "star12":
  755. return "star12"
  756. case "star24":
  757. return "star24"
  758. case "bang":
  759. return "irregularSeal1"
  760. case "rectangular-callout":
  761. return "wedgeRectCallout"
  762. case "round-rectangular-callout":
  763. return "wedgeRoundRectCallout"
  764. case "round-callout":
  765. return "wedgeEllipseCallout"
  766. case "cloud-callout":
  767. return "cloudCallout"
  768. case "left-bracket":
  769. return "leftBracket"
  770. case "right-bracket":
  771. return "rightBracket"
  772. case "bracket-pair":
  773. return "bracketPair"
  774. case "left-brace":
  775. return "leftBrace"
  776. case "right-brace":
  777. return "rightBrace"
  778. case "brace-pair":
  779. return "bracePair"
  780. case "mathplus":
  781. return "mathPlus"
  782. case "mathminus":
  783. return "mathMinus"
  784. case "mathmultiply":
  785. return "mathMultiply"
  786. case "mathdivide":
  787. return "mathDivide"
  788. case "mathequal":
  789. return "mathEqual"
  790. case "mathnotequal":
  791. return "mathNotEqual"
  792. case "flowchart-process":
  793. return "flowChartProcess"
  794. case "flowchart-alternate-process":
  795. return "flowChartAlternateProcess"
  796. case "flowchart-decision":
  797. return "flowChartDecision"
  798. case "flowchart-data":
  799. return "flowChartInputOutput"
  800. case "flowchart-predefined-process":
  801. return "flowChartPredefinedProcess"
  802. case "flowchart-internal-storage":
  803. return "flowChartInternalStorage"
  804. case "flowchart-document":
  805. return "flowChartDocument"
  806. case "flowchart-multidocument":
  807. return "flowChartMultidocument"
  808. case "flowchart-terminator":
  809. return "flowChartTerminator"
  810. case "flowchart-preparation":
  811. return "flowChartPreparation"
  812. case "flowchart-manual-input":
  813. return "flowChartManualInput"
  814. case "flowchart-manual-operation":
  815. return "flowChartManualOperation"
  816. case "flowchart-connector":
  817. return "flowChartConnector"
  818. case "flowchart-off-page-connector":
  819. return "flowChartOffpageConnector"
  820. case "flowchart-card":
  821. return "flowChartPunchedCard"
  822. case "flowchart-punched-tape":
  823. return "flowChartPunchedTape"
  824. case "flowchart-summing-junction":
  825. return "flowChartSummingJunction"
  826. case "flowchart-or":
  827. return "flowChartOr"
  828. case "flowchart-collate":
  829. return "flowChartCollate"
  830. case "flowchart-sort":
  831. return "flowChartSort"
  832. case "flowchart-extract":
  833. return "flowChartExtract"
  834. case "flowchart-merge":
  835. return "flowChartMerge"
  836. case "flowchart-delay":
  837. return "flowChartDelay"
  838. case "flowchart-magnetic-disk":
  839. return "flowChartMagneticDisk"
  840. case "flowchart-display":
  841. return "flowChartDisplay"
  842. }
  843. return "rect"
  844. }
  845. func (cv *odpConverter) line(n *onode, slide *model.Slide, ox, oy, kx, ky float64) {
  846. x1 := ox + odfLenToPx(n.attr("x1"))*cv.sx*kx
  847. y1 := oy + odfLenToPx(n.attr("y1"))*cv.sy*ky
  848. x2 := ox + odfLenToPx(n.attr("x2"))*cv.sx*kx
  849. y2 := oy + odfLenToPx(n.attr("y2"))*cv.sy*ky
  850. st := cv.styleOf(n)
  851. stroke := st.Stroke
  852. if stroke == "" || stroke == "none" {
  853. stroke = "#333333"
  854. }
  855. sw := st.StrokeW
  856. if sw <= 0 {
  857. sw = 2
  858. }
  859. cv.add(slide, &model.Object{Type: "line", X: x1, Y: y1, W: x2 - x1, H: y2 - y1,
  860. Props: model.Props{Stroke: stroke, StrokeW: units.Round2(sw), Dash: st.Dash,
  861. ArrowEnd: st.ArrowEnd, ArrowStart: st.ArrowHead}})
  862. }
  863. func (cv *odpConverter) table(tbl *onode, slide *model.Slide, x, y, w, h float64, st odpStyle) {
  864. var rows [][]string
  865. var fills [][]string
  866. // relative column widths, so a table keeps its proportions
  867. var colW []float64
  868. var colSum float64
  869. for _, col := range tbl.all("table-column") {
  870. cw := 0.0
  871. if cs, ok := cv.styles[col.attr("style-name")]; ok {
  872. if tp := cs.first("table-column-properties"); tp != nil {
  873. cw = odfLenToPx(tp.attr("column-width"))
  874. }
  875. }
  876. colW = append(colW, cw)
  877. colSum += cw
  878. }
  879. if colSum <= 0 {
  880. colW = nil
  881. } else {
  882. for i := range colW {
  883. colW[i] = units.Round2(colW[i] / colSum * 100)
  884. }
  885. }
  886. for _, tr := range tbl.all("table-row") {
  887. var row, rowFills []string
  888. for _, c := range tr.children {
  889. if c.el == nil || c.el.name != "table-cell" {
  890. continue
  891. }
  892. cellStyle := cv.resolveStyle(c.el.attr("style-name"), st)
  893. fill := ""
  894. if cs, ok := cv.styles[c.el.attr("style-name")]; ok {
  895. if tp := cs.first("table-cell-properties"); tp != nil {
  896. if v := tp.attr("background-color"); strings.HasPrefix(v, "#") {
  897. fill = strings.ToLower(v)
  898. }
  899. }
  900. }
  901. res := cv.buildText(c.el, cellStyle)
  902. row = append(row, res.HTML)
  903. rowFills = append(rowFills, fill)
  904. // a spanned cell occupies the columns it covers
  905. for i := 1; i < int(units.AtofDefault(c.el.attr("number-columns-spanned"), 1)); i++ {
  906. row = append(row, "")
  907. rowFills = append(rowFills, fill)
  908. }
  909. }
  910. if len(row) > 0 {
  911. rows = append(rows, row)
  912. fills = append(fills, rowFills)
  913. }
  914. }
  915. if len(rows) == 0 {
  916. return
  917. }
  918. if !anyFilled(fills) {
  919. fills = nil
  920. }
  921. cv.add(slide, &model.Object{Type: "table", X: x, Y: y, W: w, H: h,
  922. Props: model.Props{Rows: rows, FontSize: units.Round2(units.PtToPx(st.SizePt)),
  923. ColW: colW, CellFill: fills}})
  924. }
  925. /* ---------------- text ---------------- */
  926. // odpText is the rendered HTML of a text body plus its first run's style
  927. type odpText struct {
  928. HTML string
  929. Plain string
  930. First odpStyle
  931. // the whole box is bold / italic / underlined: every run is. The editor
  932. // applies these to the box itself, and a descendant cannot take an
  933. // underline off again, so the first run alone must not decide.
  934. Bold, Italic, Underline bool
  935. }
  936. // buildText renders every paragraph and list under a container into the
  937. // editor's storage HTML, resolving each paragraph's and span's styles
  938. func (cv *odpConverter) buildText(container *onode, base odpStyle) odpText {
  939. out := odpText{First: base}
  940. flags := &odpRunFlags{bold: true, italic: true, under: true}
  941. outer := cv.runs
  942. cv.runs = flags
  943. defer func() { cv.runs = outer }()
  944. var sb strings.Builder
  945. var plain []string
  946. first := true
  947. counters := map[int]int{}
  948. var walk func(n *onode, depth int, listStyle string)
  949. walk = func(n *onode, depth int, listStyle string) {
  950. for _, c := range n.children {
  951. if c.el == nil {
  952. continue
  953. }
  954. switch c.el.name {
  955. case "p", "h":
  956. ps := cv.resolveStyle(c.el.attr("style-name"), base)
  957. bullet := ""
  958. if depth > 0 {
  959. bullet, counters = odpListMarker(cv.lists[listStyle], depth, counters)
  960. }
  961. html, text, firstRun := cv.paragraphHTML(c.el, ps, bullet, depth)
  962. if first && strings.TrimSpace(text) != "" {
  963. out.First = firstRun
  964. first = false
  965. }
  966. sb.WriteString(html)
  967. plain = append(plain, text)
  968. case "list":
  969. ls := c.el.attr("style-name")
  970. if ls == "" {
  971. ls = listStyle
  972. }
  973. if depth == 0 {
  974. counters = map[int]int{}
  975. }
  976. for _, item := range c.el.all("list-item") {
  977. walk(item, depth+1, ls)
  978. }
  979. case "text-box":
  980. walk(c.el, depth, listStyle)
  981. }
  982. }
  983. }
  984. walk(container, 0, "")
  985. out.HTML = sb.String()
  986. out.Plain = strings.Join(plain, "\n")
  987. if strings.TrimSpace(out.Plain) == "" {
  988. out.HTML = ""
  989. }
  990. if flags.any {
  991. out.Bold, out.Italic, out.Underline = flags.bold, flags.italic, flags.under
  992. } else {
  993. out.Bold, out.Italic, out.Underline = out.First.Bold, out.First.Italic, out.First.Underline
  994. }
  995. return out
  996. }
  997. // paragraphHTML renders one text:p / text:h into a storage-HTML block
  998. func (cv *odpConverter) paragraphHTML(p *onode, ps odpStyle, bullet string, depth int) (string, string, odpStyle) {
  999. var runs strings.Builder
  1000. var text strings.Builder
  1001. firstRun := ps
  1002. haveRun := false
  1003. minSize := 0.0
  1004. var emit func(n *onode, st odpStyle)
  1005. emit = func(n *onode, st odpStyle) {
  1006. for _, c := range n.children {
  1007. if c.el == nil {
  1008. if c.text == "" {
  1009. continue
  1010. }
  1011. if !haveRun {
  1012. firstRun = st
  1013. haveRun = true
  1014. }
  1015. if minSize == 0 || st.SizePt < minSize {
  1016. minSize = st.SizePt
  1017. }
  1018. if f := cv.runs; f != nil && strings.TrimSpace(c.text) != "" {
  1019. f.any = true
  1020. f.bold = f.bold && st.Bold
  1021. f.italic = f.italic && st.Italic
  1022. f.under = f.under && st.Underline
  1023. }
  1024. runs.WriteString(`<span style="` + odpRunCSS(st) + `">` +
  1025. xmltree.Escape(c.text) + `</span>`)
  1026. text.WriteString(c.text)
  1027. continue
  1028. }
  1029. switch c.el.name {
  1030. case "span":
  1031. emit(c.el, cv.resolveStyle(c.el.attr("style-name"), st))
  1032. case "a":
  1033. emit(c.el, st)
  1034. case "line-break":
  1035. runs.WriteString("<br>")
  1036. text.WriteString("\n")
  1037. case "tab":
  1038. runs.WriteString(`<span style="` + odpRunCSS(st) + `">&#9;</span>`)
  1039. text.WriteString("\t")
  1040. case "s":
  1041. n := int(units.AtofDefault(c.el.attr("c"), 1))
  1042. runs.WriteString(strings.Repeat(" ", n))
  1043. text.WriteString(strings.Repeat(" ", n))
  1044. default:
  1045. emit(c.el, st)
  1046. }
  1047. }
  1048. }
  1049. emit(p, ps)
  1050. if minSize <= 0 {
  1051. minSize = ps.SizePt
  1052. }
  1053. var css strings.Builder
  1054. css.WriteString("text-align:" + ps.Align + ";")
  1055. css.WriteString("line-height:" + strconv.FormatFloat(units.Round2(odpLineHeight(ps)), 'f', -1, 64) + ";")
  1056. css.WriteString("font-size:" + units.FmtPx(units.PtToPx(minSize)) + ";")
  1057. if ps.SpaceTop > 0 {
  1058. css.WriteString("margin-top:" + units.FmtPx(ps.SpaceTop) + ";")
  1059. }
  1060. if ps.SpaceBot > 0 {
  1061. css.WriteString("margin-bottom:" + units.FmtPx(ps.SpaceBot) + ";")
  1062. }
  1063. marL := ps.MarginLeft
  1064. if depth > 0 && marL == 0 {
  1065. marL = float64(depth) * 24
  1066. }
  1067. if marL != 0 {
  1068. css.WriteString("padding-left:" + units.FmtPx(marL) + ";")
  1069. }
  1070. body := runs.String()
  1071. marker := ""
  1072. if bullet != "" {
  1073. indent := ps.Indent
  1074. if indent == 0 {
  1075. indent = -18
  1076. }
  1077. css.WriteString("position:relative;")
  1078. bs := firstRun
  1079. bs.Underline, bs.Strike, bs.Highlight = false, false, ""
  1080. marker = `<span style="position:absolute;left:` + units.FmtPx(marL+indent) + `;` +
  1081. odpRunCSS(bs) + `">` + xmltree.Escape(bullet) + `</span>`
  1082. } else if ps.Indent != 0 {
  1083. css.WriteString("text-indent:" + units.FmtPx(ps.Indent) + ";")
  1084. }
  1085. if body == "" {
  1086. body = `<span style="font-size:` + units.FmtPx(units.PtToPx(minSize)) + `">&#8203;</span>`
  1087. }
  1088. return `<div style="` + css.String() + `">` + marker + body + `</div>`,
  1089. text.String(), firstRun
  1090. }
  1091. // odpRunCSS renders the inline style of one text run
  1092. func odpRunCSS(st odpStyle) string {
  1093. var sb strings.Builder
  1094. sb.WriteString("font-size:" + units.FmtPx(units.PtToPx(st.SizePt)) + ";")
  1095. if st.Font != "" {
  1096. sb.WriteString("font-family:" + fonts.StackFor(st.Font, st.Font) + ";")
  1097. }
  1098. // only fo:font-weight sets a weight; a weight word in the family name
  1099. // belongs to that face, not to the CJK text the run falls back for
  1100. // (see runCSS in the pptx reader)
  1101. if st.Bold {
  1102. sb.WriteString("font-weight:700;")
  1103. }
  1104. if st.Italic {
  1105. sb.WriteString("font-style:italic;")
  1106. }
  1107. deco := ""
  1108. if st.Underline {
  1109. deco = "underline"
  1110. }
  1111. if st.Strike {
  1112. if deco != "" {
  1113. deco += " "
  1114. }
  1115. deco += "line-through"
  1116. }
  1117. if deco != "" {
  1118. sb.WriteString("text-decoration:" + deco + ";")
  1119. }
  1120. if st.Color != "" {
  1121. sb.WriteString("color:" + st.Color + ";")
  1122. }
  1123. if st.Highlight != "" {
  1124. sb.WriteString("background-color:" + st.Highlight + ";")
  1125. }
  1126. return sb.String()
  1127. }
  1128. // odpListMarker renders the marker of a list level, advancing the counter
  1129. // of a numbered list
  1130. func odpListMarker(listStyle *onode, depth int, counters map[int]int) (string, map[int]int) {
  1131. bullet := "•"
  1132. if listStyle != nil {
  1133. for _, lvl := range listStyle.children {
  1134. if lvl.el == nil {
  1135. continue
  1136. }
  1137. if int(units.AtofDefault(lvl.el.attr("level"), 0)) != depth {
  1138. continue
  1139. }
  1140. switch lvl.el.name {
  1141. case "list-level-style-bullet":
  1142. if c := lvl.el.attr("bullet-char"); c != "" {
  1143. bullet = c
  1144. }
  1145. case "list-level-style-number":
  1146. counters[depth]++
  1147. return numberedMarker(lvl.el, counters[depth]), counters
  1148. }
  1149. }
  1150. }
  1151. return bullet, counters
  1152. }
  1153. func numberedMarker(lvl *onode, n int) string {
  1154. format := lvl.attr("num-format")
  1155. prefix := lvl.attr("num-prefix")
  1156. suffix := lvl.attr("num-suffix")
  1157. if suffix == "" {
  1158. suffix = "."
  1159. }
  1160. var body string
  1161. switch format {
  1162. case "a":
  1163. body = slidetext.AlphaMarker(n, 'a')
  1164. case "A":
  1165. body = slidetext.AlphaMarker(n, 'A')
  1166. case "i":
  1167. body = strings.ToLower(slidetext.RomanMarker(n))
  1168. case "I":
  1169. body = slidetext.RomanMarker(n)
  1170. default:
  1171. body = strconv.Itoa(n)
  1172. }
  1173. return prefix + body + suffix
  1174. }
  1175. // odfClipFractions turns an fo:clip rect into the editor's crop fractions.
  1176. // ODF states the clip as inset lengths, which is only convertible into
  1177. // fractions when the frame states them as percentages.
  1178. func odfClipFractions(clip string) []float64 {
  1179. clip = strings.TrimSpace(clip)
  1180. if !strings.HasPrefix(clip, "rect(") || !strings.HasSuffix(clip, ")") {
  1181. return nil
  1182. }
  1183. parts := strings.FieldsFunc(clip[5:len(clip)-1], func(r rune) bool {
  1184. return r == ',' || r == ' '
  1185. })
  1186. if len(parts) != 4 {
  1187. return nil
  1188. }
  1189. out := make([]float64, 4)
  1190. // fo:clip lists top, right, bottom, left; the editor wants l,t,r,b
  1191. order := []int{3, 0, 1, 2}
  1192. for i, src := range order {
  1193. v := strings.TrimSpace(parts[src])
  1194. if !strings.HasSuffix(v, "%") {
  1195. return nil
  1196. }
  1197. out[i] = units.Round4(units.ParseNum(v) / 100)
  1198. }
  1199. return out
  1200. }