Module: Cooltrainer::DistorteD::Technology::Pango
- Included in:
- Molecule::Font, Molecule::Text
- Defined in:
- lib/distorted/modular_technology/pango.rb
Instance Method Summary collapse
-
#cr ⇒ Object
Returns a Pango-escaped Carriage Return.
-
#crlf ⇒ Object
Returns a Pango’escaped CRLF pair.
-
#g_markup_escape_char(c) ⇒ Object
The char-by-char actual function used by g_markup_escape_text.
-
#g_markup_escape_text(text) ⇒ Object
Escape text as necessary for Pango Markup, which is what Vips::Image.text() expects for its argv.
-
#lf ⇒ Object
Returns a Pango-escapped Line Feed.
-
#overlong_null ⇒ Object
“Modified UTF-8” uses a normally-illegal byte sequence to encode the NULL character so 0x00 can exclusively be a string terminator.
Instance Method Details
#cr ⇒ Object
Returns a Pango-escaped Carriage Return. Use this for linebreaking Pango Markup output.
24 25 26 |
# File 'lib/distorted/modular_technology/pango.rb', line 24 def cr g_markup_escape_char(0x0D) end |
#crlf ⇒ Object
Returns a Pango’escaped CRLF pair. Also not needed for anything.
37 38 39 |
# File 'lib/distorted/modular_technology/pango.rb', line 37 def crlf cr << lf end |
#g_markup_escape_char(c) ⇒ Object
The char-by-char actual function used by g_markup_escape_text
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/distorted/modular_technology/pango.rb', line 49 def g_markup_escape_char(c) # I think a fully-working version of this function would # be as simple as `sprintf('&#x%x;', c.ord)` ALL THE THINGS, # but I want to copy the structure of the C implementation # as closely as possible, which means using the named escape # sequences for common characters and separating the # Latin-1 Supplement range from the other # the Unicode control characters (> 0x7f) even though three's no # need to in Ruby. case c.ord when '&'.ord '&' when '<'.ord '<' when '>'.ord '>' when '\''.ord ''' when '"'.ord '"' when 0x1..0x8, 0xb..0xc, 0xe..0x1f, 0x7f sprintf('&#x%x;', c.ord) when 0x80..0x84, 0x86..0x9f # The original C implementation separates this range # from the above range due to its need to handle the # UTF control character bytes with gunichar: # https://wiki.tcl-lang.org/page/UTF%2D8+bit+by+bit # https://www.fileformat.info/info/unicode/utf8.htm # Ruby has already done this for us here :) sprintf('&#x%x;', c.ord) when 0x0 # what's this…? # Avoid a `ArgumentError: string contains null byte` # by not printing one :) else c end end |
#g_markup_escape_text(text) ⇒ Object
Escape text as necessary for Pango Markup, which is what Vips::Image.text() expects for its argv. This code should be in GLib but is unimplemented in Ruby’s:
ruby-gnome2.osdn.jp/hiki.cgi?Gtk%3A%3ALabel#Markup+%28styled+text%29 “The markup passed to Gtk::Label#set_markup() must be valid; for example, literal </>/& characters must be escaped as <, >, and &. If you pass text obtained from the user, file, or a network to Gtk::Label#set_markup(), you’ll want to escape it with GLib::Markup.escape_text?(not implemented yet).”
Base my own implementation on the original C version found in gmarkup: gitlab.gnome.org/GNOME/glib/-/blob/master/glib/gmarkup.c
18 19 20 |
# File 'lib/distorted/modular_technology/pango.rb', line 18 def g_markup_escape_text(text) text.map{ |c| g_markup_escape_char(c) } end |
#lf ⇒ Object
Returns a Pango-escapped Line Feed. This isn’t used/needed for anything with Pango but it felt weird to include CR and not LF lmao
31 32 33 |
# File 'lib/distorted/modular_technology/pango.rb', line 31 def lf g_markup_escape_char(0x0A) end |
#overlong_null ⇒ Object
“Modified UTF-8” uses a normally-illegal byte sequence to encode the NULL character so 0x00 can exclusively be a string terminator.
44 45 46 |
# File 'lib/distorted/modular_technology/pango.rb', line 44 def overlong_null [0xC0, 0x80].pack('C*').force_encoding('UTF-8') end |