dimensions.html 117 KB
Newer Older
Etienne Nadji's avatar
Etienne Nadji committed
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232


<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>pyscribus.dimensions &mdash; PyScribus 0.1 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> PyScribus
          

          
            
            <img src="../../_static/logo-300.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                0.1
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../guide/en/quickstart.html">Quickstart</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../guide/fr/general.html">[FR] Fonctionnement général</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../guide/fr/dims.html">[FR] Mesures et autres nombres</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../guide/en/psm.html">PyScribus Story Markup</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../guide/en/templating.html">PyScribus templating</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/index.html">Examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../credits.html">Credits</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../thanks.html">Acknowledgements</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../modules.html">PyScribus reference</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../articles/en/spec.html">[EN] SLA document-able attributes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../articles/en/spec-suggestions.html">SLA’s enhancement suggestions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../articles/fr/stories.html">[FR] Comment les <em>stories</em> de Scribus (semblent) fonctionner</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">PyScribus</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>pyscribus.dimensions</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for pyscribus.dimensions</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python3</span>
<span class="c1"># -*- coding:Utf-8 -*-</span>

<span class="c1"># PyScribus, python library for Scribus SLA</span>
<span class="c1"># Copyright (C) 2020 Étienne Nadji</span>
<span class="c1">#</span>
<span class="c1"># This program is free software: you can redistribute it and/or modify</span>
<span class="c1"># it under the terms of the GNU General Public License as published by</span>
<span class="c1"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c1"># (at your option) any later version.</span>
<span class="c1">#</span>
<span class="c1"># This program is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c1"># GNU General Public License for more details.</span>
<span class="c1">#</span>
<span class="c1"># You should have received a copy of the GNU General Public License</span>
<span class="c1"># along with this program.  If not, see &lt;https://www.gnu.org/licenses/&gt;.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">PyScribus classes for measures and geometrical manipulations.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">math</span>

<span class="kn">from</span> <span class="nn">pyscribus.common.math</span> <span class="k">import</span> <span class="n">PICA_TO_MM</span><span class="p">,</span><span class="n">INCH_TO_MM</span>

<span class="kn">import</span> <span class="nn">pyscribus.exceptions</span> <span class="k">as</span> <span class="nn">exceptions</span>

<span class="kn">import</span> <span class="nn">pyscribus.papers.ansi</span> <span class="k">as</span> <span class="nn">ansipaper</span>
<span class="kn">import</span> <span class="nn">pyscribus.papers.iso216</span> <span class="k">as</span> <span class="nn">iso216paper</span>

<span class="kn">from</span> <span class="nn">pyscribus.common.xml</span> <span class="k">import</span> <span class="o">*</span>

<span class="c1"># Variables globales ====================================================#</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Etienne Nadji &lt;etnadji@eml.cc&gt;&quot;</span>

<span class="c1"># Classes ===============================================================#</span>

<div class="viewcode-block" id="Dim"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim">[docs]</a><span class="k">class</span> <span class="nc">Dim</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Dimension object. Allows to convert and export in the correct unit.</span>

<span class="sd">    :type value: float,int</span>
<span class="sd">    :param value: Value of the dimension</span>
<span class="sd">    :type unit: str</span>
<span class="sd">    :param unit: Unit of the dimension.</span>
<span class="sd">    :type is_int: boolean</span>
<span class="sd">    :param is_int: Use integer instead of float type for value</span>

<span class="sd">    :ivar float,int value: Value of the dimension in the original unit</span>
<span class="sd">    :ivar string unit: Unit of the dimension</span>

<span class="sd">    +-------------------------+---------------+</span>
<span class="sd">    | Unit / Notation         | unit argument |</span>
<span class="sd">    +=========================+===============+</span>
<span class="sd">    | Milimeter               | mm            |</span>
<span class="sd">    +-------------------------+---------------+</span>
<span class="sd">    | Pica point              | pica, pt      |</span>
<span class="sd">    +-------------------------+---------------+</span>
<span class="sd">    | Percentage (0 to 100)   | perc, pc      |</span>
<span class="sd">    +-------------------------+---------------+</span>
233
<span class="sd">    | Percentage (0.0 to 1)   | pcdecim, pcd  |</span>
Etienne Nadji's avatar
Etienne Nadji committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<span class="sd">    +-------------------------+---------------+</span>
<span class="sd">    | Calligraphic pen degree | cdeg          |</span>
<span class="sd">    +-------------------------+---------------+</span>
<span class="sd">    | Regular degree          | deg           |</span>
<span class="sd">    +-------------------------+---------------+</span>
<span class="sd">    | Dot per inch (DPI/PPP)  | dpi, ppp, ppi |</span>
<span class="sd">    +-------------------------+---------------+</span>
<span class="sd">    | Line per inch (LPI)     | lpi           |</span>
<span class="sd">    +-------------------------+---------------+</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># 1 pica point = 25,4/72 milimeters</span>
    <span class="n">PICA_TO_MM</span> <span class="o">=</span> <span class="p">(</span><span class="mf">25.4</span> <span class="o">/</span> <span class="mi">72</span><span class="p">)</span>

    <span class="n">UNIT_ARGS</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s2">&quot;mm&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;mm&quot;</span><span class="p">],</span>
        <span class="s2">&quot;pica&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;pica&quot;</span><span class="p">,</span> <span class="s2">&quot;pt&quot;</span><span class="p">],</span>
        <span class="s2">&quot;perc&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;perc&quot;</span><span class="p">,</span> <span class="s2">&quot;pc&quot;</span><span class="p">],</span>
252
        <span class="s2">&quot;pcdecim&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;pcdecim&quot;</span><span class="p">,</span> <span class="s2">&quot;pcd&quot;</span><span class="p">],</span>
Etienne Nadji's avatar
Etienne Nadji committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
        <span class="s2">&quot;cdeg&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;cdeg&quot;</span><span class="p">],</span>
        <span class="s2">&quot;deg&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;deg&quot;</span><span class="p">],</span>
        <span class="s2">&quot;dpi&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;dpi&quot;</span><span class="p">,</span> <span class="s2">&quot;ppp&quot;</span><span class="p">,</span> <span class="s2">&quot;ppi&quot;</span><span class="p">],</span>
        <span class="s2">&quot;lpi&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;lpi&quot;</span><span class="p">],</span>
    <span class="p">}</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">unit</span><span class="o">=</span><span class="s2">&quot;pica&quot;</span><span class="p">,</span> <span class="n">is_int</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">original_unit</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_int</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="n">is_int</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">is_int</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">value</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">set_unit</span><span class="p">(</span><span class="n">unit</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">original_unit</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">from_original_unit</span><span class="p">(</span><span class="n">original_unit</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">check_value</span><span class="p">()</span>

    <span class="c1">#--- Checking, setting the unit --------------------------------------</span>

<div class="viewcode-block" id="Dim.from_original_unit"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.from_original_unit">[docs]</a>    <span class="k">def</span> <span class="nf">from_original_unit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">original_unit</span><span class="p">):</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="Dim.check_value"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.check_value">[docs]</a>    <span class="k">def</span> <span class="nf">check_value</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check value validity according to unit.</span>

<span class="sd">        :rtype: boolean</span>
<span class="sd">        :return: True if the value is valid</span>
<span class="sd">        :raises pyscribus.exceptions.InvalidDim: Raised if value is invalid.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;pica&quot;</span><span class="p">,</span> <span class="s2">&quot;pt&quot;</span><span class="p">]:</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">InvalidDim</span><span class="p">(</span>
                    <span class="s2">&quot;Pica points must not be inferior to 0.&quot;</span>
                <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;dpi&quot;</span><span class="p">:</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">InvalidDim</span><span class="p">(</span>
                    <span class="s2">&quot;DPI/PPP must be a positive number&quot;</span>
                <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;lpi&quot;</span><span class="p">:</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">InvalidDim</span><span class="p">(</span>
                    <span class="s2">&quot;LPI must be a positive number&quot;</span>
                <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;cdeg&quot;</span><span class="p">:</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&lt;=</span> <span class="mi">180</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">InvalidDim</span><span class="p">(</span>
                        <span class="s2">&quot;Calligraph pen angle must range from 0 to 180&quot;</span>
                    <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">InvalidDim</span><span class="p">(</span>
                    <span class="s2">&quot;Calligraph pen angle must range from 0 to 180&quot;</span>
                <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;deg&quot;</span><span class="p">:</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&lt;=</span> <span class="mi">360</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">InvalidDim</span><span class="p">(</span>
                        <span class="s2">&quot;Angle must range from 0 to 360&quot;</span>
                    <span class="p">)</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">InvalidDim</span><span class="p">(</span>
                    <span class="s2">&quot;Angle must range from 0 to 360&quot;</span>
                <span class="p">)</span>

        <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="Dim.set_unit"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.set_unit">[docs]</a>    <span class="k">def</span> <span class="nf">set_unit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unit</span><span class="o">=</span><span class="s2">&quot;pica&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set the unit used.</span>

<span class="sd">        :type value: float,int</span>
<span class="sd">        :param value: Value of the dimension</span>
<span class="sd">        :type unit: str</span>
<span class="sd">        :param unit: Unit of the dimension.</span>
<span class="sd">        :rtype: boolean</span>
<span class="sd">        :return: True if the unit is valid</span>

<span class="sd">        +-------------------------+---------------+</span>
<span class="sd">        | Unit / Notation         | unit argument |</span>
<span class="sd">        +=========================+===============+</span>
<span class="sd">        | Milimeter               | mm            |</span>
<span class="sd">        +-------------------------+---------------+</span>
<span class="sd">        | Pica point              | pica, pt      |</span>
<span class="sd">        +-------------------------+---------------+</span>
<span class="sd">        | Percentage (0 to 100)   | perc, pc      |</span>
<span class="sd">        +-------------------------+---------------+</span>
362
<span class="sd">        | Percentage (0.0 to 1)   | pcdecim, pcd  |</span>
Etienne Nadji's avatar
Etienne Nadji committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<span class="sd">        +-------------------------+---------------+</span>
<span class="sd">        | Calligraphic pen degree | cdeg          |</span>
<span class="sd">        +-------------------------+---------------+</span>
<span class="sd">        | Regular degree          | deg           |</span>
<span class="sd">        +-------------------------+---------------+</span>
<span class="sd">        | Dot per inch (DPI/PPP)  | dpi, ppp, ppi |</span>
<span class="sd">        +-------------------------+---------------+</span>
<span class="sd">        | Line per inch (LPI)     | lpi           |</span>
<span class="sd">        +-------------------------+---------------+</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">tmp_unit</span> <span class="o">=</span> <span class="n">unit</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="n">valid_unit</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">for</span> <span class="n">code</span><span class="p">,</span><span class="n">args</span> <span class="ow">in</span> <span class="n">Dim</span><span class="o">.</span><span class="n">UNIT_ARGS</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>

            <span class="k">if</span> <span class="n">tmp_unit</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">=</span> <span class="n">code</span>
                <span class="n">valid_unit</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">break</span>

        <span class="k">return</span> <span class="n">valid_unit</span></div>

    <span class="c1">#--- XML export ------------------------------------------------------</span>

<div class="viewcode-block" id="Dim.toxmlstr"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.toxmlstr">[docs]</a>    <span class="k">def</span> <span class="nf">toxmlstr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">no_useless_decimals</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a XML string of the dimension according to its unit.</span>

<span class="sd">        :type no_useless_decimals: boolean</span>
<span class="sd">        :param no_useless_decimals: Returns integer instead of float if</span>
<span class="sd">            decimals are 0. So 1.0 -&gt; 1 ; 1.1 -&gt; 1.1.</span>
<span class="sd">        :rtype: str</span>
<span class="sd">        :return: str</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">def</span> <span class="nf">decimals</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">==</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">n</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;perc&quot;</span><span class="p">,</span> <span class="s2">&quot;pcdecim&quot;</span><span class="p">,</span> <span class="s2">&quot;cdeg&quot;</span><span class="p">,</span> <span class="s2">&quot;deg&quot;</span><span class="p">,</span> <span class="s2">&quot;lpi&quot;</span><span class="p">,</span> <span class="s2">&quot;dpi&quot;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">no_useless_decimals</span><span class="p">:</span>
                <span class="n">pica</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">topica</span><span class="p">()</span>
                <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">decimals</span><span class="p">(</span><span class="n">pica</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">topica</span><span class="p">())</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;pcdecim&quot;</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">==</span> <span class="mf">1.0</span><span class="p">:</span>
                    <span class="k">return</span> <span class="s2">&quot;1&quot;</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">no_useless_decimals</span><span class="p">:</span>
                        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">decimals</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">no_useless_decimals</span><span class="p">:</span>
                    <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">decimals</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span></div>

<div class="viewcode-block" id="Dim.toxml"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.toxml">[docs]</a>    <span class="k">def</span> <span class="nf">toxml</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Alias of Dimension.toxmlstr()</span>

<span class="sd">        Returns a XML string of the dimension according to its unit.</span>

<span class="sd">        :rtype: str</span>
<span class="sd">        :return: str</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">toxmlstr</span><span class="p">()</span></div>

    <span class="c1">#--- Conversion into other units -------------------------------------</span>

    <span class="k">def</span> <span class="nf">_ceil</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">ceil</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Ceil value if ceil is True.</span>

<span class="sd">        :type value: float,int</span>
<span class="sd">        :param value: Value to ceil (or not)</span>
<span class="sd">        :type ceil: boolean</span>
<span class="sd">        :param ceil: If True, apply ceil to value</span>
<span class="sd">        :rtype: float,int</span>
<span class="sd">        :return: Ceiled (or not) number</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">ceil</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">_convertorval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">unit</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a Dim object with unit &lt;unit&gt; if &lt;obj&gt;, or &lt;value&gt;.</span>

<span class="sd">        :rtype: float,int,Dim</span>
<span class="sd">        :return: Dim object or Dim value</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">obj</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">Dim</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">value</span>

<div class="viewcode-block" id="Dim.is_convertable_length"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.is_convertable_length">[docs]</a>    <span class="k">def</span> <span class="nf">is_convertable_length</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :rtype: boolean</span>
<span class="sd">        :return: If the Dim instance is a convertable length</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;perc&quot;</span><span class="p">,</span> <span class="s2">&quot;pcdecim&quot;</span><span class="p">,</span> <span class="s2">&quot;cdeg&quot;</span><span class="p">,</span> <span class="s2">&quot;deg&quot;</span><span class="p">,</span> <span class="s2">&quot;dpi&quot;</span><span class="p">,</span> <span class="s2">&quot;lpi&quot;</span><span class="p">]:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="Dim.topica"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.topica">[docs]</a>    <span class="k">def</span> <span class="nf">topica</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ceil</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the value of Dim in pica point unit.</span>
<span class="sd">        Raise ValueError if Dim is not convertable</span>

<span class="sd">        :type ceil: boolean</span>
<span class="sd">        :param ceil: If True, apply ceil to returned value</span>
<span class="sd">        :type obj: boolean</span>
<span class="sd">        :param obj: If True, returns a Dim object instead of value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_convertable_length</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">IncompatibleDim</span><span class="p">(</span>
                <span class="s2">&quot;Can&#39;t convert that (</span><span class="si">{}</span><span class="s2">) into pica points&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">unit</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;pica&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convertorval</span><span class="p">(</span>
                <span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ceil</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">ceil</span><span class="p">),</span> <span class="s2">&quot;pica&quot;</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;mm&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convertorval</span><span class="p">(</span>
                <span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ceil</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">/</span> <span class="n">Dim</span><span class="o">.</span><span class="n">PICA_TO_MM</span><span class="p">,</span> <span class="n">ceil</span><span class="p">),</span> <span class="s2">&quot;pica&quot;</span>
            <span class="p">)</span></div>

<div class="viewcode-block" id="Dim.todpi"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.todpi">[docs]</a>    <span class="k">def</span> <span class="nf">todpi</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ceil</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the value of Dim in DPI unit.</span>
<span class="sd">        Raise ValueError if Dim is not convertable</span>

<span class="sd">        :type ceil: boolean</span>
<span class="sd">        :param ceil: If True, apply ceil to returned value</span>
<span class="sd">        :type obj: boolean</span>
<span class="sd">        :param obj: If True, returns a Dim object instead of value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;lpi&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convertorval</span><span class="p">(</span>
                <span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ceil</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">*</span> <span class="mi">16</span><span class="p">),</span> <span class="s2">&quot;dpi&quot;</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">IncompatibleDim</span><span class="p">(</span>
                <span class="s2">&quot;Can&#39;t convert that into DPI&quot;</span>
            <span class="p">)</span></div>

<div class="viewcode-block" id="Dim.topc"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.topc">[docs]</a>    <span class="k">def</span> <span class="nf">topc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ceil</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the value of Dim as integer percentage.</span>
<span class="sd">        Raise ValueError if Dim is not convertable</span>

<span class="sd">        :type ceil: boolean</span>
<span class="sd">        :param ceil: If True, apply ceil to returned value</span>
<span class="sd">        :type obj: boolean</span>
<span class="sd">        :param obj: If True, returns a Dim object instead of value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;pcdecim&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convertorval</span><span class="p">(</span>
                <span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ceil</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">*</span> <span class="mi">100</span><span class="p">),</span> <span class="s2">&quot;pc&quot;</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">IncompatibleDim</span><span class="p">(</span>
                <span class="s2">&quot;Can&#39;t convert that into percentage&quot;</span>
            <span class="p">)</span></div>

<div class="viewcode-block" id="Dim.tolpi"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.tolpi">[docs]</a>    <span class="k">def</span> <span class="nf">tolpi</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ceil</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the value of Dim in LPI unit.</span>
<span class="sd">        Raise ValueError if Dim is not convertable</span>

<span class="sd">        :type ceil: boolean</span>
<span class="sd">        :param ceil: If True, apply ceil to returned value</span>
<span class="sd">        :type obj: boolean</span>
<span class="sd">        :param obj: If True, returns a Dim object instead of value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;dpi&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convertorval</span><span class="p">(</span>
                <span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ceil</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">/</span> <span class="mi">16</span><span class="p">),</span> <span class="s2">&quot;lpi&quot;</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">IncompatibleDim</span><span class="p">(</span>
                <span class="s2">&quot;Can&#39;t convert that into LPI&quot;</span>
            <span class="p">)</span></div>

<div class="viewcode-block" id="Dim.tomm"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.tomm">[docs]</a>    <span class="k">def</span> <span class="nf">tomm</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ceil</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the value of Dim in milimeter unit.</span>

<span class="sd">        :type ceil: boolean</span>
<span class="sd">        :param ceil: If True, apply ceil to returned value</span>
<span class="sd">        :type obj: boolean</span>
<span class="sd">        :param obj: If True, returns a Dim object instead of value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_convertable_length</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">IncompatibleDim</span><span class="p">(</span>
                <span class="s2">&quot;Can&#39;t convert that into milimeters&quot;</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;pica&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convertorval</span><span class="p">(</span>
                <span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ceil</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">*</span> <span class="n">Dim</span><span class="o">.</span><span class="n">PICA_TO_MM</span><span class="p">,</span> <span class="n">ceil</span><span class="p">),</span> <span class="s2">&quot;mm&quot;</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="s2">&quot;mm&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convertorval</span><span class="p">(</span>
                <span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ceil</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">ceil</span><span class="p">),</span> <span class="s2">&quot;mm&quot;</span>
            <span class="p">)</span></div>

    <span class="c1">#--- Defaults --------------------------------------------------------</span>

<div class="viewcode-block" id="Dim.fromdefault"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.Dim.fromdefault">[docs]</a>    <span class="k">def</span> <span class="nf">fromdefault</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">default</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set Dim attributes according to a named default.</span>

<span class="sd">        :type default: str</span>
<span class="sd">        :param default: Name of the set of defaults.</span>
<span class="sd">        :rtype: boolean</span>
<span class="sd">        :return: boolean</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">default</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;a4-&quot;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">set_unit</span><span class="p">(</span><span class="s2">&quot;pica&quot;</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">default</span> <span class="o">==</span> <span class="s2">&quot;a4-width&quot;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">iso216paper</span><span class="o">.</span><span class="n">A4</span><span class="o">.</span><span class="n">WIDTH</span>

            <span class="k">if</span> <span class="n">default</span> <span class="o">==</span> <span class="s2">&quot;a4-height&quot;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">iso216paper</span><span class="o">.</span><span class="n">A4</span><span class="o">.</span><span class="n">HEIGHT</span>

        <span class="k">if</span> <span class="n">default</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;letter-&quot;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">set_unit</span><span class="p">(</span><span class="s2">&quot;pica&quot;</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">default</span> <span class="o">==</span> <span class="s2">&quot;letter-width&quot;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">ansipaper</span><span class="o">.</span><span class="n">Letter</span><span class="o">.</span><span class="n">WIDTH</span>

            <span class="k">if</span> <span class="n">default</span> <span class="o">==</span> <span class="s2">&quot;letter-height&quot;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">ansipaper</span><span class="o">.</span><span class="n">Letter</span><span class="o">.</span><span class="n">HEIGHT</span>

        <span class="k">return</span> <span class="kc">True</span></div>

    <span class="c1">#--- Python __ methods -----------------------------------------------</span>

    <span class="k">def</span> <span class="nf">__bool__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">r</span><span class="p">,</span><span class="n">u</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">),</span><span class="s2">&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="ow">in</span> <span class="n">Dim</span><span class="o">.</span><span class="n">UNIT_ARGS</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="n">short</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;mm&quot;</span><span class="p">:</span> <span class="s2">&quot;mm&quot;</span><span class="p">,</span> <span class="s2">&quot;perc&quot;</span><span class="p">:</span> <span class="s2">&quot;%&quot;</span><span class="p">,</span> <span class="s2">&quot;pica&quot;</span><span class="p">:</span> <span class="s2">&quot;pt&quot;</span><span class="p">,</span> <span class="s2">&quot;lpi&quot;</span><span class="p">:</span> <span class="s2">&quot;lpi&quot;</span><span class="p">}</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="ow">in</span> <span class="n">short</span><span class="p">:</span>
                <span class="n">u</span> <span class="o">=</span> <span class="n">short</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">unit</span><span class="p">]</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;cdeg&quot;</span><span class="p">,</span> <span class="s2">&quot;deg&quot;</span><span class="p">]:</span>
                <span class="n">u</span> <span class="o">=</span> <span class="s2">&quot;°&quot;</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">UnknownDimUnit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">unit</span><span class="p">)</span>

        <span class="k">return</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">u</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Dim(value=</span><span class="si">{}</span><span class="s2">, unit=</span><span class="si">{}</span><span class="s2">, is_int=</span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_int</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__float__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__int__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__iadd__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dim</span><span class="p">):</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dim</span><span class="p">,</span> <span class="nb">float</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">+=</span> <span class="n">dim</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dim</span><span class="p">,</span> <span class="n">Dim</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">dim</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">+=</span> <span class="n">dim</span><span class="o">.</span><span class="n">value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">IncompatibleDim</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">__isub__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dim</span><span class="p">):</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dim</span><span class="p">,</span> <span class="nb">float</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">-=</span> <span class="n">dim</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dim</span><span class="p">,</span> <span class="n">Dim</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">dim</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">-=</span> <span class="n">dim</span><span class="o">.</span><span class="n">value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">IncompatibleDim</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">__imul__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dim</span><span class="p">):</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dim</span><span class="p">,</span> <span class="nb">float</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">*=</span> <span class="n">dim</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dim</span><span class="p">,</span> <span class="n">Dim</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">dim</span><span class="o">.</span><span class="n">unit</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">*=</span> <span class="n">dim</span><span class="o">.</span><span class="n">value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">IncompatibleDim</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__sub__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dim</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__isub__</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__add__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dim</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__iadd__</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__mul__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dim</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__imul__</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span></div>


<div class="viewcode-block" id="DimBox"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.DimBox">[docs]</a><span class="k">class</span> <span class="nc">DimBox</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Box/rectangle object to manipulate Scribus frames coordinates.</span>

<span class="sd">    :type kwargs: dict</span>
<span class="sd">    :param kwargs: kwargs (see kwargs table)</span>

<span class="sd">    :ivar dict dims: Width and height of the box as Dim objects</span>
<span class="sd">    :ivar dict coords: Coordinates of the box, dict of list of</span>
<span class="sd">        Dim objects ([x, y]) for each point</span>
<span class="sd">    :ivar Dim rotation: Rotation angle of the box as Dim object</span>
<span class="sd">        (unit : degree)</span>
<span class="sd">    :ivar dict rotated_coords: Coordinates of the box when rotated by</span>
<span class="sd">        rotation degree</span>

<span class="sd">    +-------------------------+------------+</span>
<span class="sd">    | Box point coordinate    | kwargs key |</span>
<span class="sd">    +=========================+============+</span>
<span class="sd">    | Top left x position     | top_lx     |</span>
<span class="sd">    +-------------------------+------------+</span>
<span class="sd">    | Top left y position     | top_ly     |</span>
<span class="sd">    +-------------------------+------------+</span>
<span class="sd">    | Top right x position    | top_rx     |</span>
<span class="sd">    +-------------------------+------------+</span>
<span class="sd">    | Top right y position    | top_ry     |</span>
<span class="sd">    +-------------------------+------------+</span>
<span class="sd">    | Bottom right x position | bottom_rx  |</span>
<span class="sd">    +-------------------------+------------+</span>
<span class="sd">    | Bottom right y position | bottom_ry  |</span>
<span class="sd">    +-------------------------+------------+</span>
<span class="sd">    | Box width               | width      |</span>
<span class="sd">    +-------------------------+------------+</span>
<span class="sd">    | Box height              | height     |</span>
<span class="sd">    +-------------------------+------------+</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c1"># X,Y coordinates for each corner</span>
        <span class="c1"># Use setx, sety, getx, gety methods as shorthands</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">coords</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;top-left&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">)],</span>
            <span class="s2">&quot;top-right&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">)],</span>
            <span class="s2">&quot;bottom-left&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">)],</span>
            <span class="s2">&quot;bottom-right&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">)],</span>
        <span class="p">}</span>

        <span class="c1"># Height and width</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dims</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;width&quot;</span><span class="p">:</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="s2">&quot;height&quot;</span><span class="p">:</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">)}</span>

        <span class="c1"># Coords &amp; angle for rotated boxes --------------------------</span>
        <span class="c1"># NOTE rotated_coords is modified through set_box and rotate</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">rotation</span> <span class="o">=</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;deg&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rotated_coords</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;top-left&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">)],</span>
            <span class="s2">&quot;top-right&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">)],</span>
            <span class="s2">&quot;bottom-left&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">)],</span>
            <span class="s2">&quot;bottom-right&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">Dim</span><span class="p">(</span><span class="mi">0</span><span class="p">)],</span>
        <span class="p">}</span>

        <span class="c1"># -----------------------------------------------------------</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">set_box</span><span class="p">(</span><span class="n">kwargs</span><span class="o">=</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="c1">#--- Shorthands for corners coordinates ------------------------------</span>

    <span class="k">def</span> <span class="nf">_setxy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">corner</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">xy</span><span class="p">,</span> <span class="n">rotated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">corner</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="p">[</span>
                <span class="s2">&quot;top-left&quot;</span><span class="p">,</span> <span class="s2">&quot;top-right&quot;</span><span class="p">,</span>
                <span class="s2">&quot;bottom-left&quot;</span><span class="p">,</span> <span class="s2">&quot;bottom-right&quot;</span><span class="p">]:</span>

            <span class="k">if</span> <span class="n">xy</span> <span class="o">==</span> <span class="s2">&quot;x&quot;</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">rotated</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">rotated_coords</span><span class="p">[</span><span class="n">corner</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">[</span><span class="n">corner</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">rotated</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">rotated_coords</span><span class="p">[</span><span class="n">corner</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">[</span><span class="n">corner</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>

            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_getxy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">corner</span><span class="p">,</span> <span class="n">xy</span><span class="p">,</span> <span class="n">rotated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">corner</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="p">[</span>
                <span class="s2">&quot;top-left&quot;</span><span class="p">,</span> <span class="s2">&quot;top-right&quot;</span><span class="p">,</span>
                <span class="s2">&quot;bottom-left&quot;</span><span class="p">,</span> <span class="s2">&quot;bottom-right&quot;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">xy</span> <span class="o">==</span> <span class="s2">&quot;x&quot;</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">rotated</span><span class="p">:</span>
                    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rotated_coords</span><span class="p">[</span><span class="n">corner</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">[</span><span class="n">corner</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">rotated</span><span class="p">:</span>
                    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rotated_coords</span><span class="p">[</span><span class="n">corner</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">[</span><span class="n">corner</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">()</span>

<div class="viewcode-block" id="DimBox.setx"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.DimBox.setx">[docs]</a>    <span class="k">def</span> <span class="nf">setx</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">corner</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">rotated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_setxy</span><span class="p">(</span><span class="n">corner</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="s2">&quot;x&quot;</span><span class="p">,</span> <span class="n">rotated</span><span class="p">)</span></div>

<div class="viewcode-block" id="DimBox.sety"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.DimBox.sety">[docs]</a>    <span class="k">def</span> <span class="nf">sety</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">corner</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">rotated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_setxy</span><span class="p">(</span><span class="n">corner</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="s2">&quot;y&quot;</span><span class="p">,</span> <span class="n">rotated</span><span class="p">)</span></div>

<div class="viewcode-block" id="DimBox.getx"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.DimBox.getx">[docs]</a>    <span class="k">def</span> <span class="nf">getx</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">corner</span><span class="p">,</span> <span class="n">rotated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getxy</span><span class="p">(</span><span class="n">corner</span><span class="p">,</span> <span class="s2">&quot;x&quot;</span><span class="p">,</span> <span class="n">rotated</span><span class="p">)</span></div>

<div class="viewcode-block" id="DimBox.gety"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.DimBox.gety">[docs]</a>    <span class="k">def</span> <span class="nf">gety</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">corner</span><span class="p">,</span> <span class="n">rotated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getxy</span><span class="p">(</span><span class="n">corner</span><span class="p">,</span> <span class="s2">&quot;y&quot;</span><span class="p">,</span> <span class="n">rotated</span><span class="p">)</span></div>

    <span class="c1">#--- Box modification ------------------------------------------------</span>

<div class="viewcode-block" id="DimBox.move"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.DimBox.move">[docs]</a>    <span class="k">def</span> <span class="nf">move</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">posx</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">posy</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">refpoint</span><span class="o">=</span><span class="s2">&quot;top-left&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Move the box at posx, posy position.</span>

<span class="sd">        :type posx: float</span>
<span class="sd">        :param posx: New X position</span>
<span class="sd">        :type posy: float</span>
<span class="sd">        :param posy: New Y position</span>
<span class="sd">        :type refpoint: string</span>
<span class="sd">        :param refpoint: Coordinate point of reference (DimBox.coords key)</span>
<span class="sd">        :rtype: boolean</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">origin_x</span><span class="p">,</span><span class="n">origin_y</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span><span class="kc">None</span>

        <span class="k">if</span> <span class="n">refpoint</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="n">origin_x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">[</span><span class="n">refpoint</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
            <span class="n">origin_y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">[</span><span class="n">refpoint</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>

        <span class="k">if</span> <span class="n">origin_x</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">origin_y</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>

            <span class="k">if</span> <span class="n">origin_x</span> <span class="o">!=</span> <span class="n">posx</span> <span class="ow">or</span> <span class="n">origin_y</span> <span class="o">!=</span> <span class="n">posy</span><span class="p">:</span>
                <span class="c1"># Noving the box according to the reference point</span>

                <span class="k">if</span> <span class="n">refpoint</span> <span class="o">==</span> <span class="s2">&quot;top-left&quot;</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">set_box</span><span class="p">(</span>
                        <span class="n">top_lx</span><span class="o">=</span><span class="n">posx</span><span class="p">,</span> <span class="n">top_ly</span><span class="o">=</span><span class="n">posy</span><span class="p">,</span>
                        <span class="n">width</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dims</span><span class="p">[</span><span class="s2">&quot;width&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                        <span class="n">height</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dims</span><span class="p">[</span><span class="s2">&quot;height&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="p">)</span>

                    <span class="k">return</span> <span class="kc">True</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Moving the box at the exact same position is not a mistake</span>
                <span class="c1"># even if it&#39;s useless</span>
                <span class="k">return</span> <span class="kc">True</span>

        <span class="k">return</span> <span class="kc">False</span></div>

<div class="viewcode-block" id="DimBox.translate"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.DimBox.translate">[docs]</a>    <span class="k">def</span> <span class="nf">translate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">amountx</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">amounty</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">refpoint</span><span class="o">=</span><span class="s2">&quot;top-left&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Move the box by an amount of amountx, amounty</span>

<span class="sd">        :type amountx: float</span>
<span class="sd">        :param amountx: Amount of X translation</span>
<span class="sd">        :type amounty: float</span>
<span class="sd">        :param amounty: Amount of Y translation</span>
<span class="sd">        :type refpoint: string</span>
<span class="sd">        :param refpoint: Coordinate point of reference (DimBox.coords key)</span>
<span class="sd">        :rtype: boolean</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">origin_x</span><span class="p">,</span><span class="n">origin_y</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span><span class="kc">None</span>

        <span class="k">if</span> <span class="n">refpoint</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="n">origin_x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">[</span><span class="n">refpoint</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
            <span class="n">origin_y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">[</span><span class="n">refpoint</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>

        <span class="k">if</span> <span class="n">origin_x</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">origin_y</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>

            <span class="c1"># Translating the box by amountx, amounty</span>

            <span class="k">if</span> <span class="n">refpoint</span> <span class="o">==</span> <span class="s2">&quot;topleft&quot;</span><span class="p">:</span>
                <span class="n">npx</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">[</span><span class="s2">&quot;top-left&quot;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="n">amountx</span>
                <span class="n">npy</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">[</span><span class="s2">&quot;top-left&quot;</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="n">amounty</span>

                <span class="bp">self</span><span class="o">.</span><span class="n">set_box</span><span class="p">(</span>
                    <span class="n">top_lx</span><span class="o">=</span><span class="n">npx</span><span class="p">,</span> <span class="n">top_ly</span><span class="o">=</span><span class="n">npy</span><span class="p">,</span>
                    <span class="n">width</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dims</span><span class="p">[</span><span class="s2">&quot;width&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">height</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dims</span><span class="p">[</span><span class="s2">&quot;height&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="p">)</span>

                <span class="k">return</span> <span class="kc">True</span>

        <span class="k">return</span> <span class="kc">False</span></div>

<div class="viewcode-block" id="DimBox.set_box"><a class="viewcode-back" href="../../pyscribus.html#pyscribus.dimensions.DimBox.set_box">[docs]</a>    <span class="k">def</span> <span class="nf">set_box</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set all coordinates of the box from a set a coordinates</span>
<span class="sd">        and/or width &amp; height.</span>

<span class="sd">        +-------------------------+------------+</span>
<span class="sd">        | Box point coordinate    | kwargs key |</span>
<span class="sd">        +=========================+============+</span>
<span class="sd">        | Top left x position     | top_lx     |</span>
<span class="sd">        +-------------------------+------------+</span>
<span class="sd">        | Top left y position     | top_ly     |</span>
<span class="sd">        +-------------------------+------------+</span>
<span class="sd">        | Top right x position    | top_rx     |</span>
<span class="sd">        +-------------------------+------------+</span>
<span class="sd">        | Top right y position    | top_ry     |</span>
<span class="sd">        +-------------------------+------------+</span>
<span class="sd">        | Bottom right x position | bottom_rx  |</span>
<span class="sd">        +-------------------------+------------+</span>
<span class="sd">        | Bottom right y position | bottom_ry  |</span>
<span class="sd">        +-------------------------+------------+</span>
<span class="sd">        | Box width               | width      |</span>
<span class="sd">        +-------------------------+------------+</span>
<span class="sd">        | Box height              | height     |</span>
<span class="sd">        +-------------------------+------------+</span>

<span class="sd">        :rtype: boolean</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">def</span> <span class="nf">all_case</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            Define DimBox points from all corners.</span>

<span class="sd">            X-------X</span>
<span class="sd">            |       |</span>
<span class="sd">            |       |</span>
<span class="sd">            X-------X</span>

<span class="sd">            Height and width are deduced</span>

<span class="sd">            rtype: pyscribus.dimensions.DimBox</span>
<span class="sd">            &quot;&quot;&quot;</span>

            <span class="n">tlx</span><span class="p">,</span><span class="n">tly</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;top_lx&quot;</span><span class="p">],</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;top_ly&quot;</span><span class="p">]</span>
            <span class="n">brx</span><span class="p">,</span><span class="n">bry</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;bottom_rx&quot;</span><span class="p">],</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;bottom_ry&quot;</span><span class="p">]</span>

            <span class="n">obj</span><span class="o">.</span><span class="n">dims</span><span class="p">[</span><span class="s2">&quot;width&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">brx</span> <span class="o">-</span> <span class="n">tlx</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">dims</span><span class="p">[</span><span class="s2">&quot;height&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">bry</span> <span class="o">-</span> <span class="n">tly</span>

            <span class="n">obj</span><span class="o">.</span><span class="n">setx</span><span class="p">(</span><span class="s2">&quot;top-right&quot;</span><span class="p">,</span> <span class="n">brx</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">sety</span><span class="p">(</span><span class="s2">&quot;top-right&quot;</span><span class="p">,</span> <span class="n">tly</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">setx</span><span class="p">(</span><span class="s2">&quot;top-left&quot;</span><span class="p">,</span> <span class="n">tlx</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">sety</span><span class="p">(</span><span class="s2">&quot;top-left&quot;</span><span class="p">,</span> <span class="n">tly</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">setx</span><span class="p">(</span><span class="s2">&quot;bottom-left&quot;</span><span class="p">,</span> <span class="n">tlx</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">sety</span><span class="p">(</span><span class="s2">&quot;bottom-left&quot;</span><span class="p">,</span> <span class="n">bry</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">setx</span><span class="p">(</span><span class="s2">&quot;bottom-right&quot;</span><span class="p">,</span> <span class="n">brx</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">sety</span><span class="p">(</span><span class="s2">&quot;bottom-right&quot;</span><span class="p">,</span> <span class="n">bry</span><span class="p">)</span>

            <span class="k">return</span> <span class="n">obj</span>

        <span class="k">def</span> <span class="nf">from_tr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            Define DimBox points from top-right corner.</span>

<span class="sd">            &lt;--------X</span>
<span class="sd">                     |</span>
<span class="sd">                     |</span>
<span class="sd">                     v</span>

<span class="sd">            rtype: pyscribus.dimensions.DimBox</span>
<span class="sd">            &quot;&quot;&quot;</span>

            <span class="c1"># Troy to avoid using try and &quot;tory&quot;.</span>
            <span class="c1"># I don’t care about UK parties.</span>
            <span class="n">trox</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;top_rx&quot;</span><span class="p">])</span>
            <span class="n">troy</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;top_ry&quot;</span><span class="p">])</span>
            <span class="n">width</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;width&quot;</span><span class="p">])</span>
            <span class="n">height</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;height&quot;</span><span class="p">])</span>

            <span class="n">obj</span><span class="o">.</span><span class="n">dims</span><span class="p">[</span><span class="s2">&quot;width&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">width</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">dims</span><span class="p">[</span><span class="s2">&quot;height&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">height</span>

            <span class="n">obj</span><span class="o">.</span><span class="n">setx</span><span class="p">(</span><span class="s2">&quot;top-right&quot;</span><span class="p">,</span> <span class="n">trox</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">sety</span><span class="p">(</span><span class="s2">&quot;top-right&quot;</span><span class="p">,</span> <span class="n">troy</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">setx</span><span class="p">(</span><span class="s2">&quot;top-left&quot;</span><span class="p">,</span> <span class="n">trox</span> <span class="o">-</span> <span class="n">width</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">sety</span><span class="p">(</span><span class="s2">&quot;top-left&quot;</span><span class="p">,</span> <span class="n">troy</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">setx</span><span class="p">(</span><span class="s2">&quot;bottom-left&quot;</span><span class="p">,</span> <span class="n">trox</span> <span class="o">-</span> <span class="n">width</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">sety</span><span class="p">(</span><span class="s2">&quot;bottom-left&quot;</span><span class="p">,</span> <span class="n">troy</span> <span class="o">+</span> <span class="n">height</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">setx</span><span class="p">(</span><span class="s2">&quot;bottom-right&quot;</span><span class="p">,</span> <span class="n">trox</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">sety</span><span class="p">(</span><span class="s2">&quot;bottom-right&quot;</span><span class="p">,</span> <span class="n">troy</span> <span class="o">+</span> <span class="n">height</span><span class="p">)</span>

            <span class="k">return</span> <span class="n">obj</span>

        <span class="k">def</span> <span class="nf">from_tl</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            Define DimBox points from top-left corner.</span>

<span class="sd">            X-------&gt;</span>
<span class="sd">            |</span>
<span class="sd">            |</span>
<span class="sd">            v</span>

<span class="sd">            rtype: pyscribus.dimensions.DimBox</span>
<span class="sd">            &quot;&quot;&quot;</span>
For faster browsing, not all history is shown. View entire blame