Debian Codegraph @

core/mui/cairo_markup_generator.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # frozen_string_literal: true
3
4 require 'gtk3'
5 require 'lib/diva_hacks'
6
7 module Pango
8 ESCAPE_RULE = {'&' => '&amp;' ,'>' => '&gt;', '<' => '&lt;'}.freeze
9 class << self
10
11 # テキストをPango.parse_markupで安全にパースできるようにエスケープする。
12 def escape(text)
13 text.gsub(/[<>&]/){|m| ESCAPE_RULE[m] }
14 end
15
16 alias old_parse_markup parse_markup
17
18 # パースエラーが発生した場合、その文字列をerrorで印字する。
19 def parse_markup(str)
20 old_parse_markup(str)
21 rescue GLib::Error => e
22 error str
23 raise e
24 end
25 end
26 end
27
28 =begin rdoc
29 本文の、描画するためのテキストを生成するモジュール。
30 =end
31
32 module Gdk::MarkupGenerator
33 # 表示する際に本文に適用すべき装飾オブジェクトを作成する
34 # ==== Return
35 # Pango::AttrList 本文に適用する装飾
36 def description_attr_list(attr_list=Pango::AttrList.new, emoji_height: 24, pango_context: nil)
37 descent = 0
38 if pango_context
39 metrics = pango_context.get_metrics(pango_context.font_description, pango_context.language)
40 descent = metrics.descent
41 end
42 height = emoji_height * Pango::SCALE
43 y = -height + descent
44 score.inject(0){|start_index, note|
45 end_index = start_index + note.description.bytesize
46 if UserConfig[:miraclepainter_expand_custom_emoji] && note.respond_to?(:inline_photo)
47 end_index += -note.description.bytesize + 1
48 rect = Pango::Rectangle.new(0, y, height, height)
49 shape = Pango::AttrShape.new(rect, rect, note.inline_photo)
50 shape.start_index = start_index
51 shape.end_index = end_index
52 attr_list.insert(shape)
53 elsif clickable?(note)
54 underline = Pango::AttrUnderline.new(Pango::Underline::SINGLE)
55 underline.start_index = start_index
56 underline.end_index = end_index
57 attr_list.insert(underline)
58 end
59 end_index
60 }
61 attr_list
62 end
63
64 def clickable?(model)
65 has_model_intent = Plugin.collect(:intent_select_by_model_slug, model.class.slug, Pluggaloid::COLLECT).first
66 return true if has_model_intent
67 Plugin.collect(:model_of_uri, model.uri).any? do |model_slug|
68 Plugin.collect(:intent_select_by_model_slug, model_slug, Pluggaloid::COLLECT).first
69 end
70 end
71
72 # Entityを適用したあとのプレーンテキストを返す。
73 # Pangoの都合上、絵文字は1文字で表現する
74 def plain_description
75 _plain_description[UserConfig[:miraclepainter_expand_custom_emoji]]
76 end
77
78 private def _plain_description
79 @_plain_description ||= Hash.new do |h, expand_emoji|
80 h[expand_emoji] = score.map{|note|
81 if expand_emoji && note.respond_to?(:inline_photo)
82 '.'
83 else
84 note.description
85 end
86 }.to_a.join
87 end
88 end
89
90 end