@@ -45,21 +45,38 @@ public function setRows($rows, $additional = null, $first_page = null)
4545 $ this ->max_rows_first_page = $ first_page ?? $ this ->max_rows_first_page ;
4646 }
4747
48+ // Costanti per i fattori di scala ottimizzati
49+ private const MULTILINE_FACTOR = 0.8 ;
50+ private const SMALL_TEXT_FACTOR = 0.65 ;
51+
4852 public function count ($ text , $ small = null )
4953 {
5054 $ count = 0 ;
51- $ textLines = explode ("\n" , (string ) $ text );
55+ $ text = (string ) $ text ;
56+
57+ // Usa mb_strlen per supporto multibyte corretto
58+ if (empty ($ text )) {
59+ $ this ->set (0 );
60+ return ;
61+ }
62+
63+ $ textLines = explode ("\n" , $ text );
5264
5365 foreach ($ textLines as $ line ) {
54- $ count += ceil (strlen ($ line ) / $ this ->char_number );
66+ // Usa mb_strlen per caratteri multibyte
67+ $ line_length = mb_strlen (trim ($ line ), 'UTF-8 ' );
68+ if ($ line_length > 0 ) {
69+ $ count += ceil ($ line_length / $ this ->char_number );
70+ }
5571 }
5672
73+ // Applica fattori di scala ottimizzati
5774 if ($ count > 1 ) {
58- $ count /= 1.25 ;
75+ $ count *= self :: MULTILINE_FACTOR ;
5976 }
6077
6178 if ($ small ) {
62- $ count /= 1.538461538 ;
79+ $ count *= self :: SMALL_TEXT_FACTOR ;
6380 }
6481
6582 $ this ->set ($ count );
@@ -76,50 +93,68 @@ public function next()
7693 $ this ->current = 0 ;
7794 }
7895
79- public function getAdditionalNumber ()
96+ /**
97+ * Reset completo dello stato dell'oggetto.
98+ * Utile per riutilizzare la stessa istanza per documenti diversi.
99+ */
100+ public function reset ()
80101 {
81- if ($ this ->space <= $ this ->max_rows ) {
82- $ page = 1 ;
83- } else {
84- if ($ this ->space <= $ this ->max_rows_first_page ) {
85- $ page = 2 ;
86- } else {
87- $ page = ceil (1 + (($ this ->space - $ this ->max_rows_first_page ) / $ this ->max_rows ));
88- }
89- }
102+ $ this ->space = 0 ;
103+ $ this ->current = 0 ;
104+ }
90105
91- if ($ page > 1 ) {
92- $ rows = $ this ->space - $ this ->max_rows_first_page * ($ page - 1 );
106+ public function getAdditionalNumber ()
107+ {
108+ $ space = $ this ->space ;
109+ $ number = 0 ;
110+
111+ // Algoritmo per il calcolo delle pagine
112+ if ($ space <= $ this ->max_rows ) {
113+ // Prima pagina - calcolo diretto
114+ $ number = max (0 , $ this ->max_rows - ceil ($ space ));
115+ } elseif ($ space <= $ this ->max_rows_first_page ) {
116+ // Seconda pagina - calcolo diretto
117+ $ number = max (0 , $ this ->max_rows - ceil ($ space - $ this ->max_rows_first_page ));
93118 } else {
94- $ rows = ceil ($ this ->space );
119+ // Pagine successive - calcolo
120+ $ remaining_space = $ space - $ this ->max_rows_first_page ;
121+ $ additional_pages = ceil ($ remaining_space / $ this ->max_rows );
122+ $ rows_on_last_page = $ remaining_space - (($ additional_pages - 1 ) * $ this ->max_rows );
123+ $ number = max (0 , $ this ->max_rows - ceil ($ rows_on_last_page ));
95124 }
96125
97- $ number = $ this ->max_rows - $ rows ;
98-
99126 return $ number ;
100127 }
101128
102129 public function generate ()
103130 {
104131 $ this ->next ();
105132
106- $ result = '' ;
107-
108133 $ number = $ this ->getAdditionalNumber ();
109134
110- for ($ i = 0 ; $ i < $ number ; ++$ i ) {
111- $ result .= '
112- <tr> ' ;
113-
114- for ($ c = 0 ; $ c < $ this ->column_number ; ++$ c ) {
115- $ result .= '
116- <td> </td> ' ;
117- }
135+ // Se non ci sono righe da generare, ritorna stringa vuota
136+ if ($ number <= 0 ) {
137+ return '' ;
138+ }
118139
119- $ result .= '
120- </tr> ' ;
140+ // Genera template per una singola riga
141+ $ single_row_template = "\n <tr> " ;
142+ for ($ c = 0 ; $ c < $ this ->column_number ; ++$ c ) {
143+ $ single_row_template .= "\n <td> </td> " ;
121144 }
145+ $ single_row_template .= "\n </tr> " ;
122146
123- return $ result ;
147+ // Usa str_repeat per generazione
148+ return str_repeat ($ single_row_template , $ number );
149+ }
150+
151+ /**
152+ * Restituisce lo spazio attualmente utilizzato.
153+ *
154+ * @return float
155+ */
156+ public function getSpace ()
157+ {
158+ return $ this ->space ;
124159 }
125160}
0 commit comments