def _cast_inline(self, LINE, runinfo, F, FSTYLE): self._color = FSTYLE['color'] if self['cl_fraction']: F_fraction = F + self['cl_fraction'] else: F_fraction = F ascent, descent = FSTYLE['__fontmetrics__'] vy = int(FSTYLE['fontsize'] * 0.25) numerator = cast_mono_line(LINE, self._numerator.content, runinfo, F_fraction) denominator = cast_mono_line(LINE, self._denominator.content, runinfo, F_fraction) gapline = cast_mono_line(LINE, [], runinfo, F_fraction) vgap = int(gapline['fstyle']['fontsize'] * 0.15) fracwidth = max(numerator['advance'], denominator['advance']) fracwidth = fracwidth + LINE['leading']*0.05 + sqrt(fracwidth)*LINE['leading']*0.05 numerator['x'] = (fracwidth - numerator['advance'])/2 numerator['y'] = -vy - vgap + numerator['descent'] denominator['x'] = (fracwidth - denominator['advance'])/2 denominator['y'] = -vy + vgap + denominator['ascent'] fascent = vy + vgap+ numerator['ascent'] - numerator['descent'] fdescent = vy - vgap - denominator['ascent'] + denominator['descent'] self._fracwidth = fracwidth return [numerator, denominator], fracwidth, fascent, fdescent, (self._paint, 0, -vy, 0)
def _cast_inline(self, LINE, runinfo, F, FSTYLE): limit = cast_mono_line(LINE, self.content, runinfo, F + self['cl_limit']) lim = cast_mono_line(LINE, 'lim', runinfo, F) width = max(lim['advance'], limit['advance']) lim['x'] = (width - lim['advance'])*0.5 lim['y'] = 0 limit['x'] = (width - limit['advance'])*0.5 limit['y'] = -lim['descent'] + limit['ascent'] return [lim, limit], width, lim['ascent'], limit['descent']
def _cast_inline(self, LINE, runinfo, F, FSTYLE): limit = cast_mono_line(LINE, self.content, runinfo, F + self['cl_limit']) lim = cast_mono_line(LINE, 'lim', runinfo, F) width = max(lim['advance'], limit['advance']) lim['x'] = (width - lim['advance']) * 0.5 lim['y'] = 0 limit['x'] = (width - limit['advance']) * 0.5 limit['y'] = -lim['descent'] + limit['ascent'] return [lim, limit], width, lim['ascent'], limit['descent']
def _yield_linespaces(self, frames, BSTYLE): leading = BSTYLE['leading'] indent_range = BSTYLE['indent_range'] D, SIGN, K = BSTYLE['indent'] indent = None l = 0 while True: u, x1, x2, y, c, pn = frames.fit(leading) # calculate indentation if l in indent_range: if indent is None: if K: length = cast_mono_line({'l': l, 'c': c, 'page': pn, 'BLOCK': self, 'leading': leading}, self.content[:K], BSTYLE['__runinfo__'], length_only=True) indent = D + length * SIGN else: indent = D x1 += indent if x1 > x2: x1, x2 = x2, x1 yield OT_line({'BLOCK': self, 'leading': leading, 'start': x1, 'width': x2 - x1, 'y': y, 'c': c, 'u': u, 'l': l, 'page': pn}) l += 1
def _generate_numbers(self, numerals, PARENTLINE, runinfo, k): sign = round(self._perpendicular[0]) for x, y, u in numerals: label = cast_mono_line(PARENTLINE, str(u).replace('-', '−'), runinfo) label.nail_to(x, y, k, sign) yield label
def _layout_block(self, frames, BSTYLE, overlay): self._frames = frames monos = [] planes = [] paint_functions = [] paint_annot_functions = [] x1, width, y, c, pn, block_top_u, block_bottom_u = self._layout_axes(frames, overlay + self['cl_axes'], planes) u = self._keys.layout_keys(frames, overlay + self['cl_key'], planes, paint_functions, block_top_u, block_bottom_u, self['key_shift'], self['key_bottom']) PARENTLINE = {'l': 0, 'c': c, 'page': pn, 'leading': BSTYLE['leading'], 'BLOCK': self} for E in chain(self._CS, self._DS): mono, paint, paint_annot = E.inflate(width, x1, y, PARENTLINE, BSTYLE) monos.extend(mono) paint_functions.extend((pn, (F, x1, y, Z)) for Z, F in paint) #paint_annot_functions.extend((pn, (F, x1, y)) for F in paint_annot) paint_functions.sort(key=lambda k: k[1][3]) if all(A.floating for A in self._CS): perp_x, perp_y = map(sum, zip( * (A.lettervector for A in self._CS) )) mag = sqrt(perp_x**2 + perp_y**2) O_label = cast_mono_line(PARENTLINE, 'O', BSTYLE['__runinfo__'], self._CS[0]['cl_variable']) O_label.nail_to(x1 + self._origin[0]*width + perp_x, y + self._origin[1]*-self['height'] + perp_y, align=round(perp_x/mag)) monos.append(O_label) planes_set = set(planes) return u, monos, [], planes, paint_functions, [], lambda O: O in planes_set, self._keys.get_color()
def _cast_inline(self, LINE, runinfo, F, FSTYLE): self._color = FSTYLE['color'] y = FSTYLE['shift'] rad = cast_mono_line(LINE, self._radicand.content, runinfo, F) rad_asc = rad['ascent'] rad_desc = rad['descent'] rfs = FSTYLE['fontsize'] iy = y - rfs * 0.44 - FSTYLE['shift'] ix = rfs*0.05 jx = ix - rad_desc * 0.4 kx = jx + (rad_asc - rad_desc)*0.3 self._radix = [(ix - rfs*0.050, iy), (ix + rfs*0.105, iy - rfs*0.02), #crest (ix + rfs*0.14, iy + rfs*0.08), (jx + rfs*0.135, y - rad_desc - rfs*0.35), # inner vertex (kx + rfs*0.05, y - rad_asc - rfs*0.04), (kx + rad['advance'] + rfs*0.35, y - rad_asc - rfs*0.04), # overbar (kx + rad['advance'] + rfs*0.345, y - rad_asc), (kx + rfs*0.09, y - rad_asc), (jx + rfs*0.15, y - rad_desc - rfs*0.24), # outer vertex (jx + rfs*0.08, y - rad_desc - rfs*0.24), # outer vertex (ix + rfs*0.06, iy + rfs*0.15), (ix + rfs*0.010, iy + rfs*0.04), # lip (ix - rfs*0.050, iy + rfs*0.03), ] rad['x'] = kx + rfs * 0.2 rad['y'] = y if self._index is None: NL = [rad] else: index = cast_mono_line(LINE, self._index.content, runinfo, F + self['cl_radicand']) index['x'] = jx - index['advance']*0.5 index['y'] = y - rfs * 0.6 NL = [index, rad] width = kx + rfs * 0.35 + rad['advance'] return NL, width, rad_asc + rfs*0.2, rad_desc, (self._paint, 0, 0, 0)
def _cast_inline(self, LINE, runinfo, F, FSTYLE): C = cast_mono_line(LINE, list(str(LINE['page'] + self['offset'])), runinfo, F) C['x'] = 0 C['y'] = FSTYLE['shift'] return [ C ], C['advance'], C['ascent'], C['descent'], None, (self._paint_annot, 0, 0)
def _cast_inline(self, LINE, runinfo, F, FSTYLE): C = cast_mono_line(LINE, self.char, runinfo, F + self['mathvariant']) C['x'] = 0 C['y'] = FSTYLE['shift'] self._cad = C['advance'] ink = cairo.ScaledFont(C['fstyle']['font'], cairo.Matrix(yy=C['fstyle']['fontsize'], xx=C['fstyle']['fontsize']), cairo.Matrix(), cairo.FontOptions()) self._rise = -ink.text_extents(self.char[-1])[1] self._icorrection = self._rise * self['correct'] * 0.17632698070846498 # tan(10°) charwidth = self._cad + self._icorrection return [C], charwidth, C['ascent'], C['descent'], None, (self._draw_annot, 0, 0)
def _construct(self, x2, y2, ox, oy, PARENTLINE, BSTYLE): totalvector, vector, perpendicular = self._calculate_vectors() # print variable variable = cast_mono_line(PARENTLINE, list(self['variable']), BSTYLE['__runinfo__'], self['cl_variable']) k = variable['fstyle']['fontsize']*0.3 # arrowheads if self['arrowhead']: hp1, (hx2, hy2) = self._hint_line(self._line) a_length = 8*self['line_width'] angle = atan2(totalvector[1], totalvector[0]) + pi aa = 0.275 a_distance = cos(aa)*a_length x3 = hx2 + 0.5*a_distance*vector[0] y3 = hy2 + 0.5*a_distance*vector[1] cx = x3 + a_distance*vector[0] cy = y3 + a_distance*vector[1] ax = cx + a_length * cos(angle - aa) ay = cy + a_length * sin(angle - aa) bx = cx + a_length * cos(angle + aa) by = cy + a_length * sin(angle + aa) self._arrow = (cx, cy), (ax, ay), (bx, by) variable_space = 2.5*k + 1.5*a_distance self._extended_line = hp1, (x3, y3) else: variable_space = 4*k self._extended_line = self._hint_line(self._line) variable.nail_to(ox + x2 + variable_space*vector[0], oy + y2 + variable_space*vector[1], k, round(vector[0])) # ticks if self['ticks']: spacing = (2 - 0.5*self.floating) *k ticks = chain(self._generate_ticks(self._positions_to_xy(self._major, * self._line[0], * totalvector ), -spacing*self.floating, spacing), self._generate_ticks(self._positions_to_xy(self._minor, * self._line[0], * totalvector ), -0.5*spacing*self.floating, 0.5*spacing)) self._ticks = list(map(self._hint_line, ticks)) else: spacing = 0 # numbers letterspacing = spacing + 2.5*k self.lettervector = letterspacing*perpendicular[0], letterspacing*perpendicular[1] mono = chain((variable,), self._generate_numbers(self._positions_to_xy(self._numbers, ox + self._line[0][0] + self.lettervector[0], oy + self._line[0][1] + self.lettervector[1], * totalvector), PARENTLINE, BSTYLE['__runinfo__'], k)) return mono, ((self._z_center, self.paint),), ()
def _layout_block(self, frames, BSTYLE, overlay): self._frames = frames monos = [] planes = [] paint_functions = [] paint_annot_functions = [] x1, width, y, c, pn, block_top_u, block_bottom_u = self._layout_axes( frames, overlay + self['cl_axes'], planes) u = self._keys.layout_keys(frames, overlay + self['cl_key'], planes, paint_functions, block_top_u, block_bottom_u, self['key_shift'], self['key_bottom']) PARENTLINE = { 'l': 0, 'c': c, 'page': pn, 'leading': BSTYLE['leading'], 'BLOCK': self } for E in chain(self._CS, self._DS): mono, paint, paint_annot = E.inflate(width, x1, y, PARENTLINE, BSTYLE) monos.extend(mono) paint_functions.extend((pn, (F, x1, y, Z)) for Z, F in paint) #paint_annot_functions.extend((pn, (F, x1, y)) for F in paint_annot) paint_functions.sort(key=lambda k: k[1][3]) if all(A.floating for A in self._CS): perp_x, perp_y = map(sum, zip(*(A.lettervector for A in self._CS))) mag = sqrt(perp_x**2 + perp_y**2) O_label = cast_mono_line(PARENTLINE, 'O', BSTYLE['__runinfo__'], self._CS[0]['cl_variable']) O_label.nail_to(x1 + self._origin[0] * width + perp_x, y + self._origin[1] * -self['height'] + perp_y, align=round(perp_x / mag)) monos.append(O_label) planes_set = set(planes) return u, monos, [], planes, paint_functions, [], lambda O: O in planes_set, self._keys.get_color( )
def layout_observer(self, BSTYLE, wheels, LINE): wheels = wheels.increment(BSTYLE['incr_place_value'], BSTYLE['incr_assign']) # print para flag flag = (-2, -BSTYLE['leading'], 0, 0, -1) # print counters if BSTYLE['show_count'] is not None: flagline = cast_mono_line(LINE, BSTYLE['show_count'](wheels), BSTYLE['__runinfo__']) flagline['x'] = LINE['start'] - flagline['advance'] - BSTYLE['leading']*BSTYLE['counter_space'] flagline['y'] = LINE['y'] else: flagline = OT_line(LINE) flagline._ANO.append((flag, LINE['fstyle'], 0)) self._OBSERVERLINES = [flagline] self.left_edge = LINE['start'] - BSTYLE['leading']*0.5 self._whole_location = -1, LINE, 0, LINE['fstyle'] self.wheels = wheels
def _yield_linespaces(self, frames, BSTYLE): leading = BSTYLE['leading'] indent_range = BSTYLE['indent_range'] D, SIGN, K = BSTYLE['indent'] indent = None l = 0 while True: u, x1, x2, y, c, pn = frames.fit(leading) # calculate indentation if l in indent_range: if indent is None: if K: length = cast_mono_line( { 'l': l, 'c': c, 'page': pn, 'BLOCK': self, 'leading': leading }, self.content[:K], BSTYLE['__runinfo__'], length_only=True) indent = D + length * SIGN else: indent = D x1 += indent if x1 > x2: x1, x2 = x2, x1 yield OT_line({ 'BLOCK': self, 'leading': leading, 'start': x1, 'width': x2 - x1, 'y': y, 'c': c, 'u': u, 'l': l, 'page': pn }) l += 1
def layout(self, frames, BSTYLE, wheels, overlay, preceeding, halt): if BSTYLE['margin_left'] or BSTYLE['margin_right']: frames = Margined(frames, BSTYLE['margin_left'], BSTYLE['margin_right']) frames.save_u() u, left, right, y, c, pn = frames.fit(BSTYLE['leading']) frames.restore_u() if halt and self._preceeding is preceeding: n = len(self._OBSERVERLINES) self.layout_observer(BSTYLE, wheels, self.line0) self.__lines[-n:] = self._OBSERVERLINES frames.start(self.u_bottom) return True, self.wheels else: self.line0 = cast_mono_line({'l': 0, 'c': c, 'page': pn, 'leading': BSTYLE['leading'], 'BLOCK': self}, '', BSTYLE['__runinfo__']) self.line0.update({'u': u, 'start': left, 'width': right - left, 'x': left, 'y': y}) self.layout_observer(BSTYLE, wheels, self.line0) self.u = u - BSTYLE['leading'] self._preceeding = preceeding return self._cast( frames, * self._layout_block(frames, BSTYLE, overlay) ), self.wheels
def _cast_inline(self, LINE, runinfo, F, FSTYLE): C = cast_mono_line(LINE, self.char, runinfo, F + self['mathvariant']) C['x'] = 0 C['y'] = FSTYLE['shift'] self._cad = C['advance'] ink = cairo.ScaledFont( C['fstyle']['font'], cairo.Matrix(yy=C['fstyle']['fontsize'], xx=C['fstyle']['fontsize']), cairo.Matrix(), cairo.FontOptions()) self._rise = -ink.text_extents(self.char[-1])[1] self._icorrection = self._rise * self[ 'correct'] * 0.17632698070846498 # tan(10°) charwidth = self._cad + self._icorrection return [ C ], charwidth, C['ascent'], C['descent'], None, (self._draw_annot, 0, 0)
def layout_observer(self, BSTYLE, wheels, LINE): wheels = wheels.increment(BSTYLE['incr_place_value'], BSTYLE['incr_assign']) # print para flag flag = (-2, -BSTYLE['leading'], 0, 0, -1) # print counters if BSTYLE['show_count'] is not None: flagline = cast_mono_line(LINE, BSTYLE['show_count'](wheels), BSTYLE['__runinfo__']) flagline['x'] = LINE['start'] - flagline[ 'advance'] - BSTYLE['leading'] * BSTYLE['counter_space'] flagline['y'] = LINE['y'] else: flagline = OT_line(LINE) flagline._ANO.append((flag, LINE['fstyle'], 0)) self._OBSERVERLINES = [flagline] self.left_edge = LINE['start'] - BSTYLE['leading'] * 0.5 self._whole_location = -1, LINE, 0, LINE['fstyle'] self.wheels = wheels
def layout(self, frames, BSTYLE, wheels, overlay, preceeding, halt): if BSTYLE['margin_left'] or BSTYLE['margin_right']: frames = Margined(frames, BSTYLE['margin_left'], BSTYLE['margin_right']) frames.save_u() u, left, right, y, c, pn = frames.fit(BSTYLE['leading']) frames.restore_u() if halt and self._preceeding is preceeding: n = len(self._OBSERVERLINES) self.layout_observer(BSTYLE, wheels, self.line0) self.__lines[-n:] = self._OBSERVERLINES frames.start(self.u_bottom) return True, self.wheels else: self.line0 = cast_mono_line( { 'l': 0, 'c': c, 'page': pn, 'leading': BSTYLE['leading'], 'BLOCK': self }, '', BSTYLE['__runinfo__']) self.line0.update({ 'u': u, 'start': left, 'width': right - left, 'x': left, 'y': y }) self.layout_observer(BSTYLE, wheels, self.line0) self.u = u - BSTYLE['leading'] self._preceeding = preceeding return self._cast(frames, *self._layout_block(frames, BSTYLE, overlay)), self.wheels
def _mono(self, LINE, runinfo, F): F_small = F + self['cl_bound'] symbol = cast_mono_line(LINE, self._symbol.content, runinfo, F + self['cl_symbol']) a = cast_mono_line(LINE, self._a.content, runinfo, F_small) b = cast_mono_line(LINE, self._b.content, runinfo, F_small) return symbol, a, b
def _construct(self, x2, y2, ox, oy, PARENTLINE, BSTYLE): totalvector, vector, perpendicular = self._calculate_vectors() # print variable variable = cast_mono_line(PARENTLINE, list(self['variable']), BSTYLE['__runinfo__'], self['cl_variable']) k = variable['fstyle']['fontsize'] * 0.3 # arrowheads if self['arrowhead']: hp1, (hx2, hy2) = self._hint_line(self._line) a_length = 8 * self['line_width'] angle = atan2(totalvector[1], totalvector[0]) + pi aa = 0.275 a_distance = cos(aa) * a_length x3 = hx2 + 0.5 * a_distance * vector[0] y3 = hy2 + 0.5 * a_distance * vector[1] cx = x3 + a_distance * vector[0] cy = y3 + a_distance * vector[1] ax = cx + a_length * cos(angle - aa) ay = cy + a_length * sin(angle - aa) bx = cx + a_length * cos(angle + aa) by = cy + a_length * sin(angle + aa) self._arrow = (cx, cy), (ax, ay), (bx, by) variable_space = 2.5 * k + 1.5 * a_distance self._extended_line = hp1, (x3, y3) else: variable_space = 4 * k self._extended_line = self._hint_line(self._line) variable.nail_to(ox + x2 + variable_space * vector[0], oy + y2 + variable_space * vector[1], k, round(vector[0])) # ticks if self['ticks']: spacing = (2 - 0.5 * self.floating) * k ticks = chain( self._generate_ticks( self._positions_to_xy(self._major, *self._line[0], *totalvector), -spacing * self.floating, spacing), self._generate_ticks( self._positions_to_xy(self._minor, *self._line[0], *totalvector), -0.5 * spacing * self.floating, 0.5 * spacing)) self._ticks = list(map(self._hint_line, ticks)) else: spacing = 0 # numbers letterspacing = spacing + 2.5 * k self.lettervector = letterspacing * perpendicular[ 0], letterspacing * perpendicular[1] mono = chain((variable, ), self._generate_numbers( self._positions_to_xy( self._numbers, ox + self._line[0][0] + self.lettervector[0], oy + self._line[0][1] + self.lettervector[1], *totalvector), PARENTLINE, BSTYLE['__runinfo__'], k)) return mono, ((self._z_center, self.paint), ), ()
def _cast_inline(self, LINE, runinfo, F, FSTYLE): C = cast_mono_line(LINE, list(str(LINE['page'] + self['offset'])), runinfo, F) C['x'] = 0 C['y'] = FSTYLE['shift'] return [C], C['advance'], C['ascent'], C['descent'], None, (self._paint_annot, 0, 0)