@@ -3,7 +3,7 @@ ffi = require "ffi"
3
3
has_freetype, freetype = pcall ffi. load, " freetype"
4
4
has_fontconfig, fontconfig = pcall ffi. load, " fontconfig"
5
5
6
- import C , cdef, gc, new from ffi
6
+ import C , cast , cdef, gc, new from ffi
7
7
8
8
-- Set C definitions for freetype / taken from https://github.com/luapower/freetype/blob/master/freetype_h.lua
9
9
cdef [[
@@ -392,7 +392,7 @@ set_font_metrics = (face) ->
392
392
-- Mimicking GDI's behavior for asc/desc/height.
393
393
-- These fields are (apparently) sometimes used for signed values,
394
394
-- despite being unsigned in the spec.
395
- os2 = ffi . cast " TT_OS2*" , C . FT_Get_Sfnt_Table face, C . FT_SFNT_OS2
395
+ os2 = cast " TT_OS2*" , C . FT_Get_Sfnt_Table face, C . FT_SFNT_OS2
396
396
if os2 and ( tonumber ( os2. usWinAscent) + tonumber ( os2. usWinDescent) != 0 )
397
397
face. ascender = tonumber os2. usWinAscent
398
398
face. descender = - tonumber os2. usWinDescent
@@ -420,9 +420,9 @@ set_font_metrics = (face) ->
420
420
421
421
-- https://github.com/libass/libass/blob/17cb8da964c852835881658d0d7af35ef2d92f9e/libass/ass_font.c#L502
422
422
ass_face_set_size = ( face, size) ->
423
- rq = ffi . new " FT_Size_RequestRec"
424
- ffi . C . memset rq, 0 , ffi. sizeof rq
425
- rq. type = ffi . C . FT_SIZE_REQUEST_TYPE_REAL_DIM
423
+ rq = new " FT_Size_RequestRec"
424
+ C . memset rq, 0 , ffi. sizeof rq
425
+ rq. type = C . FT_SIZE_REQUEST_TYPE_REAL_DIM
426
426
rq. width = 0
427
427
rq. height = size * FONT_UPSCALE
428
428
rq. horiResolution = 0
@@ -438,7 +438,7 @@ ass_font_get_asc_desc = (face) ->
438
438
439
439
-- https://github.com/libass/libass/blob/db83d6770ba11cb9ef72f4a9de7a0e2b1dd7baa3/libass/ass_font.c#L516
440
440
ass_face_get_weight = ( face) ->
441
- os2 = ffi . cast " TT_OS2*" , freetype. FT_Get_Sfnt_Table face, C . FT_SFNT_OS2
441
+ os2 = cast " TT_OS2*" , freetype. FT_Get_Sfnt_Table face, C . FT_SFNT_OS2
442
442
os2Weight = os2 and tonumber ( os2. usWeightClass) or 0
443
443
styleFlags = tonumber face. style_flags
444
444
if os2Weight == 0
@@ -450,14 +450,14 @@ ass_face_get_weight = (face) ->
450
450
451
451
-- https://github.com/libass/libass/blob/db83d6770ba11cb9ef72f4a9de7a0e2b1dd7baa3/libass/ass_font.c#L561
452
452
ass_glyph_embolden = ( slot) ->
453
- if slot. format != ffi . C . FT_GLYPH_FORMAT_OUTLINE
453
+ if slot. format != C . FT_GLYPH_FORMAT_OUTLINE
454
454
return
455
455
str = freetype. FT_MulFix ( slot. face. units_per_EM, slot. face. size. metrics. y_scale) / FONT_UPSCALE
456
456
freetype. FT_Outline_Embolden slot. outline, str
457
457
458
458
-- https://github.com/libass/libass/blob/db83d6770ba11cb9ef72f4a9de7a0e2b1dd7baa3/libass/ass_font.c#L577
459
459
ass_glyph_italicize = ( slot) ->
460
- xfrm = ffi . new " FT_Matrix" , {
460
+ xfrm = new " FT_Matrix" , {
461
461
xx : 0x10000
462
462
xy : 0x05700
463
463
yx : 0x00000
@@ -480,21 +480,22 @@ class FreeType extends Init
480
480
error " Couldn't find #{@family} among your fonts"
481
481
482
482
-- Init FreeType
483
- @library = ffi . new " FT_Library[1]"
483
+ @library = new " FT_Library[1]"
484
484
err = freetype. FT_Init_FreeType @library
485
485
486
486
if err != 0
487
487
error " Failed to load freetype library"
488
488
489
489
-- Load font face
490
- @face = ffi . new " FT_Face[1]"
490
+ @face = new " FT_Face[1]"
491
491
err = freetype. FT_New_Face @library [ 0 ] , font_path, 0 , @face
492
492
493
493
if err != 0
494
494
error " Failed to load freetype face"
495
495
496
496
set_font_metrics @face [ 0 ]
497
497
ass_face_set_size @face [ 0 ] , @size
498
+
498
499
@ascender , @descender = ass_font_get_asc_desc @face [ 0 ]
499
500
@height = @ascender + @descender
500
501
@weight = tonumber ass_face_get_weight @face [ 0 ]
@@ -505,7 +506,7 @@ class FreeType extends Init
505
506
width, height = 0 , 0
506
507
for ci, char in UTF8 ( text) \ chars!
507
508
glyph_index = freetype. FT_Get_Char_Index face_size, UTF8 . charcodepoint char
508
- err = freetype. FT_Load_Glyph face_size, glyph_index, ffi . C . FT_LOAD_DEFAULT
509
+ err = freetype. FT_Load_Glyph face_size, glyph_index, C . FT_LOAD_DEFAULT
509
510
if err != 0
510
511
error " Failed to load the freetype glyph" , 2
511
512
callback ci, char, face_size. glyph
@@ -545,25 +546,47 @@ class FreeType extends Init
545
546
for i = 0 , glyph. outline. n_points - 1
546
547
glyph. outline. points[ i] . x += x
547
548
glyph. outline. points[ i] . y = ( glyph. outline. points[ i] . y * - 1 ) + @ascender * FONT_UPSCALE
548
- -- callbacks for point decomposition
549
+ -- callbacks for outline decomposition
549
550
move_to = ( to, user) ->
550
- table.insert build, { " m" , tonumber ( to. x) / FONT_UPSCALE , tonumber ( to. y) / FONT_UPSCALE }
551
+ table.insert build, {
552
+ " m"
553
+ tonumber ( to. x) * FONT_DOWNSCALE
554
+ tonumber ( to. y) * FONT_DOWNSCALE
555
+ }
551
556
return 0
552
557
line_to = ( to, user) ->
553
- table.insert build, { " l" , tonumber ( to. x) / FONT_UPSCALE , tonumber ( to. y) / FONT_UPSCALE }
558
+ table.insert build, {
559
+ " l"
560
+ tonumber ( to. x) * FONT_DOWNSCALE
561
+ tonumber ( to. y) * FONT_DOWNSCALE
562
+ }
554
563
return 0
555
564
conic_to = ( control, to, user) ->
556
- table.insert build, { " c" , tonumber ( control. x) / FONT_UPSCALE , tonumber ( control. y) / FONT_UPSCALE , tonumber ( to. x) / FONT_UPSCALE , tonumber ( to. y) / FONT_UPSCALE }
565
+ table.insert build, {
566
+ " c"
567
+ tonumber ( control. x) * FONT_DOWNSCALE
568
+ tonumber ( control. y) * FONT_DOWNSCALE
569
+ tonumber ( to. x) * FONT_DOWNSCALE
570
+ tonumber ( to. y) * FONT_DOWNSCALE
571
+ }
557
572
return 0
558
573
cubic_to = ( control1, control2, to, user) ->
559
- table.insert build, { " b" , tonumber ( control1. x) / FONT_UPSCALE , tonumber ( control1. y) / FONT_UPSCALE , tonumber ( control2. x) / FONT_UPSCALE , tonumber ( control2. y) / FONT_UPSCALE , tonumber ( to. x) / FONT_UPSCALE , tonumber ( to. y) / FONT_UPSCALE }
574
+ table.insert build, {
575
+ " b"
576
+ tonumber ( control1. x) * FONT_DOWNSCALE
577
+ tonumber ( control1. y) * FONT_DOWNSCALE
578
+ tonumber ( control2. x) * FONT_DOWNSCALE
579
+ tonumber ( control2. y) * FONT_DOWNSCALE
580
+ tonumber ( to. x) * FONT_DOWNSCALE
581
+ tonumber ( to. y) * FONT_DOWNSCALE
582
+ }
560
583
return 0
561
584
-- Define outline functions
562
- outline_funcs = ffi . new " FT_Outline_Funcs[1]"
563
- outline_funcs[ 0 ] . move_to = ffi . cast " FT_Outline_MoveToFunc" , move_to
564
- outline_funcs[ 0 ] . line_to = ffi . cast " FT_Outline_LineToFunc" , line_to
565
- outline_funcs[ 0 ] . conic_to = ffi . cast " FT_Outline_ConicToFunc" , conic_to
566
- outline_funcs[ 0 ] . cubic_to = ffi . cast " FT_Outline_CubicToFunc" , cubic_to
585
+ outline_funcs = new " FT_Outline_Funcs[1]"
586
+ outline_funcs[ 0 ] . move_to = cast " FT_Outline_MoveToFunc" , move_to
587
+ outline_funcs[ 0 ] . line_to = cast " FT_Outline_LineToFunc" , line_to
588
+ outline_funcs[ 0 ] . conic_to = cast " FT_Outline_ConicToFunc" , conic_to
589
+ outline_funcs[ 0 ] . cubic_to = cast " FT_Outline_CubicToFunc" , cubic_to
567
590
-- Decompose outline
568
591
err = freetype. FT_Outline_Decompose glyph. outline, outline_funcs, nil
569
592
if err != 0
@@ -592,36 +615,37 @@ class FreeType extends Init
592
615
x += tonumber ( glyph. metrics. horiAdvance) + ( @hspace * FONT_UPSCALE )
593
616
return table.concat paths, " "
594
617
618
+ -- Gets the complete list of fonts available on the system
595
619
getFonts : =>
596
620
-- Check whether or not the fontconfig library was loaded
597
621
unless has_fontconfig
598
622
error " fontconfig library couldn't be loaded" , 2
599
623
-- Get fonts list from fontconfig
600
- fontset = ffi. gc fontconfig. FcFontList ( fontconfig. FcInitLoadConfigAndFonts () , ffi. gc( fontconfig. FcPatternCreate () , fontconfig. FcPatternDestroy ) , ffi. gc( fontconfig. FcObjectSetBuild ( " family" , " fullname" , " style" , " outline" , " file" , nil ) , fontconfig. FcObjectSetDestroy )) , fontconfig. FcFontSetDestroy
624
+ fontset = ffi. gc fontconfig. FcFontList ( fontconfig. FcInitLoadConfigAndFonts () , ffi. gc( fontconfig. FcPatternCreate () , fontconfig. FcPatternDestroy ) , ffi. gc( fontconfig. FcObjectSetBuild ( " family" , " fullname" , " style" , " outline" , " file" , nil ) , fontconfig. FcObjectSetDestroy )) , fontconfig. FcFontSetDestroy
601
625
local font, family, fullname, style, outline, file, cstr, cbool
602
- cstr = ffi . new " FcChar8*[1]"
603
- cbool = ffi . new " FcBool[1]"
626
+ cstr = new " FcChar8*[1]"
627
+ cbool = new " FcBool[1]"
604
628
fonts = { n : 0 }
605
629
for i = 0 , fontset[ 0 ] . nfont - 1
606
630
font = fontset[ 0 ] . fonts[ i]
607
631
family, fullname, style, outline, file = nil , nil , nil , nil , nil
608
- if fontconfig. FcPatternGetString ( font, " family" , 0 , cstr) == ffi . C . FcResultMatchILL
632
+ if fontconfig. FcPatternGetString ( font, " family" , 0 , cstr) == C . FcResultMatchILL
609
633
family = ffi. string cstr[ 0 ]
610
- if fontconfig. FcPatternGetString ( font, " fullname" , 0 , cstr) == ffi . C . FcResultMatchILL
634
+ if fontconfig. FcPatternGetString ( font, " fullname" , 0 , cstr) == C . FcResultMatchILL
611
635
fullname = ffi. string cstr[ 0 ]
612
- if fontconfig. FcPatternGetString ( font, " style" , 0 , cstr) == ffi . C . FcResultMatchILL
636
+ if fontconfig. FcPatternGetString ( font, " style" , 0 , cstr) == C . FcResultMatchILL
613
637
style = ffi. string cstr[ 0 ]
614
- if fontconfig. FcPatternGetBool ( font, " outline" , 0 , cbool) == ffi . C . FcResultMatchILL
638
+ if fontconfig. FcPatternGetBool ( font, " outline" , 0 , cbool) == C . FcResultMatchILL
615
639
outline = cbool[ 0 ]
616
- if fontconfig. FcPatternGetString ( font, " file" , 0 , cstr) == ffi . C . FcResultMatchILL
640
+ if fontconfig. FcPatternGetString ( font, " file" , 0 , cstr) == C . FcResultMatchILL
617
641
file = ffi. string cstr[ 0 ]
618
642
if family and fullname and style and outline
619
643
fonts. n += 1
620
644
fonts[ fonts. n] = {
621
- name : family,
622
- longname : fullname,
623
- style : style,
624
- type : outline == 0 and " Raster" or " Outline" ,
645
+ name : family
646
+ longname : fullname
647
+ style : style
648
+ type : outline == 0 and " Raster" or " Outline"
625
649
path : file
626
650
}
627
651
-- Order fonts by name & style
0 commit comments