Skip to content

Conversation

@adcondev
Copy link
Owner

@adcondev adcondev commented Jan 27, 2026

📝 Description

This pull request introduces significant improvements and refactoring to QR code generation and printing logic, focusing on consistency, configurability, and code clarity. The main changes include standardizing the naming of QR code options, making error correction levels and maximum pixel widths configurable based on printer profiles, and improving the handling of QR code sizing and validation.

QR Code Options Refactoring and Consistency:

  • Renamed QROptions to QrOptions throughout the codebase for consistency and clarity. Updated all relevant function signatures, struct names, and references. [1] [2] [3] [4] [5] [6] [7] [8]

Configurable QR Code Settings:

  • Changed the default QR error correction level from Q to M for better compatibility and reliability. Updated default values in both constants and QR option initialization. [1] [2] [3]
  • Added new constants for paper widths (Paper58mm, Paper80mm) and ensured that maximum QR code pixel width is set dynamically based on the printer's paper width profile. [1] [2]

Improved QR Code Generation and Validation:

  • Enhanced pixel width validation logic in QrOptions to clamp values within printer-specific limits and provide detailed logging if adjustments are made.
  • Improved error correction level mapping and validation in QR code handlers to use the new constants and ensure only valid levels are accepted. [1] [2]

Printer Service Enhancements:

  • Refactored the QR printing logic in the printer service to use the new QrOptions, dynamically set maximum pixel width, and provide more robust fallback between native and image QR code printing.

Other Minor Improvements:

  • Removed unused or redundant constants, improved comments, and added additional debug logging for QR code generation and printing steps. [1] [2] [3]

These changes improve the maintainability of the QR code subsystem, make it easier to add support for new printer types, and ensure more predictable and configurable QR code output.

🎯 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation only
  • 🔧 Configuration/DevOps
  • ♻️ Code refactoring
  • 📦 Dependency update
  • ⚡ Performance improvement
  • 🧪 Test addition/modification

🎯 Component(s) Affected

  • composer - ESC/POS protocol composer
  • connection - Printer connections
  • commands - ESC/POS command implementations
  • builder - Document builder
  • executor - Document processing
  • graphics - Image processing
  • tables - Table formatting and layout
  • emulator - Visual emulator
  • profile - Printer profiles
  • service - High-level printer service
  • github - GitHub related files and workflows

🧪 How Has This Been Tested?

  • Unit tests pass locally
  • Integration tests pass
  • Manual testing with physical printer
  • Examples built and run successfully
  • Benchmarks show no regression (or improvement)
  • N/A (documentation/configuration only)

Test Configuration

  • Go Version:
  • OS:
  • Printer Model (if applicable):

🤖 Automated Checks

The following automated workflows will run on this PR:

  • 📋 PR Validation - Validates commit message format
  • 🧪 Tests - Runs on Ubuntu, Windows, macOS
  • ⚡ Benchmarks - Compares performance with base branch
  • 🔒 Security Scan - Trivy vulnerability scanner
  • 🔍 Linting - golangci-lint with custom config
  • 🔐 CodeQL - Advanced security analysis

💡 Tip: You can view detailed results in the "Checks" tab above or in the job summary after completion.

✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published
  • My PR title follows Conventional Commits format

📸 Screenshots/Examples

Click to expand
// Example code here

💥 Breaking Changes

  • What breaks:
  • Migration path for users:
  • Why this breaking change is necessary:

🔗 Related Issues

  • Closes #
  • Relates to #
  • Depends on #

📝 Additional Notes


📚 Resources:

- Ensure the generated QR code width does not exceed the requested width
- Update the pixel width assignment logic to use the generated width when necessary
- Add node_modules to .gitignore to prevent unnecessary files from being tracked

Signed-off-by: Adrián Constante <41898282+github-actions[bot]@users.noreply.github.com>
- Introduce new constants for paper widths (58mm and 80mm)
- Change default error correction level from Q to M
- Update QR options struct and related methods to reflect new naming conventions
- Ensure compatibility with existing QR printing functionality

Signed-off-by: Adrián Constante <41898282+github-actions[bot]@users.noreply.github.com>
@github-actions
Copy link
Contributor

👋 Thanks for opening this PR, @adcondev!

Here's what will happen next:

  • 🤖 Automated checks will run
  • 🏷️ Labels will be added automatically
  • 👀 A maintainer will review your changes

Please make sure:

  • ✅ All tests pass
  • 📝 The PR title follows conventional commits
  • 📋 The PR template is filled out completely

@github-actions
Copy link
Contributor

⚡ Benchmark Results

📈 Performance Comparison

📊 Click to expand detailed results

Current Branch Results

BenchmarkNewDocument-4    	1000000000	         0.3121 ns/op	       0 B/op	       0 allocs/op
BenchmarkBuildSimple-4    	 3734337	       323.9 ns/op	     240 B/op	       4 allocs/op
BenchmarkBuildComplex-4   	  408078	      2736 ns/op	    1809 B/op	      25 allocs/op
BenchmarkToJSON-4         	  544863	      2073 ns/op	     592 B/op	       3 allocs/op
BenchmarkParseDocument_Minimal-4    	  512481	      2302 ns/op	     568 B/op	      14 allocs/op
BenchmarkParseDocument_Receipt-4    	   94330	     12519 ns/op	    2392 B/op	      34 allocs/op
BenchmarkCommandUnmarshal_Text-4    	  421194	      2711 ns/op	     568 B/op	      19 allocs/op
BenchmarkCommandUnmarshal_Table-4   	  275048	      4177 ns/op	     944 B/op	      21 allocs/op
BenchmarkTextCommandParsing-4       	  533938	      2167 ns/op	     544 B/op	      16 allocs/op
BenchmarkTableCommandParsing-4      	  248830	      4792 ns/op	    1000 B/op	      29 allocs/op
BenchmarkParseHexString-4           	 8732049	       138.2 ns/op	      48 B/op	       2 allocs/op
BenchmarkCleanHexString-4           	 6704113	       178.7 ns/op	      64 B/op	       2 allocs/op
BenchmarkContainsSequence-4         	369031422	         3.293 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckCriticalCommands-4    	31792400	        37.94 ns/op	       0 B/op	       0 allocs/op
BenchmarkDocument_Validate-4       	14602411	        83.39 ns/op	       0 B/op	       0 allocs/op
BenchmarkParseDocument_Simple-4    	  501414	      2425 ns/op	     568 B/op	      14 allocs/op
BenchmarkParseDocument_Complex-4   	  138168	      8529 ns/op	    1352 B/op	      26 allocs/op
BenchmarkPrintImage_Small-4             	     601	   1956104 ns/op	 3469945 B/op	      24 allocs/op
BenchmarkPrintImage_Medium-4            	     322	   3747194 ns/op	 5481085 B/op	      24 allocs/op
BenchmarkPrintImage_ThermalPreview-4    	     136	   8691726 ns/op	 5235282 B/op	  230723 allocs/op

Base Branch Results

BenchmarkNewDocument-4    	1000000000	         0.3123 ns/op	       0 B/op	       0 allocs/op
BenchmarkBuildSimple-4    	 3669132	       326.9 ns/op	     240 B/op	       4 allocs/op
BenchmarkBuildComplex-4   	  424555	      2827 ns/op	    1809 B/op	      25 allocs/op
BenchmarkToJSON-4         	  543607	      2096 ns/op	     592 B/op	       3 allocs/op
BenchmarkParseDocument_Minimal-4    	  509538	      2311 ns/op	     568 B/op	      14 allocs/op
BenchmarkParseDocument_Receipt-4    	   93472	     12745 ns/op	    2392 B/op	      34 allocs/op
BenchmarkCommandUnmarshal_Text-4    	  417613	      2707 ns/op	     568 B/op	      19 allocs/op
BenchmarkCommandUnmarshal_Table-4   	  278623	      4220 ns/op	     944 B/op	      21 allocs/op
BenchmarkTextCommandParsing-4       	  538184	      2119 ns/op	     544 B/op	      16 allocs/op
BenchmarkTableCommandParsing-4      	  243291	      4804 ns/op	    1000 B/op	      29 allocs/op
BenchmarkParseHexString-4           	 7701726	       152.6 ns/op	      48 B/op	       2 allocs/op
BenchmarkCleanHexString-4           	 6696916	       172.0 ns/op	      64 B/op	       2 allocs/op
BenchmarkContainsSequence-4         	295013030	         4.113 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckCriticalCommands-4    	33249980	        35.75 ns/op	       0 B/op	       0 allocs/op
BenchmarkDocument_Validate-4       	14486622	        83.17 ns/op	       0 B/op	       0 allocs/op
BenchmarkParseDocument_Simple-4    	  495930	      2405 ns/op	     568 B/op	      14 allocs/op
BenchmarkParseDocument_Complex-4   	  136256	      8623 ns/op	    1352 B/op	      26 allocs/op
BenchmarkPrintImage_Small-4             	     604	   1993028 ns/op	 3469938 B/op	      24 allocs/op
BenchmarkPrintImage_Medium-4            	     327	   3817151 ns/op	 5481087 B/op	      24 allocs/op
BenchmarkPrintImage_ThermalPreview-4    	     136	   8685220 ns/op	 5235274 B/op	  230723 allocs/op

💡 Note: Use benchstat for statistical comparison

🎯 Summary

  • Total Benchmarks: 32
  • Average Speed: 2643249 ns/op
  • Average Memory: 2396936 B/op
  • Average Allocations: 41093 allocs/op

@adcondev adcondev merged commit 7bae47c into master Jan 27, 2026
48 checks passed
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors QR code configuration and printing to use a new QrOptions struct, makes QR sizing depend on printer profiles, and adjusts defaults (notably error correction) for more predictable QR output.

Changes:

  • Renames QROptions to QrOptions and threads the new type through the service layer, executor, and graphics subsystem.
  • Makes QR maximum pixel width configurable per printer profile and improves pixel width clamping and image fallback behavior.
  • Updates QR defaults (error correction level and pixel width handling) via new constants and handler logic, and fixes minor tooling/config issues.

Reviewed changes

Copilot reviewed 7 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/service/printer_service.go Updates PrintQR / printQRNative / printQRAsImage to use graphics.QrOptions, set a profile-based MaxPixelWidth, and adjust bitmap rendering based on generated image width.
pkg/service/printer_mock.go Aligns the mock printer’s PrintQR signature with the new graphics.QrOptions type.
pkg/service/printer_interface.go Updates the PrinterActions interface to use *graphics.QrOptions for PrintQR.
pkg/graphics/qr.go Renames QROptionsQrOptions, introduces MaxPixelWidth, refines pixel-width validation and module-size calculations, and sets new defaults (error correction M, max width from paper pixel width).
pkg/document/executor/qr_handler.go Uses graphics.DefaultQROptions() with new QR defaults and constants, and remaps string correction levels through constants.ErrorCorrection values.
pkg/constants/defaults.go Changes DefaultQrErrorCorrection from Q to M and keeps QR-related defaults centralized.
pkg/constants/constants.go Adds millimeter-based paper-width constants (Paper58mm, Paper80mm) alongside existing pixel-width constants.
.gitignore Ignores node_modules to avoid committing Node-based tooling artifacts.
.github/codeql/codeql-config.yml Fixes a glob pattern typo to correctly ignore *.exe build artifacts in CodeQL analysis.
Comments suppressed due to low confidence (1)

pkg/document/executor/qr_handler.go:73

  • handleQR gained additional behavior around default error-correction and pixel width (via constants.DefaultQrErrorCorrection and constants.DefaultQrPixelWidth), but the existing tests in qr_handler_test.go only cover JSON parsing and struct defaults, not the handler logic itself. Given this file already has tests, consider adding handler-level tests that exercise the default correction level, validation of invalid correction values, and the new default pixel width so future changes to these constants can be caught by the test suite.
	// Mapear corrección de errores (default: Q según schema)
	correction := constants.DefaultQrErrorCorrection.String()
	if cmd.Correction != "" {
		correction = strings.ToUpper(cmd.Correction)
	}

	switch correction {
	case constants.L.String():
		opts.ErrorCorrection = posqr.LevelL
	case constants.M.String():
		opts.ErrorCorrection = posqr.LevelM
	case constants.Q.String():
		opts.ErrorCorrection = posqr.LevelQ
	case constants.H.String():
		opts.ErrorCorrection = posqr.LevelH
	default:
		return fmt.Errorf("invalid QR correction level: %s (valid: L, M, Q, H)", cmd.Correction)
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 54 to 72
}

// Mapear corrección de errores (default: Q según schema)
correction := "Q"
correction := constants.DefaultQrErrorCorrection.String()
if cmd.Correction != "" {
correction = strings.ToUpper(cmd.Correction)
}

switch correction {
case "L":
case constants.L.String():
opts.ErrorCorrection = posqr.LevelL
case "M":
case constants.M.String():
opts.ErrorCorrection = posqr.LevelM
case "Q":
case constants.Q.String():
opts.ErrorCorrection = posqr.LevelQ
case "H":
case constants.H.String():
opts.ErrorCorrection = posqr.LevelH
default:
return fmt.Errorf("invalid QR correction level: %s (valid: L, M, Q, H)", cmd.Correction)
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment // Default: 128 no longer matches the behavior here: when pixel_width is omitted, the handler now sets opts.PixelWidth = constants.DefaultQrPixelWidth, which is currently 256, while the JSON schema (api/v1/document.schema.json, QRCommand.pixel_width.default) also documents 128 as the default. To avoid confusing API users, please either restore the 128px default or update both this comment and the schema default to match the actual value used by the handler (and keep them in sync going forward).

Copilot uses AI. Check for mistakes.
@adcondev adcondev deleted the fix/qr branch January 27, 2026 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants