• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Try font size (Undo option?)

Guest
Jun 07, 2015 Jun 07, 2015

Copy link to clipboard

Copied

Hi,

Suppose I have a rectangle and want to write a paragraph of text inside it. I'd like to set such a maximum size of a font, to have all the text be inside a rectangle (nothing may go outside of its boundary).

So, I first try font size of 20 (for example). If with that size the text is not inside the rectangle I'd like to try with smaller size untill the success.

But... how can I only try the font size without writing the text on the page? Is there something like "undo" option?

Thanks in advance,

Jacek

TOPICS
Programming

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Jun 07, 2015 Jun 07, 2015

Copy link to clipboard

Copied

PostScript is a programming language. You can conceivably query the set widths for the characters in the text and dynamically choose the layout and point size based on making everything fit in a particular area. This is terribly inefficient (that's why you have layout programs), but it can work. Note that such a similar feature does not exist in PDF which is truly a final form file format, dispensing with the variability of the output.

        - Dov

- Dov Isaacs, former Adobe Principal Scientist (April 30, 1990 - May 30, 2021)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 08, 2015 Jun 08, 2015

Copy link to clipboard

Copied

I use PrintParagraph routine from Acumen Journal (January 2006). Could you give me an advice how may I "test" some font sizes? By "test" I mean getting to know if a particular size is too big or not.

I wish it could be possible to:

1. save current page state

2. execute some procedure/routine to "try" printing some text (moving current point)

3. check currentpoint if it is outside my rectangle

4. if it is outside then restore the saved page status (undo changes made by the procedure/routine)

Or is it possible to execute show without showing anything on the page (only moving currentpoint)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 08, 2015 Jun 08, 2015

Copy link to clipboard

Copied

stringwidth is what you want

/Helvetica 10 selectfont

72 720 moveto

(Hello World) stringwidth pop = % 51.6462

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 08, 2015 Jun 08, 2015

Copy link to clipboard

Copied

LATEST

Hi, Jacek -

This is possible and not terribly difficult. Issue 61 of the Acumen Journal (March 2011) has an article on fitting text to a specific width that will give you a notion on how to approach the problem. The short version: directly calculate the point size you need.

1. Temporarily set your font to a 1-point font of your choice

2. Use stringwidth to find the width of the text you want.

3. Divide the width of your box by the width of the 1-point text; that’s your desired point size!

There’s a bit of imprecision in this, because the value returned by stringwidth includes side bearings. A more precise procedure I sometimes use looks like this:

/hiresStringwidth % (str) => width  offset

{

  gsave

  0 0 moveto

  true charpath

  pathbbox           % => xll yll xur yur

  pop exch pop   % => xll xur

  1 index sub

  exch

  grestore

} bind def

=========

And while I’m at it, here’s a procedure to print text to a specific width using point size. (The Acumen Journal article uses ashow and scaling.)

/FitToWidth % (str) /Font width

{

  3 dict begin      % temporary dict

  /w exch def      % I'm feeling too lazy to

  findfont      % manage the stack this morning

  /f exch def

  /s exch def

  f 100 scalefont setfont % 1-pt font

  s hiresStringwidth % => wid offset

  w 3 -1 roll div 100 mul % => offset pointSize

  f exch scalefont

  setfont

  100 div 0 rmoveto

  s show

  end

} bind def

1 0 0 setrgbcolor

.5 setlinewidth

100 600 300 100 rectstroke

0 setgray

100 625 moveto

(This is fitting) /Times-Roman 300 FitToWidth

showpage

==========

Note that these procedures aren't the speediest little bunnies, so you may not want to use them to print page after page of fitted text.

Also note that the above example was knocked off fairly quickly (and I’m using temporary variables rather than managing the stack properly), so treat this as a starting point for your own work.

Best regards,

John

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines