7
$\begingroup$

I am trying to ascertain how large a justified text block will be when printed by rasterizing it in the "Printout" style environment. However, Rasterize is reverting to the "Working" environment. How can I obtain a raster of the graphic as it appears in "Printout" style?

The following code illustrates the problem. Note the text differs in size in the two environments: In the "Printout" environment the text occupies eight lines, but in the rasterised "Working" version it takes up only seven lines.

I am using text height prediction for page layouts.

SetOptions[EvaluationNotebook[], ScreenStyleEnvironment -> "Printout"];

lorem = StringTake[ExampleData[{"Text", "LoremIpsum"}], {198, 922}];

{wd, ht} = {564, 120};

box = Graphics[{Green, Rectangle[{0, 0}, {wd, ht}],
   Black, Inset[TextCell[lorem, LineSpacing -> {0, 16},
     TextJustification -> 1], {0, 0}, {Left, Bottom},
    {wd, Automatic}]},
  BaseStyle -> {FontFamily -> "Times", FontSize -> 13},
  PlotRange -> {{0, wd}, {0, ht}}, ImageSize -> wd]

Rasterize[box]

enter image description here

The notebook screen environment can be restored with the following statement:

SetOptions[EvaluationNotebook[], ScreenStyleEnvironment -> Inherited]

Edit

Further to george2079's suggestion I made an automated procedure to produce a rasterised "Printout" graphic. It is subsequently processed in a separate procedure (not shown here) to ascertain the height of the text box required for print output.

createPrintBox[text_] := Module[{wd = 564, ht = 120, nb},
  nb = CreateDocument[ExpressionCell[
     Graphics[{Green, Rectangle[{0, 0}, {wd, ht}],
       Black, Inset[TextCell[text, LineSpacing -> {0, 16},
         TextJustification -> 1], {0, 0}, {Left, Bottom},
        {wd, Automatic}]},
      BaseStyle -> {FontFamily -> "Times", FontSize -> 13},
      PlotRange -> {{0, wd}, {0, ht}}, ImageSize -> wd], "Print"],
    ScreenStyleEnvironment -> "Printout", Visible -> False];
  SelectionMove[nb, Next, Cell];
  FrontEndExecute[FrontEndToken[nb, "CopySpecial", "MGF"]];
  SelectionMove[nb, After, Cell];
  FrontEndExecute[FrontEndToken[nb, "Paste"]];
  SelectionMove[nb, All, Cell];
  cellcontent = NotebookRead[nb];
  NotebookClose[nb];
  output = ToExpression[First@cellcontent];
  Rasterize[Show[output, ImageSize -> wd]]]

lorem = StringTake[ExampleData[{"Text", "LoremIpsum"}], {198, 922}];

printoutbox = createPrintBox[lorem]

enter image description here

As you can see, this is a useable raster image showing the correct "Printout" style, justified text height of eight lines.

In contrast, here is the seven line "Working" screen-style raster produced by the code below:-

enter image description here

lorem = StringTake[ExampleData[{"Text", "LoremIpsum"}], {198, 922}];
{wd, ht} = {564, 120};
Rasterize[Graphics[{Green, Rectangle[{0, 0}, {wd, ht}],
   Black, Inset[TextCell[lorem, LineSpacing -> {0, 16},
     TextJustification -> 1], {0, 0}, {Left, Bottom},
    {wd, Automatic}]},
  BaseStyle -> {FontFamily -> "Times", FontSize -> 13},
  PlotRange -> {{0, wd}, {0, ht}}, ImageSize -> wd]]
$\endgroup$

2 Answers 2

2
$\begingroup$

Try this: copy as bitmap using the GUI menu and paste into an input cell, the resulting image reports a slightly different pixel size than the raster object. The difference is so subtle I'm not sure if thats what you are after or not.

If that works I'm not sure how to automate the procedure..

Edit: This does indeed do what I suspect you need, resulting in an image that preserves the formatted linewrap. Now it is really bugging me I cant see how to programatically select the graphic. The copy should be some incarnation of

FrontEndTokenExecute[FrontEnd`SelectedNotebook[], "CopySpecial", "MGF"]
$\endgroup$
2
$\begingroup$

To change the StyleEnvironement for Rasterize[] (and also Export) you need to change the option of the $FrontEnd object:

SetOptions[$FrontEnd, ScreenStyleEnvironment -> "Printout"]

Now, all Rasterize and Export calls will return the same thing that you see in your notebook after setting

SetOptions[EvaluationNotebook[], ScreenStyleEnvironment -> "Printout"];
$\endgroup$
1
  • $\begingroup$ Note that changes to the $FrontEnd object are persistent and saved automatically in the init.m file. If you do not like this behavior, please consider the answer by Michael E2 in the question mathematica.stackexchange.com/questions/57709/… $\endgroup$
    – denizb
    Aug 20, 2014 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.