How do I use multiple fonts in a single paragraph and compute text layout?

Q: First, a Report Card: I am the programmer that is actually
integrating PDFTron into our vertical. While I am still fuzzy on the
thousands of details in the PDF definition, integrating PDFTron into
what we do has been a dream so far. It took about 30 hours and most of
that was learning more than I ever wanted to know about PDF's .
PDFTron was well worth the money!

I already understand how to format a paragraph with PDFTron as long as
there is only one font in the paragraph. Using the sample program
ElementBuilder as a guideline, I totally understand right justify,
left justify, center, and word spacing.

However, I don't understand how to print multiple fonts in a single
paragraph. Up to now I have made one entire paragraph one text object
as the example in ElementBuilder.

Also, I don't understand how the program finds out where the end of
the text object will finish. Behind the end of one paragraph, I will
have to put other objects like more text, images, lines, boxes,
whatever. The program will be saying how wide (the x coordinate) the
paragraph is, but where did the Y coordinate stop? I am not sure how
that works when you are putting multiple objects on a single page.
------
A: Writing multiple fonts in a single paragraph works in the same way
as for a single font. The only difference is that you provide a
different font in the call to builder.CreateTextRun(). For example:

// Note: Reuse the fonts throughout the document to avoid embedding
the same font multiple times.
pdftron.PDF.Font fnt1 = ... ;
pdftron.PDF.Font fnt2 = ... ;

...
writer.WriteElement(builder.CreateTextRun("Hello", fnt1, 42));
writer.WriteElement(builder.CreateTextRun("World", fnt2, 42));
writer.WriteElement(builder.CreateTextRun("!", fnt1, 45));
writer.WriteElement(builder.CreateTextRun("!!")); // Use the last font
(fnt1, 45).
...

Regarding the paragraph layout the following snippet (taken from
ElementBuilder sample) is the most basic text layout algorithm:

// Begin writing a block of text
System.Drawing.Font sys_font = new System.Drawing.Font("Comic Sans
MS", 12);
Font my_font = Font.CreateTrueTypeFont(doc, sys_font, true, true);
element = eb.CreateTextBegin(my_font, 12);
element.SetTextMatrix(1.5, 0, 0, 1.5, 50, 600);
element.GetGState().SetLeading(15); // Set the spacing between lines
writer.WriteElement(element);

string para = "PDFNet SDK is an amazingly comprehensive, high-quality
PDF developer toolkit";

int para_end = para.Length;
int text_run = 0;
int text_run_end;

double para_width = 300; // paragraph width is 300 units
double cur_width = 0;

while (text_run < para_end) {
  text_run_end = para.IndexOf(' ', text_run);
  if (text_run_end < 0)
    text_run_end = para_end - 1;

  string text = para.Substring(text_run, text_run_end-text_run+1);
  element = eb.CreateTextRun(text);
  if (cur_width + element.GetTextLength() < para_width) {
    writer.WriteElement(element);
    cur_width += element.GetTextLength();
  }
  else {
    writer.WriteElement(eb.CreateTextNewLine()); // New line
    text = para.Substring(text_run, text_run_end-text_run+1);
    element = eb.CreateTextRun(text);
    cur_width = element.GetTextLength();
    writer.WriteElement(element);
  }

  text_run = text_run_end+1;
}
writer.WriteElement(eb.CreateTextEnd());

If you need to track the Y coordinate of each text run you could use
element.GetBBox(rect) when creating each text run. This will return
the positioning information for each text run (in PDF user
coordinates). You can also set explicit positioning information for
each text-run (e.g. using element.SetTextMatrix() &&
element.GetGstate().SetTransform()), however this requires some
familiarity with PDF graphics model.

The positioning information for the last word/line can be used to
guide the layout of subsequent elements on the page.