summaryrefslogtreecommitdiff
path: root/source/html/css-apply.c
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-01-04 16:56:26 +0100
committerTor Andersson <tor.andersson@artifex.com>2016-01-05 14:47:37 +0100
commitd299bd0ab5f2d1ede4266febe5728a0f4075b961 (patch)
tree09319d7ea0baa6f2ac0a7d919b7cf19c115ea68c /source/html/css-apply.c
parentbececf00d7da9b2a200ab1e967c101e058b74dfa (diff)
downloadmupdf-d299bd0ab5f2d1ede4266febe5728a0f4075b961.tar.xz
epub: Speed up CSS style application by using faster strtod.
CSS doesn't need precision, and doesn't support exponential notation. Use a simpler string to float conversion algorithm, which is magnitudes of order faster.
Diffstat (limited to 'source/html/css-apply.c')
-rw-r--r--source/html/css-apply.c45
1 files changed, 42 insertions, 3 deletions
diff --git a/source/html/css-apply.c b/source/html/css-apply.c
index acb19244..40359d28 100644
--- a/source/html/css-apply.c
+++ b/source/html/css-apply.c
@@ -691,6 +691,45 @@ make_number(float v, int u)
return n;
}
+/* Fast but inaccurate strtof. */
+static float
+fz_css_strtof(char *s, char **endptr)
+{
+ float sign = 1;
+ float v = 0;
+ float n = 0;
+ float d = 1;
+
+ if (*s == '-')
+ {
+ sign = -1;
+ ++s;
+ }
+
+ while (*s >= '0' && *s <= '9')
+ {
+ v = v * 10 + (*s - '0');
+ ++s;
+ }
+
+ if (*s == '.')
+ {
+ ++s;
+ while (*s >= '0' && *s <= '9')
+ {
+ n = n * 10 + (*s - '0');
+ d = d * 10;
+ ++s;
+ }
+ v += n / d;
+ }
+
+ if (endptr)
+ *endptr = s;
+
+ return sign * v;
+}
+
static fz_css_number
number_from_value(fz_css_value *value, float initial, int initial_unit)
{
@@ -700,14 +739,14 @@ number_from_value(fz_css_value *value, float initial, int initial_unit)
return make_number(initial, initial_unit);
if (value->type == CSS_PERCENT)
- return make_number((float)fz_strtod(value->data, NULL), N_PERCENT);
+ return make_number(fz_css_strtof(value->data, NULL), N_PERCENT);
if (value->type == CSS_NUMBER)
- return make_number((float)fz_strtod(value->data, NULL), N_NUMBER);
+ return make_number(fz_css_strtof(value->data, NULL), N_NUMBER);
if (value->type == CSS_LENGTH)
{
- float x = (float)fz_strtod(value->data, &p);
+ float x = fz_css_strtof(value->data, &p);
if (p[0] == 'e' && p[1] == 'm')
return make_number(x, N_SCALE);