Title: Simplify glyph stream logic in ps backend · Issue #23965 · matplotlib/matplotlib · GitHub
Open Graph Title: Simplify glyph stream logic in ps backend · Issue #23965 · matplotlib/matplotlib
X Title: Simplify glyph stream logic in ps backend · Issue #23965 · matplotlib/matplotlib
Description: Seems good, but perhaps simpler to use a groupby here? Something like (untested) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index f209e811f1..4d31fd881a 100644 --- a/lib/matplotlib/backends...
Open Graph Description: Seems good, but perhaps simpler to use a groupby here? Something like (untested) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index f209e811f1..4d31fd8...
X Description: Seems good, but perhaps simpler to use a groupby here? Something like (untested) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index f209e811f1..4d31fd8...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/23965
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Simplify glyph stream logic in ps backend ","articleBody":"Seems good, but perhaps simpler to use a groupby here? Something like (untested)\r\n```patch\r\ndiff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py\r\nindex f209e811f1..4d31fd881a 100644\r\n--- a/lib/matplotlib/backends/backend_ps.py\r\n+++ b/lib/matplotlib/backends/backend_ps.py\r\n@@ -7,6 +7,7 @@ import datetime\r\n from enum import Enum\r\n import functools\r\n from io import StringIO\r\n+import itertools\r\n import logging\r\n import os\r\n import pathlib\r\n@@ -626,10 +627,13 @@ grestore\r\n if ismath:\r\n return self.draw_mathtext(gc, x, y, s, prop, angle)\r\n \r\n+ stream = [] # list of (ps_name, x, char_name)\r\n+\r\n if mpl.rcParams['ps.useafm']:\r\n font = self._get_font_afm(prop)\r\n+ ps_name = (font.postscript_name\r\n+ .encode(\"ascii\", \"replace\").decode(\"ascii\"))\r\n scale = 0.001 * prop.get_size_in_points()\r\n- stream = []\r\n thisx = 0\r\n last_name = None # kerns returns 0 for None.\r\n xs_names = []\r\n@@ -643,37 +647,24 @@ grestore\r\n kern = font.get_kern_dist_from_name(last_name, name)\r\n last_name = name\r\n thisx += kern * scale\r\n- xs_names.append((thisx, name))\r\n+ xs_names.append((ps_name, thisx, name))\r\n thisx += width * scale\r\n- ps_name = (font.postscript_name\r\n- .encode(\"ascii\", \"replace\").decode(\"ascii\"))\r\n- stream.append((ps_name, xs_names))\r\n \r\n else:\r\n font = self._get_font_ttf(prop)\r\n self._character_tracker.track(font, s)\r\n- stream = []\r\n- prev_font = curr_stream = None\r\n for item in _text_helpers.layout(s, font):\r\n ps_name = (item.ft_object.postscript_name\r\n .encode(\"ascii\", \"replace\").decode(\"ascii\"))\r\n- if item.ft_object is not prev_font:\r\n- if curr_stream:\r\n- stream.append(curr_stream)\r\n- prev_font = item.ft_object\r\n- curr_stream = [ps_name, []]\r\n- curr_stream[1].append(\r\n- (item.x, item.ft_object.get_glyph_name(item.glyph_idx))\r\n- )\r\n- # append the last entry\r\n- stream.append(curr_stream)\r\n+ glyph_name = item.ft_object.get_glyph_name(item.glyph_idx)\r\n+ stream.append((ps_name, item.x, glyph_name))\r\n \r\n self.set_color(*gc.get_rgb())\r\n \r\n- for ps_name, xs_names in stream:\r\n+ for ps_name, group in itertools.groupby(stream, lambda entry: entry[0]):\r\n self.set_font(ps_name, prop.get_size_in_points(), False)\r\n thetext = \"\\n\".join(f\"{x:g} 0 m /{name:s} glyphshow\"\r\n- for x, name in xs_names)\r\n+ for _, x, name in group)\r\n self._pswriter.write(f\"\"\"\\\r\n gsave\r\n {self._get_clip_cmd(gc)}\r\n```\r\n\r\nEdit: In fact set_font itself already checks if the font has already been set previously and skips redundant setfont calls, so we could just entirely get rid of the groupby logic too.\r\n\r\n_Originally posted by @anntzer in https://github.com/matplotlib/matplotlib/issues/23910#issuecomment-1252201631_","author":{"url":"https://github.com/tacaswell","@type":"Person","name":"tacaswell"},"datePublished":"2022-09-20T14:28:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/23965/matplotlib/issues/23965"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:459e3974-aad3-57ec-ae24-367afaaf67c7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9F36:39F5AD:1E5BEDE:2930CDA:6A5407C6 |
| html-safe-nonce | d8f3b28133b20337ca06e1a95f37075f9af7f13a658dc646dbd60b70701cca76 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RjM2OjM5RjVBRDoxRTVCRURFOjI5MzBDREE6NkE1NDA3QzYiLCJ2aXNpdG9yX2lkIjoiMTM2ODY1NDg0ODkyNTYzMjQ1NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6eb18c0c4b02df44fb2193bbdbe65629c9cd7ecb8af61e83199cb4078e859eb2 |
| hovercard-subject-tag | issue:1379519678 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/23965/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a2cf08890809fc30646a5eb3475a260671527950d986a0b86336e2ae56d57592/matplotlib/matplotlib/issues/23965 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a2cf08890809fc30646a5eb3475a260671527950d986a0b86336e2ae56d57592/matplotlib/matplotlib/issues/23965 |
| og:image:alt | Seems good, but perhaps simpler to use a groupby here? Something like (untested) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index f209e811f1..4d31fd8... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tacaswell |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width