PDF Sharp setting font styles - c#

I have a font dialog to let user pick a font name, font size and also possibly a font style.
but this code doesn't work
XFont myFont = new XFont(txtP.Font.Name, txtP.Font.Size, txtP.Font.Style);
Error is: Error 2 Argument 3: cannot convert from
'System.Drawing.FontStyle' to 'PdfSharp.Drawing.XFontStyle' /
Error 1 The best overloaded method match for
'PdfSharp.Drawing.XFont.XFont(string, double,
PdfSharp.Drawing.XFontStyle)' has some invalid arguments
is there a workaround on this? I would really like to let users choose Font Style also not just name and size.

XFontStyle is an enum defined by PdfSharp. Like the error says, there is no way to convert System.Drawing.FontStyle' to 'PdfSharp.Drawing.XFontStyle'.
Options:
Let the user select directly from xFontStyle values.
Create a converter method between FontStyle and XFontStyle.
The second method is a little complicated because you have to map the values in the first enum to the second one, and as you can see they are not exactly the same, but it can be done.

I solved the issue by
XFont myFont = new XFont(txtP.Font.Name, txtP.Font.Size, (PdfSharp.Drawing.XFontStyle)fontStyle);

Related

Check a Font for a 32bit Unicode Glyph [duplicate]

How can I determine from the .NET runtime if, for a given font, if it has the glyph for a character? I want to switch the font to Arial Unicode MS if I have text that the specified font does not have a glyph for (very common for CJK).
Update: I'm looking for a C# (ie all managed code) solution. I think GlyphTypeface may be what I need but I can't see a way in it to ask if a given character has a glyph. You can get the entire map back, but I assume that would be an expensive call.
I've done some unicode tools and the technique I use is getting the map and chache it
for each font used.
IDictionary<int, ushort> characterMap = GlyphTypeface.CharacterToGlyphMap
will give you the defined glyph index per codepoint.
msdn ref
if (characterMap.ContainsKey(CodePoint))
glyphExists = true;
else
glyphExists = false;

PDFLib set_graphics_option how to pass list of colors in fillcolor option

How to pass multiple PANTONE Colors list to PDF library set_graphics_option function?
optList = "fillcolor={spotname {PANTONE 281 U} 1}";
p.set_graphics_option(optlist);
How to pass multiple PANTONE Color set to PDF library set_graphics_option function?
Maybe I don't get your question yet, but you can only have one color set at a time. When you want to fill some content with a different color, you have to set the (fill|stroke)color accordingly.
When you want to the stoke and fill color in a single set, you can combine this:
optList = "fillcolor={spotname {PANTONE 281 U} 1} strokecolor={spotname {PANTONE 128 U} 1}";
p.set_graphics_option(optlist);
Have you tried raising this with PDFLib support as a bug? I don't know if you can trace the Pantone colour property through PDFLib to see whether the problem is in the SVG loading not picking it up, or whether it's being dropped when assembled into the document maybe, or not being written into the PDF - that might help you find out where the problem is, and if you can fix it yourself. But otherwise this just reads like a bug report and I'm not sure what we can do to help you.

Loading font-family from disk to PrivateFontCollection

I'm using the following code to load a font into memory for generating an image with GDI+:
var fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(Server.MapPath("~/fonts/abraham-webfont.ttf"));
fontCollection.Families.Count(); // => This line tells me, that the collection has 0 items.
There are no exceptions, but the fontCollection Families property is empty after the AddFontFile method has run without any exceptions.
I've verified that the path is valid (File.Exists returns true):
Response.Write(System.IO.File.Exists(Server.MapPath("~/fonts/abraham-webfont.ttf"))); // # => Renders "True"
The TTF-file seems to work fine, when I open the file, so it's not an invalid TTF-file:
Any suggestions?
Answer from Hans Passant solved the problem:
PrivateFontCollection is notoriously flakey. One failure mode that's pretty common today is that the font is actually an OpenType font with TrueType outlines. GDI+ only supports "pure" ones. The shoe fits, the web says that Abraham is an OpenType font. Works in WPF, not in Winforms.

How to detect a string-overflow from a line in C#?

I wanted to know how to word-wrap in C# when I came across a very good solution to my problem on word-wrapping text in a line in this URL. Unfortunately, I do not have enough reputations to ask the OP directly about one specific problem I am having(and most likely people dealing with this will be having indefinitely)
Problem Description:
I can word-wrap a string if it needs to be word-wrapped with this code:
Graphics PAddress = e.Graphics;
SizeF PAddressLength = PAddress.MeasureString("Residential Address: " + RAddressTextBox.Text, new Font(fontface, fontsize, FontStyle.Regular),700);
PAddress.DrawString("Residential Address: "+PAddressLength + RAddressTextBox.Text, new Font(fontface, fontsize, FontStyle.Regular), Brushes.Black, new RectangleF(new Point(pagemarginX, newline()),PAddressLength),StringFormat.GenericTypographic);
However, I could not find the place to receive a trigger whenever the word-length overflows from a single line.
for example:
In LINE-2 of that code, whenever the wordlength exceeds 700px, it moves to the next line. It does that by following the RectangleF to wordwrap. It is doing so automatically, which is a problem since that makes it difficult to know whether it has crossed 700px or not.This is the format in which information is displayed whenever I tried to print PAddressLength:
{Width=633.1881, Height=47.14897}
I am thinking that If I can extract the value of width from that using PAddressLength.Width ,then I can partially solve this problem. But with that, I will need to calculate if the remaining space(i.e 700px - 633.1881px ) will accommodate the next word or not(if there is one)
BREAKING DOWN THE PROBLEM:
I already know how to word-wrap when there is a string longer than what specify by using Graphics.MeasureString as given in this solution in another question.
But that^ process happens automatically, so I want to know how to detect if the word-wrap has occured(and how may lines it has wrapped with each line being 700px width maximum)
I need to know the number of lines that have been wrapped in order to know the number of times to execute newline() function that I wrote, which gives appropriate line spacing upon executing each time.
ADDITIONALLY, (bonus question; may or maynot solve) Is there some way to extract the value 633.1881 and then calculate whether the next word fits in ( 700 - 633.1881 )px space or not?
There is an overload to MeasureString that returns the number of lines used in an out parameter: https://msdn.microsoft.com/en-us/library/957webty%28v=vs.110%29.aspx

System.Drawing.FontFamily->IsStyleAvailable always returns true

Im using a PrivateFontCollection to load a font via the AddMemoryFont. I retrieve the FontFamily, and then I query it using IsStyleAvailable to determine what the font supports as styles. However, with myriad fonts every single call to IsStyleAvailable returns true.
PrivateFontCollection pfc = new PrivateFontCollection();
var fontBuffer = Marshal.AllocCoTaskMem(dta.Length);
Marshal.Copy(dta, 0, fontBuffer, dta.Length);
pfc.AddMemoryFont(fontBuffer, dta.Length);
System.Drawing.FontFamily fam = pfc.Families[0];
if (fam.IsStyleAvailable(d.FontStyle.Bold)) //do something
Does anyone know how to get the actual style information from the FontFamily? If you look at the C:\Windows\Fonts folder you can see the supported styles. For example: Agency FB supports Bold; Regular, but when I query it in this fashion I get styles for Underline, Strikeout, and Italic, as well as Bold and Regular.
Is there a better way to go about this?
The font engine in Windows knows how to synthesize a style from the unstyled base font. It isn't particularly difficult to do on paper, just makes the stems fatter to get bold, tilt them to get italic, draw a line to get underline or strike-out. It isn't exactly as pretty as the dedicated outlines that a good designer will create but it certainly gets the job done. So when you ask "can you do that?" then you'll get a resounding "sure thing!"
Since you explicitly added the TTF files, you already know what styles are directly supported without synthesis and should not need to ask. Finding out anyway is perhaps possible with pinvoke and/or digging through the TTF tables but it is going to be ugly and certainly not directly supported by .NET. There's no winapi function I know of that tells you directly.

Categories