1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 from libreoffice
.util
import printing
14 class BigIntPrinter(object):
15 '''Prints big integer'''
17 def __init__(self
, typename
, val
):
24 return self
.val
['nVal']
27 len = self
.val
['nLen']
28 digits
= self
.val
['nNum']
29 dsize
= digits
.dereference().type.sizeof
* 8
31 # The least significant byte is on index 0
32 for i
in reversed(range(0, len)):
37 class ColorPrinter(object):
38 '''Prints color as rgb(r, g, b) or rgba(r, g, b, a)'''
40 def __init__(self
, typename
, val
):
49 return "rgba(%d, %d, %d, %d)" % (r
, g
, b
, 255 - t
)
51 return "rgb(%d, %d, %d)" % (r
, g
, b
)
53 class DateTimeImpl(object):
55 def __init__(self
, date
, time
):
62 result
+= str(self
.date
)
66 result
+= str(self
.time
)
71 return DateTimeImpl(DateImpl
.parse(val
), TimeImpl
.parse(val
))
73 class DateTimePrinter(object):
74 '''Prints date and time'''
76 def __init__(self
, typename
, val
):
80 return str(DateTimeImpl
.parse(self
.val
))
82 class DateImpl(DateTimeImpl
):
84 def __init__(self
, year
, month
, day
):
85 super(DateImpl
, self
).__init
__(self
, None)
91 return "%d-%d-%d" % (self
.year
, self
.month
, self
.day
)
99 m
= (date
/ 100) % 100
101 return DateImpl(y
, m
, d
)
103 class DatePrinter(object):
106 def __init__(self
, typename
, val
):
110 return str(DateImpl
.parse(self
.val
))
112 class TimeImpl(DateTimeImpl
):
114 def __init__(self
, hour
, minute
, second
, nanosecond
= 0):
115 super(TimeImpl
, self
).__init
__(None, self
)
119 self
.nanosecond
= nanosecond
123 if self
.nanosecond
!= 0:
124 decimal
= '.%09d' % self
.nanosecond
125 return "%02d:%02d:%02d%s" % (self
.hour
, self
.minute
, self
.second
, decimal
)
130 h
= time
/ 10000000000000
131 m
= (time
/ 100000000000) % 100
132 s
= (time
/ 1000000000) % 100
133 ns
= time
% 1000000000
134 return TimeImpl(h
, m
, s
, ns
)
136 class TimePrinter(object):
139 def __init__(self
, typename
, val
):
143 return str(TimeImpl
.parse(self
.val
))
145 class PointPrinter(object):
146 '''Prints a Point.'''
148 def __init__(self
, typename
, value
):
149 self
.typename
= typename
153 return "%s" % (self
.typename
)
156 x
= self
.value
['mnA']
157 y
= self
.value
['mnB']
158 children
= [('x', x
), ('y', y
)]
159 return children
.__iter
__()
161 class SizePrinter(object):
164 def __init__(self
, typename
, value
):
165 self
.typename
= typename
169 return "%s" % (self
.typename
)
172 width
= self
.value
['mnA']
173 height
= self
.value
['mnB']
174 children
= [('width', width
), ('height', height
)]
175 return children
.__iter
__()
177 class RectanglePrinter(object):
178 '''Prints a Rectangle.'''
180 def __init__(self
, typename
, value
):
181 self
.typename
= typename
185 return "%s" % (self
.typename
)
188 left
= self
.value
['mnLeft']
189 top
= self
.value
['mnTop']
190 right
= self
.value
['mnRight']
191 bottom
= self
.value
['mnBottom']
192 children
= [('left', left
), ('top', top
), ('right', right
), ('bottom', bottom
)]
193 return children
.__iter
__()
197 def build_pretty_printers():
200 printer
= printing
.Printer('libreoffice/tl')
203 printer
.add('BigInt', BigIntPrinter
)
204 printer
.add('Color', ColorPrinter
)
205 printer
.add('DateTime', DateTimePrinter
)
206 printer
.add('Date', DatePrinter
)
207 printer
.add('Time', TimePrinter
)
208 printer
.add('Point', PointPrinter
)
209 printer
.add('Size', SizePrinter
)
210 printer
.add('Rectangle', RectanglePrinter
)
212 def register_pretty_printers(obj
):
213 printing
.register_pretty_printer(printer
, obj
)
215 build_pretty_printers()
217 # vim:set shiftwidth=4 softtabstop=4 expandtab: