diff options
| author | Kaalleen <36401965+kaalleen@users.noreply.github.com> | 2024-05-21 20:26:22 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-21 20:26:22 +0200 |
| commit | bd53e413c72a00d9e8999df2b057929855a6151f (patch) | |
| tree | b0586e4dbe321ec10644282d5f23d94bfafc4526 /lib/tartan | |
| parent | 92f5c4a956346ff72aba504e9f47eb0ccc9e6dbd (diff) | |
Tartan stroke distance (#2931)
Diffstat (limited to 'lib/tartan')
| -rw-r--r-- | lib/tartan/palette.py | 47 | ||||
| -rw-r--r-- | lib/tartan/utils.py | 33 |
2 files changed, 59 insertions, 21 deletions
diff --git a/lib/tartan/palette.py b/lib/tartan/palette.py index 6f29c9ae..2e33fbc8 100644 --- a/lib/tartan/palette.py +++ b/lib/tartan/palette.py @@ -58,14 +58,13 @@ class Palette: for i, outer_sizer in enumerate(sizers): stripes = [] for stripe_sizer in outer_sizer.Children: - stripe = {'render': True, 'color': '#000000', 'width': '5'} + stripe = {'render': 1, 'color': '#000000', 'width': '5'} stripe_info = stripe_sizer.GetSizer() for color in stripe_info.GetChildren(): widget = color.GetWindow() if isinstance(widget, wx.CheckBox): # in embroidery it is ok to have gaps between the stripes - if not widget.GetValue(): - stripe['render'] = False + stripe['render'] = widget.Get3StateValue() elif isinstance(widget, wx.ColourPickerCtrl): stripe['color'] = widget.GetColour().GetAsString(wx.C2S_HTML_SYNTAX) elif isinstance(widget, wx.SpinCtrlDouble): @@ -113,7 +112,12 @@ class Palette: code = [] for i, direction in enumerate(self.palette_stripes): for stripe in direction: - render = '' if stripe['render'] else '?' + if stripe['render'] == 0: + render = '?' + elif stripe['render'] == 2: + render = '*' + else: + render = '' code.append(f"({stripe['color']}){render}{stripe['width']}") if i == 0 and self.equal_warp_weft is False: code.append("|") @@ -138,7 +142,7 @@ class Palette: :param code: the tartan pattern code to apply """ stripes = [] - stripe_info = re.findall(r'([a-zA-Z]+)(\?)?([0-9.]*)', code) + stripe_info = re.findall(r'([a-zA-Z]+)(\?|\*)?([0-9.]*)', code) for color, render, width in stripe_info: if not width: continue @@ -146,8 +150,14 @@ class Palette: width = float(width) * self.tt_unit if not color: color = '#000000' - render = '?' - stripes.append({'render': not bool(render), 'color': color, 'width': float(width)}) + render = 0 + elif render == '?': + render = 0 + elif render == '*': + render = 2 + else: + render = 1 + stripes.append({'render': render, 'color': color, 'width': float(width)}) self.palette_stripes[0] = stripes def parse_inkstitch_code(self, code_str: str) -> None: @@ -163,7 +173,7 @@ class Palette: code = code_str.split('|') for i, direction in enumerate(code): stripes = [] - stripe_info = re.findall(r'\(([0-9A-Za-z#]+)\)(\?)?([0-9.]+)', direction) + stripe_info = re.findall(r'\(([0-9A-Za-z#]+)\)(\?|\*)?([0-9.]+)', direction) for color, render, width in stripe_info: try: # on macOS we need to run wxpython color method inside the app otherwise @@ -174,8 +184,14 @@ class Palette: color = str(Color(color).to_named()) if not color: color = '#000000' - render = False - stripes.append({'render': not bool(render), 'color': color, 'width': float(width)}) + render = 0 + elif render == '?': + render = 0 + elif render == '*': + render = 2 + else: + render = 1 + stripes.append({'render': render, 'color': color, 'width': float(width)}) self.palette_stripes[i] = stripes def parse_threadcount_code(self, code: str) -> None: @@ -216,12 +232,12 @@ class Palette: stripe_info = re.findall(r'([a-zA-Z]+)([0-9.]*)', thread_code) for color, width in stripe_info: - render = True + render = 1 try: color = f'#{color_dict[color]}' except KeyError: color = '#000000' - render = False + render = 0 width = float(width) * self.tt_unit stripes.append({'render': render, 'color': color, 'width': width}) @@ -237,8 +253,13 @@ class Palette: :returns: the width of all tartan stripes in given direction """ width = 0 + stroke_width = 0 for stripe in self.palette_stripes[direction]: stripe_width = stripe['width'] * (scale / 100) - if stripe_width >= min_width or not stripe['render']: + if stripe_width >= min_width and stripe['render'] != 2: width += stripe_width + elif stripe_width < min_width and stripe['render'] != 0: + stroke_width += stripe_width + if width == 0: + width = stroke_width return width diff --git a/lib/tartan/utils.py b/lib/tartan/utils.py index b71b0384..4f64fc6f 100644 --- a/lib/tartan/utils.py +++ b/lib/tartan/utils.py @@ -55,6 +55,8 @@ def stripes_to_shapes( left = minx top = miny + add_to_stroke = 0 + add_to_fill = 0 i = -1 while True: i += 1 @@ -68,24 +70,39 @@ def stripes_to_shapes( right = left + width bottom = top + width - if (top > maxy and weft) or (left > maxx and not weft): + if ((top > maxy and weft) or (left > maxx and not weft) or + (add_to_stroke > maxy and weft) or (add_to_stroke > maxx and not weft)): return _merge_polygons(shapes, outline, intersect_outline) - if not stripe['render']: - left = right - top = bottom + if stripe['render'] == 0: + left = right + add_to_stroke + top = bottom + add_to_stroke + add_to_stroke = 0 + continue + elif stripe['render'] == 2: + add_to_stroke += width continue shape_dimensions = [top, bottom, left, right, minx, miny, maxx, maxy] if width <= min_stripe_width * PIXELS_PER_MM: + add_to_fill = add_to_stroke + shape_dimensions[0] += add_to_stroke + shape_dimensions[2] += add_to_stroke linestrings = _get_linestrings(outline, shape_dimensions, rotation, rotation_center, weft) shapes[stripe['color']].extend(linestrings) + add_to_stroke += width continue + add_to_stroke = 0 + + # add the space of the lines to the following object to avoid bad symmetry + shape_dimensions[1] += add_to_fill + shape_dimensions[3] += add_to_fill polygon = _get_polygon(shape_dimensions, rotation, rotation_center, weft) shapes[stripe['color']].append(polygon) - left = right - top = bottom + left = right + add_to_fill + top = bottom + add_to_fill + add_to_fill = 0 def _merge_polygons( @@ -252,9 +269,9 @@ def get_tartan_stripes(settings: dict) -> Tuple[list, list]: warp = [] if palette.get_palette_width(settings['scale'], settings['min_stripe_width'], 1) == 0: weft = [] - if len([stripe for stripe in warp if stripe['render'] is True]) == 0: + if len([stripe for stripe in warp if stripe['render'] == 1]) == 0: warp = [] - if len([stripe for stripe in weft if stripe['render'] is True]) == 0: + if len([stripe for stripe in weft if stripe['render'] == 1]) == 0: weft = [] if palette.equal_warp_weft: |
