Position of string with drawtextinrect

I wonder if there is any way to get the x and y coordinates of a string with drawtextinrect, or if there is any way to get some text of the drwatextinrect with one font and other text, in the middle, with other font.
Mi idea is that the string reveals in the text after some time, maybe within a rectangle or maybe with any other font.

I have an idea for you, which might be a little tricky to pull off but should be entirely possible. Bitmap fonts are really just rendered as images for each character, and the .fnt file encodes the exact width of each character and any offsets set in the kerning table. This means that if you have two separate font files with the exact same numerical metrics (glyph size, leading, character widths, kerning tables, etc.), the position that each renders a given character in an arbitrary string will be identical.

The text drawing options also support formatted text — bold, italic, etc. — by using special characters within the text. This works when using a font family. So, here's the idea:

  1. Choose your base font, e.g. "MyFont-Regular-14".
  2. Duplicate that font file and call it something like "MyFont-Redacted-14".
  3. In the copied version, erase all the pixel content of the characters so they draw as transparent (or fill them with a color or pattern to render for the unrevealed characters).
  4. Load these fonts as a font family and provide the redacted version for the "bold" variant.
  5. Just use standard bold formatting to redact text within your string, e.g. "This part *right here* is redacted."
  6. If you want to animate the reveal, you can just render the same string into an image without the redaction formatting and cross fade between them somehow — the characters will all still align properly.

This approach doesn't actually tell you where the redacted substring winds up within the rendered text, but you can achieve an effect like you describe without ever needing to know.

If you want to have the revealed font be different from the normal font, you can just repeat the same trick. Create a third font file duplicate, e.g. "MyFont-Revealed-14", swap in the glyphs for the revealed characters (again being careful not to change any numeric metrics), and load it as the italic font in the family. Then you can just swap from "This part *right here* is redacted." to "This part _right here_ is redacted." to reveal the characters in your alternate font.

Mmmmm, interesting idea, I am going to test it.
Thanks so much

I just tested right now and works absolutelly perfect, this is just what I was looking for.
Thank you so, so, so much

1 Like

Awesome, glad to hear it worked!