Skip to content

Commit

Permalink
Implement a piece of a font module
Browse files Browse the repository at this point in the history
  • Loading branch information
milenkoviclazar committed Sep 17, 2018
1 parent 5c174cf commit 956418a
Showing 1 changed file with 35 additions and 41 deletions.
76 changes: 35 additions & 41 deletions pygame/font.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
var $builtinmodule = function (name) {
// pygame.font
PygameLib.font_module = function (name) {
mod = {};
mod.__is_initialized = false;
mod.SysFont = Sk.misceval.buildClass(mod, font_SysFont, "SysFontType", []);
PygameLib.SysFontType = mod.SysFontType;
mod.Font = Sk.misceval.buildClass(mod, font_Font, "FontType", []);
PygameLib.FontType = mod.Font;
mod.SysFont = new Sk.builtin.func(function (name, size, bold, italic) {
var font = Sk.misceval.callsim(PygameLib.FontType, size);
Sk.abstr.sattr(font, 'name', name, false);
Sk.abstr.sattr(font, 'sz', size, false);
if (bold === undefined) {
Sk.abstr.sattr(font, 'bold', Sk.ffi.remapToPy(false), false);
} else {
Sk.abstr.sattr(font, 'bold', bold, false);
}
if (italic === undefined) {
Sk.abstr.sattr(font, 'italic', Sk.ffi.remapToPy(false), false);
} else {
Sk.abstr.sattr(font, 'italic', italic, false);
}
Sk.abstr.sattr(font, 'underline', Sk.ffi.remapToPy(false), false);
return font;
});
mod.init = new Sk.builtin.func(function () {
mod.__is_initialized = true;
});
Expand All @@ -19,43 +37,23 @@ var $builtinmodule = function (name) {
return Sk.ffi.remapToPy('arial');
});
mod.get_fonts = new Sk.builtin.func(function () {
return Sk.ffi.remapToPy(['arial', 'helvetica', 'times', 'courier']);
return Sk.ffi.remapToPy(fonts_osx);
});
mod.match_font = new Sk.builtin.func(function () {
return Sk.builtin.none.none$;
});
return mod;
};

function font_SysFont($gbl, $loc) {
$loc.__init__ = new Sk.builtin.func(function (self, name, size, bold, italic) {
function font_Font($gbl, $loc) {
$loc.__init__ = new Sk.builtin.func(function (self, filename, size) {
Sk.abstr.sattr(self, 'name', name, false);
Sk.abstr.sattr(self, 'sz', size, false);
if (bold === undefined) {
Sk.abstr.sattr(self, 'bold', Sk.ffi.remapToPy(false), false);
} else {
Sk.abstr.sattr(self, 'bold', bold, false);
}
if (italic === undefined) {
Sk.abstr.sattr(self, 'italic', Sk.ffi.remapToPy(false), false);
} else {
Sk.abstr.sattr(self, 'italic', italic, false);
}
Sk.abstr.sattr(self, 'bold', Sk.ffi.remapToPy(false), false);
Sk.abstr.sattr(self, 'italic', Sk.ffi.remapToPy(false), false);
Sk.abstr.sattr(self, 'underline', Sk.ffi.remapToPy(false), false);
return Sk.builtin.none.none$;
}, $gbl);
$loc.__init__.co_name = new Sk.builtins['str']('__init__');
$loc.__init__.co_varnames = ['bold', 'italic'];
$loc.__init__.$defaults = [false, false];

$loc.__repr__ = new Sk.builtin.func(function (self) {
var name = Sk.ffi.remapToJs(Sk.abstr.gattr(self, 'name', false));
var size = Sk.ffi.remapToJs(Sk.abstr.gattr(self, 'sz', false));
return Sk.ffi.remapToPy('<SysFont(' + name + ' ' + size + ')>');
});
$loc.__repr__.co_name = new Sk.builtins['str']('__repr__');
$loc.__repr__.co_varnames = ['self'];

$loc.render = new Sk.builtin.func(renderFont, $gbl);
$loc.render.co_name = new Sk.builtins['str']('render');
$loc.render.co_varnames = ['self', 'text', 'antialias', 'color', 'background'];
Expand Down Expand Up @@ -86,8 +84,6 @@ function font_SysFont($gbl, $loc) {
}, $gbl);
}

font_SysFont.co_name = new Sk.builtins['str']('SysFont');

function fontSize(self, text) {
var msg = Sk.ffi.remapToJs(text);
var h = 1.01 * Sk.ffi.remapToJs(Sk.abstr.gattr(self, 'sz', false));
Expand All @@ -105,9 +101,8 @@ function fontSize(self, text) {

// Create a dummy canvas in order to exploit its measureText() method
var t = Sk.builtin.tuple([w, h]);
var s = Sk.misceval.callsim(PygameLib.SurfaceType, t);
var ctx = s.offscreen_canvas.getContext("2d");

var s = Sk.misceval.callsim(PygameLib.SurfaceType, t, false);
var ctx = s.main_canvas.getContext("2d");
ctx.font = fontName;
return new Sk.builtin.tuple([ctx.measureText(msg).width, h]);
}
Expand All @@ -132,23 +127,22 @@ function renderFont(self, text, antialias, color, background) {

// Create a dummy canvas in order to exploit its measureText() method
var t = Sk.builtin.tuple([w, h]);
var s = Sk.misceval.callsim(PygameLib.SurfaceType, t);
var ctx = s.offscreen_canvas.getContext("2d");

var s = Sk.misceval.callsim(PygameLib.SurfaceType, t, false);
var ctx = s.main_canvas.getContext("2d");
ctx.font = fontName;
w = ctx.measureText(msg).width;

t = Sk.builtin.tuple([w, h]);
s = Sk.misceval.callsim(PygameLib.SurfaceType, t);
ctx = s.offscreen_canvas.getContext("2d");
s = Sk.misceval.callsim(PygameLib.SurfaceType, t, false);
ctx = s.main_canvas.getContext("2d");
if (background !== undefined) {
var background_js = PygameLib.extract_color(background);
var background_js = extract_color(background);
ctx.fillStyle = 'rgba(' + background_js[0] + ', ' + background_js[1] + ', ' + background_js[2] + ', '
+ background_js[3] + ')';
ctx.fillRect(0, 0, s.offscreen_canvas.width, s.offscreen_canvas.height);
ctx.fillRect(0, 0, s.main_canvas.width, s.main_canvas.height);
}
ctx.font = fontName;
var color_js = PygameLib.extract_color(color);
var color_js = extract_color(color);
ctx.fillStyle = 'rgba(' + color_js[0] + ', ' + color_js[1] + ', ' + color_js[2] + ', ' + color_js[3] + ')';
ctx.fillText(msg, 0, 1 / STRETCH_CONST * h);
if (underline) {
Expand Down

0 comments on commit 956418a

Please sign in to comment.