xref: /trunk/test/odf-validate/fixtures/t14/table_cell_props.py (revision 1d752b3e78c8d3ca01c3e22bcd8352a148c195bb)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19
20
21# usage: run-in-mode.sh <mode> <port> <outdir> fixtures/t14/table_cell_props.py [<file.odp>]
22# no file: writes <outdir>/table-cell-props-<mode>.odp with an Impress table whose four cells carry the non-fill cell
23#          properties: text alignment, text distances, word wrap and writing mode, borders, paragraph and character
24#          properties, in the office's current save mode.
25# file:    loads that file and prints the same properties.
26import os, sys
27import uno
28from unohelp import FILTERS, connect, desktop, pv
29
30port, out, tag = int(sys.argv[1]), os.path.abspath(sys.argv[2]), sys.argv[3]
31os.makedirs(out, exist_ok=True)
32desk = desktop(connect(port))
33dst = os.path.abspath(sys.argv[4]) if len(sys.argv) > 4 else os.path.join(out, "table-cell-props-%s.odp" % tag)
34phase = "reload" if len(sys.argv) > 4 else "make"
35url = uno.systemPathToFileUrl
36
37NAMES = ["TextVerticalAdjust", "TextHorizontalAdjust", "TextLeftDistance", "TextRightDistance", "TextUpperDistance",
38         "TextLowerDistance", "TextWordWrap", "TextWritingMode", "ParaAdjust", "ParaTopMargin", "CharWeight", "CharHeight"]
39
40
41def val(v):
42    return v.value if hasattr(v, "value") and not isinstance(v, (int, float, str)) else v
43
44
45def border(c):
46    b = uno.createUnoStruct("com.sun.star.table.BorderLine")
47    b.Color, b.OuterLineWidth, b.InnerLineWidth, b.LineDistance = c, 35, 0, 0
48    return b
49
50
51def show(label, cell):
52    items = [label]
53    for n in NAMES:
54        items.append("%s=%s" % (n, val(cell.getPropertyValue(n))))
55    for n in ("TopBorder", "BottomBorder", "LeftBorder", "RightBorder"):
56        b = cell.getPropertyValue(n)
57        items.append("%s=0x%06x/%d" % (n, b.Color, b.OuterLineWidth))
58    print(*items)
59
60
61def cells(shape):
62    tbl = shape.Model
63    return [("c%d" % i, tbl.getCellByPosition(i % 2, i // 2)) for i in range(4)]
64
65
66def adj(t, n):
67    return uno.Enum("com.sun.star.drawing." + t, n)
68
69
70if phase == "make":
71    doc = desk.loadComponentFromURL("private:factory/simpress", "_blank", 0, (pv("Hidden", True),))
72    page = doc.DrawPages.getByIndex(0)
73    shape = doc.createInstance("com.sun.star.drawing.TableShape")
74    page.add(shape)
75    shape.Position = uno.createUnoStruct("com.sun.star.awt.Point", 2000, 2000)
76    shape.Size = uno.createUnoStruct("com.sun.star.awt.Size", 12000, 6000)
77    tbl = shape.Model
78    tbl.Columns.insertByIndex(1, 1)
79    tbl.Rows.insertByIndex(1, 1)
80    cs = cells(shape)
81    c0, c1, c2, c3 = [c for _, c in cs]
82    c0.TextVerticalAdjust = adj("TextVerticalAdjust", "CENTER")
83    c0.TextHorizontalAdjust = adj("TextHorizontalAdjust", "RIGHT")
84    c1.TextLeftDistance, c1.TextRightDistance, c1.TextUpperDistance, c1.TextLowerDistance = 500, 600, 300, 400
85    c2.TextWordWrap = True
86    c2.TextWritingMode = uno.Enum("com.sun.star.text.WritingMode", "RL_TB")
87    c3.TopBorder, c3.BottomBorder = border(0xff0000), border(0x00ff00)
88    c3.LeftBorder, c3.RightBorder = border(0x0000ff), border(0x808080)
89    c3.ParaAdjust = 1
90    c3.ParaTopMargin = 250
91    c3.CharWeight = 150.0
92    c3.CharHeight = 18.0
93    for n, c in cs:
94        show(n, c)
95    if os.path.exists(dst):
96        os.remove(dst)
97    doc.storeToURL(url(dst), (pv("FilterName", FILTERS["odp"]), pv("Overwrite", True)))
98    doc.close(True)
99    print("stored", dst)
100else:
101    doc = desk.loadComponentFromURL(url(dst), "_blank", 0, (pv("Hidden", True),))
102    page = doc.DrawPages.getByIndex(0)
103    shape = [page.getByIndex(i) for i in range(page.Count) if page.getByIndex(i).ShapeType.endswith("TableShape")][0]
104    for n, c in cells(shape):
105        show(n, c)
106    doc.close(True)
107