-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
System font infromation gathering #11
- Loading branch information
1 parent
9842670
commit 5e4115f
Showing
4 changed files
with
88 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" Typecase controller | ||
Rest API to handle system fonts and curated/user collections. | ||
Created by Lahiru Pathirage @ Mooniak <[email protected]> on 2/2/2017 | ||
""" | ||
|
||
from flask import Blueprint, jsonify | ||
|
||
from utility import FontManager | ||
|
||
typecase_blueprint = Blueprint("typecase_blueprint", __name__) | ||
|
||
|
||
@typecase_blueprint.route("/system/fonts") | ||
def get_active_fonts(): | ||
return jsonify(FontManager().get_active_fonts_list()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,6 @@ | |
Created by Lahiru Pathirage @ Mooniak<[email protected]> on 28/12/2016 | ||
""" | ||
|
||
from os import walk | ||
import os | ||
import platform | ||
|
||
if platform.system() in "Windows": | ||
from utility.win_fix import fixed_install_font | ||
|
||
from consumer import FontsConsumer | ||
from service import FontFaceService | ||
from service import FontFileService | ||
|
@@ -20,11 +13,18 @@ | |
from service import SystemService | ||
from utility import FileManager | ||
|
||
from os import walk | ||
import os | ||
import platform | ||
|
||
if platform.system() in "Windows": | ||
from utility.win_fix import fixed_install_font | ||
|
||
|
||
def find_files_by_extension(source_dir, extension): | ||
files_list = [] | ||
|
||
for root, dirs, files in walk(source_dir): | ||
for root, dirs, files in walk(os.path.expanduser(source_dir)): | ||
for file in files: | ||
if file.endswith(extension): | ||
files_list.append( | ||
|
@@ -37,33 +37,81 @@ def find_files_by_extension(source_dir, extension): | |
return files_list | ||
|
||
|
||
|
||
class FontManager: | ||
|
||
def __init__(self): | ||
self.__system = SystemService().find_system_info() | ||
|
||
def get_system_font_list(self): | ||
def get_active_fonts_list(self): | ||
active_fonts = [] | ||
font_data = [] | ||
fonts_dirs = [] | ||
|
||
if self.__system.platform in "Windows": | ||
return os.listdir(os.path.join(os.environ["WINDIR"], "Fonts")) | ||
fonts_dirs.append( | ||
os.path.join(os.environ["WINDIR"], "Fonts") | ||
) | ||
|
||
elif self.__system.platform in "Linux": | ||
fonts_dirs.append("/usr/share/fonts") | ||
|
||
try: | ||
fonts_dirs.append(os.path.join("~", ".fonts")) | ||
|
||
except: | ||
print("No user font directory") | ||
|
||
else: | ||
import fontconfig | ||
return fontconfig.query() | ||
fonts_dirs.append(os.path.join("~", "Library/Fonts")) | ||
fonts_dirs.append("/System/Library/Fonts") | ||
|
||
# list font files in font directories | ||
for dir in fonts_dirs: | ||
active_fonts += find_files_by_extension(dir, ".ttf") | ||
active_fonts += find_files_by_extension(dir, ".otf") | ||
|
||
for font in active_fonts: | ||
font_info = [] | ||
|
||
if "-" in font["name"]: | ||
font_info = font["name"].split(".")[0].split("-") | ||
|
||
else: | ||
font_info.append(font["name"].split(".")[0]) | ||
font_info.append("Regular") | ||
|
||
trigger = True | ||
|
||
for element in font_data: | ||
if font_info[0] in element["name"]: | ||
element["fontfaces"].append({ | ||
"fontface": font_info[1], | ||
"resource_path": font["file_path"] | ||
}) | ||
|
||
trigger = False | ||
break | ||
|
||
if trigger: | ||
font_data.append({ | ||
"name": font_info[0], | ||
"fontfaces": [{ | ||
"fontface": font_info[1], | ||
"resource_path": font["file_path"] | ||
}] | ||
}) | ||
|
||
return font_data | ||
|
||
def install_font(self, font_id, rel_id): | ||
font_dir = "./data/" + font_id | ||
sys_font_dir = self.__system.font_directory | ||
sys_fonts_list = self.get_system_font_list() | ||
artifacts_dir = "./data/" + font_id + "/extracted" | ||
|
||
FileManager().create_directory(artifacts_dir) | ||
|
||
requested_font = FontService().find_by_font_id(font_id).first() | ||
|
||
for font in sys_fonts_list: | ||
if requested_font.name in font: | ||
print("Warning! Font already exists") | ||
|
||
if rel_id in "devel": | ||
try: | ||
fontfaces = FontFaceService().find_by_font_id(font_id) | ||
|
@@ -146,16 +194,11 @@ def install_font(self, font_id, rel_id): | |
artifacts_dir, ".otf" | ||
) | ||
|
||
if fontfaces is []: | ||
fontfaces = find_files_by_extension( | ||
artifacts_dir, ".ufo" | ||
) | ||
|
||
for fontface in fontfaces: | ||
if "Windows" in self.__system.platform: | ||
fixed_install_font(fontface["file_path"]) | ||
|
||
else : | ||
else: | ||
FileManager().move_file( | ||
fontface["name"], | ||
sys_font_dir, | ||
|