> */ public static function formats() { return array( 'code128' => array( 'label' => __( 'Code 128', 'barcodegen' ), 'hint' => __( 'Any ASCII text. The best default for internal codes, tickets and documents.', 'barcodegen' ), 'sample' => 'BCGN-2026-0001', ), 'code39' => array( 'label' => __( 'Code 39', 'barcodegen' ), 'hint' => __( 'Digits, A-Z and - . $ / + % and space. Still common on older industrial equipment.', 'barcodegen' ), 'sample' => 'INV-2026-0001', ), 'ean13' => array( 'label' => __( 'EAN-13', 'barcodegen' ), 'hint' => __( '12 digits, or 13 with the check digit. Retail products worldwide.', 'barcodegen' ), 'sample' => '590123412345', ), 'ean8' => array( 'label' => __( 'EAN-8', 'barcodegen' ), 'hint' => __( '7 digits, or 8 with the check digit. Small retail packages.', 'barcodegen' ), 'sample' => '9638507', ), 'upca' => array( 'label' => __( 'UPC-A', 'barcodegen' ), 'hint' => __( '11 digits, or 12 with the check digit. Retail products in the US and Canada.', 'barcodegen' ), 'sample' => '03600029145', ), 'itf14' => array( 'label' => __( 'ITF-14', 'barcodegen' ), 'hint' => __( '13 digits, or 14 with the check digit. Cartons and shipping labels.', 'barcodegen' ), 'sample' => '1540014128876', ), ); } /** * Check whether a format is supported. * * @param string $format Format slug. * @return bool */ public static function supports( $format ) { return array_key_exists( $format, self::formats() ); } /** * Encode a value into bar and space widths. * * @param string $format Format slug. * @param string $text Value to encode. * @param array $args Optional settings. * @return array|WP_Error */ public static function encode( $format, $text, $args = array() ) { $format = strtolower( (string) $format ); $text = (string) $text; switch ( $format ) { case 'code128': $symbol = BCGN_Code128::encode( $text, 'auto' ); break; case 'code128a': case 'code128b': case 'code128c': $symbol = BCGN_Code128::encode( $text, substr( $format, -1 ) ); break; case 'code39': $symbol = BCGN_Code39::encode( $text, ! empty( $args['checksum'] ), isset( $args['wide_ratio'] ) ? $args['wide_ratio'] : 3 ); break; case 'ean13': case 'ean8': case 'upca': $symbol = BCGN_EAN::encode( $text, $format ); break; case 'itf14': case 'itf': $symbol = BCGN_ITF::encode( $text, $format, isset( $args['wide_ratio'] ) ? $args['wide_ratio'] : 2 ); break; default: return new WP_Error( 'bcgn_format', __( 'Unsupported barcode format.', 'barcodegen' ) ); } if ( is_wp_error( $symbol ) ) { return $symbol; } $quiet = self::quiet_zones( $format ); if ( isset( $args['quiet'] ) && null !== $args['quiet'] ) { $quiet = array( (int) $args['quiet'], (int) $args['quiet'] ); } $symbol['format'] = $format; $symbol['quiet'] = $quiet; $symbol['lead'] = isset( $symbol['lead'] ) ? $symbol['lead'] : ''; $symbol['tail'] = isset( $symbol['tail'] ) ? $symbol['tail'] : ''; $symbol['guard'] = isset( $symbol['guard'] ) ? $symbol['guard'] : array(); return $symbol; } /** * Encode a value and render it as inline SVG. * * @param string $format Format slug. * @param string $text Value to encode. * @param array $args Optional settings. * @return string|WP_Error */ public static function svg( $format, $text, $args = array() ) { $symbol = self::encode( $format, $text, $args ); if ( is_wp_error( $symbol ) ) { return $symbol; } return BCGN_SVG::render( $symbol, $args ); } /** * Quiet zone widths, in modules, for a format. * * @param string $format Format slug. * @return array{0:int,1:int} */ public static function quiet_zones( $format ) { switch ( $format ) { case 'ean13': return array( 11, 7 ); case 'upca': return array( 9, 9 ); case 'ean8': return array( 7, 7 ); default: return array( 10, 10 ); } } }