Class: Skia::Typeface
Constant Summary collapse
- WEIGHT_NORMAL =
400- WEIGHT_BOLD =
700- WIDTH_NORMAL =
5
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
- .default ⇒ Object
- .fixed_16_16_to_float(raw) ⇒ Object
- .from_file(path, index = 0) ⇒ Object
- .from_name(name, weight: WEIGHT_NORMAL, width: WIDTH_NORMAL, slant: :upright) ⇒ Object
- .tag_to_string(tag_u32) ⇒ Object
- .tag_to_uint32(tag) ⇒ Object
Instance Method Summary collapse
- #family_name ⇒ Object
- #fixed_pitch? ⇒ Boolean
- #glyph_count ⇒ Object
-
#initialize(ptr) ⇒ Typeface
constructor
A new instance of Typeface.
- #post_script_name ⇒ Object
- #slant ⇒ Object
- #style ⇒ Object
- #table_count ⇒ Object
- #table_data(tag) ⇒ Object
- #table_tags ⇒ Object
- #units_per_em ⇒ Object
- #variation_axes ⇒ Object
- #weight ⇒ Object
- #width ⇒ Object
Methods inherited from Base
#null?, release_callback, #to_ptr
Constructor Details
#initialize(ptr) ⇒ Typeface
9 10 11 |
# File 'lib/skia/typeface.rb', line 9 def initialize(ptr) super(ptr, :sk_typeface_unref) end |
Class Method Details
.default ⇒ Object
32 33 34 35 36 37 |
# File 'lib/skia/typeface.rb', line 32 def self.default ptr = Native.sk_typeface_create_default return nil if ptr.nil? || ptr.null? new(ptr) end |
.fixed_16_16_to_float(raw) ⇒ Object
170 171 172 173 |
# File 'lib/skia/typeface.rb', line 170 def self.fixed_16_16_to_float(raw) signed = raw >= 0x8000_0000 ? raw - 0x1_0000_0000 : raw signed / 65_536.0 end |
.from_file(path, index = 0) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/skia/typeface.rb', line 25 def self.from_file(path, index = 0) ptr = Native.sk_typeface_create_from_file(path, index) raise FileNotFoundError, "Failed to load typeface: #{path}" if ptr.nil? || ptr.null? new(ptr) end |
.from_name(name, weight: WEIGHT_NORMAL, width: WIDTH_NORMAL, slant: :upright) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/skia/typeface.rb', line 13 def self.from_name(name, weight: WEIGHT_NORMAL, width: WIDTH_NORMAL, slant: :upright) style = Native.sk_fontstyle_new(weight, width, slant) begin ptr = Native.sk_typeface_create_from_name(name, style) return nil if ptr.nil? || ptr.null? new(ptr) ensure Native.sk_fontstyle_delete(style) if style && !style.null? end end |
.tag_to_string(tag_u32) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/skia/typeface.rb', line 156 def self.tag_to_string(tag_u32) big_endian = [tag_u32].pack('N') return big_endian if big_endian.match?(/\A[\x20-\x7E]{4}\z/) [tag_u32].pack('V') end |
.tag_to_uint32(tag) ⇒ Object
163 164 165 166 167 168 |
# File 'lib/skia/typeface.rb', line 163 def self.tag_to_uint32(tag) return tag if tag.is_a?(Integer) raise ArgumentError, 'tag must be a 4-character String or Integer' unless tag.is_a?(String) && tag.bytesize == 4 tag.unpack1('N') end |
Instance Method Details
#family_name ⇒ Object
39 40 41 |
# File 'lib/skia/typeface.rb', line 39 def family_name read_native_string(:sk_typeface_get_family_name) end |
#fixed_pitch? ⇒ Boolean
61 62 63 |
# File 'lib/skia/typeface.rb', line 61 def fixed_pitch? Native.sk_typeface_is_fixed_pitch(@ptr) end |
#glyph_count ⇒ Object
69 70 71 |
# File 'lib/skia/typeface.rb', line 69 def glyph_count Native.sk_typeface_count_glyphs(@ptr) end |
#post_script_name ⇒ Object
43 44 45 46 47 |
# File 'lib/skia/typeface.rb', line 43 def post_script_name return nil unless Native.function_available?(:sk_typeface_get_post_script_name) read_native_string(:sk_typeface_get_post_script_name) end |
#slant ⇒ Object
57 58 59 |
# File 'lib/skia/typeface.rb', line 57 def slant Native.sk_typeface_get_font_slant(@ptr) end |
#style ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/skia/typeface.rb', line 73 def style { weight: weight, width: width, slant: slant, fixed_pitch: fixed_pitch?, units_per_em: units_per_em } end |
#table_count ⇒ Object
83 84 85 |
# File 'lib/skia/typeface.rb', line 83 def table_count Native.sk_typeface_count_tables(@ptr) end |
#table_data(tag) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/skia/typeface.rb', line 96 def table_data(tag) tag_u32 = self.class.tag_to_uint32(tag) size = Native.sk_typeface_get_table_size(@ptr, tag_u32) return nil if size <= 0 data_ptr = FFI::MemoryPointer.new(:uint8, size) bytes_read = Native.sk_typeface_get_table_data(@ptr, tag_u32, 0, size, data_ptr) return nil if bytes_read <= 0 data_ptr.read_bytes(bytes_read) end |
#table_tags ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/skia/typeface.rb', line 87 def count = table_count return [] if count <= 0 = FFI::MemoryPointer.new(:uint32, count) read_count = Native.(@ptr, ) .read_array_of_uint32(read_count).map { |tag| self.class.tag_to_string(tag) } end |
#units_per_em ⇒ Object
65 66 67 |
# File 'lib/skia/typeface.rb', line 65 def units_per_em Native.sk_typeface_get_units_per_em(@ptr) end |
#variation_axes ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/skia/typeface.rb', line 108 def variation_axes fvar = table_data('fvar') return [] if fvar.nil? || fvar.bytesize < 16 version, offset_to_data, = fvar.unpack('Nn') return [] unless version == 0x0001_0000 axis_count = fvar.byteslice(8, 2).unpack1('n') axis_size = fvar.byteslice(10, 2).unpack1('n') return [] if axis_count <= 0 || axis_size < 20 axes = [] axis_count.times do |index| offset = offset_to_data + (index * axis_size) break if offset + 20 > fvar.bytesize record = fvar.byteslice(offset, 20) tag = record.byteslice(0, 4) min_raw, default_raw, max_raw, flags, name_id = record.byteslice(4, 16).unpack('N3n2') axes << { tag: tag, min_value: self.class.fixed_16_16_to_float(min_raw), default_value: self.class.fixed_16_16_to_float(default_raw), max_value: self.class.fixed_16_16_to_float(max_raw), hidden: (flags & 0x0001) != 0, name_id: name_id } end axes end |
#weight ⇒ Object
49 50 51 |
# File 'lib/skia/typeface.rb', line 49 def weight Native.sk_typeface_get_font_weight(@ptr) end |
#width ⇒ Object
53 54 55 |
# File 'lib/skia/typeface.rb', line 53 def width Native.sk_typeface_get_font_width(@ptr) end |