def test_width_height(images: List[Path]): # https://github.com/jgraph/drawio-desktop/issues/254 # Widths and heights are not exact assert get_image_size(images[0]) == (103, 53) assert get_image_size(images[1]) == (202, 102) assert get_image_size(images[2]) == (1007, 511)
def test_scale(images: List[Path]): # https://github.com/jgraph/drawio-desktop/issues/254 # Scale is not exact # actual image size is 124x63 assert get_image_size(images[0]) == (245, 124) assert get_image_size(images[1]) == (1217, 616) assert get_image_size(images[2]) == (610, 309)
def test_scale(images: List[Path]): # https://github.com/jgraph/drawio-desktop/issues/254 # Scale is not exact # actual image size is 124x63 assert get_image_size(images[0]) == (245, 124) assert get_image_size(images[1]) == (1217, 616) assert get_image_size(images[2]) == (64, 33) assert get_image_size(images[3]) == (124, 63) assert get_image_size(images[4]) == (610, 309) # deprecated drawio directive assert get_image_size(images[5]) == (245, 124) assert get_image_size(images[6]) == (1217, 616) assert get_image_size(images[7]) == (610, 309)
def visit_image(self, node: Element) -> None: olduri = node['uri'] # rewrite the URI if the environment knows about it if olduri in self.builder.images: node['uri'] = posixpath.join(self.builder.imgpath, self.builder.images[olduri]) if 'scale' in node: # Try to figure out image height and width. Docutils does that too, # but it tries the final file name, which does not necessarily exist # yet at the time the HTML file is written. if not ('width' in node and 'height' in node): size = get_image_size(os.path.join(self.builder.srcdir, olduri)) if size is None: logger.warning( __('Could not obtain image size. :scale: option is ignored.' ), # NOQA location=node) else: if 'width' not in node: node['width'] = str(size[0]) if 'height' not in node: node['height'] = str(size[1]) uri = node['uri'] if uri.lower().endswith(('svg', 'svgz')): atts = {'src': uri} if 'width' in node: atts['width'] = node['width'] if 'height' in node: atts['height'] = node['height'] if 'scale' in node: scale = node['scale'] / 100.0 if 'width' in atts: atts['width'] = int(atts['width']) * scale if 'height' in atts: atts['height'] = int(atts['height']) * scale atts['alt'] = node.get('alt', uri) if 'align' in node: self.body.append('<div align="%s" class="align-%s">' % (node['align'], node['align'])) self.context.append('</div>\n') else: self.context.append('') self.body.append(self.emptytag(node, 'img', '', **atts)) return super().visit_image(node)
def test_figure(content: Sphinx, directives: List[Tag]): filenames_sizes = [ ("box.png", (124, 63)), ("box1.png", (185, 94)), ] for img, (filename, size) in zip(directives, filenames_sizes): assert img.name == "img" assert img["src"] == "_images/" + filename assert img["alt"] == "_images/" + filename assert img["class"] == ["drawio"] image_path = content.outdir / img["src"] assert get_image_size(image_path) == size div = img.parent assert div.name == "div" assert "figure" in div["class"]
def visit_image(self, node): olduri = node['uri'] # rewrite the URI if the environment knows about it if olduri in self.builder.images: node['uri'] = posixpath.join(self.builder.imgpath, self.builder.images[olduri]) if node['uri'].lower().endswith('svg') or \ node['uri'].lower().endswith('svgz'): atts = {'src': node['uri']} if 'width' in node: atts['width'] = node['width'] if 'height' in node: atts['height'] = node['height'] if 'alt' in node: atts['alt'] = node['alt'] if 'align' in node: self.body.append('<div align="%s" class="align-%s">' % (node['align'], node['align'])) self.context.append('</div>\n') else: self.context.append('') self.body.append(self.emptytag(node, 'img', '', **atts)) return if 'scale' in node: # Try to figure out image height and width. Docutils does that too, # but it tries the final file name, which does not necessarily exist # yet at the time the HTML file is written. if not ('width' in node and 'height' in node): size = get_image_size(os.path.join(self.builder.srcdir, olduri)) if size is None: self.builder.env.warn_node( 'Could not obtain image size. ' ':scale: option is ignored.', node) else: if 'width' not in node: node['width'] = str(size[0]) if 'height' not in node: node['height'] = str(size[1]) BaseTranslator.visit_image(self, node)
def visit_image(self, node): olduri = node['uri'] # rewrite the URI if the environment knows about it if olduri in self.builder.images: node['uri'] = posixpath.join(self.builder.imgpath, self.builder.images[olduri]) if node['uri'].lower().endswith('svg') or \ node['uri'].lower().endswith('svgz'): atts = {'src': node['uri']} if 'width' in node: atts['width'] = node['width'] if 'height' in node: atts['height'] = node['height'] if 'alt' in node: atts['alt'] = node['alt'] if 'align' in node: self.body.append('<div align="%s" class="align-%s">' % (node['align'], node['align'])) self.context.append('</div>\n') else: self.context.append('') self.body.append(self.emptytag(node, 'img', '', **atts)) return if 'scale' in node: # Try to figure out image height and width. Docutils does that too, # but it tries the final file name, which does not necessarily exist # yet at the time the HTML file is written. if not ('width' in node and 'height' in node): size = get_image_size(os.path.join(self.builder.srcdir, olduri)) if size is None: self.builder.env.warn_node('Could not obtain image size. ' ':scale: option is ignored.', node) else: if 'width' not in node: node['width'] = str(size[0]) if 'height' not in node: node['height'] = str(size[1]) BaseTranslator.visit_image(self, node)
def test_page_index(images: List[Path]): assert images[0].name == "pages.png" assert images[1].name == "pages1.png" assert images[2].name == "pages2.png" assert images[3].name == "pages.png" assert get_image_size(images[0]) == (124, 63) assert get_image_size(images[1]) == (64, 63) assert get_image_size(images[2]) == (64, 63) assert get_image_size(images[3]) == (124, 63) # deprecated drawio directive assert images[ 4].name == "drawio-23596e9713a51f864e734695324de3c19e930125.png" assert images[ 5].name == "drawio-d890782f09bf6478d353265d5894253de5fefbd3.png" assert images[ 6].name == "drawio-4d4e2f8704b20c01096eed9b97cd6588d479f3a6.png" assert images[ 7].name == "drawio-23596e9713a51f864e734695324de3c19e930125.png" assert get_image_size(images[4]) == (124, 63) assert get_image_size(images[5]) == (64, 63) assert get_image_size(images[6]) == (64, 63) assert get_image_size(images[7]) == (124, 63)
def visit_image(self, node): olduri = node["uri"] # rewrite the URI if the environment knows about it if olduri in self.builder.images: node["uri"] = posixpath.join(self.builder.imgpath, self.builder.images[olduri]) uri = node["uri"] if uri.lower().endswith("svg") or uri.lower().endswith("svgz"): atts = {"src": uri} if "width" in node: atts["width"] = node["width"] if "height" in node: atts["height"] = node["height"] atts["alt"] = node.get("alt", uri) if "align" in node: self.body.append('<div align="%s" class="align-%s">' % (node["align"], node["align"])) self.context.append("</div>\n") else: self.context.append("") self.body.append(self.emptytag(node, "img", "", **atts)) return if "scale" in node: # Try to figure out image height and width. Docutils does that too, # but it tries the final file name, which does not necessarily exist # yet at the time the HTML file is written. if not ("width" in node and "height" in node): size = get_image_size(path.join(self.builder.srcdir, olduri)) if size is None: self.builder.env.warn_node("Could not obtain image size. " ":scale: option is ignored.", node) else: if "width" not in node: node["width"] = str(size[0]) if "height" not in node: node["height"] = str(size[1]) BaseTranslator.visit_image(self, node)
def visit_image(self, node): if 'uri' not in node or not node['uri']: self.verbose('skipping image with no uri') raise nodes.SkipNode uri = node['uri'] uri = self.encode(uri) dochost = None img_key = None img_sz = None internal_img = uri.find('://') == -1 and not uri.startswith('data:') is_svg = uri.startswith('data:image/svg+xml') or \ guess_mimetype(uri) == 'image/svg+xml' if internal_img: asset_docname = None if 'single' in self.builder.name: asset_docname = self.docname img_key, dochost, img_path = \ self.assets.fetch(node, docname=asset_docname) # if this image has not already be processed (injected at a later # stage in the sphinx process); try processing it now if not img_key: # if this is an svg image, additional processing may also needed if is_svg: confluence_supported_svg(self.builder, node) if not asset_docname: asset_docname = self.docname img_key, dochost, img_path = \ self.assets.process_image_node( node, asset_docname, standalone=True) if not img_key: self.warn('unable to find image: ' + uri) raise nodes.SkipNode # extract height, width and scale values on this image height, hu = extract_length(node.get('height')) scale = node.get('scale') width, wu = extract_length(node.get('width')) # if a scale value is provided and a height/width is not set, attempt to # determine the size of the image so that we can apply a scale value on # the detected size values if scale and not height and not width: if internal_img: img_sz = get_image_size(img_path) if img_sz is None: self.warn('could not obtain image size; :scale: option is ' 'ignored for ' + img_path) else: width = img_sz[0] wu = 'px' else: self.warn('cannot not obtain image size for external image; ' ':scale: option is ignored for ' + node['uri']) # apply scale factor to height/width fields if scale: if height: height = int(round(float(height) * scale / 100)) if width: width = int(round(float(width) * scale / 100)) # confluence only supports pixel sizes and percentage sizes in select # cases (e.g. applying a percentage width for an attached image can # result in an macro render error) -- adjust any other unit type (if # possible) to an acceptable pixel/percentage length if height: height = convert_length(height, hu) if height is None: self.warn('unsupported unit type for confluence: ' + hu) if width: width = convert_length(width, wu) if width is None: self.warn('unsupported unit type for confluence: ' + wu) # disable height/width entries for attached svgs as using these # attributes can result in a "broken image" rendering; instead, we will # track any desired height/width entry and inject them when publishing if internal_img and is_svg and (height or width): height = None hu = None width = None wu = None # [sphinx-gallery] create "thumbnail" images for sphinx-gallery # # If a sphinx-gallery-specific class type is detected for an image, # assume there is a desire for thumbnail-like images. Images are then # restricted with a specific height (a pattern observed when restricting # images to a smaller size with a Confluence editor). Although, if the # detected image size is smaller than our target, ignore any forced size # changes. if height is None and width is None and internal_img and not is_svg: if 'sphx-glr-multi-img' in node.get('class', []): if not img_sz: img_sz = get_image_size(img_path) if not img_sz or img_sz[1] > 250: height = '250' hu = 'px' # forward image options opts = {} opts['dochost'] = dochost opts['height'] = height opts['hu'] = hu opts['key'] = img_key opts['width'] = width opts['wu'] = wu self._visit_image(node, opts)
def test_get_image_size(testroot): assert get_image_size(testroot / GIF_FILENAME) == (200, 181) assert get_image_size(testroot / PNG_FILENAME) == (200, 181) assert get_image_size(testroot / PDF_FILENAME) is None assert get_image_size(testroot / TXT_FILENAME) is None
def test_get_image_size(testroot): assert get_image_size(testroot / GIF_FILENAME) == (200, 181) assert get_image_size(testroot / PNG_FILENAME) == (200, 181) assert get_image_size(testroot / PDF_FILENAME) is None assert get_image_size(testroot / TXT_FILENAME) is None
def test_get_image_size(): assert get_image_size(GIF_FILENAME) == (200, 181) assert get_image_size(PNG_FILENAME) == (200, 181) assert get_image_size(PDF_FILENAME) is None assert get_image_size(TXT_FILENAME) is None
def test_get_image_size(rootdir): assert get_image_size(rootdir / 'test-root' / GIF_FILENAME) == (200, 181) assert get_image_size(rootdir / 'test-root' / PNG_FILENAME) == (200, 181) assert get_image_size(rootdir / 'test-root' / PDF_FILENAME) is None assert get_image_size(rootdir / 'test-root' / TXT_FILENAME) is None