<commit_before>#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <libtexpdf/libtexpdf.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

pdf_doc *p = NULL;
double height = 0.0;
double precision = 65536.0;

int pdf_init (lua_State *L) {
  pdf_rect mediabox;
  const char*  fn = luaL_checkstring(L, 1);
  double w = luaL_checknumber(L, 2);
  height = luaL_checknumber(L, 3);

  p = texpdf_open_document(fn, 0, w, height, 0,0,0);
  texpdf_init_device(p, 1/precision, 2, 0);

  mediabox.llx = 0.0;
  mediabox.lly = 0.0;
  mediabox.urx = w;
  mediabox.ury = height;
  texpdf_files_init();
  texpdf_init_fontmaps();
  texpdf_doc_set_mediabox(p, 0, &mediabox);
  texpdf_add_dict(p->info,
               texpdf_new_name("Producer"),
               texpdf_new_string("SILE", 4));
  return 0;
}

int pdf_endpage(lua_State *L) {
  ASSERT(p);
  texpdf_doc_end_page(p);
  return 0;
};

int pdf_beginpage(lua_State *L) {
  ASSERT(p);
  texpdf_doc_begin_page(p, 1,0,height);
  return 0;
}

int pdf_finish(lua_State *L) {
  ASSERT(p);
  texpdf_close_document(p);
  texpdf_close_device  ();
  texpdf_close_fontmaps();
  texpdf_files_close();
  return 0;
}

int pdf_loadfont(lua_State *L) {
  const char * filename;
  int index = 0;
  double ptsize;
  int layout_dir = 0;
  int extend = 65536;
  int slant = 0;<commit_msg>Order of closing is important. Fixes #297 ?<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <libtexpdf/libtexpdf.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

pdf_doc *p = NULL;
double height = 0.0;
double precision = 65536.0;

int pdf_init (lua_State *L) {
  pdf_rect mediabox;
  const char*  fn = luaL_checkstring(L, 1);
  double w = luaL_checknumber(L, 2);
  height = luaL_checknumber(L, 3);

  p = texpdf_open_document(fn, 0, w, height, 0,0,0);
  texpdf_init_device(p, 1/precision, 2, 0);

  mediabox.llx = 0.0;
  mediabox.lly = 0.0;
  mediabox.urx = w;
  mediabox.ury = height;
  texpdf_files_init();
  texpdf_init_fontmaps();
  texpdf_doc_set_mediabox(p, 0, &mediabox);
  texpdf_add_dict(p->info,
               texpdf_new_name("Producer"),
               texpdf_new_string("SILE", 4));
  return 0;
}

int pdf_endpage(lua_State *L) {
  ASSERT(p);
  texpdf_doc_end_page(p);
  return 0;
};

int pdf_beginpage(lua_State *L) {
  ASSERT(p);
  texpdf_doc_begin_page(p, 1,0,height);
  return 0;
}

int pdf_finish(lua_State *L) {
  ASSERT(p);
  texpdf_files_close();
  texpdf_close_device  ();
  texpdf_close_document(p);
  texpdf_close_fontmaps();
  return 0;
}

int pdf_loadfont(lua_State *L) {
  const char * filename;
  int index = 0;
  double ptsize;
  int layout_dir = 0;
  int extend = 65536;
  int slant = 0;
{'fraction_non_alphanumeric': 0.1397331356560415, 'fraction_numerical': 0.02928094885100074, 'mean_word_length': 4.007421150278293, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3796862', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1139, 'n_words': 236}