xref: /plugin/dw2pdf/src/Styles.php (revision f2000117f688f925c0f5fdc1ec614c297f3d0398)
1c5c184fdSAndreas Gohr<?php
2c5c184fdSAndreas Gohr
3c5c184fdSAndreas Gohrnamespace dokuwiki\plugin\dw2pdf\src;
4c5c184fdSAndreas Gohr
5081fc334SAndreas Gohruse dokuwiki\StyleUtils;
6081fc334SAndreas Gohr
7081fc334SAndreas Gohrclass Styles
8081fc334SAndreas Gohr{
9081fc334SAndreas Gohr    protected Config $config;
10c5c184fdSAndreas Gohr
11081fc334SAndreas Gohr    /**
12081fc334SAndreas Gohr     * @param Config $config
13081fc334SAndreas Gohr     */
14081fc334SAndreas Gohr    public function __construct(Config $config)
15081fc334SAndreas Gohr    {
16081fc334SAndreas Gohr        $this->config = $config;
17081fc334SAndreas Gohr    }
18081fc334SAndreas Gohr
19081fc334SAndreas Gohr    /**
20081fc334SAndreas Gohr     * Get the full CSS to include in the PDF
21081fc334SAndreas Gohr     *
22081fc334SAndreas Gohr     * Gathers all relevant CSS files, applies style replacements and parses LESS.
23081fc334SAndreas Gohr     *
24081fc334SAndreas Gohr     * @return string
25081fc334SAndreas Gohr     */
26081fc334SAndreas Gohr    public function getCSS(): string
27081fc334SAndreas Gohr    {
28081fc334SAndreas Gohr        //reuse the CSS dispatcher functions without triggering the main function
29*f2000117SAndreas Gohr        if (!defined('SIMPLE_TEST')) {
30081fc334SAndreas Gohr            define('SIMPLE_TEST', 1);
31*f2000117SAndreas Gohr        }
32081fc334SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
33081fc334SAndreas Gohr
34081fc334SAndreas Gohr        // prepare CSS files
35081fc334SAndreas Gohr        $files = $this->getStyleFiles();
36081fc334SAndreas Gohr        $css = '';
37081fc334SAndreas Gohr        foreach ($files as $file => $location) {
38081fc334SAndreas Gohr            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
39081fc334SAndreas Gohr            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
40081fc334SAndreas Gohr            $css .= css_loadfile($file, $location);
41081fc334SAndreas Gohr        }
42081fc334SAndreas Gohr
43081fc334SAndreas Gohr        // apply style replacements
44081fc334SAndreas Gohr        $styleUtils = new StyleUtils();
45081fc334SAndreas Gohr        $styleini = $styleUtils->cssStyleini();
46081fc334SAndreas Gohr        $css = css_applystyle($css, $styleini['replacements']);
47081fc334SAndreas Gohr
48081fc334SAndreas Gohr        // parse less
49081fc334SAndreas Gohr        return css_parseless($css);
50081fc334SAndreas Gohr    }
51081fc334SAndreas Gohr
52081fc334SAndreas Gohr
53081fc334SAndreas Gohr    /**
54081fc334SAndreas Gohr     * Returns the list of style files to include in the PDF
55081fc334SAndreas Gohr     *
56081fc334SAndreas Gohr     * The array keys are the file paths on disk, the values are the
57081fc334SAndreas Gohr     * paths as used inside the Styles (for resolving relative links).
58081fc334SAndreas Gohr     *
59081fc334SAndreas Gohr     * @return array<string,string>
60081fc334SAndreas Gohr     */
61081fc334SAndreas Gohr    protected function getStyleFiles(): array
62081fc334SAndreas Gohr    {
63081fc334SAndreas Gohr        $tpl = $this->config->getTemplateName();
64081fc334SAndreas Gohr
65081fc334SAndreas Gohr        return array_merge(
66081fc334SAndreas Gohr            [
67081fc334SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css' => DOKU_BASE . 'lib/styles/',
68081fc334SAndreas Gohr                DOKU_INC . 'lib/styles/print.css' => DOKU_BASE . 'lib/styles/',
69081fc334SAndreas Gohr            ],
70081fc334SAndreas Gohr            $this->getExtensionStyles(),
71081fc334SAndreas Gohr            [
72081fc334SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css' => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
73081fc334SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/style.css' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $tpl . '/',
74081fc334SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
75081fc334SAndreas Gohr            ]
76081fc334SAndreas Gohr        );
77081fc334SAndreas Gohr    }
78081fc334SAndreas Gohr
79081fc334SAndreas Gohr    /**
80081fc334SAndreas Gohr     * Returns a list of possible Plugin and Template PDF Styles
81081fc334SAndreas Gohr     *
82081fc334SAndreas Gohr     * Checks for a pdf.css, falls back to print.css. For configured usestyles plugins
83081fc334SAndreas Gohr     * the screen.css and style.css are also included.
84081fc334SAndreas Gohr     */
85081fc334SAndreas Gohr    protected function getExtensionStyles()
86081fc334SAndreas Gohr    {
87081fc334SAndreas Gohr        $list = [];
88081fc334SAndreas Gohr        $plugins = plugin_list();
89081fc334SAndreas Gohr
90081fc334SAndreas Gohr        $usestyle = $this->config->getStyledExtensions();
91081fc334SAndreas Gohr        foreach ($plugins as $p) {
92081fc334SAndreas Gohr            if (in_array($p, $usestyle)) {
93081fc334SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
94081fc334SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/";
95081fc334SAndreas Gohr
96081fc334SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
97081fc334SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/";
98081fc334SAndreas Gohr            }
99081fc334SAndreas Gohr
100081fc334SAndreas Gohr            $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/";
101081fc334SAndreas Gohr            $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/";
102081fc334SAndreas Gohr
103081fc334SAndreas Gohr            if (file_exists(DOKU_PLUGIN . "$p/pdf.css") || file_exists(DOKU_PLUGIN . "$p/pdf.less")) {
104081fc334SAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
105081fc334SAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/";
106081fc334SAndreas Gohr            } else {
107081fc334SAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
108081fc334SAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/";
109081fc334SAndreas Gohr            }
110081fc334SAndreas Gohr        }
111081fc334SAndreas Gohr
112081fc334SAndreas Gohr        // template support
113081fc334SAndreas Gohr        foreach (
114081fc334SAndreas Gohr            [
115081fc334SAndreas Gohr                'pdf.css',
116081fc334SAndreas Gohr                'pdf.less',
117081fc334SAndreas Gohr                'css/pdf.css',
118081fc334SAndreas Gohr                'css/pdf.less',
119081fc334SAndreas Gohr                'styles/pdf.css',
120081fc334SAndreas Gohr                'styles/pdf.less'
121081fc334SAndreas Gohr            ] as $file
122081fc334SAndreas Gohr        ) {
123081fc334SAndreas Gohr            if (file_exists(tpl_incdir() . $file)) {
124081fc334SAndreas Gohr                $list[tpl_incdir() . $file] = tpl_basedir() . $file;
125081fc334SAndreas Gohr            }
126081fc334SAndreas Gohr        }
127081fc334SAndreas Gohr
128081fc334SAndreas Gohr        return $list;
129081fc334SAndreas Gohr    }
130c5c184fdSAndreas Gohr}
131