1# -*- coding: utf-8 -*-
2from reportlab.lib.pagesizes import (A0, A1, A2, A3, A4, A5, A6, B0, B1, B2, B3,
3                                     B4, B5, B6, LETTER, LEGAL, ELEVENSEVENTEEN)
4
5from xhtml2pdf import util
6
7# Copyright 2010 Dirk Holtwick, holtwick.it
8#
9# Licensed under the Apache License, Version 2.0 (the "License");
10# you may not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13#     http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS,
17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20
21PML_WARNING = "warning"
22PML_ERROR = "error"
23PML_EXCEPTION = "PML Exception"
24PML_PREFIX = "pdf:"
25
26#CLASS   = 1
27BOOL = 2
28FONT = 3
29COLOR = 4
30FILE = 5
31SIZE = 6
32INT = 7
33STRING = 8
34BOX = 9
35POS = 10
36#STYLE   = 11
37MUST = 23
38
39"""
40Definition of all known tags. Also used for building the reference
41"""
42
43TAGS = {
44
45    # FORMAT
46
47    #"document": (1, {
48    #    "format":               (["a0", "a1", "a2", "a3", "a4", "a5", "a6",
49    #                              "b0", "b1", "b2", "b3", "b4", "b5", "b6",
50    #                              "letter", "legal", "elevenseventeen"], "a4"),
51    #    "orientation":          ["portrait", "landscape"],
52    #    "fullscreen":           (BOOL, "0"),
53    #    "author":               (STRING, ""),
54    #    "subject":              (STRING, ""),
55    #    "title":                (STRING, ""),
56    #    "duration":             INT,
57    #    "showoutline":          (BOOL, "0"),
58    #    "outline":              INT,
59    #    }),
60
61    "pdftemplate": (1, {
62        "name": (STRING, "body"),
63        "format": (["a0", "a1", "a2", "a3", "a4", "a5", "a6",
64                    "b0", "b1", "b2", "b3", "b4", "b5", "b6",
65                    "letter", "legal", "elevenseventeen"], "a4"),
66        "orientation": ["portrait", "landscape"],
67        "background": FILE,
68    }),
69
70    "pdfframe": (0, {
71        "name": (STRING, ""),
72        "box": (BOX, MUST),
73        "border": (BOOL, "0"),
74        "static": (BOOL, "0"),
75    }),
76
77    #"static": (1, {
78    #    "name":                 STRING,
79    #    "box":                  (BOX, MUST),
80    #    "border":               (BOOL, "0"),
81    #    }),
82
83    "pdfnexttemplate": (0, {
84        "name": (STRING, "body"),
85    }),
86
87    "pdfnextpage": (0, {
88        "name": (STRING, ""),
89        # "background":           FILE,
90    }),
91
92    "pdfnextframe": (0, {}),
93
94    "pdffont": (0, {
95        "src": (FILE, MUST),
96        "name": (STRING, MUST),
97        # "print":                (BOOL, "0"),
98        "encoding": (STRING, "WinAnsiEncoding"),
99    }),
100
101    "pdflanguage": (0, {
102        "name": (STRING, ""),
103    }),
104
105    "pdfdrawline": (0, {
106        "from": (POS, MUST),
107        "to": (POS, MUST),
108        "color": (COLOR, "#000000"),
109        "width": (SIZE, 1),
110    }),
111
112    "drawpoint": (0, {
113        "pos": (POS, MUST),
114        "color": (COLOR, "#000000"),
115        "width": (SIZE, 1),
116    }),
117
118    "pdfdrawlines": (0, {
119        "coords": (STRING, MUST),
120        "color": (COLOR, "#000000"),
121        "width": (SIZE, 1),
122    }),
123
124    "pdfdrawstring": (0, {
125        "pos": (POS, MUST),
126        "text": (STRING, MUST),
127        "color": (COLOR, "#000000"),
128        "align": (["left", "center", "right"], "right"),
129        "valign": (["top", "middle", "bottom"], "bottom"),
130        # "class":                CLASS,
131        "rotate": (INT, "0"),
132    }),
133
134    "pdfdrawimg": (0, {
135        "pos": (POS, MUST),
136        "src": (FILE, MUST),
137        "width": SIZE,
138        "height": SIZE,
139        "align": (["left", "center", "right"], "right"),
140        "valign": (["top", "middle", "bottom"], "bottom"),
141    }),
142
143    "pdfspacer": (0, {
144        "height": (SIZE, MUST),
145    }),
146
147    "pdfpagenumber": (0, {
148        "example": (STRING, "0"),
149    }),
150
151    "pdfpagecount": (0, {
152    }),
153
154    "pdftoc": (0, {
155    }),
156
157    "pdfversion": (0, {
158    }),
159
160    "pdfkeeptogether": (1, {
161    }),
162
163    "pdfkeepinframe": (1, {
164        "maxwidth": SIZE,
165        "maxheight": SIZE,
166        "mergespace": (INT, 1),
167        "mode": (["error", "overflow", "shrink", "truncate"], "shrink"),
168        "name": (STRING, "")
169    }),
170
171    # The chart example, see pml_charts
172    "pdfchart": (1, {
173        "type": (["spider", "bar"], "bar"),
174        "strokecolor": (COLOR, "#000000"),
175        "width": (SIZE, MUST),
176        "height": (SIZE, MUST),
177    }),
178
179    "pdfchartdata": (0, {
180        "set": (STRING, MUST),
181        "value": (STRING),
182        # "label":                (STRING),
183        "strokecolor": (COLOR),
184        "fillcolor": (COLOR),
185        "strokewidth": (SIZE),
186    }),
187
188    "pdfchartlabel": (0, {
189        "value": (STRING, MUST),
190    }),
191
192    "pdfbarcode": (0, {
193        "value": (STRING, MUST),
194        "type": (["i2of5", "itf",
195                  "code39", "extendedcode39",
196                  "code93", "extendedcode93",
197                  "msi",
198                  "codabar", "nw7",
199                  "code11",
200                  "fim",
201                  "postnet",
202                  "usps4s",
203                  "code128",
204                  "ean13", "ean8",
205                  "qr",
206                 ], "code128"),
207        "humanreadable": (STRING, "0"),
208        "vertical": (STRING, "0"),
209        "checksum": (STRING, "1"),
210        "barwidth": SIZE,
211        "barheight": SIZE,
212        "fontsize": SIZE,
213        "align": (["baseline", "top", "middle", "bottom"], "baseline"),
214    }),
215
216    # ========================================================
217
218    "link": (0, {
219        "href": (STRING, MUST),
220        "rel": (STRING, ""),
221        "type": (STRING, ""),
222        "media": (STRING, "all"),
223        "charset": (STRING, "latin1"), # XXX Must be something else...
224    }),
225
226    "meta": (0, {
227        "name": (STRING, ""),
228        "content": (STRING, ""),
229    }),
230
231    "style": (0, {
232        "type": (STRING, ""),
233        "media": (STRING, "all"),
234    }),
235
236    "img": (0, {
237        "src": (FILE, MUST),
238        "width": SIZE,
239        "height": SIZE,
240        "align": ["top", "middle", "bottom", "left", "right",
241                  "texttop", "absmiddle", "absbottom", "baseline"],
242    }),
243
244    "table": (1, {
245        "align": (["left", "center", "right"], "left"),
246        "valign": (["top", "bottom", "middle"], "middle"),
247        "border": (SIZE, "0"),
248        "bordercolor": (COLOR, "#000000"),
249        "bgcolor": COLOR,
250        "cellpadding": (SIZE, "0"),
251        "cellspacing": (SIZE, "0"),
252        "repeat": (INT, "0"), # XXX Remove this! Set to 0
253        "width": STRING,
254        #"keepmaxwidth":         SIZE,
255        #"keepmaxheight":        SIZE,
256        #"keepmergespace":       (INT, 1),
257        #"keepmode":             (["error", "overflow", "shrink", "truncate"], "shrink"),
258    }),
259
260    "tr": (1, {
261        "bgcolor": COLOR,
262        "valign": ["top", "bottom", "middle"],
263        "border": SIZE,
264        "bordercolor": (COLOR, "#000000"),
265    }),
266
267    "td": (1, {
268        "align": ["left", "center", "right", "justify"],
269        "valign": ["top", "bottom", "middle"],
270        "width": STRING,
271        "bgcolor": COLOR,
272        "border": SIZE,
273        "bordercolor": (COLOR, "#000000"),
274        "colspan": INT,
275        "rowspan": INT,
276    }),
277
278    "th": (1, {
279        "align": ["left", "center", "right", "justify"],
280        "valign": ["top", "bottom", "middle"],
281        "width": STRING,
282        "bgcolor": COLOR,
283        "border": SIZE,
284        "bordercolor": (COLOR, "#000000"),
285        "colspan": INT,
286        "rowspan": INT,
287    }),
288
289    "dl": (1, {
290    }),
291
292    "dd": (1, {
293    }),
294
295    "dt": (1, {
296    }),
297
298    "ol": (1, {
299        "type": (["1", "a", "A", "i", "I"], "1"),
300    }),
301
302    "ul": (1, {
303        "type": (["circle", "disk", "square"], "disk"),
304    }),
305
306    "li": (1, {
307    }),
308
309    "hr": (0, {
310        "color": (COLOR, "#000000"),
311        "size": (SIZE, "1"),
312        "width": STRING,
313        "align": ["left", "center", "right", "justify"],
314    }),
315
316    "div": (1, {
317        "align": ["left", "center", "right", "justify"],
318    }),
319
320    "p": (1, {
321        "align": ["left", "center", "right", "justify"],
322    }),
323
324    "br": (0, {
325    }),
326
327    "h1": (1, {
328        "outline": STRING,
329        "closed": (INT, 0),
330        "align": ["left", "center", "right", "justify"],
331    }),
332
333    "h2": (1, {
334        "outline": STRING,
335        "closed": (INT, 0),
336        "align": ["left", "center", "right", "justify"],
337    }),
338
339    "h3": (1, {
340        "outline": STRING,
341        "closed": (INT, 0),
342        "align": ["left", "center", "right", "justify"],
343    }),
344
345    "h4": (1, {
346        "outline": STRING,
347        "closed": (INT, 0),
348        "align": ["left", "center", "right", "justify"],
349    }),
350
351    "h5": (1, {
352        "outline": STRING,
353        "closed": (INT, 0),
354        "align": ["left", "center", "right", "justify"],
355    }),
356
357    "h6": (1, {
358        "outline": STRING,
359        "closed": (INT, 0),
360        "align": ["left", "center", "right", "justify"],
361    }),
362
363    "font": (1, {
364        "face": FONT,
365        "color": COLOR,
366        "size": STRING,
367    }),
368
369    "a": (1, {
370        "href": STRING,
371        "name": STRING,
372    }),
373
374    "input": (0, {
375        "name": STRING,
376        "value": STRING,
377        "type": (["text", "hidden", "checkbox"], "text"),
378    }),
379
380    "textarea": (1, {
381        "name": STRING,
382    }),
383
384    "select": (1, {
385        "name": STRING,
386        "value": STRING,
387    }),
388
389    "option": (0, {
390        "value": STRING,
391    }),
392}
393
394# XXX use "html" not "*" as default!
395DEFAULT_CSS = """
396html {
397    font-family: Helvetica;
398    font-size: 10px;
399    font-weight: normal;
400    color: #000000;
401    background-color: transparent;
402    margin: 0;
403    padding: 0;
404    line-height: 150%;
405    border: 1px none;
406    display: inline;
407    width: auto;
408    height: auto;
409    white-space: normal;
410}
411
412b,
413strong {
414    font-weight: bold;
415}
416
417i,
418em {
419    font-style: italic;
420}
421
422u {
423    text-decoration: underline;
424}
425
426s,
427strike {
428    text-decoration: line-through;
429}
430
431a {
432    text-decoration: underline;
433    color: blue;
434}
435
436ins {
437    color: green;
438    text-decoration: underline;
439}
440del {
441    color: red;
442    text-decoration: line-through;
443}
444
445pre,
446code,
447kbd,
448samp,
449tt {
450    font-family: "Courier New";
451}
452
453h1,
454h2,
455h3,
456h4,
457h5,
458h6 {
459    font-weight:bold;
460    -pdf-outline: true;
461    -pdf-outline-open: false;
462}
463
464h1 {
465    /*18px via YUI Fonts CSS foundation*/
466    font-size:138.5%;
467    -pdf-outline-level: 0;
468}
469
470h2 {
471    /*16px via YUI Fonts CSS foundation*/
472    font-size:123.1%;
473    -pdf-outline-level: 1;
474}
475
476h3 {
477    /*14px via YUI Fonts CSS foundation*/
478    font-size:108%;
479    -pdf-outline-level: 2;
480}
481
482h4 {
483    -pdf-outline-level: 3;
484}
485
486h5 {
487    -pdf-outline-level: 4;
488}
489
490h6 {
491    -pdf-outline-level: 5;
492}
493
494h1,
495h2,
496h3,
497h4,
498h5,
499h6,
500p,
501pre,
502hr {
503    margin:1em 0;
504}
505
506address,
507blockquote,
508body,
509center,
510dl,
511dir,
512div,
513fieldset,
514form,
515h1,
516h2,
517h3,
518h4,
519h5,
520h6,
521hr,
522isindex,
523menu,
524noframes,
525noscript,
526ol,
527p,
528pre,
529table,
530th,
531tr,
532td,
533ul,
534li,
535dd,
536dt,
537pdftoc {
538    display: block;
539}
540
541table {
542}
543
544tr,
545th,
546td {
547
548    vertical-align: middle;
549    width: auto;
550}
551
552th {
553    text-align: center;
554    font-weight: bold;
555}
556
557center {
558    text-align: center;
559}
560
561big {
562    font-size: 125%;
563}
564
565small {
566    font-size: 75%;
567}
568
569
570ul {
571    margin-left: 1.5em;
572    list-style-type: disc;
573}
574
575ul ul {
576    list-style-type: circle;
577}
578
579ul ul ul {
580    list-style-type: square;
581}
582
583ol {
584    list-style-type: decimal;
585    margin-left: 1.5em;
586}
587
588pre {
589    white-space: pre;
590}
591
592blockquote {
593    margin-left: 1.5em;
594    margin-right: 1.5em;
595}
596
597noscript {
598    display: none;
599}
600"""
601
602DEFAULT_LANGUAGE_LIST = {
603    "arabic": "arabic",
604    "hebrew": "hebrew",
605    "persian": "persian",
606    "urdu": "urdu",
607    "pashto": "pashto",
608    "sindhi": "sindhi"
609}
610
611DEFAULT_ASIAN_FONT = util.get_default_asian_font()
612
613DEFAULT_FONT = {
614    "courier": "Courier",
615    "courier-bold": "Courier-Bold",
616    "courier-boldoblique": "Courier-BoldOblique",
617    "courier-oblique": "Courier-Oblique",
618    "helvetica": "Helvetica",
619    "helvetica-bold": "Helvetica-Bold",
620    "helvetica-boldoblique": "Helvetica-BoldOblique",
621    "helvetica-oblique": "Helvetica-Oblique",
622    "times": "Times-Roman",
623    "times-roman": "Times-Roman",
624    "times-bold": "Times-Bold",
625    "times-boldoblique": "Times-BoldOblique",
626    "times-oblique": "Times-Oblique",
627    "symbol": "Symbol",
628    "zapfdingbats": "ZapfDingbats",
629    "zapf-dingbats": "ZapfDingbats",
630
631    # Alias
632    "arial": "Helvetica",
633    "times new roman": "Times-Roman",
634    "georgia": "Times-Roman",
635    'serif': 'Times-Roman',
636    'sansserif': 'Helvetica',
637    'sans': 'Helvetica',
638    'monospaced': 'Courier',
639    'monospace': 'Courier',
640    'mono': 'Courier',
641    'courier new': 'Courier',
642    'verdana': 'Helvetica',
643    'geneva': 'Helvetica',
644}
645
646PML_PAGESIZES = {
647    "a0": A0,
648    "a1": A1,
649    "a2": A2,
650    "a3": A3,
651    "a4": A4,
652    "a5": A5,
653    "a6": A6,
654    "b0": B0,
655    "b1": B1,
656    "b2": B2,
657    "b3": B3,
658    "b4": B4,
659    "b5": B5,
660    "b6": B6,
661    "letter": LETTER,
662    "legal": LEGAL,
663    "ledger": ELEVENSEVENTEEN,
664    "elevenseventeen": ELEVENSEVENTEEN,
665}
666