[Dev-luatex] dimension helper

Paweł Jackowski P.Jackowski at gust.org.pl
Sat Feb 20 23:56:47 CET 2010


Thank you.

In such moments I always wonder how far I can annoy you until being 
banned. One more try ;)

Hans:
> luatex is consistently scaled points and a helper can be written in luq 
> quite easily

tex.scale() is just a multiplication and a range check, which is 
trivial, much more trivial then what dimen_to_number does. tex.round() 
function is also trivial, but we do have a helper!

Nearly everything can be written in lua. Such a helper is needed not 
because it is difficult to implement, but to let the user have the same 
mechanism that is used internally. It is so generic, that everyone who 
will enter an intimate relationship with luatex will need to have his 
own dimen-to-sp function, unless luatex provide one.

Taco:
> Although I do fear that dimen_to_number (in ltexlib.c) will need
> un-fixing to be totally compatible with TeX82: I am using strtod()
> and the documented conversion ratios there instead of the actual
> code from Knuth. 

And when it will happen, the effects of

	tex.dimen[100] = '123mm'
	image.bbox = {0,0,'100bp','200pt'}

may be slightly different. So be it, noone will cry, because tex.sp() 
function would follow the new behaviour of dimen_to_number()...


Here is what works for me. Accept or deny, I won't insist any longer.

Index: ltexlib.c
===================================================================
--- ltexlib.c	(revision 3434)
+++ ltexlib.c	(working copy)
@@ -1269,6 +1269,24 @@
      return 1;
  }

+static int tex_scaledimen(lua_State * L) /* following vsetdimen() */
+{
+    int sp;
+    if (!lua_isnumber(L, 1)) {
+        if (lua_isstring(L, 1)) {
+            sp = dimen_to_number(L, lua_tostring(L, 1));
+        } else {
+            lua_pushstring(L, "argument must be a string or a number");
+            lua_error(L);
+            return 0;
+        }
+    } else {
+        lua_number2int(sp, lua_tonumber(L, 1));
+    }
+    lua_pushnumber(L, sp);
+    return 1;
+}
+
  #define hash_text(A) hash[(A)].rh

  static int tex_definefont(lua_State * L)
@@ -1571,6 +1589,7 @@
      {"getboxdp", getboxdp},
      {"round", tex_roundnumber},
      {"scale", tex_scaletable},
+    {"sp", tex_scaledimen},
      {"fontname", getfontname},
      {"fontidentifier", getfontidentifier},
      {"pdffontname", getpdffontname},

-- 
Pawe/l Jackowski
P.Jackowski at gust.org.pl


More information about the dev-luatex mailing list