Browse Source

text: rewrite embedding to use set_shape_renderer

No more arcane unicode magic!

Closes #177

Related to #170, #171, #158, #103, and #153 too.
dev
Andy Meneely 9 years ago
parent
commit
ee2ed28550
  1. 9
      CHANGELOG.md
  2. 33
      lib/squib/api/text_embed.rb
  3. 28
      lib/squib/graphics/embedding_utils.rb
  4. 101
      lib/squib/graphics/text.rb
  5. 36
      samples/embed_text.rb
  6. 72
      spec/data/samples/embed_text.rb.txt
  7. 3
      spec/data/samples/text_options.rb.txt
  8. 73
      spec/graphics/embedding_utils_spec.rb
  9. 6
      spec/spec_helper.rb
  10. 8
      squib.gemspec

9
CHANGELOG.md

@ -1,7 +1,7 @@
# Squib CHANGELOG
Squib follows [semantic versioning](http://semver.org).
## v0.11.0 / 2016-06-27
## v0.11.0 / Unreleased
Features:
* Unit conversion supports mm (#161)
@ -9,12 +9,17 @@ Features:
Docs:
* Provide previews of each built-in template on the docs (#163)
* Documented lack of `:scale` behavior on text embedding. (#160)
Bugs:
* Fresh installs of Squib were broken due to two hidden dependencies, gio2 and gobject-introspection. (#172)
* Embedding icons in text show unicode placeholders on some OSs. This is a workaround until we get a better solution for embedding icons. See #170, #171, and #176. For that matter, see #103, #153, and #30 if you really want the whole story.
* Embedding icons in text show unicode placeholders on some OSs. Actually, all of icon embedding was one giant bug waiting to be squashed. I finally implemented this functionality using the proper API calls. See #177 for the feature, and then the bugs were #170, #171, #158, and #176. For that matter, see #103, #153, and #30 if you really want the whole story.
* With #177, the `:native` option for image width and height text embedding should work ().
* Unit conversion is supported when using `extends` in layouts, as promised in the docs (#173)
Compatibility:
* I reworked the way icons are embedded, and that was a big change internally (#177). We're now using Pango's API in the expected way and not dealing with "undefined behavior" situations with zero-sized fonts and obscure UTF-8 characters as we were before. But, as a result, Pango handles custom shapes a little differently than before. Wrapping doesn't render exactly the same way as before, although it's acceptable in most cases. If you still find problems, please file a bug.
Special thanks to everyone who tested, reported, suggested, and helped for this release! bcompter, rhyok, temetherian, rpond-pa, Nibodhika, briant-spindance, lcarlsen, spilth
## v0.10.0 / 2016-05-06

33
lib/squib/api/text_embed.rb

@ -23,21 +23,7 @@ module Squib
@rules = {} # store an array of options for later usage
end
# Context object for embedding an svg icon within text
#
# @option opts key [String] ('*') the string to replace with the graphic. Can be multiple letters, e.g. ':tool:'
# @option opts file [String] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:README.md#Specifying_Files Specifying Files}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts id [String] (nil) if set, then only render the SVG element with the given id. Prefix '#' is optional. Note: the x-y coordinates are still relative to the SVG document's page. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts force_id [Boolean] (false) if set, then this svg will not be rendered at all if the id is empty or nil. If not set, the entire SVG is rendered. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts width [Integer] (:native) the width of the image rendered.
# @option opts height [Integer] (:native) the height the height of the image rendered.
# @option opts dx [Integer] (0) "delta x", or adjust the icon horizontally by x pixels
# @option opts dy [Integer] (0) "delta y", or adjust the icon vertically by y pixels
# @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @api public
# DSL method. See http://squib.readthedocs.io
def svg(opts = {})
key = Args::EmbedKey.new.validate_key(opts[:key])
range = Args::CardRange.new(opts[:range], deck_size: @deck_size)
@ -47,7 +33,8 @@ module Squib
trans = Args::Transform.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
ifile = Args::InputFile.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
svg_args = Args::SvgSpecial.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
rule = { type: :png, file: ifile, box: box, paint: paint, trans: trans, adjust: adjust }
rule = { type: :svg, file: ifile, box: box, paint: paint, trans: trans,
adjust: adjust, svg_args: svg_args }
rule[:draw] = Proc.new do |card, x, y|
i = card.index
b = box[i]
@ -59,19 +46,7 @@ module Squib
@rules[key] = rule
end
# Context object for embedding a png within text
#
# @option opts key [String] ('*') the string to replace with the graphic. Can be multiple letters, e.g. ':tool:'
# @option opts file [String] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:README.md#Specifying_Files Specifying Files}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts width [Fixnum] (:native) the width of the image rendered
# @option opts height [Fixnum] (:native) the height of the image rendered
# @option opts dx [Integer] (0) "delta x", or adjust the icon horizontally by x pixels
# @option opts dy [Integer] (0) "delta y", or adjust the icon vertically by y pixels
# @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @api public
# DSL method. See http://squib.readthedocs.io
def png(opts = {})
key = Args::EmbedKey.new.validate_key(opts[:key])
range = Args::CardRange.new(opts[:range], deck_size: @deck_size)

28
lib/squib/graphics/embedding_utils.rb

@ -0,0 +1,28 @@
module Squib
class EmbeddingUtils
# Given a string and a bunch of keys, give us back a mapping of those keys
# to where those keys start, and where they end (in ranges)
#
# See the spec for expected outputs
def self.indices(str, keys)
map = {}
keys.each do |key|
map[key] ||= []
start = 0
while true
idx = str.index(key, start)
if idx.nil?
break; # done searching
else
idx_bytes = str[0..idx].bytesize - 1
map[key] << (idx_bytes..(idx_bytes + key.size))
start = idx + 1
end
end
end
return map
end
end
end

101
lib/squib/graphics/text.rb

@ -1,5 +1,6 @@
require 'pango'
require_relative '../args/typographer'
require_relative 'embedding_utils'
module Squib
class Card
@ -51,55 +52,59 @@ module Squib
layout.height = height * Pango::SCALE unless height.nil? || height == :auto
end
def max_embed_height(embed_draws)
embed_draws.inject(0) do |max, ed|
ed[:h] > max ? ed[:h] : max
end
end
# :nodoc:
# @api private
def next_embed(keys, str)
ret = nil
ret_key = nil
keys.each do |key|
i = str.index(key)
ret ||= i
unless i.nil? || i > ret
ret = i
ret_key = key
# Compute the width of the carve that we need
def compute_carve(rule, range)
w = rule[:box].width[@index]
if w == :native
file = rule[:file][@index].file
case rule[:type]
when :png
Squib.cache_load_image(file).width.to_f / (range.size - 1)
when :svg
svg_data = rule[:svg_args].data[@index]
unless file.to_s.empty? || svg_data.to_s.empty?
Squib.logger.warn 'Both an SVG file and SVG data were specified'
end
return 0 if (file.nil? or file.eql? '') and svg_data.nil?
svg_data = File.read(file) if svg_data.to_s.empty?
RSVG::Handle.new_from_data(svg_data).width
end
else
rule[:box].width[@index] * Pango::SCALE / (range.size - 1)
end
ret_key
end
# :nodoc:
# @api private
def process_embeds(embed, str, layout)
# # :nodoc:
# # @api private
def embed_images!(embed, str, layout, valign)
return [] unless embed.rules.any?
layout.markup = str
clean_str = layout.text
draw_calls = []
searches = []
while (key = next_embed(embed.rules.keys, clean_str)) != nil
rule = embed.rules[key]
spacing = rule[:box].width[@index] * Pango::SCALE
kindex = clean_str.index(key)
kindex = clean_str[0..kindex].bytesize # byte index (bug #57)
str = str.sub(key, "\u2062<span letter_spacing=\"#{spacing.to_i}\">\u2062</span>\u2062")
layout.markup = str
clean_str = layout.text
searches << { index: kindex, rule: rule }
attrs = layout.attributes || Pango::AttrList.new
EmbeddingUtils.indices(clean_str, embed.rules.keys).each do |key, ranges|
rule = embed.rules[key]
ranges.each do |range|
carve = Pango::Rectangle.new(0, 0, compute_carve(rule, range), 0)
att = Pango::AttrShape.new(carve, carve, rule)
att.start_index = range.first
att.end_index = range.last
attrs.insert(att)
end
end
searches.each do |search|
rect = layout.index_to_pos(search[:index])
x = Pango.pixels(rect.x) + search[:rule][:adjust].dx[@index]
y = Pango.pixels(rect.y) + search[:rule][:adjust].dy[@index]
h = rule[:box].height[@index]
draw_calls << { x: x, y: y, h: h, # defer drawing until we've valigned
draw: search[:rule][:draw] }
layout.attributes = attrs
layout.context.set_shape_renderer do |cxt, att, do_path|
unless do_path # when stroking the text
rule = att.data
x = Pango.pixels(layout.index_to_pos(att.start_index).x) +
rule[:adjust].dx[@index]
y = Pango.pixels(layout.index_to_pos(att.start_index).y) +
rule[:adjust].dy[@index] +
compute_valign(layout, valign, rule[:box].height[@index])
rule[:draw].call(self, x, y)
cxt.reset_clip
[cxt, att, do_path]
end
end
return draw_calls
end
def stroke_outline!(cc, layout, draw)
@ -146,24 +151,16 @@ module Squib
layout.justify = para.justify unless para.justify.nil?
layout.spacing = para.spacing unless para.spacing.nil?
embed_draws = process_embeds(embed, para.str, layout)
embed_images!(embed, para.str, layout, para.valign)
vertical_start = compute_valign(layout, para.valign, max_embed_height(embed_draws))
cc.move_to(0, vertical_start) # TODO clean this up a bit
vertical_start = compute_valign(layout, para.valign, 0)
cc.move_to(0, vertical_start)
stroke_outline!(cc, layout, draw) if draw.stroke_strategy == :stroke_first
cc.move_to(0, vertical_start)
cc.show_pango_layout(layout)
stroke_outline!(cc, layout, draw) if draw.stroke_strategy == :fill_first
begin
embed_draws.each { |ed| ed[:draw].call(self, ed[:x], ed[:y] + vertical_start) }
rescue Exception => e
puts '====EXCEPTION!===='
puts e
puts 'If this was a non-invertible matrix error, this is a known issue with a potential workaround. Please report it at: https://github.com/andymeneely/squib/issues/55'
puts '=================='
raise e
end
draw_text_hint(cc, box.x, box.y, layout, para.hint)
extents = { width: layout.extents[1].width / Pango::SCALE,
height: layout.extents[1].height / Pango::SCALE }

36
samples/embed_text.rb

@ -8,10 +8,9 @@ Squib::Deck.new do
text(str: embed_text, font: 'Sans 21',
x: 0, y: 0, width: 180, hint: :red,
align: :left, ellipsize: false, justify: false) do |embed|
# Notice how we use dx and dy to adjust the icon to not rest directly on the baseline
embed.svg key: ':tool:', width: 28, height: 28, dx: 0, dy: 4, file: 'spanner.svg'
embed.svg key: ':health:', width: 28, height: 28, dx: -2, dy: 4, file: 'glass-heart.svg'
embed.png key: ':purse:', width: 28, height: 28, dx: 0, dy: 4, file: 'shiny-purse.png'
embed.svg key: ':tool:', width: 28, height: 28, file: 'spanner.svg'
embed.svg key: ':health:', width: 28, height: 28, file: 'glass-heart.svg'
embed.png key: ':purse:', width: 28, height: 28, file: 'shiny-purse.png'
end
embed_text = 'Middle align: Take 1 :tool: and gain 2 :health:. Take 2 :tool: and gain 3 :purse:'
@ -32,11 +31,11 @@ Squib::Deck.new do
embed.png key: ':purse:', width: 28, height: 28, file: 'shiny-purse.png'
end
embed_text = 'Wrapping multiples: These are 1 :tool::tool::tool: and these are multiple :tool::tool: :tool::tool:'
text(str: embed_text, font: 'Sans 21',
x: 600, y: 0, width: 180, height: 300, valign: :middle,
embed_text = 'Yes, this wraps strangely. We are trying to determine the cause. These are 1 :tool::tool::tool: and these are multiple :tool::tool: :tool::tool:'
text(str: embed_text, font: 'Sans 18',
x: 600, y: 0, width: 180, height: 300, wrap: :word_char,
align: :left, ellipsize: false, justify: false, hint: :cyan) do |embed|
embed.svg key: ':tool:', width: 28, height: 28, file: 'spanner.svg'
embed.svg key: ':tool:', width: 28, height: 28, file: 'spanner.svg'
end
embed_text = ':tool:Justify will :tool: work too, and :purse: with more words just for fun'
@ -87,13 +86,32 @@ Squib::Deck.new do
embed_text = ":tool: Death to Nemesis bug 103!! :purse:"
text(str: embed_text, font: 'Sans Bold 24', stroke_width: 2,
color: :red, stroke_color: :blue, dash: '3 3', align: :center,
color: :red, stroke_color: :blue, dash: '3 3', align: :left,
valign: :middle, x: 0, y: 700, width: 380, height: 150,
hint: :magenta) do |embed|
embed.svg key: ':tool:', file: 'spanner.svg', width: 32, height: 32
embed.png key: ':purse:', file: 'shiny-purse.png', width: 32, height: 32
end
embed_text = 'You can adjust the icon with dx and dy. Normal: :tool: Adjusted: :heart:'
text(str: embed_text, font: 'Sans 18', x: 400, y: 640, width: 180,
height: 300, hint: :magenta) do |embed|
embed.svg key: ':tool:', width: 28, height: 28, file: 'spanner.svg'
embed.svg key: ':heart:', width: 28, height: 28, dx: 10, dy: 10,
file: 'glass-heart.svg'
end
embed_text = "Native sizes work too\n:tool:\n\n\n\n\n\n:shiny-purse:\n\n\n\n\n\n:tool2:"
text(str: embed_text, font: 'Sans 18', x: 600, y: 640, width: 180,
height: 475, hint: :magenta) do |embed|
embed.svg key: ':tool:', width: :native, height: :native,
file: 'spanner.svg'
embed.svg key: ':tool2:', width: :native, height: :native,
data: File.open('spanner.svg','r').read
embed.png key: ':shiny-purse:', width: :native, height: :native,
file: 'shiny-purse.png'
end
save_png prefix: 'embed_'
end

72
spec/data/samples/embed_text.rb.txt

@ -30,6 +30,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["Take 11 :tool: and gain 2 :health:. Take <b>2</b> :tool: <i>and gain 3 :purse: if level 2.</i>"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0])
cairo: move_to([0, 0])
cairo: show_pango_layout([MockDouble])
@ -53,6 +54,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["Middle align: Take 1 :tool: and gain 2 :health:. Take 2 :tool: and gain 3 :purse:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
cairo: show_pango_layout([MockDouble])
@ -76,6 +78,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["This :tool: aligns on the bottom properly. :purse:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
cairo: show_pango_layout([MockDouble])
@ -91,16 +94,17 @@ cairo: translate([600, 0])
cairo: rotate([0])
cairo: move_to([0, 0])
pango: font_description=([MockDouble])
pango: text=(["Wrapping multiples: These are 1 :tool::tool::tool: and these are multiple :tool::tool: :tool::tool:"])
pango: text=(["Yes, this wraps strangely. We are trying to determine the cause. These are 1 :tool::tool::tool: and these are multiple :tool::tool: :tool::tool:"])
pango: width=([184320])
pango: height=([307200])
pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["Wrapping multiples: These are 1 :tool::tool::tool: and these are multiple :tool::tool: :tool::tool:"])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
pango: markup=(["Yes, this wraps strangely. We are trying to determine the cause. These are 1 :tool::tool::tool: and these are multiple :tool::tool: :tool::tool:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0])
cairo: move_to([0, 0])
cairo: show_pango_layout([MockDouble])
cairo: rounded_rectangle([0, 0, 0, 0, 0, 0])
cairo: set_source_color([:cyan])
@ -122,6 +126,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([true])
pango: markup=([":tool:Justify will :tool: work too, and :purse: with more words just for fun"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
cairo: show_pango_layout([MockDouble])
@ -145,6 +150,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment right>])
pango: justify=([false])
pango: markup=(["Right-aligned works :tool: with :health: and :purse:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
cairo: show_pango_layout([MockDouble])
@ -168,6 +174,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment center>])
pango: justify=([false])
pango: markup=([":tool:Center-aligned works :tool: with :health: and :purse:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0])
cairo: move_to([0, 0])
cairo: show_pango_layout([MockDouble])
@ -192,6 +199,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment center>])
pango: justify=([false])
pango: markup=(["foo"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0])
cairo: move_to([0, 0])
cairo: show_pango_layout([MockDouble])
@ -216,6 +224,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment center>])
pango: justify=([false])
pango: markup=(["foo"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
cairo: show_pango_layout([MockDouble])
@ -240,6 +249,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment center>])
pango: justify=([false])
pango: markup=(["foo"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
cairo: show_pango_layout([MockDouble])
@ -260,9 +270,10 @@ pango: width=([389120])
pango: height=([153600])
pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment center>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=([":tool: Death to Nemesis bug 103!! :purse:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
cairo: show_pango_layout([MockDouble])
@ -280,6 +291,54 @@ cairo: set_line_width([2.0])
cairo: stroke([])
pango: ellipsized?([])
cairo: restore([])
cairo: save([])
cairo: set_source_color(["black"])
cairo: translate([400, 640])
cairo: rotate([0])
cairo: move_to([0, 0])
pango: font_description=([MockDouble])
pango: text=(["You can adjust the icon with dx and dy. Normal: :tool: Adjusted: :heart:"])
pango: width=([184320])
pango: height=([307200])
pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["You can adjust the icon with dx and dy. Normal: :tool: Adjusted: :heart:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0])
cairo: move_to([0, 0])
cairo: show_pango_layout([MockDouble])
cairo: rounded_rectangle([0, 0, 0, 0, 0, 0])
cairo: set_source_color([:magenta])
cairo: set_line_width([2.0])
cairo: stroke([])
pango: ellipsized?([])
cairo: restore([])
cairo: save([])
cairo: set_source_color(["black"])
cairo: translate([600, 640])
cairo: rotate([0])
cairo: move_to([0, 0])
pango: font_description=([MockDouble])
pango: text=(["Native sizes work too\n:tool:\n\n\n\n\n\n:shiny-purse:\n\n\n\n\n\n:tool2:"])
pango: width=([184320])
pango: height=([486400])
pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["Native sizes work too\n:tool:\n\n\n\n\n\n:shiny-purse:\n\n\n\n\n\n:tool2:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0])
cairo: move_to([0, 0])
cairo: show_pango_layout([MockDouble])
cairo: rounded_rectangle([0, 0, 0, 0, 0, 0])
cairo: set_source_color([:magenta])
cairo: set_line_width([2.0])
cairo: stroke([])
pango: ellipsized?([])
cairo: restore([])
surface: write_to_png(["_output/embed_00.png"])
cairo: antialias=(["subpixel"])
cairo: antialias=(["subpixel"])
@ -299,6 +358,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["Take 1 :tool: and gain 2 :health:."])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
cairo: show_pango_layout([MockDouble])
@ -323,6 +383,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["Take 1 :tool: and gain 2 :health:."])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
cairo: show_pango_layout([MockDouble])
@ -347,6 +408,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["Take 1 :tool: and gain 2 :health:."])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0.0])
cairo: move_to([0, 0.0])
cairo: show_pango_layout([MockDouble])

3
spec/data/samples/text_options.rb.txt

@ -803,6 +803,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["Embedded icons! Take 1 :tool: and gain 2:health:. If Level 2, take 2 :tool:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0])
cairo: move_to([0, 0])
cairo: show_pango_layout([MockDouble])
@ -821,6 +822,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["Embedded icons! Take 1 :tool: and gain 2:health:. If Level 2, take 2 :tool:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0])
cairo: move_to([0, 0])
cairo: show_pango_layout([MockDouble])
@ -839,6 +841,7 @@ pango: ellipsize=([#<Pango::Layout::EllipsizeMode none>])
pango: alignment=([#<Pango::Layout::Alignment left>])
pango: justify=([false])
pango: markup=(["Embedded icons! Take 1 :tool: and gain 2:health:. If Level 2, take 2 :tool:"])
pango: attributes=([Pango::AttrList])
cairo: move_to([0, 0])
cairo: move_to([0, 0])
cairo: show_pango_layout([MockDouble])

73
spec/graphics/embedding_utils_spec.rb

@ -0,0 +1,73 @@
require 'spec_helper'
require 'squib/graphics/embedding_utils'
describe Squib::EmbeddingUtils do
context(:indices) do
it 'returns nothing when given nothing' do
expect(Squib::EmbeddingUtils.indices('just some text', [])).to eq({})
end
it 'returns emptiness for given keys that are not in the string' do
str = 'just some text'
keys = [':tool:']
expect(Squib::EmbeddingUtils.indices(str, keys)).to eq({
':tool:' => []
})
end
it 'returns correctly for one key, one time' do
str = 'some :tool: text'
keys = [':tool:']
expect(Squib::EmbeddingUtils.indices(str, keys)).to eq({
':tool:' => [5..11]
})
end
it 'handles one key, multiple times' do
str = 'some :tool: text :tool:'
keys = [':tool:']
expect(Squib::EmbeddingUtils.indices(str, keys)).to eq({
':tool:' => [5..11, 17..23]
})
end
it 'handles one key, multiple times next to each other' do
str = 'some :tool::tool: text'
keys = [':tool:']
expect(Squib::EmbeddingUtils.indices(str, keys)).to eq({
':tool:' => [5..11, 11..17]
})
end
it 'handles multiple keys, one time each' do
str = 'some :tool: heart text'
keys = %w(:tool: heart)
expect(Squib::EmbeddingUtils.indices(str, keys)).to eq({
':tool:' => [5..11],
'heart' => [12..17]
})
end
it 'handles multiple keys, multiple times each' do
str = ':tool:some :tool: heart text heart tool'
keys = %w(:tool: heart)
expect(Squib::EmbeddingUtils.indices(str, keys)).to eq({
':tool:' => [0..6, 11..17],
'heart' => [18..23, 29..34]
})
end
it 'handles multibyte properly' do
str = '💡 📷 :tool: heart text'
keys = %w(:tool: heart 💡)
expect(Squib::EmbeddingUtils.indices(str, keys)).to eq({
':tool:' => [10..16],
'heart' => [17..22],
'💡' => [3..4]
})
end
end
end

6
spec/spec_helper.rb

@ -65,6 +65,7 @@ def scrub_hex(str)
str.gsub(/0x\w{1,8}/, '')
.gsub(/ptr=\w{1,8}/, '')
.gsub(/#<Pango::FontDescription:.*>/, '')
.gsub(/#<Pango::AttrList:.*>/, 'Pango::AttrList')
.gsub(/#<Cairo::ImageSurface:.*>/, 'ImageSurface')
.gsub(/#<Cairo::LinearPattern:.*>/, 'LinearPattern')
.gsub(/#<Cairo::RadialPattern:.*>/, 'RadialPattern')
@ -103,6 +104,8 @@ def mock_cairo(strio)
allow(pango).to receive(:alignment).and_return(Pango::Layout::Alignment::LEFT)
allow(pango).to receive(:text).and_return('foo')
allow(pango).to receive(:context).and_return(pango_cxt)
allow(pango).to receive(:attributes).and_return(nil)
allow(pango_cxt).to receive(:set_shape_renderer)
allow(pango_cxt).to receive(:font_options=)
allow(iter).to receive(:next_char!).and_return(false)
allow(iter).to receive(:char_extents).and_return(Pango::Rectangle.new(5, 5, 5, 5))
@ -120,7 +123,8 @@ def mock_cairo(strio)
end
%w(font_description= text= width= height= wrap= ellipsize= alignment=
justify= spacing= markup= ellipsized?).each do |m|
justify= spacing= markup= ellipsized? attributes=
set_shape_renderer).each do |m|
allow(pango).to receive(m) {|*args| strio << scrub_hex("pango: #{m}(#{args})\n") }
end

8
squib.gemspec

@ -27,15 +27,15 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
spec.require_paths = ['lib']
spec.add_runtime_dependency 'gobject-introspection', '~> 3.0.0' # for bug 172
spec.add_runtime_dependency 'gio2', '~> 3.0.0' # for bug 172
spec.add_runtime_dependency 'cairo', '~> 1.15.2'
spec.add_runtime_dependency 'pango', '~> 3.0.1'
spec.add_runtime_dependency 'pango', '~> 3.0.9'
spec.add_runtime_dependency 'nokogiri', '~> 1.6.7'
spec.add_runtime_dependency 'roo', '~> 2.4.0'
spec.add_runtime_dependency 'rsvg2', '~> 3.0.0'
spec.add_runtime_dependency 'rsvg2', '~> 3.0.9'
spec.add_runtime_dependency 'mercenary', '~> 0.3.4'
spec.add_runtime_dependency 'ruby-progressbar', '~> 1.8'
spec.add_runtime_dependency 'gobject-introspection', '~> 3.0.9' # for bug 172
spec.add_runtime_dependency 'gio2', '~> 3.0.9' # for bug 172
spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'rake'

Loading…
Cancel
Save