1# usage: runpy.sh fixtures/fill-others/make.py <port> <outdir> (office saving ODF 1.2 Extended) 2# Other consumers of fills: Writer frame/table/section, Calc cell/page/shape, Draw shape/page, Impress shape/slide. 3import sys, os, uno 4from unohelp import connect, desktop, pv 5from com.sun.star.awt import Gradient, Rectangle 6from com.sun.star.awt.GradientStyle import LINEAR 7from com.sun.star.drawing.FillStyle import SOLID, GRADIENT 8from com.sun.star.awt import Size, Point 9 10port, outdir = int(sys.argv[1]), sys.argv[2] 11d = desktop(connect(port)) 12 13def grad(): 14 g = Gradient(); g.Style = LINEAR; g.StartColor = 0xFF0000; g.EndColor = 0x0000FF 15 g.Angle = 0; g.Border = 0; g.XOffset = 50; g.YOffset = 50 16 g.StartIntensity = 100; g.EndIntensity = 100; g.StepCount = 0 17 return g 18 19def new(kind): 20 return d.loadComponentFromURL("private:factory/" + kind, "_blank", 0, (pv("Hidden", True),)) 21 22def save(doc, name, flt): 23 doc.storeToURL(uno.systemPathToFileUrl(os.path.join(outdir, name)), (pv("FilterName", flt),)) 24 doc.close(True) 25 26def shape(doc, page, x, y, kind, text, fillkind): 27 s = doc.createInstance(kind) 28 page.add(s) 29 s.Position = Point(x, y); s.Size = Size(4000, 2000) 30 s.String = text 31 if fillkind == "solid": 32 s.FillStyle = SOLID; s.FillColor = 0x33AA55 33 else: 34 s.FillStyle = GRADIENT; s.FillGradient = grad() 35 return s 36 37# Writer: frame, table cell, section, with solid and gradient fills 38doc = new("swriter") 39text = doc.Text 40cur = text.createTextCursor() 41for i, fk in enumerate(("solid", "gradient")): 42 fr = doc.createInstance("com.sun.star.text.TextFrame") 43 fr.Width = 4000; fr.Height = 1500 44 text.insertTextContent(cur, fr, False) 45 fr.Text.String = "frame " + fk 46 if fk == "solid": 47 fr.FillStyle = SOLID; fr.FillColor = 0x33AA55 48 else: 49 fr.FillStyle = GRADIENT; fr.FillGradient = grad() 50tbl = doc.createInstance("com.sun.star.text.TextTable") 51tbl.initialize(2, 2) 52text.insertTextContent(text.End, tbl, False) 53tbl.getCellByName("A1").BackColor = 0xFFCC00 54tbl.getCellByName("B1").BackColor = 0xFFAACC 55tbl.BackColor = 0xCCFFCC 56sec = doc.createInstance("com.sun.star.text.TextSection") 57text.insertTextContent(text.End, sec, False) 58sec.BackColor = 0xCCCCFF 59save(doc, "others-writer.odt", "writer8") 60 61# Calc: cell back colour, page style back colour, shape with gradient and with text 62doc = new("scalc") 63sh = doc.Sheets.getByIndex(0) 64sh.getCellByPosition(0, 0).String = "cell" 65sh.getCellByPosition(0, 0).CellBackColor = 0xFFCC00 66sh.getCellByPosition(1, 0).String = "grad" 67pst = doc.StyleFamilies.getByName("PageStyles").getByName("Default") 68pst.BackColor = 0xDDEEFF 69dp = sh.DrawPage 70shape(doc, dp, 1000, 3000, "com.sun.star.drawing.RectangleShape", "calc shape", "gradient") 71save(doc, "others-calc.ods", "calc8") 72 73# Draw: shapes (solid and gradient), page background 74doc = new("sdraw") 75page = doc.DrawPages.getByIndex(0) 76shape(doc, page, 1000, 1000, "com.sun.star.drawing.RectangleShape", "draw solid", "solid") 77shape(doc, page, 6000, 1000, "com.sun.star.drawing.EllipseShape", "draw grad", "gradient") 78try: 79 page.Background = None 80 bg = doc.createInstance("com.sun.star.drawing.Background") 81 bg.FillStyle = GRADIENT; bg.FillGradient = grad() 82 page.Background = bg 83except Exception as e: 84 print("draw background:", repr(e)) 85save(doc, "others-draw.odg", "draw8") 86 87# Impress: shapes with text, slide background 88doc = new("simpress") 89page = doc.DrawPages.getByIndex(0) 90shape(doc, page, 1000, 1000, "com.sun.star.drawing.RectangleShape", "impress solid", "solid") 91shape(doc, page, 6000, 1000, "com.sun.star.drawing.RectangleShape", "impress grad", "gradient") 92try: 93 bg = doc.createInstance("com.sun.star.drawing.Background") 94 bg.FillStyle = SOLID; bg.FillColor = 0xCCEEFF 95 page.Background = bg 96except Exception as e: 97 print("impress background:", repr(e)) 98save(doc, "others-impress.odp", "impress8") 99