You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/TextRegion.md
+32-14
Original file line number
Diff line number
Diff line change
@@ -1,34 +1,36 @@
1
+
_New in [:octicons-tag-24: 2.7.7](https://github.com/py-pdf/fpdf2/blob/master/CHANGELOG.md)_
1
2
# Text Flow Regions #
2
-
_New in [:octicons-tag-24: 2.7.6](https://github.com/py-pdf/fpdf2/blob/master/CHANGELOG.md)_
3
3
4
-
**Notice:** As of fpdf2 release 2.7.6, this is an experimental feature. Both the API and the functionality may change before it is finalized, without prior notice.
4
+
**Notice:** As of fpdf2 release 2.7.7, this is an experimental feature. Both the API and the functionality may change before it is finalized, without prior notice.
5
5
6
6
Text regions are a hierarchy of classes that enable to flow text within a given outline. In the simplest case, it is just the running text column of a page. But it can also be a sequence of outlines, such as several parallel columns or the cells of a table. Other outlines may be combined by addition or subtraction to create more complex shapes.
7
7
8
8
There are two general categories of regions. One defines boundaries for running text that will just continue in the same manner one the next page. Those include columns and tables. The second category are distinct shapes. Examples would be a circle, a rectangle, a polygon of individual shape or even an image. They may be used individually, in combination, or to modify the outline of a multipage column. Shape regions will typically not cause a page break when they are full. In the future, a possibility to chain them may be implemented, so that a new shape will continue with the text that didn't fit into the previous one.
9
9
10
+
**The current implementation only supports columns.** Shaped regions and combinations are still in the design phase.
11
+
10
12
## General Operation ##
11
13
12
14
Using the different region types and combination always follows the same pattern. The main difference to the normal `FPDF.write()` method is that all added text will first be buffered, and you need to explicitly trigger its rendering on the page. This is necessary so that text can be aligned within the given boundaries even if its font, style, or size are arbitrarily varied along the way.
13
15
14
16
* Create the region instance with an `FPDF` method.
15
-
*If desired, add or subtract other shapes from it (with geometric regions).
17
+
*future: (_If desired, add or subtract other shapes from it (with geometric regions)_).
16
18
* Use the `.write()` method to feed text into its buffer.
17
19
* Best practise is to use the region instance as a context manager for filling.
18
20
* Text will be rendered automatically after closing the context.
19
21
* When used as a context manager, you can change all text styling parameters within that context, and they will be used by the added text, but won't leak to the surroundings
20
-
*For adding text with the already existing settings, just use the region instance as is. In that case, you'll have to explicitly use the `render()` method.
22
+
*Alternatively, eg. for filling a single column of text with the already existing settings, just use the region instance as is. In that case, you'll have to explicitly use the `render()` method after adding the text.
21
23
* Within a region, paragraphs can be inserted. The primary purpose of a paragraph is to apply a different horizontal alignment than the surrounding text.
22
24
23
25
### Text Start Position ###
24
26
25
-
When rendering, the vertical start position of the text will be at the lowest one out of the current y position, the top of the region (if it has a defined top), or the top margin of the page. The horizontal start position will either at the current x position or at the left edge of the region, whichever is further to the right. In both horizontal and vertical positioning, regions with multiple columns may follow additional rules and restrictions.
27
+
When rendering, the vertical start position of the text will be at the lowest one out of the current y position, the top of the region (if it has a defined top), or the top margin of the page. The horizontal start position will be either at the current x position if that lies within the boundaries of the region/column or at the left edge of the region. In both horizontal and vertical positioning, regions with multiple columns may follow additional rules and restrictions.
26
28
27
29
28
30
### Interaction between Regions ###
29
31
30
-
Several region instances can exist at the same time. But only one of them can act as context manager at any given time. It is not currently possible to operate them recursively.
31
-
But it is possible to use them intermittingly. This will probably most often make sense between a columnar region and a table. You may have some running text ending at a given height, then insert a table with data, and finally continue the running text at the new height below the table within the existing column.
32
+
Several region instances can exist at the same time. But only one of them can act as context manager at any given time. It is not currently possible to use them recursively.
33
+
But it is possible to use them intermittingly. This will probably most often make sense between a columnar region and a table. You may have some running text ending at a given height, then insert a table with data, and finally continue the running text at the new height below the table within the existing column(s).
32
34
33
35
34
36
## Columns ##
@@ -53,8 +55,7 @@ In this example an inserted paragraph is used in order to format its content wit
53
55
Here we have a layout with three columns. Note that font type and text size can be varied within a text region, while still maintaining the justified (in this case) horizontal alignment.
with pdf.text_columns(align="J", ncols=3, gutter=5) as cols
58
59
cols.write(txt=LOREM_IPSUM)
59
60
pdf.set_font("Times", "", 8)
60
61
cols.write(txt=LOREM_IPSUM)
@@ -69,11 +70,22 @@ Normally the columns will be filled left to right, and if the text ends before t
69
70
If you prefer that all columns on a page end on the same height, you can use the `balance=True` argument. In that case a simple algorithm will be applied that attempts to approximately balance their bottoms.
70
71
71
72
```python
72
-
with pdf.text_columns(align="J", ncols=3, gutter=5, balanced=True) as cols:
Note that this only works reliably when the font size (specifically the line height) doesn't change. If parts of the text use a larger or smaller font than the rest, then the balancing will usually be out of whack. Contributions for a more refined balancing algorithm are welcome.
87
+
88
+
Note that column balancing only works reliably when the font size (specifically the line height) doesn't change. If parts of the text use a larger or smaller font than the rest, then the balancing will usually be out of whack. Contributions for a more refined balancing algorithm are welcome.
77
89
78
90
### Possible future extensions
79
91
@@ -84,13 +96,19 @@ Those features are currently not supported, but Pull Requests are welcome to imp
84
96
85
97
## Paragraphs ##
86
98
87
-
The primary purpose of paragraphs is to enable variations in horizontal text alignment, while the horizontal extents of the text are managed by the text region.
99
+
The primary purpose of paragraphs is to enable variations in horizontal text alignment, while the horizontal extents of the text are managed by the text region. To set the alignment, you can use the `align` argument when creating the paragraph, with the same `Align` values as elsewhere in the library. Note that the `write()` methods of paragraphs and text regions in general don't accept this argument, they only accept text.
100
+
101
+
For more typographical control, you can also use the following arguments:
102
+
* line_height (default: 1.0) - This is a factor by which the line spacing will be different from the font height. It works similar to the attribute of the same name in HTML/CSS.
103
+
* top_margin (default: 0.0)
104
+
* bottom_margin (default: 0.0) - Those two values determine how much spacing is added above and below the paragraph. No spacing will be added at the top if the paragraph if the current y position is at (or above) the top margin of the page. Similarly, none will be added at the bottom if it would result in overstepping the bottom margin of the page.
105
+
* skip_leading_spaces (default: False) - This flag is primarily used by `write_html()`, but may also have other uses. It removes all space characters at the beginning of each line.
88
106
89
107
Other than text regions, paragraphs should alway be used as context managers and never be reused. Violating those rules may result in the entered text turning up on the page out of sequence.
90
108
91
109
### Possible future extensions
92
110
93
111
Those features are currently not supported, but Pull Requests are welcome to implement them:
0 commit comments