This is a migrated thread and some comments may be shown as answers.

HtmlFormatProvider font size

1 Answer 66 Views
RichTextBox (obsolete as of Q3 2014 SP1)
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Dan asked on 02 Jul 2014, 08:07 PM
Hello, I am using a combination of RtfFormatProvider and HtmlFormatProvider to allow users to convert Rich Text to HTML. I noticed that even when font size is expressed in RTF in points (or really half-points), when exported to HTML it exports it as pixel sizes - it appears to assume 96 pixels per inch.

Based on this, it changes a 14.5 point font in RTF to a 19.33333 pixel font in HTML.

This works well when the resulting HTML is viewed in an environment that renders at 96 pixels per inch, but in some environments (such as when viewing on a retina display iPad) it makes the font appear much smaller.

Is there a way to control the HtmlFormatProvider to save fonts in points instead of pixels?

1 Answer, 1 is accepted

Sort by
Ivan Todorov
Telerik team
answered on 07 Jul 2014, 11:24 AM
Hello Dan,

Thank you for contacting us.

RadRichTextBox uses pixels to calculate the internal layout of the document and for this reason all measurement units are converted to pixels when loading the document. However, I agree that storing them as pixels when exporting will cause compatibility issues across different devices. I have logged this in our Feedback Portal and you can review, track or vote for it here:

For the time being, the only workaround I can suggest is to post process the export result using regular expressions and convert all font sizes to pt units:
RtfFormatProvider provider = new RtfFormatProvider();
RadDocument document = provider.Import(File.ReadAllBytes("test.rtf"));
HtmlFormatProvider html = new HtmlFormatProvider();
string res = html.Export(document);
Regex regex = new Regex(@"font-size: [0-9]*\.?[0-9]*px");
Match match = null;
    match = regex.Match(res);
    if (!match.Success)
    string value = match.Value.Substring("font-size: ".Length, match.Value.Length - "font-size: ".Length - "px".Length);
    double pts = double.Parse(value) * 72 / 96;
    res = res.Replace(match.Value, @"font-size: " + Math.Round(pts, 4) + "pt");
} while (match.Success);
File.WriteAllText("output.html", res);

I have updated your Telerik points for bringing this to our attention.

Hope this will help. Should you have any further questions, do not hesitate to write back.

Ivan Todorov
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
RichTextBox (obsolete as of Q3 2014 SP1)
Asked by
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Share this question