Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 13 additions & 27 deletions cmd/poster/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"os"
"path/filepath"
"strings"

"github.com/adcondev/poster/internal/load"
"github.com/adcondev/poster/pkg/constants"
Expand Down Expand Up @@ -153,40 +152,27 @@ func getPrinterNameFromDocument(config *Config, jsonData []byte) (string, error)
}

func createProfile(doc *schema.Document) *profile.Escpos {
var prof *profile.Escpos

// Select profile based on paper width or model
if doc.Profile.Model != "" {
switch strings.ToLower(doc.Profile.Model) {
case "80mm ec-pm-80250", "ec-pm-80250":
prof = profile.CreateECPM80250()
case "58mm pt-210", "pt-210":
prof = profile.CreatePt210()
case "58mm gp-58n", "gp-58n":
prof = profile.CreateGP58N()
default:
if doc.Profile.PaperWidth >= 80 {
prof = profile.CreateProfile80mm()
} else {
prof = profile.CreateProfile58mm()
}
}
} else {
// Default based on paper width
if doc.Profile.PaperWidth >= 80 {
prof = profile.CreateProfile80mm()
} else {
prof, ok := profile.ProfileFromModel(doc.Profile.Model)
if !ok {
if doc.Profile.PaperWidth == 58 {
prof = profile.CreateProfile58mm()
} else {
prof = profile.CreateProfile80mm()
}
}

// Apply JSON overrides
if doc.Profile.Model != "" {
prof.Model = doc.Profile.Model
prof.Model = doc.Profile.Model
if doc.Profile.PaperWidth > 0 {
prof.PaperWidth = float64(doc.Profile.PaperWidth)
}
if doc.Profile.DPI > 0 {
prof.DPI = doc.Profile.DPI
}
if doc.Profile.CodeTable != "" {
if ct, ok := profile.ParseCodeTable(doc.Profile.CodeTable); ok {
prof.CodeTable = ct
}
}
prof.HasQR = doc.Profile.HasQR

return prof
Expand Down
6 changes: 5 additions & 1 deletion pkg/constants/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ var (
ValidDPIs = []int{DefaultDPI, 300, 600}

// ValidCodeTables for character encoding
ValidCodeTables = []string{"WPC1252", "PC850", "PC437", "PC858"}
ValidCodeTables = []string{
"PC437", "PC850", "PC852", "PC855", "PC858", "PC860", "PC862", "PC863", "PC865", "PC866",
"WPC1250", "WPC1251", "WPC1252", "WPC1253", "WPC1254", "WPC1255", "WPC1256", "WPC1257", "WPC1258",
"ISO88592", "ISO88597", "ISO885915", "Katakana", "Hiragana", "ShiftJIS",
}
)

// Text defaults
Expand Down
22 changes: 9 additions & 13 deletions pkg/document/executor/handler_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/adcondev/poster/pkg/commands/character"
"github.com/adcondev/poster/pkg/constants"
"github.com/adcondev/poster/pkg/document/schema"
"github.com/adcondev/poster/pkg/profile"
"github.com/adcondev/poster/pkg/service"
)

Expand Down Expand Up @@ -105,16 +106,7 @@ func (e *Executor) Execute(doc *schema.Document) error {

// setCodeTable configures the character code table with fallback to Windows-1252.
func (e *Executor) setCodeTable(tableName string) error {
tables := map[string]character.CodeTable{
"PC437": character.PC437,
"PC850": character.PC850,
"PC852": character.PC852,
"WPC1252": character.WPC1252,
"PC866": character.PC866,
"PC858": character.PC858,
}

table, ok := tables[tableName]
table, ok := profile.ParseCodeTable(tableName)
if !ok {
return e.printer.SetCodeTable(character.WPC1252)
}
Expand Down Expand Up @@ -165,10 +157,14 @@ func (e *Executor) applyProfileFromDocument(doc *schema.Document) error {
// applyProfileOrDefaults applies default profile settings when omitted in JSON.
func (e *Executor) applyProfileOrDefaults(config schema.ProfileConfig) error {
prof := e.printer.GetProfile()
if config.PaperWidth == 0 {
prof.PaperWidth = constants.Paper80mm
} else {
if config.PaperWidth > 0 {
prof.PaperWidth = float64(config.PaperWidth)
} else {
if preset, ok := profile.ProfileFromModel(config.Model); ok && preset.PaperWidth > 0 {
prof.PaperWidth = preset.PaperWidth
} else if prof.PaperWidth <= 0 {
prof.PaperWidth = constants.Paper80mm
}
}

if config.CodeTable == "" {
Expand Down
96 changes: 96 additions & 0 deletions pkg/document/executor/handler_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"testing"

"github.com/adcondev/poster/pkg/commands/character"
"github.com/adcondev/poster/pkg/constants"
"github.com/adcondev/poster/pkg/document/schema"
"github.com/adcondev/poster/pkg/service"
)
Expand Down Expand Up @@ -653,3 +655,97 @@ func BenchmarkCommandUnmarshal_Table(b *testing.B) {
_ = json.Unmarshal(data, &cmd)
}
}

// ============================================================================
// CodeTable Resolution & Preset Profile Tests
// ============================================================================

func TestSetCodeTable_AllSupportedTablesAndFallback(t *testing.T) {
mock := service.NewMockPrinter()
exec := NewExecutor(mock)

testCases := []struct {
name string
input string
expected character.CodeTable
}{
{"PC437", "PC437", character.PC437},
{"CP437 alias", "CP437", character.PC437},
{"PC850", "PC850", character.PC850},
{"PC852", "PC852", character.PC852},
{"PC855", "PC855", character.PC855},
{"PC858", "PC858", character.PC858},
{"PC860", "PC860", character.PC860},
{"PC862", "PC862", character.PC862},
{"PC863", "PC863", character.PC863},
{"PC865", "PC865", character.PC865},
{"PC866", "PC866", character.PC866},
{"WPC1250", "WPC1250", character.WPC1250},
{"WPC1251", "WPC1251", character.WPC1251},
{"WPC1252", "WPC1252", character.WPC1252},
{"WPC1253", "WPC1253", character.WPC1253},
{"WPC1254", "WPC1254", character.WPC1254},
{"WPC1255", "WPC1255", character.WPC1255},
{"WPC1256", "WPC1256", character.WPC1256},
{"WPC1257", "WPC1257", character.WPC1257},
{"WPC1258", "WPC1258", character.WPC1258},
{"ISO88592", "ISO88592", character.ISO88592},
{"ISO88597", "ISO88597", character.ISO88597},
{"ISO885915", "ISO885915", character.ISO885915},
{"Katakana", "Katakana", character.Katakana},
{"Hiragana", "Hiragana", character.Hiragana},
{"ShiftJIS", "ShiftJIS", character.Katakana},
{"Unknown fallback", "UNKNOWN_TABLE", character.WPC1252},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := exec.setCodeTable(tc.input)
if err != nil {
t.Fatalf("setCodeTable(%s) failed: %v", tc.input, err)
}
lastCall := mock.LastCall()
if lastCall == nil || lastCall.Method != "SetCodeTable" {
t.Fatalf("expected SetCodeTable call on mock")
}
got := lastCall.Args[0].(character.CodeTable)
if got != tc.expected {
t.Errorf("setCodeTable(%s): got %v, expected %v", tc.input, got, tc.expected)
}
})
}
}

func TestExecutor_Execute_58mmPresetPreservesPaperWidth(t *testing.T) {
mock := service.NewMockPrinter()
exec := NewExecutor(mock)

// Document has Model = "58mm PT-210" and PaperWidth = 0 (omitted / default)
doc := &schema.Document{
Version: "1.0",
Profile: schema.ProfileConfig{
Model: "58mm PT-210",
PaperWidth: 0,
DPI: 203,
},
Commands: []schema.Command{
{
Type: "feed",
Data: json.RawMessage(`{"lines": 1}`),
},
},
}

if err := exec.Execute(doc); err != nil {
t.Fatalf("exec.Execute failed: %v", err)
}

prof := mock.GetProfile()
if prof.PaperWidth != 58 {
t.Errorf("expected PaperWidth == 58, got %v", prof.PaperWidth)
}
if prof.DotsPerLine != constants.PaperPxWidth58mm {
t.Errorf("expected DotsPerLine == %d, got %d", constants.PaperPxWidth58mm, prof.DotsPerLine)
}
}

64 changes: 64 additions & 0 deletions pkg/profile/escpos_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package profile

import (
"fmt"
"strings"

"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
Expand Down Expand Up @@ -57,6 +58,7 @@ var codeTableMap = map[character.CodeTable]encoding.Encoding{

// EncodeString encodes a string using the specified code table
func (e *Escpos) EncodeString(text string) (string, error) {
text = NormalizeTypography(text)
enc := e.getEncoding(e.CodeTable)
result, err := enc.String(text)
if err != nil {
Expand All @@ -65,6 +67,68 @@ func (e *Escpos) EncodeString(text string) (string, error) {
return result, nil
}

// ParseCodeTable normalizes a code table name or alias and returns the corresponding character.CodeTable.
// It removes hyphens, underscores, and spaces and converts to uppercase before matching.
func ParseCodeTable(name string) (character.CodeTable, bool) {
clean := strings.ToUpper(strings.TrimSpace(name))
clean = strings.ReplaceAll(clean, "-", "")
clean = strings.ReplaceAll(clean, "_", "")
clean = strings.ReplaceAll(clean, " ", "")

switch clean {
case "PC437", "CP437", "437":
return character.PC437, true
case "PC850", "CP850", "850":
return character.PC850, true
case "PC852", "CP852", "852":
return character.PC852, true
case "PC855", "CP855", "855":
return character.PC855, true
case "PC858", "CP858", "858":
return character.PC858, true
case "PC860", "CP860", "860":
return character.PC860, true
case "PC862", "CP862", "862":
return character.PC862, true
case "PC863", "CP863", "863":
return character.PC863, true
case "PC865", "CP865", "865":
return character.PC865, true
case "PC866", "CP866", "866":
return character.PC866, true
case "WPC1250", "CP1250", "WINDOWS1250", "1250":
return character.WPC1250, true
case "WPC1251", "CP1251", "WINDOWS1251", "1251":
return character.WPC1251, true
case "WPC1252", "CP1252", "WINDOWS1252", "1252":
return character.WPC1252, true
case "WPC1253", "CP1253", "WINDOWS1253", "1253":
return character.WPC1253, true
case "WPC1254", "CP1254", "WINDOWS1254", "1254":
return character.WPC1254, true
case "WPC1255", "CP1255", "WINDOWS1255", "1255":
return character.WPC1255, true
case "WPC1256", "CP1256", "WINDOWS1256", "1256":
return character.WPC1256, true
case "WPC1257", "CP1257", "WINDOWS1257", "1257":
return character.WPC1257, true
case "WPC1258", "CP1258", "WINDOWS1258", "1258":
return character.WPC1258, true
case "ISO88592", "88592":
return character.ISO88592, true
case "ISO88597", "88597":
return character.ISO88597, true
case "ISO885915", "885915":
return character.ISO885915, true
case "KATAKANA", "SHIFTJIS", "SJIS":
return character.Katakana, true
case "HIRAGANA":
return character.Hiragana, true
default:
return 0, false
}
}

// getEncoding returns the encoding.Encoder for the specified code table
func (e *Escpos) getEncoding(codeTable character.CodeTable) *encoding.Encoder {
enc, ok := codeTableMap[codeTable]
Expand Down
7 changes: 7 additions & 0 deletions pkg/profile/escpos_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ func TestEncodeString(t *testing.T) {
expectedError: false,
expectedBytes: []byte("test"),
},
{
name: "Typography normalization in EncodeString",
codeTable: character.PC437,
input: "“Hello”",
expectedError: false,
expectedBytes: []byte("\"Hello\""),
},
}

p := profile.CreateProfile58mm()
Expand Down
13 changes: 10 additions & 3 deletions pkg/profile/escpos_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type Escpos struct {
// Code table and encoding configuration
CodeTable character.CodeTable

// Encoding behavior options
StrictEncoding bool // Return error on unencodable runes instead of '?' replacement
AutoSwitchCodePage bool // Automatically switch code tables inline (ESC t n) for mixed UTF-8
FallbackCodePages []character.CodeTable // Candidate code tables for AutoSwitchCodePage

// Configuración avanzada (opcional)
ImageThreshold int // Umbral para conversión B/N (0-255)
Dithering graphics.DitherMode // Tipo de dithering por defecto
Expand All @@ -40,7 +45,6 @@ type Escpos struct {
}

// CreatePt210 crea un perfil para impresora térmica de 58mm PT-58N
// TODO: Hardcoded Models: Factory functions for specific models might become hard to maintain. Consider configuration-based approach.
func CreatePt210() *Escpos {
p := CreateProfile58mm()
p.Model = "58mm PT-210"
Expand All @@ -67,8 +71,8 @@ func CreateProfile58mm() *Escpos {

PaperWidth: 58,
DPI: 203,
DotsPerLine: 384, // Típico para 58mm a 203 DPI
PrintWidth: 48, // Ancho de impresión efectivo
DotsPerLine: shared.Dpl58mm203dpi, // Típico para 58mm a 203 DPI
PrintWidth: 48, // Ancho de impresión efectivo

SupportsGraphics: true,
SupportsBarcode: true,
Expand All @@ -77,6 +81,8 @@ func CreateProfile58mm() *Escpos {
SupportsDrawer: false,

CodeTable: character.PC850,

ImageThreshold: 128,
}
}

Expand All @@ -95,6 +101,7 @@ func CreateProfile80mm() *Escpos {
PaperWidth: 80,
DPI: 203,
DotsPerLine: shared.Dpl80mm203dpi, // Típico para 80mm (72mm) a 203 DPI
PrintWidth: 72,

SupportsGraphics: true,
SupportsBarcode: true,
Expand Down
Loading
Loading