Class: Fontisan::Type1::UPMScaler

Inherits:
Object
  • Object
show all
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.

Examples:

Scale to Type 1 standard

scaler = Fontisan::Type1::UPMScaler.type1_standard(font)
scaled_width = scaler.scale(1024)  # => 500 for 2048 UPM font

Keep native UPM

scaler = Fontisan::Type1::UPMScaler.native(font)
scaled_width = scaler.scale(1024)  # => 1024 (no scaling)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font, target_upm: 1000) ⇒ UPMScaler

Initialize a new UPM scaler

Parameters:

  • font (Fontisan::Font)

    Source font

  • target_upm (Integer) (defaults to: 1000)

    Target UPM (default: 1000 for Type 1)



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_factorRational (readonly)

Returns Scale factor (target / source).

Returns:

  • (Rational)

    Scale factor (target / source)



28
29
30
# File 'lib/fontisan/type1/upm_scaler.rb', line 28

def scale_factor
  @scale_factor
end

#source_upmInteger (readonly)

Returns Source font's units per em.

Returns:

  • (Integer)

    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_upmInteger (readonly)

Returns Target units per em.

Returns:

  • (Integer)

    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

Parameters:

  • font (Fontisan::Font)

    Source font

  • upm (Integer)

    Target UPM

Returns:



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)

Parameters:

  • font (Fontisan::Font)

    Source font

Returns:



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)

Parameters:

  • font (Fontisan::Font)

    Source font

Returns:



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

Parameters:

  • value (Integer, Float)

    Value to scale

Returns:

  • (Integer)

    Scaled value (rounded)



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

Parameters:

  • values (Array<Integer, Float>)

    Values to scale

Returns:

  • (Array<Integer>)

    Scaled 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]

Parameters:

  • bbox (Array<Integer, Float>)

    Bounding box

Returns:

  • (Array<Integer>)

    Scaled bounding box



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]

Parameters:

  • value (Array<Integer, Float>)

    Coordinate pair

Returns:

  • (Array<Integer>)

    Scaled coordinates



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

Parameters:

  • width (Integer, Float)

    Character width

Returns:

  • (Integer)

    Scaled 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

Returns:

  • (Boolean)

    True if source and target UPM differ



88
89
90
# File 'lib/fontisan/type1/upm_scaler.rb', line 88

def scaling_needed?
  @source_upm != @target_upm
end