Simulating bold and italic styles when a matching font is not available.

320 views
Skip to first unread message

Support

unread,
Aug 25, 2009, 5:23:22 PM8/25/09
to PDFTron PDFNet SDK
Q: I am using Font.CreateCIDTrueTypeFont on Windows with a font source
that has the bold styling applied, yet the output does not produce
bold text. With the provided example code, and my limited knowledge
of PDF file format, I would expect to see a font entry like "/
BaseFont /FranklinGothicBook,Bold" somewhere in the file, yet I do not
see it. What am I doing wrong?

public void TestBoldFont()
{
PDFNet.Initialize("Omitted");
PDFDoc doc = new PDFDoc();
Page page = doc.PageCreate();
doc.PagePushBack(page);
ElementBuilder builder = new ElementBuilder();
ElementWriter writer = new ElementWriter();
writer.Begin(page);
string fontFace = "Franklin Gothic Book";
string testText = "Quick Brown Lazy Fox";
System.Drawing.FontFamily fontFamily = new
System.Drawing.FontFamily(fontFace);
System.Drawing.Font font = new System.Drawing.Font(fontFamily,
14);
System.Drawing.Font fontBold = new System.Drawing.Font(fontFamily,
14,

System.Drawing.FontStyle.Bold);
pdftron.PDF.Font tronFont = Font.CreateCIDTrueTypeFont(doc, font,
true, true);
pdftron.PDF.Font tronFontBold = Font.CreateCIDTrueTypeFont(doc,
fontBold, true,
true);
Element textBlockBegin = builder.CreateTextBegin(tronFont, 14);
writer.WriteElement(textBlockBegin);
Element textHeader = builder.CreateUnicodeTextRun(testText + "
This is normal
style");
double x = inchesToPDFUnits(.5+ 0.35);
double y = inchesToPDFUnits(10 + 0.1);
textHeader.SetTextMatrix(1, 0, 0, 1, x, y);
//Graphics State needed to set the color
GState graphicsState = textHeader.GetGState();
graphicsState.SetTextRenderMode
(GState.TextRenderingMode.e_fill_text);
graphicsState.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
graphicsState.SetFillColor(new ColorPt(64 / 255.0, 100 / 255.0,
126 / 255.0));
writer.WriteElement(textHeader);
Element textBlockEnd = builder.CreateTextEnd();
writer.WriteElement(textBlockEnd);

//Bold Test
textBlockBegin = builder.CreateTextBegin();
writer.WriteElement(textBlockBegin);
textHeader = builder.CreateUnicodeTextRun(testText + " This is
bold style");
x = inchesToPDFUnits(.5 + 0.35);
y = inchesToPDFUnits(9.5 + 0.1);
textHeader.SetTextMatrix(1, 0, 0, 1, x, y);
//Graphics State needed to set the color
graphicsState.SetFont(tronFontBold, 14); //try setting font on
run, rather than
//text block
graphicsState = textHeader.GetGState();
graphicsState.SetTextRenderMode
(GState.TextRenderingMode.e_fill_text);
graphicsState.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
graphicsState.SetFillColor(new ColorPt(64 / 255.0, 100 / 255.0,
126 / 255.0));
writer.WriteElement(textHeader);
textBlockEnd = builder.CreateTextEnd();
writer.WriteElement(textBlockEnd);
writer.Flush();
writer.End();
string path = @"C:\temp\fontBold.pdf";
doc.Save(path , pdftron.SDF.SDFDoc.SaveOptions.e_remove_unused);
PDFNet.Terminate();
Process.Start(path);
}

internal double inchesToPDFUnits(double inches)
{
return inches * 72;
}

-----------
A: Is it safe to assume that your system is missing bold version for
"Franklin Gothic Book"? If this is a correct assumption PDFNet is
embedding the font which most closely matches description provided in
'System.Drawing.Font'. Unlike GDI, PDF format does not have a way of
simulating/synthesizing font styles. So bold, italic styles require
separate font files (which are sometimes wrapped into a single file
called True Type Collection - ttc; or which are sometimes stored as
separate files - e.g. as in case of Arial font). PDFNet searches for
all fonts in "Franklin Gothic Book" family and then selects a specific
font based on a style.

In the absence of a specific fonts, some PDFNet users simulate the
bold appearance by enabling font stroking (element.GetGState
().SetTextRenderMode(GState.TextRenderingMode.e_fill_stroke_text);
element.GetGState().SetStrokeColorSpace(ColorSpace.CreateDeviceRGB());
element.GetGState().SetStrokeColor(same_as_fillcolor)) and setting
certain stoke width (element.GetGState().SetLineWidth(width)) which is
proportionate to font weight. Similarly, italic style can be simulated
by adding a slight shear component to the text matrix
(element.SetTextmatrix()).


Support

unread,
Aug 26, 2009, 3:22:20 PM8/26/09
to PDFTron PDFNet SDK
Q: I had seen another article in your forum about PDF format not
simulating/synthesizing font styles and the suggestion regarding font
stroke. However, there was a caveat in the suggestion for stroking
saying there might be some sort of distortions based on the size.
Since I must conform to Marketing requirements, I am required to
produce the document with the exact font so I kept attemtpting to get
the right font to work.

I had looked on my system and did not see the bold font, but thought I
was ok because I thought I had read somewhere that a basic font file
contained four styles: Normal, Italic, Bold, and one other that I
cannot remember right now. So I was under the impression that my
calls to the System.Drawing namespace wold produce the correct font
data structure, because I was thinking that setting the style was
pulling the right portion out of the font file:

string fontFace = "Franklin Gothic Book"; System.Drawing.FontFamily
fontFamily = new System.Drawing.FontFamily(fontFace);
System.Drawing.Font fontBold = new System.Drawing.Font(fontFamily, 14,

System.Drawing.FontStyle.Bold); pdftron.PDF.Font tronFontBold =
Font.CreateCIDTrueTypeFont(doc, fontBold, true,
true);

Obvously this is not working, but your email caused me to look at the
fonts again, and I did see an Italic version. So I plugged Italic
into the code for a quik test like this:

string fontFace = "Franklin Gothic Book"; System.Drawing.FontFamily
fontFamily = new System.Drawing.FontFamily(fontFace);
System.Drawing.Font fontBold = new System.Drawing.Font(fontFamily, 14,
System.Drawing.FontStyle.Italic); pdftron.PDF.Font tronFontBold =
Font.CreateCIDTrueTypeFont(doc, fontBold, true, true);

And it did effect the output with italic and gave me this in the PDF
File:
/BaseFont /CGWTLL+FranklinGothic-BookItalic/

So, I need someone to dig up a Frankling Gothic Bold TTF file for me.

------
A: Yes, in case you need to preserve the exact look and feel of the
font you would need to obtain the bold version of this font.
Reply all
Reply to author
Forward
0 new messages