Merge pull request #243 from jp9000/font-test-mac
Merge freetype text plugin
This commit is contained in:
commit
a0897b0adf
56
cmake/Modules/FindFreetype.cmake
Normal file
56
cmake/Modules/FindFreetype.cmake
Normal file
@ -0,0 +1,56 @@
|
||||
# Once done these will be defined:
|
||||
#
|
||||
# LIBFREETYPE_FOUND
|
||||
# LIBFREETYPE_INCLUDE_DIRS
|
||||
# LIBFREETYPE_LIBRARIES
|
||||
#
|
||||
# For use in OBS:
|
||||
#
|
||||
# FREETYPE_INCLUDE_DIR
|
||||
#
|
||||
|
||||
if(LIBFREETYPE_INCLUDE_DIRS AND LIBFREETYPE_LIBRARIES)
|
||||
set(LIBFREETYPE_FOUND TRUE)
|
||||
else()
|
||||
find_package(PkgConfig QUIET)
|
||||
if (PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(_FREETYPE QUIET freetype2)
|
||||
endif()
|
||||
|
||||
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
set(_lib_suffix 64)
|
||||
else()
|
||||
set(_lib_suffix 32)
|
||||
endif()
|
||||
|
||||
set(FREETYPE_PATH_ARCH FreetypePath${_lib_suffix})
|
||||
|
||||
find_path(FREETYPE_INCLUDE_DIR
|
||||
NAMES ft2build.h
|
||||
HINTS
|
||||
${_FREETYPE_INCLUDE_DIRS}
|
||||
"${CMAKE_SOURCE_DIR}/additional_install_files/include"
|
||||
"$ENV{obsAdditionalInstallFiles}/include"
|
||||
ENV FreetypePath
|
||||
ENV ${FREETYPE_PATH_ARCH}
|
||||
PATHS
|
||||
/usr/include /usr/local/include /opt/local/include /sw/include)
|
||||
|
||||
find_library(FREETYPE_LIB
|
||||
NAMES ${_FREETYPE_LIBRARIES} freetype libfreetype
|
||||
HINTS
|
||||
${_FREETYPE_LIBRARY_DIRS}
|
||||
"${FREETYPE_INCLUDE_DIR}/../lib"
|
||||
"${FREETYPE_INCLUDE_DIR}/../lib${_lib_suffix}"
|
||||
"${FREETYPE_INCLUDE_DIR}/../libs${_lib_suffix}"
|
||||
"${FREETYPE_INCLUDE_DIR}/lib"
|
||||
"${FREETYPE_INCLUDE_DIR}/lib${_lib_suffix}"
|
||||
PATHS
|
||||
/usr/lib /usr/local/lib /opt/local/lib /sw/lib)
|
||||
|
||||
set(LIBFREETYPE_INCLUDE_DIRS ${FREETYPE_INCLUDE_DIR} CACHE PATH "freetype include dir")
|
||||
set(LIBFREETYPE_LIBRARIES ${FREETYPE_LIB} CACHE STRING "freetype libraries")
|
||||
|
||||
find_package_handle_standard_args(Libfreetype DEFAULT_MSG FREETYPE_LIB FREETYPE_INCLUDE_DIR)
|
||||
mark_as_advanced(FREETYPE_INCLUDE_DIR FREETYPE_LIB)
|
||||
endif()
|
56
cmake/Modules/Findiconv.cmake
Normal file
56
cmake/Modules/Findiconv.cmake
Normal file
@ -0,0 +1,56 @@
|
||||
# Once done these will be defined:
|
||||
#
|
||||
# LIBICONV_FOUND
|
||||
# LIBICONV_INCLUDE_DIRS
|
||||
# LIBICONV_LIBRARIES
|
||||
#
|
||||
# For use in OBS:
|
||||
#
|
||||
# ICONV_INCLUDE_DIR
|
||||
#
|
||||
|
||||
if(LIBICONV_INCLUDE_DIRS AND LIBICONV_LIBRARIES)
|
||||
set(LIBICONV_FOUND TRUE)
|
||||
else()
|
||||
find_package(PkgConfig QUIET)
|
||||
if (PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(_ICONV QUIET iconv)
|
||||
endif()
|
||||
|
||||
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
set(_lib_suffix 64)
|
||||
else()
|
||||
set(_lib_suffix 32)
|
||||
endif()
|
||||
|
||||
set(ICONV_PATH_ARCH IConvPath${_lib_suffix})
|
||||
|
||||
find_path(ICONV_INCLUDE_DIR
|
||||
NAMES iconv.h
|
||||
HINTS
|
||||
${_ICONV_INCLUDE_DIRS}
|
||||
"${CMAKE_SOURCE_DIR}/additional_install_files/include"
|
||||
"$ENV{obsAdditionalInstallFiles}/include"
|
||||
ENV IConvPath
|
||||
ENV ${ICONV_PATH_ARCH}
|
||||
PATHS
|
||||
/usr/include /usr/local/include /opt/local/include /sw/include)
|
||||
|
||||
find_library(ICONV_LIB
|
||||
NAMES ${_ICONV_LIBRARIES} iconv libiconv
|
||||
HINTS
|
||||
${_ICONV_LIBRARY_DIRS}
|
||||
"${ICONV_INCLUDE_DIR}/../lib"
|
||||
"${ICONV_INCLUDE_DIR}/../lib${_lib_suffix}"
|
||||
"${ICONV_INCLUDE_DIR}/../libs${_lib_suffix}"
|
||||
"${ICONV_INCLUDE_DIR}/lib"
|
||||
"${ICONV_INCLUDE_DIR}/lib${_lib_suffix}"
|
||||
PATHS
|
||||
/usr/lib /usr/local/lib /opt/local/lib /sw/lib)
|
||||
|
||||
set(LIBICONV_INCLUDE_DIRS ${ICONV_INCLUDE_DIR} CACHE PATH "iconv include dir")
|
||||
set(LIBICONV_LIBRARIES ${ICONV_LIB} CACHE STRING "iconv libraries")
|
||||
|
||||
find_package_handle_standard_args(Libiconv DEFAULT_MSG ICONV_LIB ICONV_INCLUDE_DIR)
|
||||
mark_as_advanced(ICONV_INCLUDE_DIR ICONV_LIB)
|
||||
endif()
|
@ -227,6 +227,7 @@ static inline size_t get_property_size(enum obs_property_type type)
|
||||
case OBS_PROPERTY_LIST: return sizeof(struct list_data);
|
||||
case OBS_PROPERTY_COLOR: return 0;
|
||||
case OBS_PROPERTY_BUTTON: return sizeof(struct button_data);
|
||||
case OBS_PROPERTY_FONT: return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -386,6 +387,13 @@ obs_property_t obs_properties_add_button(obs_properties_t props,
|
||||
return p;
|
||||
}
|
||||
|
||||
obs_property_t obs_properties_add_font(obs_properties_t props,
|
||||
const char *name, const char *desc)
|
||||
{
|
||||
if (!props || has_prop(props, name)) return NULL;
|
||||
return new_prop(props, name, desc, OBS_PROPERTY_FONT);
|
||||
}
|
||||
|
||||
static inline bool is_combo(struct obs_property *p)
|
||||
{
|
||||
return p->type == OBS_PROPERTY_LIST;
|
||||
|
@ -34,6 +34,7 @@ enum obs_property_type {
|
||||
OBS_PROPERTY_LIST,
|
||||
OBS_PROPERTY_COLOR,
|
||||
OBS_PROPERTY_BUTTON,
|
||||
OBS_PROPERTY_FONT,
|
||||
};
|
||||
|
||||
enum obs_combo_format {
|
||||
@ -60,6 +61,11 @@ enum obs_text_type {
|
||||
OBS_TEXT_MULTILINE,
|
||||
};
|
||||
|
||||
#define OBS_FONT_BOLD (1<<0)
|
||||
#define OBS_FONT_ITALIC (1<<1)
|
||||
#define OBS_FONT_UNDERLINE (1<<2)
|
||||
#define OBS_FONT_STRIKEOUT (1<<3)
|
||||
|
||||
struct obs_properties;
|
||||
struct obs_property;
|
||||
typedef struct obs_properties *obs_properties_t;
|
||||
@ -142,6 +148,18 @@ EXPORT obs_property_t obs_properties_add_button(obs_properties_t props,
|
||||
const char *name, const char *text,
|
||||
obs_property_clicked_t callback);
|
||||
|
||||
/**
|
||||
* Adds a font selection property.
|
||||
*
|
||||
* A font is an obs_data sub-object which contains the following items:
|
||||
* face: face name string
|
||||
* style: style name string
|
||||
* size: size integer
|
||||
* flags: font flags integer (OBS_FONT_* defined above)
|
||||
*/
|
||||
EXPORT obs_property_t obs_properties_add_font(obs_properties_t props,
|
||||
const char *name, const char *description);
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
|
@ -93,6 +93,7 @@ Basic.SourceSelect.AddExisting="Add Existing"
|
||||
Basic.PropertiesWindow="Properties for '%1'"
|
||||
Basic.PropertiesWindow.AutoSelectFormat="%1 (unsupported; autoselect: %2)"
|
||||
Basic.PropertiesWindow.SelectColor="Select color"
|
||||
Basic.PropertiesWindow.SelectFont="Select font"
|
||||
|
||||
# status bar
|
||||
Basic.StatusBar.Reconnecting="Disconnected, reconnecting (attempt %1)"
|
||||
|
@ -1,6 +1,8 @@
|
||||
#include <QFormLayout>
|
||||
#include <QLabel>
|
||||
#include <QCheckBox>
|
||||
#include <QFont>
|
||||
#include <QFontDialog>
|
||||
#include <QLineEdit>
|
||||
#include <QSpinBox>
|
||||
#include <QDoubleSpinBox>
|
||||
@ -353,6 +355,64 @@ void OBSPropertiesView::AddColor(obs_property_t prop, QFormLayout *layout,
|
||||
layout->addRow(label, subLayout);
|
||||
}
|
||||
|
||||
static void MakeQFont(obs_data_t font_obj, QFont &font)
|
||||
{
|
||||
const char *face = obs_data_get_string(font_obj, "face");
|
||||
const char *style = obs_data_get_string(font_obj, "style");
|
||||
int size = (int)obs_data_get_int(font_obj, "size");
|
||||
uint32_t flags = (uint32_t)obs_data_get_int(font_obj, "flags");
|
||||
|
||||
if (face) {
|
||||
font.setFamily(face);
|
||||
font.setStyleName(style);
|
||||
}
|
||||
|
||||
if (size)
|
||||
font.setPointSize(size);
|
||||
|
||||
if (flags & OBS_FONT_BOLD) font.setBold(true);
|
||||
if (flags & OBS_FONT_ITALIC) font.setItalic(true);
|
||||
if (flags & OBS_FONT_UNDERLINE) font.setUnderline(true);
|
||||
if (flags & OBS_FONT_STRIKEOUT) font.setStrikeOut(true);
|
||||
}
|
||||
|
||||
void OBSPropertiesView::AddFont(obs_property_t prop, QFormLayout *layout,
|
||||
QLabel *&label)
|
||||
{
|
||||
const char *name = obs_property_name(prop);
|
||||
obs_data_t font_obj = obs_data_get_obj(settings, name);
|
||||
const char *face = obs_data_get_string(font_obj, "face");
|
||||
const char *style = obs_data_get_string(font_obj, "style");
|
||||
QPushButton *button = new QPushButton;
|
||||
QLabel *fontLabel = new QLabel;
|
||||
QFont font;
|
||||
|
||||
font = fontLabel->font();
|
||||
MakeQFont(font_obj, font);
|
||||
|
||||
button->setText(QTStr("Basic.PropertiesWindow.SelectFont"));
|
||||
|
||||
fontLabel->setFrameStyle(QFrame::Sunken | QFrame::Panel);
|
||||
fontLabel->setFont(font);
|
||||
fontLabel->setText(QString("%1 %2").arg(face, style));
|
||||
fontLabel->setAlignment(Qt::AlignCenter);
|
||||
|
||||
QHBoxLayout *subLayout = new QHBoxLayout;
|
||||
subLayout->setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
subLayout->addWidget(fontLabel);
|
||||
subLayout->addWidget(button);
|
||||
|
||||
WidgetInfo *info = new WidgetInfo(this, prop, fontLabel);
|
||||
connect(button, SIGNAL(clicked()), info, SLOT(ControlChanged()));
|
||||
children.emplace_back(info);
|
||||
|
||||
label = new QLabel(QT_UTF8(obs_property_description(prop)));
|
||||
layout->addRow(label, subLayout);
|
||||
|
||||
obs_data_release(font_obj);
|
||||
}
|
||||
|
||||
void OBSPropertiesView::AddProperty(obs_property_t property,
|
||||
QFormLayout *layout)
|
||||
{
|
||||
@ -390,6 +450,9 @@ void OBSPropertiesView::AddProperty(obs_property_t property,
|
||||
case OBS_PROPERTY_COLOR:
|
||||
AddColor(property, layout, label);
|
||||
break;
|
||||
case OBS_PROPERTY_FONT:
|
||||
AddFont(property, layout, label);
|
||||
break;
|
||||
case OBS_PROPERTY_BUTTON:
|
||||
widget = AddButton(property);
|
||||
break;
|
||||
@ -548,6 +611,47 @@ bool WidgetInfo::ColorChanged(const char *setting)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WidgetInfo::FontChanged(const char *setting)
|
||||
{
|
||||
obs_data_t font_obj = obs_data_get_obj(view->settings, setting);
|
||||
bool success;
|
||||
uint32_t flags;
|
||||
QFont font;
|
||||
|
||||
if (!font_obj) {
|
||||
font = QFontDialog::getFont(&success, view);
|
||||
} else {
|
||||
MakeQFont(font_obj, font);
|
||||
font = QFontDialog::getFont(&success, font, view);
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
obs_data_release(font_obj);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!font_obj) {
|
||||
font_obj = obs_data_create();
|
||||
obs_data_set_obj(view->settings, setting, font_obj);
|
||||
}
|
||||
|
||||
obs_data_set_string(font_obj, "face", QT_TO_UTF8(font.family()));
|
||||
obs_data_set_string(font_obj, "style", QT_TO_UTF8(font.styleName()));
|
||||
obs_data_set_int(font_obj, "size", font.pointSize());
|
||||
flags = font.bold() ? OBS_FONT_BOLD : 0;
|
||||
flags |= font.italic() ? OBS_FONT_ITALIC : 0;
|
||||
flags |= font.underline() ? OBS_FONT_UNDERLINE : 0;
|
||||
flags |= font.strikeOut() ? OBS_FONT_STRIKEOUT : 0;
|
||||
obs_data_set_int(font_obj, "flags", flags);
|
||||
|
||||
QLabel *label = static_cast<QLabel*>(widget);
|
||||
label->setFont(font);
|
||||
label->setText(QString("%1 %2").arg(font.family(), font.styleName()));
|
||||
|
||||
obs_data_release(font_obj);
|
||||
return true;
|
||||
}
|
||||
|
||||
void WidgetInfo::ButtonClicked()
|
||||
{
|
||||
obs_property_button_clicked(property, view->obj);
|
||||
@ -570,6 +674,10 @@ void WidgetInfo::ControlChanged()
|
||||
if (!ColorChanged(setting))
|
||||
return;
|
||||
break;
|
||||
case OBS_PROPERTY_FONT:
|
||||
if (!FontChanged(setting))
|
||||
return;
|
||||
break;
|
||||
case OBS_PROPERTY_PATH:
|
||||
if (!PathChanged(setting))
|
||||
return;
|
||||
|
@ -28,6 +28,7 @@ private:
|
||||
bool PathChanged(const char *setting);
|
||||
void ListChanged(const char *setting);
|
||||
bool ColorChanged(const char *setting);
|
||||
bool FontChanged(const char *setting);
|
||||
void ButtonClicked();
|
||||
|
||||
public:
|
||||
@ -69,6 +70,7 @@ private:
|
||||
QWidget *AddList(obs_property_t prop, bool &warning);
|
||||
QWidget *AddButton(obs_property_t prop);
|
||||
void AddColor(obs_property_t prop, QFormLayout *layout, QLabel *&label);
|
||||
void AddFont(obs_property_t prop, QFormLayout *layout, QLabel *&label);
|
||||
|
||||
void AddProperty(obs_property_t property, QFormLayout *layout);
|
||||
|
||||
|
@ -19,3 +19,4 @@ add_subdirectory(obs-libfdk)
|
||||
add_subdirectory(obs-ffmpeg)
|
||||
add_subdirectory(obs-outputs)
|
||||
add_subdirectory(rtmp-services)
|
||||
add_subdirectory(text-freetype2)
|
||||
|
72
plugins/text-freetype2/CMakeLists.txt
Normal file
72
plugins/text-freetype2/CMakeLists.txt
Normal file
@ -0,0 +1,72 @@
|
||||
project(text-freetype2)
|
||||
|
||||
find_package(Freetype QUIET)
|
||||
if(NOT LIBFREETYPE_FOUND)
|
||||
message(STATUS "Freetype library not found, Freetype text plugin disabled")
|
||||
return()
|
||||
endif()
|
||||
|
||||
if(APPLE)
|
||||
find_package(iconv QUIET)
|
||||
if(NOT LIBICONV_FOUND)
|
||||
message(STATUS "IConv library not found, Freetype text plugin disabled")
|
||||
return()
|
||||
endif()
|
||||
|
||||
find_library(COCOA Cocoa)
|
||||
|
||||
set(text-freetype2_PLATFORM_SOURCES
|
||||
find-font-cocoa.m
|
||||
find-font-iconv.c)
|
||||
|
||||
include_directories(${COCOA}
|
||||
${LIBICONV_INCLUDE_DIRS})
|
||||
|
||||
set(text-freetype2_PLATFORM_DEPS
|
||||
${COCOA}
|
||||
${LIBICONV_LIBRARIES})
|
||||
|
||||
set_source_files_properties(find-font-cocoa.m
|
||||
PROPERTIES LANGUAGE C)
|
||||
elseif(WIN32)
|
||||
set(text-freetype2_PLATFORM_SOURCES
|
||||
find-font-windows.c)
|
||||
else()
|
||||
message(STATUS "Linux-specific code has yet to be written for the text plugin, just needs load_os_font_list written.. which, er, may or may not be a pain. (My apologies in advance, please don't strangle me)")
|
||||
return()
|
||||
|
||||
find_package(ICONV QUIET)
|
||||
if(NOT LIBICONV_FOUND)
|
||||
message(STATUS "IConv library not found, Freetype text plugin disabled")
|
||||
return()
|
||||
endif()
|
||||
|
||||
set(text-freetype2_PLATFORM_SOURCES
|
||||
find-font-iconv.c)
|
||||
|
||||
include_directories(${LIBICONV_INCLUDE_DIR})
|
||||
target_link_libraries(${LIBICONV_LIBRARIES})
|
||||
endif()
|
||||
|
||||
include_directories(${LIBFREETYPE_INCLUDE_DIRS})
|
||||
add_definitions(${LIBFREETYPE_DEFINITIONS})
|
||||
|
||||
set(text-freetype2_SOURCES
|
||||
find-font.h
|
||||
find-font.c
|
||||
obs-convenience.c
|
||||
text-functionality.c
|
||||
text-freetype2.c
|
||||
obs-convenience.h
|
||||
text-freetype2.h)
|
||||
|
||||
add_library(text-freetype2 MODULE
|
||||
${text-freetype2_PLATFORM_SOURCES}
|
||||
${text-freetype2_SOURCES})
|
||||
target_link_libraries(text-freetype2
|
||||
libobs
|
||||
${text-freetype2_PLATFORM_DEPS}
|
||||
${LIBFREETYPE_LIBRARIES})
|
||||
|
||||
install_obs_plugin(text-freetype2)
|
||||
install_obs_plugin_data(text-freetype2 data)
|
11
plugins/text-freetype2/data/locale/en-US.ini
Normal file
11
plugins/text-freetype2/data/locale/en-US.ini
Normal file
@ -0,0 +1,11 @@
|
||||
Font="Font"
|
||||
Text="Text"
|
||||
TextFile="Text File (UTF-8 or UTF-16)"
|
||||
ChatLogMode="Chat log mode (last 6 lines)"
|
||||
Color1="Color 1"
|
||||
Color2="Color 2"
|
||||
Outline="Outline"
|
||||
DropShadow="Drop Shadow"
|
||||
ReadFromFile="Read from file"
|
||||
CustomWidth="Custom text width"
|
||||
WordWrap="Word Wrap"
|
56
plugins/text-freetype2/data/text_default.effect
Normal file
56
plugins/text-freetype2/data/text_default.effect
Normal file
@ -0,0 +1,56 @@
|
||||
uniform float4x4 ViewProj;
|
||||
uniform float4x4 color_matrix;
|
||||
uniform float3 color_range_min = {0.0, 0.0, 0.0};
|
||||
uniform float3 color_range_max = {1.0, 1.0, 1.0};
|
||||
uniform texture2d image;
|
||||
|
||||
sampler_state def_sampler {
|
||||
Filter = Linear;
|
||||
AddressU = Clamp;
|
||||
AddressV = Clamp;
|
||||
};
|
||||
|
||||
struct VertInOut {
|
||||
float4 pos : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
float4 col : COLOR;
|
||||
};
|
||||
|
||||
VertInOut VSDefault(VertInOut vert_in)
|
||||
{
|
||||
VertInOut vert_out;
|
||||
vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj);
|
||||
vert_out.uv = vert_in.uv;
|
||||
vert_out.col = vert_in.col;
|
||||
return vert_out;
|
||||
}
|
||||
|
||||
float4 PSDrawBare(VertInOut vert_in) : TARGET
|
||||
{
|
||||
return image.Sample(def_sampler, vert_in.uv) * vert_in.col;
|
||||
}
|
||||
|
||||
float4 PSDrawMatrix(VertInOut vert_in) : TARGET
|
||||
{
|
||||
float4 yuv = image.Sample(def_sampler, vert_in.uv);
|
||||
yuv.xyz = clamp(yuv.xyz, color_range_min, color_range_max);
|
||||
return saturate(mul(float4(yuv.xyz, 1.0), color_matrix));
|
||||
}
|
||||
|
||||
technique Draw
|
||||
{
|
||||
pass
|
||||
{
|
||||
vertex_shader = VSDefault(vert_in);
|
||||
pixel_shader = PSDrawBare(vert_in);
|
||||
}
|
||||
}
|
||||
|
||||
technique DrawMatrix
|
||||
{
|
||||
pass
|
||||
{
|
||||
vertex_shader = VSDefault(vert_in);
|
||||
pixel_shader = PSDrawMatrix(vert_in);
|
||||
}
|
||||
}
|
58
plugins/text-freetype2/find-font-cocoa.m
Normal file
58
plugins/text-freetype2/find-font-cocoa.m
Normal file
@ -0,0 +1,58 @@
|
||||
#include <util/darray.h>
|
||||
#include "find-font.h"
|
||||
#include "text-freetype2.h"
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
static inline void add_path_font(const char *path)
|
||||
{
|
||||
FT_Face face;
|
||||
FT_Long idx = 0;
|
||||
FT_Long max_faces = 1;
|
||||
|
||||
while (idx < max_faces) {
|
||||
if (FT_New_Face(ft2_lib, path, idx, &face) != 0)
|
||||
break;
|
||||
|
||||
build_font_path_info(face, idx++, path);
|
||||
max_faces = face->num_faces;
|
||||
FT_Done_Face(face);
|
||||
}
|
||||
}
|
||||
|
||||
static void add_path_fonts(NSFileManager *file_manager, NSString *path)
|
||||
{
|
||||
NSArray *files = NULL;
|
||||
NSError *error = NULL;
|
||||
|
||||
files = [file_manager contentsOfDirectoryAtPath:path error:&error];
|
||||
|
||||
for (NSString *file in files) {
|
||||
NSString *full_path = [path stringByAppendingPathComponent:file];
|
||||
|
||||
add_path_font(full_path.fileSystemRepresentation);
|
||||
}
|
||||
}
|
||||
|
||||
void load_os_font_list(void)
|
||||
{
|
||||
@autoreleasepool {
|
||||
BOOL is_dir;
|
||||
NSArray *paths = NSSearchPathForDirectoriesInDomains(
|
||||
NSLibraryDirectory, NSAllDomainsMask, true);
|
||||
|
||||
for (NSString *path in paths) {
|
||||
NSFileManager *file_manager =
|
||||
[NSFileManager defaultManager];
|
||||
NSString *font_path =
|
||||
[path stringByAppendingPathComponent:@"Fonts"];
|
||||
|
||||
bool folder_exists = [file_manager
|
||||
fileExistsAtPath:font_path
|
||||
isDirectory:&is_dir];
|
||||
|
||||
if (folder_exists && is_dir)
|
||||
add_path_fonts(file_manager, font_path);
|
||||
}
|
||||
}
|
||||
}
|
154
plugins/text-freetype2/find-font-iconv.c
Normal file
154
plugins/text-freetype2/find-font-iconv.c
Normal file
@ -0,0 +1,154 @@
|
||||
#include <iconv.h>
|
||||
#include <errno.h>
|
||||
#include "find-font.h"
|
||||
|
||||
struct mac_font_mapping {
|
||||
unsigned short encoding_id;
|
||||
unsigned short language_id;
|
||||
const char *code_page;
|
||||
};
|
||||
|
||||
#define TT_MAC_LANGID_ANY 0xFFFF
|
||||
|
||||
static const struct mac_font_mapping mac_codes[] = {
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_ENGLISH, "macintosh"},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_ICELANDIC,"x-mac-icelandic"},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_TURKISH, "x-mac-ce"},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_POLISH, "x-mac-ce"},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_ROMANIAN, "x-mac-romanian"},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_CZECH, "x-mac-ce"},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_SLOVAK, "x-mac-ce"},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_ANY, "macintosh"},
|
||||
{TT_MAC_ID_JAPANESE, TT_MAC_LANGID_JAPANESE, "Shift_JIS"},
|
||||
{TT_MAC_ID_JAPANESE, TT_MAC_LANGID_ANY, "Shift_JIS"},
|
||||
{TT_MAC_ID_KOREAN, TT_MAC_LANGID_KOREAN, "EUC-KR"},
|
||||
{TT_MAC_ID_KOREAN, TT_MAC_LANGID_ANY, "EUC-KR"},
|
||||
{TT_MAC_ID_ARABIC, TT_MAC_LANGID_ARABIC, "x-mac-arabic"},
|
||||
{TT_MAC_ID_ARABIC, TT_MAC_LANGID_URDU, "x-mac-farsi"},
|
||||
{TT_MAC_ID_ARABIC, TT_MAC_LANGID_FARSI, "x-mac-farsi"},
|
||||
{TT_MAC_ID_ARABIC, TT_MAC_LANGID_ANY, "x-mac-arabic"},
|
||||
{TT_MAC_ID_HEBREW, TT_MAC_LANGID_HEBREW, "x-mac-hebrew"},
|
||||
{TT_MAC_ID_HEBREW, TT_MAC_LANGID_ANY, "x-mac-hebrew"},
|
||||
{TT_MAC_ID_GREEK, TT_MAC_LANGID_ANY, "x-mac-greek"},
|
||||
{TT_MAC_ID_RUSSIAN, TT_MAC_LANGID_ANY, "x-mac-cyrillic"},
|
||||
{TT_MAC_ID_DEVANAGARI, TT_MAC_LANGID_ANY, "x-mac-devanagari"},
|
||||
{TT_MAC_ID_GURMUKHI, TT_MAC_LANGID_ANY, "x-mac-gurmukhi"},
|
||||
{TT_MAC_ID_GUJARATI, TT_MAC_LANGID_ANY, "x-mac-gujarati"},
|
||||
{
|
||||
TT_MAC_ID_TRADITIONAL_CHINESE,
|
||||
TT_MAC_LANGID_CHINESE_SIMPLIFIED,
|
||||
"Big5"
|
||||
},
|
||||
{
|
||||
TT_MAC_ID_TRADITIONAL_CHINESE,
|
||||
TT_MAC_LANGID_ANY,
|
||||
"Big5"
|
||||
},
|
||||
{
|
||||
TT_MAC_ID_SIMPLIFIED_CHINESE,
|
||||
TT_MAC_LANGID_CHINESE_SIMPLIFIED,
|
||||
"GB2312"
|
||||
},
|
||||
{
|
||||
TT_MAC_ID_SIMPLIFIED_CHINESE,
|
||||
TT_MAC_LANGID_ANY,
|
||||
"GB2312"
|
||||
}
|
||||
};
|
||||
|
||||
const char *iso_codes[] = {
|
||||
"us-ascii",
|
||||
NULL,
|
||||
"iso-8859-1"
|
||||
};
|
||||
|
||||
const char *ms_codes[] = {
|
||||
"UTF-16BE",
|
||||
"UTF-16BE",
|
||||
"Shift_JIS",
|
||||
NULL,
|
||||
"Big5",
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
"UTF-16BE"
|
||||
};
|
||||
|
||||
static const size_t mac_code_count = sizeof(mac_codes) / sizeof(mac_codes[0]);
|
||||
static const size_t iso_code_count = sizeof(iso_codes) / sizeof(iso_codes[0]);
|
||||
static const size_t ms_code_count = sizeof(ms_codes) / sizeof(ms_codes[0]);
|
||||
|
||||
static const char *get_mac_code(uint16_t encoding_id, uint16_t language_id)
|
||||
{
|
||||
for (size_t i = 0; i < mac_code_count; i++) {
|
||||
const struct mac_font_mapping *mac_code = &mac_codes[i];
|
||||
|
||||
if (mac_code->encoding_id == encoding_id &&
|
||||
mac_code->language_id == language_id)
|
||||
return mac_code->code_page;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *get_code_page_for_font(uint16_t platform_id,
|
||||
uint16_t encoding_id, uint16_t language_id)
|
||||
{
|
||||
const char *ret;
|
||||
|
||||
switch (platform_id) {
|
||||
case TT_PLATFORM_APPLE_UNICODE:
|
||||
return "UTF-16BE";
|
||||
case TT_PLATFORM_MACINTOSH:
|
||||
ret = get_mac_code(encoding_id, language_id);
|
||||
if (!ret)
|
||||
ret = get_mac_code(encoding_id, TT_MAC_LANGID_ANY);
|
||||
return ret;
|
||||
case TT_PLATFORM_ISO:
|
||||
if (encoding_id < iso_code_count)
|
||||
return iso_codes[encoding_id];
|
||||
break;
|
||||
case TT_PLATFORM_MICROSOFT:
|
||||
if (encoding_id < ms_code_count)
|
||||
return ms_codes[encoding_id];
|
||||
break;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *sfnt_name_to_utf8(FT_SfntName *sfnt_name)
|
||||
{
|
||||
const char *charset = get_code_page_for_font(sfnt_name->platform_id,
|
||||
sfnt_name->encoding_id, sfnt_name->language_id);
|
||||
char utf8[256];
|
||||
char *conv_in, *conv_out;
|
||||
size_t in_len, out_len;
|
||||
|
||||
iconv_t ic = iconv_open("UTF-8", charset);
|
||||
if (ic == (iconv_t)-1) {
|
||||
blog(LOG_WARNING, "couldn't intialize font code page "
|
||||
"conversion: '%s' to 'utf-8': errno = %d",
|
||||
charset, (int)errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
conv_in = (char*)sfnt_name->string;
|
||||
conv_out = utf8;
|
||||
in_len = sfnt_name->string_len;
|
||||
out_len = 256;
|
||||
|
||||
size_t n = iconv(ic, &conv_in, &in_len, &conv_out, &out_len);
|
||||
if (n == (size_t)-1) {
|
||||
blog(LOG_WARNING, "couldn't convert font name text: errno = %d",
|
||||
(int)errno);
|
||||
iconv_close(ic);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
iconv_close(ic);
|
||||
*conv_out = 0;
|
||||
return bstrdup(utf8);
|
||||
}
|
248
plugins/text-freetype2/find-font-windows.c
Normal file
248
plugins/text-freetype2/find-font-windows.c
Normal file
@ -0,0 +1,248 @@
|
||||
#include <util/dstr.h>
|
||||
#include <util/darray.h>
|
||||
#include "find-font.h"
|
||||
#include "text-freetype2.h"
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
#include <shlobj.h>
|
||||
|
||||
extern DARRAY(struct font_path_info) font_list;
|
||||
|
||||
struct mac_font_mapping {
|
||||
unsigned short encoding_id;
|
||||
unsigned short language_id;
|
||||
unsigned int code_page;
|
||||
};
|
||||
|
||||
#define TT_MAC_LANGID_ANY 0xFFFF
|
||||
|
||||
static const struct mac_font_mapping mac_codes[] = {
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_ENGLISH, 10000},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_ICELANDIC, 10079},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_TURKISH, 10081},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_POLISH, 10029},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_ROMANIAN, 10010},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_CZECH, 10029},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_SLOVAK, 10029},
|
||||
{TT_MAC_ID_ROMAN, TT_MAC_LANGID_ANY, 10000},
|
||||
{TT_MAC_ID_JAPANESE, TT_MAC_LANGID_JAPANESE, 932},
|
||||
{TT_MAC_ID_JAPANESE, TT_MAC_LANGID_ANY, 932},
|
||||
{TT_MAC_ID_TRADITIONAL_CHINESE,TT_MAC_LANGID_CHINESE_SIMPLIFIED, 950},
|
||||
{TT_MAC_ID_TRADITIONAL_CHINESE,TT_MAC_LANGID_ANY, 950},
|
||||
{TT_MAC_ID_KOREAN, TT_MAC_LANGID_KOREAN, 51949},
|
||||
{TT_MAC_ID_KOREAN, TT_MAC_LANGID_ANY, 51949},
|
||||
{TT_MAC_ID_ARABIC, TT_MAC_LANGID_ARABIC, 10004},
|
||||
{TT_MAC_ID_ARABIC, TT_MAC_LANGID_URDU, 0},
|
||||
{TT_MAC_ID_ARABIC, TT_MAC_LANGID_FARSI, 0},
|
||||
{TT_MAC_ID_ARABIC, TT_MAC_LANGID_ANY, 10004},
|
||||
{TT_MAC_ID_HEBREW, TT_MAC_LANGID_HEBREW, 10005},
|
||||
{TT_MAC_ID_HEBREW, TT_MAC_LANGID_ANY, 10005},
|
||||
{TT_MAC_ID_GREEK, TT_MAC_LANGID_ANY, 10006},
|
||||
{TT_MAC_ID_RUSSIAN, TT_MAC_LANGID_ANY, 10007},
|
||||
{TT_MAC_ID_DEVANAGARI, TT_MAC_LANGID_ANY, 0},
|
||||
{TT_MAC_ID_GURMUKHI, TT_MAC_LANGID_ANY, 0},
|
||||
{TT_MAC_ID_GUJARATI, TT_MAC_LANGID_ANY, 0},
|
||||
{TT_MAC_ID_SIMPLIFIED_CHINESE, TT_MAC_LANGID_CHINESE_SIMPLIFIED, 936},
|
||||
{TT_MAC_ID_SIMPLIFIED_CHINESE, TT_MAC_LANGID_ANY, 936}
|
||||
};
|
||||
|
||||
unsigned int iso_codes[] = {
|
||||
20127,
|
||||
0,
|
||||
28591
|
||||
};
|
||||
|
||||
unsigned int ms_codes[] = {
|
||||
1201,
|
||||
1201,
|
||||
932,
|
||||
0,
|
||||
950,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1201
|
||||
};
|
||||
|
||||
static const size_t mac_code_count = sizeof(mac_codes) / sizeof(mac_codes[0]);
|
||||
static const size_t iso_code_count = sizeof(iso_codes) / sizeof(iso_codes[0]);
|
||||
static const size_t ms_code_count = sizeof(ms_codes) / sizeof(ms_codes[0]);
|
||||
|
||||
static unsigned int get_mac_code(uint16_t encoding_id, uint16_t language_id)
|
||||
{
|
||||
for (size_t i = 0; i < mac_code_count; i++) {
|
||||
const struct mac_font_mapping *mac_code = &mac_codes[i];
|
||||
|
||||
if (mac_code->encoding_id == encoding_id &&
|
||||
mac_code->language_id == language_id)
|
||||
return mac_code->code_page;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int get_code_page_for_font(uint16_t platform_id,
|
||||
uint16_t encoding_id, uint16_t language_id)
|
||||
{
|
||||
unsigned int ret;
|
||||
|
||||
switch (platform_id) {
|
||||
case TT_PLATFORM_APPLE_UNICODE:
|
||||
return 1201;
|
||||
case TT_PLATFORM_MACINTOSH:
|
||||
ret = get_mac_code(encoding_id, language_id);
|
||||
if (!ret)
|
||||
ret = get_mac_code(encoding_id, TT_MAC_LANGID_ANY);
|
||||
return ret;
|
||||
case TT_PLATFORM_ISO:
|
||||
if (encoding_id < iso_code_count)
|
||||
return iso_codes[encoding_id];
|
||||
break;
|
||||
case TT_PLATFORM_MICROSOFT:
|
||||
if (encoding_id < ms_code_count)
|
||||
return ms_codes[encoding_id];
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char *wide_to_utf8(const wchar_t *str, size_t len)
|
||||
{
|
||||
size_t utf8_len;
|
||||
char *utf8_str = NULL;
|
||||
|
||||
utf8_len = (size_t)WideCharToMultiByte(CP_UTF8, 0, str, (int)len,
|
||||
NULL, 0, NULL, false);
|
||||
if (utf8_len) {
|
||||
utf8_str = bzalloc(utf8_len + 1);
|
||||
utf8_len = (size_t)WideCharToMultiByte(CP_UTF8, 0,
|
||||
str, (int)len,
|
||||
utf8_str, (int)utf8_len + 1, NULL, false);
|
||||
|
||||
if (!utf8_len) {
|
||||
bfree(utf8_str);
|
||||
utf8_str = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return utf8_str;
|
||||
}
|
||||
|
||||
static char *convert_utf16_be_to_utf8(FT_SfntName *sfnt_name)
|
||||
{
|
||||
size_t utf16_len = sfnt_name->string_len / 2;
|
||||
wchar_t *utf16_str = malloc((utf16_len + 1) * sizeof(wchar_t));
|
||||
char *utf8_str = NULL;
|
||||
|
||||
utf16_str[utf16_len] = 0;
|
||||
|
||||
/* convert to little endian */
|
||||
for (size_t i = 0; i < utf16_len; i++) {
|
||||
size_t pos = i * 2;
|
||||
wchar_t ch = *(wchar_t *)&sfnt_name->string[pos];
|
||||
|
||||
utf16_str[i] = ((ch >> 8) & 0xFF) | ((ch << 8) & 0xFF00);
|
||||
}
|
||||
|
||||
utf8_str = wide_to_utf8(utf16_str, utf16_len);
|
||||
|
||||
free(utf16_str);
|
||||
return utf8_str;
|
||||
}
|
||||
|
||||
char *sfnt_name_to_utf8(FT_SfntName *sfnt_name)
|
||||
{
|
||||
unsigned int code_page = get_code_page_for_font(
|
||||
sfnt_name->platform_id,
|
||||
sfnt_name->encoding_id,
|
||||
sfnt_name->language_id);
|
||||
|
||||
char *utf8_str = NULL;
|
||||
wchar_t *utf16_str;
|
||||
size_t utf16_len;
|
||||
|
||||
if (code_page == 1201)
|
||||
return convert_utf16_be_to_utf8(sfnt_name);
|
||||
else if (code_page == 0)
|
||||
return NULL;
|
||||
|
||||
utf16_len = MultiByteToWideChar(code_page, 0,
|
||||
sfnt_name->string, sfnt_name->string_len, NULL, 0);
|
||||
if (utf16_len) {
|
||||
utf16_str = malloc((utf16_len + 1) * sizeof(wchar_t));
|
||||
utf16_len = MultiByteToWideChar(code_page, 0,
|
||||
sfnt_name->string, sfnt_name->string_len,
|
||||
utf16_str, (int)utf16_len);
|
||||
|
||||
if (utf16_len) {
|
||||
utf16_str[utf16_len] = 0;
|
||||
utf8_str = wide_to_utf8(utf16_str, utf16_len);
|
||||
}
|
||||
|
||||
free(utf16_str);
|
||||
}
|
||||
|
||||
return utf8_str;
|
||||
}
|
||||
|
||||
void load_os_font_list(void)
|
||||
{
|
||||
struct dstr path = {0};
|
||||
HANDLE handle;
|
||||
WIN32_FIND_DATAA wfd;
|
||||
|
||||
dstr_reserve(&path, MAX_PATH);
|
||||
|
||||
HRESULT res = SHGetFolderPathA(NULL, CSIDL_FONTS, NULL,
|
||||
SHGFP_TYPE_CURRENT, path.array);
|
||||
if (res != S_OK) {
|
||||
blog(LOG_WARNING, "Error finding windows font folder");
|
||||
return;
|
||||
}
|
||||
|
||||
path.len = strlen(path.array);
|
||||
dstr_cat(&path, "\\*.*");
|
||||
|
||||
handle = FindFirstFileA(path.array, &wfd);
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
goto free_string;
|
||||
|
||||
dstr_resize(&path, path.len - 4);
|
||||
|
||||
do {
|
||||
struct dstr full_path = {0};
|
||||
FT_Face face;
|
||||
FT_Long idx = 0;
|
||||
FT_Long max_faces = 1;
|
||||
|
||||
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
||||
continue;
|
||||
|
||||
dstr_copy_dstr(&full_path, &path);
|
||||
dstr_cat(&full_path, "\\");
|
||||
dstr_cat(&full_path, wfd.cFileName);
|
||||
|
||||
while (idx < max_faces) {
|
||||
FT_Error ret = FT_New_Face(ft2_lib, full_path.array,
|
||||
idx, &face);
|
||||
if (ret != 0)
|
||||
break;
|
||||
|
||||
build_font_path_info(face, idx++, full_path.array);
|
||||
max_faces = face->num_faces;
|
||||
FT_Done_Face(face);
|
||||
}
|
||||
|
||||
dstr_free(&full_path);
|
||||
} while (FindNextFileA(handle, &wfd));
|
||||
|
||||
FindClose(handle);
|
||||
|
||||
free_string:
|
||||
dstr_free(&path);
|
||||
}
|
202
plugins/text-freetype2/find-font.c
Normal file
202
plugins/text-freetype2/find-font.c
Normal file
@ -0,0 +1,202 @@
|
||||
#include <ctype.h>
|
||||
#include <obs.h>
|
||||
#include "find-font.h"
|
||||
|
||||
DARRAY(struct font_path_info) font_list;
|
||||
|
||||
static void create_bitmap_sizes(struct font_path_info *info, FT_Face face)
|
||||
{
|
||||
DARRAY(int) sizes;
|
||||
|
||||
if (!info->is_bitmap) {
|
||||
info->num_sizes = 0;
|
||||
info->sizes = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
da_init(sizes);
|
||||
da_reserve(sizes, face->num_fixed_sizes);
|
||||
|
||||
for (int i = 0; i < face->num_fixed_sizes; i++) {
|
||||
int val = face->available_sizes[i].size >> 6;
|
||||
da_push_back(sizes, &val);
|
||||
}
|
||||
|
||||
info->sizes = sizes.array;
|
||||
info->num_sizes = face->num_fixed_sizes;
|
||||
}
|
||||
|
||||
static void add_font_path(FT_Face face,
|
||||
FT_Long idx,
|
||||
const char *family_in,
|
||||
const char *style_in,
|
||||
const char *path)
|
||||
{
|
||||
struct dstr face_and_style = {0};
|
||||
struct font_path_info info;
|
||||
|
||||
dstr_copy(&face_and_style, family_in);
|
||||
if (face->style_name) {
|
||||
struct dstr style = {0};
|
||||
|
||||
dstr_copy(&style, style_in);
|
||||
dstr_replace(&style, "Bold", "");
|
||||
dstr_replace(&style, "Italic", "");
|
||||
dstr_replace(&style, " ", " ");
|
||||
dstr_depad(&style);
|
||||
|
||||
if (!dstr_is_empty(&style)) {
|
||||
dstr_cat(&face_and_style, " ");
|
||||
dstr_cat_dstr(&face_and_style, &style);
|
||||
}
|
||||
|
||||
dstr_free(&style);
|
||||
}
|
||||
|
||||
info.face_and_style = face_and_style.array;
|
||||
info.full_len = face_and_style.len;
|
||||
info.face_len = strlen(family_in);
|
||||
|
||||
info.is_bitmap = !!(face->face_flags & FT_FACE_FLAG_FIXED_SIZES);
|
||||
info.bold = !!(face->style_flags & FT_STYLE_FLAG_BOLD);
|
||||
info.italic = !!(face->style_flags & FT_STYLE_FLAG_ITALIC);
|
||||
info.index = idx;
|
||||
|
||||
info.path = bstrdup(path);
|
||||
|
||||
create_bitmap_sizes(&info, face);
|
||||
da_push_back(font_list, &info);
|
||||
|
||||
/*blog(LOG_DEBUG, "name: %s\n\tstyle: %s\n\tpath: %s\n",
|
||||
family_in,
|
||||
style_in,
|
||||
path);*/
|
||||
}
|
||||
|
||||
void build_font_path_info(FT_Face face, FT_Long idx, const char *path)
|
||||
{
|
||||
FT_UInt num_names = FT_Get_Sfnt_Name_Count(face);
|
||||
DARRAY(char*) family_names;
|
||||
|
||||
da_init(family_names);
|
||||
da_push_back(family_names, &face->family_name);
|
||||
|
||||
for (FT_UInt i = 0; i < num_names; i++) {
|
||||
FT_SfntName name;
|
||||
char *family;
|
||||
FT_Error ret = FT_Get_Sfnt_Name(face, i, &name);
|
||||
|
||||
if (ret != 0 || name.name_id != TT_NAME_ID_FONT_FAMILY)
|
||||
continue;
|
||||
|
||||
family = sfnt_name_to_utf8(&name);
|
||||
if (!family)
|
||||
continue;
|
||||
|
||||
for (size_t i = 0; i < family_names.num; i++) {
|
||||
if (astrcmpi(family_names.array[i], family) == 0) {
|
||||
bfree(family);
|
||||
family = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (family)
|
||||
da_push_back(family_names, &family);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < family_names.num; i++) {
|
||||
add_font_path(face, idx, family_names.array[i],
|
||||
face->style_name, path);
|
||||
|
||||
/* first item isn't our allocation */
|
||||
if (i > 0)
|
||||
bfree(family_names.array[i]);
|
||||
}
|
||||
|
||||
da_free(family_names);
|
||||
}
|
||||
|
||||
void free_os_font_list(void)
|
||||
{
|
||||
for (size_t i = 0; i < font_list.num; i++)
|
||||
font_path_info_free(font_list.array + i);
|
||||
da_free(font_list);
|
||||
}
|
||||
|
||||
static inline size_t get_rating(struct font_path_info *info, struct dstr *cmp)
|
||||
{
|
||||
const char *src = info->face_and_style;
|
||||
const char *dst = cmp->array;
|
||||
size_t num = 0;
|
||||
|
||||
do {
|
||||
char ch1 = (char)toupper(*src);
|
||||
char ch2 = (char)toupper(*dst);
|
||||
|
||||
if (ch1 != ch2)
|
||||
break;
|
||||
|
||||
num++;
|
||||
} while (*src++ && *dst++);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
const char *get_font_path(const char *family, uint16_t size, const char *style,
|
||||
uint32_t flags, FT_Long *idx)
|
||||
{
|
||||
const char *best_path = NULL;
|
||||
double best_rating = 0.0;
|
||||
struct dstr face_and_style = {0};
|
||||
struct dstr style_str = {0};
|
||||
bool bold = !!(flags & OBS_FONT_BOLD);
|
||||
bool italic = !!(flags & OBS_FONT_ITALIC);
|
||||
|
||||
if (!family)
|
||||
return NULL;
|
||||
|
||||
dstr_copy(&style_str, style);
|
||||
dstr_replace(&style_str, "Bold", "");
|
||||
dstr_replace(&style_str, "Italic", "");
|
||||
dstr_replace(&style_str, " ", " ");
|
||||
dstr_depad(&style_str);
|
||||
|
||||
dstr_copy(&face_and_style, family);
|
||||
if (!dstr_is_empty(&style_str)) {
|
||||
dstr_cat(&face_and_style, " ");
|
||||
dstr_cat_dstr(&face_and_style, &style_str);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < font_list.num; i++) {
|
||||
struct font_path_info *info = font_list.array + i;
|
||||
|
||||
double rating = (double)get_rating(info, &face_and_style);
|
||||
if (rating < info->face_len)
|
||||
continue;
|
||||
|
||||
if (info->is_bitmap) {
|
||||
int best_diff = 1000;
|
||||
for (size_t j = 0; j < info->num_sizes; j++) {
|
||||
int diff = abs(info->sizes[j] - size);
|
||||
if (diff < best_diff)
|
||||
best_diff = diff;
|
||||
}
|
||||
|
||||
rating /= (double)(best_diff + 1.0);
|
||||
}
|
||||
|
||||
if (info->bold == bold) rating += 1.0;
|
||||
if (info->italic == italic) rating += 1.0;
|
||||
|
||||
if (rating > best_rating) {
|
||||
best_path = info->path;
|
||||
*idx = info->index;
|
||||
best_rating = rating;
|
||||
}
|
||||
}
|
||||
|
||||
dstr_free(&style_str);
|
||||
dstr_free(&face_and_style);
|
||||
return best_path;
|
||||
}
|
40
plugins/text-freetype2/find-font.h
Normal file
40
plugins/text-freetype2/find-font.h
Normal file
@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include FT_SFNT_NAMES_H
|
||||
#include FT_TRUETYPE_IDS_H
|
||||
|
||||
#include <util/dstr.h>
|
||||
#include <util/darray.h>
|
||||
|
||||
struct font_path_info {
|
||||
char *face_and_style;
|
||||
size_t full_len;
|
||||
size_t face_len;
|
||||
|
||||
bool is_bitmap;
|
||||
size_t num_sizes;
|
||||
int *sizes;
|
||||
|
||||
bool bold;
|
||||
bool italic;
|
||||
|
||||
char *path;
|
||||
FT_Long index;
|
||||
};
|
||||
|
||||
static inline void font_path_info_free(struct font_path_info *info)
|
||||
{
|
||||
bfree(info->sizes);
|
||||
bfree(info->face_and_style);
|
||||
bfree(info->path);
|
||||
}
|
||||
|
||||
extern void build_font_path_info(FT_Face face, FT_Long idx, const char *path);
|
||||
extern char *sfnt_name_to_utf8(FT_SfntName *sfnt_name);
|
||||
|
||||
extern void load_os_font_list(void);
|
||||
extern void free_os_font_list(void);
|
||||
extern const char *get_font_path(const char *family, uint16_t size,
|
||||
const char *style, uint32_t flags, FT_Long *idx);
|
84
plugins/text-freetype2/obs-convenience.c
Normal file
84
plugins/text-freetype2/obs-convenience.c
Normal file
@ -0,0 +1,84 @@
|
||||
/******************************************************************************
|
||||
Copyright (C) 2014 by Nibbles
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
******************************************************************************/
|
||||
|
||||
#include <obs-module.h>
|
||||
#include <graphics/vec2.h>
|
||||
#include <graphics/vec3.h>
|
||||
#include <graphics/vec4.h>
|
||||
#include "obs-convenience.h"
|
||||
|
||||
gs_vertbuffer_t create_uv_vbuffer(uint32_t num_verts, bool add_color) {
|
||||
obs_enter_graphics();
|
||||
|
||||
gs_vertbuffer_t tmp = NULL;
|
||||
struct gs_vb_data *vrect = NULL;
|
||||
|
||||
vrect = gs_vbdata_create();
|
||||
vrect->num = num_verts;
|
||||
vrect->points = (struct vec3 *)bmalloc(sizeof(struct vec3) * num_verts);
|
||||
vrect->num_tex = 1;
|
||||
vrect->tvarray =
|
||||
(struct gs_tvertarray *)bmalloc(sizeof(struct gs_tvertarray));
|
||||
vrect->tvarray[0].width = 2;
|
||||
vrect->tvarray[0].array = bmalloc(sizeof(struct vec2) * num_verts);
|
||||
if (add_color)
|
||||
vrect->colors = (uint32_t *)bmalloc
|
||||
(sizeof(uint32_t)* num_verts);
|
||||
|
||||
memset(vrect->points, 0, sizeof(struct vec3) * num_verts);
|
||||
memset(vrect->tvarray[0].array, 0, sizeof(struct vec2) * num_verts);
|
||||
if (add_color)
|
||||
memset(vrect->colors, 0, sizeof(uint32_t)* num_verts);
|
||||
|
||||
tmp = gs_vertexbuffer_create(vrect, GS_DYNAMIC);
|
||||
|
||||
if (tmp == NULL) {
|
||||
blog(LOG_WARNING, "Couldn't create UV vertex buffer.");
|
||||
}
|
||||
|
||||
obs_leave_graphics();
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void draw_uv_vbuffer(gs_vertbuffer_t vbuf, gs_texture_t tex, gs_effect_t effect,
|
||||
uint32_t num_verts) {
|
||||
gs_texture_t texture = tex;
|
||||
gs_technique_t tech = gs_effect_get_technique(effect, "Draw");
|
||||
gs_eparam_t image = gs_effect_get_param_by_name(effect, "image");
|
||||
size_t passes;
|
||||
|
||||
if (vbuf == NULL || tex == NULL) return;
|
||||
|
||||
gs_vertexbuffer_flush(vbuf);
|
||||
gs_load_vertexbuffer(vbuf);
|
||||
gs_load_indexbuffer(NULL);
|
||||
|
||||
passes = gs_technique_begin(tech);
|
||||
|
||||
for (size_t i = 0; i < passes; i++) {
|
||||
if (gs_technique_begin_pass(tech, i)) {
|
||||
gs_effect_set_texture(image, texture);
|
||||
|
||||
gs_draw(GS_TRIS, 0, num_verts);
|
||||
|
||||
gs_technique_end_pass(tech);
|
||||
}
|
||||
}
|
||||
|
||||
gs_technique_end(tech);
|
||||
}
|
43
plugins/text-freetype2/obs-convenience.h
Normal file
43
plugins/text-freetype2/obs-convenience.h
Normal file
@ -0,0 +1,43 @@
|
||||
/******************************************************************************
|
||||
Copyright (C) 2014 by Nibbles
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
******************************************************************************/
|
||||
|
||||
#include <obs-module.h>
|
||||
|
||||
gs_vertbuffer_t create_uv_vbuffer(uint32_t num_verts, bool add_color);
|
||||
void draw_uv_vbuffer(gs_vertbuffer_t vbuf, gs_texture_t tex, gs_effect_t effect,
|
||||
uint32_t num_verts);
|
||||
|
||||
#define set_v3_rect(a, x, y, w, h) \
|
||||
vec3_set(a, x, y, 0.0f); \
|
||||
vec3_set(a + 1, x + w, y, 0.0f); \
|
||||
vec3_set(a + 2, x, y + h, 0.0f); \
|
||||
vec3_set(a + 3, x, y + h, 0.0f); \
|
||||
vec3_set(a + 4, x + w, y, 0.0f); \
|
||||
vec3_set(a + 5, x + w, y + h, 0.0f);
|
||||
|
||||
#define set_v2_uv(a, u, v, u2, v2) \
|
||||
vec2_set(a, u, v); \
|
||||
vec2_set(a + 1, u2, v); \
|
||||
vec2_set(a + 2, u, v2); \
|
||||
vec2_set(a + 3, u, v2); \
|
||||
vec2_set(a + 4, u2, v); \
|
||||
vec2_set(a + 5, u2, v2);
|
||||
|
||||
#define set_rect_colors2(a, c1, c2) \
|
||||
uint32_t *b = a; \
|
||||
b[0] = b[1] = b[4] = c1; \
|
||||
b[2] = b[3] = b[5] = c2;
|
428
plugins/text-freetype2/text-freetype2.c
Normal file
428
plugins/text-freetype2/text-freetype2.c
Normal file
@ -0,0 +1,428 @@
|
||||
/******************************************************************************
|
||||
Copyright (C) 2014 by Nibbles
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
******************************************************************************/
|
||||
|
||||
#include <obs-module.h>
|
||||
#include <util/platform.h>
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include <sys/stat.h>
|
||||
#include "text-freetype2.h"
|
||||
#include "obs-convenience.h"
|
||||
#include "find-font.h"
|
||||
|
||||
FT_Library ft2_lib;
|
||||
|
||||
OBS_DECLARE_MODULE()
|
||||
OBS_MODULE_USE_DEFAULT_LOCALE("text-freetype2", "en-US")
|
||||
|
||||
uint32_t texbuf_w = 2048, texbuf_h = 2048;
|
||||
|
||||
static struct obs_source_info freetype2_source_info = {
|
||||
.id = "text_ft2_source",
|
||||
.type = OBS_SOURCE_TYPE_INPUT,
|
||||
.output_flags = OBS_SOURCE_VIDEO,
|
||||
.get_name = ft2_source_get_name,
|
||||
.create = ft2_source_create,
|
||||
.destroy = ft2_source_destroy,
|
||||
.update = ft2_source_update,
|
||||
.get_width = ft2_source_get_width,
|
||||
.get_height = ft2_source_get_height,
|
||||
.video_render = ft2_source_render,
|
||||
.video_tick = ft2_video_tick,
|
||||
.get_properties = ft2_source_properties,
|
||||
};
|
||||
|
||||
bool obs_module_load()
|
||||
{
|
||||
FT_Init_FreeType(&ft2_lib);
|
||||
|
||||
if (ft2_lib == NULL) {
|
||||
blog(LOG_WARNING, "FT2-text: Failed to initialize FT2.");
|
||||
return false;
|
||||
}
|
||||
|
||||
load_os_font_list();
|
||||
|
||||
obs_register_source(&freetype2_source_info);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void obs_module_unload(void)
|
||||
{
|
||||
free_os_font_list();
|
||||
FT_Done_FreeType(ft2_lib);
|
||||
}
|
||||
|
||||
static const char *ft2_source_get_name(void)
|
||||
{
|
||||
return obs_module_text("Text (FreeType 2)");
|
||||
}
|
||||
|
||||
static uint32_t ft2_source_get_width(void *data)
|
||||
{
|
||||
struct ft2_source *srcdata = data;
|
||||
|
||||
return srcdata->cx;
|
||||
}
|
||||
|
||||
static uint32_t ft2_source_get_height(void *data)
|
||||
{
|
||||
struct ft2_source *srcdata = data;
|
||||
|
||||
return srcdata->cy;
|
||||
}
|
||||
|
||||
static obs_properties_t ft2_source_properties(void)
|
||||
{
|
||||
obs_properties_t props = obs_properties_create();
|
||||
//obs_property_t prop;
|
||||
|
||||
// TODO:
|
||||
// Scrolling. Can't think of a way to do it with the render
|
||||
// targets currently being broken. (0.4.2)
|
||||
// Better/pixel shader outline/drop shadow
|
||||
// Some way to pull text files from network, I dunno
|
||||
|
||||
obs_properties_add_font(props, "font",
|
||||
obs_module_text("Font"));
|
||||
|
||||
obs_properties_add_text(props, "text",
|
||||
obs_module_text("Text"), OBS_TEXT_MULTILINE);
|
||||
|
||||
obs_properties_add_bool(props, "from_file",
|
||||
obs_module_text("ReadFromFile"));
|
||||
|
||||
obs_properties_add_bool(props, "log_mode",
|
||||
obs_module_text("ChatLogMode"));
|
||||
|
||||
obs_properties_add_path(props,
|
||||
"text_file", obs_module_text("TextFile"),
|
||||
OBS_PATH_FILE, "All font formats (*.txt);;", NULL);
|
||||
|
||||
obs_properties_add_color(props, "color1",
|
||||
obs_module_text("Color1"));
|
||||
|
||||
obs_properties_add_color(props, "color2",
|
||||
obs_module_text("Color2"));
|
||||
|
||||
obs_properties_add_bool(props, "outline",
|
||||
obs_module_text("Outline"));
|
||||
|
||||
obs_properties_add_bool(props, "drop_shadow",
|
||||
obs_module_text("DropShadow"));
|
||||
|
||||
obs_properties_add_int(props, "custom_width",
|
||||
obs_module_text("CustomWidth"), 0, 4096, 1);
|
||||
|
||||
obs_properties_add_bool(props, "word_wrap",
|
||||
obs_module_text("WordWrap"));
|
||||
|
||||
return props;
|
||||
}
|
||||
|
||||
static void ft2_source_destroy(void *data)
|
||||
{
|
||||
struct ft2_source *srcdata = data;
|
||||
|
||||
if (srcdata->font_face != NULL) {
|
||||
FT_Done_Face(srcdata->font_face);
|
||||
srcdata->font_face = NULL;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < num_cache_slots; i++) {
|
||||
if (srcdata->cacheglyphs[i] != NULL) {
|
||||
bfree(srcdata->cacheglyphs[i]);
|
||||
srcdata->cacheglyphs[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (srcdata->font_name != NULL)
|
||||
bfree(srcdata->font_name);
|
||||
if (srcdata->font_style != NULL)
|
||||
bfree(srcdata->font_style);
|
||||
if (srcdata->text != NULL)
|
||||
bfree(srcdata->text);
|
||||
if (srcdata->texbuf != NULL)
|
||||
bfree(srcdata->texbuf);
|
||||
if (srcdata->colorbuf != NULL)
|
||||
bfree(srcdata->colorbuf);
|
||||
if (srcdata->text_file != NULL)
|
||||
bfree(srcdata->text_file);
|
||||
|
||||
obs_enter_graphics();
|
||||
|
||||
if (srcdata->tex != NULL) {
|
||||
gs_texture_destroy(srcdata->tex);
|
||||
srcdata->tex = NULL;
|
||||
}
|
||||
if (srcdata->vbuf != NULL) {
|
||||
gs_vertexbuffer_destroy(srcdata->vbuf);
|
||||
srcdata->vbuf = NULL;
|
||||
}
|
||||
if (srcdata->draw_effect != NULL) {
|
||||
gs_effect_destroy(srcdata->draw_effect);
|
||||
srcdata->draw_effect = NULL;
|
||||
}
|
||||
|
||||
obs_leave_graphics();
|
||||
|
||||
bfree(srcdata);
|
||||
}
|
||||
|
||||
static void ft2_source_render(void *data, gs_effect_t effect)
|
||||
{
|
||||
struct ft2_source *srcdata = data;
|
||||
if (srcdata == NULL) return;
|
||||
|
||||
if (srcdata->tex == NULL || srcdata->vbuf == NULL) return;
|
||||
|
||||
gs_reset_blend_state();
|
||||
if (srcdata->outline_text) draw_outlines(srcdata);
|
||||
if (srcdata->drop_shadow) draw_drop_shadow(srcdata);
|
||||
|
||||
draw_uv_vbuffer(srcdata->vbuf, srcdata->tex,
|
||||
srcdata->draw_effect, (uint32_t)wcslen(srcdata->text) * 6);
|
||||
|
||||
UNUSED_PARAMETER(effect);
|
||||
}
|
||||
|
||||
static void ft2_video_tick(void *data, float seconds)
|
||||
{
|
||||
struct ft2_source *srcdata = data;
|
||||
if (srcdata == NULL) return;
|
||||
if (srcdata->text_file == NULL) return;
|
||||
|
||||
if (os_gettime_ns() - srcdata->last_checked >= 1000000000) {
|
||||
time_t t = get_modified_timestamp(srcdata->text_file);
|
||||
srcdata->last_checked = os_gettime_ns();
|
||||
|
||||
if (srcdata->m_timestamp != t) {
|
||||
if (srcdata->log_mode)
|
||||
read_from_end(srcdata, srcdata->text_file);
|
||||
else
|
||||
load_text_from_file(srcdata,
|
||||
srcdata->text_file);
|
||||
set_up_vertex_buffer(srcdata);
|
||||
}
|
||||
}
|
||||
|
||||
UNUSED_PARAMETER(seconds);
|
||||
}
|
||||
|
||||
static bool init_font(struct ft2_source *srcdata)
|
||||
{
|
||||
FT_Long index;
|
||||
const char *path = get_font_path(srcdata->font_name, srcdata->font_size,
|
||||
srcdata->font_style, srcdata->font_flags, &index);
|
||||
if (!path)
|
||||
return false;
|
||||
|
||||
if (srcdata->font_face != NULL) {
|
||||
FT_Done_Face(srcdata->font_face);
|
||||
srcdata->font_face = NULL;
|
||||
}
|
||||
|
||||
return FT_New_Face(ft2_lib, path, index, &srcdata->font_face) == 0;
|
||||
}
|
||||
|
||||
static void ft2_source_update(void *data, obs_data_t settings)
|
||||
{
|
||||
struct ft2_source *srcdata = data;
|
||||
obs_data_t font_obj = obs_data_get_obj(settings, "font");
|
||||
bool vbuf_needs_update = false;
|
||||
bool word_wrap = false;
|
||||
uint32_t color[2];
|
||||
uint32_t custom_width = 0;
|
||||
|
||||
const char *font_name = obs_data_get_string(font_obj, "face");
|
||||
const char *font_style = obs_data_get_string(font_obj, "style");
|
||||
uint16_t font_size = (uint16_t)obs_data_get_int(font_obj, "size");
|
||||
uint32_t font_flags = (uint32_t)obs_data_get_int(font_obj, "flags");
|
||||
|
||||
if (!font_obj)
|
||||
return;
|
||||
|
||||
srcdata->drop_shadow = obs_data_get_bool(settings, "drop_shadow");
|
||||
srcdata->outline_text = obs_data_get_bool(settings, "outline");
|
||||
word_wrap = obs_data_get_bool(settings, "word_wrap");
|
||||
|
||||
color[0] = (uint32_t)obs_data_get_int(settings, "color1");
|
||||
color[1] = (uint32_t)obs_data_get_int(settings, "color2");
|
||||
|
||||
custom_width = (uint32_t)obs_data_get_int(settings, "custom_width");
|
||||
if (custom_width >= 100) {
|
||||
if (custom_width != srcdata->custom_width) {
|
||||
srcdata->custom_width = custom_width;
|
||||
vbuf_needs_update = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (srcdata->custom_width >= 100)
|
||||
vbuf_needs_update = true;
|
||||
srcdata->custom_width = 0;
|
||||
}
|
||||
|
||||
if (word_wrap != srcdata->word_wrap) {
|
||||
srcdata->word_wrap = word_wrap;
|
||||
vbuf_needs_update = true;
|
||||
}
|
||||
|
||||
if (color[0] != srcdata->color[0] || color[1] != srcdata->color[1]) {
|
||||
srcdata->color[0] = color[0];
|
||||
srcdata->color[1] = color[1];
|
||||
vbuf_needs_update = true;
|
||||
}
|
||||
|
||||
bool from_file = obs_data_get_bool(settings, "from_file");
|
||||
bool chat_log_mode = obs_data_get_bool(settings, "log_mode");
|
||||
|
||||
srcdata->log_mode = chat_log_mode;
|
||||
|
||||
if (ft2_lib == NULL) return;
|
||||
|
||||
if (!from_file) {
|
||||
if (srcdata->text_file != NULL) {
|
||||
bfree(srcdata->text_file);
|
||||
srcdata->text_file = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (srcdata->draw_effect == NULL) {
|
||||
char *effect_file = NULL;
|
||||
char *error_string = NULL;
|
||||
|
||||
effect_file =
|
||||
obs_module_file("text_default.effect");
|
||||
|
||||
if (effect_file) {
|
||||
obs_enter_graphics();
|
||||
srcdata->draw_effect = gs_effect_create_from_file(
|
||||
effect_file, &error_string);
|
||||
obs_leave_graphics();
|
||||
|
||||
bfree(effect_file);
|
||||
if (error_string != NULL)
|
||||
bfree(error_string);
|
||||
}
|
||||
}
|
||||
|
||||
if (srcdata->font_size != font_size)
|
||||
vbuf_needs_update = true;
|
||||
|
||||
if (srcdata->font_name != NULL) {
|
||||
if (strcmp(font_name, srcdata->font_name) == 0 &&
|
||||
strcmp(font_style, srcdata->font_style) == 0 &&
|
||||
font_flags == srcdata->font_flags &&
|
||||
font_size == srcdata->font_size)
|
||||
goto skip_font_load;
|
||||
|
||||
bfree(srcdata->font_name);
|
||||
bfree(srcdata->font_style);
|
||||
srcdata->font_name = NULL;
|
||||
srcdata->font_style = NULL;
|
||||
srcdata->max_h = 0;
|
||||
}
|
||||
|
||||
srcdata->font_name = bstrdup(font_name);
|
||||
srcdata->font_style = bstrdup(font_style);
|
||||
srcdata->font_size = font_size;
|
||||
srcdata->font_flags = font_flags;
|
||||
|
||||
if (!init_font(srcdata) || srcdata->font_face == NULL) {
|
||||
blog(LOG_WARNING, "FT2-text: Failed to load font %s",
|
||||
srcdata->font_name);
|
||||
goto error;
|
||||
}
|
||||
else {
|
||||
FT_Set_Pixel_Sizes(srcdata->font_face, 0, srcdata->font_size);
|
||||
FT_Select_Charmap(srcdata->font_face, FT_ENCODING_UNICODE);
|
||||
}
|
||||
|
||||
if (srcdata->texbuf != NULL) {
|
||||
bfree(srcdata->texbuf);
|
||||
srcdata->texbuf = NULL;
|
||||
}
|
||||
srcdata->texbuf = bzalloc(texbuf_w * texbuf_h * 4);
|
||||
|
||||
cache_standard_glyphs(srcdata);
|
||||
skip_font_load:;
|
||||
if (from_file) {
|
||||
const char *tmp = obs_data_get_string(settings, "text_file");
|
||||
if (strlen(tmp) == 0)
|
||||
return;
|
||||
if (srcdata->text_file != NULL) {
|
||||
if (strcmp(srcdata->text_file, tmp) == 0
|
||||
&& !vbuf_needs_update)
|
||||
goto error;
|
||||
bfree(srcdata->text_file);
|
||||
srcdata->text_file = NULL;
|
||||
}
|
||||
else
|
||||
blog(LOG_WARNING,
|
||||
"FT2-text: Failed to open %s for reading", tmp);
|
||||
srcdata->text_file = bstrdup(tmp);
|
||||
|
||||
if (chat_log_mode)
|
||||
read_from_end(srcdata, tmp);
|
||||
else
|
||||
load_text_from_file(srcdata, tmp);
|
||||
srcdata->last_checked = os_gettime_ns();
|
||||
}
|
||||
else {
|
||||
const char *tmp = obs_data_get_string(settings, "text");
|
||||
if (strlen(tmp) == 0) goto error;
|
||||
|
||||
if (srcdata->text != NULL) {
|
||||
bfree(srcdata->text);
|
||||
srcdata->text = NULL;
|
||||
}
|
||||
|
||||
os_utf8_to_wcs_ptr(tmp, strlen(tmp), &srcdata->text);
|
||||
}
|
||||
|
||||
cache_glyphs(srcdata, srcdata->text);
|
||||
|
||||
set_up_vertex_buffer(srcdata);
|
||||
|
||||
error:
|
||||
obs_data_release(font_obj);
|
||||
}
|
||||
|
||||
static void *ft2_source_create(obs_data_t settings, obs_source_t source)
|
||||
{
|
||||
struct ft2_source *srcdata = bzalloc(sizeof(struct ft2_source));
|
||||
obs_data_t font_obj = obs_data_create();
|
||||
srcdata->src = source;
|
||||
|
||||
srcdata->font_size = 32;
|
||||
|
||||
obs_data_set_default_string(font_obj, "face", "Arial");
|
||||
obs_data_set_default_int(font_obj, "size", 32);
|
||||
obs_data_set_default_obj(settings, "font", font_obj);
|
||||
|
||||
obs_data_set_default_int(settings, "color1", 0xFFFFFFFF);
|
||||
obs_data_set_default_int(settings, "color2", 0xFFFFFFFF);
|
||||
obs_data_set_default_string(settings, "text",
|
||||
"The lazy snake jumps over the happy MASKEN.");
|
||||
|
||||
ft2_source_update(srcdata, settings);
|
||||
|
||||
obs_data_release(font_obj);
|
||||
|
||||
return srcdata;
|
||||
}
|
92
plugins/text-freetype2/text-freetype2.h
Normal file
92
plugins/text-freetype2/text-freetype2.h
Normal file
@ -0,0 +1,92 @@
|
||||
/******************************************************************************
|
||||
Copyright (C) 2014 by Nibbles
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
******************************************************************************/
|
||||
|
||||
#include <obs-module.h>
|
||||
#include <ft2build.h>
|
||||
|
||||
#define num_cache_slots 65535
|
||||
#define src_glyph srcdata->cacheglyphs[glyph_index]
|
||||
|
||||
struct glyph_info {
|
||||
float u, v, u2, v2;
|
||||
int32_t w, h, xoff, yoff;
|
||||
int32_t xadv;
|
||||
};
|
||||
|
||||
struct ft2_source {
|
||||
char *font_name;
|
||||
char *font_style;
|
||||
uint16_t font_size;
|
||||
uint32_t font_flags;
|
||||
|
||||
char *text_file;
|
||||
wchar_t *text;
|
||||
time_t m_timestamp;
|
||||
uint64_t last_checked;
|
||||
|
||||
uint32_t cx, cy, max_h, custom_width;
|
||||
uint32_t texbuf_x, texbuf_y;
|
||||
uint32_t color[2];
|
||||
uint32_t *colorbuf;
|
||||
|
||||
int32_t cur_scroll, scroll_speed;
|
||||
|
||||
gs_texture_t tex;
|
||||
|
||||
struct glyph_info *cacheglyphs[num_cache_slots];
|
||||
|
||||
FT_Face font_face;
|
||||
|
||||
uint32_t *texbuf;
|
||||
gs_vertbuffer_t vbuf;
|
||||
|
||||
gs_effect_t draw_effect;
|
||||
bool outline_text, drop_shadow;
|
||||
bool log_mode, word_wrap;
|
||||
|
||||
obs_source_t src;
|
||||
};
|
||||
|
||||
extern FT_Library ft2_lib;
|
||||
|
||||
static void *ft2_source_create(obs_data_t settings, obs_source_t source);
|
||||
static void ft2_source_destroy(void *data);
|
||||
static void ft2_source_update(void *data, obs_data_t settings);
|
||||
static void ft2_source_render(void *data, gs_effect_t effect);
|
||||
static void ft2_video_tick(void *data, float seconds);
|
||||
|
||||
void draw_outlines(struct ft2_source *srcdata);
|
||||
void draw_drop_shadow(struct ft2_source *srcdata);
|
||||
|
||||
static uint32_t ft2_source_get_width(void *data);
|
||||
static uint32_t ft2_source_get_height(void *data);
|
||||
|
||||
static obs_properties_t ft2_source_properties(void);
|
||||
|
||||
static const char *ft2_source_get_name(void);
|
||||
|
||||
uint32_t get_ft2_text_width(wchar_t *text, struct ft2_source *srcdata);
|
||||
|
||||
time_t get_modified_timestamp(char *filename);
|
||||
void load_text_from_file(struct ft2_source *srcdata, const char *filename);
|
||||
void read_from_end(struct ft2_source *srcdata, const char *filename);
|
||||
|
||||
void cache_standard_glyphs(struct ft2_source *srcdata);
|
||||
void cache_glyphs(struct ft2_source *srcdata, wchar_t *cache_glyphs);
|
||||
|
||||
void set_up_vertex_buffer(struct ft2_source *srcdata);
|
||||
void fill_vertex_buffer(struct ft2_source *srcdata);
|
457
plugins/text-freetype2/text-functionality.c
Normal file
457
plugins/text-freetype2/text-functionality.c
Normal file
@ -0,0 +1,457 @@
|
||||
/******************************************************************************
|
||||
Copyright (C) 2014 by Nibbles
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
******************************************************************************/
|
||||
|
||||
#include <obs-module.h>
|
||||
#include <util/platform.h>
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include <sys/stat.h>
|
||||
#include "text-freetype2.h"
|
||||
#include "obs-convenience.h"
|
||||
|
||||
float offsets[16] = { -2.0f, 0.0f, 0.0f, -2.0f, 2.0f, 0.0f, 2.0f, 0.0f,
|
||||
0.0f, 2.0f, 0.0f, 2.0f, -2.0f, 0.0f, -2.0f, 0.0f };
|
||||
|
||||
extern uint32_t texbuf_w, texbuf_h;
|
||||
|
||||
void draw_outlines(struct ft2_source *srcdata)
|
||||
{
|
||||
// Horrible (hopefully temporary) solution for outlines.
|
||||
uint32_t *tmp;
|
||||
|
||||
struct gs_vb_data *vdata = gs_vertexbuffer_get_data(srcdata->vbuf);
|
||||
tmp = vdata->colors;
|
||||
vdata->colors = srcdata->colorbuf;
|
||||
|
||||
gs_matrix_push();
|
||||
for (int32_t i = 0; i < 8; i++) {
|
||||
gs_matrix_translate3f(offsets[i * 2], offsets[(i * 2) + 1],
|
||||
0.0f);
|
||||
draw_uv_vbuffer(srcdata->vbuf, srcdata->tex,
|
||||
srcdata->draw_effect,
|
||||
(uint32_t)wcslen(srcdata->text) * 6);
|
||||
}
|
||||
gs_matrix_identity();
|
||||
gs_matrix_pop();
|
||||
|
||||
vdata->colors = tmp;
|
||||
}
|
||||
|
||||
void draw_drop_shadow(struct ft2_source *srcdata)
|
||||
{
|
||||
// Horrible (hopefully temporary) solution for drop shadow.
|
||||
uint32_t *tmp;
|
||||
|
||||
struct gs_vb_data *vdata = gs_vertexbuffer_get_data(srcdata->vbuf);
|
||||
tmp = vdata->colors;
|
||||
vdata->colors = srcdata->colorbuf;
|
||||
|
||||
gs_matrix_push();
|
||||
gs_matrix_translate3f(4.0f, 4.0f, 0.0f);
|
||||
draw_uv_vbuffer(srcdata->vbuf, srcdata->tex,
|
||||
srcdata->draw_effect, (uint32_t)wcslen(srcdata->text) * 6);
|
||||
gs_matrix_identity();
|
||||
gs_matrix_pop();
|
||||
|
||||
vdata->colors = tmp;
|
||||
}
|
||||
|
||||
void set_up_vertex_buffer(struct ft2_source *srcdata)
|
||||
{
|
||||
FT_UInt glyph_index = 0;
|
||||
uint32_t x = 0, space_pos = 0, word_width = 0;
|
||||
|
||||
if (srcdata->custom_width >= 100)
|
||||
srcdata->cx = srcdata->custom_width;
|
||||
else
|
||||
srcdata->cx = get_ft2_text_width(srcdata->text, srcdata);
|
||||
srcdata->cy = srcdata->max_h;
|
||||
|
||||
obs_enter_graphics();
|
||||
if (srcdata->vbuf != NULL) {
|
||||
gs_vertbuffer_t tmpvbuf = srcdata->vbuf;
|
||||
srcdata->vbuf = NULL;
|
||||
gs_vertexbuffer_destroy(tmpvbuf);
|
||||
}
|
||||
srcdata->vbuf = create_uv_vbuffer((uint32_t)wcslen(srcdata->text) * 6,
|
||||
true);
|
||||
|
||||
if (srcdata->custom_width <= 100) goto skip_word_wrap;
|
||||
if (!srcdata->word_wrap) goto skip_word_wrap;
|
||||
|
||||
for (uint32_t i = 0; i <= wcslen(srcdata->text); i++) {
|
||||
if (i == wcslen(srcdata->text)) goto eos_check;
|
||||
|
||||
if (srcdata->text[i] != L' ' && srcdata->text[i] != L'\n')
|
||||
goto next_char;
|
||||
|
||||
eos_check:;
|
||||
if (x + word_width > srcdata->custom_width) {
|
||||
if (space_pos != 0)
|
||||
srcdata->text[space_pos] = L'\n';
|
||||
x = 0;
|
||||
}
|
||||
if (i == wcslen(srcdata->text)) goto eos_skip;
|
||||
|
||||
x += word_width;
|
||||
word_width = 0;
|
||||
if (srcdata->text[i] == L'\n')
|
||||
x = 0;
|
||||
if (srcdata->text[i] == L' ')
|
||||
space_pos = i;
|
||||
next_char:;
|
||||
glyph_index = FT_Get_Char_Index(srcdata->font_face,
|
||||
srcdata->text[i]);
|
||||
word_width += src_glyph->xadv;
|
||||
eos_skip:;
|
||||
}
|
||||
|
||||
skip_word_wrap:;
|
||||
fill_vertex_buffer(srcdata);
|
||||
obs_leave_graphics();
|
||||
}
|
||||
|
||||
void fill_vertex_buffer(struct ft2_source *srcdata)
|
||||
{
|
||||
struct gs_vb_data *vdata = gs_vertexbuffer_get_data(srcdata->vbuf);
|
||||
if (vdata == NULL) return;
|
||||
struct vec2 *tvarray = (struct vec2 *)vdata->tvarray[0].array;
|
||||
uint32_t *col = (uint32_t *)vdata->colors;
|
||||
|
||||
FT_UInt glyph_index = 0;
|
||||
|
||||
uint32_t dx = 0, dy = srcdata->max_h, max_y = dy;
|
||||
uint32_t cur_glyph = 0;
|
||||
|
||||
if (srcdata->colorbuf != NULL) {
|
||||
bfree(srcdata->colorbuf);
|
||||
srcdata->colorbuf = NULL;
|
||||
}
|
||||
srcdata->colorbuf = bzalloc(sizeof(uint32_t)*wcslen(srcdata->text) * 6);
|
||||
for (uint32_t i = 0; i < wcslen(srcdata->text) * 6; i++) {
|
||||
srcdata->colorbuf[i] = 0xFF000000;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < wcslen(srcdata->text); i++) {
|
||||
add_linebreak:;
|
||||
if (srcdata->text[i] != L'\n') goto draw_glyph;
|
||||
dx = 0; i++;
|
||||
dy += srcdata->max_h + 4;
|
||||
if (i == wcslen(srcdata->text)) goto skip_glyph;
|
||||
if (srcdata->text[i] == L'\n') goto add_linebreak;
|
||||
draw_glyph:;
|
||||
// Skip filthy dual byte Windows line breaks
|
||||
if (srcdata->text[i] == L'\r') goto skip_glyph;
|
||||
|
||||
glyph_index = FT_Get_Char_Index(srcdata->font_face,
|
||||
srcdata->text[i]);
|
||||
if (src_glyph == NULL)
|
||||
goto skip_glyph;
|
||||
|
||||
if (srcdata->custom_width < 100) goto skip_custom_width;
|
||||
|
||||
if (dx + src_glyph->xadv > srcdata->custom_width) {
|
||||
dx = 0;
|
||||
dy += srcdata->max_h + 4;
|
||||
}
|
||||
|
||||
skip_custom_width:;
|
||||
|
||||
set_v3_rect(vdata->points + (cur_glyph * 6),
|
||||
(float)dx + (float)src_glyph->xoff,
|
||||
(float)dy - (float)src_glyph->yoff,
|
||||
(float)src_glyph->w,
|
||||
(float)src_glyph->h);
|
||||
set_v2_uv(tvarray + (cur_glyph * 6),
|
||||
src_glyph->u,
|
||||
src_glyph->v,
|
||||
src_glyph->u2,
|
||||
src_glyph->v2);
|
||||
set_rect_colors2(col + (cur_glyph * 6),
|
||||
srcdata->color[0],
|
||||
srcdata->color[1]);
|
||||
dx += src_glyph->xadv;
|
||||
if (dy - (float)src_glyph->yoff + src_glyph->h > max_y)
|
||||
max_y = dy - src_glyph->yoff + src_glyph->h;
|
||||
cur_glyph++;
|
||||
skip_glyph:;
|
||||
}
|
||||
|
||||
srcdata->cy = max_y;
|
||||
}
|
||||
|
||||
void cache_standard_glyphs(struct ft2_source *srcdata)
|
||||
{
|
||||
for (uint32_t i = 0; i < num_cache_slots; i++) {
|
||||
if (srcdata->cacheglyphs[i] != NULL) {
|
||||
bfree(srcdata->cacheglyphs[i]);
|
||||
srcdata->cacheglyphs[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
srcdata->texbuf_x = 0;
|
||||
srcdata->texbuf_y = 0;
|
||||
|
||||
cache_glyphs(srcdata, L"abcdefghijklmnopqrstuvwxyz" \
|
||||
L"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" \
|
||||
L"!@#$%^&*()-_=+,<.>/?\\|[]{}`~ \'\"\0");
|
||||
}
|
||||
|
||||
#define glyph_pos x + (y*slot->bitmap.pitch)
|
||||
#define buf_pos (dx + x) + ((dy + y) * texbuf_w)
|
||||
|
||||
void cache_glyphs(struct ft2_source *srcdata, wchar_t *cache_glyphs)
|
||||
{
|
||||
FT_GlyphSlot slot = srcdata->font_face->glyph;
|
||||
FT_UInt glyph_index = 0;
|
||||
|
||||
uint32_t dx = srcdata->texbuf_x, dy = srcdata->texbuf_y;
|
||||
uint8_t alpha;
|
||||
|
||||
int32_t g_pitch = 0;
|
||||
int32_t cached_glyphs = 0;
|
||||
|
||||
for (uint32_t i = 0; i < wcslen(cache_glyphs); i++) {
|
||||
glyph_index = FT_Get_Char_Index(srcdata->font_face,
|
||||
cache_glyphs[i]);
|
||||
|
||||
if (src_glyph != NULL)
|
||||
goto skip_glyph;
|
||||
|
||||
FT_Load_Glyph(srcdata->font_face, glyph_index, FT_LOAD_DEFAULT);
|
||||
FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
|
||||
|
||||
uint32_t g_w = slot->bitmap.width;
|
||||
uint32_t g_h = slot->bitmap.rows;
|
||||
g_pitch = slot->bitmap.pitch;
|
||||
|
||||
if (srcdata->max_h < g_h) srcdata->max_h = g_h;
|
||||
|
||||
if (dx + g_w >= texbuf_w) {
|
||||
dx = 0;
|
||||
dy += srcdata->max_h + 1;
|
||||
}
|
||||
|
||||
src_glyph = bzalloc(sizeof(struct glyph_info));
|
||||
src_glyph->u = (float)dx / (float)texbuf_w;
|
||||
src_glyph->u2 = (float)(dx + g_w) / (float)texbuf_w;
|
||||
src_glyph->v = (float)dy / (float)texbuf_h;
|
||||
src_glyph->v2 = (float)(dy + g_h) / (float)texbuf_h;
|
||||
src_glyph->w = g_w;
|
||||
src_glyph->h = g_h;
|
||||
src_glyph->yoff = slot->bitmap_top;
|
||||
src_glyph->xoff = slot->bitmap_left;
|
||||
src_glyph->xadv = slot->advance.x >> 6;
|
||||
|
||||
for (uint32_t y = 0; y < g_h; y++) {
|
||||
for (uint32_t x = 0; x < g_w; x++) {
|
||||
alpha = slot->bitmap.buffer[glyph_pos];
|
||||
srcdata->texbuf[buf_pos] =
|
||||
0x00FFFFFF ^ (alpha << 24);
|
||||
}
|
||||
}
|
||||
|
||||
dx += (g_w + 1);
|
||||
if (dx >= texbuf_w) {
|
||||
dx = 0;
|
||||
dy += srcdata->max_h;
|
||||
}
|
||||
|
||||
cached_glyphs++;
|
||||
skip_glyph:;
|
||||
}
|
||||
|
||||
srcdata->texbuf_x = dx;
|
||||
srcdata->texbuf_y = dy;
|
||||
|
||||
if (cached_glyphs > 0) {
|
||||
|
||||
obs_enter_graphics();
|
||||
|
||||
if (srcdata->tex != NULL) {
|
||||
gs_texture_t tmp_texture = NULL;
|
||||
tmp_texture = srcdata->tex;
|
||||
srcdata->tex = NULL;
|
||||
gs_texture_destroy(tmp_texture);
|
||||
}
|
||||
|
||||
srcdata->tex = gs_texture_create(texbuf_w, texbuf_h,
|
||||
GS_RGBA, 1, (const uint8_t **)&srcdata->texbuf, 0);
|
||||
|
||||
obs_leave_graphics();
|
||||
}
|
||||
}
|
||||
|
||||
time_t get_modified_timestamp(char *filename)
|
||||
{
|
||||
struct stat stats;
|
||||
|
||||
// stat is apparently terrifying and horrible, but we only call it once
|
||||
// every second at most.
|
||||
stat(filename, &stats);
|
||||
|
||||
return stats.st_mtime;
|
||||
}
|
||||
|
||||
void load_text_from_file(struct ft2_source *srcdata, const char *filename)
|
||||
{
|
||||
FILE *tmp_file = NULL;
|
||||
uint32_t filesize = 0;
|
||||
char *tmp_read = NULL;
|
||||
uint16_t header = 0;
|
||||
|
||||
tmp_file = fopen(filename, "rb");
|
||||
if (tmp_file == NULL) {
|
||||
blog(LOG_WARNING, "Failed to open file %s", filename);
|
||||
return;
|
||||
}
|
||||
fseek(tmp_file, 0, SEEK_END);
|
||||
filesize = (uint32_t)ftell(tmp_file);
|
||||
fseek(tmp_file, 0, SEEK_SET);
|
||||
fread(&header, 2, 1, tmp_file);
|
||||
|
||||
if (header == 0xFEFF) {
|
||||
// File is already in UTF-16 format
|
||||
if (srcdata->text != NULL) {
|
||||
bfree(srcdata->text);
|
||||
srcdata->text = NULL;
|
||||
}
|
||||
srcdata->text = bzalloc(filesize);
|
||||
fread(srcdata->text, filesize - 2, 1, tmp_file);
|
||||
|
||||
srcdata->m_timestamp =
|
||||
get_modified_timestamp(srcdata->text_file);
|
||||
bfree(tmp_read);
|
||||
fclose(tmp_file);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(tmp_file, 0, SEEK_SET);
|
||||
srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file);
|
||||
|
||||
tmp_read = bzalloc(filesize + 1);
|
||||
fread(tmp_read, filesize, 1, tmp_file);
|
||||
fclose(tmp_file);
|
||||
|
||||
if (srcdata->text != NULL) {
|
||||
bfree(srcdata->text);
|
||||
srcdata->text = NULL;
|
||||
}
|
||||
srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t));
|
||||
os_utf8_to_wcs(tmp_read, strlen(tmp_read),
|
||||
srcdata->text, (strlen(tmp_read) + 1));
|
||||
bfree(tmp_read);
|
||||
}
|
||||
|
||||
void read_from_end(struct ft2_source *srcdata, const char *filename)
|
||||
{
|
||||
FILE *tmp_file = NULL;
|
||||
uint32_t filesize = 0, cur_pos = 0;
|
||||
char *tmp_read = NULL;
|
||||
uint16_t value = 0, line_breaks = 0;
|
||||
char bvalue;
|
||||
|
||||
bool utf16 = false;
|
||||
|
||||
tmp_file = fopen(filename, "rb");
|
||||
if (tmp_file == NULL) {
|
||||
blog(LOG_WARNING, "Failed to open file %s", filename);
|
||||
return;
|
||||
}
|
||||
fread(&value, 2, 1, tmp_file);
|
||||
|
||||
if (value == 0xFEFF)
|
||||
utf16 = true;
|
||||
|
||||
fseek(tmp_file, 0, SEEK_END);
|
||||
filesize = (uint32_t)ftell(tmp_file);
|
||||
cur_pos = filesize;
|
||||
|
||||
while (line_breaks <= 6 && cur_pos != 0) {
|
||||
if (!utf16) cur_pos--;
|
||||
else cur_pos -= 2;
|
||||
fseek(tmp_file, cur_pos, SEEK_SET);
|
||||
|
||||
if (!utf16) {
|
||||
fread(&bvalue, 1, 1, tmp_file);
|
||||
if (bvalue == '\n')
|
||||
line_breaks++;
|
||||
}
|
||||
else {
|
||||
fread(&value, 2, 1, tmp_file);
|
||||
if (value == L'\n')
|
||||
line_breaks++;
|
||||
}
|
||||
}
|
||||
|
||||
if (cur_pos != 0)
|
||||
cur_pos += (utf16) ? 2 : 1;
|
||||
|
||||
fseek(tmp_file, cur_pos, SEEK_SET);
|
||||
|
||||
if (utf16) {
|
||||
if (srcdata->text != NULL) {
|
||||
bfree(srcdata->text);
|
||||
srcdata->text = NULL;
|
||||
}
|
||||
srcdata->text = bzalloc(filesize - cur_pos);
|
||||
fread(srcdata->text, (filesize - cur_pos), 1, tmp_file);
|
||||
|
||||
srcdata->m_timestamp =
|
||||
get_modified_timestamp(srcdata->text_file);
|
||||
bfree(tmp_read);
|
||||
fclose(tmp_file);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
tmp_read = bzalloc((filesize - cur_pos) + 1);
|
||||
fread(tmp_read, filesize - cur_pos, 1, tmp_file);
|
||||
fclose(tmp_file);
|
||||
|
||||
if (srcdata->text != NULL) {
|
||||
bfree(srcdata->text);
|
||||
srcdata->text = NULL;
|
||||
}
|
||||
srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t));
|
||||
os_utf8_to_wcs(tmp_read, strlen(tmp_read),
|
||||
srcdata->text, (strlen(tmp_read) + 1));
|
||||
|
||||
srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file);
|
||||
bfree(tmp_read);
|
||||
}
|
||||
|
||||
uint32_t get_ft2_text_width(wchar_t *text, struct ft2_source *srcdata)
|
||||
{
|
||||
FT_GlyphSlot slot = srcdata->font_face->glyph;
|
||||
FT_UInt glyph_index = 0;
|
||||
uint32_t w = 0, max_w = 0;
|
||||
|
||||
for (uint32_t i = 0; i < (uint32_t)wcslen(text); i++) {
|
||||
glyph_index = FT_Get_Char_Index(srcdata->font_face, text[i]);
|
||||
FT_Load_Glyph(srcdata->font_face, glyph_index, FT_LOAD_DEFAULT);
|
||||
|
||||
if (text[i] == L'\n') w = 0;
|
||||
else {
|
||||
w += slot->advance.x >> 6;
|
||||
if (w > max_w) max_w = w;
|
||||
}
|
||||
}
|
||||
|
||||
return max_w;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user