Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F64351371
transkriptionField.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, May 26, 07:36
Size
6 KB
Mime Type
text/x-python
Expires
Tue, May 28, 07:36 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17888243
Attached To
rNIETZSCHEPYTHON nietzsche-python
transkriptionField.py
View Options
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" This program can be used to transform a svg file according to the dimension of its transkription field.
"""
# Copyright (C) University of Basel 2019 {{{1
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/> 1}}}
import
sys
from
os.path
import
exists
from
svgpathtools
import
svg_to_paths
import
xml.etree.ElementTree
as
ET
from
xml.parsers.expat
import
ExpatError
__author__
=
"Christian Steiner"
__maintainer__
=
__author__
__copyright__
=
'University of Basel'
__email__
=
"christian.steiner@unibas.ch"
__status__
=
"Development"
__version__
=
"0.0.1"
class
TranskriptionField
:
"""
A class containing the dimensions of the transkription field.
Args:
filename (str): name of the svg file
"""
def
__init__
(
self
,
filename
):
self
.
width
=
0.0
self
.
height
=
0.0
self
.
xmin
=
0.0
self
.
xmax
=
0.0
self
.
ymin
=
0.0
self
.
ymax
=
0.0
self
.
documentWidth
=
0.0
self
.
documentHeight
=
0.0
self
.
path
=
None
self
.
filename
=
filename
MAX_SMALLER_PATH_WIDTH
=
50.0
MAX_SMALLER_PATH_HEIGHT
=
50.0
MAX_DIFF_DOC_SELF_WIDTH
=
100.0
MAX_DIFF_DOC_SELF_HEIGHT
=
100.0
try
:
paths
,
attributes
,
self
.
svg_attributes
=
svg_to_paths
.
svg2paths
(
filename
,
return_svg_attributes
=
True
)
except
ExpatError
:
#tb = sys.exc_info()[2]
raise
ExpatError
(
'File {} is empty!'
.
format
(
filename
))
#.with_traceback(tb)
if
len
(
self
.
svg_attributes
)
>
0
and
bool
(
self
.
svg_attributes
.
get
(
'viewBox'
)):
viewBox
=
(
self
.
svg_attributes
[
'viewBox'
]
.
split
())
else
:
raise
Exception
(
'File "{}" does not have an attribute "viewBox"'
.
format
(
filename
))
self
.
documentWidth
=
float
(
viewBox
[
2
])
self
.
documentHeight
=
float
(
viewBox
[
3
])
for
path
in
paths
:
if
bool
(
path
):
try
:
if
path
.
iscontinuous
()
and
path
.
isclosed
():
xmin
,
xmax
,
ymin
,
ymax
=
path
.
bbox
()
width
=
xmax
-
xmin
height
=
ymax
-
ymin
if
(
width
>
self
.
width
and
height
>
self
.
height
)
\
and
width
>
MAX_SMALLER_PATH_WIDTH
\
and
height
>
MAX_SMALLER_PATH_HEIGHT
\
and
self
.
documentWidth
-
width
>
MAX_DIFF_DOC_SELF_WIDTH
\
and
self
.
documentHeight
-
height
>
MAX_DIFF_DOC_SELF_HEIGHT
:
self
.
xmin
=
xmin
self
.
xmax
=
xmax
self
.
ymin
=
ymin
self
.
ymax
=
ymax
self
.
width
=
width
self
.
height
=
height
self
.
path
=
path
except
AssertionError
:
print
(
"Assertion Error"
)
if
self
.
is_shrunk
():
self
.
xmin
=
float
(
viewBox
[
0
])
self
.
ymin
=
float
(
viewBox
[
1
])
def
is_shrunk
(
self
):
"""Returns True if viewbox[0] and viewBox[1] != 0.
"""
if
len
(
self
.
svg_attributes
)
==
0
or
not
bool
(
self
.
svg_attributes
.
get
(
'viewBox'
)):
return
False
viewBox
=
self
.
svg_attributes
[
'viewBox'
]
.
split
()
return
float
(
viewBox
[
0
])
!=
0
and
float
(
viewBox
[
1
])
!=
0
def
get_svg_attributes
(
self
,
attrib_key
):
"""Returns the svg attribute for the corresponding key or None if empty.
"""
if
self
.
svg_attributes
is
None
or
len
(
self
.
svg_attributes
)
==
0
or
not
bool
(
self
.
svg_attributes
.
get
(
attrib_key
)):
return
None
return
self
.
svg_attributes
[
attrib_key
]
def
shrink_svg_to_transkription_field
(
self
,
target_filename
=
None
):
""" Changes the viewBox of the svg graphics to the size of the transkription field.
If a target_filename is specified, the changes are saved to a new file,
otherwise they are saved to the input file.
Args:
target_filename (str): name of the target svg file
"""
if
bool
(
self
.
svg_attributes
.
get
(
'xmlns'
)):
ET
.
register_namespace
(
''
,
self
.
svg_attributes
[
'xmlns'
])
if
bool
(
self
.
svg_attributes
.
get
(
'xmlns:xlink'
)):
ET
.
register_namespace
(
'xlink'
,
self
.
svg_attributes
[
'xmlns:xlink'
])
et
=
ET
.
parse
(
self
.
filename
)
root
=
et
.
getroot
()
if
bool
(
root
.
attrib
.
get
(
'viewBox'
)):
if
(
not
self
.
is_shrunk
()):
root
.
attrib
[
'viewBox'
]
=
'{} {} {} {}'
.
format
(
self
.
xmin
,
self
.
ymin
,
self
.
width
,
self
.
height
)
if
bool
(
root
.
attrib
.
get
(
'width'
)):
root
.
attrib
[
'width'
]
=
'{}pt'
.
format
(
self
.
width
)
if
bool
(
root
.
attrib
.
get
(
'height'
)):
root
.
attrib
[
'height'
]
=
'{}pt'
.
format
(
self
.
height
)
if
not
bool
(
target_filename
):
target_filename
=
self
.
filename
et
.
write
(
target_filename
)
return
0
else
:
#print('File {} already transformed!'.format(self.filename))
return
1
else
:
print
(
'ERROR: file {} does not contain a svg/@viewBox!'
.
format
(
self
.
filename
))
#TODO: throw error
return
2
def
transkription_field_found
(
self
):
""" Returns whether transkription field was found in __init__
:return: True/False
"""
return
self
.
width
>
0.0
and
self
.
height
>
0.0
and
self
.
xmin
>
0.0
and
self
.
xmax
>
0.0
and
self
.
ymin
>
0.0
and
self
.
ymax
>
0.0
def
getWidth
(
self
):
"""Returns documentWidth
"""
return
self
.
documentWidth
def
getHeight
(
self
):
"""Returns documentHeight if not is_shrunk, else height.
"""
return
self
.
documentHeight
Event Timeline
Log In to Comment