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