povray

Condition based on length of string


Using POV-Ray for Windows version 3.7, I wrote a script to render translucent cubes representing chemical elements:

#declare H=difference {
  box {                      
    <-1, -1, 0>, <1, 1, 0.5>
    material {
      M_Glass
    }
  }                          

  text {
    ttf "ariblk.ttf", element,
    0.2, // depth
    0  // spacing
    texture { 
      pigment { color <0,0,0,1> }
    }
    scale 1.3
    translate <-0.8, -0.45, -0.1>  
  }                             
}

where element is a string. Now the chemical element designators can be one character long (H... hydrogen), two characters long (At... astatine) or three characters long (unnamed new discoveries). I wrote an animation loop to iterate over all the element ordinal numbers, and already managed to render 1..118 in a single run.

The label should be translated and scaled, inside the cube in x-direction based on a factor of the string length. While for translation, centered alignment or measuring the actual bounding box first might work, I'd still like to be able to express something like this for scaling:

#switch(string_length(element))
#case(1)
    scale 0.9
    #break

#case(2)
    scale 0.72
    #break

#case(3)
    scale 0.62
    #break

#end

But there is no such function string_length. The documentation does not mention one.

Is there a string length scalar function or macro in POV-Ray? Or a workaround which does not mean to write 118 cases?


Solution

  • Found it, strlen(element):

    Source

    Then I search the documentation for strlen and it contains a reference, which is in the general function documentation part, and not in the string functions chapter:

    strlen(S)
    Length of S. Returns an integer value that is the number of characters in the string S.