mirror of
https://github.com/GNS3/gns3-server.git
synced 2024-11-16 16:54:51 +02:00
Function for computing size of symbols
This commit is contained in:
parent
796ebf7210
commit
4d8cf8460e
@ -72,8 +72,8 @@ class Drawing:
|
||||
try:
|
||||
return data.decode()
|
||||
except UnicodeError:
|
||||
width, height = get_size(data)
|
||||
return "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" height=\"{height}\" width=\"{width}\">\n<image height=\"{height}\" width=\"{width}\" xlink:href=\"data:image/{extension};base64,{b64}\" />\n</svg>".format(b64=base64.b64encode(data).decode(), extension=filename.split(".")[1], width=width, height=width)
|
||||
width, height, filetype = get_size(data)
|
||||
return "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" height=\"{height}\" width=\"{width}\">\n<image height=\"{height}\" width=\"{width}\" xlink:href=\"data:image/{filetype};base64,{b64}\" />\n</svg>".format(b64=base64.b64encode(data).decode(), filetype=filetype, width=width, height=width)
|
||||
except OSError:
|
||||
log.warning("Image file %s missing", filename)
|
||||
return "<svg></svg>"
|
||||
|
@ -15,16 +15,22 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import io
|
||||
import struct
|
||||
from xml.etree.ElementTree import ElementTree
|
||||
|
||||
|
||||
def get_size(data):
|
||||
def get_size(data, default_width=0, default_height=0):
|
||||
"""
|
||||
Get image size
|
||||
:param data: A buffer with image content
|
||||
:return: Tuple (width, height)
|
||||
:return: Tuple (width, height, filetype)
|
||||
"""
|
||||
|
||||
|
||||
height = default_height
|
||||
width = default_width
|
||||
filetype = None
|
||||
|
||||
# Original version:
|
||||
# https://github.com/shibukawa/imagesize_py
|
||||
#
|
||||
@ -38,21 +44,20 @@ def get_size(data):
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
height = 0
|
||||
width = 0
|
||||
|
||||
size = len(data)
|
||||
# handle GIFs
|
||||
if size >= 10 and data[:6] in (b'GIF87a', b'GIF89a'):
|
||||
# Check to see if content_type is correct
|
||||
try:
|
||||
width, height = struct.unpack("<hh", data[6:10])
|
||||
filetype = "gif"
|
||||
except struct.error:
|
||||
raise ValueError("Invalid GIF file")
|
||||
# see png edition spec bytes are below chunk length then and finally the
|
||||
elif size >= 24 and data.startswith(b'\211PNG\r\n\032\n') and data[12:16] == b'IHDR':
|
||||
try:
|
||||
width, height = struct.unpack(">LL", data[16:24])
|
||||
filetype = "png"
|
||||
except struct.error:
|
||||
raise ValueError("Invalid PNG file")
|
||||
# Maybe this is for an older PNG version.
|
||||
@ -60,12 +65,14 @@ def get_size(data):
|
||||
# Check to see if we have the right content type
|
||||
try:
|
||||
width, height = struct.unpack(">LL", data[8:16])
|
||||
filetype = "png"
|
||||
except struct.error:
|
||||
raise ValueError("Invalid PNG file")
|
||||
# handle JPEGs
|
||||
elif size >= 2 and data.startswith(b'\377\330'):
|
||||
try:
|
||||
fhandle.seek(0) # Read 0xff next
|
||||
# Not very efficient to copy data to a buffer
|
||||
fhandle = io.BytesIO(data)
|
||||
size = 2
|
||||
ftype = 0
|
||||
while not 0xc0 <= ftype <= 0xcf:
|
||||
@ -78,13 +85,44 @@ def get_size(data):
|
||||
# We are at a SOFn block
|
||||
fhandle.seek(1, 1) # Skip `precision' byte.
|
||||
height, width = struct.unpack('>HH', fhandle.read(4))
|
||||
filetype = "jpg"
|
||||
except struct.error:
|
||||
raise ValueError("Invalid JPEG file")
|
||||
# handle JPEG2000s
|
||||
elif size >= 12 and data.startswith(b'\x00\x00\x00\x0cjP \r\n\x87\n'):
|
||||
fhandle.seek(48)
|
||||
# End of https://github.com/shibukawa/imagesize_py
|
||||
|
||||
# handle SVG
|
||||
elif size >= 10 and data.startswith(b'<?xml'):
|
||||
filetype = "svg"
|
||||
fhandle = io.BytesIO(data)
|
||||
tree = ElementTree()
|
||||
tree.parse(fhandle)
|
||||
root = tree.getroot()
|
||||
|
||||
try:
|
||||
height, width = struct.unpack('>LL', fhandle.read(8))
|
||||
except struct.error:
|
||||
raise ValueError("Invalid JPEG2000 file")
|
||||
return width, height
|
||||
width = _svg_convert_size(root.attrib["width"])
|
||||
height = _svg_convert_size(root.attrib["height"])
|
||||
except IndexError:
|
||||
raise ValueError("Invalid SVG file")
|
||||
|
||||
return width, height, filetype
|
||||
|
||||
def _svg_convert_size(size):
|
||||
"""
|
||||
Convert svg size to the px version
|
||||
|
||||
:param size: String with the size
|
||||
"""
|
||||
|
||||
# https://www.w3.org/TR/SVG/coords.html#Units
|
||||
conversion_table = {
|
||||
"pt": 1.25,
|
||||
"pc": 15,
|
||||
"mm": 3.543307,
|
||||
"cm": 35.43307,
|
||||
"in": 90
|
||||
}
|
||||
if len(size) > 3:
|
||||
if size[-2:] in conversion_table:
|
||||
return round(float(size[:-2]) * conversion_table[size[-2:]])
|
||||
|
||||
return round(float(size))
|
||||
|
BIN
tests/resources/gns3_icon_128x128.png
Normal file
BIN
tests/resources/gns3_icon_128x128.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.7 KiB |
BIN
tests/resources/gns3_icon_128x64.gif
Normal file
BIN
tests/resources/gns3_icon_128x64.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.7 KiB |
BIN
tests/resources/gns3_icon_128x64.jpg
Normal file
BIN
tests/resources/gns3_icon_128x64.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.0 KiB |
BIN
tests/resources/gns3_icon_128x64.png
Normal file
BIN
tests/resources/gns3_icon_128x64.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.5 KiB |
41
tests/utils/test_picture.py
Normal file
41
tests/utils/test_picture.py
Normal file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 GNS3 Technologies Inc.
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from gns3server.utils.picture import get_size
|
||||
|
||||
|
||||
def test_get_size():
|
||||
with open("tests/resources/nvram_iou", "rb") as f:
|
||||
res = get_size(f.read(), default_width=100, default_height=50)
|
||||
assert res == (100, 50, None)
|
||||
with open("tests/resources/gns3_icon_128x64.gif", "rb") as f:
|
||||
res = get_size(f.read())
|
||||
assert res == (128, 64, "gif")
|
||||
with open("tests/resources/gns3_icon_128x64.jpg", "rb") as f:
|
||||
res = get_size(f.read())
|
||||
assert res == (128, 64, "jpg")
|
||||
with open("tests/resources/gns3_icon_128x64.png", "rb") as f:
|
||||
res = get_size(f.read())
|
||||
assert res == (128, 64, "png")
|
||||
with open("gns3server/symbols/dslam.svg", "rb") as f:
|
||||
res = get_size(f.read())
|
||||
assert res == (50, 53, "svg")
|
||||
# Symbol using size with cm
|
||||
with open("gns3server/symbols/cloud.svg", "rb") as f:
|
||||
res = get_size(f.read())
|
||||
assert res == (159, 71, "svg")
|
Loading…
Reference in New Issue
Block a user