Blender V4.3
text_format_pov.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cstring>
10
11#include "BLI_blenlib.h"
12
13#include "DNA_space_types.h"
14#include "DNA_text_types.h"
15
16#include "BKE_text.h"
17
18#include "text_format.hh"
19
20/* -------------------------------------------------------------------- */
26 /* Force single column, sorted list. */
27 /* clang-format off */
28 "append",
29 "break",
30 "case",
31 "debug",
32 "declare",
33 "default",
34 "deprecated",
35 "else",
36 "elseif",
37 "end",
38 "error",
39 "fclose",
40 "fopen",
41 "for",
42 "if",
43 "ifdef",
44 "ifndef",
45 "include",
46 "local",
47 "macro",
48 "patch",
49 "persistent",
50 "range",
51 "read",
52 "render",
53 "statistics",
54 "switch",
55 "undef",
56 "version",
57 "warning",
58 "while",
59 "write",
60 /* clang-format on */
61};
64
65/* POV-Ray Built-in Variables
66 * list is from...
67 * http://www.povray.org/documentation/view/3.7.0/212/
68 */
69
72 /* Force single column, sorted list. */
73 /* clang-format on */
74 "SRGB",
75 "abs",
76 "acos",
77 "acosh",
78 "albedo",
79 "altitude",
80 "angle",
81 "asc",
82 "asin",
83 "asinh",
84 "atan",
85 "atan2",
86 "atand",
87 "atanh",
88 "bitwise_and",
89 "bitwise_or",
90 "bitwise_xor",
91 "blink",
92 "blue",
93 "ceil",
94 "child",
95 "chr",
96 "clipped_by",
97 "collect",
98 "concat",
99 "conserve_energy",
100 "cos",
101 "cosh",
102 "crand",
103 "datetime",
104 "defined",
105 "degrees",
106 "dimension_size",
107 "dimensions",
108 "direction",
109 "div",
110 "evaluate",
111 "exp",
112 "file_exists",
113 "file_time",
114 "filter",
115 "floor",
116 "form",
117 "function",
118 "gamma",
119 "gray",
120 "green",
121 "gts_load",
122 "gts_save",
123 "hsl",
124 "hsv",
125 "inside",
126 "int",
127 "inverse",
128 "jitter",
129 "ln",
130 "load_file",
131 "location",
132 "log",
133 "look_at",
134 "matrix",
135 "max",
136 "max_extent",
137 "max_intersections",
138 "max_trace",
139 "metallic",
140 "min",
141 "min_extent",
142 "mod",
143 "phong_size",
144 "pov",
145 "pow",
146 "precompute",
147 "prod",
148 "pwr",
149 "quaternion",
150 "radians",
151 "rand",
152 "reciprocal",
153 "red",
154 "rgb",
155 "rgbf",
156 "rgbft",
157 "rgbt",
158 "right",
159 "rotate",
160 "roughness",
161 "sRGB",
162 "save_file",
163 "scale",
164 "seed",
165 "select",
166 "shadowless",
167 "sin",
168 "sinh",
169 "sky",
170 "sqr",
171 "sqrt",
172 "srgb",
173 "srgbf",
174 "srgbft",
175 "srgbt",
176 "str",
177 "strcmp",
178 "strlen",
179 "strlwr",
180 "strupr",
181 "sturm",
182 "substr",
183 "sum",
184 "tan",
185 "tanh",
186 "target",
187 "tessel",
188 "tesselate",
189 "trace",
190 "transform",
191 "translate",
192 "transmit",
193 "turb_depth",
194 "up",
195 "val",
196 "vaxis_rotate",
197 "vcross",
198 "vdot",
199 "vlength",
200 "vnormalize",
201 "vrotate",
202 "vstr",
203 "vturbulence",
204 "warp",
205 "with",
206 "xyl",
207 "xyv",
208 /* clang-format on */
209};
212
213/* POV-Ray Built-in Variables
214 * list is from...
215 * http://www.povray.org/documentation/view/3.7.0/212/
216 */
217
218/* Language Keywords */
220 /* Force single column, sorted list. */
221 /* clang-format off */
222 "aa_threshold",
223 "absorption",
224 "agate",
225 "akima_spline",
226 "all",
227 "all_intersections",
228 "alpha",
229 "ambient",
230 "aoi",
231 "arc_angle",
232 "area_illumination",
233 "array",
234 "average",
235 "b_spline",
236 "background",
237 "basic_x_spline",
238 "bend",
239 "bezier_spline",
240 "bicubic_patch",
241 "binary",
242 "black_hole",
243 "blob",
244 "box",
245 "boxed",
246 "bozo",
247 "brick",
248 "brilliance",
249 "bump_map",
250 "bumps",
251 "camera",
252 "cells",
253 "checker",
254 "clock",
255 "clock_delta",
256 "clock_on",
257 "color",
258 "color_space",
259 "colour",
260 "colour_space",
261 "component",
262 "composite",
263 "cone",
264 "conic_sweep",
265 "coords",
266 "crackle",
267 "cube",
268 "cubic",
269 "cubic",
270 "cubic_spline",
271 "cubic_spline",
272 "cutaway_textures",
273 "cylinder",
274 "cylindrical",
275 "density_file",
276 "dents",
277 "difference",
278 "diffuse",
279 "disc",
280 "displace",
281 "dist_exp",
282 "emission",
283 "extended_x_spline",
284 "exterior",
285 "facets",
286 "falloff_angle",
287 "file_gamma",
288 "final_clock",
289 "final_frame",
290 "flatness",
291 "flip",
292 "fog",
293 "frame_number",
294 "galley",
295 "general_x_spline",
296 "global_settings",
297 "gradient",
298 "granite",
299 "height_field",
300 "hexagon",
301 "hierarchy",
302 "hypercomplex",
303 "image_height",
304 "image_map",
305 "image_pattern",
306 "image_width",
307 "initial_clock",
308 "initial_frame",
309 "input_file_name",
310 "interior",
311 "intermerge",
312 "internal",
313 "intersection",
314 "interunion",
315 "irid",
316 "iridescence",
317 "isosurface",
318 "julia",
319 "julia_fractal",
320 "keep",
321 "lathe",
322 "lemon",
323 "leopard",
324 "light_group",
325 "light_source",
326 "linear_spline",
327 "linear_sweep",
328 "lommel_seeliger",
329 "look_at",
330 "magnet",
331 "major_radius",
332 "mandel",
333 "marble",
334 "masonry",
335 "material",
336 "max_distance",
337 "max_extent",
338 "max_iteration",
339 "media",
340 "merge",
341 "mesh",
342 "mesh2",
343 "metric",
344 "minnaert",
345 "move",
346 "natural_spline",
347 "now",
348 "object",
349 "offset",
350 "onion",
351 "oren_nayar",
352 "orientation",
353 "ovus",
354 "parametric",
355 "pattern",
356 "pavement",
357 "phong",
358 "photons",
359 "pigment_pattern",
360 "planar",
361 "plane",
362 "planet",
363 "poly",
364 "polygon",
365 "polynomial",
366 "pot",
367 "precision",
368 "prism",
369 "proportion",
370 "proximity",
371 "quadratic_spline",
372 "quadric",
373 "quartic",
374 "quilted",
375 "radial",
376 "radiosity",
377 "rainbow",
378 "reflection",
379 "reflection_exponent",
380 "refraction",
381 "repeat",
382 "ripples",
383 "roll",
384 "scattering",
385 "screw",
386 "size",
387 "sky_sphere",
388 "slice",
389 "slope",
390 "smooth",
391 "smooth_triangle",
392 "solid",
393 "sor",
394 "sor_spline",
395 "specular",
396 "sphere",
397 "sphere_sweep",
398 "spherical",
399 "spiral1",
400 "spiral2",
401 "spline",
402 "spotted",
403 "square",
404 "subsurface",
405 "superellipsoid",
406 "t",
407 "tcb_spline",
408 "text",
409 "texture",
410 "tile2",
411 "tiles",
412 "tiling",
413 "tolerance",
414 "toroidal",
415 "torus",
416 "triangle",
417 "triangular",
418 "type",
419 "u",
420 "union",
421 "v",
422 "voronoi",
423 "water_level",
424 "waves",
425 "width",
426 "wood",
427 "wrinkles",
428 "x",
429 "y",
430 "z",
431 /* clang-format on */
432};
435
442 /* Force single column, sorted list. */
443 /* clang-format off */
444 "aa_level",
445 "accuracy",
446 "accuracy",
447 "adaptive",
448 "adc_bailout",
449 "agate_turb",
450 "aitoff_hammer",
451 "albinos",
452 "always_sample",
453 "ambient_light",
454 "amount",
455 "aperture",
456 "area_light",
457 "assumed_gamma",
458 "autostop",
459 "balthasart",
460 "behrmann",
461 "blur_samples",
462 "bounded_by",
463 "brick_size",
464 "brightness",
465 "bump_size",
466 "camera_direction",
467 "camera_location",
468 "camera_right",
469 "camera_type",
470 "camera_up",
471 "caustics",
472 "charset",
473 "circular",
474 "color_map",
475 "colour_map",
476 "confidence",
477 "contained_by",
478 "control0",
479 "control1",
480 "count",
481 "cubic",
482 "cubic_wave",
483 "density",
484 "density_map",
485 "dispersion",
486 "dispersion_samples",
487 "distance",
488 "double_illuminate",
489 "eccentricity",
490 "eckert_iv",
491 "eckert_vi",
492 "edwards",
493 "error_bound",
494 "expand_thresholds",
495 "exponent",
496 "extinction",
497 "face_indices",
498 "fade_color",
499 "fade_colour",
500 "fade_distance",
501 "fade_power",
502 "fade_power",
503 "falloff",
504 "finish",
505 "fisheye",
506 "fixed",
507 "focal_point",
508 "fog_alt",
509 "fog_offset",
510 "fog_type",
511 "frequency",
512 "fresnel",
513 "gall",
514 "gather",
515 "global_lights",
516 "gray_threshold",
517 "hf_gray_16",
518 "hobo_dyer",
519 "hollow",
520 "icosa",
521 "importance",
522 "inbound",
523 "inner",
524 "inside_point",
525 "inside_vector",
526 "interior_texture",
527 "interpolate",
528 "intervals",
529 "ior",
530 "irid_wavelength",
531 "lambda",
532 "lambert_azimuthal",
533 "lambert_cylindrical",
534 "looks_like",
535 "low_error_factor",
536 "map_type",
537 "material_map",
538 "max_gradient",
539 "max_sample",
540 "max_trace_level",
541 "maximal",
542 "maximum_reuse",
543 "media_attenuation",
544 "media_interaction",
545 "mercator",
546 "mesh_camera",
547 "method",
548 "miller_cylindrical",
549 "minimal",
550 "minimum_reuse",
551 "mm_per_unit",
552 "modulation",
553 "mollweide",
554 "mortar",
555 "nearest_count",
556 "no_bump_scale",
557 "no_cache",
558 "no_image",
559 "no_radiosity",
560 "no_reflection",
561 "no_shadow",
562 "noise_generator",
563 "normal",
564 "normal_indices",
565 "normal_map",
566 "normal_vectors",
567 "number_of_waves",
568 "octa",
569 "octaves",
570 "offset",
571 "omega",
572 "omni_directional_stereo",
573 "omnimax",
574 "once",
575 "open",
576 "orient",
577 "origin",
578 "original",
579 "orthographic",
580 "outbound",
581 "outside",
582 "panoramic",
583 "parallaxe",
584 "parallel",
585 "pass_through",
586 "perspective",
587 "peters",
588 "phase",
589 "pigment",
590 "pigment_map",
591 "plate_carree",
592 "point_at",
593 "polarity",
594 "poly_wave",
595 "precision",
596 "pretrace_end",
597 "pretrace_start",
598 "projected_through",
599 "quick_color",
600 "quick_colour",
601 "radius",
602 "ramp_wave",
603 "ratio",
604 "recursion_limit",
605 "samples",
606 "scallop_wave",
607 "sine_wave",
608 "slope_map",
609 "smyth_craster",
610 "spacing",
611 "split_union",
612 "spotlight",
613 "stereo",
614 "strength",
615 "tetra",
616 "texture_list",
617 "texture_map",
618 "thickness",
619 "threshold",
620 "tightness",
621 "translucency",
622 "triangle_wave",
623 "turbulence",
624 "u_steps",
625 "ultra_wide_angle",
626 "use_alpha",
627 "use_color",
628 "use_colour",
629 "use_index",
630 "uv_indices",
631 "uv_mapping",
632 "uv_vectors",
633 "v_steps",
634 "van_der_grinten",
635 "variance",
636 "vertex_vectors",
637 /* clang-format on */
638};
642
645 /* Force single column, sorted list. */
646 /* clang-format off */
647 "ascii",
648 "bt2020",
649 "bt709",
650 "df3",
651 "exr",
652 "false",
653 "gif",
654 "hdr",
655 "iff",
656 "jpeg",
657 "no",
658 "off",
659 "on",
660 "pgm",
661 "pi",
662 "png",
663 "ppm",
664 "sint16be",
665 "sint16le",
666 "sint32be",
667 "sint32le",
668 "sint8",
669 "sys",
670 "tau",
671 "tga",
672 "tiff",
673 "true",
674 "ttf",
675 "uint16be",
676 "uint16le",
677 "uint8",
678 "unofficial",
679 "utf8",
680 "yes",
681 /* clang-format on */
682};
685
688/* -------------------------------------------------------------------- */
698static int txtfmt_pov_find_keyword(const char *string)
699{
700
702
703 /* If next source char is an identifier (eg. 'i' in "definite") no match */
704 return (i == 0 || text_check_identifier(string[i])) ? -1 : i;
705}
706
707static int txtfmt_pov_find_reserved_keywords(const char *string)
708{
710
711 /* If next source char is an identifier (eg. 'i' in "definite") no match */
712 return (i == 0 || text_check_identifier(string[i])) ? -1 : i;
713}
714
715static int txtfmt_pov_find_reserved_builtins(const char *string)
716{
718
719 /* If next source char is an identifier (eg. 'i' in "definite") no match */
720 return (i == 0 || text_check_identifier(string[i])) ? -1 : i;
721}
722
723static int txtfmt_pov_find_specialvar(const char *string)
724{
726
727 /* If next source char is an identifier (eg. 'i' in "definite") no match */
728 return (i == 0 || text_check_identifier(string[i])) ? -1 : i;
729}
730
731static int txtfmt_pov_find_bool(const char *string)
732{
734
735 /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */
736 return (i == 0 || text_check_identifier(string[i])) ? -1 : i;
737}
738
739static char txtfmt_pov_format_identifier(const char *str)
740{
741 char fmt;
742
743 /* Keep aligned args for readability. */
744 /* clang-format off */
745
747 } else if (txtfmt_pov_find_keyword(str) != -1) { fmt = FMT_TYPE_KEYWORD;
750 } else { fmt = FMT_TYPE_DEFAULT;
751 }
752
753 /* clang-format on */
754
755 return fmt;
756}
757
760/* -------------------------------------------------------------------- */
764static void txtfmt_pov_format_line(SpaceText *st, TextLine *line, const bool do_next)
765{
766 FlattenString fs;
767 const char *str;
768 char *fmt;
769 char cont_orig, cont, find, prev = ' ';
770 int len, i;
771
772 /* Get continuation from previous line */
773 if (line->prev && line->prev->format != nullptr) {
774 fmt = line->prev->format;
775 cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */
776 BLI_assert((FMT_CONT_ALL & cont) == cont);
777 }
778 else {
779 cont = FMT_CONT_NOP;
780 }
781
782 /* Get original continuation from this line */
783 if (line->format != nullptr) {
784 fmt = line->format;
785 cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */
786 BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig);
787 }
788 else {
789 cont_orig = 0xFF;
790 }
791
792 len = flatten_string(st, &fs, line->line);
793 str = fs.buf;
794 if (!text_check_format_len(line, len)) {
796 return;
797 }
798 fmt = line->format;
799
800 while (*str) {
801 /* Handle escape sequences by skipping both \ and next char */
802 if (*str == '\\') {
803 *fmt = prev;
804 fmt++;
805 str++;
806 if (*str == '\0') {
807 break;
808 }
809 *fmt = prev;
810 fmt++;
812 continue;
813 }
814 /* Handle continuations */
815 if (cont) {
816 /* C-Style comments */
817 if (cont & FMT_CONT_COMMENT_C) {
818 if (*str == '*' && *(str + 1) == '/') {
819 *fmt = FMT_TYPE_COMMENT;
820 fmt++;
821 str++;
822 *fmt = FMT_TYPE_COMMENT;
823 cont = FMT_CONT_NOP;
824 }
825 else {
826 *fmt = FMT_TYPE_COMMENT;
827 }
828 /* Handle other comments */
829 }
830 else {
831 find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\'';
832 if (*str == find) {
833 cont = 0;
834 }
835 *fmt = FMT_TYPE_STRING;
836 }
837
839 }
840 /* Not in a string... */
841 else {
842 /* C-Style (multi-line) comments */
843 if (*str == '/' && *(str + 1) == '*') {
844 cont = FMT_CONT_COMMENT_C;
845 *fmt = FMT_TYPE_COMMENT;
846 fmt++;
847 str++;
848 *fmt = FMT_TYPE_COMMENT;
849 }
850 /* Single line comment */
851 else if (*str == '/' && *(str + 1) == '/') {
852 text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - int(fmt - line->format));
853 }
854 else if (ELEM(*str, '"', '\'')) {
855 /* Strings */
856 find = *str;
857 cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE;
858 *fmt = FMT_TYPE_STRING;
859 }
860 /* White-space (all white-space has been converted to spaces). */
861 else if (*str == ' ') {
862 *fmt = FMT_TYPE_WHITESPACE;
863 }
864 /* Numbers (digits not part of an identifier and periods followed by digits) */
865 else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) ||
866 (*str == '.' && text_check_digit(*(str + 1))))
867 {
868 *fmt = FMT_TYPE_NUMERAL;
869 }
870 /* Booleans */
871 else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_pov_find_bool(str)) != -1) {
872 if (i > 0) {
874 }
875 else {
877 *fmt = FMT_TYPE_DEFAULT;
878 }
879 }
880 /* Punctuation */
881 else if (text_check_delim(*str)) {
882 *fmt = FMT_TYPE_SYMBOL;
883 }
884 /* Identifiers and other text (no previous white-space/delimiters so text continues). */
885 else if (prev == FMT_TYPE_DEFAULT) {
887 *fmt = FMT_TYPE_DEFAULT;
888 }
889 /* Not white-space, a digit, punctuation, or continuing text.
890 * Must be new, check for special words. */
891 else {
892 /* Keep aligned arguments for readability. */
893 /* clang-format off */
894
895 /* Special vars(v) or built-in keywords(b) */
896 /* keep in sync with `txtfmt_pov_format_identifier()`. */
897 if ((i = txtfmt_pov_find_specialvar(str)) != -1) { prev = FMT_TYPE_SPECIAL;
898 } else if ((i = txtfmt_pov_find_keyword(str)) != -1) { prev = FMT_TYPE_KEYWORD;
899 } else if ((i = txtfmt_pov_find_reserved_keywords(str)) != -1) { prev = FMT_TYPE_RESERVED;
900 } else if ((i = txtfmt_pov_find_reserved_builtins(str)) != -1) { prev = FMT_TYPE_DIRECTIVE;
901 }
902
903 /* clang-format on */
904
905 if (i > 0) {
906 text_format_fill_ascii(&str, &fmt, prev, i);
907 }
908 else {
910 *fmt = FMT_TYPE_DEFAULT;
911 }
912 }
913 }
914 prev = *fmt;
915 fmt++;
916 str++;
917 }
918
919 /* Terminate and add continuation char */
920 *fmt = '\0';
921 fmt++;
922 *fmt = cont;
923
924 /* If continuation has changed and we're allowed, process the next line */
925 if (cont != cont_orig && do_next && line->next) {
926 txtfmt_pov_format_line(st, line->next, do_next);
927 }
928
930}
931
934/* -------------------------------------------------------------------- */
956
bool text_check_digit(char ch)
Definition text.cc:2303
bool text_check_identifier(char ch)
Definition text.cc:2314
bool text_check_delim(char ch)
Definition text.cc:2287
#define BLI_assert(a)
Definition BLI_assert.h:50
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define ARRAY_SIZE(arr)
#define ELEM(...)
int len
#define str(s)
SpaceLink * next
const char * comment_line
char(* format_identifier)(const char *string)
const char ** ext
void(* format_line)(SpaceText *st, TextLine *line, bool do_next)
int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len)
void flatten_string_free(FlattenString *fs)
const bool text_format_string_literals_check_sorted_array(const Span< const char * > string_literals)
int text_format_string_literal_find(const Span< const char * > string_literals, const char *text)
void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
void ED_text_format_register(TextFormatType *tft)
int text_check_format_len(TextLine *line, uint len)
@ FMT_CONT_QUOTEDOUBLE
@ FMT_CONT_QUOTESINGLE
@ FMT_CONT_NOP
@ FMT_CONT_COMMENT_C
@ FMT_TYPE_DIRECTIVE
@ FMT_TYPE_STRING
@ FMT_TYPE_COMMENT
@ FMT_TYPE_SPECIAL
@ FMT_TYPE_DEFAULT
@ FMT_TYPE_KEYWORD
@ FMT_TYPE_WHITESPACE
@ FMT_TYPE_NUMERAL
@ FMT_TYPE_RESERVED
@ FMT_TYPE_SYMBOL
#define FMT_CONT_ALL
void ED_text_format_register_pov()
static const char * text_format_pov_literals_bool_data[]
static int txtfmt_pov_find_reserved_keywords(const char *string)
static char txtfmt_pov_format_identifier(const char *str)
static const char * text_format_pov_literals_builtins_data[]
static const Span< const char * > text_format_pov_literals_bool(text_format_pov_literals_bool_data, ARRAY_SIZE(text_format_pov_literals_bool_data))
static const Span< const char * > text_format_pov_literals_builtins(text_format_pov_literals_builtins_data, ARRAY_SIZE(text_format_pov_literals_builtins_data))
static void txtfmt_pov_format_line(SpaceText *st, TextLine *line, const bool do_next)
static const char * text_format_pov_literals_keyword_data[]
static int txtfmt_pov_find_reserved_builtins(const char *string)
static const char * text_format_pov_literals_specialvar_data[]
static const char * text_format_pov_literals_reserved_data[]
static int txtfmt_pov_find_bool(const char *string)
static const Span< const char * > text_format_pov_literals_keyword(text_format_pov_literals_keyword_data, ARRAY_SIZE(text_format_pov_literals_keyword_data))
static const Span< const char * > text_format_pov_literals_reserved(text_format_pov_literals_reserved_data, ARRAY_SIZE(text_format_pov_literals_reserved_data))
static int txtfmt_pov_find_keyword(const char *string)
static int txtfmt_pov_find_specialvar(const char *string)
static const Span< const char * > text_format_pov_literals_specialvar(text_format_pov_literals_specialvar_data, ARRAY_SIZE(text_format_pov_literals_specialvar_data))