forked from fsmMLK/inkscapeMadeEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inkscapeMadeEasy_Draw.py
1902 lines (1504 loc) · 85.4 KB
/
inkscapeMadeEasy_Draw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# --------------------------------------------------------------------------------------
#
# inkscapeMadeEasy: - Helper module that extends Aaron Spike's inkex.py module,
# focusing productivity in inkscape extension development
#
# Copyright (C) 2016 by Fernando Moura
#
# 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/>.
#
# --------------------------------------------------------------------------------------
# Please uncomment (remove the # character) in the following line to disable LaTeX support via textext extension.
#useLatex=False
try:
useLatex
except NameError:
useLatex=True
else:
useLatex=False
import inkex
import math
import simplestyle
import numpy as np
from lxml.etree import tostring
if useLatex:
import textextLib.textext as textext
import sys
import tempfile
"""
This module contains a set of classes and some functions to help dealing with drawings.
This module requires the following modules: inkex, math, simplestyle (from inkex module), numpy, lxml and sys
"""
def displayMsg(msg):
"""Displays a message to the user.
:returns: nothing
:rtype: -
.. note:: Identical function has been also defined inside inkscapeMadeEasy class
"""
sys.stderr.write(msg + '\n')
def Dump(obj, file='./dump_file.txt', mode='w'):
"""Function to easily output the result of ``str(obj)`` to a file
This function was created to help debugging the code while it is running under inkscape. Since inkscape does not possess a terminal as today (2016), this function overcomes partially the issue of sending things to stdout by dumping result of the function ``str()`` in a text file.
:param obj: object to sent to the file. Any type that can be used in ``str()``
:param file: file path. Default: ``./dump_file.txt``
:param mode: writing mode of the file. Default: ``w`` (write)
:type obj: any, as long as ``str(obj``) is implemented (see ``__str__()`` metaclass definition )
:type arg2: string
:type mode: string
:returns: nothing
:rtype: -
.. note:: Identical function has been also defined inside inkscapeMadeEasy class
**Example**
>>> vector1=[1,2,3,4,5,6]
>>> Dump(vector,file='~/temporary.txt',mode='w') % writes the list to a file
>>> vector2=[7,8,9,10]
>>> Dump(vector2,file='~/temporary.txt',mode='a') % append the list to a file
"""
file = open(file, mode)
file.write(str(obj) + '\n')
file.close()
class color():
"""
Class to manipulate colors.
This class manipulates color information, generating a string in inkscape's expected format ``#RRGGBB``
This class contains only static methods so that you don't have to inherit this in your class
.. note:: alpha channel is not implemented yet. Assume alpha=1.0
"""
@staticmethod
def defined(colorName):
""" Returns the color string representing a predefined color name
:param colorName: color name
:type colorName: string
:returns: string representing the color in inkscape's expected format ``#RRGGBB``
:rtype: string
**Available pre defined colors**
.. image:: ../imagesDocs/Default_colors.png
:width: 400px
**Example**
>>> colorString = color.defined('red') # returns #ff0000 representing red color
"""
if colorName not in ['Dred', 'red', 'Lred',
'Dblue', 'blue', 'Lblue',
'Dgreen', 'green', 'Lgreen',
'Dyellow', 'yellow', 'Lyellow',
'Dmagen', 'magen', 'Lmagen',
'black', 'white']:
sys.exit("InkscapeDraw.color.defined() : Error. color -->" + colorName + "<-- not defined")
if colorName == 'Dred':
return '#800000'
if colorName == 'red':
return '#FF0000'
if colorName == 'Lred':
return '#FF8181'
if colorName == 'Dblue':
return '#000080'
if colorName == 'blue':
return '#0000FF'
if colorName == 'Lblue':
return '#8181FF'
if colorName == 'Dgreen':
return '#008000'
if colorName == 'green':
return '#00FF00'
if colorName == 'Lgreen':
return '#81FF81'
if colorName == 'black':
return '#000000'
if colorName == 'white':
return '#FFFFFF'
if colorName == 'Dyellow':
return '#808000'
if colorName == 'yellow':
return '#FFFF00'
if colorName == 'Lyellow':
return '#FFFF81'
if colorName == 'Dmagen':
return '#800080'
if colorName == 'magen':
return '#FF00FF'
if colorName == 'Lmagen':
return '#FF81FF'
@staticmethod
def RGB(RGBlist):
""" returns a string representing a color specified by RGB level in the range 0-255
:param RGBlist: list containing RGB levels in the range 0-225 each
:type RGBlist: list
:returns: string representing the color in inkscape's expected format ``#RRGGBB``
:rtype: string
**Example**
>>> colorString = color.RGB([120,80,0]) # returns a string representing the color R=120, G=80, B=0
"""
RGBhex = [''] * 3
for i in range(3):
if RGBlist[i] > 255:
RGBlist[i] = 255
if RGBlist[i] < 0:
RGBlist[i] = 0
if RGBlist[i] < 16:
RGBhex[i] = '0' + hex(int(RGBlist[i]))[2:].upper()
else:
RGBhex[i] = hex(int(RGBlist[i]))[2:].upper()
return '#' + '%s%s%s' % (RGBhex[0], RGBhex[1], RGBhex[2])
#---------------------------------------------
@staticmethod
def gray(percentage):
""" returns a gray level compatible string based on white percentage between 0.0 and 1.0
if percentage is higher than 1.0, percentage is truncated to 1.0 (white)
if percentage is lower than 0.0, percentage is truncated to 0.0 (black)
:param percentage: value between 0.0 (black) and 1.0 (white)
:type percentage: float
:returns: string representing the color in inkscape's expected format ``#RRGGBB``
:rtype: string
**Example**
>>> colorString = color.gray(0.6) # returns a string representing the gray level with 60% of white
"""
RGBLevel = 255 * percentage
if percentage > 1.0:
RGBLevel = 255
if percentage < 0.0:
RGBLevel = 0
return color.RGB([RGBLevel] * 3)
#---------------------------------------------
@staticmethod
def colorPickerToRGBalpha(colorPickerString):
""" Function that converts the string returned by the widget 'color' in the .inx file into 2 strings, one representing the color in format ``#RRGGBB`` and the other representing the alpha channel ``AA``
:param colorPickerString: string returned by 'color' widget
:type colorPickerString: string
:returns: a list of strings: [color,alpha]
- color: string in ``#RRGGBB`` format
- alpha: string in ``AA`` format
:rtype: list
.. note:: For more information on this widget, see <http://wiki.inkscape.org/wiki/index.php/INX_Parameters>
.. Warning:: you probably don't need to use this function. Consider using the method ``color.parseColorPicker()``
**usage**
1- in your inx file you must have one attribute of the type 'color'::
<param name="myColorPicker" type="color"></param>
2- in your .py file, you must parse it as a string:
>>> self.OptionParser.add_option("--myColorPicker", action="store", type="string", dest="myColorPickerVar", default='0')
3- call this function to convert so.myColorPickerVar to two strings
- #RRGGBB with RGB values in hex
- AA with alpha value in hex
**Example**
Let your .inx file contains a widget of type 'color' with the name myColorPicker::
<param name="myColorPicker" type="color"></param>
Then in the .py file
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> inkex.Effect.__init__(self)
>>> self.OptionParser.add_option("--myColorPicker", action="store", type="string", dest="myColorPickerVar", default='#000000') # parses the input parameter
>>>
>>> def effect(self):
>>> color,alpha = inkDraw.color.colorPickerToRGBalpha(self.options.myColorPickerVar) # returns the string representing the selected color and alpha channel
"""
colorHex = hex(int(colorPickerString) & 0xffffffff)[2:].zfill(8).upper() # [2:] removes the 0x , zfill adds the leading zeros, upper: uppercase
RGB = '#' + colorHex[0:6]
alpha = colorHex[6:]
return [RGB, alpha]
#---------------------------------------------
@staticmethod
def parseColorPicker(stringColorOption, stringColorPicker):
""" Function that converts the string returned by the widgets 'color' and 'optiongroup' in the .inx file into 2 strings, one representing the color in format ``#RRGGBB`` and the other representing the alpha channel ``AA``
You must have in your .inx both 'optiongroup' and 'color' widgets as defined below. You don't have to have all the color options presented in the example. That is the most complete example, considering the default colors in color.defined method.
:param stringColorOption: string returned by 'optiongroup' widget
:type stringColorOption: string
:param stringColorPicker: string returned by 'color' widget
:type stringColorPicker: string
:returns: a list of strings: [color,alpha]
- color: string in ``#RRGGBB`` format
- alpha: string in ``AA`` format
:rtype: list
.. note:: For more information on this widget, see <http://wiki.inkscape.org/wiki/index.php/INX_Parameters>
**Example**
It works in the following manner: The user select in the optiongroup list the desired color. All pre defined colors are listed there. There is also a 'my default color' where you can set your preferred default color and a 'use color picker' to select from the color picker widget. Keep in mind that the selected color in this widget will be considered ONLY if 'use color picker' option is selected.
Let your .inx file contains a widget of type 'color' with the name 'myColorPicker' and another 'optiongroup' with the name 'myColorOption'::
<param name="myColorOption" type="optiongroup" appearance="minimal" _gui-text="some text here">
<_option value="#FF0022">my default color</_option> <--you can set your pre define color in the form #RRGGBB
<_option value="none">none</_option> <-- no color
<_option value="black">black</_option>
<_option value="red">red</_option>
<_option value="blue">blue</_option>
<_option value="yellow">yellow</_option>
<_option value="green">green</_option> <-- these are all standardized colors in inkscapeMadeEasy_Draw.color class!
<_option value="magen">magenta</_option>
<_option value="white">white</_option>
<_option value="Lred">Lred</_option>
<_option value="Lblue">Lblue</_option>
<_option value="Lyellow">Lyellow</_option>
<_option value="Lgreen">Lgreen</_option>
<_option value="Lmagen">Lmagenta</_option>
<_option value="Dred">Dred</_option>
<_option value="Dblue">Dblue</_option>
<_option value="Dyellow">Dyellow</_option>
<_option value="Dgreen">Dgreen</_option>
<_option value="Dmagen">Dmagenta</_option>
<_option value="picker">use color picker</_option> <-- indicate that the color must be taken from the colorPicker attribute
</param>
<param name="myColorPicker" type="color"></param>
Then in the .py file
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> inkex.Effect.__init__(self)
>>> self.OptionParser.add_option("--myColorPicker", action="store", type="string", dest="myColorPickerVar", default='0') # parses the input parameters
>>> self.OptionParser.add_option("--myColorOption", action="store", type="string", dest="myColorOptionVar", default='black') # parses the input parameter
>>>
>>> def effect(self):
>>> so = self.options
>>> [RGBstring,alpha] = inkDraw.color.parseColorPicker(so.myColorOptionVar,so.myColorPickerVar)
"""
alphaString = 'FF'
if stringColorOption.startswith("#"):
return [stringColorOption, alphaString]
else:
if stringColorOption == 'none':
colorString = 'none'
else:
if stringColorOption == 'picker':
[colorString, alphaString] = color.colorPickerToRGBalpha(stringColorPicker)
else:
colorString = color.defined(stringColorOption)
return [colorString, alphaString]
class marker():
"""
Class to manipulate markers.
This class is used to create new custom markers. Markers can be used with the lineStyle class to define line types that include start, mid and end markers
This class contains only static methods so that you don't have to inherit this in your class
"""
#---------------------------------------------
@staticmethod
def createMarker(ExtensionBaseObj, nameID, markerPath, RenameMode=0, strokeColor=color.defined('black'), fillColor=color.defined('black'), lineWidth=1.0, markerTransform=None):
"""Creates a custom line marker
:param ExtensionBaseObj: Most of the times you have to use 'self' from inkscapeMadeEasy related objects
:param nameID: nameID of the marker
:param markerPath: path definition. Must follow 'd' attribute format. See <https://www.w3.org/TR/SVG/paths.html#PathElement> for further information
:param RenameMode: Renaming behavior mode
- 0: (default) do not rename the marker. If nameID is already taken, the marker will not be modified.
- 1: overwrite marker definition if nameID is already taken
- 2: Create a new unique nameID, adding a suffix number (Please refer to inkscapeMadeEasy.uniqueIdNumber(prefix_id) ).
:param strokeColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:param fillColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:param lineWidth: line width of the marker. Default: 1.0
:param markerTransform: custom transform applied to marker's path. Default: ``None``
the transform must follow 'transform' attribute format. See <https://www.w3.org/TR/SVG/coords.html#TransformAttribute> for further information
:type ExtensionBaseObj: inkscapeMadeEasy object (see example below)
:type nameID: string
:type markerPath: string
:type RenameMode: int
:type strokeColor: string
:type fillColor: string
:type lineWidth: float
:type markerTransform: string
:returns: NameID of the new marker
:rtype: string
**System of coordinates**
The system of coordinates of the marker depends on the point under consideration. The following figure presents the coordinate system for all cases
.. image:: ../imagesDocs/marker_Orientation.png
:width: 600px
**Example**
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> ...
>>> ...
>>>
>>> def effect(self):
>>> nameID='myMarker'
>>> markerPath='M 3,0 L 0,1 L 0,-1 z' # defines a path forming an triangle with vertices (3,0) (0,1) (0,-1)
>>> strokeColor=inkDraw.color.defined('red')
>>> fillColor=None
>>> RenameMode=1
>>> width=1
>>> markerTransform=None
>>> markerID=inkDraw.marker.createMarker(self,nameID,markerPath,RenameMode,strokeColor,fillColor,width,markerTransform)
>>> myLineStyle = inkDraw.lineStyle.set(1.0, markerEnd=markerID,lineColor=inkDraw.color.defined('black')) # see lineStyle class for further information on this function
>>>
>>> #tries to make another marker with the same nameID, changing RenameMode
>>> strokeColor=inkDraw.color.defined('blue')
>>> RenameMode=0
>>> markerID=inkDraw.marker.createMarker(self,nameID,RenameMode,scale,strokeColor,fillColor) # this will not modify the marker
>>> RenameMode=1
>>> markerID=inkDraw.marker.createMarker(self,nameID,RenameMode,scale,strokeColor,fillColor) # modifies the marker 'myMarker'
>>> RenameMode=2
>>> markerID=inkDraw.marker.createMarker(self,nameID,RenameMode,scale,strokeColor,fillColor) # creates a new marker with nameID='myMarker-0001'
.. note:: In next versions, path definition and transformation will be modified to make it easier. =)
"""
# print tostring(ExtensionBaseObj.getDefinitions())
if RenameMode == 0 and ExtensionBaseObj.findMarker(nameID):
return nameID
if RenameMode == 2:
numberID = 1
new_id = nameID + '_n%05d' % (numberID)
while new_id in ExtensionBaseObj.doc_ids:
numberID += 1
new_id = nameID + '_n%05d' % (numberID)
ExtensionBaseObj.doc_ids[new_id] = 1
nameID = new_id
if RenameMode == 1 and ExtensionBaseObj.findMarker(nameID):
defs = ExtensionBaseObj.getDefinitions()
for obj in defs.iter():
if obj.get('id') == nameID:
defs.remove(obj)
# creates a new marker
marker_attribs = {inkex.addNS('stockid', 'inkscape'): nameID,
'orient': 'auto', 'refY': '0.0', 'refX': '0.0',
'id': nameID,
'style': 'overflow:visible'}
newMarker = inkex.etree.SubElement(ExtensionBaseObj.getDefinitions(), inkex.addNS('marker', 'defs'), marker_attribs)
if not fillColor:
fillColor = 'none'
if not strokeColor:
strokeColor = 'none'
marker_style = {'fill-rule': 'evenodd', 'fill': fillColor,
'stroke': strokeColor, 'stroke-width': str(lineWidth)}
marker_lineline_attribs = {'d': markerPath, 'style': simplestyle.formatStyle(marker_style)}
if markerTransform:
marker_lineline_attribs['transform'] = markerTransform
inkex.etree.SubElement(newMarker, inkex.addNS('path', 'defs'), marker_lineline_attribs)
ExtensionBaseObj.doc_ids[nameID] = 1
# print tostring(ExtensionBaseObj.getDefinitions())
return nameID
#---------------------------------------------
@staticmethod
def createDotMarker(ExtensionBaseObj, nameID, RenameMode=0, scale=0.4, strokeColor=color.defined('black'), fillColor=color.defined('black')):
"""Creates a dotS/M/L marker, exactly like inkscape default markers
:param ExtensionBaseObj: Most of the times you have to use 'self' from inkscapeMadeEasy related objects
:param nameID: nameID of the marker
:param RenameMode: Renaming behavior mode. For more information, see documentation of marker.createMarker(...) method.
- 0: (default) do not rename the marker. If nameID is already taken, the marker will not be modified
- 1: overwrite marker if nameID is already taken
- 2: Create a new unique nameID, adding a suffix number (Please refer to inkscapeMadeEasy.uniqueIdNumber(prefix_id) ).
:param scale: scale of the marker. To copy exactly inkscape sizes dotS/M/L, use 0.2, 0.4 and 0.8 respectively. Default: 0.4
:param strokeColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:param fillColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:type ExtensionBaseObj: inkscapeMadeEasy object (see example below)
:type nameID: string
:type RenameMode: int
:type scale: float
:type strokeColor: string
:type fillColor: string
:returns: NameID of the new marker
:rtype: string
**Example**
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> ...
>>> ...
>>>
>>> def effect(self):
>>> myMarker=inkDraw.marker.createDotMarker(self,nameID='myDotMarkerA',RenameMode=1,scale=0.5,strokeColor=inkDraw.color.defined('red'),fillColor=None)
>>> myLineStyle = inkDraw.lineStyle.set(1.0, markerEnd=myMarker,lineColor=inkDraw.color.defined('black')) # see lineStyle class for further information on this function
"""
markerPath = 'M -2.5,-1.0 C -2.5,1.7600000 -4.7400000,4.0 -7.5,4.0 C -10.260000,4.0 -12.5,1.7600000 -12.5,-1.0 C -12.5,-3.7600000 -10.260000,-6.0 -7.5,-6.0 C -4.7400000,-6.0 -2.5,-3.7600000 -2.5,-1.0 z '
width = 1.0
markerTransform = 'scale(' + str(scale) + ') translate(7.4, 1)'
return marker.createMarker(ExtensionBaseObj, nameID, markerPath, RenameMode, strokeColor, fillColor, width, markerTransform)
#---------------------------------------------
@staticmethod
def createCrossMarker(ExtensionBaseObj, nameID, RenameMode=0, scale=0.4, strokeColor=color.defined('black'), fillColor=color.defined('black')):
"""Creates a cross marker
:param ExtensionBaseObj: Most of the times you have to use 'self' from inkscapeMadeEasy related objects
:param nameID: nameID of the marker
:param RenameMode: Renaming behavior mode. For more information, see documentation of marker.createMarker(...) method.
- 0: (default) do not rename the marker. If nameID is already taken, the marker will not be modified
- 1: overwrite marker if nameID is already taken
- 2: Create a new unique nameID, adding a suffix number (Please refer to inkscapeMadeEasy.uniqueIdNumber(prefix_id) ).
:param scale: scale of the marker. Default: 0.4
:param strokeColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:param fillColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:type ExtensionBaseObj: inkscapeMadeEasy object (see example below)
:type nameID: string
:type RenameMode: int
:type scale: float
:type strokeColor: string
:type fillColor: string
:returns: NameID of the new marker
:rtype: string
**Example**
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> ...
>>> ...
>>>
>>> def effect(self):
>>> myMarker=inkDraw.marker.createCrossMarker(self,nameID='myDotMarkerA',RenameMode=1,scale=0.5,strokeColor=inkDraw.color.defined('red'),fillColor=None)
>>> myLineStyle = inkDraw.lineStyle.set(1.0, markerEnd=myMarker,lineColor=inkDraw.color.defined('black')) # see lineStyle class for further information on this function
"""
markerPath = 'M -5,5 L 5,-5 M 5,5 L -5,-5'
markerTransform = 'scale(' + str(scale) + ')'
width = 1.0
return marker.createMarker(ExtensionBaseObj, nameID, markerPath, RenameMode, strokeColor, fillColor, width, markerTransform)
#---------------------------------------------
@staticmethod
def createArrow1Marker(ExtensionBaseObj, nameID, RenameMode=0, scale=0.4, strokeColor=color.defined('black'), fillColor=color.defined('black')):
"""Creates a arrowS/M/L arrow markers (both start and end markers), exactly like inkscape
:param ExtensionBaseObj: Most of the times you have to use 'self' from inkscapeMadeEasy related objects
:param nameID: nameID of the marker. Start and End markers will have 'Start' and 'End' suffix respectively
:param RenameMode: Renaming behavior mode. For more information, see documentation of marker.createMarker(...) method.
- 0: (default) do not rename the marker. If nameID is already taken, the marker will not be modified
- 1: overwrite marker if nameID is already taken
- 2: Create a new unique nameID, adding a suffix number (Please refer to inkscapeMadeEasy.uniqueIdNumber(prefix_id) ).
:param scale: scale of the marker. Default: 0.4
:param strokeColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:param fillColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:type ExtensionBaseObj: inkscapeMadeEasy object (see example below)
:type nameID: string
:type RenameMode: int
:type scale: float
:type strokeColor: string
:type fillColor: string
:returns: a list of strings: [startArrowMarker,endArrowMarker]
- startArrowMarker: nameID of start marker
- endArrowMarker: nameID of end marker
:rtype: list
**Example**
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> ...
>>> ...
>>>
>>> def effect(self):
>>> StartArrowMarker,EndArrowMarker=inkDraw.marker.createArrow1Marker(self,nameID='myArrow',RenameMode=1,scale=0.5,strokeColor=inkDraw.color.defined('red'),fillColor=None)
>>> myLineStyle = inkDraw.lineStyle.set(1.0, markerStart=StartArrowMarker,markerEnd=EndArrowMarker,lineColor='#000000') # see lineStyle class for further information on this function
"""
# transform="scale(0.8) rotate(180) translate(12.5,0)" />
# transform="scale(0.4) rotate(180) translate(10,0)" />
# transform="scale(0.2) rotate(180) translate(6,0)" />
# translation=12.5-17.5/(scale*10)
# linear regression from data of small medium and large
translation = 10.17 * scale + 4.75
width = 1.0
markerPath = 'M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z '
markerTransform = 'scale(' + str(scale) + ') rotate(0) translate(' + str(translation) + ',0)'
nameStart = marker.createMarker(ExtensionBaseObj, nameID + 'Start', markerPath, RenameMode, strokeColor, fillColor, width, markerTransform)
markerTransform = 'scale(' + str(scale) + ') rotate(180) translate(' + str(translation) + ',0)'
nameEnd = marker.createMarker(ExtensionBaseObj, nameID + 'End', markerPath, RenameMode, strokeColor, fillColor, width, markerTransform)
return [nameStart, nameEnd]
#---------------------------------------------
@staticmethod
def createInfLineMarker(ExtensionBaseObj, nameID, RenameMode=0, scale=1.0, strokeColor=None, fillColor=color.defined('black')):
"""Creates ellipsis markers, both start and end markers.
These markers differ from inkscape's default ellipsis since these markers are made such that the diameter of the dots are equal to the line width.
:param ExtensionBaseObj: Most of the times you have to use 'self' from inkscapeMadeEasy related objects
:param nameID: nameID of the marker. Start and End markers will have 'Start' and 'End' suffix respectively
:param RenameMode: Renaming behavior mode. For more information, see documentation of marker.createMarker(...) method.
- 0: (default) do not rename the marker. If nameID is already taken, the marker will not be modified
- 1: overwrite marker if nameID is already taken
- 2: Create a new unique nameID, adding a suffix number (Please refer to inkscapeMadeEasy.uniqueIdNumber(prefix_id) ).
:param scale: scale of the marker. Default 1.0
:param strokeColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: ``None``
:param fillColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:type ExtensionBaseObj: inkscapeMadeEasy object (see example below)
:type nameID: string
:type RenameMode: int
:type scale: float
:type strokeColor: string
:type fillColor: string
:returns: a list of strings: [startInfMarker,endInfMarker]
- startInfMarker: nameID of start marker
- endInfMarker: nameID of end marker
:rtype: list
**Example**
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> ...
>>> ...
>>>
>>> def effect(self):
>>> startInfMarker,endInfMarker=inkDraw.marker.createInfLineMarker(self,nameID='myInfMarker',RenameMode=1,scale=1.0,strokeColor=None,fillColor='#00FF00')
>>> myLineStyle = inkDraw.lineStyle.set(1.0, markerStart=startInfMarker,markerEnd=endInfMarker,lineColor='#000000') # see lineStyle class for further information on this function
"""
# build path for 3 circles
markerPath = ''
radius = scale / 2.0
for i in range(3):
prefix = 'M %f %f ' % (i * 2 + radius, 0)
arcStringA = 'a %f %f 0 1 1 %f %f ' % (radius, radius, -2 * radius, 0)
arcStringB = 'a %f %f 0 1 1 %f %f ' % (radius, radius, 2 * radius, 0)
markerPath = markerPath + prefix + arcStringA + arcStringB + 'z '
if scale != 1.0:
markerTransform = 'translate(' + str(-6.0 * scale) + ', 0) scale(' + str(scale) + ')'
else:
markerTransform = 'translate(' + str(-6.0 * scale) + ', 0)'
width = 1.0
# add small line segment
nameStart = marker.createMarker(ExtensionBaseObj, nameID + 'Start', markerPath, RenameMode, strokeColor, fillColor, width, markerTransform)
if scale != 1.0:
markerTransform = 'translate(' + str(2.0 * scale) + ', 0) scale(' + str(scale) + ')'
else:
markerTransform = 'translate(' + str(2.0 * scale) + ', 0)'
nameEnd = marker.createMarker(ExtensionBaseObj, nameID + 'End', markerPath, RenameMode, strokeColor, fillColor, width, markerTransform)
return [nameStart, nameEnd]
class lineStyle():
"""
Class to create line styles.
This class is used to define line styles. It is capable of setting stroke and filling colors, line width, linejoin and linecap, markers (start, mid, and end) and stroke dash array
This class contains only static methods so that you don't have to inherit this in your class
"""
#---------------------------------------------
@staticmethod
def set(lineWidth=1.0, lineColor=color.defined('black'), fillColor=None, lineJoin='round',
lineCap='round', markerStart=None, markerMid=None, markerEnd=None, strokeDashArray=None):
""" Creates a new line style
:param lineWidth: line width. Default: 1.0
:param lineColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:param fillColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: ``None``
:param lineJoin: shape of the lines at the joints. Valid values 'miter', 'round', 'bevel'. Default: round
:param lineCap: shape of the lines at the ends. Valid values 'butt', 'square', 'round'. Default: round
:param markerStart: marker at the start node. Default: ``None``
:param markerMid: marker at the mid nodes. Default: ``None``
:param markerEnd: marker at the end node. Default: ``None``
:param strokeDashArray: dashed line pattern definition. Default: ``None`` See <http://www.w3schools.com/svg/svg_stroking.asp> for further information
:type lineWidth: float
:type lineColor: string
:type fillColor: string
:type lineJoin: string
:type lineCap: string
:type markerStart: string
:type markerMid: string
:type markerEnd: string
:type strokeDashArray: string
:returns: line definition following the provided specifications
:rtype: string
**Line node types**
.. image:: ../imagesDocs/line_nodes.png
:width: 600px
**Example**
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> ...
>>> ...
>>>
>>> def effect(self):
>>>
>>> # creates a line style using a dot marker at its end node
>>> myMarker=inkDraw.marker.createDotMarker(self,nameID='myMarker',RenameMode=1,scale=0.5,strokeColor=color.defined('red'),fillColor=None) # see marker class for further information on this function
>>> myLineStyle = inkDraw.lineStyle.set(lineWidth=1.0, markerEnd=myMarker,lineColor=inkDraw.color.defined('black'),fillColor=inkDraw.color('red'))
>>>
>>> # creates a line style with dashed line (5 units dash , 10 units space
>>> myDashedStyle = inkDraw.lineStyle.set(lineWidth=1.0,lineColor=inkDraw.color.defined('black'),fillColor=inkDraw.color,strokeDashArray='5,10')
>>> # creates a line style with a more complex pattern (5 units dash , 10 units space, 2 units dash, 3 units space
>>> myDashedStyle = inkDraw.lineStyle.set(lineWidth=1.0,lineColor=inkDraw.color.defined('black'),fillColor=inkDraw.color,strokeDashArray='5,10,2,3')
"""
if not fillColor:
fillColor = 'none'
if not lineColor:
lineColor = 'none'
if not strokeDashArray:
strokeDashArray = 'none'
# dictionary with the styles
lineStyle = {'stroke': lineColor,
'stroke-width': str(lineWidth),
'stroke-dasharray': strokeDashArray,
'fill': fillColor}
#Endpoint and junctions
lineStyle['stroke-linecap'] = lineCap
lineStyle['stroke-linejoin'] = lineJoin
# add markers if needed
if markerStart:
lineStyle['marker-start'] = 'url(#' + markerStart + ')'
if markerMid:
lineStyle['marker-mid'] = 'url(#' + markerMid + ')'
if markerEnd:
lineStyle['marker-end'] = 'url(#' + markerEnd + ')'
return lineStyle
#---------------------------------------------
@staticmethod
def setSimpleBlack(lineWidth=1.0):
"""Defines a standard black line style.
The only adjustable parameter is its width. The fixed parameters are: lineColor=black, fillColor=None, lineJoin='round', lineCap='round', no markers, no dash pattern
:param lineWidth: line width. Default: 1.0
:type lineWidth: float
:returns: line definition following the provided specifications
:rtype: string
**Example**
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> ...
>>> ...
>>>
>>> def effect(self):
>>>
>>> mySimpleStyle = inkDraw.lineStyle.setSimpleBlack(lineWidth=2.0)
"""
return lineStyle.set(lineWidth)
class textStyle():
"""
Class to create text styles.
This class is used to define text styles. It is capable of setting font size, justification, text color, font family, font style, font weight, line spacing, letter spacing and word spacing
This class contains only static methods so that you don't have to inherit this in your class
"""
#---------------------------------------------
@staticmethod
def set(fontSize=10, justification='left', textColor=color.defined('black'), fontFamily='Sans', fontStyle='normal', fontWeight='normal', lineSpacing='100%', letterSpacing='0px', wordSpacing='0px'):
"""Defines a new text style
:param fontSize: size of the font in px. Default: 10
:param justification: text justification. ``left``, ``right``, ``center``. Default: ``left``
:param textColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:param fontFamily: font family name. Default ``Sans``
:param fontStyle: ``normal`` or ``italic``. Default: ``normal``
:param fontWeight: ``normal`` or ``bold``. Default: ``normal``
:param lineSpacing: spacing between lines in percentage. Default: ``100%``
:param letterSpacing: extra space between letters. Format: ``_px``. Default: ``0px``
:param wordSpacing: extra space between words. Format: ``_px``. Default: ``0px``
:type fontSize: float
:type justification: string
:type textColor: string
:type fontFamily: string
:type fontStyle: string
:type fontWeight: string
:type lineSpacing: string
:type letterSpacing: string
:type wordSpacing: string
:returns: text style definition following the provided specifications
:rtype: string
.. Warning: This method does NOT verify whether the font family is installed in your machine or not.
**Example**
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> ...
>>> ...
>>>
>>> def effect(self):
>>>
>>> myTextStyle=inkDraw.textStyle.set(fontSize=10, justification='left', textColor=color.defined('black'), fontFamily='Sans', fontStyle='normal', fontWeight='normal', lineSpacing='100%', letterSpacing='0px', wordSpacing='0px')
"""
if not textColor:
textColor = 'none'
if justification == 'left':
justification = 'start'
anchor = 'start'
if justification == 'right':
justification = 'end'
anchor = 'end'
if justification == 'center':
anchor = 'middle'
textStyle = {'font-size': str(fontSize) + 'px',
'font-style': fontStyle,
'font-weight': fontWeight,
'text-align': justification, # start, center, end
'line-height': lineSpacing,
'letter-spacing': letterSpacing,
'word-spacing': wordSpacing,
'text-anchor': anchor, # start, middle, end
'fill': textColor,
'fill-opacity': '1',
'stroke': 'none',
'font-family': fontFamily}
return textStyle
#---------------------------------------------
@staticmethod
def setSimpleBlack(fontSize=10, justification='left'):
"""Defines a standard black text style
The only adjustable parameter are font size and justification. The fixed parameters are: textColor=color.defined('black'), fontFamily='Sans', fontStyle='normal', fontWeight='normal', lineSpacing='100%', letterSpacing='0px', wordSpacing='0px.
:param fontSize: size of the font in px. Default: 10
:param justification: text justification. ``left``, ``right``, ``center``. Default: ``left``
:type fontSize: float
:type justification: string
:returns: line definition following the provided specifications
:rtype: string
**Example**
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> ...
>>> ...
>>>
>>> def effect(self):
>>>
>>> mySimpleStyle = inkDraw.textStyle.setSimpleBlack(fontSize=20,justification='center')
"""
return textStyle.set(fontSize, justification)
#---------------------------------------------
@staticmethod
def setSimpleColor(fontSize=10, justification='left', textColor=color.defined('black')):
"""Defines a standard colored text style
The only adjustable parameter are font size justification and textColor. The fixed parameters are: fontFamily='Sans', fontStyle='normal', fontWeight='normal', lineSpacing='100%', letterSpacing='0px', wordSpacing='0px.
:param fontSize: size of the font in px. Default: 10
:param justification: text justification. ``left``, ``right``, ``center``. Default: ``left``
:param textColor: color in the format ``#RRGGBB`` (hexadecimal), or ``None`` for no color. Default: color.defined('black')
:type fontSize: float
:type justification: string
:type textColor: string
:returns: line definition following the provided specifications
:rtype: string
**Example**
>>> import inkex
>>> import inkscapeMadeEasy_Base as inkBase
>>> import inkscapeMadeEasy_Draw as inkDraw
>>>
>>> class myExtension(inkBase.inkscapeMadeEasy):
>>> def __init__(self):
>>> ...