Skip to content

Commit

Permalink
[Worksheet][Added] Undocumented font face and color
Browse files Browse the repository at this point in the history
Now can be used, but not correctly rendered

See #695
  • Loading branch information
set-soft committed Oct 16, 2024
1 parent 15ac217 commit bc58773
Show file tree
Hide file tree
Showing 10 changed files with 1,658 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- SVG: `use_aux_axis_as_origin` option (#681)
- Report: thickness units (#685)
- PCB Print: Note about `colored_pads` and `colored_vias` side effects (#682)
- Worksheet: undocumented font face and color now can be used, but not
correctly rendered (See #695)

### Fixed
- PCB Print: allow specifying `repeat_for_layer` with empty `repeat_layers`.
Expand Down
3 changes: 2 additions & 1 deletion docs/samples/generic_plot.kibot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2603,7 +2603,8 @@ outputs:
# internal: KiBot loads the `.kicad_wks` and does the drawing work.
# Best option, but some details are different from what the GUI generates.
# plot: uses KiCad Python API. Not available for KiCad 5.
# You get the default frame and some substitutions doesn't work
# You get the default frame and some substitutions doesn't work. |br|
# Important: colors and fonts doesn't work, not supported by the API. Try *gui*, might work
frame_plot_mechanism: 'internal'
# [boolean=false] Hide components in the Fab layer that are marked as excluded by a variant.
# Affected by global options
Expand Down
2 changes: 2 additions & 0 deletions docs/source/Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Added
- Report: thickness units (#685)
- PCB Print: Note about ``colored_pads`` and ``colored_vias`` side
effects (#682)
- Worksheet: undocumented font face and color now can be used, but not
correctly rendered (See #695)

Fixed
~~~~~
Expand Down
26 changes: 26 additions & 0 deletions kibot/kicad/sexp_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
# 'kicad_sch', 'lib_symbols', 'symbol', 'sheet', 'sheet_instances', 'symbol_instances'}


def _symbol(name, content=None):
if content is None:
return [Symbol(name)]
return [Symbol(name)] + content


class Point(object):
def __init__(self, items):
super().__init__()
Expand All @@ -22,6 +28,26 @@ def parse(items):
return Point(items)


class Color(object):
def __init__(self, items=None):
super().__init__()
if items:
self.r = _check_integer(items, 1, 'red color')
self.g = _check_integer(items, 2, 'green color')
self.b = _check_integer(items, 3, 'blue color')
# Sheet sheet.fill.color is float ...
self.a = _check_float(items, 4, 'alpha color')
else:
self.r = self.g = self.b = self.a = 0

@staticmethod
def parse(items):
return Color(items)

def write(self):
return _symbol('color', [self.r, self.g, self.b, self.a])


def make_separated(sexp):
""" Add separators to make the file more readable """
if not isinstance(sexp, list):
Expand Down
28 changes: 1 addition & 27 deletions kibot/kicad/v6_sch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .sexp_helpers import (_check_is_symbol_list, _check_len, _check_len_total, _check_symbol, _check_hide, _check_integer,
_check_float, _check_str, _check_symbol_value, _check_symbol_float, _check_symbol_int,
_check_symbol_str, _get_offset, _get_yes_no, _get_at, _get_size, _get_xy, _get_points,
_check_relaxed)
_check_relaxed, Color, _symbol)
from .v5_sch import SchematicComponent, Schematic

logger = log.get_logger()
Expand Down Expand Up @@ -268,26 +268,6 @@ def write(self):
return _symbol('effects', data)


class Color(object):
def __init__(self, items=None):
super().__init__()
if items:
self.r = _check_integer(items, 1, 'red color')
self.g = _check_integer(items, 2, 'green color')
self.b = _check_integer(items, 3, 'blue color')
# Sheet sheet.fill.color is float ...
self.a = _check_float(items, 4, 'alpha color')
else:
self.r = self.g = self.b = self.a = 0

@staticmethod
def parse(items):
return Color(items)

def write(self):
return _symbol('color', [self.r, self.g, self.b, self.a])


class Stroke(object):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -1816,12 +1796,6 @@ def parse(items):
return layers


def _symbol(name, content=None):
if content is None:
return [Symbol(name)]
return [Symbol(name)] + content


def _symbol_yn(name, val):
return [Symbol(name), NO_YES[val]]

Expand Down
19 changes: 15 additions & 4 deletions kibot/kicad/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
if GS.ki7:
from pcbnew import (PCB_SHAPE, PCB_TEXT, FILL_T_FILLED_SHAPE, SHAPE_T_POLY, GR_TEXT_H_ALIGN_LEFT,
GR_TEXT_H_ALIGN_RIGHT, GR_TEXT_H_ALIGN_CENTER, GR_TEXT_V_ALIGN_TOP, GR_TEXT_V_ALIGN_CENTER,
GR_TEXT_V_ALIGN_BOTTOM)
GR_TEXT_V_ALIGN_BOTTOM, COLOR4D)
# Is this change really needed??!!! People doesn't have much to do ...
GR_TEXT_HJUSTIFY_LEFT = GR_TEXT_H_ALIGN_LEFT
GR_TEXT_HJUSTIFY_RIGHT = GR_TEXT_H_ALIGN_RIGHT
Expand All @@ -34,17 +34,17 @@
elif GS.ki6:
from pcbnew import (PCB_SHAPE, PCB_TEXT, FILL_T_FILLED_SHAPE, SHAPE_T_POLY, GR_TEXT_HJUSTIFY_LEFT,
GR_TEXT_HJUSTIFY_RIGHT, GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_TOP, GR_TEXT_VJUSTIFY_CENTER,
GR_TEXT_VJUSTIFY_BOTTOM)
GR_TEXT_VJUSTIFY_BOTTOM, COLOR4D)
else:
from pcbnew import (DRAWSEGMENT, TEXTE_PCB, GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_HJUSTIFY_RIGHT, GR_TEXT_HJUSTIFY_CENTER,
GR_TEXT_VJUSTIFY_TOP, GR_TEXT_VJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_BOTTOM)
GR_TEXT_VJUSTIFY_TOP, GR_TEXT_VJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_BOTTOM, COLOR4D)
PCB_SHAPE = DRAWSEGMENT
PCB_TEXT = TEXTE_PCB
FILL_T_FILLED_SHAPE = 0
SHAPE_T_POLY = 4
from .sexpdata import load, SExpData
from .sexp_helpers import (_check_is_symbol_list, _check_float, _check_integer, _check_symbol_value, _check_str, _check_symbol,
_check_relaxed, _get_points, _check_symbol_str)
_check_relaxed, _get_points, _check_symbol_str, Color)
from ..svgutils.transform import ImageElement, GroupElement
from ..misc import W_WKSVERSION
from .. import log
Expand Down Expand Up @@ -225,6 +225,8 @@ def __init__(self):
self.italic = False
self.size = wxSize(setup.text_w, setup.text_h)
self.line_width = setup.text_line_width
self.color = None
self.face = None

@staticmethod
def parse(items):
Expand All @@ -239,6 +241,10 @@ def parse(items):
s.size = _get_size(items, c+1, i_type, WksFont.name)
elif i_type == 'linewidth':
s.line_width = _check_mm(i, 1, i_type)
elif i_type == 'color': # Undocumented, as usually
s.color = Color.parse(i)
elif i_type == 'face': # Undocumented, as usually
s.face = _check_str(i, 1, 'font face')
else:
raise WksError('Unknown font attribute `{}`'.format(i))
return s
Expand Down Expand Up @@ -317,6 +323,11 @@ def draw(e, p):
s.SetLayer(p.layer)
if e.font.italic:
s.SetItalic(True)
if e.font.color:
# For KiCad 8.0.5 this is useless because the plot API fails to use the color
s.SetTextColor(COLOR4D(e.font.color.r/255.0, e.font.color.g/255.0, e.font.color.b/255.0, e.font.color.a))
# if e.face:
# ... incomplete API for 8.0.5, SetFont needs KIFONT::FONT, not defined
if e.rotate:
s.SetTextAngle(GS.angle(e.rotate))
# Adjust the text size to the maximum allowed
Expand Down
7 changes: 6 additions & 1 deletion kibot/out_pcb_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ def __init__(self):
internal: KiBot loads the `.kicad_wks` and does the drawing work.
Best option, but some details are different from what the GUI generates.
plot: uses KiCad Python API. Not available for KiCad 5.
You get the default frame and some substitutions doesn't work """
You get the default frame and some substitutions doesn't work. |br|
Important: colors and fonts doesn't work, not supported by the API. Try *gui*, might work"""
self.pages = PagesOptions
""" *[list(dict)=[]] List of pages to include in the output document.
Each page contains one or more layers of the PCB """
Expand Down Expand Up @@ -540,6 +541,8 @@ def plot_frame_internal(self, pc, po, p, page, pages):
po.SetPlotFrameRef(False)
po.SetScale(1.0)
po.SetNegative(False)
# Trying to set the color mode here doesn't change the mode (GetColorMode() returns False)
# pc.SetColorMode(True)
pc.SetLayer(self.cleared_layer)
# Load the WKS
error = None
Expand All @@ -552,6 +555,8 @@ def plot_frame_internal(self, pc, po, p, page, pages):
tb_vars = self.fill_kicad_vars(page, pages, p)
ws.draw(GS.board, self.cleared_layer, page, self.paper_w, self.paper_h, tb_vars)
pc.OpenPlotfile('frame', PLOT_FORMAT_SVG, p.sheet)
# At least on 8.0.5 the following is useless:
# pc.SetColorMode(True)
pc.PlotLayer()
pc.ClosePlot()
ws.undraw(GS.board)
Expand Down
Loading

0 comments on commit bc58773

Please sign in to comment.