Debian Codegraph @

Nasal/canvas/api/text.nas

raw
1 #-------------------------------------------------------------------------------
2 # canvas.Text
3 #-------------------------------------------------------------------------------
4 # Class for a text element on a canvas
5 #
6 var font_mapper = func(family = nil, weight = nil, style = nil, options = nil)
7 {
8 var defaults = {
9 "default-font-family": "LiberationSans",
10 "default-font-weight": "",
11 "default-font-style": "",
12 };
13
14 # setup defaults if no options are given
15 if (options == nil) {
16 options = defaults;
17 }
18 if (!ishash(options)) {
19 logprint(LOG_ALERT, "font_mapper: options must be a hash!")
20 }
21
22 # use defaults for missing arguments
23 if (family == nil) {
24 family = options["default-font-family"] or defaults["default-font-family"];
25 }
26 if (weight == nil) {
27 weight = options["default-font-weight"] or defaults["default-font-weight"];
28 }
29 if (style == nil) {
30 style = options["default-font-style"] or defaults["default-font-style"];
31 }
32
33 if (isfunc(options["font-mapper"])) {
34 var font = options["font-mapper"](family, weight, style);
35 if (font != nil) {
36 return font;
37 }
38 }
39
40 # Remove '' that Inkscape puts around font names containing spaces
41 if (left(family, 1) == "'") family = substr(family, 1);
42 if (right(family, 1) == "'") family = substr(family, 0, size(family) - 1);
43
44 # map generic Inkscape sans serif to our default font
45 if (string.lc(family) == "sans" or string.lc(family) == "sans-serif") {
46 family = "LiberationSans";
47 }
48 if (left(family, 10) == "Liberation") {
49 style = style == "italic" ? "Italic" : "";
50 weight = weight == "bold" ? "Bold" : "";
51
52 var s = weight~style;
53 if (s == "") s = "Regular";
54
55 return "LiberationFonts/"~string.replace(family, " ", "")~"-"~s~".ttf";
56 }
57
58 return "LiberationFonts/LiberationMono-Bold.ttf";
59 };
60
61 var Text = {
62 new: func(ghost) {
63 var obj = {
64 parents: [Text, Element.new(ghost)],
65 };
66 return obj;
67 },
68
69 # Set the text
70 setText: func(text) {
71 me.set("text", typeof(text) == "scalar" ? text : "");
72 },
73
74 getText: func() {
75 return me.get("text");
76 },
77
78 # enable reduced property I/O update function
79 enableUpdate: func () {
80 me._lasttext = "INIT_BLANK";
81 me.updateText = func (text)
82 {
83 if (text == me._lasttext) {return;}
84 me._lasttext = text;
85 me.set("text", typeof(text) == "scalar" ? text : "");
86 };
87 },
88
89 # reduced property I/O text update template
90 updateText: func (text) {
91 die("updateText() requires enableUpdate() to be called first");
92 },
93
94
95 # enable fast setprop-based text writing
96 enableFast: func () {
97 me._node_path = me._node.getPath()~"/text";
98 me.setTextFast = func(text) {
99 setprop(me._node_path, text);
100 };
101 },
102
103 # fast, setprop-based text writing template
104 setTextFast: func (text) {
105 die("setTextFast() requires enableFast() to be called first");
106 },
107
108 # append text to an existing string
109 appendText: func(text) {
110 me.set("text", (me.get("text") or "")~(typeof(text) == "scalar" ? text : ""));
111 },
112
113 # Set alignment
114 #
115 # @param align String, one of:
116 # left-top
117 # left-center
118 # left-bottom
119 # center-top
120 # center-center
121 # center-bottom
122 # right-top
123 # right-center
124 # right-bottom
125 # left-baseline
126 # center-baseline
127 # right-baseline
128 # left-bottom-baseline
129 # center-bottom-baseline
130 # right-bottom-baseline
131 #
132 setAlignment: func(align) {
133 me.set("alignment", align);
134 },
135
136 # Set the font size
137 setFontSize: func(size, aspect = 1) {
138 me.setDouble("character-size", size);
139 me.setDouble("character-aspect-ratio", aspect);
140 },
141
142 # Set font (by name of font file)
143 setFont: func(name) {
144 me.set("font", name);
145 },
146
147 # Enumeration of values for drawing mode:
148 TEXT: 0x01, # The text itself
149 BOUNDINGBOX: 0x02, # A bounding box (only lines)
150 FILLEDBOUNDINGBOX: 0x04, # A filled bounding box
151 ALIGNMENT: 0x08, # Draw a marker (cross) at the position of the text
152 # Set draw mode. Binary combination of the values above. Since I have not
153 # found a bitwise "or" we have to use a "+" instead.
154 # e.g. my_text.setDrawMode(Text.TEXT + Text.BOUNDINGBOX);
155 setDrawMode: func(mode) {
156 me.setInt("draw-mode", mode);
157 },
158
159 # Set bounding box padding
160 setPadding: func(pad) {
161 me.setDouble("padding", pad);
162 },
163
164 setMaxWidth: func(w) {
165 me.setDouble("max-width", w);
166 },
167
168 setColor: func {
169 me.set("fill", _getColor(arg));
170 },
171
172 getColor: func {
173 me.get("fill");
174 },
175
176 setColorFill: func {
177 me.set("background", _getColor(arg));
178 },
179
180 getColorFill: func {
181 me.get("background");
182 },
183 };