Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.9K Views
|
13 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Can I use the Wolfram Language to generate Vector images of text?

POSTED BY: Kyle Zimmer
13 Replies

Just for people's reference, attached is the updated notebook.

And here is the associated function:

ClearAll[PsychoSentenceSVG, modifyWords];

modifyWords[{word1_String, word2_String}, {style1_, style2_}] :=

  Switch[{style1, style2},

   {None, None},
   {word1, word2},

   {None, _},
   {word1, Style[word2, style2]},

   {_, None},
   {Style[word1, style1], word2},

   {_, _},
   {Style[word1, style1], Style[word2, style2]}
   ];


Options[PsychoSentenceSVG] = {ReplacementCharacter -> "-", 
   FontFamily -> "Courier", FontSize -> 12};

PsychoSentenceSVG[sentence_String, path_String, 
   fileName_String, {style1_, style2_}, OptionsPattern[]] :=

  Module[{splitSentence, dashedTemplate, stringTable, 
    processedSentences, replacementCharacter, fontFamily, fontSize},

   replacementCharacter = OptionValue[ReplacementCharacter];
   fontFamily = OptionValue[FontFamily];
   fontSize = OptionValue[FontSize];

   splitSentence = Partition[StringSplit[sentence], 2, 2, 1, ""];

   dashedTemplate = 
    Map[StringReplace[#, _ -> replacementCharacter] &, 
     splitSentence, {2}];

   splitSentence = modifyWords[#, {style1, style2}] & /@ splitSentence;

   stringTable = Table[
     Riffle[Flatten[
       ReplacePart[dashedTemplate, i -> splitSentence[[i]]]], 
      " "], {i, Length[splitSentence]}];

   processedSentences = Row /@ stringTable;
   processedSentences = 
    TextCell[#, "Text", FontFamily -> fontFamily, 
       FontSize -> fontSize, ShowStringCharacters -> False] & /@ 
     processedSentences;

   MapIndexed[
    Export[FileNameJoin[{path, 
        "s" <> fileName <> "w" <> ToString[#2[[1]]] <> ".svg"}], #1, 
      "SVG"] &, processedSentences]

   ];
Attachments:
POSTED BY: David Reiss

Bill, yes, that's a wonderful example.

Kyle, send me an email at the address in my Wolfram Community profile (click on my name) and we can finish up via email.

Best, David

POSTED BY: David Reiss

Thanks Bill.

POSTED BY: Kyle Zimmer
Posted 12 years ago

It is not the kind of presentation you were asking for, but if you want another impressive example of what a few dozen lines of Mathemaica can do for presenting carefully positioned and formatted and highlighted text to a viewer then you might search the forum for the word spritz

POSTED BY: Bill Simpson
POSTED BY: Kyle Zimmer

...Blushes...

Actually, it's the Wolfram Language that's amazing.

Spread the word!

POSTED BY: David Reiss

You are amazing David.

POSTED BY: Kyle Zimmer

Kyle, see the attached updated notebook which now has an additional function, PsychoSentenceSVG, that takes a directory path as an additional argument and which exports each individual line to its own svg file. --David

Attachments:
POSTED BY: David Reiss

Thank you so much David. That notebook is fantastic. I suppose there is probably a way to extend it so that it separates the final product into its individual lines and exports .svg files of a set size for each? Fu-Ping, would the function you gave me do the export part? I'm not sure I understand how to implement it. I apologize for my incompetence.

Thanks again both of you, this is extremely helpful to me.

POSTED BY: Kyle Zimmer

Do look at the notebook posted above, but for others that want a quick copy and paste of the final example code here it is:

ClearAll[modifyWords];

modifyWords[{word1_String, word2_String}, {style1_, style2_}] :=

  Switch[{style1, style2},

   {None, None},
   {word1, word2},

   {None, _},
   {word1, Style[word2, style2]},

   {_, None},
   {Style[word1, style1], word2},

   {_, _},
   {Style[word1, style1], Style[word2, style2]}
   ];

ClearAll[PsychoSentence];

Options[PsychoSentence] = {ReplacementCharacter -> "-"};

PsychoSentence[sentence_String, {style1_, style2_}, 
   OptionsPattern[]] :=

  Module[{splitSentence, dashedTemplate, stringTable, 
    processedSentences, replacementCharacter},

   replacementCharacter = OptionValue[ReplacementCharacter];

   splitSentence = Partition[StringSplit[sentence], 2, 2, 1, ""];

   dashedTemplate = 
    Map[StringReplace[#, _ -> replacementCharacter] &, 
     splitSentence, {2}];

   splitSentence = modifyWords[#, {style1, style2}] & /@ splitSentence;

   stringTable = Table[
     Riffle[Flatten[
       ReplacePart[dashedTemplate, i -> splitSentence[[i]]]], 
      " "], {i, Length[splitSentence]}];

   processedSentences = Row /@ stringTable;
   processedSentences = Riffle[processedSentences, "\n"];

   CellPrint[
    TextCell[Row[processedSentences], "Text", FontFamily -> "Courier",
      FontSize -> 11, ShowStringCharacters -> False]]

   ];

PsychoSentence[sentence_String] := 
 PsychoSentence[sentence, {None, None}]

And here is an example of it's use (it prints out a formatted Cell as its output):

PsychoSentence["The time has come for all good iguanas to have some kelp", {LightPurple, Green}]

enter image description here

POSTED BY: David Reiss
Posted 12 years ago

Maybe you like this: Export["pi-shu.svg", Rasterize@"pi.shu.edu.cn"]

POSTED BY: Fuping Tan

Longer answer: see the attached notebook that implements something along the lines of what I think you are wanting. Of course this is just a quick sketch (20 minutes development time) but it could be expanded in a variety of ways....

--David

Attachments:
POSTED BY: David Reiss

Short answer: yes.

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard