Class: SilkLayout::Render::FontLibrary
- Inherits:
-
Object
- Object
- SilkLayout::Render::FontLibrary
- Defined in:
- lib/silk_layout/render/font_library.rb
Constant Summary collapse
- BASE_FONTS =
{ helvetica: { normal: { normal: "Helvetica", italic: "Helvetica-Oblique" }, bold: { normal: "Helvetica-Bold", italic: "Helvetica-BoldOblique" } }, times: { normal: { normal: "Times-Roman", italic: "Times-Italic" }, bold: { normal: "Times-Bold", italic: "Times-BoldItalic" } }, courier: { normal: { normal: "Courier", italic: "Courier-Oblique" }, bold: { normal: "Courier-Bold", italic: "Courier-BoldOblique" } } }.freeze
- FAMILY_ALIASES =
{ "arial" => :helvetica, "helvetica" => :helvetica, "sans-serif" => :helvetica, "sans serif" => :helvetica, "times" => :times, "times new roman" => :times, "serif" => :times, "courier" => :courier, "courier new" => :courier, "monospace" => :courier }.freeze
Class Method Summary collapse
- .measure_text(text, font_size:, font_family:, font_weight: "normal", font_style: "normal") ⇒ Object
- .metrics(font_family, font_weight: "normal", font_style: "normal") ⇒ Object
- .resolve_font_name(font_family, font_weight: "normal", font_style: "normal") ⇒ Object
Class Method Details
.measure_text(text, font_size:, font_family:, font_weight: "normal", font_style: "normal") ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/silk_layout/render/font_library.rb', line 67 def measure_text(text, font_size:, font_family:, font_weight: "normal", font_style: "normal") return 0 if text.to_s.empty? font = metrics(font_family, font_weight: font_weight, font_style: font_style)[:font] glyph_width = font.decode_utf8(text.to_s).sum(&:width) glyph_width * font_size / 1000.0 end |
.metrics(font_family, font_weight: "normal", font_style: "normal") ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/silk_layout/render/font_library.rb', line 55 def metrics(font_family, font_weight: "normal", font_style: "normal") font_name = resolve_font_name(font_family, font_weight: font_weight, font_style: font_style) font = font_wrapper(font_name) { font_name: font_name, font: font, ascender: normalized_metric(font, :ascender), descender: normalized_metric(font, :descender) } end |
.resolve_font_name(font_family, font_weight: "normal", font_style: "normal") ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/silk_layout/render/font_library.rb', line 75 def resolve_font_name(font_family, font_weight: "normal", font_style: "normal") family = normalized_family(font_family) weight = bold?(font_weight) ? :bold : :normal style = italic?(font_style) ? :italic : :normal BASE_FONTS.fetch(family, BASE_FONTS[:helvetica]).fetch(weight).fetch(style) end |