Skip to content

Commit

Permalink
Added labels to points (#52)
Browse files Browse the repository at this point in the history
First version for adding the labels of the points to the svg

---------

Co-authored-by: Juan Nunez-Iglesias <[email protected]>
  • Loading branch information
Micro-Sandworms and jni authored Jan 14, 2025
1 parent 921a275 commit 2875486
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
21 changes: 20 additions & 1 deletion napari_svg/_tests/test_write_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,23 @@ def test_write_points_with_attributes(request, tmp_path):
expected_path = Path(__file__).parent / f'{test_name}-expected.svg'
actual_text = path.read_text().replace(NOOP_TRANSFORM_STR, '')
expected_text = expected_path.read_text().replace(NOOP_TRANSFORM_STR, '')
assert actual_text == expected_text
assert actual_text == expected_text


def test_write_points_with_text(tmp_path):
data = [
[0, 0],
[0, 128],
[128, 128],
]
text_data = ['car', 'crosswalk', 'bicycle']
layer = Points(data, text=text_data)
layer_data, layer_attrs, _ = layer.as_layer_data_tuple()

path = tmp_path / 'points-with-text.svg'
return_path = napari_write_points(path, layer_data, layer_attrs)
assert return_path == path

svg_text = path.read_text().replace(NOOP_TRANSFORM_STR, '')
for elem in text_data:
assert elem in svg_text
28 changes: 26 additions & 2 deletions napari_svg/layer_to_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ def points_to_xml(data, meta):
else:
opacity = 1

if 'text' in meta:
text = meta['text']
else:
text = None

if 'font_size' in meta:
font_size = meta['font_size']
else:
font_size = '12'

# Check if more than 2 dimensional and if so error.
if data.shape[1] > 2:
raise ValueError('Points must be 2 dimensional to save as svg')
Expand All @@ -303,12 +313,13 @@ def points_to_xml(data, meta):
stroke_width = np.broadcast_to(stroke_width, (data.shape[0],)).copy()

if meta.get('border_width_is_relative') or meta.get('edge_width_is_relative'):
stroke_width *= size
stroke_width = (stroke_width * size)


transform = layer_transforms_to_xml_string(meta)
layer_xml = Element('g', transform=transform)

for p, s, fc, sc, sw in zip(points, size, face_color, stroke_color, stroke_width):
for i, (p, s, fc, sc, sw) in enumerate(zip(points, size, face_color, stroke_color, stroke_width)):
cx = str(p[1])
cy = str(p[0])
r = str(s / 2)
Expand All @@ -329,6 +340,19 @@ def points_to_xml(data, meta):
)
layer_xml.append(element)


if text is not None and 'array' in text['string'] and text['visible']:
text_color_int = (255 * np.array(text['color']['constant'])).astype(int)
text_fill = f'rgba{tuple(map(int, text_color_int))}'
text_element = Element(
'text',
x=cx, y=cy,
fill=text_fill,
**{'font-size': str(text['size'])}
)
text_element.text = text['string']['array'][i]
layer_xml.append(text_element)

return layer_xml, extrema


Expand Down

0 comments on commit 2875486

Please sign in to comment.