/* exported function documented in render/font_internal.h */ void font_plot_style_from_css(const css_computed_style *css, plot_font_style_t *fstyle) { lwc_string **families; css_fixed length = 0; css_unit unit = CSS_UNIT_PX; css_color col; fstyle->family = plot_font_generic_family( css_computed_font_family(css, &families)); css_computed_font_size(css, &length, &unit); fstyle->size = FIXTOINT(FMUL(nscss_len2pt(length, unit), INTTOFIX(FONT_SIZE_SCALE))); /* Clamp font size to configured minimum */ if (fstyle->size < (nsoption_int(font_min_size) * FONT_SIZE_SCALE) / 10) fstyle->size = (nsoption_int(font_min_size) * FONT_SIZE_SCALE) / 10; fstyle->weight = plot_font_weight(css_computed_font_weight(css)); fstyle->flags = plot_font_flags(css_computed_font_style(css), css_computed_font_variant(css)); css_computed_color(css, &col); fstyle->foreground = nscss_color_to_ns(col); fstyle->background = 0; }
/** * Convert a CSS length to pixels. * * \param length Length to convert * \param unit Corresponding unit * \param style Computed style applying to length. May be NULL if unit is * neither em nor ex * \return length in pixels */ css_fixed nscss_len2px(css_fixed length, css_unit unit, const css_computed_style *style) { /* We assume the screen and ay other output has the same dpi */ css_fixed px_per_unit; assert(style != NULL || (unit != CSS_UNIT_EM && unit != CSS_UNIT_EX)); switch (unit) { case CSS_UNIT_EM: case CSS_UNIT_EX: { css_fixed font_size = 0; css_unit font_unit = CSS_UNIT_PT; css_computed_font_size(style, &font_size, &font_unit); /* Convert to points */ font_size = nscss_len2pt(font_size, font_unit); /* Clamp to configured minimum */ if (font_size < FDIV(INTTOFIX(nsoption_int(font_min_size)), F_10)) { font_size = FDIV(INTTOFIX(nsoption_int(font_min_size)), F_10); } /* Convert to pixels (manually, to maximise precision) * 1in = 72pt => 1pt = (DPI/72)px */ px_per_unit = FDIV(FMUL(font_size, nscss_screen_dpi), F_72); /* Scale ex units: we use a fixed ratio of 1ex = 0.6em */ if (unit == CSS_UNIT_EX) px_per_unit = FMUL(px_per_unit, FLTTOFIX(0.6)); } break; case CSS_UNIT_PX: px_per_unit = F_1; break; /* 1in = DPIpx */ case CSS_UNIT_IN: px_per_unit = nscss_screen_dpi; break; /* 1in = 2.54cm => 1cm = (DPI/2.54)px */ case CSS_UNIT_CM: px_per_unit = FDIV(nscss_screen_dpi, FLTTOFIX(2.54)); break; /* 1in = 25.4mm => 1mm = (DPI/25.4)px */ case CSS_UNIT_MM: px_per_unit = FDIV(nscss_screen_dpi, FLTTOFIX(25.4)); break; /* 1in = 72pt => 1pt = (DPI/72)px */ case CSS_UNIT_PT: px_per_unit = FDIV(nscss_screen_dpi, F_72); break; /* 1pc = 12pt => 1in = 6pc => 1pc = (DPI/6)px */ case CSS_UNIT_PC: px_per_unit = FDIV(nscss_screen_dpi, INTTOFIX(6)); break; default: px_per_unit = 0; break; } /* Ensure we round px_per_unit to the nearest whole number of pixels: * the use of FIXTOINT() below will truncate. */ px_per_unit += F_0_5; /* Calculate total number of pixels */ return FMUL(length, TRUNCATEFIX(px_per_unit)); }