Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Lawrence D'Oliveiro Newsgroups: alt.folklore.computers,comp.os.linux.misc Subject: Re: The joy of Typography Date: Fri, 18 Oct 2024 21:15:29 -0000 (UTC) Organization: A noiseless patient Spider Lines: 34 Message-ID: References: <20241014080601.00007478@gmail.com> <38fb5a91-5d00-be42-4bfe-2a05232a82c1@example.net> <1420907830.750803935.503389.peter_flass-yahoo.com@news.eternal-september.org> <20241017090224.000072ce@gmail.com> <20241017203051.3143245f5330ac675cc1c166@127.0.0.1> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 18 Oct 2024 23:15:30 +0200 (CEST) Injection-Info: dont-email.me; posting-host="dfe289e3b0a8ce3b90c1b30ae00e2a59"; logging-data="3658277"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19g4ApADe5lTehFjmOzPkxn" User-Agent: Pan/0.160 (Toresk; ) Cancel-Lock: sha1:ceS8r1n1B5zxOS3uXql3GsXj73o= Bytes: 3465 On Fri, 18 Oct 2024 18:44:26 GMT, Charlie Gibbs wrote: > I wrote some routines to generate my columnar reports in PDF. > On the whole it worked fairly well, but I never found a way to > right-justify a field. Then it became a pain in the ass. You mean “right-align”, not “right-justify”. Here’s a routine that does alignment of text within a given line width, based on the current point, using the current font settings. It can do left-align, right-align, centre-align and anything in-between. def align_text(g, text_lines, line_width, align) : "renders the sequence of text_lines horizontally-aligned within line_width according" \ " to align: 0 for left-aligned, 0.5 for centred, 1.0 for right-aligned; in-between" \ " values also allowed. Returns the bounds of the rendered text." font_extents = g.font_extents bounds = Rect.empty for line in text_lines : save_pos = g.current_point extents = g.text_extents(line) if align != 0 : text_width = extents.width g.rel_move_to(((line_width - text_width) * align, 0)) #end if bounds |= Rect(extents.x_bearing, extents.y_bearing, extents.width, extents.height) + g.current_point g.show_text(line) g.move_to(save_pos + Vector(0, font_extents.height)) #end for return \ bounds #end align_text That comes from the “text_align_justify” example in this repo .