1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
from dataclasses import dataclass
from fontFeatures import ValueRecord
from fontFeatures.utils import categorize_glyph
from youseedee import ucd_data
import sys
import warnings
def _add_value_records(vr1, vr2):
if vr1.xPlacement or vr2.xPlacement:
vr1.xPlacement = (vr1.xPlacement or 0) + (vr2.xPlacement or 0)
if vr1.yPlacement or vr2.yPlacement:
vr1.yPlacement = (vr1.yPlacement or 0) + (vr2.yPlacement or 0)
if vr1.xAdvance or vr2.xAdvance:
vr1.xAdvance = (vr1.xAdvance or 0) + (vr2.xAdvance or 0)
if vr1.yAdvance or vr2.yAdvance:
vr1.yAdvance = (vr1.yAdvance or 0) + (vr2.yAdvance or 0)
@dataclass
class BufferItem:
# codepoint: int
# glyph: str
# position: ValueRecord
# category: str
def __repr__(self):
s = ""
if self.glyph:
s = self.glyph
if self.category[0] == "base":
s = s + "_"
elif self.category[0] == "mark":
s = s + "^"
elif self.category[0] == "ligature":
s = s + "(fi)"
else:
s = s + "?"
else:
s = "U+%04x" % self.codepoint
return "BufferItem(%s)" % s
@classmethod
def new_unicode(klass, codepoint):
self = klass()
self.codepoint = codepoint
self.glyph = None
self.feature_masks = {}
return self
@classmethod
def new_glyph(klass, glyph, font):
self = klass()
self.codepoint = None
self.glyph = glyph
self.feature_masks = {}
self.prep_glyph(font)
return self
def map_to_glyph(self, font):
if not self.glyph:
self.glyph = font.unicode_map.get(self.codepoint)
if not self.glyph:
# Notdef
self.glyph = list(font.glyphs.keys())[0]
self.prep_glyph(font)
def prep_glyph(self, font):
if "pytest" in sys.modules:
if self.glyph in font.exported_glyphs():
self.gid = font.exported_glyphs().index(self.glyph)
else:
self.gid = -1 # ?
self.substituted = False
self.ligated = False
self.multiplied = False
self.recategorize(font)
try:
self.position = ValueRecord(xAdvance=0)
self.position.xAdvance = font.default_master.get_glyph_layer(
self.glyph
).width
except Exception:
if "pytest" in sys.modules:
# We tolerate broken fonts in pytest
pass
else:
raise ValueError(
"Could not get xAdvance for glyph %s (%i)"
% (self.glyph, self.codepoint)
)
def recategorize(self, font):
try:
self.category = categorize_glyph(font, self.glyph)
if not self.category[0]:
self.category = ("unknown", None)
except Exception as e:
warnings.warn("Error getting category: %s" % str(e))
self._fallback_categorize()
def _fallback_categorize(self):
if not self.codepoint:
# Now what?
self.category = ("unknown", None)
return
genCat = ucd_data(self.codepoint).get("General_Category", "L")
if genCat[0] == "M":
self.category = ("mark", None)
elif genCat == "Ll":
self.category = ("ligature", None)
elif genCat[0] == "L":
self.category = ("base", None)
else:
self.category = ("unknown", None)
def add_position(self, vr2):
_add_value_records(self.position, vr2)
class Buffer:
"""A buffer holding either characters to be shaped or shaped glyphs."""
itemclass = BufferItem
def __init__(
self, font, glyphs=[], unicodes=[], direction=None, script=None, language=None
):
self.font = font
self.direction = direction
self.script = script
self.language = language
self.fallback_mark_positioning = False
self.fallback_glyph_classes = False
self.items = []
self.mask = []
self.flags = 0
self.current_feature_mask = None
if glyphs:
self.store_glyphs(glyphs)
self.clear_mask()
elif unicodes:
self.store_unicode(unicodes)
self.guess_segment_properties()
def store_glyphs(self, glyphs):
"""Initialize the buffer with list of glyphs.
Args:
glyphs (list): A list of glyph names.
"""
self.items = [self.itemclass.new_glyph(g, self.font) for g in glyphs]
def store_unicode(self, unistring):
"""Initialize the buffer with list of Unicode codepoints.
Args:
glyphs (list or str): A list of characters.
"""
self.items = [self.itemclass.new_unicode(ord(char)) for char in unistring]
def guess_segment_properties(self):
"""Try to automatically determine the script and direction properties."""
for u in self.items:
# Guess segment properties
if not self.script:
thisScript = ucd_data(u.codepoint)["Script"]
if thisScript not in ["Common", "Unknown", "Inherited"]:
self.script = thisScript
if not self.direction:
from fontFeatures.shaperLib.Shaper import _script_direction
self.direction = _script_direction(self.script)
def map_to_glyphs(self):
"""Convert a buffer of codepoints to its initial glyph mapping."""
for u in self.items:
u.map_to_glyph(self.font)
self.clear_mask()
@property
def is_all_glyphs(self):
"""Returns true if the contents are glyph items."""
return all([x.glyph is not None for x in self.items])
@property
def is_all_unicodes(self):
"""Returns true if the contents are character items."""
return all([x.codepoint is not None for x in self.items])
def __getitem__(self, key):
indexed = self.mask[key]
if isinstance(indexed, range) or isinstance(indexed, slice):
indexed = slice(indexed.start, indexed.stop, indexed.step)
if isinstance(indexed, list):
return [self.items[g] for g in indexed]
return self.items[indexed]
def __setitem__(self, key, value):
indexed = self.mask[key]
if len(indexed) == 1: # Easy
self.items[indexed[0] : indexed[0] + 1] = value
return
if len(value) == 1: # Also easy
self.items[indexed[0]] = value[0]
for i in reversed(indexed[1:]):
del self.items[i]
return
else:
raise ValueError("Too hard :-(")
def __len__(self):
return len(self.mask)
def update(self):
"""Categorises glyphs and recomputes masks.
Called internally when the contents of the buffer changes."""
for g in self.items:
g.recategorize(self.font)
self.recompute_mask()
def clear_mask(self):
"""Clear the buffer mask."""
self.flags = 0
self.markFilteringSet = None
self.markAttachmentSet = None
self.current_feature_mask = None
self.recompute_mask()
def set_mask(self, flags, markFilteringSet=None, markAttachmentSet=None):
"""Apply a routine's flags and mark filtering set to the buffer."""
self.flags = flags
if self.flags & 0x10:
assert markFilteringSet
self.markFilteringSet = markFilteringSet
if self.flags & 0xFF00:
assert markAttachmentSet
self.markAttachmentSet = markAttachmentSet
self.recompute_mask()
def recompute_mask(self):
"""Computes the mask property
``buffer.mask`` is a list of buffer item indices which is the current
"view" of the buffer. For example, if ``buffer.flags == 0x8``, then the
indices of all mark glyphs will be excluded from ``buffer.mask``.
"""
mask = range(0, len(self.items))
self.flags = self.flags or 0
if self.flags & 0x2: # IgnoreBases
mask = list(filter(lambda ix: self.items[ix].category[0] != "base", mask))
if self.flags & 0x4: # IgnoreLigatures
mask = list(
filter(lambda ix: self.items[ix].category[0] != "ligature", mask)
)
if self.flags & 0x8: # IgnoreMarks
mask = list(filter(lambda ix: self.items[ix].category[0] != "mark", mask))
if self.flags & 0x10: # UseMarkFilteringSet
mask = list(
filter(
lambda ix: self.items[ix].category[0] != "mark"
or self.items[ix].glyph in self.markFilteringSet,
mask,
)
)
if self.flags & 0xFF00: # MarkAttachmentType
mask = list(
filter(
lambda ix: self.items[ix].category[0] != "mark"
or self.items[ix].glyph in self.markAttachmentSet,
mask,
)
)
if self.current_feature_mask:
feature = self.current_feature_mask
mask = list(
filter(
lambda ix: (feature not in self.items[ix].feature_masks)
or (not self.items[ix].feature_masks[feature]),
mask,
)
)
self.mask = mask
def set_feature_mask(self, feature):
"""Applies the mask for a particular feature."""
self.current_feature_mask = feature
self.recompute_mask()
def move_item(self, src, dest):
"""Moves an item from src to dest."""
self.items[dest:dest] = [self.items.pop(src)]
def merge_clusters(self, start, end):
"""Currently unimplemented."""
pass # XXX
def serialize(self, additional=None, position=True, names=True, ned=False):
"""Serialize a buffer to a string.
Returns:
The contents of the given buffer in a string format similar to
that used by ``hb-shape``.
"""
outs = []
if additional:
if not isinstance(additional, list):
additional = [additional]
else:
additional = []
xcursor = 0
for ix, info in enumerate(self.items):
if hasattr(info, "glyph") and info.glyph:
if names:
outs.append("%s" % info.glyph)
else:
outs.append("%s" % info.gid)
else:
outs.append("U+%04x" % info.codepoint)
if ned:
position = info.position
if xcursor + (position.xPlacement or 0):
outs[-1] = outs[-1] + "@%i,%i" % (
xcursor + (position.xPlacement or 0),
position.yPlacement or 0,
)
xcursor = xcursor + position.xAdvance
elif position and hasattr(info, "position"):
position = info.position
if hasattr(info, "syllable_index"):
cluster = info.syllable_index
else:
cluster = ix
outs[-1] = outs[-1] + "=%i" % (cluster)
if position.xPlacement or position.yPlacement:
outs[-1] = outs[-1] + "@%i,%i" % (
position.xPlacement or 0,
position.yPlacement or 0,
)
outs[-1] = outs[-1] + "+%i" % (position.xAdvance)
relevant = list(filter(lambda a: hasattr(info, a), additional))
if relevant:
outs[-1] = outs[-1] + "(%s)" % ",".join(
[str(getattr(info, a)) for a in relevant]
)
return "|".join(outs)
|