Added image dimensions

This commit is contained in:
Liam Cain
2012-04-28 13:47:07 -04:00
parent 796e0698d2
commit 4c233830b8
2 changed files with 90 additions and 12 deletions

63
afn_img_utils.py Normal file
View File

@@ -0,0 +1,63 @@
#
# These methods were taken from
# Zencoding utils (I don't own them)
#
def char_at(text, pos):
"""
Returns character at specified index of text.
If index if out of range, returns empty string
"""
return text[pos] if pos < len(text) else ''
def get_image_size(stream):
"""
Gets image size from image byte stream.
@author http://romeda.org/rePublish/
@param stream: Image byte stream (use <code>zen_file.read()</code>)
@type stream: str
@return: dict with <code>width</code> and <code>height</code> properties
"""
png_magic_num = "\211PNG\r\n\032\n"
jpg_magic_num = "\377\330"
gif_magic_num = "GIF8"
pos = [0]
def next_byte():
char = char_at(stream, pos[0])
pos[0] += 1
return ord(char)
if stream.startswith(png_magic_num):
# PNG. Easy peasy.
pos[0] = stream.find('IHDR') + 4
return {
'width': (next_byte() << 24) | (next_byte() << 16) | (next_byte() << 8) | next_byte(),
'height': (next_byte() << 24) | (next_byte() << 16) | (next_byte() << 8) | next_byte()
}
elif stream.startswith(gif_magic_num):
pos[0] = 6
return {
'width': next_byte() | (next_byte() << 8),
'height': next_byte() | (next_byte() << 8)
}
elif stream.startswith(jpg_magic_num):
hex_list = ["%02X" % ord(ch) for ch in stream]
for k in range(len(hex_list) - 1):
if hex_list[k] == 'FF' and (hex_list[k + 1] == 'C0' or hex_list[k + 1] == 'C2'):
#print k, hex(k) # test
return {
'height': int(hex_list[k + 5], 16) * 256 + int(hex_list[k + 6], 16),
'width': int(hex_list[k + 7], 16) * 256 + int(hex_list[k + 8], 16)
}
else:
return {
'width': -1,
'height': -1
}

View File

@@ -2,16 +2,29 @@ import sublime
import sublime_plugin
import os
import glob
from afn_img_utils import get_image_size
class AfnCommitCompCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
sel = view.sel()[0].a
if not 'string' in view.scope_name(sel): return
scope_end = view.extract_scope(sel-1).b - 1
region = sublime.Region(sel, scope_end)
scope_end = view.extract_scope(sel-1).b
region = sublime.Region(sel, scope_end-1)
view.erase(edit, region)
path = view.substr(view.extract_scope(sel-1))
if path.startswith(("'","\"","(")):
path = path[1:-1]
if 'img' in view.substr(view.line(sel)) and path.endswith(('.png','.jpg','.jpeg','.gif')):
with open(path,'r') as r:
read_data = r.read()
dim = get_image_size(read_data)
string = ' width='+str(dim.get('width'))+ ' height='+str(dim.get('height'))
view.insert(edit, scope_end, string)
class FileNameComplete(sublime_plugin.EventListener):
committing_filename = False
@@ -26,10 +39,16 @@ class FileNameComplete(sublime_plugin.EventListener):
return True
return False
def fix_dir(self, path):
if not '.' in path:
return path + '/'
return path
def fix_dir(self,dir,fn):
if not '.' in fn[1:]:
return fn + '/'
elif fn.endswith(('.png','.jpg','.jpeg','.gif')):
path = os.path.join(dir + '/', fn)
with open(path,'r') as r:
read_data = r.read()
dim = get_image_size(read_data)
return fn + '\t' + 'w:'+str(dim.get('width'))+" h:"+str(dim.get('height'))
return fn
def on_query_completions(self, view, prefix, locations):
completions = []
@@ -52,10 +71,7 @@ class FileNameComplete(sublime_plugin.EventListener):
if view.extract_scope(sel-1).b - sel != 1:
wild_pos = sel - view.extract_scope(sel-1).a - 1
cur_path = cur_path[:wild_pos] + '*' + cur_path[wild_pos:]
if '\\' in view.substr(sel):
cur_path += '*'
cur_path = cur_path[:wild_pos] + '*' + cur_path[wild_pos:] + '*'
if is_proj_rel and os.path.isabs(cur_path):
this_dir = sublime.load_settings(PACKAGE_SETTINGS).get("afn_proj_root")
@@ -64,7 +80,6 @@ class FileNameComplete(sublime_plugin.EventListener):
if f in view.file_name():
this_dir = f
cur_path = cur_path[1:]
this_dir = os.path.join(this_dir + '/', cur_path)
try:
@@ -77,7 +92,7 @@ class FileNameComplete(sublime_plugin.EventListener):
for d in list(set(dir_files)):
n = d.decode('utf-8')
completions.append((self.fix_dir(n), n))
completions.append((self.fix_dir(this_dir,n), n))
if completions:
self.committing_filename = True
return completions