Cocoanetics / DTCoreText

Methods to allow using HTML code with CoreText
BSD 2-Clause "Simplified" License
6.35k stars 1.18k forks source link

support for css rem #1265

Open Mamong opened 1 year ago

Mamong commented 1 year ago

in "NSCharacterSet+HTML.h" replace _cssLengthUnitCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"pxtem"]; to _cssLengthUnitCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"pxtrem"];

in "NSString+CSS.h" replace if ([numberUnitStr isEqualToString:@"em"] | [numberUnitStr isEqualToString:@"px"] | [numberUnitStr isEqualToString:@"pt"])
to
if ([numberUnitStr isEqualToString:@"em"] | [numberUnitStr isEqualToString:@"px"] | [numberUnitStr isEqualToString:@"pt"] | [numberUnitStr isEqualToString:@"rem"])

add below

    else if (ch == 'r') {
        // rem
        if (i<stringLength)
        {
            if (_characters[i++] == 'e')
            {
                if (i<stringLength)
                {
                    if (_characters[i++] == 'm')
                    {
                        // rem value
                       //  this is just a workaround. FIXME: rem should be calculated as  'value *=  root element font-size'
                        value *= textSize;
                    }
                 }
            }
        }
    }
  else if (ch == 'e')
odrobnik commented 1 year ago

That only allows it to parse but you are missing the conversion to points. Please send a change request with you changes/additions

Mamong commented 1 year ago

@odrobnik Hi, I'm writting an epub reader, then I run into a problem with one epub file. The font size of some content is just 1.00pt. I find the problem is that DTCoreText does not parse rem correctly. I fix it with my patch as above. But, I dont know how to get the 'root element' font-size. According to the defination, rem should be calculated as 'value *= root element font-size'. As a workaround, I just make it works like an em.