1
+ <?php
2
+
3
+ namespace App \Models \Labels \Tapes \Generic ;
4
+
5
+ use App \Helpers \Helper ;
6
+
7
+ abstract class Continuous_Landscape_0_59in extends GenericTape
8
+ {
9
+ // abstract class for printers using 0.59in width paper in landscape orientation
10
+ // Using a larger TAPE_WIDTH value to increase font sizes
11
+ protected const TAPE_WIDTH = 0.59 ;
12
+ private float $ tapeHeight ;
13
+ protected float $ minHeight = 1 ;
14
+
15
+ /**
16
+ * @param float $length Length of the label in inches (default 2.36in which is 60mm)
17
+ * @param bool $continuous Whether the tape is continuous or pre-cut
18
+ * @param float $spacing Spacing between labels for non-continuous tapes (in inches)
19
+ */
20
+ public function __construct ($ length = 0.6 , $ continuous = true , $ spacing = 0.0 ) {
21
+ // Swap width and height for landscape orientation
22
+ // The height becomes the width, and the length becomes the height
23
+ parent ::__construct ($ length , self ::TAPE_WIDTH , $ continuous , $ spacing );
24
+ $ this ->tapeHeight = $ length ;
25
+
26
+ $ this ->marginTop = 0.1 ;
27
+ $ this ->marginBottom = 0.1 ;
28
+ // Keep small horizontal margins
29
+ $ this ->marginLeft = self ::TAPE_WIDTH * 0.2 ;
30
+ // $this->marginRight = self::TAPE_WIDTH * 0.1;
31
+
32
+ // Override font sizes to make them larger
33
+ // Calculate a larger base font size (3x the default)
34
+ $ baseFontSize = self ::TAPE_WIDTH * 0.16 ; // 3x the default 0.07
35
+
36
+ // Recalculate all element sizing based on the larger base font size
37
+ $ this ->titleSize = $ baseFontSize ; // Same as base font size
38
+ $ this ->titleMargin = $ baseFontSize * 0.3 ; // 30% of base font size
39
+ $ this ->fieldSize = $ baseFontSize * 1.1 ; // 110% of base font size
40
+ $ this ->fieldMargin = $ baseFontSize * 0.1 ; // 10% of base font size
41
+ $ this ->labelSize = $ baseFontSize * 0.7 ; // 70% of base font size
42
+ $ this ->labelMargin = $ baseFontSize * -0.1 ; // -10% of base font size
43
+ $ this ->barcodeMargin = $ baseFontSize * 0.9 ; // 20% of base font size
44
+ $ this ->tagSize = $ baseFontSize * 0.8 ; // 80% of base font size
45
+ }
46
+
47
+ public function getBarcodeRatio () {
48
+ return 1.0 ; // Barcode should use 100% of available height
49
+ }
50
+
51
+ /**
52
+ * Calculate the required length for the content
53
+ *
54
+ * @param $record The record to calculate length for
55
+ * @return float The calculated length in inches
56
+ */
57
+ protected function calculateRequiredLength ($ record ) {
58
+
59
+ // Calculate length needed for barcode and fields side by side
60
+ $ requiredLength = 0 ;
61
+
62
+ // Add barcode length if present
63
+ if (($ record ->has ('barcode2d ' ) && $ this ->getSupport2DBarcode ()) ||
64
+ ($ record ->has ('barcode ' ) && $ this ->getSupport1DBarcode ())) {
65
+ // Use full tape width for barcode size
66
+ $ barcodeSize = self ::TAPE_WIDTH ;
67
+ $ requiredLength += $ barcodeSize + $ this ->barcodeMargin * 0.3 ; // Minimal margin
68
+ }
69
+
70
+ // Add fields length if present - calculate based on actual content
71
+ if ($ record ->has ('fields ' ) && $ this ->getSupportFields () > 0 ) {
72
+ $ fields = array_slice ($ record ->get ('fields ' )->toArray (), 0 , $ this ->getSupportFields ());
73
+
74
+ // Base width for field area
75
+ $ fieldsWidth = self ::TAPE_WIDTH ;
76
+
77
+ // Calculate additional width based on text length
78
+ foreach ($ fields as $ field ) {
79
+ // Get label and value text
80
+ $ labelText = $ field ['label ' ] ?? '' ;
81
+ $ valueText = $ field ['value ' ] ?? '' ;
82
+
83
+ // Calculate approximate width needed based on text length
84
+ // Increase character width to ensure enough space (0.15 inches per character)
85
+ $ labelWidth = strlen ($ labelText ) * 0.09 ;
86
+ $ valueWidth = strlen ($ valueText ) * 0.09 ;
87
+
88
+ // Use the longer of the two
89
+ $ textWidth = max ($ labelWidth , $ valueWidth );
90
+
91
+ // Ensure minimum width and add to total
92
+ $ fieldsWidth = max ($ fieldsWidth , $ textWidth );
93
+ }
94
+
95
+ // Add the calculated width for fields
96
+ $ requiredLength += $ fieldsWidth ;
97
+
98
+ // Add minimal extra space for field padding
99
+ // Reduce padding to eliminate extraneous space on right edge
100
+ // $requiredLength += self::TAPE_WIDTH * 0.1;
101
+ }
102
+
103
+ // Ensure minimum length
104
+ return max ($ this ->minHeight , $ requiredLength );
105
+ }
106
+
107
+ /**
108
+ * Calculate text width accurately using the PDF object
109
+ *
110
+ * @param $pdf The PDF object
111
+ * @param string $text The text to measure
112
+ * @param string $font The font to use
113
+ * @param string $style The font style
114
+ * @param float $size The font size
115
+ * @return float The calculated width
116
+ */
117
+ protected function calculateTextWidth ($ pdf , $ text , $ font , $ style , $ size ) {
118
+ $ originalFont = $ pdf ->getFontFamily ();
119
+ $ originalStyle = $ pdf ->getFontStyle ();
120
+ $ originalSize = $ pdf ->getFontSizePt ();
121
+
122
+ $ pdf ->SetFont ($ font , $ style , Helper::convertUnit ($ size , $ this ->getUnit (), 'pt ' , true ));
123
+ $ width = $ pdf ->GetStringWidth ($ text );
124
+
125
+ // Restore original font settings
126
+ $ pdf ->SetFont ($ originalFont , $ originalStyle , $ originalSize );
127
+
128
+ return $ width ;
129
+ }
130
+
131
+ /**
132
+ * Override the writeAll method to support dynamic page sizes for continuous tapes
133
+ */
134
+ public function writeAll ($ pdf , $ data ) {
135
+ // Use auto-sizing for continuous tapes, fixed height for die-cut tapes
136
+ if ($ this ->continuous ) {
137
+ $ data ->each (function ($ record , $ index ) use ($ pdf ) {
138
+ // Calculate the required length by calling write with calculateOnly=true
139
+ $ requiredLength = $ this ->write ($ pdf , $ record );
140
+
141
+ // If write didn't return a length (old implementation), fall back to calculateRequiredLength
142
+ if ($ requiredLength === null ) {
143
+ $ requiredLength = $ this ->calculateRequiredLength ($ record );
144
+ }
145
+
146
+ // Temporarily update the height property
147
+ $ originalHeight = $ this ->height ;
148
+ $ this ->height = self ::TAPE_WIDTH ; // Keep height fixed at tape width
149
+
150
+ // Add a new page with the calculated dimensions
151
+ // Keep height fixed at TAPE_WIDTH, use calculated length for width
152
+ $ pdf ->AddPage (
153
+ $ this ->getOrientation (),
154
+ [$ requiredLength , self ::TAPE_WIDTH ],
155
+ false , // Don't reset page number
156
+ false // Don't reset object ID
157
+ );
158
+
159
+ // Write the content
160
+ $ this ->write ($ pdf , $ record );
161
+
162
+ // Restore the original height
163
+ $ this ->height = $ originalHeight ;
164
+ });
165
+ } else {
166
+ // Use the default implementation for non-continuous (die-cut) tapes
167
+ parent ::writeAll ($ pdf , $ data );
168
+ }
169
+ }
170
+ }
0 commit comments