@@ -72,30 +72,34 @@ class Datamatrix extends \Com\Tecnick\Barcode\Type\Square
7272 */
7373 protected bool $ gsonemode = false ;
7474
75+ /**
76+ * Datamatrix default encoding.
77+ * See Data::SWITCHCDW for valid values.
78+ */
79+ protected int $ defenc = Data::ENC_ASCII ;
80+
7581 /**
7682 * Set extra (optional) parameters:
77- * 1: SHAPE - S=square (default), R=rectangular
78- * 2: MODE - N=default, GS1 = the FNC1 codeword is added in the first position of Data Matrix ECC 200 version
83+ * 1: SHAPE: S=square (default), R=rectangular.
84+ * 2: MODE: N=default, GS1 = the FNC1 codeword is added in the first position of Data Matrix ECC 200 version.
85+ * 3: ENCODING: ASCII (default), C40, TXT, X12, EDIFACT, BASE256.
7986 */
8087 protected function setParameters (): void
8188 {
8289 parent ::setParameters ();
8390
8491 // shape
85- if (isset ($ this ->params [0 ]) && ($ this ->params [0 ] == 'R ' )) {
92+ if (isset ($ this ->params [0 ]) && ($ this ->params [0 ] === 'R ' )) {
8693 $ this ->shape = 'R ' ;
8794 }
8895
8996 // mode
90- if (! isset ($ this ->params [1 ])) {
91- return ;
92- }
97+ $ this ->gsonemode = (isset ($ this ->params [1 ]) && ($ this ->params [1 ] === 'GS1 ' ));
9398
94- if ($ this ->params [1 ] != 'GS1 ' ) {
95- return ;
99+ // encoding
100+ if (isset ($ this ->params [2 ])) {
101+ $ this ->defenc = Data::ENCOPTS [$ this ->params [2 ]] ?? Data::ENC_ASCII ;
96102 }
97-
98- $ this ->gsonemode = true ;
99103 }
100104
101105 /**
@@ -226,14 +230,21 @@ protected function setGrid(
226230 */
227231 protected function getHighLevelEncoding (string $ data ): array
228232 {
229- // STEP A. Start in ASCII encodation.
230- $ enc = Data:: ENC_ASCII ; // current encoding mode
233+ // STEP A. Start in predefined encodation.
234+ $ enc = $ this -> defenc ; // current encoding mode
231235 $ this ->dmx ->last_enc = $ enc ; // last used encoding
232236 $ pos = 0 ; // current position
233237 $ cdw = []; // array of codewords to be returned
234238 $ cdw_num = 0 ; // number of data codewords
235239 $ data_length = strlen ($ data ); // number of chars
236240 $ field_length = 0 ; // number of chars in current field
241+
242+ // Switch to predefined encoding (no action needed if ASCII because it's the default encoding)
243+ if ($ this ->defenc !== Data::ENC_ASCII ) {
244+ $ cdw [] = $ this ->dmx ->getSwitchEncodingCodeword ($ this ->defenc );
245+ ++$ cdw_num ;
246+ }
247+
237248 while ($ pos < $ data_length ) {
238249 // Determine if current char is FNC1 (don't encode it, just pass it through)
239250 if ($ this ->gsonemode && ($ data [$ pos ] == chr (232 ))) {
0 commit comments