UNPKG

node-gtk

Version:

GNOME Gtk+ bindings for NodeJS

160 lines (125 loc) 3.96 kB
/* * generator-scaled-font.js */ const fs = require('fs') const path = require('path') const util = require('util') const removeTrailingSpaces = require('remove-trailing-spaces') const { unindent } = require('./indent.js') const { generateClassMethodSource, generateSource, parseFile, getJSName, } = require('./generator.js') util.inspect.defaultOptions = { depth: 6 } const base = { name: 'ScaledFont', constructor: null, functions: null, isBase: true, type: 'cairo_scaled_font_t', prefix: /cairo_(([a-z0-9]+_)?scaled_font)?/, destroy: 'cairo_scaled_font_destroy', filename: __filename, dependencies: [ 'scaled-font.h', 'font-extents.h', 'font-face.h', 'font-options.h', 'glyph.h', 'matrix.h', 'text-cluster.h', 'text-extents.h', ], } generateCairoScaledFont() function generateCairoScaledFont() { const result = parseFile(path.join(__dirname, 'scaled-font.nid')) const declarations = result.declarations console.log(declarations) const namespaces = declarations.map((cur) => { const name = cur.namespace.name const options = name === base.name ? base : { name, constructor: null, functions: null, isBase: false, type: base.type, prefix: base.prefix, base, } const allFunctions = cur.namespace.declarations .filter(d => d.function) .map(d => { const fn = d.function fn.source = generateClassMethodSource(fn, options) return fn }) options.constructor = allFunctions.find(fn => fn.attributes.constructor === true) options.functions = allFunctions.filter(fn => fn.attributes.constructor !== true) return options }) /* ToyScaledFont * FtScaledFont * Win32ScaledFont * QuartzScaledFont */ const header = generateHeader(namespaces) const source = generateSource(base, namespaces) fs.writeFileSync(path.join(__dirname, 'scaled-font.h'), header) fs.writeFileSync(path.join(__dirname, 'scaled-font.cc'), source) } // Helpers function generateHeader(namespaces) { const classDeclarations = namespaces.map(generateClassDeclaration) return removeTrailingSpaces(unindent(` /* autogenerated by ${path.basename(__filename)} */ #pragma once #include <nan.h> #include <node.h> #include <girepository.h> #include <glib.h> #include <cairo.h> #include <cairo-ft.h> #ifdef PLATFORM_WIN # include <cairo-win32.h> #endif #ifdef PLATFORM_MAC # include <cairo-quartz.h> #endif namespace GNodeJS { namespace Cairo { ${classDeclarations.join('\n ')} }; // Cairo }; // GNodeJS `)) } function generateClassDeclaration(options) { return ` class ${options.name}: public ${options.isBase ? 'Nan::ObjectWrap' : 'ScaledFont'} { public: static Nan::Persistent<v8::FunctionTemplate> constructorTemplate; static Nan::Persistent<v8::Function> constructor; static void Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target);${options.isBase ? ` static void SetupTemplate();` : ` static void SetupTemplate(Local<v8::FunctionTemplate> parentTpl);`} static Local<v8::FunctionTemplate> GetTemplate(); static Local<v8::Function> GetConstructor(); static NAN_METHOD(New); ${options.functions.map(fn => (fn.attributes.ifdef ? `#ifdef ${fn.attributes.ifdef}\n ` : '') + `static NAN_METHOD(${getFunctionJSName(fn)});` + (fn.attributes.ifdef ? `\n #endif` : '') ).join('\n ')} ${options.isBase ? ` ${options.name}(${options.type}* data); ~${options.name}(); ${options.type}* _data; ` : ` ${options.name}(${options.base.type}* data) : ScaledFont(data) {}; `} };` } function getFunctionJSName(fn) { return getJSName(fn.name, /cairo_(([a-z0-9]+_)?scaled_font)?/) }