presentation.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package model
  2. /*
  3. presentation.go - the Slides document body (src/web/Office/slides): the
  4. deck, its slides and the objects on them, in the editor's 960 px wide
  5. coordinate space.
  6. PowerPoint (.pptx, .ppt) and OpenDocument presentations (.odp) both
  7. read into it.
  8. */
  9. import (
  10. "encoding/json"
  11. "errors"
  12. "math"
  13. )
  14. // Slide coordinate space used by the Slides webapp: always 960 px wide,
  15. // and 540 tall for the default 16:9 deck. A deck of another shape keeps
  16. // its shape (SlideSizeFor) - a 4:3 PowerPoint deck is 960x720 - so nothing
  17. // on it is stretched to fit.
  18. const (
  19. SlidePxW = 960
  20. SlidePxH = 540
  21. )
  22. // SlideSizeFor is the editor size of a slide whose source page is w x h
  23. // (in any one unit): 960 wide and as tall as the page's shape.
  24. func SlideSizeFor(w, h float64) (int, int) {
  25. if !(w > 0) || !(h > 0) {
  26. return SlidePxW, SlidePxH
  27. }
  28. ph := int(math.Round(float64(SlidePxW) * h / w))
  29. if ph < 100 || ph > 4000 {
  30. return SlidePxW, SlidePxH
  31. }
  32. return SlidePxW, ph
  33. }
  34. // dims is the deck's slide size, the 16:9 default when it states none
  35. func (p *Presentation) Dims() (int, int) {
  36. if p != nil && len(p.Size) == 2 && p.Size[0] >= 100 && p.Size[1] >= 100 &&
  37. p.Size[0] <= 4000 && p.Size[1] <= 4000 {
  38. return p.Size[0], p.Size[1]
  39. }
  40. return SlidePxW, SlidePxH
  41. }
  42. // Presentation is the Slides document body
  43. type Presentation struct {
  44. Size []int `json:"size,omitempty"`
  45. Theme string `json:"theme,omitempty"`
  46. Slides []*Slide `json:"slides"`
  47. // font faces the source file carried with it, so the deck renders in
  48. // the typeface it was designed in even where that font is not
  49. // installed. Empty for documents authored in the editor.
  50. Fonts []*EmbeddedFont `json:"fonts,omitempty"`
  51. }
  52. // EmbeddedFont is one @font-face the Slides editor installs for a document
  53. type EmbeddedFont struct {
  54. Family string `json:"family"`
  55. Weight int `json:"weight,omitempty"` // 400 or 700
  56. Style string `json:"style,omitempty"` // "" or "italic"
  57. Src string `json:"src"` // data URL
  58. }
  59. // Rect is a plain rectangle in slide units
  60. type Rect struct {
  61. X float64 `json:"x"`
  62. Y float64 `json:"y"`
  63. W float64 `json:"w"`
  64. H float64 `json:"h"`
  65. }
  66. // Slide is a single slide
  67. type Slide struct {
  68. ID string `json:"id,omitempty"`
  69. Bg string `json:"bg,omitempty"` // "" = theme default background
  70. // a picture or gradient background, drawn over Bg (pptx p:bg with a
  71. // blipFill / gradFill, directly or through the theme's bgRef). The
  72. // editor keeps them until a colour is chosen for the slide.
  73. BgImage *BgImage `json:"bgImage,omitempty"`
  74. BgGrad *Gradient `json:"bgGrad,omitempty"`
  75. Notes string `json:"notes,omitempty"`
  76. // entry transition: "" / "none" | "fade" | "slide" | "zoom"
  77. Transition string `json:"transition,omitempty"`
  78. // Hidden is a slide the slide show skips ("Skip slide" in the editor):
  79. // pptx <p:sld show="0">, odp presentation:visibility="hidden". It
  80. // stays in the deck and in every export.
  81. Hidden bool `json:"hidden,omitempty"`
  82. Objects []*Object `json:"objects"`
  83. }
  84. // BgImage is a picture that fills the slide background. X/Y/W/H place the
  85. // whole picture in slide px - it may reach past the slide's edges, which
  86. // clip it - or, when Tile is set, give the first tile of a repeating
  87. // pattern.
  88. type BgImage struct {
  89. Src string `json:"src"`
  90. X float64 `json:"x"`
  91. Y float64 `json:"y"`
  92. W float64 `json:"w"`
  93. H float64 `json:"h"`
  94. Tile bool `json:"tile,omitempty"`
  95. Opacity float64 `json:"opacity,omitempty"` // 0..1, 0 = fully opaque
  96. }
  97. // Gradient is a gradient fill: linear along an angle, or radial about a
  98. // centre. Angle follows CSS (0 = towards the top, 90 = towards the right).
  99. type Gradient struct {
  100. Kind string `json:"kind"` // "linear" | "radial"
  101. Angle float64 `json:"angle,omitempty"`
  102. CX float64 `json:"cx,omitempty"` // radial centre, fraction of the box
  103. CY float64 `json:"cy,omitempty"`
  104. Stops []GradientStop `json:"stops"`
  105. }
  106. // GradientStop is one colour of a gradient at a position from 0 to 1
  107. type GradientStop struct {
  108. Pos float64 `json:"pos"`
  109. Color string `json:"color"` // #rrggbb or #rrggbbaa
  110. }
  111. // Object is one visual element on a slide
  112. type Object struct {
  113. ID string `json:"id,omitempty"`
  114. Type string `json:"type"`
  115. X float64 `json:"x"`
  116. Y float64 `json:"y"`
  117. W float64 `json:"w"`
  118. H float64 `json:"h"`
  119. Rot float64 `json:"rot,omitempty"`
  120. Z int `json:"z,omitempty"`
  121. Props Props `json:"props"`
  122. }
  123. // Props carries the per-type properties. Fields for every object type are
  124. // merged into one struct so the JSON stays schema-compatible with slides.js.
  125. type Props struct {
  126. // text
  127. HTML string `json:"html,omitempty"`
  128. FontSize float64 `json:"fontSize,omitempty"`
  129. Color string `json:"color,omitempty"`
  130. Align string `json:"align,omitempty"`
  131. Bold bool `json:"bold,omitempty"`
  132. Italic bool `json:"italic,omitempty"`
  133. Underline bool `json:"underline,omitempty"`
  134. // text layout - set by the pptx/odp readers so an imported deck keeps
  135. // the typography of the file it came from. All are optional: a text
  136. // object without them renders on the editor's own defaults.
  137. FontFamily string `json:"fontFamily,omitempty"` // CSS font stack
  138. VAlign string `json:"valign,omitempty"` // top | middle | bottom
  139. Pad []float64 `json:"pad,omitempty"` // insets [t,r,b,l] px
  140. LineHeight float64 `json:"lineHeight,omitempty"` // unitless multiplier
  141. // image
  142. Src string `json:"src,omitempty"`
  143. Fit string `json:"fit,omitempty"`
  144. Crop []float64 `json:"crop,omitempty"` // [left,top,right,bottom] fractions
  145. Radius float64 `json:"radius,omitempty"` // corner radius, px
  146. Opacity float64 `json:"opacity,omitempty"` // 0..1, 0 = fully opaque
  147. // a shaped crop: the picture is clipped to one of the shape kinds
  148. // below ("" or "rect" = a plain rectangle)
  149. Mask string `json:"mask,omitempty"`
  150. FlipH bool `json:"flipH,omitempty"`
  151. FlipV bool `json:"flipV,omitempty"`
  152. // picture colour: a re-colour preset key (see RECOLORS in
  153. // slides/slides_image.js) and the brightness / contrast offsets, where
  154. // 0 is "leave alone" and the range is roughly -0.9 .. 1
  155. Recolor string `json:"recolor,omitempty"`
  156. Bright float64 `json:"bright,omitempty"`
  157. Contrast float64 `json:"contrast,omitempty"`
  158. // editor-only: the crop panel's aspect-ratio latch
  159. LockAspect bool `json:"lockAspect,omitempty"`
  160. // the frame the whole picture filled before it was first cropped, so
  161. // Reset image can put it back. Editor state - no format stores it.
  162. Orig *Rect `json:"orig,omitempty"`
  163. // shape
  164. Kind string `json:"kind,omitempty"`
  165. // the outline of a "custom" shape (pptx custGeom, pptx/custgeom.go)
  166. Geom *CustomGeom `json:"geom,omitempty"`
  167. Fill string `json:"fill,omitempty"`
  168. // a gradient fill; Fill then holds its first colour for anything that
  169. // draws flat. On a picture, a fill (either) is painted behind it, where
  170. // a transparent picture lets it through.
  171. FillGrad *Gradient `json:"fillGrad,omitempty"`
  172. Stroke string `json:"stroke,omitempty"`
  173. StrokeW float64 `json:"strokeW,omitempty"`
  174. Text string `json:"text,omitempty"`
  175. TextColor string `json:"textColor,omitempty"`
  176. // line
  177. Dash bool `json:"dash,omitempty"`
  178. ArrowEnd bool `json:"arrowEnd,omitempty"`
  179. ArrowStart bool `json:"arrowStart,omitempty"`
  180. // the line's look, as the editor's line menus set it (pptx/lines.go):
  181. // the dash ("" / solid, dot, dash, dashDot, longDash, longDashDot) and
  182. // each end (none, arrow, triangle, circle, square, diamond and their
  183. // open* versions). Absent, Dash / ArrowEnd / ArrowStart decide.
  184. DashStyle string `json:"dashStyle,omitempty"`
  185. StartHead string `json:"startHead,omitempty"`
  186. EndHead string `json:"endHead,omitempty"`
  187. // a preset shape's adjustments, named and scaled as PresentationML's
  188. // (adj1, adj2 ... in 1/100000 of the frame): where a callout's tip
  189. // points, how round a corner is
  190. Adj map[string]float64 `json:"adj,omitempty"`
  191. // a text object that is a placeholder: "title", "subtitle" or "body".
  192. // Empty, the editor shows its prompt ("Click to add title"); the
  193. // .pptx writer makes it a <p:ph> again, so PowerPoint does too.
  194. Ph string `json:"ph,omitempty"`
  195. // elbow / rotated connectors imported from pptx: the polyline the
  196. // connector actually follows, relative to the object's x/y. Absent on
  197. // a plain two-point line, which is the only kind the editor draws.
  198. Points [][]float64 `json:"points,omitempty"`
  199. // table
  200. Rows [][]string `json:"rows,omitempty"`
  201. HeaderRow bool `json:"headerRow,omitempty"`
  202. ColW []float64 `json:"colW,omitempty"` // column widths, percent
  203. RowH []float64 `json:"rowH,omitempty"` // row heights, percent
  204. // per-cell background colours ("" = none) and the cell insets, both
  205. // set by the pptx reader so an imported table keeps its shading
  206. CellFill [][]string `json:"cellFill,omitempty"`
  207. CellPad []float64 `json:"cellPad,omitempty"` // [t,r,b,l] px
  208. // an imported table that came with a table style: the cell fills, text
  209. // and rules are the style's (Stroke / StrokeW give the rule colour and
  210. // width, "none" for no rules), so the editor adds no look of its own
  211. Styled bool `json:"styled,omitempty"`
  212. // merged cells: [row, col, rowSpan, colSpan] for each cell that covers
  213. // more than itself; the cells it covers stay in Rows, empty
  214. Merges [][]int `json:"merges,omitempty"`
  215. // per-cell vertical alignment ("" = top, "middle", "bottom")
  216. CellAnchor [][]string `json:"cellAnchor,omitempty"`
  217. // chart (spec kept opaque; Png is a client-side raster for export)
  218. Spec json.RawMessage `json:"spec,omitempty"`
  219. Png string `json:"png,omitempty"`
  220. // any type: followed when clicked while presenting - "#N" (slide N)
  221. // or an http(s) address
  222. Link string `json:"link,omitempty"`
  223. }
  224. // theme background approximations, mirroring THEMES in slides.js
  225. // (gradients are approximated by their first color stop for pptx export)
  226. var ThemeBg = map[string]string{
  227. "clean": "FFFFFF",
  228. "midnight": "232A36",
  229. "ocean": "0F4C75",
  230. "sunset": "C0392B",
  231. "forest": "0F3D33",
  232. "paper": "F6F1E5",
  233. }
  234. var ThemeText = map[string]string{
  235. "clean": "202124",
  236. "midnight": "E8EAED",
  237. "ocean": "F4FAFF",
  238. "sunset": "FDF2EC",
  239. "forest": "EAFAF1",
  240. "paper": "3D3A33",
  241. }
  242. // PresentationToJSON serializes a Presentation back to the Slides body JSON
  243. func PresentationToJSON(p *Presentation) (string, error) {
  244. if p == nil {
  245. return "", errors.New("nil presentation")
  246. }
  247. out, err := json.Marshal(p)
  248. if err != nil {
  249. return "", err
  250. }
  251. return string(out), nil
  252. }
  253. // ParsePresentationJSON decodes the Slides body JSON into a Presentation
  254. func ParsePresentationJSON(jsonStr string) (*Presentation, error) {
  255. p := Presentation{}
  256. if err := json.Unmarshal([]byte(jsonStr), &p); err != nil {
  257. return nil, errors.New("invalid presentation JSON: " + err.Error())
  258. }
  259. if len(p.Slides) == 0 {
  260. return nil, errors.New("presentation contains no slides")
  261. }
  262. return &p, nil
  263. }
  264. // CustomGeom is the outline of a custom shape: its paths, drawn in order
  265. type CustomGeom struct {
  266. Paths []CustomPath `json:"paths"`
  267. }
  268. // CustomPath is one path of a custom shape in its own W x H space, as an
  269. // SVG path of M, L, C and Z
  270. type CustomPath struct {
  271. W float64 `json:"w"`
  272. H float64 `json:"h"`
  273. D string `json:"d"`
  274. NoFill bool `json:"noFill,omitempty"`
  275. NoStroke bool `json:"noStroke,omitempty"`
  276. }
  277. // PowerPoint's "single" line spacing is the font's line height, which is
  278. // about 1.2 em for the fonts office documents use. A <a:lnSpc> percentage
  279. // multiplies that, so 100% renders as CSS line-height 1.2 - checked
  280. // against Google Slides' own PDF export of the reference decks.
  281. const LineHeightFactor = 1.2