-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpattern_MakeStyler.py
273 lines (232 loc) · 8.15 KB
/
pattern_MakeStyler.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
# pattern_MakeStyler.py
import progSpec
'''
These styler fields are always available:
our Styler: styler
# The ISO color names. E.g.: styler.Black
styler.frGndColor
styler.bkGndColor
styler.highlight1Color
styler.highlight2Color
styler.highlight3Color
styler.setCustomColor(me string: ID, me cdColor: C)
styler.color(me string: ID, me cdColor: default)
styler.titleFontname
styler.normalFontname
styler.H1_fontname
styler.H2_fontname
styler.H3_fontname
styler.H4_fontname
styler.H5_fontname
styler.H6_fontname
styler.timesFontname
styler.sansSerifFontname
styler.comicFontname
styler.scriptFontname
styler.monoFontname
styler.setCustomFont(me string: ID, me string: fontName)
styler.font(me string: ID)
styler.fontSizeVerySmall
styler.fontSizeSmall
styler.fontSizeNormalSize
styler.fontSizeLarge
styler.fontSizeVeryLarge
styler.setCustomFontSize(me string: ID, me string: fontSize)
styler.fontSize(me string: ID)
==========================
New style items can be added as well as the above items overridden by using tags.
Examples:
mainStyle = {
colors = {
frGndColor = sysDefaultFrGndColor
//bkGndColor = [0, 0, 0] // RGB
highlight1Color = LightGreen
MySpecialColor = SlateBlue
}
fontNames = {
titleFontname = 'Ariel Bold'
normalFontname = 'Ariel'
H1_font = sysDefaultH1_fontname
LogoFont = 'CompanyFont'
}
fontSizes = {
styler.fontSizeVerySmall = sysDefaultFontSizeVerySmall
styler.fontSizeSmall = ":pp8"
styler.fontSizeLarge = ":sp20"
styler.LogoFontSize = ":dp18"
}
implDetails = {
}
}
NOTES:
1. If one of the always available fields is not given in the tags, its "sysDefaultxxxxx" value is given.
This value is platform dependent if the platform as a value, otherwise the value is a standard CodeDog value.
2. ISO color names are constants and cannot be changed. But the others are variables and can be assigned.
'''
import progSpec
import codeDogParser
def stringifyList(theList):
S=''
count=0
for item in theList:
if not(isinstance(item, str)):cdErr("List item not basesring")
if count >0: S = S+', '
S = S + item
count = count +1
return S
def processStylerMap(stylerTags, varOwner, varType, setFunc, defaultVars):
S=''
for varName in stylerTags:
varValue = stylerTags[varName]
if isinstance(varValue, str):
RHS=' <- '+varValue
elif isinstance(varValue, list):
RHS=stringifyList(varValue)
RHS=' <- '+varType+'('+RHS+')'
else: cdErr("UNKNOWN RHS type: "+varName)
if varName in defaultVars:
S = S+' ' + varName + RHS +'\n'
else:
S = S+' '+setFunc+'("'+ varName +'", '+ varValue +')\n'
return S
def processStyler(stylerTagValue):
varOwner = ''
varType = ''
S = ''
for tag in stylerTagValue:
if tag =="colors":
varOwner = 'me'
varType = 'cdColor'
setFunc = 'setCustomColor'
defaultVars = ['frGndColor', 'bkGndColor', 'highlight1Color', 'highlight2Color', 'highlight3Color']
elif tag =="fontNames":
varOwner = 'me'
varType = 'string'
setFunc = 'setCustomFontname'
defaultVars = ['titleFontname', 'normalFontname', 'H1_fontname', 'H2_fontname', 'H3_fontname', 'H4_fontname', 'H5_fontname', 'H6_fontname', 'timesFontname', 'sansSerifFontname', 'comicFontname', 'scriptFontname', 'monoFontname']
elif tag =="fontSizes":
varOwner = 'me'
varType = 'string'
setFunc = 'setCustomFontSize'
defaultVars = ['verySmallFontSize', 'smallFontSize', 'normalFontSize', 'largeFontSize', 'veryLargeFontSize']
elif tag =="strs":
varOwner = 'me'
varType = 'string'
setFunc = 'setCustomString'
defaultVars = []
elif tag =="nums":
varOwner = 'me'
varType = 'int'
setFunc = 'setCustomInteger'
defaultVars = []
elif tag =="doubles":
varOwner = 'me'
varType = 'double'
setFunc = 'setCustomDouble'
defaultVars = []
else:
print(' tag not found')
if isinstance(stylerTagValue[tag], dict):
S = S + processStylerMap(stylerTagValue[tag], varOwner, varType, setFunc, defaultVars)
elif isinstance(stylerTagValue[tag], str):
S = S + ' ' + varType + RHS +'\n'
else: print("!!!!!!!!!!!!!!!!!!styler not map or basestring", stylerTagValue[tag])
return S
def apply(classes, tags, stylerTagName):
if not(isinstance(stylerTagName,str)):
cdErr("Styler tag name must be a string")
stylerTagValue = progSpec.fetchTagValue(tags, stylerTagName)
initCode = processStyler(stylerTagValue)
code = r"""
struct GLOBAL{
our Styler:: styler
}
struct Styler{
me Map<me string, our cdColor>: userColors
me cdColor: frGndColor <- Black
me cdColor: bkGndColor <- White
me cdColor: highlight1Color <- Black
me cdColor: highlight2Color <- Cornflower
me cdColor: highlight3Color <- OrangeRed
me cdColor: primaryTextColor <- Black
me cdColor: data1Color <- Cornflower
me cdColor: data2Color <- Turquoise
me cdColor: data3Color <- Magenta
me cdColor: data4Color <- MediumVioletRed
me cdColor: data5Color <- OrangeRed
me cdColor: data6Color <- Gold
//TODO: Why don't these constructor inits work?
our fontSpec:: defaultFont{"Ariel", 10, 0}
our fontSpec:: titleFont{"Ariel", 16, 0}
our fontSpec:: smallFont{"Ariel", 8, 0}
our fontSpec:: verySmallFont{"Ariel", 5, 0}
void: setCustomColor(me string: ID, me cdColor: color) <- {
our cdColor:: tmpColor <- color
userColors.insert(ID, tmpColor)
}
me cdColor: color(me string: ID, me cdColor: defaultColor) <- {
itr Map<me string, our cdColor>: colorItr <- userColors.find(ID)
if(colorItr != userColors.end()){return(colorItr.val)}
return(defaultColor)
}
// FONT NAMES
me Map<me string, me string>: userFontNames
me string: titleFontname
me string: normalFontname
me string: H1_fontname
me string: H2_fontname
me string: H3_fontname
me string: H4_fontname
me string: H5_fontname
me string: H6_fontname
me string: timesFontname
me string: sansSerifFontname
me string: comicFontname
me string: scriptFontname
me string: monoFontname
void: setCustomFont(me string: ID, me string: fontName) <- {
userFontNames.insert(ID, fontName)
}
me string: font(me string: ID) <- {return(userFontNames.at(ID))}
// FONT SIZES
me Map<me string, me int>: userFontSizes
me int: verySmallFontSize
me int: smallFontSize
me int: normalSizeFontSize
me int: largeFontSize
me int: veryLargeFontSize
void: setCustomFontSize(me string: ID, me int: fontSize) <- {
userFontSizes.insert(ID, fontSize)
}
me int: fontSize(me string: ID) <- {
return(userFontSizes.at(ID))
}
// FONT SIZE MODES
me mode[pp, dp, sp]: pixelMode <- pp
me int: widgetLabelBoxWidth <- 100
me int: widgetValueBoxWidth <- 100
// OTHER KEY/VALUES
me Map<me string, me string>: userStrings
void: setCustomString(me string: key, me string: value) <- {
userStrings.insert(key, value)
}
me Map<me string, me int>: userIntegers
void: setCustomInteger(me string: key, me string: value) <- {
// userIntegers.insert(key, (value))
}
me Map<me string, me int>: userDoubles
void: setCustomDouble(me string: key, me string: value) <- {
// userIntegers.insert(key, (value))
}
void: INIT()<-{
<INITCODE>
Allocate(defaultFont, "ariel", "14")
Allocate(titleFont, "ariel", "20")
Allocate(smallFont, "ariel", "10")
Allocate(verySmallFont, "ariel", "5")
}
}
"""
code = code.replace('<INITCODE>', initCode)
#print '==========================================================\n'+code
codeDogParser.AddToObjectFromText(classes[0], classes[1], code , 'Pattern: MakeStyler')