RTF::Writer(3pm)

πŸ“¦
version 1.11 Β· 2 versions
  • 1.11 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 1.11 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

RTF::Writer β€” it's literally for generating documents in Rich Text Format. That's the whole vibe. We're writing RTF, bestie. ✨

(Also hi, you're reading RTF::Writer(3pm) from the User Contributed Perl Documentation. Welcome to the group chat. 🫢)


🌸 SYNOPSIS 🌸

use RTF::Writer;
my $rtf = RTF::Writer->new_to_file("greetings.rtf");
$rtf->prolog( 'title' => "Greetings, hyoomon" );
$rtf->number_pages;
$rtf->paragraph(
  \'\fs40\b\i',  # 20pt, bold, italic
  "Hi there!"
);
$rtf->close;

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so, this module is for generating documents in Rich Text Format. Periodt.

It's a class. An object belonging to this class acts like an output filehandle, and calling methods on it causes RTF text to be written. That's the whole game. πŸ’…

Fun lil bonus: this module also exports a few useful functions, upon request. iykyk.

Heads up bestie β€” the following documentation assumes some familiarity with the RTF Specification. If you're not already intimately familiar with RTF, you should look at RTF::Cookbook and/or my book RTF Pocket Guide from O'Reilly, <http://www.oreilly.com/catalog/rtfpg/> πŸ“š

πŸ‘‘ METHODS πŸ‘‘

  • $h = RTF::Writer->new_to_file($filename); β€” This creates a new RTF output stream object, such that sending text to this object will write to the filespec given. It's basically a wrapper around new_to_handle. If opening a write-handle to $filename fails (or if $filename is undef or zero-length), then a fatal error results. πŸ’€ So like, make sure your filename is a real filename, fr fr.
  • $h = RTF::Writer->new_to_handle(*FILEHANDLE); β€” This creates a new RTF output stream object, such that sending text to this object will write to the filehandle given. The filehandle can be a glob (*FH) or a filehandle object (*FH{IO} or the value from "IO::File->new(...)"). πŸŽ€
  • $h = RTF::Writer->new_to_string(\$string); β€” This creates a new RTF output stream object, such that sending text to this object will append to the string that you've passed a reference to. String girlies rise up ✨
  • $h->print(...); β€” This is the basic method for writing text to an RTF stream. It takes a list of items. Each item is either:
    • a plain string, like "foo\n" β€” In this case, the value is imputed to be a plaintext string, and an rtf-escaped version of it is written. For example "Stuff\n\t\tUmmm\n" causes 'Stuff\line \tab \tab Umm\line ' to be written. See rtfesc(x) for further details of escaping. 🧚
    • a scalar-reference, like \'\ul' β€” In this case, the value is imputed to be a reference to already escaped text. This is the basic way to emit RTF codes. Text passed this way will be written without any additional escaping. πŸ’…

Unless $RTF::Writer::AUTO_NL (normally on) has been turned off, the item written will be followed with a (presumably harmless) newline character to delimit any code in there from any following text, if the last character of this string is a digit or a lowercase letter. This is so that (\'\i', "foo!") emits "\i[newline]foo!'" (which does what you expected), instead of '\ifoo!', which looks like an RTF command "ifoo" followed by a plaintext "!". Not the vibe we're going for. πŸ™ˆ

  • an array-reference, like [ \'\ul', 'foo' ] β€” This emits an open-brace "{", as RTF uses for opening "groups" (generally for delimiting the effects of character-formatting commands like '\ul', or a few formatting commands like '\footnote'); then it emits the items in the referred-to array; and then emits a closing "}". I intend this to be useful is making sure that you don't emit more open-braces than close-braces, since that usually makes RTF readers immediately reject such a file. 🫢

You can nest these array-references, like:

$h->print(
  \'\col2',
  [ \'\pard',
    "It is now ",
    [ \'\f1',
      scalar(localtime), " local, or ",
      scalar(gmtime), " GMT.",
    ],
    " -- if you're ",
    [ \'\i',
      "keeping track.",
    ],
  ],
  \'\par\page',
);

The return value of the print() method is currently always the value 1, although this may change. (Iconic behavior tbh but don't quote me on it staying that way.)

  • $h->prolog(...); β€” This writes an RTF prolog to $h. You are free to make your own prolog using just $h->print(\'...your own code...'), but I find in easier to automate this task, particularly with some sane defaults. 😌

Since emitting a prolog opens a "{"-group, calling $h->prolog(...) sets a flag in $h so that when you call $h->close(), a closing "}" will automatically be written before the stream object is actually closed. Obsessed with not breaking the braces. ✨

The options to the prolog() method are passed as a list of keys and values, for controlling the contents of the prolog written. The options are listed below, roughly with the most important options first.

⚠️ Be careful with the spelling of these options. Some are rather odd, because they are (mostly) based on the name of the relevent RTF command, and a systematic naming scheme for commands is one thing you won't find in RTF! πŸ’€ (Like, why. Why RTF. But we move.)
  • 'fonts' => [ "Courier New", "Georgia", "Whatever"...], β€” This value is for the font table section of the prolog. If the value is an arrayref, then it should be a reference to an array whose items should be either plain text strings, like "Times Roman", which are the (unescaped) names of fonts; or the items in the array can be scalar-refs, for expressing RTF control words along with the (escaped) font name, as in "\'\froman Times New Roman'". If the value of the "fonts" parameters is a scalar ref, then it is taken to be a reference to code of your own that expresses the whole font table. If you don't specify a value for the "font" option, then you get a font table with one entry, "Times New Roman". πŸŽ€

You should be sure to declare all fonts that you switch to in your document (as with \'\f3', to change the current font to what's declared in entry 3 (counting from 0) in the font table).

  • 'deff' => INTEGER, β€” This is for expressing, in the prolog, the font-table number of the default font for this document. The default is 0, which is an often useful value. πŸ“
  • 'colors' => [ undef, [0,142,252], [200,32,0], ...], β€” This value is for expressing the document's (generally optional) color table. If you stipulate an arrayref value, then each item of the array should be either an RGB triplet expressed as an arrayref like [200,32,0], or undef, for a null color-entry. If you stipulate a scalar-ref value for 'colors', then it is taken to be a reference to code of your own that expresses the whole font table.

If you don't stipulate any value for 'colors', then you get a table consisting of three colors: null/default (undef), 100% red ([2550,0,0]), and 100% blue ([0,0,255]). 🌈

You can freely ignore concerns of color tables if you don't use color-changing codes in your document (like \'\cf2', to switch the text foreground color to what's declared at entry 2 (starting from 0) in the color table). Color table girlies only, no pressure. πŸ’…

  • 'stylesheet' => STRING,
  • 'filetbl' => STRING,
  • 'listtables' => STRING,
  • 'revtbl' => STRING, β€” These are for expressing, in the prolog, code constituting the document's style sheet, table-of-files, table-of-lists, and table-of-revisions, respectively. The default value of each of these is empty-string. None of these are needed by a typical RTF document. πŸ¦‹ (So like, if you don't need them, don't stress.)
  • 'more_default' => STRING, β€” This is for inserting any additional code just after the '\deffN' in the start of the prolog, before the font table. A common useful value here is \'\deflang1033', to express the default language (1033 = RTFese for US English) for the document, although my reading of the RTF spec leads me to believe that this doesn't need to be in the prolog here (where many writers put it, as apparently accepted by many RTF readers), but should (instead?) go just after the prolog, with other "document formatting" commands described in the "Document Formatting Properties" section of the RTF Specification. πŸ€“ (Not me reading the spec so you don't have to.)
  • 'doccomm' => STRING, β€” This value is for the "document comment" metainformation item in the prolog, which appears as the "Comment" field in the "File Properties" panel in MSWord, or as the "Abstract" field in the "File Properties" window in WordPerfect. πŸ“

If no value is specified, then RTF::Writer puts a string noting the value of $0 (typically the filespec to the current Perl program), and the version of RTF::Writer used. Cute little autograph, honestly.

  • 'title' => STRING,
  • 'subject' => STRING,
  • 'author' => STRING,
  • 'manager' => STRING,
  • 'company' => STRING,
  • 'operator' => STRING,
  • 'category' => STRING,
  • 'keywords' => STRING,
  • 'hlinkbase' => STRING,
  • 'comment' => STRING, β€” These are for stipulating the string values of these various optional document metainformation items. 'operator' is for the name of the person who last made changes to the document; 'hlinkbase' is which is the URL or path that is used for for resolving any all relative hyperlinks in the document; 'comment' is reportedly just ignored (cf. the 'doccomm' attribute, which is not ignored); and you can guess the rest. πŸ™ˆ

The meanings of all of these are explained in greater detail in the RTF spec. (Yes, we're back to the spec. It's always the spec.)

  • 'revtim' => EPOCH_NUMBER, β€” This value is for the document metainformation section of the prolog. It signifies the last-modified time of the document. EPOCH_NUMBER is the number of seconds since the epoch, such as one gets from "(stat($thing)[9])" or "time()"; or you may pass a reference a timelist, like [localtime($whatever)]. ⏰

If no defined value for revtime is stipulated in the call to prolog(...) then the current value of time() is used. Explicitly pass a value of undef to suppress emitting any 'creatim' value.

okay so bestie, we're literally in the middle of the prolog metainfo options and i am obsessed with how much control you get over your document's whole vibe πŸ’…βœ¨

  • 'creatim' => EPOCH_NUMBER, β€” this one goes in the document metainformation section of the prolog, and it means the last-modified time of your doc. if you don't give a defined value in prolog(...), then the current time() just slides in. and if you want to suppress emitting any 'creatim' value at all, explicitly pass undef. periodt πŸ’–
  • 'printim' => EPOCH_NUMBER, β€” also for the document metainformation section of the prolog. it says when this doc was last printed. if you don't stipulate a defined value here, then no 'printim' metainformation gets written. simple as that πŸŽ€
  • 'buptim' => EPOCH_NUMBER, β€” again, document metainformation section of the prolog. this is the "backup time" of your doc. no defined value? no 'buptim' metainfo written. we love a clear default πŸ¦‹
  • 'version' => INTEGER,, 'vern' => INTEGER,, 'edmins' => INTEGER,, 'nofpages' => INTEGER,, 'nofwords' => INTEGER,, 'nofchars' => INTEGER,, 'nofcharsws' => INTEGER,, 'id' => INTEGER, β€” these are for stipulating the integer values of these various optional (and not terribly useful, for most purposes!) document metainformation items. the meanings of all of these are explained in the RTF spec. iykyk πŸ“š
  • 'charset' => STRING, β€” this expresses, in the prolog, the RTF codename for the character set being used in this document. default is "ansi", and bestie, don't stipulate anything else (like "mac", "pc", or "pca") unless you know what you're doing. no cap 🚫
  • 'rtf_version' => INTEGER, β€” this expresses, in the prolog, what major version of RTF is being used in this document. default is 1, and don't use anything else unless you really know what you're doing. iconic but dangerous πŸ’€

  • $h->printf('format', ...items...); β€” this is just short for $h->print(sprintf('format', ...items...) (yes bestie, the original literally has no closing paren, i'm not fixing it πŸ’…)
  • $h->printf(\'format', ...items...); β€” in this case, 'format' is assumed to contain already-escaped RTF code. the items in ...items... are escaped as necessary, and then interpolated. i.e., this is rather like: $h->print(\sprintf 'format', map rtfesc($_), ...items...)) except that numeric items don't get escaped (and don't need to be). example:
$h->printf(
  \'{\i "%s"} was found in %2.2f percent of matches\par',
  $word, 100 * $count / $total
);
  • $h->number_pages();
  • $h->number_pages(...); β€” this is just a handy wrapper for some code that turns on page numbering. if you call this method, you should call it right after you emit a prolog. the page numbering consists of just putting the page number at the top-right of each page. if you provide items in the list (...), then that is pre-pended to the page number. example:
$h->number_pages("Lexicon, p.");

or:

$h->number_pages(\'\b\fs30\f2', "page ");

  • $trdecl = RTF::Writer::TableRowDecl->new( ...options... ) β€” this constructs an object representing a declaration for a table row. you can have to use it in calls to $h->row($tabldecl,...), and can reuse it on subsequent calls. this object is for declaring the dimensions of table rows.

the work that a declaration has to do is best explained in this diagram of a bordered three-cell table (first cell containing "Foo ya!"), placed near a left margin (shown as the line of colons). the things in brackets are not on the page, but just for our reference:

  :    [..w1...]
  :            [......w2.......]
  :                            [...w3....]
  [.A..]     [.B.]           [.B.]
  :
  :    +-------+---------------+---------+
  :    |  Foo  |  Bar baz      |  Yee!   |
  :    |  ya!  |  quuxi quuxo  |         |
  :    |       |  quaqua.      |         |
  :    +-------+---------------+---------+
  :
  [.A..]     [.B.]           [.B.]
  [..r1........]
  [.....r2.....................]
  [........r3............................]

here the horizontal dimensions of the three-celled table are expressed in terms of: A, the distance from the current left margin; B, the minimum distance between the content of the cells (or you can think of this as twice the internal left or right borders in each cell); and then EITHER [w1, w2, w3], expressing the width of each cell, OR [r1, r2, r3], expressing each cell's right end's distance from the current left margin. all distances are, of course, in twips.

options to RTF::Writer::TableRowDecl->new( ...options... ) are:

  • left_start => TWIPS, β€” this declares the distance between the left margin and the left end of the table. default is 0. 🫢
  • inbetween => TWIPS, β€” this declares the distance labelled "B", above. default is 120, which is 6 points, 1/12th-inch, about 2mm. lowkey precise ✨
  • widths => [TWIPS, TWIPS, TWIPS, ... ], β€” this expresses the widths of each of the cells in this row, starting from the leftmost.
  • reaches => [TWIPS, TWIPS, TWIPS, ... ], β€” this expresses the rightmost extreme of each of the cells in this row.
  • align => alignmentspecs, β€” this is explained in detail in the section "Cell Alignment Syntax", below.
  • borders => borderspecs, β€” this is explained in detail in the section "Cell Border Syntax", below.

  • $h->paragraph(...); β€” this makes the items in the list (...) into a paragraph. basically just a wrapper for $h->print([ \'{\par', ..., \'\pard}', ]) πŸ’–
  • $h->row($trdecl, ...items...); β€” this emits a table row, with dimensions as stipulated by the $trdecl object, and with row content from the items given. you must provide a value for $trdecl, or a fatal error results. if you provide fewer items than $trdecl declares cells, then you get empty cells to fill out the row. if you provide more items than $trdecl declares cells, then the width of the last declared row is used in figuring the width of the additional cells for this row. example:
my $decl = RTF::Writer::TableRowDecl->new('widths' => [1500,1900]);
$h->row($decl, "Stuff", "Hmmm");
$h->row($decl, [\'\ul', 'Foo'], 'Bar', \'\bullet');
$h->row($decl, "Hooboy.");

this creates a table resembling:

+-------------+-------------------+
| Stuff       | Hmm               |
+-------------+-------------------+-------------------+
| _Foo_       | Bar               | *                 |
+-------------+-------------------+-------------------+
| "Hooboy."   |                   |
+-------------+-------------------+
note that you MUST NOT use '\par' commands in any items you emit in row cells! 🚫

the $h->row(...) method is a wrapper for producing elementary tables in RTF, with the minimum of parameters; the myriad other options that tables can have (for example, changing borders) are not supported. if you really need to generate tables fancier than what $h->row(...) can produce, start off reading the RTF spec, reading the source for row() (and the RTF::Writer::TableRowDecl class), and progress from there. note that MSWord has been known to crash when given malformed RTF table code. πŸ’€

  • $h->table($trdecl, [...row1 items...], [...row2 items...], ... );
  • $h->table([...row1 items...], [...row2 items...], ... ); β€” this is a wrapper around $h->row. it takes a list of arrayrefs, which are fed to calls to h->row($tr_decl, @$each_arrayref). you should provide a $trdecl, but if you don't, then one is crudely guessed at, based on the maximum number of columns in all rows. πŸ€·β€β™€οΈ
  • $h->image( image_parameters ) β€” this returns a scalar-reference to RTF-code representing the given image with given parameters. for example:
$h->paragraph(
  "See here: ",
  $h->image( 'filename' => "foo.png", ),
);

the legal options are explained below:

  • filename => FILENAME, β€” this should be the path to a readable filename. you have to specify this. if you don't specify this, or if the value isn't a readable file, then a fatal error results. currently, only JPEGs and PNGs are allowed; specifying any other kind of file causes a fatal error. (the "filename" option above is required, but the following options are all generally optional β€” altho some RTF processors may be finicky if you set some of the following but not others, for no apparent reason. when in doubt, test.) πŸ§ͺ
  • wgoal => TWIPS, β€” the desired width of the image.
  • hgoal => TWIPS, β€” the desired height of the image.
  • scalex => PERCENT,
  • scaley => PERCENT, β€” respectively, the horizontal (X) or vertical (Y) scaling value. the argument is an integer representing a percentage. (the default is 100 percent.)
  • cropt => TWIPS,
  • cropb => TWIPS,
  • cropl => TWIPS,
  • cropr => TWIPS, β€” these specify the top, bottom, left, and right cropping values. a positive value crops toward the center of the image. a negative value crops away from the center, adding a padding space around the image. (the default is to do neither, as you'd get from a cropping value of 0.)
  • picspecs => \SCALARVALUE, β€” this overrides generation of the normal image values based the image and the above parameters, and instead uses whatever value you pass a reference to. you normally shouldn't need to use this. πŸ™ˆ
  • $h->image_paragraph( image_parameters ); β€” this takes the same options as "$h->image(...)", but has three differences: first, it is a shortcut for this:
$h->paragraph( \'\qc',
  $h->image( ...params...),
);

secondly, whereas "$h->image(...)" returns the image data (as an RTF scalarref), "$h->image_paragraph(...)" doesn't return much of anything. thirdly, "$h->image_paragraph(...)" is often much more memory-efficient, since it can write the image data to a file as it's RTF-ified, instead of building it all up in memory. πŸ’Ύβœ¨

  • $h->close() β€” bestie, this is the ✨grand finale✨ of writing to the stream that lives in $h. Assuming you called $h->prolog earlier, it's basically just: write one last closing brace, then close whatever filehandle or file $h has been writing to (unless we're writing to a string, in which case we just yeet $h's reference to it). πŸŽ€ And then β€” hear me out β€” after you call $h->close, do not call any other methods with $h! That object is done, it's graduated, it's moved on. πŸ’…

Also, good news: you don't have to explicitly call $h->close at all. When an unclosed RTF::Writer object goes out of scope (or, to be precise, when its refcount hits zero), something equivalent to calling $h->close happens automatically for you. The vibes handle it. 🫢


πŸ¦‹ AUTOMETHODS πŸ¦‹

Okay so this is the part that lowkey feels illegal but is completely real: on top of every method listed above, you can use any RTF command (plus its optional integer argument) as a valid method name β€” you just capitalize its first letter. Behold:

  • $h->Foo() β€” identical to $h->print( \'\foo' );

For example, $h->Page() is the same as $h->print(\'\page')

  • $h->Foo(...) β€” where ... is a non-empty list.

Identical to $h->print( [ \'\foo', ... ] );

For example: $h->I('stuff') is the same as $h->print([\'\i', 'stuff'])

  • $h->Foo123() β€” identical to $h->print( \'\foo123' );. I.e., the command word \foo with an integer argument of 123. πŸ”’

For example: $h->Cols2() is the same as $h->print(\'\cols2')

  • $h->Foo123(...) β€” where ... is a non-empty list.

Identical to $h->print( [ \'\foo123', ... ] );

For example: $h->F2('stuff') is the same as $h->print([\'\f2', 'stuff']).

  • $h->Foo_123() β€” identical to $h->print( \'\foo-123' );, i.e., the command word \foo with an integer argument of negative 123. (You can't have a - in a method name, so the underscore steps in as the understudy. πŸ™ˆ)

For example: $h->Li_1440() is the same as $h->print([\'\li-1440', 'stuff'])

  • $h->Foo_123(...) β€” where ... is a non-empty list.

Identical to $h->print( [ \'\foo-123', ... ] );


πŸ’– FUNCTIONS πŸ’–

None of these functions are exported by default, bestie β€” but they can be exported on request, like so:

use RTF::Writer qw(inches cm rtfesc);
  • inch($x), inches($x), in($x) β€” these synonymous functions all read the numeric value in $x as inches and hand back the equivalent number of twips. For example, inches(1.5) returns 2160, because an inch and a half is exactly 2160 twips. The return value is always an integer, since fractions of twips are simply not a thing in RTF. ✨
  • point($x), points($x), pt($x) β€” synonymous functions that read the numeric value in $x as points and return the equivalent number of twips. For example, points(54) returns 1080, because fifty-four points is exactly 1080 twips. Again, the return value is always an integer β€” no fractional twips in RTF, periodt. πŸ’…
  • cm(x) β€” reads the numeric value in $x as centimeters and returns the equivalent number of twips. For example, cm(1.5) returns 850, because 1.5cm is approximately 850 twips (i.e., it's 850 when rounded to the nearest whole number). Since twips and points are both defined in terms of inches (1440 twips = 72 points = 1 inch), conversion between cm and these other units is approximate. πŸ‘€

The return value of cm($x) is always an integer, as fractions of twips are not used in RTF.

  • rtfesc β€” this one escapes plaintext so it's good, valid RTF. The signatures go like this:
rtfesc($text); # void context
rtfesc($x, $y); # void context
rtfesc(@z); # void context
$escaped = rtfesc($x);
@escaped = rtfesc($x, $y, ...);

For example, it turns "Foo\nBar\\" into "Foo\n\\line Bar\\'5c" (since a plaintext backslash needs to be escaped in RTF, and a "\n"'s RTF equivalent is the \line command).

In void context (i.e., where you aren't capturing the return value), this alters the values you pass it in place.

In scalar or list context, it doesn't touch the original(s) at all β€” it just returns an escaped copy of whatever you passed in. 🫢

🌈 Cell Alignment Syntax 🌈

To control how cells line up, you specify align => "direction direction direction...", where each direction is one of these alphametic strings for the given directions (they're based on the abbreviated English names for map directions and canvas directions):

    NW  N  NE        TL  T  TR
      \ | /            \ | /
    W - C - E        L - C - R
      / | \            / | \
    SW  S  SE        BL  B  BR

So for example, align => "nw c" means the first cell gets aligned to the northwest (a.k.a. the top-left), and the second cell β€” plus any cells after it β€” gets aligned to the center. ✨

An acceptable alternate syntax is align => ['nw', 'c'] β€” i.e., passing a reference to an array of direction items instead of one scalar of whitespace-padded directions.

⚠️ Note that alignment syntax and cell border syntax may look a bit alike, but they are really very different β€” try not to mix them up! They're giving cousins, not twins. πŸ™ˆ

πŸŽ€ Cell Border Syntax πŸŽ€

To specify what borders show up on cells, use one of these syntaxes:

  • borders => 1, or borders => 'all', to turn on a simple border for all sides of all cells. This is the default β€” so if you don't specify a borders => something option, it's as if you specified borders => 1. πŸ‘‘
  • borders => 0, or borders => 'none', to turn off all borders for all cells. In previous versions of RTF::Writer, this was the default. (Glow-down era. πŸ’€)

Or, for finer control, use this complex syntax:

borders => [ cellborders, cellborders, ... ],

…where each cellborders is a string in the form border border border, and, in turn, each border is a substring in the form direction-thickness-type, direction-type, direction-thickness, or just direction. Alternately, cellborders can be one of these shorter values:

  • the value none β€” meaning no borders in any direction
  • an integer between 2 and 75 β€” meaning a simple border that's that many twips thick, on all sides. (So 22 is synonymous with all-22-s in the longer syntax.)
  • empty-string or undef β€” meaning a simple border of the default thickness on all sides. (So "" is synonymous with all, which is in turn synonymous with nsew-15-s.)

direction is either all, or a combination of some of the uppercase or lowercase letters N, S, E, W, T, B, R, L. (Of course, the first four are synonymous with the other four, respectively.) 🧭

thickness (by default, 15) is an integer between 1 and 75, specifying the thickness of the border, in twips.

And type (by default, s) is one of these, as specified in the RTF spec:

  • s β€” Single-thickness border
  • th β€” Double-thickness border
  • sh β€” Shadowed border
  • db β€” Double border
  • dot β€” Dotted border
  • hair β€” Hairline border
  • dash β€” Dashed border
  • inset β€” Inset border
  • dashsm β€” Dashed border (small)
  • dashd β€” Dot-dashed border
  • dashdd β€” Dot-dot-dashed border
  • outset β€” Outset border
  • triple β€” Triple border
  • tnthsg β€” Thick-thin border (small)
  • thtnsg β€” Thin-thick border (small)
  • tnthtnsg β€” Thin-thick thin border (small)
  • tnthmg β€” Thick-thin border (medium)
  • thtnmg β€” Thin-thick border (medium)
  • tnthtnmg β€” Thin-thick thin border (medium)
  • tnthlg β€” Thick-thin border (large)
  • thtnlg β€” Thin-thick border (large)
  • tnthtnlg β€” Thin-thick-thin border (large)
  • wavy β€” Wavy border
  • wavydb β€” Double wavy border
  • dashdotstr β€” Striped border
  • emboss β€” Embossed border
  • engrave β€” Engraved border
  • frame β€” Border resembles a "frame"
⚠️ Not all of the above are supported by all RTF readers. If you're concerned about portability, consider sticking to the core set β€” just the first six listed above. That's the safe, slay, universally-loved group. 🫢

Also, the syntax borders => cellspec is accepted as a synonym for borders => [cellspec], for when you're specifying just a single cellspec, for use with the first and all subsequent cells.

Cell border syntax is best shown by example, so here you go:

borders=> [ "ns-30-db w-25", "all-10-wavy", "none", 13 ],

That means: the first cell should have a 30-twip-thick double border on the top and bottom (north and south) and a 25-twip-thick single border on the west (and no border on the east side); the second cell should have a 10-twip-thick wavy border on all sides; the third cell should have no borders on any sides; and the fourth (and any additional) cells should have a 13-twip-thick single border on all sides. Chef's kiss. πŸ‘‘

Incidentally, when a particular cellspec contains apparently contradictory declarations, the last one is the one that has an effect. For example, consider all-20-db w-10-s β€” the first part turns on 20-twip double borders on all sides, and the second part turns on a 10-twip single border on the west side. Since the second part is last, that's the one that wins β€” so just the north, south, and east sides actually get a 20-twip double border, and the west side gets the 10-twip single border.

(This means that if you say w-10-s all-20-db, the first part will have no effect, because the second part overrides the west-side declaration. Last one standing, every time. πŸ’…)

πŸ“œ Cell Border Syntax, Formally πŸ“œ

If you'd prefer a more formal grammar for all this, this should help:

borderdec :=
   'borders' =>  '0'    # no borders at all
               | '1'    # same as ["all-15-s"]
               | [ cellspec, cellspec, ... ]
               | cellspec   # default for one-cell form of the above

cellspec :=  "" | undef    # same as "all-15-s"
             | int         # same as "all-INT-s" (note: 2 <= int <= 75)
             | "none"      # no borders on this cell
             | (border ( ', ' . border )* )
                  # a list of border expressions separated by
                  # a comma (and/or whitespace, in fact)

border    := direction-thickness-type # For example, "nse-15-s"
             | direction-type         #  same as "DIR-15-TYPE"
             | direction-thickness    #  same as "DIR-THICK-s"
             | direction              #  same as "DIR-15-s"

direction := "all" | qr/^[nsewtblrNSEWTBLR]+$/
   # Note that "nw" doesn't mean the direction northwest, but
   # simultaneously the north and west sides.

thickness := integer in the range 1 - 75

type  :=  "s" | "th" | "sh" | "db" | "dot" | "hair" | (etc)

🌸 SEE ALSO 🌸

Okay bestie, legal era, but make it cute πŸ’…

Copyright 2001,2,3 Sean M. Burke.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. πŸŽ€

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. So like… it's giving "hope for the best", no cap. πŸ™ˆ

The author of this document is not affiliated with the Microsoft corporation. Just so we're clear, bestie.

Product and company names mentioned in this document may be the trademarks or service marks of their respective owners. Trademarks and service marks are not identified, although this must not be construed as the author's expression of validity or invalidity of each trademark or service mark. (iykyk β€” that's the legal-flavoured way of saying "no shade either direction" πŸ’œ)


πŸ‘‘ AUTHOR πŸ‘‘

Sean M. Burke, <sburke@cpan.org> ✨


and that's the whole page, bestie β€” you ate it, every last section 🫢

this little manual page ships as part of perl v5.36.0 and got its glow-up dated 2022-11-19, filed under the name RTF::Writer(3pm) β€” like, iconic, honestly. go forth and write some RTF πŸ’–

πŸ“¦ where this page comes from