Class: Fontisan::Type1::UPMScaler
- Inherits:
-
Object
- Object
- Fontisan::Type1::UPMScaler
- Defined in:
- lib/fontisan/type1/upm_scaler.rb
Overview
UPM (Units Per Em) Scaler
UPMScaler handles scaling of font metrics
from the source font's UPM to a target UPM.
Traditional Type 1 fonts use 1000 UPM, while modern TTF fonts typically use 2048 or other values. This scaler converts metrics appropriately.
Instance Attribute Summary collapse
-
#scale_factor ⇒ Rational
readonly
Scale factor (target / source).
-
#source_upm ⇒ Integer
readonly
Source font's units per em.
-
#target_upm ⇒ Integer
readonly
Target units per em.
Class Method Summary collapse
-
.custom(font, upm:) ⇒ UPMScaler
Create scaler with custom UPM.
-
.native(font) ⇒ UPMScaler
Create scaler with native UPM (no scaling).
-
.type1_standard(font) ⇒ UPMScaler
Create scaler with Type 1 standard UPM (1000).
Instance Method Summary collapse
-
#initialize(font, target_upm: 1000) ⇒ UPMScaler
constructor
Initialize a new UPM scaler.
-
#scale(value) ⇒ Integer
Scale a single value.
-
#scale_array(values) ⇒ Array<Integer>
Scale an array of values.
-
#scale_bbox(bbox) ⇒ Array<Integer>
Scale a bounding box [llx, lly, urx, ury].
-
#scale_pair(value) ⇒ Array<Integer>
Scale a coordinate pair [x, y].
-
#scale_width(width) ⇒ Integer
Scale a character width.
-
#scaling_needed? ⇒ Boolean
Check if scaling is needed.
Constructor Details
#initialize(font, target_upm: 1000) ⇒ UPMScaler
Initialize a new UPM scaler
34 35 36 37 38 39 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 34 def initialize(font, target_upm: 1000) @font = font @source_upm = font.units_per_em @target_upm = target_upm @scale_factor = Rational(target_upm, source_upm) end |
Instance Attribute Details
#scale_factor ⇒ Rational (readonly)
Returns Scale factor (target / source).
28 29 30 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 28 def scale_factor @scale_factor end |
#source_upm ⇒ Integer (readonly)
Returns Source font's units per em.
22 23 24 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 22 def source_upm @source_upm end |
#target_upm ⇒ Integer (readonly)
Returns Target units per em.
25 26 27 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 25 def target_upm @target_upm end |
Class Method Details
.custom(font, upm:) ⇒ UPMScaler
Create scaler with custom UPM
113 114 115 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 113 def self.custom(font, upm:) new(font, target_upm: upm) end |
.native(font) ⇒ UPMScaler
Create scaler with native UPM (no scaling)
96 97 98 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 96 def self.native(font) new(font, target_upm: font.units_per_em) end |
.type1_standard(font) ⇒ UPMScaler
Create scaler with Type 1 standard UPM (1000)
104 105 106 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 104 def self.type1_standard(font) new(font, target_upm: 1000) end |
Instance Method Details
#scale(value) ⇒ Integer
Scale a single value
45 46 47 48 49 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 45 def scale(value) return 0 if value.nil? || value.zero? (value * scale_factor).round end |
#scale_array(values) ⇒ Array<Integer>
Scale an array of values
55 56 57 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 55 def scale_array(values) values.map { |v| scale(v) } end |
#scale_bbox(bbox) ⇒ Array<Integer>
Scale a bounding box [llx, lly, urx, ury]
71 72 73 74 75 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 71 def scale_bbox(bbox) return nil if bbox.nil? bbox.map { |v| scale(v) } end |
#scale_pair(value) ⇒ Array<Integer>
Scale a coordinate pair [x, y]
63 64 65 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 63 def scale_pair(value) [scale(value[0]), scale(value[1])] end |
#scale_width(width) ⇒ Integer
Scale a character width
81 82 83 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 81 def scale_width(width) scale(width) end |
#scaling_needed? ⇒ Boolean
Check if scaling is needed
88 89 90 |
# File 'lib/fontisan/type1/upm_scaler.rb', line 88 def scaling_needed? @source_upm != @target_upm end |