Skip to content

Commit 0201e5c

Browse files
authored
feat(oiiotool): add --render_text modifiers measure= and render= (#4681)
`--render_text:measure=1` causes the render_text command to set user variables `TEXT_X`, `TEXT_Y`, `TEXT_WIDTH`, and `TEXT_HEIGHT` (as if set by `--set`) to the origin offset and dimensions of the rendered text size. These can then be accessed in subsequent expression evaluation. `--render_text:render=0` causes the text to not actually be rendered into the image. (The default is 1.) This is primarily of use in conjunction with `measure=1` if you want to measure the size that text would have taken, then use the variables for subsequent commands, but not actually draw the text. Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent a9bc8ea commit 0201e5c

4 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/doc/oiiotool.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4203,6 +4203,16 @@ current top image.
42034203
`shadow=` *size*
42044204
if nonzero, will make a dark shadow halo to make the text more clear
42054205
on bright backgrounds.
4206+
`measure=` *int*
4207+
if nonzero, will compute the rendered size of the text and store its
4208+
dimensions in the "user variables" (as if by `--set`) `TEXT_X`,
4209+
`TEXT_Y`, `TEXT_WIDTH`, `TEXT_HEIGHT`. (This modifier was added
4210+
in OpenImageIO 3.0.5.0.)
4211+
`render=` *int*
4212+
if zero, will not actually draw the text into the image (the
4213+
default is 1, meaning that the text will draw). Suppressing the
4214+
drawing is primarily useful in conjunction with `measure=1`.
4215+
(This modifier was added in OpenImageIO 3.0.5.0.)
42064216
`:subimages=` *indices-or-names*
42074217
Include/exclude subimages (see :ref:`sec-oiiotool-subimage-modifier`).
42084218

src/oiiotool/oiiotool.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5141,9 +5141,21 @@ OIIOTOOL_INPLACE_OP(text, 1, [&](OiiotoolOp& op, span<ImageBuf*> img) {
51415141
aligny = TextAlignY::Bottom;
51425142
if (Strutil::iequals(ay, "center") || Strutil::iequals(ay, "c"))
51435143
aligny = TextAlignY::Center;
5144-
int shadow = op.options().get_int("shadow");
5145-
return ImageBufAlgo::render_text(*img[0], x, y, op.args(1), fontsize, font,
5146-
textcolor, alignx, aligny, shadow);
5144+
int shadow = op.options().get_int("shadow");
5145+
int measure = op.options().get_int("measure");
5146+
int render = op.options().get_int("render", 1);
5147+
if (measure) {
5148+
ROI roi = ImageBufAlgo::text_size(op.args(1), fontsize, font);
5149+
ot.uservars["TEXT_X"] = roi.xbegin;
5150+
ot.uservars["TEXT_Y"] = roi.ybegin;
5151+
ot.uservars["TEXT_WIDTH"] = roi.width();
5152+
ot.uservars["TEXT_HEIGHT"] = roi.height();
5153+
}
5154+
if (render)
5155+
return ImageBufAlgo::render_text(*img[0], x, y, op.args(1), fontsize,
5156+
font, textcolor, alignx, aligny,
5157+
shadow);
5158+
return true;
51475159
});
51485160

51495161

testsuite/oiiotool-text/ref/out.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Text size: xy 1 -30 wh 251 37
12
Comparing "text.tif" and "ref/text-freetype2.7.tif"
23
PASS
34
Comparing "aligned.tif" and "ref/aligned.tif"

testsuite/oiiotool-text/run.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
"\"--text:x=25:y=120:font=Droid Serif Bold:size=40\" \"Hello, world\" "
4444
"-d uint8 -o fontbyfamily.tif")
4545

46+
# test size
47+
command += oiiotool ("--create 320x240 3 "
48+
"\"--text:measure=1:render=0:font=Droid Serif Bold:size=40\" \"Hello, world\" "
49+
"--echo \"Text size: xy {TEXT_X} {TEXT_Y} wh {TEXT_WIDTH} {TEXT_HEIGHT}\" ")
50+
4651
# Outputs to check against references
4752
outputs = [ "text.tif", "aligned.tif", "textshadowed.tif", "textalpha.tif", "fontbyfamily.tif" ]
4853

0 commit comments

Comments
 (0)