Skip to content

Commit

Permalink
svg upside down macro fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ajt1528 committed Feb 4, 2025
1 parent ba2c377 commit 54a49a4
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/pygerber/gerberx3/renderer2/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class SvgRenderingFrame:
polarity: Optional[Polarity] = None
is_region: bool = False
flip_y: bool = True
flip_around_center: bool = True

def get_group_or_mask(
self,
Expand All @@ -92,13 +93,15 @@ def __init__(
color_scheme: ColorScheme = ColorScheme.DEBUG_1,
scale: Decimal = Decimal("1"),
*,
flip_y: bool = True,
flip_y: bool = True, # AT: Default true, false fixes inversion issue but inverts the entire document.
flip_around_center: bool = True #####
) -> None:
if not IS_SVG_BACKEND_AVAILABLE:
raise DRAWSVGNotAvailableError
self.color_scheme = color_scheme
self.scale = scale
self.flip_y = flip_y
self.flip_around_center = flip_around_center #####

def init(
self,
Expand All @@ -116,6 +119,7 @@ def init(
bounding_box=self.command_buffer.get_bounding_box(),
normalize_origin_to_0_0=True,
flip_y=self.flip_y,
flip_around_center=self.flip_around_center, #####
),
]
self.apertures: dict[str, drawsvg.Group] = {}
Expand All @@ -126,13 +130,15 @@ def push_render_frame(
*,
normalize_origin_to_0_0: bool,
flip_y: bool,
flip_around_center: bool,
) -> None:
"""Push new segment render frame."""
self.rendering_stack.append(
SvgRenderingFrame(
bounding_box=bbox,
normalize_origin_to_0_0=normalize_origin_to_0_0,
flip_y=flip_y,
flip_around_center=flip_around_center, #####
),
)

Expand Down Expand Up @@ -244,6 +250,7 @@ def convert_y(self, y: Offset) -> Decimal:
y,
normalize_origin_to_0_0=self.current_frame.normalize_origin_to_0_0,
flip_y=self.current_frame.flip_y,
flip_around_center=self.current_frame.flip_around_center #####
)

def _convert_y(
Expand All @@ -252,6 +259,7 @@ def _convert_y(
*,
normalize_origin_to_0_0: bool,
flip_y: bool,
flip_around_center: bool = True #####
) -> Decimal:
"""Convert y offset to pixel y coordinate."""
if normalize_origin_to_0_0:
Expand All @@ -262,11 +270,15 @@ def _convert_y(
corrected_position_y = y.as_millimeters() - origin_offset_y

if flip_y:
flipped_position_y = (
self.current_frame.bounding_box.height.as_millimeters()
- corrected_position_y
)
if flip_around_center: #####
flipped_position_y = (
self.current_frame.bounding_box.height.as_millimeters()
- corrected_position_y
)
else: #####
flipped_position_y = -corrected_position_y #####
return flipped_position_y * self.scale

return corrected_position_y * self.scale

def convert_size(self, diameter: Offset) -> Decimal:
Expand Down Expand Up @@ -663,7 +675,8 @@ def render_flash_macro(self, command: Flash2, aperture: Macro2) -> None:
self.push_render_frame(
command.get_bounding_box(),
normalize_origin_to_0_0=False,
flip_y=False,
flip_y=True,
flip_around_center=False, #####
)
for cmd in aperture.command_buffer:
cmd.render(self.renderer)
Expand Down

0 comments on commit 54a49a4

Please sign in to comment.