refactor font rewriting

Our goal is to allow decks created on one platform to use similar fonts (even
if named differently) when moving to another platform. The old solution wasn't
useful for the web version or mobile versions. Instead, we store a mapping in
the deck, and when generating the CSS, we list all possible fonts. An option
in the interface for the user to add extra fonts might be nice.
This commit is contained in:
Damien Elmes 2011-03-11 03:41:42 +09:00
parent 81a093a8f4
commit 0e084f8e3d
3 changed files with 18 additions and 53 deletions

View file

@ -12,7 +12,6 @@ from anki.utils import parseTags, tidyHTML, ids2str, hexifyID, \
canonifyTags, joinTags, addTags, deleteTags, checksum, fieldChecksum, \
stripHTML, intTime, splitFields
from anki.fonts import toPlatformFont
from anki.hooks import runHook, hookEmpty, runFilter
from anki.sched import Scheduler
@ -55,6 +54,9 @@ defaultConf = {
\\begin{document}
""",
'latexPost': "\\end{document}",
'fontFamilies': [
[u' 明朝',u'ヒラギノ明朝 Pro W3',u'Kochi Mincho', u'東風明朝']
]
}
# this is initialized by storage.Deck

View file

@ -1,50 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
import sys
# set this to 'all', to get all fonts in a list
policy="platform"
mapping = [
[u"Mincho", u"MS Mincho", "win32"],
[u"Mincho", u" 明朝", "win32"],
[u"Mincho", u"ヒラギノ明朝 Pro W3", "mac"],
[u"Mincho", u"Kochi Mincho", "linux"],
[u"Mincho", u"東風明朝", "linux"],
]
def platform():
if sys.platform == "win32":
return "win32"
elif sys.platform.startswith("darwin"):
return "mac"
else:
return "linux"
def toCanonicalFont(family):
"Turn a platform-specific family into a canonical one."
for (s, p, type) in mapping:
if family == p:
return s
return family
def toPlatformFont(family):
"Turn a canonical font into a platform-specific one."
if policy == "all":
return allFonts(family)
ltype = platform()
for (s, p, type) in mapping:
if family == s and type == ltype:
return p
return family
def substitutions():
"Return a tuple mapping canonical fonts to platform ones."
type = platform()
return [(s, p) for (s, p, t) in mapping if t == type]
def allFonts(family):
ret = ", ".join([p for (s, p, t) in mapping if s == family])
return ret or family

View file

@ -4,7 +4,6 @@
import simplejson
from anki.utils import intTime, hexifyID
from anki.fonts import toPlatformFont
from anki.lang import _
# Models
@ -114,6 +113,8 @@ insert or replace into models values (?, ?, ?, ?, ?, ?)""",
##################################################
def genCSS(self):
if not self.id:
return ""
# fields
css = "".join([self._fieldCSS(
".fm%s.%s" % (hexifyID(self.id), hexifyID(c)),
@ -121,15 +122,27 @@ insert or replace into models values (?, ?, ?, ?, ?, ?)""",
for c, f in enumerate(self.fields)])
# templates
for t in self.templates:
if not t.id:
# not flushed yet, ignore for now
continue
css += "#cm%s {text-align:%s;background:%s}\n" % (
hexifyID(t.id),
("center", "left", "right")[t.conf['align']],
t.conf['bg'])
return css
def _rewriteFont(self, font):
"Convert a platform font to a multiplatform list."
font = font.lower()
for family in self.deck.conf['fontFamilies']:
for font2 in family:
if font == font2.lower():
return ",".join(family)
return font
def _fieldCSS(self, prefix, row):
(fam, siz, col, rtl, pre) = row
t = 'font-family:"%s";' % toPlatformFont(fam)
t = 'font-family:"%s";' % self._rewriteFont(fam)
t += 'font-size:%dpx;' % siz
t += 'color:%s;' % col
if rtl: