def empline(self, tag, stream): def invalid(op): stream.seek(op) # self.simple_container(None, 'EmpLine') oldpos = stream.tell() try: t = Tag(stream) if t.id not in (0xF579, 0xF57A): raise LRFParseError except LRFParseError: invalid(oldpos) return h = TextAttr.tag_map[t.id] attrs = {} attrs[h[0]] = TextAttr.tag_to_val(h, None, t, None) oldpos = stream.tell() try: t = Tag(stream) if t.id not in (0xF579, 0xF57A): raise LRFParseError h = TextAttr.tag_map[t.id] attrs[h[0]] = TextAttr.tag_to_val(h, None, t, None) except LRFParseError: stream.seek(oldpos) if attrs: self.content.append(self.__class__.TextTag('EmpLine', attrs=attrs))
def initialize(self): self.attrs = {} stream = io.BytesIO(self.stream) tag = Tag(stream) if tag.id != 0xF503: raise LRFParseError("Bad block content") obj = self._document.objects[tag.dword] if isinstance(obj, SimpleText): self.name = 'SimpleTextBlock' self.textstyle_id = obj.style_id elif isinstance(obj, Text): self.name = 'TextBlock' self.textstyle_id = obj.style_id elif isinstance(obj, Image): self.name = 'ImageBlock' for attr in ('x0', 'x1', 'y0', 'y1', 'xsize', 'ysize', 'refstream'): self.attrs[attr] = getattr(obj, attr) self.refstream = self._document.objects[self.attrs['refstream']] elif isinstance(obj, Button): self.name = 'ButtonBlock' else: raise LRFParseError("Unexpected block type: " + obj.__class__.__name__) self.content = obj for attr in self.extra_attrs: if hasattr(self, attr): self.attrs[attr] = getattr(self, attr)
def __init__(self, document, stream, id, scramble_key, boundary): self._scramble_key = scramble_key self._document = document self.id = id while stream.tell() < boundary: tag = Tag(stream) self.handle_tag(tag, stream)
def get_object_by_id(self, tid): from calibre.ebooks.lrf.tags import Tag for id, offset, size in self._objects(): self._file.seek(offset) tag = Tag(self._file) if tag.id == 0xF500: obj_id, obj_type = struct.unpack("<IH", tag.contents) if obj_id == tid: return obj_id, offset, size, obj_type return (False, False, False, False)
def get_object(document, stream, id, offset, size, scramble_key): stream.seek(offset) start_tag = Tag(stream) if start_tag.id != 0xF500: raise LRFParseError('Bad object start') obj_id, obj_type = struct.unpack("<IH", start_tag.contents) if obj_type < len(object_map) and object_map[obj_type] is not None: return object_map[obj_type](document, stream, obj_id, scramble_key, offset+size-Tag.tags[0][0]) raise LRFParseError("Unknown object type: %02X!" % obj_type)
def get_objects_by_type(self, type): from calibre.ebooks.lrf.tags import Tag objects = [] for id, offset, size in self._objects(): self._file.seek(offset) tag = Tag(self._file) if tag.id == 0xF500: obj_id, obj_type = struct.unpack("<IH", tag.contents) if obj_type == type: objects.append((obj_id, offset, size)) return objects
def find_first_tag(start): pos = self.stream.find(b'\xf5', start) if pos == -1: return -1 try: stream.seek(pos - 1) _t = Tag(stream) if _t.id in text_tags: return pos - 1 return find_first_tag(pos + 1) except: return find_first_tag(pos + 1)
def initialize(self): self.attrs = {} for attr in self.extra_attrs: if hasattr(self, attr): self.attrs[attr] = getattr(self, attr) self._contents = [] stream = io.BytesIO(self.stream) while stream.tell() < len(self.stream): tag = Tag(stream) try: self._contents.append( PutObj(self._document.objects, *struct.unpack("<HHI", tag.contents))) except struct.error: print('Canvas object has errors, skipping.')
def initialize(self): self.content = collections.deque() stream = io.BytesIO(self.stream) length = len(self.stream) style = self.style.as_dict() current_style = style.copy() text_tags = set( list(TextAttr.tag_map.keys()) + list(Text.text_tags.keys()) + list(ruby_tags.keys())) text_tags -= {0xf500 + i for i in range(10)} text_tags.add(0xf5cc) while stream.tell() < length: # Is there some text before a tag? def find_first_tag(start): pos = self.stream.find(b'\xf5', start) if pos == -1: return -1 try: stream.seek(pos - 1) _t = Tag(stream) if _t.id in text_tags: return pos - 1 return find_first_tag(pos + 1) except: return find_first_tag(pos + 1) start_pos = stream.tell() tag_pos = find_first_tag(start_pos) if tag_pos >= start_pos: if tag_pos > start_pos: self.add_text(self.stream[start_pos:tag_pos]) stream.seek(tag_pos) else: # No tags in this stream self.add_text(self.stream) stream.seek(0, 2) break tag = Tag(stream) if tag.id == 0xF5CC: self.add_text(stream.read(tag.word)) elif tag.id in self.__class__.text_tags: # A Text tag action = self.__class__.text_tags[tag.id] if isinstance(action, unicode_type): getattr(self, action)(tag, stream) else: getattr(self, action[0])(tag, action[1]) elif tag.id in TextAttr.tag_map: # A Span attribute action = TextAttr.tag_map[tag.id] if len(self.content) == 0: current_style = style.copy() name, val = action[0], LRFObject.tag_to_val( action, self, tag, None) if name and (name not in current_style or current_style[name] != val): # No existing Span if len(self.content) > 0 and isinstance( self.content[-1], self.__class__.Span): self.content[-1].attrs[name] = val else: self.content.append( self.__class__.Span('Span', {name: val})) current_style[name] = val if len(self.content) > 0: self.close_containers() self.stream = None
def parse_stream(self, length): while self.in_container and self.stream.tell() < length: tag = Tag(self.stream) self.handle_tag(tag)
def parse_send_message(self, tag, f): params = (tag.word, Tag.string_parser(f), Tag.string_parser(f)) self.actions[self.button_type].append((2, params))
def parse_send_message(self, tag, f): params = (tag.word, Tag.string_parser(f), Tag.string_parser(f)) self.actions[self.button_type].append((2, params))