1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
.. _tut_text:
.. module:: ezdxf.entities
:noindex:
Tutorial for Text
=================
Add a simple one line text entity by factory function :meth:`~ezdxf.layouts.BaseLayout.add_text`.
.. code-block:: python
import ezdxf
# TEXT is a basic entity and is supported by every DXF version.
# Argument setup=True for adding standard linetypes and text styles.
doc = ezdxf.new('R12', setup=True)
msp = doc.modelspace()
# use set_pos() for proper TEXT alignment:
# The relations between DXF attributes 'halign', 'valign',
# 'insert' and 'align_point' are tricky.
msp.add_text("A Simple Text").set_pos((2, 3), align='MIDDLE_RIGHT')
# Using a text style
msp.add_text("Text Style Example: Liberation Serif",
dxfattribs={
'style': 'LiberationSerif',
'height': 0.35}
).set_pos((2, 6), align='LEFT')
doc.saveas("simple_text.dxf")
Valid text alignments for argument `align` in :meth:`Text.set_pos`:
============ =============== ================= =====
Vert/Horiz Left Center Right
============ =============== ================= =====
Top ``TOP_LEFT`` ``TOP_CENTER`` ``TOP_RIGHT``
Middle ``MIDDLE_LEFT`` ``MIDDLE_CENTER`` ``MIDDLE_RIGHT``
Bottom ``BOTTOM_LEFT`` ``BOTTOM_CENTER`` ``BOTTOM_RIGHT``
Baseline ``LEFT`` ``CENTER`` ``RIGHT``
============ =============== ================= =====
Special alignments are ``ALIGNED`` and ``FIT``, they require a second alignment point, the text
is justified with the vertical alignment `Baseline` on the virtual line between these two points.
=========== ===========
Alignment Description
=========== ===========
``ALIGNED`` Text is stretched or compressed to fit exactly between `p1` and `p2` and the text height
is also adjusted to preserve height/width ratio.
``FIT`` Text is stretched or compressed to fit exactly between `p1` and `p2` but only the text
width is adjusted, the text height is fixed by the `height` attribute.
``MIDDLE`` also a `special` adjustment, but the result is the same as for ``MIDDLE_CENTER``.
=========== ===========
Standard Text Styles
--------------------
Setup some standard text styles and linetypes by argument :code:`setup=True`::
doc = ezdxf.new('R12', setup=True)
Replaced all proprietary font declarations in :code:`setup_styles()` (ARIAL, ARIAL_NARROW, ISOCPEUR and TIMES) by open
source fonts, this is also the style name (e.g. :code:`{'style': 'OpenSans-Italic'}`):
.. image:: gfx/fonts.png
New Text Style
--------------
Creating a new text style is simple:
.. code-block:: Python
doc.styles.new('myStandard', dxfattribs={'font' : 'OpenSans-Regular.ttf'})
But getting the correct font name is often not that simple, especially on Windows.
This shows the required steps to get the font name for `Open Sans`:
- open font folder `c:\\windows\\fonts`
- select and open the font-family `Open Sans`
- right-click on `Open Sans Standard` and select `Properties`
- on top of the first tab you see the font name: ``'OpenSans-Regular.ttf'``
The style name has to be unique in the DXF document, else `ezdxf` will raise an :class:`DXFTableEntryError` exception.
To replace an existing entry, delete the existing entry by :code:`doc.styles.remove(name)`, and add the replacement
entry.
3D Text
-------
It is possible to place the 2D :class:`Text` entity into 3D space by using the :ref:`OCS`,
for further information see: :ref:`tut_ocs`.
|