Commit f6aaa87f authored by Peter Billam's avatar Peter Billam

introduce LeftJustifyText, and fix a last-word bug

parent 358a0cf0
......@@ -63,6 +63,9 @@ text.ps - Some useful text-handling stuff in PostScript
/Times-Roman 18 selectfont /y 500 def
long_string 180 {20 y moveto show /y y 20 sub def} <A HREF="#BreakIntoLines">BreakIntoLines</A>
/Times-Roman 18 selectfont
20 500 long_string 180 <A HREF="#LeftJustifyText">LeftJustifyText</A>
/Times-Roman 18 selectfont
20 500 long_string 180 <A HREF="#JustifyText">JustifyText</A>
......@@ -103,6 +106,7 @@ and in <I>luser-&nbsp;-droog</I>'s postings in <I>comp.lang.postscript</I>
<B><A HREF="#OutsideCircleText">OutsideCircleText</A></B> &nbsp;
<B><A HREF="#MakeOutlineFont">MakeOutlineFont</A></B> &nbsp;
<B><A HREF="#BreakIntoLines">BreakIntoLines</A></B> &nbsp;
<B><A HREF="#LeftJustifyText">LeftJustifyText</A></B><BR>
<B><A HREF="#JustifyText">JustifyText</A></B><BR>
<B><A HREF="#rightshow">rightshow</A></B> &nbsp;
<B><A HREF="#leftshow">leftshow</A></B> &nbsp;
......@@ -159,6 +163,19 @@ The procedure <I>proc</I> will be executed at each end-of-line.<BR>
See the <I>Postscript Language Tutorial and Cookbook</I> p.179
</P>
</DD><DT>
<B><A NAME="LeftJustifyText">
x y textstring linewidth &nbsp; LeftJustifyText</A></B>
</DT><DD><P>
<I>LeftJustifyText</I> invokes <I>BreakIntoLines</I>
with a procedure that invokes <I>show</I> on each line,
<BR>
<I>x</I> and <I>y</I> give the position where the text will start,
i.e. at its top left corner.<BR>
<I>LeftJustifyText</I> leaves the <I>currentpoint</I> at the end of the
last character printed, as does <I>ashow</I>.<BR>
</P>
</DD><DT>
<B><A NAME="JustifyText">
x y textstring linewidth &nbsp; JustifyText</A></B>
......@@ -167,7 +184,7 @@ x y textstring linewidth &nbsp; JustifyText</A></B>
with a procedure that looks at the linestring,
measures its stringwidth, subtracts that from the <I>linewidth</I>,
divides the result by its length in characters,
and then invokes <I>ashow</I> with that <I>ax</I><BR>
and then invokes <I>ashow</I> with that <I>ax</I>.<BR>
<I>x</I> and <I>y</I> give the position where the text will start,
i.e. at its top left corner.<BR>
<I>JustifyText</I> leaves the <I>currentpoint</I> at the end of the
......@@ -274,6 +291,7 @@ Peter J Billam &nbsp;
</P>
<A NAME="changes"><HR></A><H3>CHANGES</H3><PRE>
20111029 <A HREF="#LeftJustifyText">LeftJustifyText</A> doesn't justify the right margin
20160607 <A HREF="#centrexshow">centrexshow</A> handles the Y dimension in sloped fonts
20160521 add <A HREF="#pathbboxwh">pathbboxwh</A> and <A HREF="#warn">warn</A>
20160515 add <A HREF="#kcharpath">kcharpath</A>
......
......@@ -121,6 +121,34 @@ xmax 0.5 mul ymax 0.3 mul 5 0 360 arc fill
darkgrey setrgbcolor
xmax 0.5 mul ymax 0.3 mul /FatSimpleZoom 40 (centrexshow Zoom) centrexshow
showpage
%%Page: 3 3
%%BeginPageSetup
<</PageSize [ 842 1190 ] >> setpagedevice % A3
%%EndPageSetup
(/home/pjb/ps/lib/colours.ps) run
(/home/pjb/ps/lib/fonts.ps) run
(/home/pjb/ps/lib/text.ps) run
/xmax currentpagedevice (PageSize) get 0 get def
/ymax currentpagedevice (PageSize) get 1 get def
black setrgbcolor /Times-BoldItalic-ISO 30 selectfont
/longstring (The fingering is based on that noted by David Bollard in 1964 \
when he was in London studying this piece with Ilona Kabos \
and later with Louis Kentnerlongword.) def
/width xmax 0.85 mul def
xmax width sub 0.5 mul ymax 0.70 mul longstring width JustifyText
/longstring (The fingering is based on that noted by David Bollard in 1964 \
when he was in London studying this piece with Ilona Kabos \
and later with Louis Kentnerlongword. ) def
/width xmax 0.85 mul def
xmax width sub 0.5 mul ymax 0.40 mul longstring width JustifyText
showpage
%%EOF
......@@ -164,16 +164,18 @@ end
/linewidth exch def
/textstring exch def
/breakwidth wordbreak stringwidth pop def
/curwidth 0 def
/curwidth 0 def
/lastwordbreak 0 def
/startchar 0 def
/startchar 0 def
/restoftext textstring def
{ % loop
% BUG must search for EITHER wordbreak OR end-of-text
% must wrap the last word even if it has no space after it !
{ % loop word by word
restoftext wordbreak search { % if?
/nextword exch def pop
/nextword exch def pop % extract the results of the search
/restoftext exch def
/wordwidth nextword stringwidth pop def
curwidth wordwidth add linewidth gt { %if?
/wordwidth nextword stringwidth pop def
curwidth wordwidth add linewidth gt { % if it overflows the line
textstring startchar lastwordbreak startchar sub
getinterval proc
/startchar lastwordbreak def
......@@ -182,17 +184,50 @@ end
/curwidth curwidth wordwidth add breakwidth add def
} ifelse
/lastwordbreak lastwordbreak nextword length add 1 add def
} { % else?
pop exit
} { % else we couldn't find a wordbreak
% so should test if restoftext fits onto the line,
restoftext stringwidth pop curwidth add linewidth lt {
pop exit
} { % and if not, should wrap it at the space where we are.
textstring startchar lastwordbreak startchar sub
getinterval proc
/startchar lastwordbreak def
/curwidth wordwidth breakwidth add def
pop exit
} ifelse
} ifelse
} loop
/lastchar textstring length def
textstring startchar lastchar startchar sub getinterval proc
textstring startchar lastchar startchar sub getinterval proc
} def
/currentfontsize { currentfont /FontMatrix get 0 get 1000 mul } def
/LeftJustifyText { % usage: x y textstring linewidth LeftJustifyText
/linewidth exch def
/textstring exch def
/y exch def
/xline exch def
/yline y def
% invocation of BreakIntoLines starts here:
textstring linewidth
{
/linestring exch def
xline yline moveto
0 0 linestring ashow
/yline yline currentfontsize 1.10 mul sub def
% google: vertical spacing font-size
% For most text, the optimal line spacing is between 120% and 145% of
% the point size. Most word processors, as well as CSS, let you define line
% spacing as a multiple. Or you can do the math—multiply your point size
% by the percentage. (The text in this paragraph has line spacing of 110%.
% https://practicaltypography.com/line-spacing.html
}
BreakIntoLines
} def
/JustifyText { % usage: x y textstring linewidth JustifyText
% 20181028 BUG :-( Should not stretch-justify the final line
/linewidth exch def
/textstring exch def
/y exch def
......@@ -203,12 +238,12 @@ end
/yline y def
% invocation of BreakIntoLines starts here:
textstring linewidth
{
{ % how can this loop be aware of when it's the last line ?
/linestring exch def
/ax linewidth linestring stringwidth pop sub linestring length div def
xline yline moveto
ax 0 linestring ashow
/yline yline currentfontsize 1.04 mul sub def
/yline yline currentfontsize 1.10 mul sub def
}
BreakIntoLines
} def
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment