csv_preview 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,621 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CsvPreview
4
+ # The only code that calls `Vips::Image.text`. One call per string both measures and draws it: the
5
+ # returned image is kept in a {Run} and composited later, so measuring and drawing can never
6
+ # disagree (rendering.md section 4). The one exception: on hosts where libvips draws subpixel
7
+ # (coloured) edges, a string that can contain a colour glyph is drawn a second time, in a
8
+ # contrasting colour with the same layout, to tell its emoji from its text (rendering.md section 9).
9
+ #
10
+ # ruby-vips is required lazily, on first use ({supported?}, {warm_up!} or the first render), never
11
+ # when this file is loaded, so the gem loads on hosts without libvips.
12
+ #
13
+ # Text is always greyscale-antialiased: on hosts where libvips draws subpixel (coloured) edges, each
14
+ # run is corrected after drawing (rendering.md section 9).
15
+ #
16
+ # Process-global state (font registration, the feature probe, the antialiasing probe, the font
17
+ # self-check, {Metrics} and ellipsis extents) is computed once under a Mutex. Everything else lives in
18
+ # a per-render {Renderer}.
19
+ # @api private
20
+ module Text
21
+ # Width of the box right-aligned strings are laid out in. Pango puts the line's logical right
22
+ # edge exactly here, which is how the logical width is recovered (rendering.md 4.3).
23
+ RIGHT_BOX = 32_768
24
+ ELLIPSIS = "\u2026"
25
+
26
+ FONT_DIR = File.expand_path("../../fonts", __dir__)
27
+ REGULAR = File.join(FONT_DIR, "Inter-Regular.ttf")
28
+ SEMIBOLD = File.join(FONT_DIR, "Inter-SemiBold.ttf")
29
+ BUNDLED_FILES = [REGULAR, SEMIBOLD].freeze
30
+
31
+ # Same set as GLib's g_markup_escape_text.
32
+ ESCAPES = { "&" => "&amp;", "<" => "&lt;", ">" => "&gt;", '"' => "&quot;", "'" => "&apos;" }.freeze
33
+ ESCAPE_PATTERN = /[&<>"']/
34
+
35
+ # Strings with no ink: libvips would raise "no text to render" for them (rendering.md 4.4).
36
+ BLANK = /\A[\p{Space}\p{Default_Ignorable_Code_Point}]*\z/
37
+ NO_TEXT = "no text to render"
38
+ INVALID_MARKUP = "invalid markup"
39
+
40
+ # Self-check probes at 100 px, right-aligned, and the extents accepted as the bundled Inter 4.1
41
+ # (rendering.md 5.4). Inter measures about 644 px for "0000000000" with tabular figures. DejaVu Sans
42
+ # and Verdana measure 634 px there, so a second probe draws "1111111111" with the default figures:
43
+ # Inter's are proportional (about 406 px), theirs are tabular (about 630 px).
44
+ SELF_CHECK_EXTENT = (630..660)
45
+ SELF_CHECK_PROPORTIONAL_EXTENT = (380..440)
46
+ # Ends messages about the libvips, Pango or font installation.
47
+ INSTALL_HINT = "see #{Help::INSTALLATION}"
48
+ # Ends every "failed to draw text" message; libvips' own message stays on the error's cause.
49
+ DRAW_HINT = "check the libvips, Pango and font installation, #{INSTALL_HINT}"
50
+ ALIGNMENTS = %i[left right].freeze
51
+ PROCESS_CACHE_LIMIT = 256
52
+
53
+ # Characters that can draw as a colour glyph (an emoji) instead of in the markup colour:
54
+ # pictographs, emoji-presentation characters, skin-tone modifiers, VS16, the keycap mark and emoji
55
+ # tag characters (rendering.md section 9).
56
+ COLOR_GLYPH = /\p{Extended_Pictographic} | \p{Emoji_Presentation} | \p{Emoji_Modifier}
57
+ | [\u{FE0F}\u{20E3}\u{E0020}-\u{E007F}]/x
58
+ # Antialiasing probe (rendering.md section 9): mid-grey text composited over white keeps its
59
+ # channels equal when antialiasing is greyscale (largest summed channel difference 0, measured on
60
+ # libvips 8.15 and 8.18). Subpixel antialiasing (rgb, bgr or vrgb) gives its edges about 160.
61
+ SUBPIXEL_PROBE = "Hamburgefonstiv"
62
+ SUBPIXEL_PROBE_COLOR = "#808080"
63
+ SUBPIXEL_SPREAD = 24
64
+
65
+ # Font and colour of one string. Sizes are device px (already multiplied by `scale`). The values
66
+ # are interpolated into Pango markup attributes, so they are validated here as well as in the
67
+ # theme.
68
+ #
69
+ # @!attribute family [String] Pango family list, for example "Inter"
70
+ # @!attribute fontfile [String, nil] font file to register, nil for system fonts (`font:` option)
71
+ # @!attribute size_px [Integer] font size in device px
72
+ # @!attribute weight [Integer] numeric weight, 400 regular, 600 semibold
73
+ # @!attribute color [String] "#RRGGBB"
74
+ # @!attribute tnum [Boolean] request tabular figures
75
+ Style = Data.define(:family, :fontfile, :size_px, :weight, :color, :tnum) do
76
+ def initialize(family:, fontfile:, size_px:, weight:, color:, tnum:)
77
+ unless family.is_a?(String) && !family.strip.empty? && !family.match?(/[[:cntrl:]]/)
78
+ raise ArgumentError, "family must be a non-empty String without control characters"
79
+ end
80
+ raise ArgumentError, "fontfile must be nil or a String" unless fontfile.nil? || fontfile.is_a?(String)
81
+ raise ArgumentError, "size_px must be a positive Integer" unless size_px.is_a?(Integer) && size_px.positive?
82
+ unless weight.is_a?(Integer) && (100..1000).cover?(weight)
83
+ raise ArgumentError, "weight must be an Integer from 100 to 1000"
84
+ end
85
+ raise ArgumentError, "color must be #RRGGBB" unless color.is_a?(String) && color.match?(/\A#\h{6}\z/)
86
+ raise ArgumentError, "tnum must be true or false" unless [true, false].include?(tnum)
87
+
88
+ super
89
+ end
90
+ end
91
+
92
+ # One rendered string: the image is both the measurement and the drawing.
93
+ #
94
+ # @!attribute string [String] the string drawn (NUL and invalid UTF-8 replaced by U+FFFD)
95
+ # @!attribute image [Vips::Image] 4-band sRGB image cropped to the ink rectangle
96
+ # @!attribute xoffset [Integer] ink left relative to the layout origin
97
+ # @!attribute yoffset [Integer] ink top relative to the line top
98
+ # @!attribute extent [Integer] device px the string needs: for left-aligned runs from the origin
99
+ # to the ink's right edge, for right-aligned runs from the ink's left edge to the logical right edge
100
+ # @!attribute align [Symbol] :left or :right, as requested
101
+ Run = Data.define(:string, :image, :xoffset, :yoffset, :extent, :align) do
102
+ # @return [Boolean] true when a right-aligned string turned out to be a right-to-left paragraph
103
+ # (Pango swapped the alignment); it is measured and placed by its ink (rendering.md 4.3)
104
+ def right_to_left?
105
+ align == :right && xoffset + image.width <= RIGHT_BOX / 2
106
+ end
107
+
108
+ # @param left [Integer] left edge of the text area (device px)
109
+ # @param right [Integer] right edge of the text area (device px)
110
+ # @return [Integer] x of the image's left edge
111
+ def x_in(left, right)
112
+ if align == :left
113
+ left + xoffset
114
+ elsif right_to_left?
115
+ right - image.width
116
+ else
117
+ right - RIGHT_BOX + xoffset
118
+ end
119
+ end
120
+ end
121
+
122
+ # Cap-height metrics of one style, from rendering "H" (rendering.md 4.6).
123
+ Metrics = Data.define(:cap_top, :cap_height) do
124
+ # @return [Integer] y of the line top that centres cap height in the band; a run is drawn at
125
+ # `line_top + run.yoffset`, so every run in the band shares one baseline
126
+ def line_top(band_top, band_height)
127
+ band_top + ((band_height - cap_height) / 2.0).round - cap_top
128
+ end
129
+ end
130
+
131
+ # Per-render text cache: repeated strings cost one libvips call (two for a string that
132
+ # {Text.text_calls} says is drawn twice). Counts every libvips call it makes and checks the deadline
133
+ # whenever the count reaches the next multiple of 32, so a string that adds two calls cannot step
134
+ # over a checkpoint. Not shared between threads.
135
+ class Renderer
136
+ DEADLINE_EVERY = 32
137
+
138
+ # @return [Integer] libvips text calls made by this renderer
139
+ attr_reader :calls
140
+
141
+ # @param deadline [#check!, nil]
142
+ def initialize(deadline: nil)
143
+ @deadline = deadline
144
+ @calls = 0
145
+ @next_deadline_check = DEADLINE_EVERY
146
+ @cache = {}
147
+ end
148
+
149
+ # @param string [String]
150
+ # @param style [Style]
151
+ # @param align [Symbol] :left or :right
152
+ # @return [Run, nil] nil when the string is blank: nothing is drawn and its extent is 0
153
+ # @raise [RenderFailed] when libvips is missing, lacks text support, or fails to draw
154
+ def render(string, style:, align:)
155
+ raise ArgumentError, "align must be :left or :right" unless ALIGNMENTS.include?(align)
156
+ raise ArgumentError, "style must be a CsvPreview::Text::Style" unless style.is_a?(Style)
157
+
158
+ key = [string, style, align]
159
+ return @cache[key] if @cache.key?(key)
160
+
161
+ @cache[key] = draw(Text.display_string(string), style, align)
162
+ end
163
+
164
+ # Ellipsis truncation by measuring (rendering.md 4.5): the longest prefix of whole grapheme
165
+ # clusters, trailing spaces removed, plus U+2026 that fits in `max_extent`.
166
+ #
167
+ # @param run [Run, nil] the full string's run
168
+ # @param style [Style] the style `run` was rendered with
169
+ # @param max_extent [Integer] device px available
170
+ # @return [Run, nil] `run` itself when it fits; nil when even the ellipsis alone does not fit
171
+ def fit(run, style:, max_extent:)
172
+ return run if run.nil? || run.extent <= max_extent
173
+
174
+ align = run.align
175
+ ellipsis = Text.ellipsis_extent(style, align)
176
+ return nil if ellipsis > max_extent
177
+
178
+ clusters = run.string.grapheme_clusters
179
+ probes = {}
180
+ fits = lambda do |k|
181
+ next true if k.zero?
182
+
183
+ probe = probes[k] ||= draw(clusters[0, k].join.rstrip + ELLIPSIS, style, align)
184
+ !probe.nil? && probe.extent <= max_extent
185
+ end
186
+
187
+ n = clusters.size
188
+ guess = ((max_extent - ellipsis) * n / run.extent.to_f).floor.clamp(0, n - 1)
189
+ k = largest_fitting(guess, n - 1, &fits)
190
+ k.zero? ? render(ELLIPSIS, style: style, align: align) : probes.fetch(k)
191
+ end
192
+
193
+ private
194
+
195
+ def draw(text, style, align)
196
+ return nil if text.match?(BLANK)
197
+
198
+ Text.prepare!(style)
199
+ @calls += Text.text_calls(text)
200
+ if @calls >= @next_deadline_check
201
+ @deadline&.check!(:text)
202
+ @next_deadline_check = ((@calls / DEADLINE_EVERY) + 1) * DEADLINE_EVERY
203
+ end
204
+ Text.build_run(text, style, align)
205
+ end
206
+
207
+ # Gallop from `guess` (up if it fits, down otherwise) with steps 1, 2, 4, ... until the answer is
208
+ # bracketed, then binary-search the bracket. fits(0) is always true.
209
+ def largest_fitting(guess, max_k)
210
+ if yield(guess)
211
+ lo = guess
212
+ hi = max_k
213
+ step = 1
214
+ while lo + step <= max_k
215
+ unless yield(lo + step)
216
+ hi = lo + step - 1
217
+ break
218
+ end
219
+ lo += step
220
+ step *= 2
221
+ end
222
+ else
223
+ lo = 0
224
+ hi = guess - 1
225
+ step = 1
226
+ while hi - step + 1 >= 0
227
+ candidate = hi - step + 1
228
+ if yield(candidate)
229
+ lo = candidate
230
+ break
231
+ end
232
+ hi = candidate - 1
233
+ step *= 2
234
+ end
235
+ end
236
+
237
+ while lo < hi
238
+ mid = (lo + hi + 1) / 2
239
+ yield(mid) ? lo = mid : hi = mid - 1
240
+ end
241
+ lo
242
+ end
243
+ end
244
+
245
+ @load_lock = Mutex.new
246
+ @warm_lock = Mutex.new
247
+ @cache_lock = Mutex.new
248
+ @support_checked = false
249
+ @support_problem = nil
250
+ @warm = false
251
+ @tnum = true
252
+ @subpixel = false
253
+ @bundled_font_active = nil
254
+ @metrics = {}
255
+ @ellipsis = {}
256
+
257
+ class << self
258
+ # @param string [String]
259
+ # @return [String] `string` with & < > " ' replaced by markup entities
260
+ def escape(string)
261
+ string.gsub(ESCAPE_PATTERN, ESCAPES)
262
+ end
263
+
264
+ # Renders one string with one libvips call. Layout uses a {Renderer} instead, for caching.
265
+ #
266
+ # @param string [String]
267
+ # @param style [Style]
268
+ # @param align [Symbol] :left or :right
269
+ # @return [Run, nil] nil when the string is blank
270
+ # @raise [RenderFailed]
271
+ def render(string, style:, align:)
272
+ Renderer.new.render(string, style: style, align: align)
273
+ end
274
+
275
+ # @param run [Run, nil]
276
+ # @param style [Style]
277
+ # @param max_extent [Integer]
278
+ # @return [Run, nil] see {Renderer#fit}
279
+ # @raise [RenderFailed]
280
+ def fit(run, style:, max_extent:)
281
+ Renderer.new.fit(run, style: style, max_extent: max_extent)
282
+ end
283
+
284
+ # Cheap capability check that never renders: ruby-vips loads, libvips is 8.12 or newer and was
285
+ # built with the text operation. Memoised.
286
+ #
287
+ # @return [Boolean]
288
+ def supported?
289
+ support_problem.nil?
290
+ end
291
+
292
+ # Once per process, under a Mutex: checks support, registers both bundled font files, probes
293
+ # OpenType feature support and the antialiasing mode, runs the bundled-font self-check and
294
+ # computes the cap-height {Metrics} of the Ledger body, header and footer styles at the globally
295
+ # configured scale (rendering.md sections 5.4 and 9). Later calls return at once.
296
+ #
297
+ # @return [void]
298
+ # @raise [RenderFailed] when libvips is missing or cannot draw text
299
+ def warm_up!
300
+ warm_up_base!
301
+ self_check!
302
+ ledger_styles.each { |style| metrics(style) }
303
+ nil
304
+ end
305
+
306
+ # @return [Boolean] whether the bundled Inter is what libvips draws with (runs the self-check
307
+ # on first use; false when libvips cannot draw text at all)
308
+ def bundled_font_active?
309
+ warm_up!
310
+ @bundled_font_active
311
+ rescue RenderFailed
312
+ false
313
+ end
314
+
315
+ # @api private
316
+ # @return [Boolean] whether libvips draws this process's text with subpixel (coloured)
317
+ # antialiasing, which every run is then corrected for (rendering.md section 9). Runs warm-up.
318
+ # @raise [RenderFailed] when libvips is missing or cannot draw text
319
+ def subpixel_antialiasing?
320
+ warm_up_base!
321
+ @subpixel
322
+ end
323
+
324
+ # @api private
325
+ # Greyscale antialiasing from a subpixel text image (rendering.md section 9): every pixel takes
326
+ # `color` and keeps its alpha, which is the coverage libvips drew. Use it only on text drawn in
327
+ # `color` alone, without colour glyphs.
328
+ #
329
+ # @param image [Vips::Image] 4-band text image from `Vips::Image.text` with `rgba: true`
330
+ # @param color [String] "#RRGGBB"
331
+ # @return [Vips::Image] 4-band uchar sRGB, same size and offsets
332
+ def recolor(image, color)
333
+ image.linear([0, 0, 0, 1], [*color.delete_prefix("#").scan(/../).map(&:hex), 0], uchar: true)
334
+ end
335
+
336
+ # @api private
337
+ # @param color [String] "#RRGGBB"
338
+ # @return [String] a colour at least 128 levels away from `color` in every channel: text drawn in
339
+ # it differs from text drawn in `color` in every pixel with coverage, colour glyphs do not
340
+ def contrast_color(color)
341
+ "##{color.delete_prefix("#").scan(/../).map { |pair| pair.hex < 128 ? "FF" : "00" }.join}"
342
+ end
343
+
344
+ # @param style [Style]
345
+ # @return [Metrics] cap-height metrics, computed once per process per family, file, size and
346
+ # weight
347
+ # @raise [RenderFailed]
348
+ def metrics(style)
349
+ prepare!(style)
350
+ key = [style.family, style.fontfile, style.size_px, style.weight]
351
+ process_cached(@metrics, key) do
352
+ image = draw(markup("H", style), style, :left)
353
+ Metrics.new(cap_top: image.yoffset, cap_height: image.height)
354
+ end
355
+ end
356
+
357
+ # @api private
358
+ # @return [Integer] extent of U+2026 alone in `style` and `align`, computed once per process
359
+ def ellipsis_extent(style, align)
360
+ prepare!(style)
361
+ process_cached(@ellipsis, [style, align]) { build_run(ELLIPSIS, style, align).extent }
362
+ end
363
+
364
+ # @api private
365
+ # Draws the self-check probe (rendering.md 5.4) in `family`, registering `fontfile` when given.
366
+ #
367
+ # @return [Array(Integer, Integer), nil] the extents in px of "0000000000" with tabular figures and
368
+ # "1111111111" with the default figures, nil when libvips cannot draw them
369
+ def self_check_extents(family: "Inter", fontfile: REGULAR)
370
+ tabular = Style.new(family: family, fontfile: fontfile, size_px: 100, weight: 400, color: "#000000", tnum: true)
371
+ [probe_extent("0000000000", tabular), probe_extent("1111111111", tabular.with(tnum: false))]
372
+ rescue Vips::Error
373
+ nil
374
+ end
375
+
376
+ # @api private
377
+ # @param extents [Array<Integer>, nil] from {self_check_extents}
378
+ # @return [Boolean] whether the extents are the bundled Inter's
379
+ def self_check_passes?(extents)
380
+ !extents.nil? && SELF_CHECK_EXTENT.cover?(extents[0]) && SELF_CHECK_PROPORTIONAL_EXTENT.cover?(extents[1])
381
+ end
382
+
383
+ # @api private
384
+ # Warm-up, plus the self-check when `style` uses a bundled font file.
385
+ def prepare!(style)
386
+ warm_up_base!
387
+ self_check! if BUNDLED_FILES.include?(style.fontfile)
388
+ end
389
+
390
+ # @api private
391
+ # @return [String] UTF-8 with NUL and invalid bytes replaced by U+FFFD, frozen
392
+ def display_string(string)
393
+ text = String(string)
394
+ unless text.encoding == Encoding::UTF_8
395
+ text = text.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "\uFFFD")
396
+ end
397
+ text = text.scrub("\uFFFD") unless text.valid_encoding?
398
+ text = text.tr("\u0000", "\uFFFD") if text.include?("\u0000")
399
+ text.frozen? ? text : text.dup.freeze
400
+ end
401
+
402
+ # @api private
403
+ # @return [String] the string escaped and wrapped in one span whose attributes come only from
404
+ # the style
405
+ def markup(text, style)
406
+ features = style.tnum && @tnum ? ' font_features="tnum"' : ""
407
+ %(<span weight="#{style.weight}" foreground="#{style.color}"#{features}>#{escape(text)}</span>)
408
+ end
409
+
410
+ # @api private
411
+ # @param text [String] a display string
412
+ # @return [Integer] the libvips text calls {build_run} makes for `text`: 2 for a string that can
413
+ # contain a colour glyph on a host with subpixel antialiasing (rendering.md section 9), else 1
414
+ # (the second is skipped only if the first finds no ink). Callers have run {prepare!}.
415
+ def text_calls(text)
416
+ @subpixel && text.match?(COLOR_GLYPH) ? 2 : 1
417
+ end
418
+
419
+ # @api private
420
+ # The libvips call for `text` (already a display string), plus the antialiasing correction on
421
+ # hosts that need it, which draws once more when {text_calls} is 2 (rendering.md section 9).
422
+ # Callers have run {prepare!}.
423
+ # @return [Run, nil] nil when libvips finds no ink
424
+ def build_run(text, style, align)
425
+ image = draw(markup(text, style), style, align)
426
+ return nil if image.nil?
427
+
428
+ image = greyscale(image, text, style, align) if @subpixel
429
+ xoffset = image.xoffset
430
+ ink_right = xoffset + image.width
431
+ # Right-to-left guard: Pango swaps the alignment of a right-to-left paragraph, so the ink
432
+ # comes back near the left of the box. Measure it by its ink, like a left-aligned string.
433
+ extent = align == :right && ink_right > RIGHT_BOX / 2 ? RIGHT_BOX - xoffset : ink_right
434
+ Run.new(string: text, image: image, xoffset: xoffset, yoffset: image.yoffset, extent: extent, align: align)
435
+ end
436
+
437
+ private
438
+
439
+ def support_problem
440
+ return @support_problem if @support_checked
441
+
442
+ @load_lock.synchronize do
443
+ unless @support_checked
444
+ @support_problem = detect_support_problem
445
+ @support_checked = true
446
+ end
447
+ end
448
+ @support_problem
449
+ end
450
+
451
+ def detect_support_problem
452
+ begin
453
+ require "vips"
454
+ rescue LoadError
455
+ return "csv_preview needs libvips, which could not be loaded. " \
456
+ "Install libvips 8.12 or newer with Pango, #{INSTALL_HINT}"
457
+ end
458
+ unless Vips.at_least_libvips?(8, 12)
459
+ return "csv_preview needs libvips 8.12 or newer (this process has libvips #{Vips.version_string}), " \
460
+ "#{INSTALL_HINT}"
461
+ end
462
+ if Vips.type_find("VipsOperation", "text").zero?
463
+ return "libvips was built without text (Pango) support. Install libvips with Pango, #{INSTALL_HINT}"
464
+ end
465
+
466
+ nil
467
+ end
468
+
469
+ def warm_up_base!
470
+ return if @warm
471
+
472
+ @warm_lock.synchronize do
473
+ next if @warm
474
+
475
+ problem = support_problem
476
+ raise RenderFailed, problem if problem
477
+
478
+ register_fonts
479
+ probe_font_features
480
+ @subpixel = probe_subpixel_antialiasing
481
+ @warm = true
482
+ end
483
+ end
484
+
485
+ # libvips adds each fontfile to fontconfig once (process-global and permanent). Both files are
486
+ # registered before any text other than these probes is drawn.
487
+ def register_fonts
488
+ [[REGULAR, 400], [SEMIBOLD, 600]].each do |file, weight|
489
+ style = probe_style(fontfile: file, weight: weight)
490
+ draw(markup("x", style), style, :left)
491
+ end
492
+ refresh_font_list unless Vips.at_least_libvips?(8, 13)
493
+ end
494
+
495
+ # libvips 8.12 adds a font file to fontconfig without telling Pango (8.13 and later clear Pango's
496
+ # font cache), and Pango keeps the font list it read at the first draw, when only the Regular file
497
+ # was registered: weight 600 then draws with Regular. Drawing once at another resolution makes
498
+ # libvips set its font map's resolution, which drops Pango's cached list; it is read again, with
499
+ # both files, before the next draw. Every other draw uses 72 dpi.
500
+ def refresh_font_list
501
+ style = probe_style
502
+ vips_text(markup("x", style), style, :left, dpi: 73)
503
+ rescue Vips::Error
504
+ raise RenderFailed, "libvips failed to draw text; #{DRAW_HINT}"
505
+ end
506
+
507
+ # Pango older than 1.38 rejects font_features as invalid markup. Then tnum is disabled for the
508
+ # process and numbers use proportional figures.
509
+ def probe_font_features
510
+ vips_text(%(<span font_features="tnum">0</span>), probe_style, :left)
511
+ rescue Vips::Error => e
512
+ raise RenderFailed, "libvips failed to draw text; #{DRAW_HINT}" unless e.message.include?(INVALID_MARKUP)
513
+
514
+ @tnum = false
515
+ warn "csv_preview: this Pango cannot apply OpenType features (font_features needs Pango 1.38), " \
516
+ "so numbers use proportional figures. Install Pango 1.44 or newer, #{INSTALL_HINT}"
517
+ end
518
+
519
+ # libvips before 8.17.1 lets fontconfig choose the antialiasing of rgba text, and many Linux
520
+ # configurations (Ubuntu's default among them) choose subpixel rendering. The alpha band is then
521
+ # the coverage of the middle subpixel, and the colour bands carry red and blue fringes
522
+ # (rendering.md section 9). Detected once, by drawing mid-grey text over white.
523
+ def probe_subpixel_antialiasing
524
+ style = probe_style(size_px: 26).with(color: SUBPIXEL_PROBE_COLOR)
525
+ image = vips_text(markup(SUBPIXEL_PROBE, style), style, :left)
526
+ flat = image.flatten(background: [255, 255, 255]).cast(:int)
527
+ spread = (flat[0] - flat[1]).abs + (flat[1] - flat[2]).abs + (flat[0] - flat[2]).abs
528
+ spread.max > SUBPIXEL_SPREAD
529
+ rescue Vips::Error
530
+ raise RenderFailed, "libvips failed to draw text; #{DRAW_HINT}"
531
+ end
532
+
533
+ # The correction for subpixel antialiasing (rendering.md section 9): text pixels take the style
534
+ # colour and keep their coverage. A string that can contain a colour glyph is drawn once more in a
535
+ # contrasting colour; pixels that come out identical belong to colour glyphs and are kept as drawn.
536
+ # Both drawings share one layout, so they always have the same size; if they ever did not, the run
537
+ # would keep libvips' own pixels rather than lose its emoji.
538
+ def greyscale(image, text, style, align)
539
+ neutral = recolor(image, style.color)
540
+ return neutral if text_calls(text) == 1
541
+
542
+ other = draw(markup(text, style.with(color: contrast_color(style.color))), style, align)
543
+ return image unless other && other.width == image.width && other.height == image.height
544
+
545
+ image.relational(other, :equal).bandand.ifthenelse(image, neutral)
546
+ .copy(interpretation: :srgb, xoffset: image.xoffset, yoffset: image.yoffset)
547
+ end
548
+
549
+ # Detects a fallback font standing in for the bundled Inter (rendering.md 5.3 and 5.4). Warns
550
+ # once and never raises.
551
+ def self_check!
552
+ return unless @bundled_font_active.nil?
553
+
554
+ @warm_lock.synchronize do
555
+ next unless @bundled_font_active.nil?
556
+
557
+ extents = self_check_extents
558
+ active = self_check_passes?(extents)
559
+ warn_fallback_font(extents) unless active
560
+ @bundled_font_active = active
561
+ end
562
+ end
563
+
564
+ # The three Ledger styles whose metrics warm-up computes. Metrics are cached per process, so
565
+ # later warm-ups are cache hits.
566
+ def ledger_styles
567
+ scale = CsvPreview.config.scale
568
+ scale = Configuration.new.scale unless scale.is_a?(Integer) && Configuration::SCALES.cover?(scale)
569
+ styles = Theme::LEDGER.styles(scale: scale)
570
+ [styles.body, styles.header, styles.footer]
571
+ end
572
+
573
+ # A bundled-font style for the warm-up probes.
574
+ def probe_style(fontfile: REGULAR, size_px: 16, weight: 400, tnum: false)
575
+ Style.new(family: "Inter", fontfile: fontfile, size_px: size_px, weight: weight, color: "#000000", tnum: tnum)
576
+ end
577
+
578
+ # Extent of `text` drawn right-aligned in `style`: from its ink's left edge to the logical right edge.
579
+ def probe_extent(text, style)
580
+ RIGHT_BOX - vips_text(markup(text, style), style, :right).xoffset
581
+ end
582
+
583
+ def warn_fallback_font(extents)
584
+ measured = if extents
585
+ "measured #{extents[0]} and #{extents[1]} px, expected 630 to 660 and 380 to 440"
586
+ else
587
+ "the probe failed"
588
+ end
589
+ warn "csv_preview: the bundled Inter font is not active (self-check #{measured}), so previews use a " \
590
+ "fallback font. On macOS, set PANGOCAIRO_BACKEND=fontconfig in the environment, or require " \
591
+ "\"csv_preview\" at boot before anything else draws text with libvips; #{INSTALL_HINT}"
592
+ end
593
+
594
+ def draw(markup, style, align)
595
+ vips_text(markup, style, align)
596
+ rescue Vips::Error => e
597
+ return nil if e.message.include?(NO_TEXT)
598
+
599
+ # The libvips message stays on the cause; this message never includes input content.
600
+ raise RenderFailed, "libvips failed to draw text; #{DRAW_HINT}"
601
+ end
602
+
603
+ def vips_text(markup, style, align, dpi: 72)
604
+ options = { font: "#{style.family}, #{style.size_px}", dpi: dpi, rgba: true }
605
+ options[:fontfile] = style.fontfile if style.fontfile
606
+ options.update(width: RIGHT_BOX, align: :high) if align == :right
607
+ Vips::Image.text(markup, **options)
608
+ end
609
+
610
+ def process_cached(store, key)
611
+ @cache_lock.synchronize do
612
+ return store[key] if store.key?(key)
613
+
614
+ value = yield
615
+ store[key] = value if store.size < PROCESS_CACHE_LIMIT
616
+ value
617
+ end
618
+ end
619
+ end
620
+ end
621
+ end