cake
/
libfontcharacter
Archived
1
0
Fork 0

Included reference and started working with it.

This commit is contained in:
Thomas Touhey 2017-02-19 21:53:01 +01:00
parent 698c550f85
commit b339f1bf1b
14 changed files with 361 additions and 1 deletions

4
.gitignore vendored
View File

@ -5,4 +5,8 @@
/lib*.dll*
/man
/include/lib*/characters.h
/src/translations/characters.c
__pycache__
.*.swp

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "reference"]
path = reference
url = https://github.com/cakeisalie5/fontcharacter_reference.git

View File

@ -24,6 +24,9 @@ mrproper: clean
$(call qcmd,$(RM) Makefile.cfg)
$(call qcmd,$(RM) -r lib$(NAME)-*)
$(call qcmd,$(RM) $(INCDIR)/lib$(NAME)/config.h)
$(call qcmd,$(RM) $(INCDIR)/lib$(NAME)/characters.h)
$(call qcmd,$(RM) $(SRCDIR)/translations/characters.c)
$(call qcmd,$(RM) -r tools/__pycache__)
# Remake everything (clean and build).
re: clean all

4
configure vendored
View File

@ -203,6 +203,10 @@ opt="--version=${version} "
# - no other option, yet?
tools/write-header-config $opt >include/${name}/config.h
# Create the characters files
tools/write-characters-header --ref ./reference >include/${name}/characters.h
tools/write-characters-source --ref ./reference >src/translations/characters.c
# Do it!
exec 3>&1 1>Makefile.cfg
cat <<EOF

View File

@ -10,8 +10,10 @@
#ifndef LIBFONTCHARACTER_H
# define LIBFONTCHARACTER_H
# include <libfontcharacter/config.h>
# include <libfontcharacter/characters.h>
# include <stdlib.h>
# include <stdint.h>
# include <wchar.h>
# define FC_MB_MAX 2
# ifdef __cplusplus
extern "C" {
@ -37,6 +39,15 @@ int fc_wctomb(char *s, FONTCHARACTER wc);
size_t fc_mbstowcs(FONTCHARACTER *dst, const char *src, size_t n);
size_t fc_wcstombs(char *dst, const FONTCHARACTER *src, size_t n);
/* These other utilities are specific to how FONTCHARACTER work.
* Resolve a multi-character: */
int fc_tomulti(FONTCHARACTER *dest, FONTCHARACTER opcode);
/* And this is to convert it to unicode! */
size_t fc_tounicode(wchar_t *dest, const FONTCHARACTER *src, size_t n);
# ifdef __cplusplus
}
# endif

View File

@ -11,6 +11,22 @@
# define LIBFONTCHARACTER_INTERNALS_H
# include <libfontcharacter.h>
/* No internals for now. */
/* ************************************************************************** */
/* Characters list */
/* ************************************************************************** */
/* character structure */
typedef struct {
FONTCHARACTER *multi;
wchar_t *uni;
short multisize, unisize;
} fc_char_t;
/* table structure */
typedef struct {
fc_char_t *chars;
} fc_table_t;
/* pointer to the entry */
const fc_table_t fc_tables[256];
#endif /* LIBFONTCHARACTER_INTERNALS_H */

1
reference Submodule

@ -0,0 +1 @@
Subproject commit f02363c3d522ea3674146eebdb59586a77355a51

32
src/translations/multi.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* _____ _ */
/* translations/multi.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: libfontcharacter | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2017/02/19 21:28:20 |___/ */
/* */
/* ************************************************************************** */
#include <libfontcharacter/internals.h>
#include <string.h>
/**
* fc_tomulti:
* Get the complete multi-character content behind a content.
*
* @arg dest the destination buffer (at least FC_MULTI_MAX bytes).
* @arg opcode the FONTCHARACTER opcode.
* @return the number of characters in the opcode.
*/
int fc_tomulti(FONTCHARACTER *dest, FONTCHARACTER opcode)
{
const fc_table_t * const table = &fc_tables[opcode >> 8];
if (!table) return (-1);
const fc_char_t * const character = &table->chars[opcode & 0xFF];
if (!character->multi) return (-1);
memcpy(dest, character->multi, character->multisize);
return ((int)character->multisize);
}

28
src/translations/touni.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* _____ _ */
/* translations/touni.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: libfontcharacter | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2017/02/19 21:23:13 |___/ */
/* */
/* ************************************************************************** */
#include <libfontcharacter/internals.h>
/**
* fc_tounicode:
* Convert a FONTCHARACTER string to Unicode.
*
* @arg dest the destination buffer.
* @arg src the source buffer (expected to be null-terminated).
* @arg n the size of the destination buffer.
* @return the number of written bytes.
*/
size_t fc_tounicode(wchar_t *dest, const FONTCHARACTER *src, size_t n)
{
(void)dest;
(void)src;
(void)n;
return (-1);
}

141
tools/fontcharacter.py Executable file
View File

@ -0,0 +1,141 @@
#!/usr/bin/env python3
import os, string, yaml
#******************************************************************************#
# Reference class #
#******************************************************************************#
class Reference:
# Initialization
def __init__(self, path, sets_only=False):
# Load sets
self.__load_sets(yaml.load(open(\
os.path.join(path, 'sets.yml')).read()))
if sets_only: return
# Load categories
self.categories = {}
for c in yaml.load(open(os.path.join(path, 'categories.yml')).read()):
self.__explore_category(c, '', '', '')
# Load all of the YAML files
self.__load_characters(yaml.load(open(\
os.path.join(path, 'characters.yml')).read()))
# Utility to explore a category
def __explore_category(self, c, id, prefix, suffix):
# Iterate on things
id += c['id']
try: prefix = c['prefix'] + ' ' + prefix
except: True
try: suffix = suffix + ' ' + c['suffix']
except: True
# Add current (sub)category
self.categories[id] = {'prefix': prefix, 'suffix': suffix}
# Explore subcategories
if c.get('sub'):
for s in c['sub']:
self.__explore_category(s, id + '/', prefix, suffix)
# Utility to explore sets
def __load_sets(self, raw):
self.default_set = ''
self.sets = {}
self.kids = {}
# Initialize parents
parents = {}
kids = {}
# Read raw entries
for s in raw:
self.sets[s['id']] = {
'description': s['description'],
'characters': {}}
self.kids[s['id']] = []
if s.get('default'): self.default_set = s['id']
try: parents[s['id']] = s['parent']
except: True
# Add kids to parents
for k, p in parents.items():
self.kids[p].append(k)
# Recursive function to expand
kids = {}
def expand(id):
if kids.get(id) != None: return kids[id]
kids[id] = []
for kid in self.kids[id]:
kids[id] += [kid]
kids[id] += expand(kid)
return kids[id]
# Use the previous function
for id, _ in self.sets.items():
self.kids[id] = expand(id)
# Utility to load characters
def __load_characters(self, raw):
# Main loop
for c in raw:
# Get the complete name
n = c['name']
if c.get('category'):
ct = self.categories[c['category']]
n = ct['prefix'] + n + ct['suffix']
# Get the character set, and the priority
try: st = c['set']
except: st = self.default_set
pr = len(self.kids[st])
# Make the character
code = c['code']
char = {'name': n, '_pr': pr}
# Check the multi thingy
m = c.get('multi')
if type(m) == str:
char['multi'] = list(map(lambda x:int(x, 16), m.split(',')))
elif type(m) == int:
char['multi'] = [m]
# Check the unicode thingy
u = c.get('unicode')
if type(u) == str:
char['unicode'] = list(map(lambda x:int(x, 16), u.split(',')))
elif type(u) == int:
char['unicode'] = [u]
# Check the id thingy
m = c.get('id')
if not m: m = ''.join(ch for ch in c['name'] if ch in \
string.ascii_letters + string.digits)
char['id'] = m
# Add it to the sets
self.sets[st]['characters'][code] = char
for kid in self.kids[st]:
exst = self.sets[kid]['characters'].get(code)
if not exst or exst['_pr'] > pr:
self.sets[kid]['characters'][code] = char
# Remove the private thingies
for _, s in self.sets.items():
for _, c in s['characters'].items():
try: del c['_pr']
except: True
# Get the list of sets
def list(self):
return self.sets.keys()
# Get a set
def get(self, id = None):
if type(id) != str:
id = self.default_set
st = self.sets[id]
st['id'] = id
return st

53
tools/write-characters-header Executable file
View File

@ -0,0 +1,53 @@
#!/usr/bin/env python3
#******************************************************************************#
# Parse arguments #
#******************************************************************************#
from argparse import ArgumentParser
ap = ArgumentParser(description='libfontcharacter characters headers maker')
ap.add_argument('--ref', dest='refpath', required=True,
help='Reference path')
ap.add_argument('--set', dest='set', default=None,
help='The set to use')
args = ap.parse_args()
#******************************************************************************#
# Import the characters #
#******************************************************************************#
from fontcharacter import Reference
ref = Reference(args.refpath)
# Get the set
st = ref.get(args.set)
st['description'] = '\n * '.join(st['description'].split('\n'))
# Get the maximum multi size
maxmulsize = 0
for _, char in st['characters'].items():
if not char.get('multi'): continue
lm = len(char['multi'])
if lm > maxmulsize: maxmulsize = lm
#******************************************************************************#
# Make the header #
#******************************************************************************#
# Beginning
print('''\
#ifndef LIBFONTCHARACTER_CHARACTERS_H
# define LIBFONTCHARACTER_CHARACTERS_H
/* Used set: '%s'
* %s */
# define FC_MAX_MULTI %d
'''%(st['id'], st['description'], maxmulsize))
# Characters
for code, char in st['characters'].items():
#print('# define fc_%s 0x%04X'%(char['id'], code))
pass # TODO
# End
print('''
#endif /* LIBFONTCHARACTER_CHARACTERS_H */''')
# End of file.

64
tools/write-characters-source Executable file
View File

@ -0,0 +1,64 @@
#!/usr/bin/env python3
#******************************************************************************#
# Parse arguments #
#******************************************************************************#
from argparse import ArgumentParser
ap = ArgumentParser(description='libfontcharacter characters headers maker')
ap.add_argument('--ref', dest='refpath', required=True,
help='Reference path')
ap.add_argument('--set', dest='set', default=None,
help='The set to use')
args = ap.parse_args()
#******************************************************************************#
# Import the characters #
#******************************************************************************#
from fontcharacter import Reference
ref = Reference(args.refpath)
# Get the set
st = ref.get(args.set)
st['description'] = '\n * '.join(st['description'].split('\n'))
# Get the leading thingies [TODO: get that from the 'leading' field?]
chars = {}
maxmulsize = 0
maxunisize = 0
for code, char in st['characters'].items():
if not chars.get(code >> 8): chars[code >> 8] = {}
chars[code >> 8][code & 0xFF] = char
# Print some characters
def getchars(chars, size):
return ', '.join(map(lambda x:'0x%0*X'%(size * 2, x), chars))
#******************************************************************************#
# Make the source #
#******************************************************************************#
# Beginning
print('''#include <libfontcharacter/internals.h>
/* Used set: '%s'
* %s */
const fc_table_t fc_tables[256] = {'''%(st['id'], st['description']))
for lead, lchars in chars.items():
print(' [0x%02X] = (fc_char_t[256]){'%lead)
for code, char in lchars.items():
if not char.get('multi'): f = [(lead << 8) | code]
else: f = char['multi']
if char.get('unicode'): u = char['unicode']
else: u = None
print(' [0x%02X] = {'%code)
print(' .multi = (FONTCHARACTER[]){%s},'%getchars(f, 2))
if u: print(' .uni = (wchar_t[]){%s},'%getchars(u, 4))
print(' .multisize = %d,'%len(f))
if u: print(' .unisize = %d,'%len(u))
print(' },')
print(' },')
print('};')
# End of file.