error converting HTML into an attributed string

  • The initWithHTML method of NSAttributedString is exactly what I need,
    and it works, for the most part.  However, I am experiencing one
    annoying problem with the conversion, and I'm wondering if anyone has
    any insight into it (i.e. despite my displeasure, perhaps this is the
    way it is intended to work).

    The problem is that the attributed-string / rich-text that I get from
    some HTML data always has a font size one-third bigger (rounded to
    the nearest whole integer) than what is specified in the HTML.  The
    fact that I am seeing an exact multiple, I thought this might
    (somehow) be on purpose.  The HTML I am asking to be converted is
    fairly limited.  It has a bunch of tags of the form:

    <ilayer style="font-family: Courier;font-size: 20pt;color: rgb
    (51,139,213);">

    i.e. an inconsequential HTML tag that includes inline CSS properties
    in order to specify the look of the text.  The problem is that the
    font size in the CSS does not result in an equivalent size in the
    resulting attributed string.  In this example, the resulting font
    size is 27 (20 * 1 1/3 = 26.66666 = 27 with rounding).

    I started experimenting with the initWithHTML method that accepts an
    options dictionary so I can specify an
    NSTextSizeMultiplierDocumentOption of 0.75, however, that does not
    seem to do a round-to-nearest-integral operation so I am ending up
    with non-integral font sizes that, while closer to the original,
    remain incorrect.

    (NOTE:  I am in control of the HTML that my code is converting, so if
    there is some way to fix this problem through a change to the HTML
    then that will suffice.)

    Hmm... I just noticed in the attributed string docs that "WebKit–
    based HTML importing" is always used on 10.4 and later, so, if no one
    has an answer, then I will repost on the web-kit list.

    Thanks in advance.

    Mark Sanvitale
    Exbiblio
    <mas...>
  • Please ignore the mistype where I wrote
    "NSTextSizeMultiplierDocumentOption of 0.75".  Yes, that will not
    result in a 1/3 reduction.  Even with the correct value, the problem
    persists.

    Mark

    On Nov 19, 2007, at 4:11 PM, Mark Sanvitale wrote:
    >
    > The initWithHTML method of NSAttributedString is exactly what I
    > need, and it works, for the most part.  However, I am experiencing
    > one annoying problem with the conversion, and I'm wondering if
    > anyone has any insight into it (i.e. despite my displeasure,
    > perhaps this is the way it is intended to work).
    >
    > The problem is that the attributed-string / rich-text that I get
    > from some HTML data always has a font size one-third bigger
    > (rounded to the nearest whole integer) than what is specified in
    > the HTML.  The fact that I am seeing an exact multiple, I thought
    > this might (somehow) be on purpose.  The HTML I am asking to be
    > converted is fairly limited.  It has a bunch of tags of the form:
    >
    > <ilayer style="font-family: Courier;font-size: 20pt;color: rgb
    > (51,139,213);">
    >
    > i.e. an inconsequential HTML tag that includes inline CSS
    > properties in order to specify the look of the text.  The problem
    > is that the font size in the CSS does not result in an equivalent
    > size in the resulting attributed string.  In this example, the
    > resulting font size is 27 (20 * 1 1/3 = 26.66666 = 27 with rounding).
    >
    > I started experimenting with the initWithHTML method that accepts
    > an options dictionary so I can specify an
    > NSTextSizeMultiplierDocumentOption of 0.75, however, that does not
    > seem to do a round-to-nearest-integral operation so I am ending up
    > with non-integral font sizes that, while closer to the original,
    > remain incorrect.
    >
    > (NOTE:  I am in control of the HTML that my code is converting, so
    > if there is some way to fix this problem through a change to the
    > HTML then that will suffice.)
    >
    > Hmm... I just noticed in the attributed string docs that "WebKit–
    > based HTML importing" is always used on 10.4 and later, so, if no
    > one has an answer, then I will repost on the web-kit list.
    >
    > Thanks in advance.
    >
    >
  • On 20/11/2007, at 10:11 AM, Mark Sanvitale wrote:

    > <ilayer style="font-family: Courier;font-size: 20pt;color:
    > rgb(51,139,213);">
    >
    > i.e. an inconsequential HTML tag that includes inline CSS properties
    > in order to specify the look of the text.  The problem is that the
    > font size in the CSS does not result in an equivalent size in the
    > resulting attributed string.  In this example, the resulting font
    > size is 27 (20 * 1 1/3 = 26.66666 = 27 with rounding).

    > (NOTE:  I am in control of the HTML that my code is converting, so
    > if there is some way to fix this problem through a change to the
    > HTML then that will suffice.)

    The problem is that point units in CSS are problematic, I recommend
    simply converting all the point measurements to pixels:

    <ilayer style="font-family: Courier;font-size: 20px;color:
    rgb(51,139,213);">

    This will probably give you the output you want.

    BTW, ilayer is an extremely old deprecated element, I assume this is
    legacy code because it is definitely no longer supported.

    --
    Rob Keniger
  • On Nov 19, 2007, at 4:11 PM, Mark Sanvitale wrote:

    > The initWithHTML method of NSAttributedString is exactly what I
    > need, and it works, for the most part.  However, I am experiencing
    > one annoying problem with the conversion, and I'm wondering if
    > anyone has any insight into it (i.e. despite my displeasure, perhaps
    > this is the way it is intended to work).
    >
    > The problem is that the attributed-string / rich-text that I get
    > from some HTML data always has a font size one-third bigger (rounded
    > to the nearest whole integer) than what is specified in the HTML.
    > The fact that I am seeing an exact multiple, I thought this might
    > (somehow) be on purpose.  The HTML I am asking to be converted is
    > fairly limited.  It has a bunch of tags of the form:
    >
    > <ilayer style="font-family: Courier;font-size: 20pt;color:
    > rgb(51,139,213);">
    >
    > i.e. an inconsequential HTML tag that includes inline CSS properties
    > in order to specify the look of the text.  The problem is that the
    > font size in the CSS does not result in an equivalent size in the
    > resulting attributed string.  In this example, the resulting font
    > size is 27 (20 * 1 1/3 = 26.66666 = 27 with rounding).

    Unfortunately, there are a number of cases in which the way that web
    browsers actually work differs from what the standard may specify, due
    to compatibility considerations.  CSS points were originally intended
    to be absolute units of 1/72", but the reality is otherwise.  For
    compatibility with IE, browsers assume a hard-coded ratio of 1 pt =
    4/3 px.  (Try creating a document with text in (a) 12px; (b) 12pt; (c)
    16px and opening it in any browser.)

    Since CSS px units correspond most closely to Cocoa's points, font
    sizes specified in px map one-to-one, while font sizes specified in pt
    are scaled by 4/3.  At the moment there is no way to control the 4/3
    ratio between pt and px.  If this is HTML that you control, the
    recommendation is to use px instead of pt.  For example, AppKit's HTML
    generation uses px so that fonts round-trip correctly.

    Douglas Davidson
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today