use super::Encoding;
#[derive(Debug, Clone)]
pub struct EncodingNormalizer {
encoding: Encoding,
font_name: String,
}
impl EncodingNormalizer {
pub fn new(encoding: Encoding, font_name: String) -> Self {
Self {
encoding,
font_name,
}
}
pub fn normalize(&self, char_code: u8) -> u32 {
match &self.encoding {
Encoding::Custom(mappings) => {
if let Some(&mapped_char) = mappings.get(&char_code) {
mapped_char as u32
} else {
char_code as u32
}
},
Encoding::Standard(encoding_name) => {
self.normalize_standard_encoding(char_code, encoding_name)
},
Encoding::Identity => {
char_code as u32
},
}
}
fn normalize_standard_encoding(&self, char_code: u8, _encoding_name: &str) -> u32 {
char_code as u32
}
pub fn encoding_type(&self) -> String {
match &self.encoding {
Encoding::Custom(_) => "Custom".to_string(),
Encoding::Standard(name) => format!("Standard({})", name),
Encoding::Identity => "Identity".to_string(),
}
}
pub fn font_name(&self) -> &str {
&self.font_name
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
#[test]
fn test_custom_encoding_normalization() {
let mut mappings = HashMap::new();
mappings.insert(0x64, 'ρ');
let encoding = Encoding::Custom(mappings);
let normalizer = EncodingNormalizer::new(encoding, "CustomFont".to_string());
let normalized = normalizer.normalize(0x64);
assert_eq!(normalized, 0x03C1, "0x64 should normalize to Greek rho");
}
#[test]
fn test_custom_encoding_no_mapping() {
let mut mappings = HashMap::new();
mappings.insert(0x64, 'ρ');
let encoding = Encoding::Custom(mappings);
let normalizer = EncodingNormalizer::new(encoding, "CustomFont".to_string());
let normalized = normalizer.normalize(0x65);
assert_eq!(normalized, 0x65, "Unmapped code should return raw value");
}
#[test]
fn test_standard_encoding_passthrough() {
let encoding = Encoding::Standard("WinAnsiEncoding".to_string());
let normalizer = EncodingNormalizer::new(encoding, "Helvetica".to_string());
let normalized = normalizer.normalize(0x41); assert_eq!(normalized, 0x41, "Standard encoding passes through");
}
#[test]
fn test_identity_encoding() {
let encoding = Encoding::Identity;
let normalizer = EncodingNormalizer::new(encoding, "CIDFont".to_string());
let normalized = normalizer.normalize(0x80);
assert_eq!(normalized, 0x80, "Identity encoding passes through");
}
#[test]
fn test_encoding_type_custom() {
let encoding = Encoding::Custom(HashMap::new());
let normalizer = EncodingNormalizer::new(encoding, "Test".to_string());
assert_eq!(normalizer.encoding_type(), "Custom");
}
#[test]
fn test_encoding_type_standard() {
let encoding = Encoding::Standard("WinAnsiEncoding".to_string());
let normalizer = EncodingNormalizer::new(encoding, "Test".to_string());
assert_eq!(normalizer.encoding_type(), "Standard(WinAnsiEncoding)");
}
#[test]
fn test_encoding_type_identity() {
let encoding = Encoding::Identity;
let normalizer = EncodingNormalizer::new(encoding, "Test".to_string());
assert_eq!(normalizer.encoding_type(), "Identity");
}
}