lint: format a lot more files

Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
This commit is contained in:
Patrizio Bekerle 2025-04-07 21:34:41 +02:00
parent 9c033736de
commit 097caef2ec
No known key found for this signature in database
GPG key ID: 75960E6926556207
222 changed files with 52271 additions and 62384 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -6,12 +6,12 @@ Searching system installed Botan 2 library using PkgConfig
#]] #]]
if(MSVC OR MINGW) if(MSVC OR MINGW)
message(FATAL_ERROR "Using system Botan 2 library in Window not supported") message(FATAL_ERROR "Using system Botan 2 library in Window not supported")
endif() endif()
if(TARGET Botan2::Botan2) if(TARGET Botan2::Botan2)
message(STATUS "Target Botan::Botan already exists. Skipping searching") message(STATUS "Target Botan::Botan already exists. Skipping searching")
return() return()
endif() endif()
find_package(PkgConfig REQUIRED QUIET) find_package(PkgConfig REQUIRED QUIET)

View file

@ -1,137 +1,97 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
project(Botan) project(Botan)
option(BUILD_WITH_SYSTEM_BOTAN "Build using system installed Botan 2 crypto library" OFF) option(BUILD_WITH_SYSTEM_BOTAN
"Build using system installed Botan 2 crypto library" OFF)
add_library(botan STATIC "")
add_library(Botan::Botan ALIAS botan) add_library(botan STATIC "")
add_library(Botan::Botan ALIAS botan)
target_sources(botan PRIVATE
botanwrapper.h target_sources(botan PRIVATE botanwrapper.h botanwrapper.cpp)
botanwrapper.cpp
) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core) target_link_libraries(botan PUBLIC Qt${QT_VERSION_MAJOR}::Core)
target_link_libraries(botan PUBLIC Qt${QT_VERSION_MAJOR}::Core) target_include_directories(botan PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(botan PUBLIC ${CMAKE_CURRENT_LIST_DIR}) if(BUILD_WITH_SYSTEM_BOTAN)
find_package(Botan2 REQUIRED)
if(BUILD_WITH_SYSTEM_BOTAN) if(NOT TARGET Botan2::Botan2)
find_package(Botan2 REQUIRED) message(FATAL_ERROR "Could not find system Botan 2 library using PkgConfig")
if(NOT TARGET Botan2::Botan2) endif()
message(FATAL_ERROR "Could not find system Botan 2 library using PkgConfig")
endif() target_link_libraries(botan PUBLIC Botan2::Botan2)
target_link_libraries(botan PUBLIC Botan2::Botan2) target_compile_definitions(botan PUBLIC USE_SYSTEM_BOTAN)
target_compile_definitions(botan PUBLIC USE_SYSTEM_BOTAN) # Instead of put next lines into else() block it is better to just return
return()
# Instead of put next lines into else() block endif()
# it is better to just return
return() target_sources(botan PRIVATE botan.h botan.cpp botan_internal.h)
endif()
# add some Botan defines for Linux
if(UNIX)
target_sources(botan PRIVATE # Append some definitions as PRIVATE only if they used only in cpp and
botan.h # botan_internal header
botan.cpp target_compile_definitions(botan PRIVATE BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
botan_internal.h endif()
) if(CMAKE_SYSTEM_NAME MATCHES "Linux")
target_compile_definitions(
botan PRIVATE BOTAN_TARGET_OS_HAS_CLOCK_GETTIME
# add some Botan defines for Linux BOTAN_TARGET_OS_HAS_POSIX_MLOCK BOTAN_TARGET_OS_HAS_POSIX1)
if(UNIX) endif()
# Append some definitions as PRIVATE only if(APPLE)
# if they used only in cpp and botan_internal header target_compile_definitions(botan PRIVATE BOTAN_TARGET_OS_HAS_POSIX1)
target_compile_definitions(botan PRIVATE endif()
BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM
) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
endif() target_compile_definitions(botan PUBLIC BOTAN_BUILD_COMPILER_IS_GCC)
if(CMAKE_SYSTEM_NAME MATCHES "Linux") elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_definitions(botan PRIVATE target_compile_definitions(botan PUBLIC BOTAN_BUILD_COMPILER_IS_CLANG)
BOTAN_TARGET_OS_HAS_CLOCK_GETTIME elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
BOTAN_TARGET_OS_HAS_POSIX_MLOCK target_compile_definitions(botan PRIVATE BOTAN_BUILD_COMPILER_IS_INTEL)
BOTAN_TARGET_OS_HAS_POSIX1 endif()
)
endif() if(CMAKE_SYSTEM_PROCESSOR MATCHES "(i386|i686|x86_64|AMD64)")
if(APPLE) if(CMAKE_SIZEOF_VOID_P EQUAL 4)
target_compile_definitions(botan PRIVATE target_compile_definitions(botan PUBLIC BOTAN_TARGET_CPU_IS_X86_FAMILY
BOTAN_TARGET_OS_HAS_POSIX1 BOTAN_TARGET_ARCH_IS_X86_32)
) elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
endif() target_compile_definitions(
botan PUBLIC BOTAN_TARGET_CPU_IS_X86_FAMILY BOTAN_TARGET_ARCH_IS_X86_64
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") BOTAN_TARGET_CPU_HAS_NATIVE_64BIT)
target_compile_definitions(botan PUBLIC BOTAN_BUILD_COMPILER_IS_GCC) endif()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") endif()
target_compile_definitions(botan PUBLIC BOTAN_BUILD_COMPILER_IS_CLANG)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel") if(WIN32)
target_compile_definitions(botan PRIVATE BOTAN_BUILD_COMPILER_IS_INTEL) target_compile_definitions(
endif() botan PRIVATE BOTAN_TARGET_OS_IS_WINDOWS BOTAN_TARGET_OS_HAS_WIN32
BOTAN_HAS_ENTROPY_SRC_WIN32)
if(MSVC)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(i386|i686|x86_64|AMD64)") # TODO Find analog for: QMAKE_CXXFLAGS_EXCEPTIONS_ON = -EHs
if(CMAKE_SIZEOF_VOID_P EQUAL 4) target_compile_options(botan PUBLIC "-EHs" "-wd4251" "-wd4290" "-wd4250")
target_compile_definitions(botan PUBLIC target_compile_definitions(botan PRIVATE _SCL_SECURE_NO_WARNINGS
BOTAN_TARGET_CPU_IS_X86_FAMILY _CRT_SECURE_NO_WARNINGS)
BOTAN_TARGET_ARCH_IS_X86_32 target_compile_definitions(botan PUBLIC BOTAN_BUILD_COMPILER_IS_MSVC)
) if(NOT (MSVC_VERSION LESS 1900))
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8) # For included library binary ABI does not matter so disable warning
target_compile_definitions(botan PUBLIC target_compile_definitions(botan PRIVATE _ENABLE_EXTENDED_ALIGNED_STORAGE)
BOTAN_TARGET_CPU_IS_X86_FAMILY endif()
BOTAN_TARGET_ARCH_IS_X86_64 else()
BOTAN_TARGET_CPU_HAS_NATIVE_64BIT target_compile_options(botan PRIVATE "-fpermissive" "-finline-functions"
) "-Wno-long-long")
endif() endif()
endif() endif()
if(WIN32) if(UNIX AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_definitions(botan PRIVATE set_target_properties(botan PROPERTIES POSITION_INDEPENDENT_CODE ON)
BOTAN_TARGET_OS_IS_WINDOWS target_compile_options(botan PRIVATE "-fpermissive" "-finline-functions"
BOTAN_TARGET_OS_HAS_WIN32 "-Wno-long-long")
BOTAN_HAS_ENTROPY_SRC_WIN32 endif()
)
if(MSVC) if(CMAKE_SYSTEM_NAME MATCHES "(Linux|FreeBSD)")
# TODO Find analog for: target_link_libraries(botan PRIVATE rt)
# QMAKE_CXXFLAGS_EXCEPTIONS_ON = -EHs target_compile_definitions(botan PRIVATE BOTAN_TARGET_OS_HAS_GETAUXVAL)
target_compile_options(botan PUBLIC endif()
"-EHs" "-wd4251" "-wd4290" "-wd4250"
)
target_compile_definitions(botan PRIVATE
_SCL_SECURE_NO_WARNINGS
_CRT_SECURE_NO_WARNINGS
)
target_compile_definitions(botan PUBLIC
BOTAN_BUILD_COMPILER_IS_MSVC
)
if(NOT (MSVC_VERSION LESS 1900))
# For included library binary ABI does not matter
# so disable warning
target_compile_definitions(botan PRIVATE
_ENABLE_EXTENDED_ALIGNED_STORAGE
)
endif()
else()
target_compile_options(botan PRIVATE
"-fpermissive" "-finline-functions" "-Wno-long-long"
)
endif()
endif()
if(UNIX AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set_target_properties(botan PROPERTIES
POSITION_INDEPENDENT_CODE ON
)
target_compile_options(botan PRIVATE
"-fpermissive" "-finline-functions" "-Wno-long-long"
)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "(Linux|FreeBSD)")
target_link_libraries(botan PRIVATE
rt
)
target_compile_definitions(botan PRIVATE
BOTAN_TARGET_OS_HAS_GETAUXVAL
)
endif()

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -3,16 +3,16 @@
#include <QDebug> #include <QDebug>
#ifdef USE_SYSTEM_BOTAN #ifdef USE_SYSTEM_BOTAN
#include <botan/pipe.h>
#include <botan/cipher_mode.h>
#include <botan/base64.h> #include <botan/base64.h>
#include <botan/cipher_mode.h>
#include <botan/filters.h> #include <botan/filters.h>
#include <botan/pbkdf2.h>
#include <botan/kdf.h>
#include <botan/hmac.h> #include <botan/hmac.h>
#include <botan/kdf.h>
#include <botan/pbkdf2.h>
#include <botan/pipe.h>
#include <botan/secmem.h>
#include <botan/sha160.h> #include <botan/sha160.h>
#include <botan/types.h> #include <botan/types.h>
#include <botan/secmem.h>
#else #else
#include "botan.h" #include "botan.h"
#endif #endif
@ -54,16 +54,15 @@ QString BotanWrapper::Encrypt(const QString &Data) {
mSalt.data(); mSalt.data();
// Create the master key // Create the master key
Botan::SecureVector<Botan::byte> mMaster = pbkdf2.derive_key( Botan::SecureVector<Botan::byte> mMaster =
48, pbkdf2
mPassword.toStdString(), .derive_key(48, mPassword.toStdString(), &mSalt[0], mSalt.size(), PBKDF2_ITERATIONS)
&mSalt[0], mSalt.size(), .bits_of();
PBKDF2_ITERATIONS).bits_of();
Botan::SymmetricKey mKey = kdf->derive_key(32, mMaster, "salt1"); Botan::SymmetricKey mKey = kdf->derive_key(32, mMaster, "salt1");
Botan::InitializationVector mIV = kdf->derive_key(16, mMaster, "salt2"); Botan::InitializationVector mIV = kdf->derive_key(16, mMaster, "salt2");
Botan::Pipe pipe(get_cipher("AES-256/CBC/PKCS7", mKey, mIV, Botan::ENCRYPTION), Botan::Pipe pipe(get_cipher("AES-256/CBC/PKCS7", mKey, mIV, Botan::ENCRYPTION),
new Botan::Base64_Encoder); new Botan::Base64_Encoder);
pipe.process_msg(Data.toStdString()); pipe.process_msg(Data.toStdString());
QString Value = QString::fromStdString(pipe.read_all_as_string(0)); QString Value = QString::fromStdString(pipe.read_all_as_string(0));
return Value; return Value;
@ -75,29 +74,27 @@ QString BotanWrapper::Encrypt(const QString &Data) {
QString BotanWrapper::Decrypt(const QString &Data) { QString BotanWrapper::Decrypt(const QString &Data) {
try { try {
//Setup the key derive functions // Setup the key derive functions
Botan::PKCS5_PBKDF2 pbkdf2(new Botan::HMAC(new Botan::SHA_160)); Botan::PKCS5_PBKDF2 pbkdf2(new Botan::HMAC(new Botan::SHA_160));
const uint32_t PBKDF2_ITERATIONS = 8192; const uint32_t PBKDF2_ITERATIONS = 8192;
//Create the KEY and IV // Create the KEY and IV
Botan::KDF *kdf = Botan::get_kdf("KDF2(SHA-1)"); Botan::KDF *kdf = Botan::get_kdf("KDF2(SHA-1)");
//Create the master key // Create the master key
Botan::SecureVector<Botan::byte> mMaster = pbkdf2.derive_key( Botan::SecureVector<Botan::byte> mMaster =
48, pbkdf2
mPassword.toStdString(), .derive_key(48, mPassword.toStdString(), &mSalt[0], mSalt.size(), PBKDF2_ITERATIONS)
&mSalt[0], mSalt.size(), .bits_of();
PBKDF2_ITERATIONS).bits_of();
Botan::SymmetricKey mKey = kdf->derive_key(32, mMaster, "salt1"); Botan::SymmetricKey mKey = kdf->derive_key(32, mMaster, "salt1");
Botan::InitializationVector mIV = kdf->derive_key(16, mMaster, "salt2"); Botan::InitializationVector mIV = kdf->derive_key(16, mMaster, "salt2");
Botan::Pipe pipe(new Botan::Base64_Decoder, Botan::Pipe pipe(new Botan::Base64_Decoder,
get_cipher("AES-256/CBC/PKCS7", mKey, mIV, Botan::DECRYPTION)); get_cipher("AES-256/CBC/PKCS7", mKey, mIV, Botan::DECRYPTION));
pipe.process_msg(Data.toStdString()); pipe.process_msg(Data.toStdString());
QString Value = QString::fromStdString(pipe.read_all_as_string(0)); QString Value = QString::fromStdString(pipe.read_all_as_string(0));
return Value; return Value;
} } catch (Botan::Exception &e) {
catch (Botan::Exception &e) {
qWarning() << "[Botan Error] " << e.what(); qWarning() << "[Botan Error] " << e.what();
return QString(); return QString();
} }
@ -111,8 +108,7 @@ void BotanWrapper::setPassword(const QString &Password) {
void BotanWrapper::setSalt(const QString &Salt) { void BotanWrapper::setSalt(const QString &Salt) {
QByteArray cBytes = Salt.toLatin1(); QByteArray cBytes = Salt.toLatin1();
if (cBytes.size() > 48) if (cBytes.size() > 48) cBytes.resize(48);
cBytes.resize(48);
mSalt.fill(0); mSalt.fill(0);
std::copy(cBytes.cbegin(), cBytes.cend(), mSalt.begin()); std::copy(cBytes.cbegin(), cBytes.cend(), mSalt.begin());

View file

@ -4,44 +4,43 @@
#include <QString> #include <QString>
#include <QVector> #include <QVector>
class BotanWrapper class BotanWrapper {
{ public:
public:
BotanWrapper(); BotanWrapper();
/*! /*!
* Creates a hash * Creates a hash
* @param Data The string to hash * @param Data The string to hash
*/ */
QString Hash(const QString &Data); QString Hash(const QString &Data);
/*! /*!
* Returns a Base64 encrypted QString * Returns a Base64 encrypted QString
* @param Data The string to encypt * @param Data The string to encypt
*/ */
QString Encrypt(const QString &Data); QString Encrypt(const QString &Data);
/*! /*!
* Returns a decrypted string from a Base64 encypted string * Returns a decrypted string from a Base64 encypted string
* @param Data The string to encypt * @param Data The string to encypt
*/ */
QString Decrypt(const QString &Data); QString Decrypt(const QString &Data);
/*! /*!
* Sets the Password * Sets the Password
* @param Password The password * @param Password The password
*/ */
void setPassword(const QString &Password); void setPassword(const QString &Password);
/*! /*!
* Sets the Salt * Sets the Salt
* @param Salt The salt value * @param Salt The salt value
*/ */
void setSalt(const QString &Salt); void setSalt(const QString &Salt);
private: private:
QVector<uint8_t> mSalt; QVector<uint8_t> mSalt;
QString mPassword; QString mPassword;
}; };
#endif // BOTANWRAPPER_H #endif // BOTANWRAPPER_H

View file

@ -1,19 +1,12 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
project(DiffMatchPatchLib) project(DiffMatchPatchLib)
add_library(diff_match_patch STATIC add_library(diff_match_patch STATIC diff_match_patch.h diff_match_patch.cpp)
diff_match_patch.h
diff_match_patch.cpp
)
add_library(Google::DiffMatchPatch ALIAS diff_match_patch) add_library(Google::DiffMatchPatch ALIAS diff_match_patch)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
target_include_directories(diff_match_patch PUBLIC target_include_directories(diff_match_patch PUBLIC ${CMAKE_CURRENT_LIST_DIR})
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(diff_match_patch PUBLIC target_link_libraries(diff_match_patch PUBLIC Qt${QT_VERSION_MAJOR}::Core)
Qt${QT_VERSION_MAJOR}::Core
)

View file

@ -5,20 +5,13 @@ project(fakevim VERSION 0.0.1)
set(bin fakevim) set(bin fakevim)
# Public headers # Public headers
set(${bin}_public_headers set(${bin}_public_headers fakevim/fakevimactions.h fakevim/fakevimhandler.h)
fakevim/fakevimactions.h
fakevim/fakevimhandler.h
)
# Source files common for all platforms # Source files common for all platforms
set(${bin}_sources set(${bin}_sources
fakevim/fakevimactions.cpp fakevim/fakevimactions.cpp fakevim/fakevimhandler.cpp
fakevim/fakevimhandler.cpp fakevim/utils/hostosinfo.cpp fakevim/utils/osspecificaspects.h
fakevim/utils/hostosinfo.cpp fakevim/utils/qtcassert.cpp ${${bin}_public_headers})
fakevim/utils/osspecificaspects.h
fakevim/utils/qtcassert.cpp
${${bin}_public_headers}
)
# Required pkg-config packages # Required pkg-config packages
set(${bin}_pkg_config_requires) set(${bin}_pkg_config_requires)
@ -30,49 +23,42 @@ include(cmake/pkg-config.cmake)
# Files with Q_OBJECT macros to pass to moc utility # Files with Q_OBJECT macros to pass to moc utility
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
if (QT_VERSION_MAJOR EQUAL 6) if(QT_VERSION_MAJOR EQUAL 6)
qt6_wrap_cpp(${bin}_mocced "fakevim/fakevimhandler.h") qt6_wrap_cpp(${bin}_mocced "fakevim/fakevimhandler.h")
else() else()
qt5_wrap_cpp(${bin}_mocced "fakevim/fakevimhandler.h") qt5_wrap_cpp(${bin}_mocced "fakevim/fakevimhandler.h")
endif() endif()
target_sources(${bin} PRIVATE ${${bin}_mocced}) target_sources(${bin} PRIVATE ${${bin}_mocced})
target_compile_definitions(${bin} PRIVATE target_compile_definitions(
QT_NO_CAST_TO_ASCII ${bin} PRIVATE QT_NO_CAST_TO_ASCII QT_RESTRICTED_CAST_FROM_ASCII
QT_RESTRICTED_CAST_FROM_ASCII QTCREATOR_UTILS_STATIC_LIB)
QTCREATOR_UTILS_STATIC_LIB
)
option(BUILD_TESTS "Build tests") option(BUILD_TESTS "Build tests")
if (BUILD_TESTS) if(BUILD_TESTS)
message(STATUS "Building tests") message(STATUS "Building tests")
find_package(Qt5Test REQUIRED) find_package(Qt5Test REQUIRED)
add_executable(fakevim_test add_executable(fakevim_test tests/fakevim_test.cpp tests/fakevimplugin.h
tests/fakevim_test.cpp example/editor.cpp)
tests/fakevimplugin.h set_property(TARGET fakevim_test PROPERTY AUTOMOC ON)
example/editor.cpp target_link_libraries(fakevim_test fakevim Qt5::Widgets Qt5::Test)
)
set_property(TARGET fakevim_test PROPERTY AUTOMOC ON)
target_link_libraries(fakevim_test fakevim Qt5::Widgets Qt5::Test)
target_include_directories(fakevim_test PRIVATE target_include_directories(fakevim_test PRIVATE ${CMAKE_SOURCE_DIR}/fakevim
${CMAKE_SOURCE_DIR}/fakevim ${CMAKE_SOURCE_DIR})
${CMAKE_SOURCE_DIR}
)
add_test(fakevim_test fakevim_test) add_test(fakevim_test fakevim_test)
enable_testing() enable_testing()
endif() endif()
option(BUILD_EXAMPLE "Build example") option(BUILD_EXAMPLE "Build example")
if (BUILD_EXAMPLE) if(BUILD_EXAMPLE)
message(STATUS "Building example") message(STATUS "Building example")
add_executable(fakevim_example example/main.cpp example/editor.cpp) add_executable(fakevim_example example/main.cpp example/editor.cpp)
set_property(TARGET fakevim_example PROPERTY AUTOMOC ON) set_property(TARGET fakevim_example PROPERTY AUTOMOC ON)
target_link_libraries(fakevim_example fakevim) target_link_libraries(fakevim_example fakevim)
target_link_libraries(fakevim_example Qt5::Widgets) target_link_libraries(fakevim_example Qt5::Widgets)
endif() endif()

View file

@ -1,5 +1,5 @@
if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1) if(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
else() else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
endif() endif()

View file

@ -1,83 +1,71 @@
set(INSTALL_LIB_DIR "lib${LIB_SUFFIX}" CACHE PATH set(INSTALL_LIB_DIR
"Installation directory for libraries") "lib${LIB_SUFFIX}"
set(INSTALL_BIN_DIR "bin" CACHE PATH CACHE PATH "Installation directory for libraries")
"Installation directory for executables") set(INSTALL_BIN_DIR
set(INSTALL_INCLUDE_DIR "include/${bin}" CACHE PATH "bin"
"Installation directory for header files") CACHE PATH "Installation directory for executables")
set(INSTALL_CMAKE_DIR "lib${LIB_SUFFIX}/cmake/${bin}" CACHE PATH set(INSTALL_INCLUDE_DIR
"Installation directory for CMake files") "include/${bin}"
CACHE PATH "Installation directory for header files")
set(INSTALL_CMAKE_DIR
"lib${LIB_SUFFIX}/cmake/${bin}"
CACHE PATH "Installation directory for CMake files")
# Shared or static library # Shared or static library
option(CREATE_STATIC_LIBRARY "Create static library" OFF) option(CREATE_STATIC_LIBRARY "Create static library" OFF)
if (CREATE_STATIC_LIBRARY) if(CREATE_STATIC_LIBRARY)
set(libtype STATIC) set(libtype STATIC)
else() else()
set(libtype SHARED) set(libtype SHARED)
endif() endif()
add_library(${bin} ${libtype} ${${bin}_sources}) add_library(${bin} ${libtype} ${${bin}_sources})
set_target_properties(${bin} PROPERTIES set_target_properties(${bin} PROPERTIES VERSION ${PROJECT_VERSION}
VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
SOVERSION ${PROJECT_VERSION_MAJOR}
)
# Headers # Headers
set(export_header "${CMAKE_CURRENT_BINARY_DIR}/private/${bin}_export.h") set(export_header "${CMAKE_CURRENT_BINARY_DIR}/private/${bin}_export.h")
set_target_properties(${bin} PROPERTIES set_target_properties(${bin} PROPERTIES PUBLIC_HEADER "${${bin}_public_headers}"
PUBLIC_HEADER "${${bin}_public_headers}" PRIVATE_HEADER "${export_header}")
PRIVATE_HEADER "${export_header}")
target_include_directories(${bin} target_include_directories(
PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${bin}> ${bin}
INTERFACE $<INSTALL_INTERFACE:include/${bin}> PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${bin}>
) INTERFACE $<INSTALL_INTERFACE:include/${bin}>)
install( install(
TARGETS ${bin} EXPORT ${bin}Targets TARGETS ${bin}
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT bin EXPORT ${bin}Targets
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT lib RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT bin
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" COMPONENT lib LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT lib
PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}" COMPONENT dev ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" COMPONENT lib
PRIVATE_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}/private" COMPONENT dev PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}" COMPONENT dev
) PRIVATE_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}/private" COMPONENT dev)
# Generate and install CMake files for the library so `find_package(<Library>)` can be used with CMake. # Generate and install CMake files for the library so `find_package(<Library>)`
# For more info: https://cmake.org/cmake/help/v3.0/manual/cmake-packages.7.html#creating-packages # can be used with CMake. For more info:
# https://cmake.org/cmake/help/v3.0/manual/cmake-packages.7.html#creating-packages
include(GenerateExportHeader) include(GenerateExportHeader)
generate_export_header(${bin} EXPORT_FILE_NAME "${export_header}") generate_export_header(${bin} EXPORT_FILE_NAME "${export_header}")
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
write_basic_package_version_file( write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${bin}ConfigVersion.cmake" "${CMAKE_CURRENT_BINARY_DIR}/${bin}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION} VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion COMPATIBILITY AnyNewerVersion)
)
export( export(EXPORT ${bin}Targets
EXPORT ${bin}Targets FILE "${CMAKE_CURRENT_BINARY_DIR}/${bin}Targets.cmake")
FILE "${CMAKE_CURRENT_BINARY_DIR}/${bin}Targets.cmake" configure_file(cmake/${bin}Config.cmake
) "${CMAKE_CURRENT_BINARY_DIR}/${bin}Config.cmake" COPYONLY)
configure_file(
cmake/${bin}Config.cmake
"${CMAKE_CURRENT_BINARY_DIR}/${bin}Config.cmake"
COPYONLY
)
install( install(
EXPORT ${bin}Targets EXPORT ${bin}Targets
FILE FILE ${bin}Targets.cmake
${bin}Targets.cmake DESTINATION "${INSTALL_CMAKE_DIR}"
DESTINATION COMPONENT dev)
"${INSTALL_CMAKE_DIR}"
COMPONENT
dev
)
install( install(
FILES FILES "${CMAKE_CURRENT_BINARY_DIR}/${bin}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${bin}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${bin}ConfigVersion.cmake" "${CMAKE_CURRENT_BINARY_DIR}/${bin}ConfigVersion.cmake"
DESTINATION DESTINATION "${INSTALL_CMAKE_DIR}"
"${INSTALL_CMAKE_DIR}" COMPONENT dev)
COMPONENT
dev
)

View file

@ -4,19 +4,14 @@ set(PKG_CONFIG_INCLUDEDIR "\${prefix}/include/${bin}")
set(PKG_CONFIG_LIBS "-L\${libdir} -l${bin}") set(PKG_CONFIG_LIBS "-L\${libdir} -l${bin}")
set(PKG_CONFIG_CFLAGS "-I\${includedir}") set(PKG_CONFIG_CFLAGS "-I\${includedir}")
#configure_file( # configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/pkg-config.pc.in"
# "${CMAKE_CURRENT_SOURCE_DIR}/pkg-config.pc.in" # "${CMAKE_CURRENT_BINARY_DIR}/${bin}.pc" )
# "${CMAKE_CURRENT_BINARY_DIR}/${bin}.pc"
# )
set(INSTALL_PKG_CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig" set(INSTALL_PKG_CONFIG_DIR
"${CMAKE_INSTALL_PREFIX}/lib/pkgconfig"
CACHE PATH "Installation directory for pkg-config files") CACHE PATH "Installation directory for pkg-config files")
install( install(
FILES FILES "${CMAKE_CURRENT_BINARY_DIR}/${bin}.pc"
"${CMAKE_CURRENT_BINARY_DIR}/${bin}.pc" DESTINATION "${INSTALL_PKG_CONFIG_DIR}"
DESTINATION COMPONENT dev)
"${INSTALL_PKG_CONFIG_DIR}"
COMPONENT
dev
)

View file

@ -3,4 +3,5 @@ find_package(Qt${QT_VERSION_MAJOR}Widgets REQUIRED)
# include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS}) # include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS})
target_link_libraries(${bin} Qt${QT_VERSION_MAJOR}::Widgets) target_link_libraries(${bin} Qt${QT_VERSION_MAJOR}::Widgets)
set(${bin}_pkg_config_requires ${${bin}_pkg_config_requires} Qt${QT_VERSION_MAJOR}::Widgets) set(${bin}_pkg_config_requires ${${bin}_pkg_config_requires}
Qt${QT_VERSION_MAJOR}::Widgets)

View file

@ -24,6 +24,7 @@
****************************************************************************/ ****************************************************************************/
#include "fakevimactions.h" #include "fakevimactions.h"
#include "fakevimhandler.h" #include "fakevimhandler.h"
// Please do not add any direct dependencies to other Qt Creator code here. // Please do not add any direct dependencies to other Qt Creator code here.
@ -41,80 +42,62 @@ namespace FakeVim {
namespace Internal { namespace Internal {
#ifdef FAKEVIM_STANDALONE #ifdef FAKEVIM_STANDALONE
FvBaseAspect::FvBaseAspect() FvBaseAspect::FvBaseAspect() {}
{
}
void FvBaseAspect::setValue(const QVariant &value) void FvBaseAspect::setValue(const QVariant &value) { m_value = value; }
{
m_value = value;
}
QVariant FvBaseAspect::value() const QVariant FvBaseAspect::value() const { return m_value; }
{
return m_value;
}
void FvBaseAspect::setDefaultValue(const QVariant &value) void FvBaseAspect::setDefaultValue(const QVariant &value) {
{
m_defaultValue = value; m_defaultValue = value;
m_value = value; m_value = value;
} }
QVariant FvBaseAspect::defaultValue() const QVariant FvBaseAspect::defaultValue() const { return m_defaultValue; }
{
return m_defaultValue;
}
void FvBaseAspect::setSettingsKey(const QString &group, const QString &key) void FvBaseAspect::setSettingsKey(const QString &group, const QString &key) {
{
m_settingsGroup = group; m_settingsGroup = group;
m_settingsKey = key; m_settingsKey = key;
} }
QString FvBaseAspect::settingsKey() const QString FvBaseAspect::settingsKey() const { return m_settingsKey; }
{
return m_settingsKey;
}
#endif #endif
FakeVimSettings::FakeVimSettings() FakeVimSettings::FakeVimSettings() {
{
#ifndef FAKEVIM_STANDALONE #ifndef FAKEVIM_STANDALONE
setup(&useFakeVim, false, "UseFakeVim", {}, tr("Use FakeVim")); setup(&useFakeVim, false, "UseFakeVim", {}, tr("Use FakeVim"));
#endif #endif
// Specific FakeVim settings // Specific FakeVim settings
setup(&readVimRc, false, "ReadVimRc", {}, tr("Read .vimrc from location:")); setup(&readVimRc, false, "ReadVimRc", {}, tr("Read .vimrc from location:"));
setup(&vimRcPath, QString(), "VimRcPath", {}, {}); // tr("Path to .vimrc") setup(&vimRcPath, QString(), "VimRcPath", {}, {}); // tr("Path to .vimrc")
setup(&showMarks, false, "ShowMarks", "sm", tr("Show position of text marks")); setup(&showMarks, false, "ShowMarks", "sm", tr("Show position of text marks"));
setup(&passControlKey, false, "PassControlKey", "pck", tr("Pass control keys")); setup(&passControlKey, false, "PassControlKey", "pck", tr("Pass control keys"));
setup(&passKeys, true, "PassKeys", "pk", tr("Pass keys in insert mode")); setup(&passKeys, true, "PassKeys", "pk", tr("Pass keys in insert mode"));
// Emulated Vsetting // Emulated Vsetting
setup(&startOfLine, true, "StartOfLine", "sol", tr("Start of line")); setup(&startOfLine, true, "StartOfLine", "sol", tr("Start of line"));
setup(&tabStop, 8, "TabStop", "ts", tr("Tabulator size:")); setup(&tabStop, 8, "TabStop", "ts", tr("Tabulator size:"));
setup(&smartTab, false, "SmartTab", "sta", tr("Smart tabulators")); setup(&smartTab, false, "SmartTab", "sta", tr("Smart tabulators"));
setup(&hlSearch, true, "HlSearch", "hls", tr("Highlight search results")); setup(&hlSearch, true, "HlSearch", "hls", tr("Highlight search results"));
setup(&shiftWidth, 8, "ShiftWidth", "sw", tr("Shift width:")); setup(&shiftWidth, 8, "ShiftWidth", "sw", tr("Shift width:"));
setup(&expandTab, false, "ExpandTab", "et", tr("Expand tabulators")); setup(&expandTab, false, "ExpandTab", "et", tr("Expand tabulators"));
setup(&autoIndent, false, "AutoIndent", "ai", tr("Automatic indentation")); setup(&autoIndent, false, "AutoIndent", "ai", tr("Automatic indentation"));
setup(&smartIndent, false, "SmartIndent", "si", tr("Smart tabulators")); setup(&smartIndent, false, "SmartIndent", "si", tr("Smart tabulators"));
setup(&incSearch, true, "IncSearch", "is", tr("Incremental search")); setup(&incSearch, true, "IncSearch", "is", tr("Incremental search"));
setup(&useCoreSearch, false, "UseCoreSearch", "ucs", tr("Use search dialog")); setup(&useCoreSearch, false, "UseCoreSearch", "ucs", tr("Use search dialog"));
setup(&smartCase, false, "SmartCase", "scs", tr("Use smartcase")); setup(&smartCase, false, "SmartCase", "scs", tr("Use smartcase"));
setup(&ignoreCase, false, "IgnoreCase", "ic", tr("Use ignorecase")); setup(&ignoreCase, false, "IgnoreCase", "ic", tr("Use ignorecase"));
setup(&wrapScan, true, "WrapScan", "ws", tr("Use wrapscan")); setup(&wrapScan, true, "WrapScan", "ws", tr("Use wrapscan"));
setup(&tildeOp, false, "TildeOp", "top", tr("Use tildeop")); setup(&tildeOp, false, "TildeOp", "top", tr("Use tildeop"));
setup(&showCmd, true, "ShowCmd", "sc", tr("Show partial command")); setup(&showCmd, true, "ShowCmd", "sc", tr("Show partial command"));
setup(&relativeNumber, false, "RelativeNumber", "rnu", tr("Show line numbers relative to cursor")); setup(&relativeNumber, false, "RelativeNumber", "rnu",
setup(&blinkingCursor, false, "BlinkingCursor", "bc", tr("Blinking cursor")); tr("Show line numbers relative to cursor"));
setup(&scrollOff, 0, "ScrollOff", "so", tr("Scroll offset:")); setup(&blinkingCursor, false, "BlinkingCursor", "bc", tr("Blinking cursor"));
setup(&backspace, "indent,eol,start", setup(&scrollOff, 0, "ScrollOff", "so", tr("Scroll offset:"));
"Backspace", "bs", tr("Backspace:")); setup(&backspace, "indent,eol,start", "Backspace", "bs", tr("Backspace:"));
setup(&isKeyword, "@,48-57,_,192-255,a-z,A-Z", setup(&isKeyword, "@,48-57,_,192-255,a-z,A-Z", "IsKeyword", "isk", tr("Keyword characters:"));
"IsKeyword", "isk", tr("Keyword characters:")); setup(&clipboard, {}, "Clipboard", "cb", tr(""));
setup(&clipboard, {}, "Clipboard", "cb", tr("")); setup(&formatOptions, {}, "formatoptions", "fo", tr(""));
setup(&formatOptions, {}, "formatoptions", "fo", tr(""));
// Emulated plugins // Emulated plugins
setup(&emulateVimCommentary, false, "commentary", {}, "vim-commentary"); setup(&emulateVimCommentary, false, "commentary", {}, "vim-commentary");
@ -126,15 +109,18 @@ FakeVimSettings::FakeVimSettings()
// Some polish // Some polish
useFakeVim.setDisplayName(tr("Use Vim-style Editing")); useFakeVim.setDisplayName(tr("Use Vim-style Editing"));
relativeNumber.setToolTip(tr("Displays line numbers relative to the line containing " relativeNumber.setToolTip(
"text cursor.")); tr("Displays line numbers relative to the line containing "
"text cursor."));
passControlKey.setToolTip(tr("Does not interpret key sequences like Ctrl-S in FakeVim " passControlKey.setToolTip(
"but handles them as regular shortcuts. This gives easier access to core functionality " tr("Does not interpret key sequences like Ctrl-S in FakeVim "
"at the price of losing some features of FakeVim.")); "but handles them as regular shortcuts. This gives easier access to core functionality "
"at the price of losing some features of FakeVim."));
passKeys.setToolTip(tr("Does not interpret some key presses in insert mode so that " passKeys.setToolTip(
"code can be properly completed and expanded.")); tr("Does not interpret some key presses in insert mode so that "
"code can be properly completed and expanded."));
tabStop.setToolTip(tr("Vim tabstop option.")); tabStop.setToolTip(tr("Vim tabstop option."));
@ -142,11 +128,12 @@ FakeVimSettings::FakeVimSettings()
backspace.setDisplayStyle(FvStringAspect::LineEditDisplay); backspace.setDisplayStyle(FvStringAspect::LineEditDisplay);
isKeyword.setDisplayStyle(FvStringAspect::LineEditDisplay); isKeyword.setDisplayStyle(FvStringAspect::LineEditDisplay);
const QString vimrcDefault = QLatin1String(HostOsInfo::isAnyUnixHost() const QString vimrcDefault =
? "$HOME/.vimrc" : "%USERPROFILE%\\_vimrc"); QLatin1String(HostOsInfo::isAnyUnixHost() ? "$HOME/.vimrc" : "%USERPROFILE%\\_vimrc");
vimRcPath.setExpectedKind(PathChooser::File); vimRcPath.setExpectedKind(PathChooser::File);
vimRcPath.setToolTip(tr("Keep empty to use the default path, i.e. " vimRcPath.setToolTip(
"%USERPROFILE%\\_vimrc on Windows, ~/.vimrc otherwise.")); tr("Keep empty to use the default path, i.e. "
"%USERPROFILE%\\_vimrc on Windows, ~/.vimrc otherwise."));
vimRcPath.setPlaceHolderText(tr("Default: %1").arg(vimrcDefault)); vimRcPath.setPlaceHolderText(tr("Default: %1").arg(vimrcDefault));
vimRcPath.setDisplayStyle(FvStringAspect::PathChooserDisplay); vimRcPath.setDisplayStyle(FvStringAspect::PathChooserDisplay);
#endif #endif
@ -154,31 +141,22 @@ FakeVimSettings::FakeVimSettings()
FakeVimSettings::~FakeVimSettings() = default; FakeVimSettings::~FakeVimSettings() = default;
FvBaseAspect *FakeVimSettings::item(const QString &name) FvBaseAspect *FakeVimSettings::item(const QString &name) {
{
return m_nameToAspect.value(name, nullptr); return m_nameToAspect.value(name, nullptr);
} }
QString FakeVimSettings::trySetValue(const QString &name, const QString &value) QString FakeVimSettings::trySetValue(const QString &name, const QString &value) {
{
FvBaseAspect *aspect = m_nameToAspect.value(name, nullptr); FvBaseAspect *aspect = m_nameToAspect.value(name, nullptr);
if (!aspect) if (!aspect) return tr("Unknown option: %1").arg(name);
return tr("Unknown option: %1").arg(name);
if (aspect == &tabStop || aspect == &shiftWidth) { if (aspect == &tabStop || aspect == &shiftWidth) {
if (value.toInt() <= 0) if (value.toInt() <= 0) return tr("Argument must be positive: %1=%2").arg(name).arg(value);
return tr("Argument must be positive: %1=%2")
.arg(name).arg(value);
} }
aspect->setValue(value); aspect->setValue(value);
return QString(); return QString();
} }
void FakeVimSettings::setup(FvBaseAspect *aspect, void FakeVimSettings::setup(FvBaseAspect *aspect, const QVariant &value, const QString &settingsKey,
const QVariant &value, const QString &shortName, const QString &labelText) {
const QString &settingsKey,
const QString &shortName,
const QString &labelText)
{
aspect->setSettingsKey("FakeVim", settingsKey); aspect->setSettingsKey("FakeVim", settingsKey);
aspect->setDefaultValue(value); aspect->setDefaultValue(value);
#ifndef FAKEVIM_STANDALONE #ifndef FAKEVIM_STANDALONE
@ -197,15 +175,13 @@ void FakeVimSettings::setup(FvBaseAspect *aspect,
m_nameToAspect[longName] = aspect; m_nameToAspect[longName] = aspect;
m_aspectToName[aspect] = longName; m_aspectToName[aspect] = longName;
} }
if (!shortName.isEmpty()) if (!shortName.isEmpty()) m_nameToAspect[shortName] = aspect;
m_nameToAspect[shortName] = aspect;
} }
FakeVimSettings *fakeVimSettings() FakeVimSettings *fakeVimSettings() {
{
static FakeVimSettings s; static FakeVimSettings s;
return &s; return &s;
} }
} // namespace Internal } // namespace Internal
} // namespace FakeVim } // namespace FakeVim

View file

@ -28,9 +28,9 @@
#define FAKEVIM_STANDALONE #define FAKEVIM_STANDALONE
#ifdef FAKEVIM_STANDALONE #ifdef FAKEVIM_STANDALONE
//# include "private/fakevim_export.h" // # include "private/fakevim_export.h"
#else #else
# include <utils/savedaction.h> #include <utils/savedaction.h>
#endif #endif
#include <QCoreApplication> #include <QCoreApplication>
@ -43,9 +43,8 @@ namespace FakeVim {
namespace Internal { namespace Internal {
#ifdef FAKEVIM_STANDALONE #ifdef FAKEVIM_STANDALONE
class FvBaseAspect class FvBaseAspect {
{ public:
public:
FvBaseAspect(); FvBaseAspect();
virtual ~FvBaseAspect() {} virtual ~FvBaseAspect() {}
@ -59,34 +58,30 @@ public:
void setDisplayName(const QString &) {} void setDisplayName(const QString &) {}
void setToolTip(const QString &) {} void setToolTip(const QString &) {}
private: private:
QVariant m_value; QVariant m_value;
QVariant m_defaultValue; QVariant m_defaultValue;
QString m_settingsGroup; QString m_settingsGroup;
QString m_settingsKey; QString m_settingsKey;
}; };
class FvBoolAspect : public FvBaseAspect class FvBoolAspect : public FvBaseAspect {
{ public:
public:
bool value() const { return FvBaseAspect::value().toBool(); } bool value() const { return FvBaseAspect::value().toBool(); }
}; };
class FvIntegerAspect : public FvBaseAspect class FvIntegerAspect : public FvBaseAspect {
{ public:
public:
qint64 value() const { return FvBaseAspect::value().toLongLong(); } qint64 value() const { return FvBaseAspect::value().toLongLong(); }
}; };
class FvStringAspect : public FvBaseAspect class FvStringAspect : public FvBaseAspect {
{ public:
public:
QString value() const { return FvBaseAspect::value().toString(); } QString value() const { return FvBaseAspect::value().toString(); }
}; };
class FvAspectContainer : public FvBaseAspect class FvAspectContainer : public FvBaseAspect {
{ public:
public:
}; };
#else #else
@ -99,11 +94,10 @@ using FvStringAspect = Utils::StringAspect;
#endif #endif
class FakeVimSettings final : public FvAspectContainer class FakeVimSettings final : public FvAspectContainer {
{
Q_DECLARE_TR_FUNCTIONS(FakeVim) Q_DECLARE_TR_FUNCTIONS(FakeVim)
public: public:
FakeVimSettings(); FakeVimSettings();
~FakeVimSettings(); ~FakeVimSettings();
@ -160,11 +154,9 @@ public:
FvBoolAspect blinkingCursor; FvBoolAspect blinkingCursor;
private: private:
void setup(FvBaseAspect *aspect, const QVariant &value, void setup(FvBaseAspect *aspect, const QVariant &value, const QString &settingsKey,
const QString &settingsKey, const QString &shortName, const QString &label);
const QString &shortName,
const QString &label);
QHash<QString, FvBaseAspect *> m_nameToAspect; QHash<QString, FvBaseAspect *> m_nameToAspect;
QHash<FvBaseAspect *, QString> m_aspectToName; QHash<FvBaseAspect *, QString> m_aspectToName;
@ -172,5 +164,5 @@ private:
FakeVimSettings *fakeVimSettings(); FakeVimSettings *fakeVimSettings();
} // namespace Internal } // namespace Internal
} // namespace FakeVim } // namespace FakeVim

File diff suppressed because it is too large Load diff

View file

@ -28,31 +28,28 @@
#define FAKEVIM_STANDALONE #define FAKEVIM_STANDALONE
#ifdef FAKEVIM_STANDALONE #ifdef FAKEVIM_STANDALONE
//# include "private/fakevim_export.h" // # include "private/fakevim_export.h"
#endif #endif
#include <QObject> #include <QObject>
#include <QTextEdit> #include <QTextEdit>
#include <functional> #include <functional>
#include <vector> #include <vector>
namespace FakeVim { namespace FakeVim {
namespace Internal { namespace Internal {
enum RangeMode enum RangeMode {
{
// Reordering first three enum items here will break // Reordering first three enum items here will break
// compatibility with clipboard format stored by Vim. // compatibility with clipboard format stored by Vim.
RangeCharMode, // v RangeCharMode, // v
RangeLineMode, // V RangeLineMode, // V
RangeBlockMode, // Ctrl-v RangeBlockMode, // Ctrl-v
RangeLineModeExclusive, RangeLineModeExclusive,
RangeBlockAndTailMode // Ctrl-v for D and X RangeBlockAndTailMode // Ctrl-v for D and X
}; };
struct Range struct Range {
{
Range() = default; Range() = default;
Range(int b, int e, RangeMode m = RangeCharMode); Range(int b, int e, RangeMode m = RangeCharMode);
QString toString() const; QString toString() const;
@ -63,11 +60,9 @@ struct Range
RangeMode rangemode = RangeCharMode; RangeMode rangemode = RangeCharMode;
}; };
struct ExCommand struct ExCommand {
{
ExCommand() = default; ExCommand() = default;
ExCommand(const QString &cmd, const QString &args = QString(), ExCommand(const QString &cmd, const QString &args = QString(), const Range &range = Range());
const Range &range = Range());
bool matches(const QString &min, const QString &full) const; bool matches(const QString &min, const QString &full) const;
@ -79,40 +74,35 @@ struct ExCommand
}; };
// message levels sorted by severity // message levels sorted by severity
enum MessageLevel enum MessageLevel {
{ MessageMode, // show current mode (format "-- %1 --")
MessageMode, // show current mode (format "-- %1 --") MessageCommand, // show last Ex command or search
MessageCommand, // show last Ex command or search MessageInfo, // result of a command
MessageInfo, // result of a command MessageWarning, // warning
MessageWarning, // warning MessageError, // error
MessageError, // error MessageShowCmd // partial command
MessageShowCmd // partial command
}; };
template <typename Type> template <typename Type>
class Signal class Signal {
{ public:
public:
using Callable = std::function<Type>; using Callable = std::function<Type>;
void connect(const Callable &callable) { m_callables.push_back(callable); } void connect(const Callable &callable) { m_callables.push_back(callable); }
template <typename ...Args> template <typename... Args>
void operator()(Args ...args) const void operator()(Args... args) const {
{ for (const Callable &callable : m_callables) callable(args...);
for (const Callable &callable : m_callables) }
callable(args...);
}
private: private:
std::vector<Callable> m_callables; std::vector<Callable> m_callables;
}; };
class FakeVimHandler : public QObject class FakeVimHandler : public QObject {
{
Q_OBJECT Q_OBJECT
public: public:
explicit FakeVimHandler(QWidget *widget, QObject *parent = nullptr); explicit FakeVimHandler(QWidget *widget, QObject *parent = nullptr);
~FakeVimHandler() override; ~FakeVimHandler() override;
@ -123,7 +113,7 @@ public:
static void updateGlobalMarksFilenames(const QString &oldFileName, const QString &newFileName); static void updateGlobalMarksFilenames(const QString &oldFileName, const QString &newFileName);
public: public:
void setCurrentFileName(const QString &fileName); void setCurrentFileName(const QString &fileName);
QString currentFileName() const; QString currentFileName() const;
@ -159,11 +149,12 @@ public:
bool eventFilter(QObject *ob, QEvent *ev) override; bool eventFilter(QObject *ob, QEvent *ev) override;
Signal<void(const QString &msg, int cursorPos, int anchorPos, int messageLevel)> commandBufferChanged; Signal<void(const QString &msg, int cursorPos, int anchorPos, int messageLevel)>
commandBufferChanged;
Signal<void(const QString &msg)> statusDataChanged; Signal<void(const QString &msg)> statusDataChanged;
Signal<void(const QString &msg)> extraInformationChanged; Signal<void(const QString &msg)> extraInformationChanged;
Signal<void(const QList<QTextEdit::ExtraSelection> &selection)> selectionChanged; Signal<void(const QList<QTextEdit::ExtraSelection> &selection)> selectionChanged;
Signal<void(const QString &needle)> highlightMatches; Signal<void(const QString &needle)> highlightMatches;
Signal<void(bool *moved, bool *forward, QTextCursor *cursor)> moveToMatchingParenthesis; Signal<void(bool *moved, bool *forward, QTextCursor *cursor)> moveToMatchingParenthesis;
Signal<void(bool *result, QChar c)> checkForElectricCharacter; Signal<void(bool *result, QChar c)> checkForElectricCharacter;
Signal<void(int beginLine, int endLine, QChar typedChar)> indentRegion; Signal<void(int beginLine, int endLine, QChar typedChar)> indentRegion;
@ -186,14 +177,14 @@ public:
Signal<void()> tabPreviousRequested; Signal<void()> tabPreviousRequested;
Signal<void()> tabNextRequested; Signal<void()> tabNextRequested;
public: public:
class Private; class Private;
private: private:
Private *d; Private *d;
}; };
} // namespace Internal } // namespace Internal
} // namespace FakeVim } // namespace FakeVim
Q_DECLARE_METATYPE(FakeVim::Internal::ExCommand) Q_DECLARE_METATYPE(FakeVim::Internal::ExCommand)

View file

@ -29,9 +29,8 @@
namespace FakeVim { namespace FakeVim {
struct Tr struct Tr {
{
Q_DECLARE_TR_FUNCTIONS(FakeVim) Q_DECLARE_TR_FUNCTIONS(FakeVim)
}; };
} // namespace FakeVim } // namespace FakeVim

View file

@ -32,13 +32,11 @@ using namespace Utils;
Qt::CaseSensitivity HostOsInfo::m_overrideFileNameCaseSensitivity = Qt::CaseSensitive; Qt::CaseSensitivity HostOsInfo::m_overrideFileNameCaseSensitivity = Qt::CaseSensitive;
bool HostOsInfo::m_useOverrideFileNameCaseSensitivity = false; bool HostOsInfo::m_useOverrideFileNameCaseSensitivity = false;
void HostOsInfo::setOverrideFileNameCaseSensitivity(Qt::CaseSensitivity sensitivity) void HostOsInfo::setOverrideFileNameCaseSensitivity(Qt::CaseSensitivity sensitivity) {
{
m_useOverrideFileNameCaseSensitivity = true; m_useOverrideFileNameCaseSensitivity = true;
m_overrideFileNameCaseSensitivity = sensitivity; m_overrideFileNameCaseSensitivity = sensitivity;
} }
void HostOsInfo::unsetOverrideFileNameCaseSensitivity() void HostOsInfo::unsetOverrideFileNameCaseSensitivity() {
{
m_useOverrideFileNameCaseSensitivity = false; m_useOverrideFileNameCaseSensitivity = false;
} }

View file

@ -25,23 +25,21 @@
#pragma once #pragma once
#include "osspecificaspects.h"
#include <QString> #include <QString>
#include "osspecificaspects.h"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#define QTC_HOST_EXE_SUFFIX QTC_WIN_EXE_SUFFIX #define QTC_HOST_EXE_SUFFIX QTC_WIN_EXE_SUFFIX
#else #else
#define QTC_HOST_EXE_SUFFIX "" #define QTC_HOST_EXE_SUFFIX ""
#endif // Q_OS_WIN #endif // Q_OS_WIN
namespace Utils { namespace Utils {
class HostOsInfo class HostOsInfo {
{ public:
public: static constexpr OsType hostOs() {
static constexpr OsType hostOs()
{
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
return OsTypeWindows; return OsTypeWindows;
#elif defined(Q_OS_LINUX) #elif defined(Q_OS_LINUX)
@ -58,8 +56,7 @@ public:
static constexpr bool isWindowsHost() { return hostOs() == OsTypeWindows; } static constexpr bool isWindowsHost() { return hostOs() == OsTypeWindows; }
static constexpr bool isLinuxHost() { return hostOs() == OsTypeLinux; } static constexpr bool isLinuxHost() { return hostOs() == OsTypeLinux; }
static constexpr bool isMacHost() { return hostOs() == OsTypeMac; } static constexpr bool isMacHost() { return hostOs() == OsTypeMac; }
static constexpr bool isAnyUnixHost() static constexpr bool isAnyUnixHost() {
{
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
return true; return true;
#else #else
@ -67,34 +64,28 @@ public:
#endif #endif
} }
static QString withExecutableSuffix(const QString &executable) static QString withExecutableSuffix(const QString &executable) {
{
return OsSpecificAspects::withExecutableSuffix(hostOs(), executable); return OsSpecificAspects::withExecutableSuffix(hostOs(), executable);
} }
static void setOverrideFileNameCaseSensitivity(Qt::CaseSensitivity sensitivity); static void setOverrideFileNameCaseSensitivity(Qt::CaseSensitivity sensitivity);
static void unsetOverrideFileNameCaseSensitivity(); static void unsetOverrideFileNameCaseSensitivity();
static Qt::CaseSensitivity fileNameCaseSensitivity() static Qt::CaseSensitivity fileNameCaseSensitivity() {
{
return m_useOverrideFileNameCaseSensitivity return m_useOverrideFileNameCaseSensitivity
? m_overrideFileNameCaseSensitivity ? m_overrideFileNameCaseSensitivity
: OsSpecificAspects::fileNameCaseSensitivity(hostOs()); : OsSpecificAspects::fileNameCaseSensitivity(hostOs());
} }
static QChar pathListSeparator() static QChar pathListSeparator() { return OsSpecificAspects::pathListSeparator(hostOs()); }
{
return OsSpecificAspects::pathListSeparator(hostOs());
}
static Qt::KeyboardModifier controlModifier() static Qt::KeyboardModifier controlModifier() {
{
return OsSpecificAspects::controlModifier(hostOs()); return OsSpecificAspects::controlModifier(hostOs());
} }
private: private:
static Qt::CaseSensitivity m_overrideFileNameCaseSensitivity; static Qt::CaseSensitivity m_overrideFileNameCaseSensitivity;
static bool m_useOverrideFileNameCaseSensitivity; static bool m_useOverrideFileNameCaseSensitivity;
}; };
} // namespace Utils } // namespace Utils

View file

@ -83,17 +83,15 @@ using std::experimental::in_place;
// TODO: make_optional is a copy, since there is no sensible way to import functions in C++ // TODO: make_optional is a copy, since there is no sensible way to import functions in C++
template <class T> template <class T>
constexpr optional<typename std::decay<T>::type> make_optional(T&& v) constexpr optional<typename std::decay<T>::type> make_optional(T&& v) {
{ return optional<typename std::decay<T>::type>(std::experimental::constexpr_forward<T>(v));
return optional<typename std::decay<T>::type>(std::experimental::constexpr_forward<T>(v));
} }
template <class X> template <class X>
constexpr optional<X&> make_optional(std::reference_wrapper<X> v) constexpr optional<X&> make_optional(std::reference_wrapper<X> v) {
{ return optional<X&>(v.get());
return optional<X&>(v.get());
} }
} // Utils } // namespace Utils
//#endif // #endif

View file

@ -36,33 +36,27 @@ enum OsType { OsTypeWindows, OsTypeLinux, OsTypeMac, OsTypeOtherUnix, OsTypeOthe
namespace OsSpecificAspects { namespace OsSpecificAspects {
inline QString withExecutableSuffix(OsType osType, const QString &executable) inline QString withExecutableSuffix(OsType osType, const QString &executable) {
{
QString finalName = executable; QString finalName = executable;
if (osType == OsTypeWindows) if (osType == OsTypeWindows) finalName += QLatin1String(QTC_WIN_EXE_SUFFIX);
finalName += QLatin1String(QTC_WIN_EXE_SUFFIX);
return finalName; return finalName;
} }
inline Qt::CaseSensitivity fileNameCaseSensitivity(OsType osType) inline Qt::CaseSensitivity fileNameCaseSensitivity(OsType osType) {
{
return osType == OsTypeWindows || osType == OsTypeMac ? Qt::CaseInsensitive : Qt::CaseSensitive; return osType == OsTypeWindows || osType == OsTypeMac ? Qt::CaseInsensitive : Qt::CaseSensitive;
} }
inline Qt::CaseSensitivity envVarCaseSensitivity(OsType osType) inline Qt::CaseSensitivity envVarCaseSensitivity(OsType osType) {
{
return fileNameCaseSensitivity(osType); return fileNameCaseSensitivity(osType);
} }
inline QChar pathListSeparator(OsType osType) inline QChar pathListSeparator(OsType osType) {
{
return QLatin1Char(osType == OsTypeWindows ? ';' : ':'); return QLatin1Char(osType == OsTypeWindows ? ';' : ':');
} }
inline Qt::KeyboardModifier controlModifier(OsType osType) inline Qt::KeyboardModifier controlModifier(OsType osType) {
{
return osType == OsTypeMac ? Qt::MetaModifier : Qt::ControlModifier; return osType == OsTypeMac ? Qt::MetaModifier : Qt::ControlModifier;
} }
} // namespace OsSpecificAspects } // namespace OsSpecificAspects
} // namespace Utils } // namespace Utils

View file

@ -29,8 +29,7 @@
namespace Utils { namespace Utils {
void writeAssertLocation(const char *msg) void writeAssertLocation(const char *msg) {
{
static bool goBoom = qEnvironmentVariableIsSet("QTC_FATAL_ASSERTS"); static bool goBoom = qEnvironmentVariableIsSet("QTC_FATAL_ASSERTS");
if (goBoom) if (goBoom)
qFatal("SOFT ASSERT made fatal: %s", msg); qFatal("SOFT ASSERT made fatal: %s", msg);
@ -38,4 +37,4 @@ void writeAssertLocation(const char *msg)
qDebug("SOFT ASSERT: %s", msg); qDebug("SOFT ASSERT: %s", msg);
} }
} // namespace Utils } // namespace Utils

View file

@ -25,16 +25,32 @@
#pragma once #pragma once
namespace Utils { void writeAssertLocation(const char *msg); } namespace Utils {
void writeAssertLocation(const char *msg);
}
#define QTC_ASSERT_STRINGIFY_HELPER(x) #x #define QTC_ASSERT_STRINGIFY_HELPER(x) #x
#define QTC_ASSERT_STRINGIFY(x) QTC_ASSERT_STRINGIFY_HELPER(x) #define QTC_ASSERT_STRINGIFY(x) QTC_ASSERT_STRINGIFY_HELPER(x)
#define QTC_ASSERT_STRING(cond) ::Utils::writeAssertLocation(\ #define QTC_ASSERT_STRING(cond) \
"\"" cond"\" in file " __FILE__ ", line " QTC_ASSERT_STRINGIFY(__LINE__)) ::Utils::writeAssertLocation("\"" cond "\" in file " __FILE__ \
", line " QTC_ASSERT_STRINGIFY(__LINE__))
// The 'do {...} while (0)' idiom is not used for the main block here to be // The 'do {...} while (0)' idiom is not used for the main block here to be
// able to use 'break' and 'continue' as 'actions'. // able to use 'break' and 'continue' as 'actions'.
#define QTC_ASSERT(cond, action) if (Q_LIKELY(cond)) {} else { QTC_ASSERT_STRING(#cond); action; } do {} while (0) #define QTC_ASSERT(cond, action) \
#define QTC_CHECK(cond) if (Q_LIKELY(cond)) {} else { QTC_ASSERT_STRING(#cond); } do {} while (0) if (Q_LIKELY(cond)) { \
} else { \
QTC_ASSERT_STRING(#cond); \
action; \
} \
do { \
} while (0)
#define QTC_CHECK(cond) \
if (Q_LIKELY(cond)) { \
} else { \
QTC_ASSERT_STRING(#cond); \
} \
do { \
} while (0)
#define QTC_GUARD(cond) ((Q_LIKELY(cond)) ? true : (QTC_ASSERT_STRING(#cond), false)) #define QTC_GUARD(cond) ((Q_LIKELY(cond)) ? true : (QTC_ASSERT_STRING(#cond), false))

View file

@ -2,14 +2,14 @@
#include <string> #include <string>
class SomeClass { class SomeClass {
public: public:
SomeClass(const std::string &var) : m_var(var) {} SomeClass(const std::string &var) : m_var(var) {}
private:
private:
std::string m_var; std::string m_var;
}; };
int main(int argc, const char *argv[]) int main(int argc, const char *argv[]) {
{
using namespace std; using namespace std;
cout << "TESTING..."; cout << "TESTING...";
cout << "1"; cout << "1";

View file

@ -21,10 +21,7 @@ using String = QString;
* Custom toLower function which is much faster than * Custom toLower function which is much faster than
* c.toLower() directly * c.toLower() directly
*/ */
static inline QChar toLower(QChar c) static inline QChar toLower(QChar c) { return c.isLower() ? c : c.toLower(); }
{
return c.isLower() ? c : c.toLower();
}
// internal // internal
// clang-format off // clang-format off
@ -191,8 +188,8 @@ static bool match_recursive(String::const_iterator pattern,
} }
// clang-format on // clang-format on
static bool match_internal(StringType pattern, StringType str, int &outScore, unsigned char *matches) static bool match_internal(StringType pattern, StringType str, int &outScore,
{ unsigned char *matches) {
if (pattern.isEmpty()) { if (pattern.isEmpty()) {
return true; return true;
} }
@ -204,14 +201,13 @@ static bool match_internal(StringType pattern, StringType str, int &outScore, un
const auto patternEnd = pattern.cend(); const auto patternEnd = pattern.cend();
const auto strEnd = str.cend(); const auto strEnd = str.cend();
return match_recursive(patternIt, strIt, outScore, strIt, strEnd, patternEnd, return match_recursive(patternIt, strIt, outScore, strIt, strEnd, patternEnd, nullptr, matches,
nullptr, matches, 0, recursionCount); 0, recursionCount);
} }
/**************************************************************/ /**************************************************************/
bool KFuzzyMatcher::matchSimple(StringType pattern, StringType str) bool KFuzzyMatcher::matchSimple(StringType pattern, StringType str) {
{
auto patternIt = pattern.cbegin(); auto patternIt = pattern.cbegin();
for (auto strIt = str.cbegin(); strIt != str.cend() && patternIt != pattern.cend(); ++strIt) { for (auto strIt = str.cbegin(); strIt != str.cend() && patternIt != pattern.cend(); ++strIt) {
if (strIt->toLower() == patternIt->toLower()) { if (strIt->toLower() == patternIt->toLower()) {
@ -221,8 +217,7 @@ bool KFuzzyMatcher::matchSimple(StringType pattern, StringType str)
return patternIt == pattern.cend(); return patternIt == pattern.cend();
} }
KFuzzyMatcher::Result KFuzzyMatcher::match(StringType pattern, StringType str) KFuzzyMatcher::Result KFuzzyMatcher::match(StringType pattern, StringType str) {
{
uint8_t matches[256]; uint8_t matches[256];
int score = 0; int score = 0;
const bool matched = match_internal(pattern, str, score, matches); const bool matched = match_internal(pattern, str, score, matches);
@ -232,12 +227,9 @@ KFuzzyMatcher::Result KFuzzyMatcher::match(StringType pattern, StringType str)
return result; return result;
} }
QString KFuzzyMatcher::toFuzzyMatchedDisplayString(const QString &pattern, const QString &str,
QString KFuzzyMatcher::toFuzzyMatchedDisplayString(const QString &pattern,
const QString &str,
const QString &htmlTag, const QString &htmlTag,
const QString &htmlTagClose) const QString &htmlTagClose) {
{
if (pattern.isEmpty()) { if (pattern.isEmpty()) {
return str; return str;
} }
@ -254,7 +246,7 @@ QString KFuzzyMatcher::toFuzzyMatchedDisplayString(const QString &pattern,
QString string = str; QString string = str;
int offset = 0; int offset = 0;
for (int i = 0; i < 256; ++i) { for (int i = 0; i < 256; ++i) {
if (matches[i] == 255){ if (matches[i] == 255) {
break; break;
} }
string.insert(matches[i] + offset, htmlTag); string.insert(matches[i] + offset, htmlTag);
@ -263,5 +255,4 @@ QString KFuzzyMatcher::toFuzzyMatchedDisplayString(const QString &pattern,
offset += htmlTagClose.size(); offset += htmlTagClose.size();
} }
return string; return string;
} }

View file

@ -74,14 +74,12 @@ using StringType = const QString&;
* @short Namespace for fuzzy matching of strings * @short Namespace for fuzzy matching of strings
* @author Waqar Ahmed <waqar.17a@gmail.com> * @author Waqar Ahmed <waqar.17a@gmail.com>
*/ */
namespace KFuzzyMatcher namespace KFuzzyMatcher {
{
/** /**
* @brief The result of a fuzzy match * @brief The result of a fuzzy match
*/ */
struct Result struct Result {
{
/** Score of this match. This can be negative. if matched is @c false /** Score of this match. This can be negative. if matched is @c false
then the score will be zero. then the score will be zero.
*/ */
@ -126,9 +124,7 @@ bool matchSimple(StringType pattern, StringType str);
*/ */
Result match(StringType pattern, StringType str); Result match(StringType pattern, StringType str);
QString toFuzzyMatchedDisplayString(const QString& pattern, QString toFuzzyMatchedDisplayString(const QString& pattern, const QString& str,
const QString& str, const QString& htmlTag, const QString& htmlTagClose);
const QString& htmlTag,
const QString& htmlTagClose);
} // namespace KFuzzyMatcher } // namespace KFuzzyMatcher

View file

@ -1,5 +1,8 @@
cmake_minimum_required(VERSION 3.7) cmake_minimum_required(VERSION 3.7)
project(qlitehtml VERSION 1.0 LANGUAGES CXX) project(
qlitehtml
VERSION 1.0
LANGUAGES CXX)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON) set(CMAKE_AUTORCC ON)
@ -13,23 +16,29 @@ set(QLITEHTML_DEVEL_COMPONENT Devel)
set(QLITEHTML_DEVEL_EXCLUDE_FROM_ALL NO) set(QLITEHTML_DEVEL_EXCLUDE_FROM_ALL NO)
if(UNIX OR MINGW) if(UNIX OR MINGW)
include(GNUInstallDirs) include(GNUInstallDirs)
set(QLITEHTML_BIN_PATH ${CMAKE_INSTALL_BINDIR}) set(QLITEHTML_BIN_PATH ${CMAKE_INSTALL_BINDIR})
set(QLITEHTML_LIBRARY_PATH ${CMAKE_INSTALL_LIBDIR}) set(QLITEHTML_LIBRARY_PATH ${CMAKE_INSTALL_LIBDIR})
set(QLITEHTML_HEADER_PATH ${CMAKE_INSTALL_INCLUDEDIR}/qlitehtml) set(QLITEHTML_HEADER_PATH ${CMAKE_INSTALL_INCLUDEDIR}/qlitehtml)
else() else()
set(QLITEHTML_BIN_PATH bin) set(QLITEHTML_BIN_PATH bin)
set(QLITEHTML_LIBRARY_PATH lib) set(QLITEHTML_LIBRARY_PATH lib)
set(QLITEHTML_HEADER_PATH include/qlitehtml) set(QLITEHTML_HEADER_PATH include/qlitehtml)
endif() endif()
find_package(QT 5.11 NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED) find_package(
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED) QT 5.11 NAMES Qt6 Qt5
COMPONENTS Widgets
REQUIRED)
find_package(
Qt${QT_VERSION_MAJOR}
COMPONENTS Widgets
REQUIRED)
set(Qt Qt${QT_VERSION_MAJOR}) set(Qt Qt${QT_VERSION_MAJOR})
set(QLITEHTML_BUILD_TESTS OFF) set(QLITEHTML_BUILD_TESTS OFF)
if (QLITEHTML_BUILD_TESTS) if(QLITEHTML_BUILD_TESTS)
add_subdirectory(tests/manual/browser) add_subdirectory(tests/manual/browser)
endif() endif()
add_subdirectory(src) add_subdirectory(src)

View file

@ -4,13 +4,13 @@ project(litehtml LANGUAGES C CXX)
include(CTest) include(CTest)
enable_testing() enable_testing()
# Soname # Soname MAJOR is incremented when symbols are removed or changed in an
# MAJOR is incremented when symbols are removed or changed in an incompatible way # incompatible way MINOR is incremented when new symbols are added
# MINOR is incremented when new symbols are added
set(PROJECT_MAJOR 0) set(PROJECT_MAJOR 0)
set(PROJECT_MINOR 0) set(PROJECT_MINOR 0)
option(EXTERNAL_GUMBO "Link against external gumbo instead of shipping a bundled copy" OFF) option(EXTERNAL_GUMBO
"Link against external gumbo instead of shipping a bundled copy" OFF)
if(WIN32) if(WIN32)
option(EXTERNAL_XXD "Use external xxd" OFF) option(EXTERNAL_XXD "Use external xxd" OFF)
else() else()
@ -18,7 +18,7 @@ else()
endif() endif()
if(NOT EXTERNAL_GUMBO) if(NOT EXTERNAL_GUMBO)
add_subdirectory(src/gumbo) add_subdirectory(src/gumbo)
endif() endif()
set(SOURCE_LITEHTML set(SOURCE_LITEHTML
@ -64,8 +64,7 @@ set(SOURCE_LITEHTML
src/utf8_strings.cpp src/utf8_strings.cpp
src/web_color.cpp src/web_color.cpp
src/num_cvt.cpp src/num_cvt.cpp
src/strtod.cpp src/strtod.cpp)
)
set(HEADER_LITEHTML set(HEADER_LITEHTML
include/litehtml.h include/litehtml.h
@ -116,8 +115,7 @@ set(HEADER_LITEHTML
include/litehtml/url_path.h include/litehtml/url_path.h
include/litehtml/utf8_strings.h include/litehtml/utf8_strings.h
include/litehtml/web_color.h include/litehtml/web_color.h
include/litehtml/num_cvt.h include/litehtml/num_cvt.h)
)
set(TEST_LITEHTML set(TEST_LITEHTML
containers/test/container_test.cpp containers/test/container_test.cpp
@ -130,30 +128,32 @@ set(TEST_LITEHTML
test/tstring_view_test.cpp test/tstring_view_test.cpp
test/url_test.cpp test/url_test.cpp
test/url_path_test.cpp test/url_path_test.cpp
test/webColorTest.cpp test/webColorTest.cpp)
)
set(PROJECT_LIB_VERSION ${PROJECT_MAJOR}.${PROJECT_MINOR}.0) set(PROJECT_LIB_VERSION ${PROJECT_MAJOR}.${PROJECT_MINOR}.0)
set(PROJECT_SO_VERSION ${PROJECT_MAJOR}) set(PROJECT_SO_VERSION ${PROJECT_MAJOR})
add_library(${PROJECT_NAME} ${SOURCE_LITEHTML}) add_library(${PROJECT_NAME} ${SOURCE_LITEHTML})
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_LIB_VERSION} SOVERSION ${PROJECT_SO_VERSION}) set_target_properties(
${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_LIB_VERSION}
SOVERSION ${PROJECT_SO_VERSION})
set_target_properties(${PROJECT_NAME} PROPERTIES set_target_properties(
CXX_STANDARD 11 ${PROJECT_NAME}
C_STANDARD 99 PROPERTIES CXX_STANDARD 11
PUBLIC_HEADER "${HEADER_LITEHTML}" C_STANDARD 99
) PUBLIC_HEADER "${HEADER_LITEHTML}")
# Export litehtml includes. # Export litehtml includes.
target_include_directories(${PROJECT_NAME} PUBLIC target_include_directories(
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src> ${PROJECT_NAME}
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>) $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>)
target_include_directories(${PROJECT_NAME} PRIVATE include/${PROJECT_NAME}) target_include_directories(${PROJECT_NAME} PRIVATE include/${PROJECT_NAME})
option(LITEHTML_UTF8 "Build litehtml with UTF-8 text conversion functions." OFF) option(LITEHTML_UTF8 "Build litehtml with UTF-8 text conversion functions." OFF)
if (LITEHTML_UTF8) if(LITEHTML_UTF8)
target_compile_definitions(${PROJECT_NAME} PUBLIC LITEHTML_UTF8) target_compile_definitions(${PROJECT_NAME} PUBLIC LITEHTML_UTF8)
endif() endif()
@ -161,96 +161,94 @@ endif()
target_link_libraries(${PROJECT_NAME} PUBLIC gumbo) target_link_libraries(${PROJECT_NAME} PUBLIC gumbo)
# install and export # install and export
install(TARGETS ${PROJECT_NAME} install(
EXPORT litehtmlTargets TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin COMPONENT libraries EXPORT litehtmlTargets
ARCHIVE DESTINATION lib${LIB_SUFFIX} COMPONENT libraries RUNTIME DESTINATION bin COMPONENT libraries
LIBRARY DESTINATION lib${LIB_SUFFIX} COMPONENT libraries ARCHIVE DESTINATION lib${LIB_SUFFIX} COMPONENT libraries
PUBLIC_HEADER DESTINATION include/litehtml LIBRARY DESTINATION lib${LIB_SUFFIX} COMPONENT libraries
) PUBLIC_HEADER DESTINATION include/litehtml)
install(FILES cmake/litehtmlConfig.cmake DESTINATION lib${LIB_SUFFIX}/cmake/litehtml) install(FILES cmake/litehtmlConfig.cmake
install(EXPORT litehtmlTargets FILE litehtmlTargets.cmake DESTINATION lib${LIB_SUFFIX}/cmake/litehtml) DESTINATION lib${LIB_SUFFIX}/cmake/litehtml)
install(
EXPORT litehtmlTargets
FILE litehtmlTargets.cmake
DESTINATION lib${LIB_SUFFIX}/cmake/litehtml)
# Binary Master.css # Binary Master.css
if (NOT EXTERNAL_XXD) if(NOT EXTERNAL_XXD)
add_executable(xxd xxd/xxd.c) add_executable(xxd xxd/xxd.c)
set(XXD_COMMAND $<TARGET_FILE:xxd>) set(XXD_COMMAND $<TARGET_FILE:xxd>)
else() else()
find_program(XXD_COMMAND xxd) find_program(XXD_COMMAND xxd)
endif() endif()
if(WIN32) if(WIN32)
file(TO_NATIVE_PATH ${XXD_COMMAND} XXD_COMMAND) file(TO_NATIVE_PATH ${XXD_COMMAND} XXD_COMMAND)
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/include/master.css MASTER_FILE) file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/include/master.css
add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc MASTER_FILE)
COMMAND type ${MASTER_FILE} | "${XXD_COMMAND}" -i > ${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc) add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc
COMMAND type ${MASTER_FILE} | "${XXD_COMMAND}" -i >
${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc)
else() else()
add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc add_custom_command(
COMMAND cat ${CMAKE_CURRENT_SOURCE_DIR}/include/master.css | xxd -i > ${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc) OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc
COMMAND cat ${CMAKE_CURRENT_SOURCE_DIR}/include/master.css | xxd -i >
${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc)
endif() endif()
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc PROPERTIES GENERATED TRUE) set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc
PROPERTIES GENERATED TRUE)
# Tests # Tests
set(BUILD_TESTING OFF) set(BUILD_TESTING OFF)
if (BUILD_TESTING) if(BUILD_TESTING)
include(FetchContent) include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
googletest googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
) )
# For Windows: Prevent overriding the parent project's compiler/linker settings # For Windows: Prevent overriding the parent project's compiler/linker
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # settings
FetchContent_GetProperties(googletest) set(gtest_force_shared_crt
if(NOT googletest_POPULATED) ON
FetchContent_Populate(googletest) CACHE BOOL "" FORCE)
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR}) FetchContent_GetProperties(googletest)
endif() if(NOT googletest_POPULATED)
FetchContent_Populate(googletest)
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
endif()
enable_testing() enable_testing()
set(TEST_NAME ${PROJECT_NAME}_tests) set(TEST_NAME ${PROJECT_NAME}_tests)
add_executable( add_executable(${TEST_NAME} ${TEST_LITEHTML}
${TEST_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc)
${TEST_LITEHTML}
${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc
)
set_target_properties(${TEST_NAME} PROPERTIES set_target_properties(
CXX_STANDARD 11 ${TEST_NAME}
C_STANDARD 99 PROPERTIES CXX_STANDARD 11
PUBLIC_HEADER "${HEADER_LITEHTML}" C_STANDARD 99
) PUBLIC_HEADER "${HEADER_LITEHTML}")
target_include_directories( target_include_directories(${TEST_NAME}
${TEST_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/containers)
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/containers
)
target_link_libraries( target_link_libraries(${TEST_NAME} ${PROJECT_NAME} gtest_main)
${TEST_NAME}
${PROJECT_NAME}
gtest_main
)
include(GoogleTest) include(GoogleTest)
gtest_discover_tests(${TEST_NAME}) gtest_discover_tests(${TEST_NAME})
endif() endif()
# set(TEST_NAME ${PROJECT_NAME}_tests) # set(TEST_NAME ${PROJECT_NAME}_tests) add_executable(${TEST_NAME}
# add_executable(${TEST_NAME} ${TEST_LITEHTML} ${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc) # ${TEST_LITEHTML} ${CMAKE_CURRENT_SOURCE_DIR}/src/master.css.inc)
# set_target_properties(${TEST_NAME} PROPERTIES # set_target_properties(${TEST_NAME} PROPERTIES CXX_STANDARD 11 C_STANDARD 99
# CXX_STANDARD 11 # PUBLIC_HEADER "${HEADER_LITEHTML}" ) target_include_directories(${TEST_NAME}
# C_STANDARD 99 # PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/containers)
# PUBLIC_HEADER "${HEADER_LITEHTML}" # target_link_libraries(${TEST_NAME} PRIVATE ${PROJECT_NAME}) # tests
# ) # add_test(NAME contextTest COMMAND ${TEST_NAME} 1) add_test(NAME cssTest
# target_include_directories(${TEST_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/containers) # COMMAND ${TEST_NAME} 2) add_test(NAME documentTest COMMAND ${TEST_NAME} 3)
# target_link_libraries(${TEST_NAME} PRIVATE ${PROJECT_NAME}) # add_test(NAME layoutGlobalTest COMMAND ${TEST_NAME} 4) add_test(NAME
# # tests # mediaQueryTest COMMAND ${TEST_NAME} 5) add_test(NAME webColorTest COMMAND
# add_test(NAME contextTest COMMAND ${TEST_NAME} 1) # ${TEST_NAME} 6) endif()
# add_test(NAME cssTest COMMAND ${TEST_NAME} 2)
# add_test(NAME documentTest COMMAND ${TEST_NAME} 3)
# add_test(NAME layoutGlobalTest COMMAND ${TEST_NAME} 4)
# add_test(NAME mediaQueryTest COMMAND ${TEST_NAME} 5)
# add_test(NAME webColorTest COMMAND ${TEST_NAME} 6)
# endif()

View file

@ -1,110 +1,106 @@
#pragma once #pragma once
#include <windows.h> #include <cairo-win32.h>
#include <mlang.h> #include <cairo.h>
#include <stdlib.h> #include <dib.h>
#include <litehtml.h>
#include <malloc.h> #include <malloc.h>
#include <memory.h> #include <memory.h>
#include <tchar.h>
#include <mlang.h> #include <mlang.h>
#include <vector> #include <stdlib.h>
#include <cairo.h> #include <tchar.h>
#include <cairo-win32.h>
#include <litehtml.h>
#include <dib.h>
#include <txdib.h> #include <txdib.h>
#include <windows.h>
#include <vector>
#ifdef LITEHTML_UTF8 #ifdef LITEHTML_UTF8
#define t_make_url make_url_utf8 #define t_make_url make_url_utf8
#else #else
#define t_make_url make_url #define t_make_url make_url
#endif #endif
struct cairo_clip_box struct cairo_clip_box {
{ typedef std::vector<cairo_clip_box> vector;
typedef std::vector<cairo_clip_box> vector; litehtml::position box;
litehtml::position box; litehtml::border_radiuses radius;
litehtml::border_radiuses radius;
cairo_clip_box(const litehtml::position& vBox, litehtml::border_radiuses vRad) cairo_clip_box(const litehtml::position& vBox, litehtml::border_radiuses vRad) {
{ box = vBox;
box = vBox; radius = vRad;
radius = vRad; }
}
cairo_clip_box(const cairo_clip_box& val) cairo_clip_box(const cairo_clip_box& val) {
{ box = val.box;
box = val.box; radius = val.radius;
radius = val.radius; }
} cairo_clip_box& operator=(const cairo_clip_box& val) {
cairo_clip_box& operator=(const cairo_clip_box& val) box = val.box;
{ radius = val.radius;
box = val.box; return *this;
radius = val.radius; }
return *this;
}
}; };
class cairo_container : public litehtml::document_container class cairo_container : public litehtml::document_container {
{ public:
public: typedef std::shared_ptr<CTxDIB> image_ptr;
typedef std::shared_ptr<CTxDIB> image_ptr; typedef std::map<std::wstring, image_ptr> images_map;
typedef std::map<std::wstring, image_ptr> images_map;
protected: protected:
cairo_surface_t* m_temp_surface; cairo_surface_t* m_temp_surface;
cairo_t* m_temp_cr; cairo_t* m_temp_cr;
images_map m_images; images_map m_images;
cairo_clip_box::vector m_clips; cairo_clip_box::vector m_clips;
IMLangFontLink2* m_font_link; IMLangFontLink2* m_font_link;
CRITICAL_SECTION m_img_sync; CRITICAL_SECTION m_img_sync;
public:
cairo_container(void);
virtual ~cairo_container(void);
virtual litehtml::uint_ptr create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) override; public:
virtual void delete_font(litehtml::uint_ptr hFont) override; cairo_container(void);
virtual int text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) override; virtual ~cairo_container(void);
virtual void draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) override;
virtual int pt_to_px(int pt) const override; virtual litehtml::uint_ptr create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) override;
virtual int get_default_font_size() const override; virtual void delete_font(litehtml::uint_ptr hFont) override;
virtual const litehtml::tchar_t* get_default_font_name() const override; virtual int text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) override;
virtual void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) override; virtual void draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) override;
virtual void load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override;
virtual void get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override;
virtual void draw_image(litehtml::uint_ptr hdc, const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, const litehtml::position& pos);
virtual void draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override;
virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override;
virtual void transform_text(litehtml::tstring& text, litehtml::text_transform tt) override; virtual int pt_to_px(int pt) const override;
virtual void set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) override; virtual int get_default_font_size() const override;
virtual void del_clip() override; virtual const litehtml::tchar_t* get_default_font_name() const override;
virtual std::shared_ptr<litehtml::element> create_element(const litehtml::tchar_t* tag_name, const litehtml::string_map& attributes, const std::shared_ptr<litehtml::document>& doc) override; virtual void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) override;
virtual void get_media_features(litehtml::media_features& media) const override; virtual void load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override;
virtual void get_language(litehtml::tstring& language, litehtml::tstring & culture) const override; virtual void get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override;
virtual void link(const std::shared_ptr<litehtml::document>& doc, const litehtml::element::ptr& el) override; virtual void draw_image(litehtml::uint_ptr hdc, const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, const litehtml::position& pos);
virtual litehtml::tstring resolve_color(const litehtml::tstring& color) const override; virtual void draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override;
virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override;
virtual void transform_text(litehtml::tstring& text, litehtml::text_transform tt) override;
virtual void set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) override;
virtual void del_clip() override;
virtual std::shared_ptr<litehtml::element> create_element(const litehtml::tchar_t* tag_name, const litehtml::string_map& attributes, const std::shared_ptr<litehtml::document>& doc) override;
virtual void get_media_features(litehtml::media_features& media) const override;
virtual void get_language(litehtml::tstring& language, litehtml::tstring& culture) const override;
virtual void link(const std::shared_ptr<litehtml::document>& doc, const litehtml::element::ptr& el) override;
virtual litehtml::tstring resolve_color(const litehtml::tstring& color) const override;
virtual void make_url( LPCWSTR url, LPCWSTR basepath, std::wstring& out ) = 0; virtual void make_url(LPCWSTR url, LPCWSTR basepath, std::wstring& out) = 0;
virtual image_ptr get_image(LPCWSTR url, bool redraw_on_ready) = 0; virtual image_ptr get_image(LPCWSTR url, bool redraw_on_ready) = 0;
void clear_images(); void clear_images();
void add_image(std::wstring& url, image_ptr& img); void add_image(std::wstring& url, image_ptr& img);
void remove_image(std::wstring& url); void remove_image(std::wstring& url);
void make_url_utf8( const char* url, const char* basepath, std::wstring& out ); void make_url_utf8(const char* url, const char* basepath, std::wstring& out);
protected: protected:
virtual void draw_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, double line_width); virtual void draw_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, double line_width);
virtual void fill_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color); virtual void fill_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color);
virtual void rounded_rectangle( cairo_t* cr, const litehtml::position &pos, const litehtml::border_radiuses &radius ); virtual void rounded_rectangle(cairo_t* cr, const litehtml::position& pos, const litehtml::border_radiuses& radius);
void set_color(cairo_t* cr, litehtml::web_color color) { cairo_set_source_rgba(cr, color.red / 255.0, color.green / 255.0, color.blue / 255.0, color.alpha / 255.0); } void set_color(cairo_t* cr, litehtml::web_color color) { cairo_set_source_rgba(cr, color.red / 255.0, color.green / 255.0, color.blue / 255.0, color.alpha / 255.0); }
private:
simpledib::dib* get_dib(litehtml::uint_ptr hdc) { return (simpledib::dib*) hdc; }
void apply_clip(cairo_t* cr);
bool add_path_arc(cairo_t* cr, double x, double y, double rx, double ry, double a1, double a2, bool neg);
void draw_txdib(cairo_t* cr, CTxDIB* bmp, int x, int y, int cx, int cy); private:
void lock_images_cache(); simpledib::dib* get_dib(litehtml::uint_ptr hdc) { return (simpledib::dib*)hdc; }
void unlock_images_cache(); void apply_clip(cairo_t* cr);
bool add_path_arc(cairo_t* cr, double x, double y, double rx, double ry, double a1, double a2, bool neg);
void draw_txdib(cairo_t* cr, CTxDIB* bmp, int x, int y, int cx, int cy);
void lock_images_cache();
void unlock_images_cache();
}; };

View file

@ -1,379 +1,323 @@
#include "cairo_font.h" #include "cairo_font.h"
cairo_font::cairo_font(IMLangFontLink2* fl, HFONT hFont, int size ) cairo_font::cairo_font(IMLangFontLink2* fl, HFONT hFont, int size) {
{ init();
init(); m_font_link = fl;
m_font_link = fl; if (m_font_link) {
if(m_font_link) m_font_link->AddRef();
{ }
m_font_link->AddRef(); m_size = size;
} set_font(hFont);
m_size = size;
set_font(hFont);
} }
cairo_font::cairo_font(IMLangFontLink2* fl, LPCWSTR facename, int size, int weight, BOOL italic, BOOL strikeout, BOOL underline ) cairo_font::cairo_font(IMLangFontLink2* fl, LPCWSTR facename, int size, int weight, BOOL italic, BOOL strikeout, BOOL underline) {
{ init();
init(); m_size = size;
m_size = size; m_font_link = fl;
m_font_link = fl; if (m_font_link) {
if(m_font_link) m_font_link->AddRef();
{ }
m_font_link->AddRef();
}
LOGFONT lf; LOGFONT lf;
ZeroMemory(&lf, sizeof(lf)); ZeroMemory(&lf, sizeof(lf));
if(!lstrcmpi(facename, L"monospace")) if (!lstrcmpi(facename, L"monospace")) {
{ wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Courier New");
wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Courier New"); } else if (!lstrcmpi(facename, L"serif")) {
} else if(!lstrcmpi(facename, L"serif")) wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Times New Roman");
{ } else if (!lstrcmpi(facename, L"sans-serif")) {
wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Times New Roman"); wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Arial");
} else if(!lstrcmpi(facename, L"sans-serif")) } else if (!lstrcmpi(facename, L"fantasy")) {
{ wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Impact");
wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Arial"); } else if (!lstrcmpi(facename, L"cursive")) {
} else if(!lstrcmpi(facename, L"fantasy")) wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Comic Sans MS");
{ } else {
wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Impact"); wcscpy_s(lf.lfFaceName, LF_FACESIZE, facename);
} else if(!lstrcmpi(facename, L"cursive")) }
{
wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Comic Sans MS");
} else
{
wcscpy_s(lf.lfFaceName, LF_FACESIZE, facename);
}
lf.lfHeight = -size; lf.lfHeight = -size;
lf.lfWeight = weight; lf.lfWeight = weight;
lf.lfItalic = italic; lf.lfItalic = italic;
lf.lfCharSet = DEFAULT_CHARSET; lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS; lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS; lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY; lf.lfQuality = DEFAULT_QUALITY;
lf.lfStrikeOut = strikeout; lf.lfStrikeOut = strikeout;
lf.lfUnderline = underline; lf.lfUnderline = underline;
HFONT fnt = CreateFontIndirect(&lf); HFONT fnt = CreateFontIndirect(&lf);
set_font(fnt); set_font(fnt);
} }
cairo_font::~cairo_font() cairo_font::~cairo_font() {
{ if (m_font_face) {
if(m_font_face) cairo_font_face_destroy(m_font_face);
{ }
cairo_font_face_destroy(m_font_face); for (size_t i = 0; i < m_linked_fonts.size(); i++) {
} if (m_linked_fonts[i]->hFont) {
for(size_t i = 0; i < m_linked_fonts.size(); i++) m_font_link->ReleaseFont(m_linked_fonts[i]->hFont);
{ }
if(m_linked_fonts[i]->hFont) if (m_linked_fonts[i]->font_face) {
{ cairo_font_face_destroy(m_linked_fonts[i]->font_face);
m_font_link->ReleaseFont(m_linked_fonts[i]->hFont); }
} }
if(m_linked_fonts[i]->font_face) m_linked_fonts.clear();
{ if (m_font_link) {
cairo_font_face_destroy(m_linked_fonts[i]->font_face); m_font_link->AddRef();
} }
} if (m_hFont) {
m_linked_fonts.clear(); DeleteObject(m_hFont);
if(m_font_link) }
{
m_font_link->AddRef();
}
if(m_hFont)
{
DeleteObject(m_hFont);
}
} }
void cairo_font::show_text( cairo_t* cr, int x, int y, const litehtml::tchar_t* str ) void cairo_font::show_text(cairo_t* cr, int x, int y, const litehtml::tchar_t* str) {
{ lock();
lock(); text_chunk::vector chunks;
text_chunk::vector chunks; split_text(str, chunks);
split_text(str, chunks); cairo_set_font_size(cr, m_size);
cairo_set_font_size(cr, m_size); cairo_move_to(cr, x, y);
cairo_move_to(cr, x, y); for (size_t i = 0; i < chunks.size(); i++) {
for(size_t i = 0; i < chunks.size(); i++) if (chunks[i]->font) {
{ cairo_set_font_face(cr, chunks[i]->font->font_face);
if(chunks[i]->font) } else {
{ cairo_set_font_face(cr, m_font_face);
cairo_set_font_face(cr, chunks[i]->font->font_face); }
} else cairo_show_text(cr, chunks[i]->text);
{ }
cairo_set_font_face(cr, m_font_face); unlock();
}
cairo_show_text(cr, chunks[i]->text);
}
unlock();
if(m_bUnderline) if (m_bUnderline) {
{ int tw = text_width(cr, chunks);
int tw = text_width(cr, chunks);
lock(); lock();
cairo_set_line_width(cr, 1); cairo_set_line_width(cr, 1);
cairo_move_to(cr, x, y + 1.5); cairo_move_to(cr, x, y + 1.5);
cairo_line_to(cr, x + tw, y + 1.5); cairo_line_to(cr, x + tw, y + 1.5);
cairo_stroke(cr); cairo_stroke(cr);
unlock(); unlock();
} }
if(m_bStrikeOut) if (m_bStrikeOut) {
{ int tw = text_width(cr, chunks);
int tw = text_width(cr, chunks);
cairo_font_metrics fm; cairo_font_metrics fm;
get_metrics(cr, &fm); get_metrics(cr, &fm);
int ln_y = y - fm.x_height / 2; int ln_y = y - fm.x_height / 2;
lock(); lock();
cairo_set_line_width(cr, 1); cairo_set_line_width(cr, 1);
cairo_move_to(cr, x, (double) ln_y - 0.5); cairo_move_to(cr, x, (double)ln_y - 0.5);
cairo_line_to(cr, x + tw, (double) ln_y - 0.5); cairo_line_to(cr, x + tw, (double)ln_y - 0.5);
cairo_stroke(cr); cairo_stroke(cr);
unlock(); unlock();
} }
free_text_chunks(chunks); free_text_chunks(chunks);
} }
void cairo_font::split_text( const litehtml::tchar_t* src, text_chunk::vector& chunks ) void cairo_font::split_text(const litehtml::tchar_t* src, text_chunk::vector& chunks) {
{ wchar_t* str;
wchar_t* str;
#ifdef LITEHTML_UTF8 #ifdef LITEHTML_UTF8
str = cairo_font::utf8_to_wchar(src); str = cairo_font::utf8_to_wchar(src);
wchar_t* str_start = str; wchar_t* str_start = str;
#else #else
str = (wchar_t*) src; str = (wchar_t*)src;
#endif #endif
int cch = lstrlen(str); int cch = lstrlen(str);
HDC hdc = GetDC(NULL); HDC hdc = GetDC(NULL);
SelectObject(hdc, m_hFont); SelectObject(hdc, m_hFont);
HRESULT hr = S_OK; HRESULT hr = S_OK;
while(cch > 0) while (cch > 0) {
{ DWORD dwActualCodePages;
DWORD dwActualCodePages; long cchActual;
long cchActual; if (m_font_link) {
if(m_font_link) hr = m_font_link->GetStrCodePages(str, cch, m_font_code_pages, &dwActualCodePages, &cchActual);
{ } else {
hr = m_font_link->GetStrCodePages(str, cch, m_font_code_pages, &dwActualCodePages, &cchActual); hr = S_FALSE;
} else }
{
hr = S_FALSE;
}
if(hr != S_OK)
{
break;
}
text_chunk* chk = new text_chunk;
int sz = WideCharToMultiByte(CP_UTF8, 0, str, cchActual, chk->text, 0, NULL, NULL) + 1; if (hr != S_OK) {
chk->text = new CHAR[sz]; break;
sz = WideCharToMultiByte(CP_UTF8, 0, str, cchActual, chk->text, sz, NULL, NULL); }
chk->text[sz] = 0;
chk->font = NULL;
if(!(dwActualCodePages & m_font_code_pages)) text_chunk* chk = new text_chunk;
{
for(linked_font::vector::iterator i = m_linked_fonts.begin(); i != m_linked_fonts.end(); i++)
{
if((*i)->code_pages == dwActualCodePages)
{
chk->font = (*i);
break;
}
}
if(!chk->font)
{
linked_font* lkf = new linked_font;
lkf->code_pages = dwActualCodePages;
lkf->hFont = NULL;
m_font_link->MapFont(hdc, dwActualCodePages, 0, &lkf->hFont);
if (lkf->hFont)
{
lkf->font_face = create_font_face(lkf->hFont);
m_linked_fonts.push_back(lkf);
}
else
{
delete lkf;
lkf = NULL;
}
chk->font = lkf;
}
}
chunks.push_back(chk); int sz = WideCharToMultiByte(CP_UTF8, 0, str, cchActual, chk->text, 0, NULL, NULL) + 1;
chk->text = new CHAR[sz];
sz = WideCharToMultiByte(CP_UTF8, 0, str, cchActual, chk->text, sz, NULL, NULL);
chk->text[sz] = 0;
chk->font = NULL;
cch -= cchActual; if (!(dwActualCodePages & m_font_code_pages)) {
str += cchActual; for (linked_font::vector::iterator i = m_linked_fonts.begin(); i != m_linked_fonts.end(); i++) {
} if ((*i)->code_pages == dwActualCodePages) {
chk->font = (*i);
break;
}
}
if (!chk->font) {
linked_font* lkf = new linked_font;
lkf->code_pages = dwActualCodePages;
lkf->hFont = NULL;
m_font_link->MapFont(hdc, dwActualCodePages, 0, &lkf->hFont);
if (lkf->hFont) {
lkf->font_face = create_font_face(lkf->hFont);
m_linked_fonts.push_back(lkf);
} else {
delete lkf;
lkf = NULL;
}
chk->font = lkf;
}
}
if(hr != S_OK) chunks.push_back(chk);
{
text_chunk* chk = new text_chunk;
int sz = WideCharToMultiByte(CP_UTF8, 0, str, -1, chk->text, 0, NULL, NULL) + 1; cch -= cchActual;
chk->text = new CHAR[sz]; str += cchActual;
sz = WideCharToMultiByte(CP_UTF8, 0, str, -1, chk->text, sz, NULL, NULL); }
chk->text[sz] = 0;
chk->font = NULL;
chunks.push_back(chk);
}
ReleaseDC(NULL, hdc); if (hr != S_OK) {
text_chunk* chk = new text_chunk;
int sz = WideCharToMultiByte(CP_UTF8, 0, str, -1, chk->text, 0, NULL, NULL) + 1;
chk->text = new CHAR[sz];
sz = WideCharToMultiByte(CP_UTF8, 0, str, -1, chk->text, sz, NULL, NULL);
chk->text[sz] = 0;
chk->font = NULL;
chunks.push_back(chk);
}
ReleaseDC(NULL, hdc);
#ifdef LITEHTML_UTF8 #ifdef LITEHTML_UTF8
delete str_start; delete str_start;
#endif #endif
} }
void cairo_font::free_text_chunks( text_chunk::vector& chunks ) void cairo_font::free_text_chunks(text_chunk::vector& chunks) {
{ for (size_t i = 0; i < chunks.size(); i++) {
for(size_t i = 0; i < chunks.size(); i++) delete chunks[i];
{ }
delete chunks[i]; chunks.clear();
}
chunks.clear();
} }
cairo_font_face_t* cairo_font::create_font_face( HFONT fnt ) cairo_font_face_t* cairo_font::create_font_face(HFONT fnt) {
{ LOGFONT lf;
LOGFONT lf; GetObject(fnt, sizeof(LOGFONT), &lf);
GetObject(fnt, sizeof(LOGFONT), &lf); return cairo_win32_font_face_create_for_logfontw(&lf);
return cairo_win32_font_face_create_for_logfontw(&lf);
} }
int cairo_font::text_width( cairo_t* cr, const litehtml::tchar_t* str ) int cairo_font::text_width(cairo_t* cr, const litehtml::tchar_t* str) {
{ text_chunk::vector chunks;
text_chunk::vector chunks; split_text(str, chunks);
split_text(str, chunks);
int ret = text_width(cr, chunks); int ret = text_width(cr, chunks);
free_text_chunks(chunks); free_text_chunks(chunks);
return (int) ret; return (int)ret;
} }
int cairo_font::text_width( cairo_t* cr, text_chunk::vector& chunks ) int cairo_font::text_width(cairo_t* cr, text_chunk::vector& chunks) {
{ lock();
lock(); cairo_set_font_size(cr, m_size);
cairo_set_font_size(cr, m_size); double ret = 0;
double ret = 0; for (size_t i = 0; i < chunks.size(); i++) {
for(size_t i = 0; i < chunks.size(); i++) if (chunks[i]->font) {
{ cairo_set_font_face(cr, chunks[i]->font->font_face);
if(chunks[i]->font) } else {
{ cairo_set_font_face(cr, m_font_face);
cairo_set_font_face(cr, chunks[i]->font->font_face); }
} else cairo_text_extents_t ext;
{ cairo_text_extents(cr, chunks[i]->text, &ext);
cairo_set_font_face(cr, m_font_face); ret += ext.x_advance;
} }
cairo_text_extents_t ext; unlock();
cairo_text_extents(cr, chunks[i]->text, &ext);
ret += ext.x_advance;
}
unlock();
return (int) ret; return (int)ret;
} }
void cairo_font::get_metrics(cairo_t* cr, cairo_font_metrics* fm ) void cairo_font::get_metrics(cairo_t* cr, cairo_font_metrics* fm) {
{ lock();
lock(); cairo_set_font_face(cr, m_font_face);
cairo_set_font_face(cr, m_font_face); cairo_set_font_size(cr, m_size);
cairo_set_font_size(cr, m_size); cairo_font_extents_t ext;
cairo_font_extents_t ext; cairo_font_extents(cr, &ext);
cairo_font_extents(cr, &ext);
cairo_text_extents_t tex; cairo_text_extents_t tex;
cairo_text_extents(cr, "x", &tex); cairo_text_extents(cr, "x", &tex);
fm->ascent = (int) ext.ascent; fm->ascent = (int)ext.ascent;
fm->descent = (int) ext.descent; fm->descent = (int)ext.descent;
fm->height = (int) (ext.ascent + ext.descent); fm->height = (int)(ext.ascent + ext.descent);
fm->x_height = (int) tex.height; fm->x_height = (int)tex.height;
unlock(); unlock();
} }
void cairo_font::set_font( HFONT hFont ) void cairo_font::set_font(HFONT hFont) {
{ clear();
clear();
m_hFont = hFont; m_hFont = hFont;
m_font_face = create_font_face(m_hFont); m_font_face = create_font_face(m_hFont);
m_font_code_pages = 0; m_font_code_pages = 0;
if(m_font_link) if (m_font_link) {
{ HDC hdc = GetDC(NULL);
HDC hdc = GetDC(NULL); SelectObject(hdc, m_hFont);
SelectObject(hdc, m_hFont); m_font_link->GetFontCodePages(hdc, m_hFont, &m_font_code_pages);
m_font_link->GetFontCodePages(hdc, m_hFont, &m_font_code_pages); ReleaseDC(NULL, hdc);
ReleaseDC(NULL, hdc); }
} LOGFONT lf;
LOGFONT lf; GetObject(m_hFont, sizeof(LOGFONT), &lf);
GetObject(m_hFont, sizeof(LOGFONT), &lf); m_bUnderline = lf.lfUnderline;
m_bUnderline = lf.lfUnderline; m_bStrikeOut = lf.lfStrikeOut;
m_bStrikeOut = lf.lfStrikeOut;
} }
void cairo_font::clear() void cairo_font::clear() {
{ if (m_font_face) {
if(m_font_face) cairo_font_face_destroy(m_font_face);
{ m_font_face = NULL;
cairo_font_face_destroy(m_font_face); }
m_font_face = NULL; for (size_t i = 0; i < m_linked_fonts.size(); i++) {
} if (m_linked_fonts[i]->hFont && m_font_link) {
for(size_t i = 0; i < m_linked_fonts.size(); i++) m_font_link->ReleaseFont(m_linked_fonts[i]->hFont);
{ }
if(m_linked_fonts[i]->hFont && m_font_link) if (m_linked_fonts[i]->font_face) {
{ cairo_font_face_destroy(m_linked_fonts[i]->font_face);
m_font_link->ReleaseFont(m_linked_fonts[i]->hFont); }
} }
if(m_linked_fonts[i]->font_face) m_linked_fonts.clear();
{ if (m_hFont) {
cairo_font_face_destroy(m_linked_fonts[i]->font_face); DeleteObject(m_hFont);
} m_hFont = NULL;
} }
m_linked_fonts.clear();
if(m_hFont)
{
DeleteObject(m_hFont);
m_hFont = NULL;
}
} }
void cairo_font::init() void cairo_font::init() {
{ m_hFont = NULL;
m_hFont = NULL; m_font_face = NULL;
m_font_face = NULL; m_font_link = NULL;
m_font_link = NULL; m_font_code_pages = 0;
m_font_code_pages = 0; m_size = 0;
m_size = 0; m_bUnderline = FALSE;
m_bUnderline = FALSE; m_bStrikeOut = FALSE;
m_bStrikeOut = FALSE;
} }
wchar_t* cairo_font::utf8_to_wchar( const char* src ) wchar_t* cairo_font::utf8_to_wchar(const char* src) {
{ if (!src) return NULL;
if(!src) return NULL;
int len = (int) strlen(src); int len = (int)strlen(src);
wchar_t* ret = new wchar_t[len + 1]; wchar_t* ret = new wchar_t[len + 1];
MultiByteToWideChar(CP_UTF8, 0, src, -1, ret, len + 1); MultiByteToWideChar(CP_UTF8, 0, src, -1, ret, len + 1);
return ret; return ret;
} }
char* cairo_font::wchar_to_utf8( const wchar_t* src ) char* cairo_font::wchar_to_utf8(const wchar_t* src) {
{ if (!src) return NULL;
if(!src) return NULL;
int len = WideCharToMultiByte(CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL); int len = WideCharToMultiByte(CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL);
char* ret = new char[len]; char* ret = new char[len];
WideCharToMultiByte(CP_UTF8, 0, src, -1, ret, len, NULL, NULL); WideCharToMultiByte(CP_UTF8, 0, src, -1, ret, len, NULL, NULL);
return ret; return ret;
} }

View file

@ -1,117 +1,99 @@
#pragma once #pragma once
#include <windows.h> #include <cairo-win32.h>
#include <stdlib.h> #include <cairo.h>
#include <litehtml.h>
#include <malloc.h> #include <malloc.h>
#include <memory.h> #include <memory.h>
#include <tchar.h>
#include <mlang.h> #include <mlang.h>
#include <stdlib.h>
#include <tchar.h>
#include <windows.h>
#include <vector> #include <vector>
#include <cairo.h>
#include <cairo-win32.h>
#include <litehtml.h>
struct linked_font struct linked_font {
{ typedef std::vector<linked_font*> vector;
typedef std::vector<linked_font*> vector;
DWORD code_pages; DWORD code_pages;
HFONT hFont; HFONT hFont;
cairo_font_face_t* font_face; cairo_font_face_t* font_face;
}; };
struct text_chunk struct text_chunk {
{ typedef std::vector<text_chunk*> vector;
typedef std::vector<text_chunk*> vector;
char* text; char* text;
linked_font* font; linked_font* font;
~text_chunk() ~text_chunk() {
{ if (text) {
if(text) delete text;
{ }
delete text; }
}
}
}; };
struct cairo_font_metrics struct cairo_font_metrics {
{ int height;
int height; int ascent;
int ascent; int descent;
int descent; int x_height;
int x_height;
}; };
class cairo_font {
HFONT m_hFont;
cairo_font_face_t* m_font_face;
IMLangFontLink2* m_font_link;
DWORD m_font_code_pages;
linked_font::vector m_linked_fonts;
int m_size;
BOOL m_bUnderline;
BOOL m_bStrikeOut;
cairo_font_metrics m_metrics;
class cairo_font public:
{ // fonts are not thread safe :(
HFONT m_hFont; // you have to declare and initialize cairo_font::m_sync before the first using.
cairo_font_face_t* m_font_face; static CRITICAL_SECTION m_sync;
IMLangFontLink2* m_font_link;
DWORD m_font_code_pages;
linked_font::vector m_linked_fonts;
int m_size;
BOOL m_bUnderline;
BOOL m_bStrikeOut;
cairo_font_metrics m_metrics;
public:
// fonts are not thread safe :(
// you have to declare and initialize cairo_font::m_sync before the first using.
static CRITICAL_SECTION m_sync;
cairo_font(IMLangFontLink2* fl, HFONT hFont, int size); cairo_font(IMLangFontLink2* fl, HFONT hFont, int size);
cairo_font(IMLangFontLink2* fl, LPCWSTR facename, int size, int weight, BOOL italic, BOOL strikeout, BOOL underline); cairo_font(IMLangFontLink2* fl, LPCWSTR facename, int size, int weight, BOOL italic, BOOL strikeout, BOOL underline);
void init(); void init();
~cairo_font(); ~cairo_font();
void show_text(cairo_t* cr, int x, int y, const litehtml::tchar_t*); void show_text(cairo_t* cr, int x, int y, const litehtml::tchar_t*);
int text_width(cairo_t* cr, const litehtml::tchar_t* str); int text_width(cairo_t* cr, const litehtml::tchar_t* str);
void load_metrics(cairo_t* cr); void load_metrics(cairo_t* cr);
cairo_font_metrics& metrics(); cairo_font_metrics& metrics();
static wchar_t* utf8_to_wchar(const char* src); static wchar_t* utf8_to_wchar(const char* src);
static char* wchar_to_utf8(const wchar_t* src); static char* wchar_to_utf8(const wchar_t* src);
private:
void split_text(const litehtml::tchar_t* str, text_chunk::vector& chunks); private:
void free_text_chunks(text_chunk::vector& chunks); void split_text(const litehtml::tchar_t* str, text_chunk::vector& chunks);
cairo_font_face_t* create_font_face(HFONT fnt); void free_text_chunks(text_chunk::vector& chunks);
void set_font(HFONT hFont); cairo_font_face_t* create_font_face(HFONT fnt);
void clear(); void set_font(HFONT hFont);
int text_width(cairo_t* cr, text_chunk::vector& chunks); void clear();
void lock(); int text_width(cairo_t* cr, text_chunk::vector& chunks);
void unlock(); void lock();
int round_d(double val); void unlock();
void get_metrics(cairo_t* cr, cairo_font_metrics* fm); int round_d(double val);
void get_metrics(cairo_t* cr, cairo_font_metrics* fm);
}; };
inline void cairo_font::lock() inline void cairo_font::lock() { EnterCriticalSection(&m_sync); }
{
EnterCriticalSection(&m_sync); inline void cairo_font::unlock() { LeaveCriticalSection(&m_sync); }
inline int cairo_font::round_d(double val) {
int int_val = (int)val;
if (val - int_val >= 0.5) {
int_val++;
}
return int_val;
} }
inline void cairo_font::unlock() inline cairo_font_metrics& cairo_font::metrics() { return m_metrics; }
{
LeaveCriticalSection(&m_sync);
}
inline int cairo_font::round_d(double val) inline void cairo_font::load_metrics(cairo_t* cr) { get_metrics(cr, &m_metrics); }
{
int int_val = (int) val;
if(val - int_val >= 0.5)
{
int_val++;
}
return int_val;
}
inline cairo_font_metrics& cairo_font::metrics()
{
return m_metrics;
}
inline void cairo_font::load_metrics(cairo_t* cr)
{
get_metrics(cr, &m_metrics);
}

View file

@ -1,198 +1,159 @@
#include "gdiplus_container.h" #include "gdiplus_container.h"
gdiplus_container::gdiplus_container(void) gdiplus_container::gdiplus_container(void) {}
{
gdiplus_container::~gdiplus_container(void) {}
void gdiplus_container::draw_ellipse(HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color, int line_width) {
Gdiplus::Graphics graphics(hdc);
Gdiplus::LinearGradientBrush* brush = NULL;
graphics.SetCompositingQuality(Gdiplus::CompositingQualityHighQuality);
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
Gdiplus::Pen pen(Gdiplus::Color(color.alpha, color.red, color.green, color.blue));
graphics.DrawEllipse(&pen, x, y, width, height);
} }
gdiplus_container::~gdiplus_container(void) void gdiplus_container::fill_ellipse(HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color) {
{ Gdiplus::Graphics graphics(hdc);
graphics.SetCompositingQuality(Gdiplus::CompositingQualityHighQuality);
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
Gdiplus::SolidBrush brush(Gdiplus::Color(color.alpha, color.red, color.green, color.blue));
graphics.FillEllipse(&brush, x, y, width, height);
} }
void gdiplus_container::draw_ellipse( HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color, int line_width ) void gdiplus_container::fill_rect(HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color, const litehtml::css_border_radius& radius) {
{ Gdiplus::Graphics graphics(hdc);
Gdiplus::Graphics graphics(hdc);
Gdiplus::LinearGradientBrush* brush = NULL;
graphics.SetCompositingQuality(Gdiplus::CompositingQualityHighQuality); Gdiplus::SolidBrush brush(Gdiplus::Color(color.alpha, color.red, color.green, color.blue));
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias); graphics.FillRectangle(&brush, x, y, width, height);
Gdiplus::Pen pen( Gdiplus::Color(color.alpha, color.red, color.green, color.blue) );
graphics.DrawEllipse(&pen, x, y, width, height);
} }
void gdiplus_container::fill_ellipse( HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color ) void gdiplus_container::get_img_size(litehtml::uint_ptr img, litehtml::size& sz) {
{ Gdiplus::Bitmap* bmp = (Gdiplus::Bitmap*)img;
Gdiplus::Graphics graphics(hdc); if (bmp) {
sz.width = bmp->GetWidth();
graphics.SetCompositingQuality(Gdiplus::CompositingQualityHighQuality); sz.height = bmp->GetHeight();
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias); }
Gdiplus::SolidBrush brush( Gdiplus::Color(color.alpha, color.red, color.green, color.blue) );
graphics.FillEllipse(&brush, x, y, width, height);
} }
void gdiplus_container::fill_rect( HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color, const litehtml::css_border_radius& radius ) void gdiplus_container::draw_img(HDC hdc, litehtml::uint_ptr img, const litehtml::position& pos) {
{ Gdiplus::Bitmap* bmp = (Gdiplus::Bitmap*)img;
Gdiplus::Graphics graphics(hdc); if (bmp) {
Gdiplus::Graphics graphics(hdc);
Gdiplus::SolidBrush brush( Gdiplus::Color(color.alpha, color.red, color.green, color.blue) ); graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor);
graphics.FillRectangle(&brush, x, y, width, height); graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
graphics.DrawImage(bmp, pos.x, pos.y, pos.width, pos.height);
}
} }
void gdiplus_container::get_img_size( litehtml::uint_ptr img, litehtml::size& sz ) void gdiplus_container::free_image(litehtml::uint_ptr img) {
{ Gdiplus::Bitmap* bmp = (Gdiplus::Bitmap*)img;
Gdiplus::Bitmap* bmp = (Gdiplus::Bitmap*) img; if (bmp) {
if(bmp) delete bmp;
{ }
sz.width = bmp->GetWidth();
sz.height = bmp->GetHeight();
}
} }
void gdiplus_container::draw_img(HDC hdc, litehtml::uint_ptr img, const litehtml::position& pos ) void gdiplus_container::draw_img_bg(HDC hdc, litehtml::uint_ptr img, const litehtml::position& draw_pos, const litehtml::position& pos, litehtml::background_repeat repeat, litehtml::background_attachment attachment) {
{ Gdiplus::Bitmap* bgbmp = (Gdiplus::Bitmap*)img;
Gdiplus::Bitmap* bmp = (Gdiplus::Bitmap*) img;
if(bmp) int img_width = bgbmp->GetWidth();
{ int img_height = bgbmp->GetHeight();
Gdiplus::Graphics graphics(hdc);
graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor); Gdiplus::Graphics graphics(hdc);
graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf); graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor);
graphics.DrawImage(bmp, pos.x, pos.y, pos.width, pos.height); graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
}
Gdiplus::Region reg(Gdiplus::Rect(draw_pos.left(), draw_pos.top(), draw_pos.width, draw_pos.height));
graphics.SetClip(&reg);
switch (repeat) {
case litehtml::background_repeat_no_repeat: {
graphics.DrawImage(bgbmp, pos.x, pos.y, bgbmp->GetWidth(), bgbmp->GetHeight());
} break;
case litehtml::background_repeat_repeat_x: {
Gdiplus::CachedBitmap bmp(bgbmp, &graphics);
for (int x = pos.left(); x < pos.right(); x += bgbmp->GetWidth()) {
graphics.DrawCachedBitmap(&bmp, x, pos.top());
}
for (int x = pos.left() - bgbmp->GetWidth(); x + (int)bgbmp->GetWidth() > draw_pos.left(); x -= bgbmp->GetWidth()) {
graphics.DrawCachedBitmap(&bmp, x, pos.top());
}
} break;
case litehtml::background_repeat_repeat_y: {
Gdiplus::CachedBitmap bmp(bgbmp, &graphics);
for (int y = pos.top(); y < pos.bottom(); y += bgbmp->GetHeight()) {
graphics.DrawCachedBitmap(&bmp, pos.left(), y);
}
for (int y = pos.top() - bgbmp->GetHeight(); y + (int)bgbmp->GetHeight() > draw_pos.top(); y -= bgbmp->GetHeight()) {
graphics.DrawCachedBitmap(&bmp, pos.left(), y);
}
} break;
case litehtml::background_repeat_repeat: {
Gdiplus::CachedBitmap bmp(bgbmp, &graphics);
if (bgbmp->GetHeight() >= 0) {
for (int x = pos.left(); x < pos.right(); x += bgbmp->GetWidth()) {
for (int y = pos.top(); y < pos.bottom(); y += bgbmp->GetHeight()) {
graphics.DrawCachedBitmap(&bmp, x, y);
}
}
}
} break;
}
} }
void gdiplus_container::free_image( litehtml::uint_ptr img ) void gdiplus_container::draw_borders(litehtml::uint_ptr hdc, const litehtml::css_borders& borders, const litehtml::position& draw_pos) {
{ apply_clip((HDC)hdc);
Gdiplus::Bitmap* bmp = (Gdiplus::Bitmap*) img;
if(bmp) // draw left border
{ if (borders.left.width.val() != 0 && borders.left.style > litehtml::border_style_hidden) {
delete bmp; HPEN pen = CreatePen(PS_SOLID, 1, RGB(borders.left.color.red, borders.left.color.green, borders.left.color.blue));
} HPEN oldPen = (HPEN)SelectObject((HDC)hdc, pen);
} for (int x = 0; x < borders.left.width.val(); x++) {
MoveToEx((HDC)hdc, draw_pos.left() + x, draw_pos.top(), NULL);
void gdiplus_container::draw_img_bg( HDC hdc, litehtml::uint_ptr img, const litehtml::position& draw_pos, const litehtml::position& pos, litehtml::background_repeat repeat, litehtml::background_attachment attachment ) LineTo((HDC)hdc, draw_pos.left() + x, draw_pos.bottom());
{ }
Gdiplus::Bitmap* bgbmp = (Gdiplus::Bitmap*) img; SelectObject((HDC)hdc, oldPen);
DeleteObject(pen);
int img_width = bgbmp->GetWidth(); }
int img_height = bgbmp->GetHeight(); // draw right border
if (borders.right.width.val() != 0 && borders.right.style > litehtml::border_style_hidden) {
Gdiplus::Graphics graphics(hdc); HPEN pen = CreatePen(PS_SOLID, 1, RGB(borders.right.color.red, borders.right.color.green, borders.right.color.blue));
graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor); HPEN oldPen = (HPEN)SelectObject((HDC)hdc, pen);
graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf); for (int x = 0; x < borders.right.width.val(); x++) {
MoveToEx((HDC)hdc, draw_pos.right() - x - 1, draw_pos.top(), NULL);
Gdiplus::Region reg(Gdiplus::Rect(draw_pos.left(), draw_pos.top(), draw_pos.width, draw_pos.height)); LineTo((HDC)hdc, draw_pos.right() - x - 1, draw_pos.bottom());
graphics.SetClip(&reg); }
SelectObject((HDC)hdc, oldPen);
switch(repeat) DeleteObject(pen);
{ }
case litehtml::background_repeat_no_repeat: // draw top border
{ if (borders.top.width.val() != 0 && borders.top.style > litehtml::border_style_hidden) {
graphics.DrawImage(bgbmp, pos.x, pos.y, bgbmp->GetWidth(), bgbmp->GetHeight()); HPEN pen = CreatePen(PS_SOLID, 1, RGB(borders.top.color.red, borders.top.color.green, borders.top.color.blue));
} HPEN oldPen = (HPEN)SelectObject((HDC)hdc, pen);
break; for (int y = 0; y < borders.top.width.val(); y++) {
case litehtml::background_repeat_repeat_x: MoveToEx((HDC)hdc, draw_pos.left(), draw_pos.top() + y, NULL);
{ LineTo((HDC)hdc, draw_pos.right(), draw_pos.top() + y);
Gdiplus::CachedBitmap bmp(bgbmp, &graphics); }
for(int x = pos.left(); x < pos.right(); x += bgbmp->GetWidth()) SelectObject((HDC)hdc, oldPen);
{ DeleteObject(pen);
graphics.DrawCachedBitmap(&bmp, x, pos.top()); }
} // draw bottom border
if (borders.bottom.width.val() != 0 && borders.bottom.style > litehtml::border_style_hidden) {
for(int x = pos.left() - bgbmp->GetWidth(); x + (int) bgbmp->GetWidth() > draw_pos.left(); x -= bgbmp->GetWidth()) HPEN pen = CreatePen(PS_SOLID, 1, RGB(borders.bottom.color.red, borders.bottom.color.green, borders.bottom.color.blue));
{ HPEN oldPen = (HPEN)SelectObject((HDC)hdc, pen);
graphics.DrawCachedBitmap(&bmp, x, pos.top()); for (int y = 0; y < borders.bottom.width.val(); y++) {
} MoveToEx((HDC)hdc, draw_pos.left(), draw_pos.bottom() - y - 1, NULL);
} LineTo((HDC)hdc, draw_pos.right(), draw_pos.bottom() - y - 1);
break; }
case litehtml::background_repeat_repeat_y: SelectObject((HDC)hdc, oldPen);
{ DeleteObject(pen);
Gdiplus::CachedBitmap bmp(bgbmp, &graphics); }
for(int y = pos.top(); y < pos.bottom(); y += bgbmp->GetHeight())
{ release_clip((HDC)hdc);
graphics.DrawCachedBitmap(&bmp, pos.left(), y);
}
for(int y = pos.top() - bgbmp->GetHeight(); y + (int) bgbmp->GetHeight() > draw_pos.top(); y -= bgbmp->GetHeight())
{
graphics.DrawCachedBitmap(&bmp, pos.left(), y);
}
}
break;
case litehtml::background_repeat_repeat:
{
Gdiplus::CachedBitmap bmp(bgbmp, &graphics);
if(bgbmp->GetHeight() >= 0)
{
for(int x = pos.left(); x < pos.right(); x += bgbmp->GetWidth())
{
for(int y = pos.top(); y < pos.bottom(); y += bgbmp->GetHeight())
{
graphics.DrawCachedBitmap(&bmp, x, y);
}
}
}
}
break;
}
}
void gdiplus_container::draw_borders( litehtml::uint_ptr hdc, const litehtml::css_borders& borders, const litehtml::position& draw_pos )
{
apply_clip((HDC) hdc);
// draw left border
if(borders.left.width.val() != 0 && borders.left.style > litehtml::border_style_hidden)
{
HPEN pen = CreatePen(PS_SOLID, 1, RGB(borders.left.color.red, borders.left.color.green, borders.left.color.blue));
HPEN oldPen = (HPEN) SelectObject((HDC) hdc, pen);
for(int x = 0; x < borders.left.width.val(); x++)
{
MoveToEx((HDC) hdc, draw_pos.left() + x, draw_pos.top(), NULL);
LineTo((HDC) hdc, draw_pos.left() + x, draw_pos.bottom());
}
SelectObject((HDC) hdc, oldPen);
DeleteObject(pen);
}
// draw right border
if(borders.right.width.val() != 0 && borders.right.style > litehtml::border_style_hidden)
{
HPEN pen = CreatePen(PS_SOLID, 1, RGB(borders.right.color.red, borders.right.color.green, borders.right.color.blue));
HPEN oldPen = (HPEN) SelectObject((HDC) hdc, pen);
for(int x = 0; x < borders.right.width.val(); x++)
{
MoveToEx((HDC) hdc, draw_pos.right() - x - 1, draw_pos.top(), NULL);
LineTo((HDC) hdc, draw_pos.right() - x - 1, draw_pos.bottom());
}
SelectObject((HDC) hdc, oldPen);
DeleteObject(pen);
}
// draw top border
if(borders.top.width.val() != 0 && borders.top.style > litehtml::border_style_hidden)
{
HPEN pen = CreatePen(PS_SOLID, 1, RGB(borders.top.color.red, borders.top.color.green, borders.top.color.blue));
HPEN oldPen = (HPEN) SelectObject((HDC) hdc, pen);
for(int y = 0; y < borders.top.width.val(); y++)
{
MoveToEx((HDC) hdc, draw_pos.left(), draw_pos.top() + y, NULL);
LineTo((HDC) hdc, draw_pos.right(), draw_pos.top() + y);
}
SelectObject((HDC) hdc, oldPen);
DeleteObject(pen);
}
// draw bottom border
if(borders.bottom.width.val() != 0 && borders.bottom.style > litehtml::border_style_hidden)
{
HPEN pen = CreatePen(PS_SOLID, 1, RGB(borders.bottom.color.red, borders.bottom.color.green, borders.bottom.color.blue));
HPEN oldPen = (HPEN) SelectObject((HDC) hdc, pen);
for(int y = 0; y < borders.bottom.width.val(); y++)
{
MoveToEx((HDC) hdc, draw_pos.left(), draw_pos.bottom() - y - 1, NULL);
LineTo((HDC) hdc, draw_pos.right(), draw_pos.bottom() - y - 1);
}
SelectObject((HDC) hdc, oldPen);
DeleteObject(pen);
}
release_clip((HDC) hdc);
} }

View file

@ -1,29 +1,29 @@
#pragma once #pragma once
#include <windows.h> #include <GdiPlus.h>
#include <mlang.h> #include <litehtml.h>
#include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <memory.h> #include <memory.h>
#include <tchar.h>
#include <mlang.h> #include <mlang.h>
#include <stdlib.h>
#include <tchar.h>
#include <windows.h>
#include <vector> #include <vector>
#include <litehtml.h>
#include <GdiPlus.h>
#include "..\win32\win32_container.h" #include "..\win32\win32_container.h"
class gdiplus_container : public litehtml::win32_container class gdiplus_container : public litehtml::win32_container {
{ public:
public: gdiplus_container(void);
gdiplus_container(void); virtual ~gdiplus_container(void);
virtual ~gdiplus_container(void);
protected: protected:
virtual void draw_ellipse(HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color, int line_width); virtual void draw_ellipse(HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color, int line_width);
virtual void fill_ellipse(HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color); virtual void fill_ellipse(HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color);
virtual void fill_rect(HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color, const litehtml::css_border_radius& radius); virtual void fill_rect(HDC hdc, int x, int y, int width, int height, const litehtml::web_color& color, const litehtml::css_border_radius& radius);
virtual void get_img_size(litehtml::uint_ptr img, litehtml::size& sz); virtual void get_img_size(litehtml::uint_ptr img, litehtml::size& sz);
virtual void draw_img(HDC hdc, litehtml::uint_ptr img, const litehtml::position& pos); virtual void draw_img(HDC hdc, litehtml::uint_ptr img, const litehtml::position& pos);
virtual void free_image(litehtml::uint_ptr img); virtual void free_image(litehtml::uint_ptr img);
virtual void draw_img_bg(HDC hdc, litehtml::uint_ptr img, const litehtml::position& draw_pos, const litehtml::position& pos, litehtml::background_repeat repeat, litehtml::background_attachment attachment); virtual void draw_img_bg(HDC hdc, litehtml::uint_ptr img, const litehtml::position& draw_pos, const litehtml::position& pos, litehtml::background_repeat repeat, litehtml::background_attachment attachment);
virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::css_borders& borders, const litehtml::position& draw_pos); virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::css_borders& borders, const litehtml::position& draw_pos);
}; };

View file

@ -6,563 +6,425 @@
*/ */
#include "container_haiku.h" #include "container_haiku.h"
#include <string>
#include <iostream>
#include <fstream>
#include <streambuf>
#include <map>
#include <Bitmap.h> #include <Bitmap.h>
#include <String.h>
#include <Entry.h> #include <Entry.h>
#include <Path.h> #include <Path.h>
#include <String.h>
#include <TranslationUtils.h> #include <TranslationUtils.h>
LiteHtmlView::LiteHtmlView(BRect frame, const char *name) #include <fstream>
: BView(frame, name, B_FOLLOW_ALL, B_WILL_DRAW), #include <iostream>
fContext(NULL), #include <map>
m_html(NULL), #include <streambuf>
m_images(), #include <string>
m_base_url(),
m_url() LiteHtmlView::LiteHtmlView(BRect frame, const char* name) : BView(frame, name, B_FOLLOW_ALL, B_WILL_DRAW), fContext(NULL), m_html(NULL), m_images(), m_base_url(), m_url() {
{ BRect bounds(Bounds());
BPoint topLeft = bounds.LeftTop();
BRect bounds(Bounds()); std::cout << "Initial bounds: topLeft x: " << +topLeft.x << ", y: " << +topLeft.y << ", width: " << +bounds.Width() << ", height: " << +bounds.Height() << std::endl;
BPoint topLeft = bounds.LeftTop();
std::cout << "Initial bounds: topLeft x: " << +topLeft.x << ", y: " SetDrawingMode(B_OP_OVER);
<< +topLeft.y << ", width: " << +bounds.Width() << ", height: " SetFont(be_plain_font);
<< +bounds.Height() << std::endl;
// FillRect(bounds,B_SOLID_LOW);
SetDrawingMode(B_OP_OVER);
SetFont(be_plain_font); // SetLowColor(B_DOCUMENT_PANEL_COLOR);
// FillRect(rect);
//FillRect(bounds,B_SOLID_LOW);
//SetLowColor(B_DOCUMENT_PANEL_COLOR);
//FillRect(rect);
} }
LiteHtmlView::~LiteHtmlView() LiteHtmlView::~LiteHtmlView() {}
{
void LiteHtmlView::SetContext(litehtml::context* ctx) { fContext = ctx; }
void LiteHtmlView::RenderFile(const char* localFilePath) {
std::cout << "RenderFile" << std::endl;
// BUrlRequest req;
// assume a local file for now, that is HTML
std::ifstream t(localFilePath);
std::string html((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
// std::cout << "HTML output:-" << std::endl << html << std::endl;
// Get parent folder for the base url
std::cout << "Loaded from file: " << localFilePath << std::endl;
BPath htmlPath(localFilePath);
BPath dirPath;
htmlPath.GetParent(&dirPath);
std::cout << "parent path: " << dirPath.Path() << std::endl;
set_base_url(dirPath.Path());
// std::cout << " base url now:" << m_base_url << std::endl;
RenderHTML(html);
} }
void void LiteHtmlView::RenderHTML(const std::string& htmlText) {
LiteHtmlView::SetContext(litehtml::context* ctx) std::cout << "RenderHTML" << std::endl;
{
fContext = ctx; // now use this string
m_html = litehtml::document::createFromString(htmlText.c_str(), this, fContext);
if (m_html) {
std::cout << "Successfully read html" << std::endl;
// success
// post-parse render operations, if required.
Invalidate();
} else {
std::cout << "Failed to read html" << std::endl;
}
// always fire the rendering complete message
std::cout << "Sending html rendered message: " << M_HTML_RENDERED << std::endl;
} }
void void LiteHtmlView::Draw(BRect b) {
LiteHtmlView::RenderFile(const char* localFilePath) std::cout << "DRAW CALLED" << std::endl;
{
std::cout << "RenderFile" << std::endl; BRect bounds(Bounds());
//BUrlRequest req; FillRect(bounds, B_SOLID_LOW);
// assume a local file for now, that is HTML
std::ifstream t(localFilePath); // b only part of the window, but we need to draw the whole lot
std::string html((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>()); if (NULL != m_html) {
BPoint leftTop = bounds.LeftTop();
//std::cout << "HTML output:-" << std::endl << html << std::endl; litehtml::position clip(leftTop.x, leftTop.y, bounds.Width(), bounds.Height());
m_html->render(bounds.Width());
// Get parent folder for the base url m_html->draw((litehtml::uint_ptr)this, 0, 0, &clip);
std::cout << "Loaded from file: " << localFilePath << std::endl; }
BPath htmlPath(localFilePath); SendNotices(M_HTML_RENDERED, new BMessage(M_HTML_RENDERED));
BPath dirPath;
htmlPath.GetParent(&dirPath);
std::cout << "parent path: " << dirPath.Path() << std::endl;
set_base_url(dirPath.Path());
//std::cout << " base url now:" << m_base_url << std::endl;
RenderHTML(html);
} }
void void LiteHtmlView::GetPreferredSize(float* width, float* height) {
LiteHtmlView::RenderHTML(const std::string& htmlText) if (NULL == m_html) {
{ BRect bounds(Bounds());
std::cout << "RenderHTML" << std::endl; *width = bounds.Width();
*height = bounds.Height();
// now use this string } else {
m_html = litehtml::document::createFromString( *width = m_html->width();
htmlText.c_str(), this, fContext); *height = m_html->height();
if (m_html) }
{
std::cout << "Successfully read html" << std::endl;
// success
// post-parse render operations, if required.
Invalidate();
} else {
std::cout << "Failed to read html" << std::endl;
}
// always fire the rendering complete message
std::cout << "Sending html rendered message: " << M_HTML_RENDERED << std::endl;
} }
void litehtml::uint_ptr LiteHtmlView::create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) {
LiteHtmlView::Draw(BRect b) // std::cout << "create_font" << std::endl;
{ litehtml::string_vector fonts;
std::cout << "DRAW CALLED" << std::endl; litehtml::split_string(faceName, fonts, ",");
litehtml::trim(fonts[0]);
BRect bounds(Bounds());
FillRect(bounds,B_SOLID_LOW);
// b only part of the window, but we need to draw the whole lot
if (NULL != m_html) {
BPoint leftTop = bounds.LeftTop();
litehtml::position clip(leftTop.x,leftTop.y,
bounds.Width(),bounds.Height());
m_html->render(bounds.Width());
m_html->draw((litehtml::uint_ptr) this,0,0,&clip);
}
SendNotices(M_HTML_RENDERED,new BMessage(M_HTML_RENDERED));
}
void uint16 face = B_REGULAR_FACE; // default
LiteHtmlView::GetPreferredSize(float* width,float* height) if (italic == litehtml::fontStyleItalic) {
{ face |= B_ITALIC_FACE;
if (NULL == m_html) }
{ if (decoration & litehtml::font_decoration_underline) {
BRect bounds(Bounds()); face |= B_UNDERSCORE_FACE;
*width = bounds.Width(); }
*height = bounds.Height(); if (decoration & litehtml::font_decoration_linethrough) {
} else { face |= B_STRIKEOUT_FACE;
*width = m_html->width(); }
*height = m_html->height(); // Note: LIGHT, HEAVY, CONDENSED not supported in BeOS R5
}
}
litehtml::uint_ptr
LiteHtmlView::create_font( const litehtml::tchar_t* faceName, int size,
int weight, litehtml::font_style italic, unsigned int decoration,
litehtml::font_metrics* fm )
{
//std::cout << "create_font" << std::endl;
litehtml::string_vector fonts;
litehtml::split_string(faceName, fonts, ",");
litehtml::trim(fonts[0]);
uint16 face = B_REGULAR_FACE; // default
if (italic == litehtml::fontStyleItalic)
{
face |= B_ITALIC_FACE;
}
if (decoration & litehtml::font_decoration_underline)
{
face |= B_UNDERSCORE_FACE;
}
if (decoration & litehtml::font_decoration_linethrough)
{
face |= B_STRIKEOUT_FACE;
}
// Note: LIGHT, HEAVY, CONDENSED not supported in BeOS R5
#ifdef __HAIKU__ #ifdef __HAIKU__
if(weight >= 0 && weight < 150) face |= B_LIGHT_FACE; if (weight >= 0 && weight < 150)
else if(weight >= 150 && weight < 250) face |= B_LIGHT_FACE; face |= B_LIGHT_FACE;
else if(weight >= 250 && weight < 350) face |= B_LIGHT_FACE; else if (weight >= 150 && weight < 250)
//else if(weight >= 350 && weight < 450) face |= B_REGULAR_FACE; face |= B_LIGHT_FACE;
//else if(weight >= 450 && weight < 550) face |= B_REGULAR_FACE; else if (weight >= 250 && weight < 350)
else if(weight >= 550 && weight < 650) face |= B_CONDENSED_FACE; face |= B_LIGHT_FACE;
// else if(weight >= 350 && weight < 450) face |= B_REGULAR_FACE;
// else if(weight >= 450 && weight < 550) face |= B_REGULAR_FACE;
else if (weight >= 550 && weight < 650)
face |= B_CONDENSED_FACE;
#else #else
else if(weight >= 550 && weight < 650) face |= B_BOLD_FACE; else if (weight >= 550 && weight < 650)
face |= B_BOLD_FACE;
#endif #endif
else if(weight >= 650 && weight < 750) face |= B_BOLD_FACE; else if (weight >= 650 && weight < 750)
face |= B_BOLD_FACE;
#ifndef __HAIKU__ #ifndef __HAIKU__
else if(weight >= 750 && weight < 850) face |= B_BOLD_FACE; else if (weight >= 750 && weight < 850)
else if(weight >= 950) face |= B_BOLD_FACE; face |= B_BOLD_FACE;
else if (weight >= 950)
face |= B_BOLD_FACE;
#else #else
else if(weight >= 750 && weight < 850) face |= B_HEAVY_FACE; else if (weight >= 750 && weight < 850)
else if(weight >= 950) face |= B_HEAVY_FACE; face |= B_HEAVY_FACE;
else if (weight >= 950)
face |= B_HEAVY_FACE;
#endif #endif
BFont* tempFont = new BFont(); BFont* tempFont = new BFont();
bool found = false; bool found = false;
for(litehtml::string_vector::iterator i = fonts.begin(); for (litehtml::string_vector::iterator i = fonts.begin(); i != fonts.end(); i++) {
i != fonts.end(); i++) if (B_OK == tempFont->SetFamilyAndFace(i->c_str(), face)) {
{ found = true;
if (B_OK == tempFont->SetFamilyAndFace(i->c_str(),face)) break;
{ }
found = true; }
break;
} if (!found) {
} // default to the Be plain font
tempFont = new BFont(be_plain_font);
if (!found) if (weight >= 550) {
{ tempFont = new BFont(be_bold_font);
// default to the Be plain font }
tempFont = new BFont(be_plain_font); tempFont->SetFace(face); // chooses closest
if (weight >= 550) }
{
tempFont = new BFont(be_bold_font); tempFont->SetSize(size);
}
tempFont->SetFace(face); // chooses closest font_height hgt;
} tempFont->GetHeight(&hgt);
fm->ascent = hgt.ascent;
tempFont->SetSize(size); fm->descent = hgt.descent;
fm->height = (int)(hgt.ascent + hgt.descent);
font_height hgt; fm->x_height = (int)hgt.leading;
tempFont->GetHeight(&hgt);
fm->ascent = hgt.ascent; return (litehtml::uint_ptr)tempFont;
fm->descent = hgt.descent;
fm->height = (int) (hgt.ascent + hgt.descent);
fm->x_height = (int) hgt.leading;
return (litehtml::uint_ptr) tempFont;
} }
void void LiteHtmlView::delete_font(litehtml::uint_ptr hFont) { std::cout << "delete_font" << std::endl; }
LiteHtmlView::delete_font( litehtml::uint_ptr hFont )
{ int LiteHtmlView::text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) {
std::cout << "delete_font" << std::endl; // std::cout << "text_width" << std::endl;
BFont* fnt = (BFont*)hFont;
int width = fnt->StringWidth(text);
// std::cout << " Width: " << +width << std::endl;
return width;
} }
int void LiteHtmlView::draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) {
LiteHtmlView::text_width( const litehtml::tchar_t* text, // std::cout << "draw_text" << std::endl;
litehtml::uint_ptr hFont ) if (!text) return;
{ if (0 == strlen(text)) return;
//std::cout << "text_width" << std::endl; BFont* fnt = (BFont*)hFont;
BFont* fnt = (BFont*)hFont;
int width = fnt->StringWidth(text); // std::cout << " left: " << +pos.left() << ", top: " << +pos.top() << std::endl;
//std::cout << " Width: " << +width << std::endl; // std::cout << " RGBA: " << +color.red << "," << +color.green << "," << +color.blue << "," << +color.alpha << std::endl;
return width; // std::cout << " Font size: " << +fnt->Size() << std::endl;
// std::cout << " Text: " << text << std::endl;
BRect bounds(Bounds());
// FillRect(bounds,B_SOLID_LOW);
BPoint leftTop = bounds.LeftTop();
// std::cout << " Bounds left: " << +leftTop.x << ", top: " << +leftTop.y << ", Width: " << +bounds.Width() << ", Height: " << +bounds.Height() << std::endl;
font_height fh;
fnt->GetHeight(&fh);
int baseline = fh.ascent + fh.descent; // + 10;
int leftbase = 0; // 10;
MovePenTo(pos.left() + leftbase, pos.top() + baseline); // leftTop.x,leftTop.y);
SetFont(fnt);
// SetFont(be_plain_font);
rgb_color clr = ui_color(B_DOCUMENT_TEXT_COLOR);
/*
rgb_color clr;
clr.blue = 40;
clr.red = 40;
clr.green = 40;
*/
clr.red = color.red;
clr.green = color.green;
clr.blue = color.blue;
clr.alpha = color.alpha;
// std::cout << " Final RGBA: " << +clr.red << "," << +clr.green << "," << +clr.blue << "," << +clr.alpha << std::endl;
SetHighColor(clr);
SetLowColor(ui_color(B_DOCUMENT_BACKGROUND_COLOR));
BString mystr("");
// mystr << "text: ";
mystr << text;
DrawString(mystr);
} }
void int LiteHtmlView::pt_to_px(int pt) {
LiteHtmlView::draw_text( litehtml::uint_ptr hdc, const litehtml::tchar_t* text, std::cout << "pt_to_px" << std::endl;
litehtml::uint_ptr hFont, litehtml::web_color color, return (int)((double)pt * 1.3333333333);
const litehtml::position& pos )
{
//std::cout << "draw_text" << std::endl;
if (!text) return;
if (0 == strlen(text)) return;
BFont* fnt = (BFont*)hFont;
//std::cout << " left: " << +pos.left() << ", top: " << +pos.top() << std::endl;
//std::cout << " RGBA: " << +color.red << "," << +color.green << "," << +color.blue << "," << +color.alpha << std::endl;
//std::cout << " Font size: " << +fnt->Size() << std::endl;
//std::cout << " Text: " << text << std::endl;
BRect bounds(Bounds());
//FillRect(bounds,B_SOLID_LOW);
BPoint leftTop = bounds.LeftTop();
//std::cout << " Bounds left: " << +leftTop.x << ", top: " << +leftTop.y << ", Width: " << +bounds.Width() << ", Height: " << +bounds.Height() << std::endl;
font_height fh;
fnt->GetHeight(&fh);
int baseline = fh.ascent + fh.descent;// + 10;
int leftbase = 0; //10;
MovePenTo(pos.left() + leftbase,pos.top() + baseline);//leftTop.x,leftTop.y);
SetFont(fnt);
//SetFont(be_plain_font);
rgb_color clr = ui_color(B_DOCUMENT_TEXT_COLOR);
/*
rgb_color clr;
clr.blue = 40;
clr.red = 40;
clr.green = 40;
*/
clr.red = color.red;
clr.green = color.green;
clr.blue = color.blue;
clr.alpha = color.alpha;
//std::cout << " Final RGBA: " << +clr.red << "," << +clr.green << "," << +clr.blue << "," << +clr.alpha << std::endl;
SetHighColor(clr);
SetLowColor(ui_color(B_DOCUMENT_BACKGROUND_COLOR));
BString mystr("");
//mystr << "text: ";
mystr << text;
DrawString(mystr);
} }
int int LiteHtmlView::get_default_font_size() const {
LiteHtmlView::pt_to_px( int pt ) // std::cout << "get_default_font_size" << std::endl;
{ return 12;
std::cout << "pt_to_px" << std::endl;
return (int) ((double) pt * 1.3333333333);
} }
int const litehtml::tchar_t* LiteHtmlView::get_default_font_name() const {
LiteHtmlView::get_default_font_size() const // std::cout << "get_default_font_name" << std::endl;
{ font_family fam;
//std::cout << "get_default_font_size" << std::endl; font_style style;
return 12; be_plain_font->GetFamilyAndStyle(&fam, &style);
char* cp = strdup(fam);
return (litehtml::tchar_t*)cp;
} }
const litehtml::tchar_t* void LiteHtmlView::draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) {
LiteHtmlView::get_default_font_name() const std::cout << "draw_list_marker" << std::endl;
{ if (!marker.image.empty()) {
//std::cout << "get_default_font_name" << std::endl; std::cout << " image marker" << std::endl;
font_family fam; }
font_style style;
be_plain_font->GetFamilyAndStyle(&fam,&style);
char* cp = strdup(fam);
return (litehtml::tchar_t*)cp;
} }
void void LiteHtmlView::load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) {
LiteHtmlView::draw_list_marker( litehtml::uint_ptr hdc, std::cout << "load_image" << std::endl;
const litehtml::list_marker& marker )
{ std::string url;
std::cout << "draw_list_marker" << std::endl; make_url(src, baseurl, url);
if (!marker.image.empty()) if (m_images.find(url.c_str()) == m_images.end()) {
{ BEntry entry(url.c_str(), true);
std::cout << " image marker" << std::endl; if (entry.Exists()) {
} std::cout << " Loading bitmap from file" << std::endl;
BBitmap* img = BTranslationUtils::GetBitmap(url.c_str());
m_images[url] = img;
}
}
} }
void void LiteHtmlView::make_url(const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out) {
LiteHtmlView::load_image( const litehtml::tchar_t* src, std::cout << "make_url" << std::endl;
const litehtml::tchar_t* baseurl, bool redraw_on_ready ) std::cout << " url: " << url << std::endl;
{ if (!basepath || (basepath && !basepath[0])) {
std::cout << "load_image" << std::endl; if (!m_base_url.empty()) {
// out = urljoin(m_base_url, std::string(url));
std::string url; std::string ns(m_base_url);
make_url(src, baseurl, url); ns += "/";
if(m_images.find(url.c_str()) == m_images.end()) ns += url;
{ out = ns;
BEntry entry(url.c_str(), true); } else {
if (entry.Exists()) { out = url;
std::cout << " Loading bitmap from file" << std::endl; }
BBitmap* img = BTranslationUtils::GetBitmap(url.c_str()); } else {
m_images[url] = img; std::cout << " basepath: " << basepath << std::endl;
} // out = urljoin(std::string(basepath), std::string(url));
} std::string ns(basepath);
ns += "/";
ns += url;
out = ns;
}
std::cout << " Output url: " << out << std::endl;
} }
void void LiteHtmlView::set_base_url(const litehtml::tchar_t* base_url) {
LiteHtmlView::make_url(const litehtml::tchar_t* url, std::cout << "set_base_url" << std::endl;
const litehtml::tchar_t* basepath, litehtml::tstring& out) /*
{ if(base_url)
std::cout << "make_url" << std::endl; {
std::cout << " url: " << url << std::endl; m_base_url = urljoin(m_url, std::string(base_url));
if(!basepath || (basepath && !basepath[0])) } else
{ {
if(!m_base_url.empty()) */
{ m_base_url = base_url;
//out = urljoin(m_base_url, std::string(url)); std::cout << " base url set to: " << m_base_url << std::endl;
std::string ns(m_base_url); //}
ns += "/";
ns += url;
out = ns;
} else
{
out = url;
}
} else
{
std::cout << " basepath: " << basepath << std::endl;
//out = urljoin(std::string(basepath), std::string(url));
std::string ns(basepath);
ns += "/";
ns += url;
out = ns;
}
std::cout << " Output url: " << out << std::endl;
} }
void void LiteHtmlView::get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) {
LiteHtmlView::set_base_url(const litehtml::tchar_t* base_url) std::cout << "get_image_size" << std::endl;
{ std::string url;
std::cout << "set_base_url" << std::endl; make_url(src, NULL, url);
/* const auto& miter(m_images.find(url.c_str()));
if(base_url) if (m_images.end() != miter) {
{ BBitmap* img = (BBitmap*)miter->second;
m_base_url = urljoin(m_url, std::string(base_url)); BRect size = img->Bounds();
} else sz.width = size.Width();
{ sz.height = size.Height();
*/ std::cout << " width: " << +sz.width << ", height: " << +sz.height << std::endl;
m_base_url = base_url; }
std::cout << " base url set to: " << m_base_url << std::endl;
//}
} }
void LiteHtmlView::draw_image(litehtml::uint_ptr hdc, const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, const litehtml::position& pos) {
void std::string url;
LiteHtmlView::get_image_size( const litehtml::tchar_t* src, make_url(src, baseurl, url);
const litehtml::tchar_t* baseurl, litehtml::size& sz ) const auto& img = m_images.find(url.c_str());
{ if (img != m_images.end()) {
std::cout << "get_image_size" << std::endl; if (img->second) {
std::string url; DrawBitmap(img->second, BPoint(pos.x, pos.y)); // TODO support scaling
make_url(src,NULL,url); // draw_txdib(cr, img->second.get(), pos.x, pos.y, pos.width, pos.height);
const auto& miter(m_images.find(url.c_str())); }
if (m_images.end() != miter) }
{
BBitmap* img = (BBitmap*)miter->second;
BRect size = img->Bounds();
sz.width = size.Width();
sz.height = size.Height();
std::cout << " width: " << +sz.width << ", height: " << +sz.height << std::endl;
}
} }
void void LiteHtmlView::draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) {
LiteHtmlView::draw_image( litehtml::uint_ptr hdc, const litehtml::tchar_t* src, std::cout << "draw_background" << std::endl;
const litehtml::tchar_t* baseurl, const litehtml::position& pos ) if (0 < bg.image.length()) {
{ std::cout << " background includes an image!" << std::endl;
std::string url; draw_image(hdc, bg.image.c_str(), m_base_url.c_str(), litehtml::position(bg.position_x, bg.position_y, bg.image_size.width, bg.image_size.height));
make_url(src, baseurl, url); }
const auto& img = m_images.find(url.c_str());
if(img != m_images.end())
{
if(img->second)
{
DrawBitmap(img->second,BPoint(pos.x,pos.y)); // TODO support scaling
//draw_txdib(cr, img->second.get(), pos.x, pos.y, pos.width, pos.height);
}
}
} }
void void LiteHtmlView::draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) {
LiteHtmlView::draw_background( litehtml::uint_ptr hdc, std::cout << "draw_borders" << std::endl;
const litehtml::background_paint& bg ) int bdr_top = 0;
{ int bdr_bottom = 0;
std::cout << "draw_background" << std::endl; int bdr_left = 0;
if (0 < bg.image.length()) int bdr_right = 0;
{
std::cout << " background includes an image!" << std::endl; // std::cout << " uint ptr: " << +hdc << std::endl;
draw_image(hdc,bg.image.c_str(),m_base_url.c_str(),litehtml::position(bg.position_x,bg.position_y,bg.image_size.width,bg.image_size.height)); // std::cout << " this ptr: " << +this << std::endl;
}
if (borders.top.width != 0 && borders.top.style > litehtml::border_style_hidden) {
bdr_top = (int)borders.top.width;
std::cout << " Border top: " << bdr_right << std::endl;
}
if (borders.bottom.width != 0 && borders.bottom.style > litehtml::border_style_hidden) {
bdr_bottom = (int)borders.bottom.width;
std::cout << " Border bottom: " << bdr_right << std::endl;
}
if (borders.left.width != 0 && borders.left.style > litehtml::border_style_hidden) {
bdr_left = (int)borders.left.width;
std::cout << " Border left: " << bdr_right << std::endl;
}
if (borders.right.width != 0 && borders.right.style > litehtml::border_style_hidden) {
bdr_right = (int)borders.right.width;
std::cout << " Border right: " << bdr_right << std::endl;
}
if (bdr_bottom) {
// draw rectangle for now - no check for radius
StrokeRect(BRect(BPoint(draw_pos.left(), draw_pos.bottom()), BPoint(draw_pos.left() + bdr_left, draw_pos.bottom() - bdr_bottom)));
}
} }
void void LiteHtmlView::transform_text(litehtml::tstring& text, litehtml::text_transform tt) { std::cout << "transform_text" << std::endl; }
LiteHtmlView::draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root)
{
std::cout << "draw_borders" << std::endl;
int bdr_top = 0;
int bdr_bottom = 0;
int bdr_left = 0;
int bdr_right = 0;
//std::cout << " uint ptr: " << +hdc << std::endl;
//std::cout << " this ptr: " << +this << std::endl;
if(borders.top.width != 0 && borders.top.style > litehtml::border_style_hidden) void LiteHtmlView::set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) { std::cout << "set_clip" << std::endl; }
{
bdr_top = (int) borders.top.width; void LiteHtmlView::del_clip() { std::cout << "del_clip" << std::endl; }
std::cout << " Border top: " << bdr_right << std::endl;
} std::shared_ptr<litehtml::element> LiteHtmlView::create_element(const litehtml::tchar_t* tag_name, const litehtml::string_map& attributes, const std::shared_ptr<litehtml::document>& doc) {
if(borders.bottom.width != 0 && borders.bottom.style > litehtml::border_style_hidden) // std::cout << "create_element" << std::endl;
{ return 0;
bdr_bottom = (int) borders.bottom.width;
std::cout << " Border bottom: " << bdr_right << std::endl;
}
if(borders.left.width != 0 && borders.left.style > litehtml::border_style_hidden)
{
bdr_left = (int) borders.left.width;
std::cout << " Border left: " << bdr_right << std::endl;
}
if(borders.right.width != 0 && borders.right.style > litehtml::border_style_hidden)
{
bdr_right = (int) borders.right.width;
std::cout << " Border right: " << bdr_right << std::endl;
}
if (bdr_bottom)
{
// draw rectangle for now - no check for radius
StrokeRect(
BRect(
BPoint(draw_pos.left(), draw_pos.bottom()),
BPoint(draw_pos.left() + bdr_left, draw_pos.bottom() - bdr_bottom)
)
);
}
} }
void void LiteHtmlView::get_media_features(litehtml::media_features& media) const {
LiteHtmlView::transform_text(litehtml::tstring& text, litehtml::text_transform tt) std::cout << "get_media_features" << std::endl;
{ litehtml::position client;
std::cout << "transform_text" << std::endl; get_client_rect(client);
media.type = litehtml::media_type_screen;
media.width = client.width;
media.height = client.height;
BRect bounds(Bounds());
media.device_width = bounds.Width();
media.device_height = bounds.Height();
media.color = 8;
media.monochrome = 0;
media.color_index = 256;
media.resolution = 96;
} }
void void LiteHtmlView::link(const std::shared_ptr<litehtml::document>& ptr, const litehtml::element::ptr& el) { std::cout << "link" << std::endl; }
LiteHtmlView::set_clip( const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y )
{ void LiteHtmlView::set_caption(const char* caption) { std::cout << "set_caption" << std::endl; }
std::cout << "set_clip" << std::endl;
void LiteHtmlView::get_client_rect(litehtml::position& client) const {
// std::cout << "get_client_rect" << std::endl;
BRect bounds(Bounds());
BPoint leftTop = bounds.LeftTop();
client.width = bounds.Width();
client.height = bounds.Height();
client.x = leftTop.x;
client.y = leftTop.y;
} }
void void LiteHtmlView::on_anchor_click(const char* base, const litehtml::element::ptr& anchor) { std::cout << "on_anchor_click" << std::endl; }
LiteHtmlView::del_clip()
{
std::cout << "del_clip" << std::endl;
}
void LiteHtmlView::set_cursor(const char* cursor) { std::cout << "set_cursor" << std::endl; }
void LiteHtmlView::import_css(litehtml::tstring& s1, const litehtml::tstring& s2, litehtml::tstring& s3) { std::cout << "import_css" << std::endl; }
std::shared_ptr<litehtml::element> void LiteHtmlView::get_language(litehtml::tstring& s1, litehtml::tstring& s2) const { std::cout << "get_language" << std::endl; }
LiteHtmlView::create_element(const litehtml::tchar_t *tag_name,
const litehtml::string_map &attributes,
const std::shared_ptr<litehtml::document> &doc)
{
//std::cout << "create_element" << std::endl;
return 0;
}
void
LiteHtmlView::get_media_features(litehtml::media_features& media) const
{
std::cout << "get_media_features" << std::endl;
litehtml::position client;
get_client_rect(client);
media.type = litehtml::media_type_screen;
media.width = client.width;
media.height = client.height;
BRect bounds(Bounds());
media.device_width = bounds.Width();
media.device_height = bounds.Height();
media.color = 8;
media.monochrome = 0;
media.color_index = 256;
media.resolution = 96;
}
void
LiteHtmlView::link(const std::shared_ptr<litehtml::document> &ptr, const litehtml::element::ptr& el)
{
std::cout << "link" << std::endl;
}
void
LiteHtmlView::set_caption(const char* caption)
{
std::cout << "set_caption" << std::endl;
}
void
LiteHtmlView::get_client_rect(litehtml::position& client) const
{
//std::cout << "get_client_rect" << std::endl;
BRect bounds(Bounds());
BPoint leftTop = bounds.LeftTop();
client.width = bounds.Width();
client.height = bounds.Height();
client.x = leftTop.x;
client.y = leftTop.y;
}
void
LiteHtmlView::on_anchor_click(const char* base, const litehtml::element::ptr& anchor)
{
std::cout << "on_anchor_click" << std::endl;
}
void
LiteHtmlView::set_cursor(const char* cursor)
{
std::cout << "set_cursor" << std::endl;
}
void
LiteHtmlView::import_css(litehtml::tstring& s1, const litehtml::tstring& s2, litehtml::tstring& s3)
{
std::cout << "import_css" << std::endl;
}
void
LiteHtmlView::get_language(litehtml::tstring& s1, litehtml::tstring& s2) const
{
std::cout << "get_language" << std::endl;
}

View file

@ -7,81 +7,73 @@
#ifndef LITEHTMLVIEW_H #ifndef LITEHTMLVIEW_H
#define LITEHTMLVIEW_H #define LITEHTMLVIEW_H
#include "../../include/litehtml.h" #include <View.h>
#include <map> #include <map>
#include <string> #include <string>
#include <View.h> #include "../../include/litehtml.h"
class BBitmap; class BBitmap;
enum { M_HTML_RENDERED = 'hrnd' };
enum { class LiteHtmlView : public BView, public litehtml::document_container {
M_HTML_RENDERED = 'hrnd' public:
}; // LiteHtmlView(BMessage *archive);
LiteHtmlView(BRect frame, const char* name);
// LiteHtmlView(const char *name, uint32 flags, BLayout *layout=NULL);
class LiteHtmlView : public BView, public litehtml::document_container virtual ~LiteHtmlView();
{
public:
//LiteHtmlView(BMessage *archive);
LiteHtmlView(BRect frame, const char *name);
//LiteHtmlView(const char *name, uint32 flags, BLayout *layout=NULL);
virtual ~LiteHtmlView();
void SetContext(litehtml::context* ctx);
void RenderFile(const char* localFilePath);
void RenderHTML(const std::string& htmlText);
virtual litehtml::uint_ptr create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) override;
virtual void delete_font(litehtml::uint_ptr hFont) override;
virtual int text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) override;
virtual void draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) override;
virtual int pt_to_px(int pt) override;
virtual int get_default_font_size() const override;
virtual const litehtml::tchar_t* get_default_font_name() const override;
virtual void load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override;
virtual void get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override;
virtual void draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override;
virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override;
virtual void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) override;
virtual std::shared_ptr<litehtml::element> create_element(const litehtml::tchar_t *tag_name,
const litehtml::string_map &attributes,
const std::shared_ptr<litehtml::document> &doc) override;
virtual void get_media_features(litehtml::media_features& media) const override;
//virtual void get_language(litehtml::tstring& language, litehtml::tstring & culture) const override;
virtual void link(const std::shared_ptr<litehtml::document> &ptr, const litehtml::element::ptr& el) override;
void SetContext(litehtml::context* ctx);
void RenderFile(const char* localFilePath);
void RenderHTML(const std::string& htmlText);
virtual void transform_text(litehtml::tstring& text, litehtml::text_transform tt) override; virtual litehtml::uint_ptr create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) override;
virtual void set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) override; virtual void delete_font(litehtml::uint_ptr hFont) override;
virtual void del_clip() override; virtual int text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) override;
virtual void draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) override;
// unimplemented virtual int pt_to_px(int pt) override;
virtual void set_caption(const char*); virtual int get_default_font_size() const override;
virtual void get_client_rect(litehtml::position& client) const; virtual const litehtml::tchar_t* get_default_font_name() const override;
virtual void set_base_url(const char*); virtual void load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override;
virtual void on_anchor_click(const char*, const litehtml::element::ptr&); virtual void get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override;
virtual void set_cursor(const char*); virtual void draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override;
virtual void import_css(litehtml::tstring&, const litehtml::tstring&, litehtml::tstring&); virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override;
virtual void get_language(litehtml::tstring&, litehtml::tstring&) const; virtual void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) override;
virtual std::shared_ptr<litehtml::element> create_element(const litehtml::tchar_t* tag_name, const litehtml::string_map& attributes, const std::shared_ptr<litehtml::document>& doc) override;
//BView virtual void get_media_features(litehtml::media_features& media) const override;
virtual void Draw(BRect updateRect) override; // virtual void get_language(litehtml::tstring& language, litehtml::tstring & culture) const override;
virtual void GetPreferredSize(float* width, float* height) override; virtual void link(const std::shared_ptr<litehtml::document>& ptr, const litehtml::element::ptr& el) override;
protected: virtual void transform_text(litehtml::tstring& text, litehtml::text_transform tt) override;
void make_url(const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out); virtual void set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) override;
virtual void draw_image( litehtml::uint_ptr hdc, const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, const litehtml::position& pos ); virtual void del_clip() override;
private: // unimplemented
litehtml::context* fContext; virtual void set_caption(const char*);
litehtml::document::ptr m_html; virtual void get_client_rect(litehtml::position& client) const;
std::map<const std::string,BBitmap*> m_images; virtual void set_base_url(const char*);
litehtml::tstring m_base_url; virtual void on_anchor_click(const char*, const litehtml::element::ptr&);
litehtml::tstring m_url; virtual void set_cursor(const char*);
virtual void import_css(litehtml::tstring&, const litehtml::tstring&, litehtml::tstring&);
virtual void get_language(litehtml::tstring&, litehtml::tstring&) const;
// BView
virtual void Draw(BRect updateRect) override;
virtual void GetPreferredSize(float* width, float* height) override;
protected:
void make_url(const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out);
virtual void draw_image(litehtml::uint_ptr hdc, const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, const litehtml::position& pos);
private:
litehtml::context* fContext;
litehtml::document::ptr m_html;
std::map<const std::string, BBitmap*> m_images;
litehtml::tstring m_base_url;
litehtml::tstring m_url;
}; };
#endif #endif

View file

@ -1,104 +1,97 @@
#ifndef LH_CONTAINER_LINUX_H #ifndef LH_CONTAINER_LINUX_H
#define LH_CONTAINER_LINUX_H #define LH_CONTAINER_LINUX_H
#include "../../include/litehtml.h"
#include <cairo.h> #include <cairo.h>
#include <gtkmm.h> #include <gtkmm.h>
#include <pango/pangocairo.h> #include <pango/pangocairo.h>
struct cairo_clip_box #include "../../include/litehtml.h"
{
typedef std::vector<cairo_clip_box> vector;
litehtml::position box;
litehtml::border_radiuses radius;
cairo_clip_box(const litehtml::position& vBox, const litehtml::border_radiuses& vRad) struct cairo_clip_box {
{ typedef std::vector<cairo_clip_box> vector;
box = vBox; litehtml::position box;
radius = vRad; litehtml::border_radiuses radius;
}
cairo_clip_box(const cairo_clip_box& val) cairo_clip_box(const litehtml::position& vBox, const litehtml::border_radiuses& vRad) {
{ box = vBox;
box = val.box; radius = vRad;
radius = val.radius; }
}
cairo_clip_box& operator=(const cairo_clip_box& val) cairo_clip_box(const cairo_clip_box& val) {
{ box = val.box;
box = val.box; radius = val.radius;
radius = val.radius; }
return *this; cairo_clip_box& operator=(const cairo_clip_box& val) {
} box = val.box;
radius = val.radius;
return *this;
}
}; };
struct cairo_font struct cairo_font {
{ PangoFontDescription* font;
PangoFontDescription* font; int size;
int size; bool underline;
bool underline; bool strikeout;
bool strikeout; int ascent;
int ascent; int descent;
int descent; int underline_thickness;
int underline_thickness; int underline_position;
int underline_position; int strikethrough_thickness;
int strikethrough_thickness; int strikethrough_position;
int strikethrough_position;
}; };
class container_linux : public litehtml::document_container class container_linux : public litehtml::document_container {
{ typedef std::map<litehtml::tstring, Glib::RefPtr<Gdk::Pixbuf> > images_map;
typedef std::map<litehtml::tstring, Glib::RefPtr<Gdk::Pixbuf> > images_map;
protected: protected:
cairo_surface_t* m_temp_surface; cairo_surface_t* m_temp_surface;
cairo_t* m_temp_cr; cairo_t* m_temp_cr;
images_map m_images; images_map m_images;
cairo_clip_box::vector m_clips; cairo_clip_box::vector m_clips;
public:
container_linux();
virtual ~container_linux();
litehtml::uint_ptr create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) override; public:
void delete_font(litehtml::uint_ptr hFont) override; container_linux();
int text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) override; virtual ~container_linux();
void draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) override;
int pt_to_px(int pt) const override;
int get_default_font_size() const override;
const litehtml::tchar_t* get_default_font_name() const override;
void load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override;
void get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override;
void draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override;
void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override;
void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) override;
std::shared_ptr<litehtml::element> create_element(const litehtml::tchar_t *tag_name,
const litehtml::string_map &attributes,
const std::shared_ptr<litehtml::document> &doc) override;
void get_media_features(litehtml::media_features& media) const override;
void get_language(litehtml::tstring& language, litehtml::tstring & culture) const override;
void link(const std::shared_ptr<litehtml::document> &ptr, const litehtml::element::ptr& el) override;
litehtml::uint_ptr create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) override;
void delete_font(litehtml::uint_ptr hFont) override;
int text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) override;
void draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) override;
int pt_to_px(int pt) const override;
int get_default_font_size() const override;
const litehtml::tchar_t* get_default_font_name() const override;
void load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override;
void get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override;
void draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override;
void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override;
void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) override;
std::shared_ptr<litehtml::element> create_element(const litehtml::tchar_t* tag_name, const litehtml::string_map& attributes, const std::shared_ptr<litehtml::document>& doc) override;
void get_media_features(litehtml::media_features& media) const override;
void get_language(litehtml::tstring& language, litehtml::tstring& culture) const override;
void link(const std::shared_ptr<litehtml::document>& ptr, const litehtml::element::ptr& el) override;
void transform_text(litehtml::tstring& text, litehtml::text_transform tt) override; void transform_text(litehtml::tstring& text, litehtml::text_transform tt) override;
void set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) override; void set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) override;
void del_clip() override; void del_clip() override;
virtual void make_url( const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out ); virtual void make_url(const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out);
virtual Glib::RefPtr<Gdk::Pixbuf> get_image(const litehtml::tchar_t* url, bool redraw_on_ready) = 0; virtual Glib::RefPtr<Gdk::Pixbuf> get_image(const litehtml::tchar_t* url, bool redraw_on_ready) = 0;
void clear_images(); void clear_images();
protected: protected:
virtual void draw_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, int line_width); virtual void draw_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, int line_width);
virtual void fill_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color); virtual void fill_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color);
virtual void rounded_rectangle( cairo_t* cr, const litehtml::position &pos, const litehtml::border_radiuses &radius ); virtual void rounded_rectangle(cairo_t* cr, const litehtml::position& pos, const litehtml::border_radiuses& radius);
private: private:
void apply_clip(cairo_t* cr); void apply_clip(cairo_t* cr);
static void add_path_arc(cairo_t* cr, double x, double y, double rx, double ry, double a1, double a2, bool neg); static void add_path_arc(cairo_t* cr, double x, double y, double rx, double ry, double a1, double a2, bool neg);
static void set_color(cairo_t* cr, const litehtml::web_color& color) { cairo_set_source_rgba(cr, color.red / 255.0, color.green / 255.0, color.blue / 255.0, color.alpha / 255.0); } static void set_color(cairo_t* cr, const litehtml::web_color& color) { cairo_set_source_rgba(cr, color.red / 255.0, color.green / 255.0, color.blue / 255.0, color.alpha / 255.0); }
static cairo_surface_t* surface_from_pixbuf(const Glib::RefPtr<Gdk::Pixbuf>& bmp); static cairo_surface_t* surface_from_pixbuf(const Glib::RefPtr<Gdk::Pixbuf>& bmp);
static void draw_pixbuf(cairo_t* cr, const Glib::RefPtr<Gdk::Pixbuf>& bmp, int x, int y, int cx, int cy); static void draw_pixbuf(cairo_t* cr, const Glib::RefPtr<Gdk::Pixbuf>& bmp, int x, int y, int cx, int cy);
}; };
#endif #endif

View file

@ -23,8 +23,8 @@ void container_test::get_image_size(const litehtml::tchar_t* src, const litehtml
void container_test::draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) {} void container_test::draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) {}
void container_test::make_url(const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out) { out = url; } void container_test::make_url(const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out) { out = url; }
void container_test::draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) {} void container_test::draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) {}
void container_test::set_caption(const litehtml::tchar_t* caption){}; //: set_caption void container_test::set_caption(const litehtml::tchar_t* caption) {}; //: set_caption
void container_test::set_base_url(const litehtml::tchar_t* base_url){}; //: set_base_url void container_test::set_base_url(const litehtml::tchar_t* base_url) {}; //: set_base_url
void container_test::link(const std::shared_ptr<litehtml::document>& ptr, const litehtml::element::ptr& el) {} void container_test::link(const std::shared_ptr<litehtml::document>& ptr, const litehtml::element::ptr& el) {}
void container_test::on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el) {} //: on_anchor_click void container_test::on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el) {} //: on_anchor_click
void container_test::set_cursor(const litehtml::tchar_t* cursor) {} //: set_cursor void container_test::set_cursor(const litehtml::tchar_t* cursor) {} //: set_cursor

View file

@ -2,43 +2,38 @@
#include "../../include/litehtml.h" #include "../../include/litehtml.h"
class container_test : public litehtml::document_container class container_test : public litehtml::document_container {
{ public:
public: container_test();
container_test(); virtual ~container_test();
virtual ~container_test();
virtual litehtml::uint_ptr create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) override; virtual litehtml::uint_ptr create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) override;
virtual void delete_font(litehtml::uint_ptr hFont) override; virtual void delete_font(litehtml::uint_ptr hFont) override;
virtual int text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) override; virtual int text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) override;
virtual void draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) override; virtual void draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) override;
virtual int pt_to_px(int pt) const override; virtual int pt_to_px(int pt) const override;
virtual int get_default_font_size() const override; virtual int get_default_font_size() const override;
virtual const litehtml::tchar_t* get_default_font_name() const override; virtual const litehtml::tchar_t* get_default_font_name() const override;
virtual void load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override; virtual void load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override;
virtual void get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override; virtual void get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override;
virtual void draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override; virtual void draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override;
virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override; virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override;
virtual void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) override; virtual void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) override;
virtual std::shared_ptr<litehtml::element> create_element(const litehtml::tchar_t *tag_name, virtual std::shared_ptr<litehtml::element> create_element(const litehtml::tchar_t* tag_name, const litehtml::string_map& attributes, const std::shared_ptr<litehtml::document>& doc) override;
const litehtml::string_map &attributes, virtual void get_media_features(litehtml::media_features& media) const override;
const std::shared_ptr<litehtml::document> &doc) override; virtual void get_language(litehtml::tstring& language, litehtml::tstring& culture) const override;
virtual void get_media_features(litehtml::media_features& media) const override; virtual void link(const std::shared_ptr<litehtml::document>& ptr, const litehtml::element::ptr& el) override;
virtual void get_language(litehtml::tstring& language, litehtml::tstring & culture) const override;
virtual void link(const std::shared_ptr<litehtml::document> &ptr, const litehtml::element::ptr& el) override;
virtual void transform_text(litehtml::tstring& text, litehtml::text_transform tt) override;
virtual void set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) override;
virtual void del_clip() override;
virtual void transform_text(litehtml::tstring& text, litehtml::text_transform tt) override; virtual void make_url(const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out);
virtual void set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) override;
virtual void del_clip() override;
virtual void make_url( const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out ); virtual void set_caption(const litehtml::tchar_t* caption) override;
virtual void set_base_url(const litehtml::tchar_t* base_url) override;
virtual void on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el) override;
virtual void set_caption(const litehtml::tchar_t* caption) override; virtual void set_cursor(const litehtml::tchar_t* cursor) override;
virtual void set_base_url(const litehtml::tchar_t* base_url) override; virtual void import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl) override;
virtual void on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el) override; virtual void get_client_rect(litehtml::position& client) const override;
virtual void set_cursor(const litehtml::tchar_t* cursor) override;
virtual void import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl) override;
virtual void get_client_rect(litehtml::position& client) const override;
}; };

View file

@ -1,327 +1,259 @@
#include <litehtml.h>
#include "win32_container.h" #include "win32_container.h"
#include <litehtml.h>
litehtml::win32_container::win32_container() litehtml::win32_container::win32_container() { m_hClipRgn = NULL; }
{
m_hClipRgn = NULL; litehtml::win32_container::~win32_container() {
if (m_hClipRgn) {
DeleteObject(m_hClipRgn);
}
} }
litehtml::win32_container::~win32_container() litehtml::uint_ptr litehtml::win32_container::create_font(const wchar_t* faceName, int size, int weight, font_style italic, unsigned int decoration) {
{ litehtml::string_vector fonts;
if(m_hClipRgn) tokenize(faceName, fonts, L",");
{ litehtml::trim(fonts[0]);
DeleteObject(m_hClipRgn);
} LOGFONT lf;
ZeroMemory(&lf, sizeof(lf));
wcscpy_s(lf.lfFaceName, LF_FACESIZE, fonts[0].c_str());
lf.lfHeight = -size;
lf.lfWeight = weight;
lf.lfItalic = (italic == litehtml::fontStyleItalic) ? TRUE : FALSE;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfStrikeOut = (decoration & litehtml::font_decoration_linethrough) ? TRUE : FALSE;
lf.lfUnderline = (decoration & litehtml::font_decoration_underline) ? TRUE : FALSE;
HFONT hFont = CreateFontIndirect(&lf);
return (uint_ptr)hFont;
} }
litehtml::uint_ptr litehtml::win32_container::create_font( const wchar_t* faceName, int size, int weight, font_style italic, unsigned int decoration ) void litehtml::win32_container::delete_font(uint_ptr hFont) { DeleteObject((HFONT)hFont); }
{
litehtml::string_vector fonts;
tokenize(faceName, fonts, L",");
litehtml::trim(fonts[0]);
LOGFONT lf; int litehtml::win32_container::line_height(uint_ptr hdc, uint_ptr hFont) {
ZeroMemory(&lf, sizeof(lf)); HFONT oldFont = (HFONT)SelectObject((HDC)hdc, (HFONT)hFont);
wcscpy_s(lf.lfFaceName, LF_FACESIZE, fonts[0].c_str()); TEXTMETRIC tm;
GetTextMetrics((HDC)hdc, &tm);
lf.lfHeight = -size; SelectObject((HDC)hdc, oldFont);
lf.lfWeight = weight; return (int)tm.tmHeight;
lf.lfItalic = (italic == litehtml::fontStyleItalic) ? TRUE : FALSE;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfStrikeOut = (decoration & litehtml::font_decoration_linethrough) ? TRUE : FALSE;
lf.lfUnderline = (decoration & litehtml::font_decoration_underline) ? TRUE : FALSE;
HFONT hFont = CreateFontIndirect(&lf);
return (uint_ptr) hFont;
} }
void litehtml::win32_container::delete_font( uint_ptr hFont ) int litehtml::win32_container::text_width(uint_ptr hdc, const wchar_t* text, uint_ptr hFont) {
{ HFONT oldFont = (HFONT)SelectObject((HDC)hdc, (HFONT)hFont);
DeleteObject((HFONT) hFont);
SIZE sz = {0, 0};
GetTextExtentPoint32((HDC)hdc, text, lstrlen(text), &sz);
SelectObject((HDC)hdc, oldFont);
return (int)sz.cx;
} }
int litehtml::win32_container::line_height( uint_ptr hdc, uint_ptr hFont ) void litehtml::win32_container::draw_text(uint_ptr hdc, const wchar_t* text, uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) {
{ apply_clip((HDC)hdc);
HFONT oldFont = (HFONT) SelectObject((HDC) hdc, (HFONT) hFont);
TEXTMETRIC tm; HFONT oldFont = (HFONT)SelectObject((HDC)hdc, (HFONT)hFont);
GetTextMetrics((HDC) hdc, &tm);
SelectObject((HDC) hdc, oldFont); SetBkMode((HDC)hdc, TRANSPARENT);
return (int) tm.tmHeight;
SetTextColor((HDC)hdc, RGB(color.red, color.green, color.blue));
RECT rcText = {pos.left(), pos.top(), pos.right(), pos.bottom()};
DrawText((HDC)hdc, text, -1, &rcText, DT_SINGLELINE | DT_NOPREFIX | DT_BOTTOM | DT_NOCLIP);
SelectObject((HDC)hdc, oldFont);
release_clip((HDC)hdc);
} }
int litehtml::win32_container::text_width( uint_ptr hdc, const wchar_t* text, uint_ptr hFont ) void litehtml::win32_container::fill_rect(uint_ptr hdc, const litehtml::position& pos, const litehtml::web_color color, const litehtml::css_border_radius& radius) {
{ apply_clip((HDC)hdc);
HFONT oldFont = (HFONT) SelectObject((HDC) hdc, (HFONT) hFont); fill_rect((HDC)hdc, pos.x, pos.y, pos.width, pos.height, color, radius);
release_clip((HDC)hdc);
SIZE sz = {0, 0};
GetTextExtentPoint32((HDC) hdc, text, lstrlen(text), &sz);
SelectObject((HDC) hdc, oldFont);
return (int) sz.cx;
} }
void litehtml::win32_container::draw_text( uint_ptr hdc, const wchar_t* text, uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos ) litehtml::uint_ptr litehtml::win32_container::get_temp_dc() { return (litehtml::uint_ptr)GetDC(NULL); }
{
apply_clip((HDC) hdc);
HFONT oldFont = (HFONT) SelectObject((HDC) hdc, (HFONT) hFont); void litehtml::win32_container::release_temp_dc(uint_ptr hdc) { ReleaseDC(NULL, (HDC)hdc); }
SetBkMode((HDC) hdc, TRANSPARENT); int litehtml::win32_container::pt_to_px(int pt) {
HDC dc = GetDC(NULL);
SetTextColor((HDC) hdc, RGB(color.red, color.green, color.blue)); int ret = MulDiv(pt, GetDeviceCaps(dc, LOGPIXELSY), 72);
ReleaseDC(NULL, dc);
RECT rcText = { pos.left(), pos.top(), pos.right(), pos.bottom() }; return ret;
DrawText((HDC) hdc, text, -1, &rcText, DT_SINGLELINE | DT_NOPREFIX | DT_BOTTOM | DT_NOCLIP);
SelectObject((HDC) hdc, oldFont);
release_clip((HDC) hdc);
} }
void litehtml::win32_container::fill_rect( uint_ptr hdc, const litehtml::position& pos, const litehtml::web_color color, const litehtml::css_border_radius& radius ) int litehtml::win32_container::get_text_base_line(uint_ptr hdc, uint_ptr hFont) {
{ HDC dc = (HDC)hdc;
apply_clip((HDC) hdc); if (!dc) {
fill_rect((HDC) hdc, pos.x, pos.y, pos.width, pos.height, color, radius); dc = GetDC(NULL);
release_clip((HDC) hdc); }
HFONT oldFont = (HFONT)SelectObject(dc, (HFONT)hFont);
TEXTMETRIC tm;
GetTextMetrics(dc, &tm);
SelectObject(dc, oldFont);
if (!hdc) {
ReleaseDC(NULL, dc);
}
return (int)tm.tmDescent;
} }
litehtml::uint_ptr litehtml::win32_container::get_temp_dc() void litehtml::win32_container::draw_list_marker(uint_ptr hdc, const litehtml::list_marker& marker) {
{ apply_clip((HDC)hdc);
return (litehtml::uint_ptr) GetDC(NULL);
int top_margin = marker.pos.height / 3;
if (top_margin < 4) top_margin = 0;
int draw_x = marker.pos.x;
int draw_y = marker.pos.y + top_margin;
int draw_width = marker.pos.height - top_margin * 2;
int draw_height = marker.pos.height - top_margin * 2;
switch (marker.marker_type) {
case list_style_type_circle: {
draw_ellipse((HDC)hdc, draw_x, draw_y, draw_width, draw_height, marker.color, 1);
} break;
case list_style_type_disc: {
fill_ellipse((HDC)hdc, draw_x, draw_y, draw_width, draw_height, marker.color);
} break;
case list_style_type_square: {
fill_rect((HDC)hdc, draw_x, draw_y, draw_width, draw_height, marker.color, css_border_radius());
} break;
}
release_clip((HDC)hdc);
} }
void litehtml::win32_container::release_temp_dc( uint_ptr hdc ) void litehtml::win32_container::load_image(const wchar_t* src, const wchar_t* baseurl, bool redraw_on_ready) {
{ std::wstring url;
ReleaseDC(NULL, (HDC) hdc); make_url(src, baseurl, url);
if (m_images.find(url.c_str()) == m_images.end()) {
uint_ptr img = get_image(url.c_str());
if (img) {
m_images[url.c_str()] = img;
}
}
} }
int litehtml::win32_container::pt_to_px( int pt ) void litehtml::win32_container::get_image_size(const wchar_t* src, const wchar_t* baseurl, litehtml::size& sz) {
{ std::wstring url;
HDC dc = GetDC(NULL); make_url(src, baseurl, url);
int ret = MulDiv(pt, GetDeviceCaps(dc, LOGPIXELSY), 72);
ReleaseDC(NULL, dc); images_map::iterator img = m_images.find(url.c_str());
return ret; if (img != m_images.end()) {
get_img_size(img->second, sz);
}
} }
int litehtml::win32_container::get_text_base_line( uint_ptr hdc, uint_ptr hFont ) void litehtml::win32_container::draw_image(uint_ptr hdc, const wchar_t* src, const wchar_t* baseurl, const litehtml::position& pos) {
{ apply_clip((HDC)hdc);
HDC dc = (HDC) hdc;
if(!dc) std::wstring url;
{ make_url(src, baseurl, url);
dc = GetDC(NULL); images_map::iterator img = m_images.find(url.c_str());
} if (img != m_images.end()) {
HFONT oldFont = (HFONT) SelectObject(dc, (HFONT) hFont); draw_img((HDC)hdc, img->second, pos);
TEXTMETRIC tm; }
GetTextMetrics(dc, &tm);
SelectObject(dc, oldFont); release_clip((HDC)hdc);
if(!hdc)
{
ReleaseDC(NULL, dc);
}
return (int) tm.tmDescent;
} }
void litehtml::win32_container::draw_list_marker(uint_ptr hdc, const litehtml::list_marker& marker) void litehtml::win32_container::clear_images() {
{ for (images_map::iterator i = m_images.begin(); i != m_images.end(); i++) {
apply_clip((HDC)hdc); if (i->second) {
free_image(i->second);
int top_margin = marker.pos.height / 3; }
if (top_margin < 4) }
top_margin = 0; m_images.clear();
int draw_x = marker.pos.x;
int draw_y = marker.pos.y + top_margin;
int draw_width = marker.pos.height - top_margin * 2;
int draw_height = marker.pos.height - top_margin * 2;
switch (marker.marker_type)
{
case list_style_type_circle:
{
draw_ellipse((HDC)hdc, draw_x, draw_y, draw_width, draw_height, marker.color, 1);
}
break;
case list_style_type_disc:
{
fill_ellipse((HDC)hdc, draw_x, draw_y, draw_width, draw_height, marker.color);
}
break;
case list_style_type_square:
{
fill_rect((HDC)hdc, draw_x, draw_y, draw_width, draw_height, marker.color, css_border_radius());
}
break;
}
release_clip((HDC)hdc);
} }
void litehtml::win32_container::load_image( const wchar_t* src, const wchar_t* baseurl, bool redraw_on_ready ) int litehtml::win32_container::get_default_font_size() const { return 16; }
{
std::wstring url; void litehtml::win32_container::draw_background(uint_ptr hdc, const wchar_t* image, const wchar_t* baseurl, const litehtml::position& draw_pos, const litehtml::css_position& bg_pos, litehtml::background_repeat repeat, litehtml::background_attachment attachment) {
make_url(src, baseurl, url); apply_clip((HDC)hdc);
if(m_images.find(url.c_str()) == m_images.end())
{ std::wstring url;
uint_ptr img = get_image(url.c_str()); make_url(image, baseurl, url);
if(img)
{ images_map::iterator img = m_images.find(url.c_str());
m_images[url.c_str()] = img; if (img != m_images.end()) {
} litehtml::size img_sz;
} get_img_size(img->second, img_sz);
litehtml::position pos = draw_pos;
if (bg_pos.x.units() != css_units_percentage) {
pos.x += (int)bg_pos.x.val();
} else {
pos.x += (int)((float)(draw_pos.width - img_sz.width) * bg_pos.x.val() / 100.0);
}
if (bg_pos.y.units() != css_units_percentage) {
pos.y += (int)bg_pos.y.val();
} else {
pos.y += (int)((float)(draw_pos.height - img_sz.height) * bg_pos.y.val() / 100.0);
}
draw_img_bg((HDC)hdc, img->second, draw_pos, pos, repeat, attachment);
}
release_clip((HDC)hdc);
} }
void litehtml::win32_container::get_image_size( const wchar_t* src, const wchar_t* baseurl, litehtml::size& sz ) wchar_t litehtml::win32_container::toupper(const wchar_t c) { return (wchar_t)CharUpper((LPWSTR)c); }
{
std::wstring url;
make_url(src, baseurl, url);
images_map::iterator img = m_images.find(url.c_str()); wchar_t litehtml::win32_container::tolower(const wchar_t c) { return (wchar_t)CharLower((LPWSTR)c); }
if(img != m_images.end())
{ void litehtml::win32_container::set_clip(const litehtml::position& pos, bool valid_x, bool valid_y) {
get_img_size(img->second, sz); litehtml::position clip_pos = pos;
} litehtml::position client_pos;
get_client_rect(client_pos);
if (!valid_x) {
clip_pos.x = client_pos.x;
clip_pos.width = client_pos.width;
}
if (!valid_y) {
clip_pos.y = client_pos.y;
clip_pos.height = client_pos.height;
}
m_clips.push_back(clip_pos);
} }
void litehtml::win32_container::draw_image( uint_ptr hdc, const wchar_t* src, const wchar_t* baseurl, const litehtml::position& pos ) void litehtml::win32_container::del_clip() {
{ if (!m_clips.empty()) {
apply_clip((HDC) hdc); m_clips.pop_back();
if (!m_clips.empty()) {
std::wstring url; litehtml::position clip_pos = m_clips.back();
make_url(src, baseurl, url); }
images_map::iterator img = m_images.find(url.c_str()); }
if(img != m_images.end())
{
draw_img((HDC) hdc, img->second, pos);
}
release_clip((HDC) hdc);
} }
void litehtml::win32_container::clear_images() void litehtml::win32_container::apply_clip(HDC hdc) {
{ if (m_hClipRgn) {
for(images_map::iterator i = m_images.begin(); i != m_images.end(); i++) DeleteObject(m_hClipRgn);
{ m_hClipRgn = NULL;
if(i->second) }
{
free_image(i->second); if (!m_clips.empty()) {
} POINT ptView = {0, 0};
} GetWindowOrgEx(hdc, &ptView);
m_images.clear();
litehtml::position clip_pos = m_clips.back();
m_hClipRgn = CreateRectRgn(clip_pos.left() - ptView.x, clip_pos.top() - ptView.y, clip_pos.right() - ptView.x, clip_pos.bottom() - ptView.y);
SelectClipRgn(hdc, m_hClipRgn);
}
} }
int litehtml::win32_container::get_default_font_size() const void litehtml::win32_container::release_clip(HDC hdc) {
{ SelectClipRgn(hdc, NULL);
return 16;
} if (m_hClipRgn) {
DeleteObject(m_hClipRgn);
void litehtml::win32_container::draw_background( uint_ptr hdc, const wchar_t* image, const wchar_t* baseurl, const litehtml::position& draw_pos, const litehtml::css_position& bg_pos, litehtml::background_repeat repeat, litehtml::background_attachment attachment ) m_hClipRgn = NULL;
{ }
apply_clip((HDC) hdc);
std::wstring url;
make_url(image, baseurl, url);
images_map::iterator img = m_images.find(url.c_str());
if(img != m_images.end())
{
litehtml::size img_sz;
get_img_size(img->second, img_sz);
litehtml::position pos = draw_pos;
if(bg_pos.x.units() != css_units_percentage)
{
pos.x += (int) bg_pos.x.val();
} else
{
pos.x += (int) ((float) (draw_pos.width - img_sz.width) * bg_pos.x.val() / 100.0);
}
if(bg_pos.y.units() != css_units_percentage)
{
pos.y += (int) bg_pos.y.val();
} else
{
pos.y += (int) ( (float) (draw_pos.height - img_sz.height) * bg_pos.y.val() / 100.0);
}
draw_img_bg((HDC) hdc, img->second, draw_pos, pos, repeat, attachment);
}
release_clip((HDC) hdc);
}
wchar_t litehtml::win32_container::toupper( const wchar_t c )
{
return (wchar_t) CharUpper((LPWSTR) c);
}
wchar_t litehtml::win32_container::tolower( const wchar_t c )
{
return (wchar_t) CharLower((LPWSTR) c);
}
void litehtml::win32_container::set_clip( const litehtml::position& pos, bool valid_x, bool valid_y )
{
litehtml::position clip_pos = pos;
litehtml::position client_pos;
get_client_rect(client_pos);
if(!valid_x)
{
clip_pos.x = client_pos.x;
clip_pos.width = client_pos.width;
}
if(!valid_y)
{
clip_pos.y = client_pos.y;
clip_pos.height = client_pos.height;
}
m_clips.push_back(clip_pos);
}
void litehtml::win32_container::del_clip()
{
if(!m_clips.empty())
{
m_clips.pop_back();
if(!m_clips.empty())
{
litehtml::position clip_pos = m_clips.back();
}
}
}
void litehtml::win32_container::apply_clip(HDC hdc)
{
if(m_hClipRgn)
{
DeleteObject(m_hClipRgn);
m_hClipRgn = NULL;
}
if(!m_clips.empty())
{
POINT ptView = {0, 0};
GetWindowOrgEx(hdc, &ptView);
litehtml::position clip_pos = m_clips.back();
m_hClipRgn = CreateRectRgn(clip_pos.left() - ptView.x, clip_pos.top() - ptView.y, clip_pos.right() - ptView.x, clip_pos.bottom() - ptView.y);
SelectClipRgn(hdc, m_hClipRgn);
}
}
void litehtml::win32_container::release_clip(HDC hdc)
{
SelectClipRgn(hdc, NULL);
if(m_hClipRgn)
{
DeleteObject(m_hClipRgn);
m_hClipRgn = NULL;
}
} }

View file

@ -1,65 +1,56 @@
#pragma once #pragma once
#include <Windows.h> #include <Windows.h>
namespace litehtml namespace litehtml {
{ class win32_container : public document_container {
class win32_container : public document_container public:
{ typedef std::map<std::wstring, litehtml::uint_ptr> images_map;
public:
typedef std::map<std::wstring, litehtml::uint_ptr> images_map;
protected:
images_map m_images;
litehtml::position::vector m_clips;
HRGN m_hClipRgn;
public: protected:
win32_container(); images_map m_images;
virtual ~win32_container(); litehtml::position::vector m_clips;
HRGN m_hClipRgn;
// litehtml::document_container members public:
virtual uint_ptr create_font(const wchar_t* faceName, int size, int weight, font_style italic, unsigned int decoration); win32_container();
virtual void delete_font(uint_ptr hFont); virtual ~win32_container();
virtual int line_height(uint_ptr hdc, uint_ptr hFont);
virtual int get_text_base_line(uint_ptr hdc, uint_ptr hFont);
virtual int text_width(uint_ptr hdc, const wchar_t* text, uint_ptr hFont);
virtual void draw_text(uint_ptr hdc, const wchar_t* text, uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos);
virtual void fill_rect(uint_ptr hdc, const litehtml::position& pos, const litehtml::web_color color, const litehtml::css_border_radius& radius);
virtual uint_ptr get_temp_dc();
virtual void release_temp_dc(uint_ptr hdc);
virtual int pt_to_px(int pt);
virtual void draw_list_marker(uint_ptr hdc, const litehtml::list_marker& marker);
virtual void load_image(const wchar_t* src, const wchar_t* baseurl, bool redraw_on_ready);
virtual void get_image_size(const wchar_t* src, const wchar_t* baseurl, litehtml::size& sz);
virtual void draw_image(uint_ptr hdc, const wchar_t* src, const wchar_t* baseurl, const litehtml::position& pos);
virtual void draw_background(uint_ptr hdc,
const wchar_t* image,
const wchar_t* baseurl,
const litehtml::position& draw_pos,
const litehtml::css_position& bg_pos,
litehtml::background_repeat repeat,
litehtml::background_attachment attachment);
virtual int get_default_font_size() const; // litehtml::document_container members
virtual wchar_t toupper(const wchar_t c); virtual uint_ptr create_font(const wchar_t* faceName, int size, int weight, font_style italic, unsigned int decoration);
virtual wchar_t tolower(const wchar_t c); virtual void delete_font(uint_ptr hFont);
virtual void set_clip(const litehtml::position& pos, bool valid_x, bool valid_y); virtual int line_height(uint_ptr hdc, uint_ptr hFont);
virtual void del_clip(); virtual int get_text_base_line(uint_ptr hdc, uint_ptr hFont);
virtual int text_width(uint_ptr hdc, const wchar_t* text, uint_ptr hFont);
virtual void draw_text(uint_ptr hdc, const wchar_t* text, uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos);
virtual void fill_rect(uint_ptr hdc, const litehtml::position& pos, const litehtml::web_color color, const litehtml::css_border_radius& radius);
virtual uint_ptr get_temp_dc();
virtual void release_temp_dc(uint_ptr hdc);
virtual int pt_to_px(int pt);
virtual void draw_list_marker(uint_ptr hdc, const litehtml::list_marker& marker);
virtual void load_image(const wchar_t* src, const wchar_t* baseurl, bool redraw_on_ready);
virtual void get_image_size(const wchar_t* src, const wchar_t* baseurl, litehtml::size& sz);
virtual void draw_image(uint_ptr hdc, const wchar_t* src, const wchar_t* baseurl, const litehtml::position& pos);
virtual void draw_background(uint_ptr hdc, const wchar_t* image, const wchar_t* baseurl, const litehtml::position& draw_pos, const litehtml::css_position& bg_pos, litehtml::background_repeat repeat, litehtml::background_attachment attachment);
protected: virtual int get_default_font_size() const;
void apply_clip(HDC hdc); virtual wchar_t toupper(const wchar_t c);
void release_clip(HDC hdc); virtual wchar_t tolower(const wchar_t c);
void clear_images(); virtual void set_clip(const litehtml::position& pos, bool valid_x, bool valid_y);
virtual void make_url( LPCWSTR url, LPCWSTR basepath, std::wstring& out ) = 0; virtual void del_clip();
virtual uint_ptr get_image(LPCWSTR url) = 0;
virtual void free_image(uint_ptr img) = 0; protected:
virtual void get_client_rect(litehtml::position& client) = 0; void apply_clip(HDC hdc);
virtual void draw_ellipse(HDC hdc, int x, int y, int width, int height, const web_color& color, int line_width) = 0; void release_clip(HDC hdc);
virtual void fill_ellipse(HDC hdc, int x, int y, int width, int height, const web_color& color) = 0; void clear_images();
virtual void fill_rect(HDC hdc, int x, int y, int width, int height, const web_color& color, const litehtml::css_border_radius& radius) = 0; virtual void make_url(LPCWSTR url, LPCWSTR basepath, std::wstring& out) = 0;
virtual void get_img_size(litehtml::uint_ptr img, litehtml::size& sz) = 0; virtual uint_ptr get_image(LPCWSTR url) = 0;
virtual void draw_img(HDC hdc, litehtml::uint_ptr img, const litehtml::position& pos) = 0; virtual void free_image(uint_ptr img) = 0;
virtual void draw_img_bg(HDC hdc, litehtml::uint_ptr img, const litehtml::position& draw_pos, const litehtml::position& pos, litehtml::background_repeat repeat, litehtml::background_attachment attachment) = 0; virtual void get_client_rect(litehtml::position& client) = 0;
}; virtual void draw_ellipse(HDC hdc, int x, int y, int width, int height, const web_color& color, int line_width) = 0;
} virtual void fill_ellipse(HDC hdc, int x, int y, int width, int height, const web_color& color) = 0;
virtual void fill_rect(HDC hdc, int x, int y, int width, int height, const web_color& color, const litehtml::css_border_radius& radius) = 0;
virtual void get_img_size(litehtml::uint_ptr img, litehtml::size& sz) = 0;
virtual void draw_img(HDC hdc, litehtml::uint_ptr img, const litehtml::position& pos) = 0;
virtual void draw_img_bg(HDC hdc, litehtml::uint_ptr img, const litehtml::position& draw_pos, const litehtml::position& pos, litehtml::background_repeat repeat, litehtml::background_attachment attachment) = 0;
};
} // namespace litehtml

View file

@ -1,10 +1,10 @@
#ifndef LITEHTML_H #ifndef LITEHTML_H
#define LITEHTML_H #define LITEHTML_H
#include <litehtml/html.h>
#include <litehtml/document.h> #include <litehtml/document.h>
#include <litehtml/element.h>
#include <litehtml/html.h>
#include <litehtml/html_tag.h> #include <litehtml/html_tag.h>
#include <litehtml/stylesheet.h> #include <litehtml/stylesheet.h>
#include <litehtml/element.h>
#endif // LITEHTML_H #endif // LITEHTML_H

View file

@ -1,35 +1,30 @@
#ifndef LH_ATTRIBUTES_H #ifndef LH_ATTRIBUTES_H
#define LH_ATTRIBUTES_H #define LH_ATTRIBUTES_H
namespace litehtml namespace litehtml {
{ struct attr_color {
struct attr_color unsigned char rgbBlue;
{ unsigned char rgbGreen;
unsigned char rgbBlue; unsigned char rgbRed;
unsigned char rgbGreen; unsigned char rgbAlpha;
unsigned char rgbRed; attr_color() {
unsigned char rgbAlpha; rgbAlpha = 255;
attr_color() rgbBlue = 0;
{ rgbGreen = 0;
rgbAlpha = 255; rgbRed = 0;
rgbBlue = 0; }
rgbGreen = 0; };
rgbRed = 0;
}
};
struct attr_border struct attr_border {
{ style_border border;
style_border border; int width;
int width; attr_color color;
attr_color color;
attr_border() attr_border() {
{ border = borderNone;
border = borderNone; width = 0;
width = 0; }
} };
}; } // namespace litehtml
}
#endif // LH_ATTRIBUTES_H #endif // LH_ATTRIBUTES_H

View file

@ -1,58 +1,56 @@
#ifndef LH_BACKGROUND_H #ifndef LH_BACKGROUND_H
#define LH_BACKGROUND_H #define LH_BACKGROUND_H
#include "types.h"
#include "attributes.h" #include "attributes.h"
#include "borders.h"
#include "css_length.h" #include "css_length.h"
#include "css_position.h" #include "css_position.h"
#include "types.h"
#include "web_color.h" #include "web_color.h"
#include "borders.h"
namespace litehtml namespace litehtml {
{ class background {
class background public:
{ tstring m_image;
public: tstring m_baseurl;
tstring m_image; web_color m_color;
tstring m_baseurl; background_attachment m_attachment;
web_color m_color; css_position m_position;
background_attachment m_attachment; background_repeat m_repeat;
css_position m_position; background_box m_clip;
background_repeat m_repeat; background_box m_origin;
background_box m_clip; css_border_radius m_radius;
background_box m_origin;
css_border_radius m_radius;
public: public:
background(); background();
background(const background& val); background(const background& val);
~background() = default; ~background() = default;
background& operator=(const background& val); background& operator=(const background& val);
}; };
class background_paint class background_paint {
{ public:
public: tstring image;
tstring image; tstring baseurl;
tstring baseurl; background_attachment attachment;
background_attachment attachment; background_repeat repeat;
background_repeat repeat; web_color color;
web_color color; position clip_box;
position clip_box; position origin_box;
position origin_box; position border_box;
position border_box; border_radiuses border_radius;
border_radiuses border_radius; size image_size;
size image_size; int position_x;
int position_x; int position_y;
int position_y; bool is_root;
bool is_root;
public:
background_paint();
background_paint(const background_paint& val);
background_paint& operator=(const background& val);
};
} public:
background_paint();
background_paint(const background_paint& val);
background_paint& operator=(const background& val);
};
} // namespace litehtml
#endif // LH_BACKGROUND_H #endif // LH_BACKGROUND_H

View file

@ -4,302 +4,259 @@
#include "css_length.h" #include "css_length.h"
#include "types.h" #include "types.h"
namespace litehtml namespace litehtml {
{ struct css_border {
struct css_border css_length width;
{ border_style style;
css_length width; web_color color;
border_style style;
web_color color;
css_border() css_border() { style = border_style_none; }
{
style = border_style_none;
}
css_border(const css_border& val) css_border(const css_border& val) {
{ width = val.width;
width = val.width; style = val.style;
style = val.style; color = val.color;
color = val.color; }
}
css_border& operator=(const css_border& val) css_border& operator=(const css_border& val) {
{ width = val.width;
width = val.width; style = val.style;
style = val.style; color = val.color;
color = val.color; return *this;
return *this; }
} };
};
struct border struct border {
{ int width;
int width; border_style style;
border_style style; web_color color;
web_color color;
border() border() { width = 0; }
{ border(const border& val) {
width = 0; width = val.width;
} style = val.style;
border(const border& val) color = val.color;
{ }
width = val.width; border(const css_border& val) {
style = val.style; width = (int)val.width.val();
color = val.color; style = val.style;
} color = val.color;
border(const css_border& val) }
{ border& operator=(const border& val) {
width = (int) val.width.val(); width = val.width;
style = val.style; style = val.style;
color = val.color; color = val.color;
} return *this;
border& operator=(const border& val) }
{ border& operator=(const css_border& val) {
width = val.width; width = (int)val.width.val();
style = val.style; style = val.style;
color = val.color; color = val.color;
return *this; return *this;
} }
border& operator=(const css_border& val) };
{
width = (int) val.width.val();
style = val.style;
color = val.color;
return *this;
}
};
struct border_radiuses struct border_radiuses {
{ int top_left_x;
int top_left_x; int top_left_y;
int top_left_y;
int top_right_x; int top_right_x;
int top_right_y; int top_right_y;
int bottom_right_x; int bottom_right_x;
int bottom_right_y; int bottom_right_y;
int bottom_left_x; int bottom_left_x;
int bottom_left_y; int bottom_left_y;
border_radiuses() border_radiuses() {
{ top_left_x = 0;
top_left_x = 0; top_left_y = 0;
top_left_y = 0; top_right_x = 0;
top_right_x = 0; top_right_y = 0;
top_right_y = 0; bottom_right_x = 0;
bottom_right_x = 0; bottom_right_y = 0;
bottom_right_y = 0; bottom_left_x = 0;
bottom_left_x = 0; bottom_left_y = 0;
bottom_left_y = 0; }
} border_radiuses(const border_radiuses& val) {
border_radiuses(const border_radiuses& val) top_left_x = val.top_left_x;
{ top_left_y = val.top_left_y;
top_left_x = val.top_left_x; top_right_x = val.top_right_x;
top_left_y = val.top_left_y; top_right_y = val.top_right_y;
top_right_x = val.top_right_x; bottom_right_x = val.bottom_right_x;
top_right_y = val.top_right_y; bottom_right_y = val.bottom_right_y;
bottom_right_x = val.bottom_right_x; bottom_left_x = val.bottom_left_x;
bottom_right_y = val.bottom_right_y; bottom_left_y = val.bottom_left_y;
bottom_left_x = val.bottom_left_x; }
bottom_left_y = val.bottom_left_y; border_radiuses& operator=(const border_radiuses& val) {
} top_left_x = val.top_left_x;
border_radiuses& operator = (const border_radiuses& val) top_left_y = val.top_left_y;
{ top_right_x = val.top_right_x;
top_left_x = val.top_left_x; top_right_y = val.top_right_y;
top_left_y = val.top_left_y; bottom_right_x = val.bottom_right_x;
top_right_x = val.top_right_x; bottom_right_y = val.bottom_right_y;
top_right_y = val.top_right_y; bottom_left_x = val.bottom_left_x;
bottom_right_x = val.bottom_right_x; bottom_left_y = val.bottom_left_y;
bottom_right_y = val.bottom_right_y; return *this;
bottom_left_x = val.bottom_left_x; }
bottom_left_y = val.bottom_left_y; void operator+=(const margins& mg) {
return *this; top_left_x += mg.left;
} top_left_y += mg.top;
void operator += (const margins& mg) top_right_x += mg.right;
{ top_right_y += mg.top;
top_left_x += mg.left; bottom_right_x += mg.right;
top_left_y += mg.top; bottom_right_y += mg.bottom;
top_right_x += mg.right; bottom_left_x += mg.left;
top_right_y += mg.top; bottom_left_y += mg.bottom;
bottom_right_x += mg.right; fix_values();
bottom_right_y += mg.bottom; }
bottom_left_x += mg.left; void operator-=(const margins& mg) {
bottom_left_y += mg.bottom; top_left_x -= mg.left;
fix_values(); top_left_y -= mg.top;
} top_right_x -= mg.right;
void operator -= (const margins& mg) top_right_y -= mg.top;
{ bottom_right_x -= mg.right;
top_left_x -= mg.left; bottom_right_y -= mg.bottom;
top_left_y -= mg.top; bottom_left_x -= mg.left;
top_right_x -= mg.right; bottom_left_y -= mg.bottom;
top_right_y -= mg.top; fix_values();
bottom_right_x -= mg.right; }
bottom_right_y -= mg.bottom; void fix_values() {
bottom_left_x -= mg.left; if (top_left_x < 0) top_left_x = 0;
bottom_left_y -= mg.bottom; if (top_left_y < 0) top_left_y = 0;
fix_values(); if (top_right_x < 0) top_right_x = 0;
} if (top_right_y < 0) top_right_y = 0;
void fix_values() if (bottom_right_x < 0) bottom_right_x = 0;
{ if (bottom_right_y < 0) bottom_right_y = 0;
if (top_left_x < 0) top_left_x = 0; if (bottom_left_x < 0) bottom_left_x = 0;
if (top_left_y < 0) top_left_y = 0; if (bottom_left_y < 0) bottom_left_y = 0;
if (top_right_x < 0) top_right_x = 0; }
if (top_right_y < 0) top_right_y = 0; };
if (bottom_right_x < 0) bottom_right_x = 0;
if (bottom_right_y < 0) bottom_right_y = 0;
if (bottom_left_x < 0) bottom_left_x = 0;
if (bottom_left_y < 0) bottom_left_y = 0;
}
};
struct css_border_radius struct css_border_radius {
{ css_length top_left_x;
css_length top_left_x; css_length top_left_y;
css_length top_left_y;
css_length top_right_x; css_length top_right_x;
css_length top_right_y; css_length top_right_y;
css_length bottom_right_x; css_length bottom_right_x;
css_length bottom_right_y; css_length bottom_right_y;
css_length bottom_left_x; css_length bottom_left_x;
css_length bottom_left_y; css_length bottom_left_y;
css_border_radius() css_border_radius() {}
{
} css_border_radius(const css_border_radius& val) {
top_left_x = val.top_left_x;
top_left_y = val.top_left_y;
top_right_x = val.top_right_x;
top_right_y = val.top_right_y;
bottom_left_x = val.bottom_left_x;
bottom_left_y = val.bottom_left_y;
bottom_right_x = val.bottom_right_x;
bottom_right_y = val.bottom_right_y;
}
css_border_radius(const css_border_radius& val) css_border_radius& operator=(const css_border_radius& val) {
{ top_left_x = val.top_left_x;
top_left_x = val.top_left_x; top_left_y = val.top_left_y;
top_left_y = val.top_left_y; top_right_x = val.top_right_x;
top_right_x = val.top_right_x; top_right_y = val.top_right_y;
top_right_y = val.top_right_y; bottom_left_x = val.bottom_left_x;
bottom_left_x = val.bottom_left_x; bottom_left_y = val.bottom_left_y;
bottom_left_y = val.bottom_left_y; bottom_right_x = val.bottom_right_x;
bottom_right_x = val.bottom_right_x; bottom_right_y = val.bottom_right_y;
bottom_right_y = val.bottom_right_y; return *this;
} }
border_radiuses calc_percents(int width, int height) {
border_radiuses ret;
ret.bottom_left_x = bottom_left_x.calc_percent(width);
ret.bottom_left_y = bottom_left_y.calc_percent(height);
ret.top_left_x = top_left_x.calc_percent(width);
ret.top_left_y = top_left_y.calc_percent(height);
ret.top_right_x = top_right_x.calc_percent(width);
ret.top_right_y = top_right_y.calc_percent(height);
ret.bottom_right_x = bottom_right_x.calc_percent(width);
ret.bottom_right_y = bottom_right_y.calc_percent(height);
return ret;
}
};
css_border_radius& operator=(const css_border_radius& val) struct css_borders {
{ css_border left;
top_left_x = val.top_left_x; css_border top;
top_left_y = val.top_left_y; css_border right;
top_right_x = val.top_right_x; css_border bottom;
top_right_y = val.top_right_y; css_border_radius radius;
bottom_left_x = val.bottom_left_x;
bottom_left_y = val.bottom_left_y;
bottom_right_x = val.bottom_right_x;
bottom_right_y = val.bottom_right_y;
return *this;
}
border_radiuses calc_percents(int width, int height)
{
border_radiuses ret;
ret.bottom_left_x = bottom_left_x.calc_percent(width);
ret.bottom_left_y = bottom_left_y.calc_percent(height);
ret.top_left_x = top_left_x.calc_percent(width);
ret.top_left_y = top_left_y.calc_percent(height);
ret.top_right_x = top_right_x.calc_percent(width);
ret.top_right_y = top_right_y.calc_percent(height);
ret.bottom_right_x = bottom_right_x.calc_percent(width);
ret.bottom_right_y = bottom_right_y.calc_percent(height);
return ret;
}
};
struct css_borders css_borders() = default;
{
css_border left;
css_border top;
css_border right;
css_border bottom;
css_border_radius radius;
css_borders() = default; bool is_visible() const { return left.width.val() != 0 || right.width.val() != 0 || top.width.val() != 0 || bottom.width.val() != 0; }
bool is_visible() const css_borders(const css_borders& val) {
{ left = val.left;
return left.width.val() != 0 || right.width.val() != 0 || top.width.val() != 0 || bottom.width.val() != 0; right = val.right;
} top = val.top;
bottom = val.bottom;
radius = val.radius;
}
css_borders(const css_borders& val) css_borders& operator=(const css_borders& val) {
{ left = val.left;
left = val.left; right = val.right;
right = val.right; top = val.top;
top = val.top; bottom = val.bottom;
bottom = val.bottom; radius = val.radius;
radius = val.radius; return *this;
} }
};
css_borders& operator=(const css_borders& val) struct borders {
{ border left;
left = val.left; border top;
right = val.right; border right;
top = val.top; border bottom;
bottom = val.bottom; border_radiuses radius;
radius = val.radius;
return *this;
}
};
struct borders borders() = default;
{
border left;
border top;
border right;
border bottom;
border_radiuses radius;
borders() = default; borders(const borders& val) {
left = val.left;
right = val.right;
top = val.top;
bottom = val.bottom;
radius = val.radius;
}
borders(const borders& val) borders(const css_borders& val) {
{ left = val.left;
left = val.left; right = val.right;
right = val.right; top = val.top;
top = val.top; bottom = val.bottom;
bottom = val.bottom; }
radius = val.radius;
}
borders(const css_borders& val) bool is_visible() const { return left.width != 0 || right.width != 0 || top.width != 0 || bottom.width != 0; }
{
left = val.left;
right = val.right;
top = val.top;
bottom = val.bottom;
}
bool is_visible() const borders& operator=(const borders& val) {
{ left = val.left;
return left.width != 0 || right.width != 0 || top.width != 0 || bottom.width != 0; right = val.right;
} top = val.top;
bottom = val.bottom;
radius = val.radius;
return *this;
}
borders& operator=(const borders& val) borders& operator=(const css_borders& val) {
{ left = val.left;
left = val.left; right = val.right;
right = val.right; top = val.top;
top = val.top; bottom = val.bottom;
bottom = val.bottom; return *this;
radius = val.radius; }
return *this; };
} } // namespace litehtml
borders& operator=(const css_borders& val)
{
left = val.left;
right = val.right;
top = val.top;
bottom = val.bottom;
return *this;
}
};
}
#endif // LH_BORDERS_H #endif // LH_BORDERS_H

View file

@ -1,120 +1,111 @@
#ifndef LH_BOX_H #ifndef LH_BOX_H
#define LH_BOX_H #define LH_BOX_H
namespace litehtml namespace litehtml {
{ class html_tag;
class html_tag;
enum box_type enum box_type { box_block, box_line };
{
box_block,
box_line
};
class box class box {
{ public:
public: typedef std::unique_ptr<litehtml::box> ptr;
typedef std::unique_ptr<litehtml::box> ptr; typedef std::vector<box::ptr> vector;
typedef std::vector< box::ptr > vector;
protected:
int m_box_top;
int m_box_left;
int m_box_right;
public:
box(int top, int left, int right)
{
m_box_top = top;
m_box_left = left;
m_box_right = right;
}
virtual ~box() = default;
int bottom() const { return m_box_top + height(); } protected:
int top() const { return m_box_top; } int m_box_top;
int right() const { return m_box_left + width(); } int m_box_left;
int left() const { return m_box_left; } int m_box_right;
virtual litehtml::box_type get_type() const = 0; public:
virtual int height() const = 0; box(int top, int left, int right) {
virtual int width() const = 0; m_box_top = top;
virtual void add_element(const element::ptr &el) = 0; m_box_left = left;
virtual bool can_hold(const element::ptr &el, white_space ws) const = 0; m_box_right = right;
virtual void finish(bool last_box = false) = 0; }
virtual bool is_empty() const = 0; virtual ~box() = default;
virtual int baseline() const = 0;
virtual void get_elements(elements_vector& els) = 0;
virtual int top_margin() const = 0;
virtual int bottom_margin() const = 0;
virtual void y_shift(int shift) = 0;
virtual void new_width(int left, int right, elements_vector& els) = 0;
};
////////////////////////////////////////////////////////////////////////// int bottom() const { return m_box_top + height(); }
int top() const { return m_box_top; }
int right() const { return m_box_left + width(); }
int left() const { return m_box_left; }
class block_box : public box virtual litehtml::box_type get_type() const = 0;
{ virtual int height() const = 0;
element::ptr m_element; virtual int width() const = 0;
public: virtual void add_element(const element::ptr& el) = 0;
block_box(int top, int left, int right) : box(top, left, right) virtual bool can_hold(const element::ptr& el, white_space ws) const = 0;
{ virtual void finish(bool last_box = false) = 0;
m_element = nullptr; virtual bool is_empty() const = 0;
} virtual int baseline() const = 0;
virtual void get_elements(elements_vector& els) = 0;
virtual int top_margin() const = 0;
virtual int bottom_margin() const = 0;
virtual void y_shift(int shift) = 0;
virtual void new_width(int left, int right, elements_vector& els) = 0;
};
litehtml::box_type get_type() const override; //////////////////////////////////////////////////////////////////////////
int height() const override;
int width() const override;
void add_element(const element::ptr &el) override;
bool can_hold(const element::ptr &el, white_space ws) const override;
void finish(bool last_box = false) override;
bool is_empty() const override;
int baseline() const override;
void get_elements(elements_vector& els) override;
int top_margin() const override;
int bottom_margin() const override;
void y_shift(int shift) override;
void new_width(int left, int right, elements_vector& els) override;
};
////////////////////////////////////////////////////////////////////////// class block_box : public box {
element::ptr m_element;
class line_box : public box public:
{ block_box(int top, int left, int right) : box(top, left, right) { m_element = nullptr; }
elements_vector m_items;
int m_height;
int m_width;
int m_line_height;
font_metrics m_font_metrics;
int m_baseline;
text_align m_text_align;
public:
line_box(int top, int left, int right, int line_height, font_metrics& fm, text_align align) : box(top, left, right)
{
m_height = 0;
m_width = 0;
m_font_metrics = fm;
m_line_height = line_height;
m_baseline = 0;
m_text_align = align;
}
litehtml::box_type get_type() const override; litehtml::box_type get_type() const override;
int height() const override; int height() const override;
int width() const override; int width() const override;
void add_element(const element::ptr &el) override; void add_element(const element::ptr& el) override;
bool can_hold(const element::ptr &el, white_space ws) const override; bool can_hold(const element::ptr& el, white_space ws) const override;
void finish(bool last_box = false) override; void finish(bool last_box = false) override;
bool is_empty() const override; bool is_empty() const override;
int baseline() const override; int baseline() const override;
void get_elements(elements_vector& els) override; void get_elements(elements_vector& els) override;
int top_margin() const override; int top_margin() const override;
int bottom_margin() const override; int bottom_margin() const override;
void y_shift(int shift) override; void y_shift(int shift) override;
void new_width(int left, int right, elements_vector& els) override; void new_width(int left, int right, elements_vector& els) override;
};
private: //////////////////////////////////////////////////////////////////////////
bool have_last_space() const;
bool is_break_only() const; class line_box : public box {
}; elements_vector m_items;
} int m_height;
int m_width;
int m_line_height;
font_metrics m_font_metrics;
int m_baseline;
text_align m_text_align;
public:
line_box(int top, int left, int right, int line_height, font_metrics& fm, text_align align) : box(top, left, right) {
m_height = 0;
m_width = 0;
m_font_metrics = fm;
m_line_height = line_height;
m_baseline = 0;
m_text_align = align;
}
litehtml::box_type get_type() const override;
int height() const override;
int width() const override;
void add_element(const element::ptr& el) override;
bool can_hold(const element::ptr& el, white_space ws) const override;
void finish(bool last_box = false) override;
bool is_empty() const override;
int baseline() const override;
void get_elements(elements_vector& els) override;
int top_margin() const override;
int bottom_margin() const override;
void y_shift(int shift) override;
void new_width(int left, int right, elements_vector& els) override;
private:
bool have_last_space() const;
bool is_break_only() const;
};
} // namespace litehtml
#endif // LH_BOX_H #endif // LH_BOX_H

View file

@ -46,6 +46,6 @@ bool is_url_reserved_codepoint(litehtml::tchar_t c);
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.1 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.1
bool is_url_scheme_codepoint(litehtml::tchar_t c); bool is_url_scheme_codepoint(litehtml::tchar_t c);
} // namespace litehtml } // namespace litehtml
#endif // LITEHTML_CODEPOINT_H__ #endif // LITEHTML_CODEPOINT_H__

View file

@ -3,18 +3,14 @@
#include "stylesheet.h" #include "stylesheet.h"
namespace litehtml namespace litehtml {
{ class context {
class context litehtml::css m_master_css;
{
litehtml::css m_master_css; public:
public: void load_master_stylesheet(const tchar_t* str);
void load_master_stylesheet(const tchar_t* str); litehtml::css& master_css() { return m_master_css; }
litehtml::css& master_css() };
{ } // namespace litehtml
return m_master_css;
}
};
}
#endif // LH_CONTEXT_H #endif // LH_CONTEXT_H

View file

@ -3,133 +3,107 @@
#include "types.h" #include "types.h"
namespace litehtml namespace litehtml {
{ class css_length {
class css_length union {
{ float m_value;
union int m_predef;
{ };
float m_value; css_units m_units;
int m_predef; bool m_is_predefined;
};
css_units m_units;
bool m_is_predefined;
public:
css_length();
css_length(const css_length& val);
css_length& operator=(const css_length& val); public:
css_length& operator=(float val); css_length();
bool is_predefined() const; css_length(const css_length& val);
void predef(int val);
int predef() const;
void set_value(float val, css_units units);
float val() const;
css_units units() const;
int calc_percent(int width) const;
void fromString(const tstring& str, const tstring& predefs = _t(""), int defValue = 0);
};
// css_length inlines css_length& operator=(const css_length& val);
css_length& operator=(float val);
bool is_predefined() const;
void predef(int val);
int predef() const;
void set_value(float val, css_units units);
float val() const;
css_units units() const;
int calc_percent(int width) const;
void fromString(const tstring& str, const tstring& predefs = _t(""), int defValue = 0);
};
inline css_length::css_length() // css_length inlines
{
m_value = 0;
m_predef = 0;
m_units = css_units_none;
m_is_predefined = false;
}
inline css_length::css_length(const css_length& val) inline css_length::css_length() {
{ m_value = 0;
if(val.is_predefined()) m_predef = 0;
{ m_units = css_units_none;
m_predef = val.m_predef; m_is_predefined = false;
} else
{
m_value = val.m_value;
}
m_units = val.m_units;
m_is_predefined = val.m_is_predefined;
}
inline css_length& css_length::operator=(const css_length& val)
{
if(val.is_predefined())
{
m_predef = val.m_predef;
} else
{
m_value = val.m_value;
}
m_units = val.m_units;
m_is_predefined = val.m_is_predefined;
return *this;
}
inline css_length& css_length::operator=(float val)
{
m_value = val;
m_units = css_units_px;
m_is_predefined = false;
return *this;
}
inline bool css_length::is_predefined() const
{
return m_is_predefined;
}
inline void css_length::predef(int val)
{
m_predef = val;
m_is_predefined = true;
}
inline int css_length::predef() const
{
if(m_is_predefined)
{
return m_predef;
}
return 0;
}
inline void css_length::set_value(float val, css_units units)
{
m_value = val;
m_is_predefined = false;
m_units = units;
}
inline float css_length::val() const
{
if(!m_is_predefined)
{
return m_value;
}
return 0;
}
inline css_units css_length::units() const
{
return m_units;
}
inline int css_length::calc_percent(int width) const
{
if(!is_predefined())
{
if(units() == css_units_percentage)
{
return (int) ((double) width * (double) m_value / 100.0);
} else
{
return (int) val();
}
}
return 0;
}
} }
inline css_length::css_length(const css_length& val) {
if (val.is_predefined()) {
m_predef = val.m_predef;
} else {
m_value = val.m_value;
}
m_units = val.m_units;
m_is_predefined = val.m_is_predefined;
}
inline css_length& css_length::operator=(const css_length& val) {
if (val.is_predefined()) {
m_predef = val.m_predef;
} else {
m_value = val.m_value;
}
m_units = val.m_units;
m_is_predefined = val.m_is_predefined;
return *this;
}
inline css_length& css_length::operator=(float val) {
m_value = val;
m_units = css_units_px;
m_is_predefined = false;
return *this;
}
inline bool css_length::is_predefined() const { return m_is_predefined; }
inline void css_length::predef(int val) {
m_predef = val;
m_is_predefined = true;
}
inline int css_length::predef() const {
if (m_is_predefined) {
return m_predef;
}
return 0;
}
inline void css_length::set_value(float val, css_units units) {
m_value = val;
m_is_predefined = false;
m_units = units;
}
inline float css_length::val() const {
if (!m_is_predefined) {
return m_value;
}
return 0;
}
inline css_units css_length::units() const { return m_units; }
inline int css_length::calc_percent(int width) const {
if (!is_predefined()) {
if (units() == css_units_percentage) {
return (int)((double)width * (double)m_value / 100.0);
} else {
return (int)val();
}
}
return 0;
}
} // namespace litehtml
#endif // LH_CSS_LENGTH_H #endif // LH_CSS_LENGTH_H

View file

@ -3,34 +3,30 @@
#include "css_length.h" #include "css_length.h"
namespace litehtml namespace litehtml {
{ struct css_margins {
struct css_margins css_length left;
{ css_length right;
css_length left; css_length top;
css_length right; css_length bottom;
css_length top;
css_length bottom;
css_margins() = default; css_margins() = default;
css_margins(const css_margins& val) css_margins(const css_margins& val) {
{ left = val.left;
left = val.left; right = val.right;
right = val.right; top = val.top;
top = val.top; bottom = val.bottom;
bottom = val.bottom; }
}
css_margins& operator=(const css_margins& val) css_margins& operator=(const css_margins& val) {
{ left = val.left;
left = val.left; right = val.right;
right = val.right; top = val.top;
top = val.top; bottom = val.bottom;
bottom = val.bottom; return *this;
return *this; }
} };
}; } // namespace litehtml
}
#endif // LH_CSS_MARGINS_H #endif // LH_CSS_MARGINS_H

View file

@ -3,34 +3,30 @@
#include "css_length.h" #include "css_length.h"
namespace litehtml namespace litehtml {
{ struct css_offsets {
struct css_offsets css_length left;
{ css_length top;
css_length left; css_length right;
css_length top; css_length bottom;
css_length right;
css_length bottom;
css_offsets() = default; css_offsets() = default;
css_offsets(const css_offsets& val) css_offsets(const css_offsets& val) {
{ left = val.left;
left = val.left; top = val.top;
top = val.top; right = val.right;
right = val.right; bottom = val.bottom;
bottom = val.bottom; }
}
css_offsets& operator=(const css_offsets& val) css_offsets& operator=(const css_offsets& val) {
{ left = val.left;
left = val.left; top = val.top;
top = val.top; right = val.right;
right = val.right; bottom = val.bottom;
bottom = val.bottom; return *this;
return *this; }
} };
}; } // namespace litehtml
}
#endif // LH_CSS_OFFSETS_H #endif // LH_CSS_OFFSETS_H

View file

@ -3,34 +3,30 @@
#include "css_length.h" #include "css_length.h"
namespace litehtml namespace litehtml {
{ struct css_position {
struct css_position css_length x;
{ css_length y;
css_length x; css_length width;
css_length y; css_length height;
css_length width;
css_length height;
css_position() = default; css_position() = default;
css_position(const css_position& val) css_position(const css_position& val) {
{ x = val.x;
x = val.x; y = val.y;
y = val.y; width = val.width;
width = val.width; height = val.height;
height = val.height; }
}
css_position& operator=(const css_position& val) css_position& operator=(const css_position& val) {
{ x = val.x;
x = val.x; y = val.y;
y = val.y; width = val.width;
width = val.width; height = val.height;
height = val.height; return *this;
return *this; }
} };
}; } // namespace litehtml
}
#endif // LH_CSS_POSITION_H #endif // LH_CSS_POSITION_H

View file

@ -1,276 +1,220 @@
#ifndef LH_CSS_SELECTOR_H #ifndef LH_CSS_SELECTOR_H
#define LH_CSS_SELECTOR_H #define LH_CSS_SELECTOR_H
#include "style.h"
#include "media_query.h" #include "media_query.h"
#include "style.h"
namespace litehtml namespace litehtml {
{ //////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
struct selector_specificity struct selector_specificity {
{ int a;
int a; int b;
int b; int c;
int c; int d;
int d;
explicit selector_specificity(int va = 0, int vb = 0, int vc = 0, int vd = 0) explicit selector_specificity(int va = 0, int vb = 0, int vc = 0, int vd = 0) {
{ a = va;
a = va; b = vb;
b = vb; c = vc;
c = vc; d = vd;
d = vd; }
}
void operator += (const selector_specificity& val) void operator+=(const selector_specificity& val) {
{ a += val.a;
a += val.a; b += val.b;
b += val.b; c += val.c;
c += val.c; d += val.d;
d += val.d; }
}
bool operator==(const selector_specificity& val) const bool operator==(const selector_specificity& val) const {
{ if (a == val.a && b == val.b && c == val.c && d == val.d) {
if(a == val.a && b == val.b && c == val.c && d == val.d) return true;
{ }
return true; return false;
} }
return false;
}
bool operator!=(const selector_specificity& val) const bool operator!=(const selector_specificity& val) const {
{ if (a != val.a || b != val.b || c != val.c || d != val.d) {
if(a != val.a || b != val.b || c != val.c || d != val.d) return true;
{ }
return true; return false;
} }
return false;
}
bool operator > (const selector_specificity& val) const bool operator>(const selector_specificity& val) const {
{ if (a > val.a) {
if(a > val.a) return true;
{ } else if (a < val.a) {
return true; return false;
} else if(a < val.a) } else {
{ if (b > val.b) {
return false; return true;
} else } else if (b < val.b) {
{ return false;
if(b > val.b) } else {
{ if (c > val.c) {
return true; return true;
} else if(b < val.b) } else if (c < val.c) {
{ return false;
return false; } else {
} else if (d > val.d) {
{ return true;
if(c > val.c) } else if (d < val.d) {
{ return false;
return true; }
} else if(c < val.c) }
{ }
return false; }
} else return false;
{ }
if(d > val.d)
{
return true;
} else if(d < val.d)
{
return false;
}
}
}
}
return false;
}
bool operator >= (const selector_specificity& val) const bool operator>=(const selector_specificity& val) const {
{ if ((*this) == val) return true;
if((*this) == val) return true; if ((*this) > val) return true;
if((*this) > val) return true; return false;
return false; }
}
bool operator <= (const selector_specificity& val) const bool operator<=(const selector_specificity& val) const {
{ if ((*this) > val) {
if((*this) > val) return false;
{ }
return false; return true;
} }
return true;
}
bool operator < (const selector_specificity& val) const bool operator<(const selector_specificity& val) const {
{ if ((*this) <= val && (*this) != val) {
if((*this) <= val && (*this) != val) return true;
{ }
return true; return false;
} }
return false; };
}
}; //////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////// enum attr_select_condition {
select_exists,
select_equal,
select_contain_str,
select_start_str,
select_end_str,
select_pseudo_class,
select_pseudo_element,
};
enum attr_select_condition //////////////////////////////////////////////////////////////////////////
{
select_exists,
select_equal,
select_contain_str,
select_start_str,
select_end_str,
select_pseudo_class,
select_pseudo_element,
};
////////////////////////////////////////////////////////////////////////// struct css_attribute_selector {
typedef std::vector<css_attribute_selector> vector;
struct css_attribute_selector tstring attribute;
{ tstring val;
typedef std::vector<css_attribute_selector> vector; string_vector class_val;
attr_select_condition condition;
tstring attribute; css_attribute_selector() { condition = select_exists; }
tstring val; };
string_vector class_val;
attr_select_condition condition;
css_attribute_selector() //////////////////////////////////////////////////////////////////////////
{
condition = select_exists;
}
};
////////////////////////////////////////////////////////////////////////// class css_element_selector {
public:
tstring m_tag;
css_attribute_selector::vector m_attrs;
class css_element_selector public:
{ void parse(const tstring& txt);
public: };
tstring m_tag;
css_attribute_selector::vector m_attrs;
public:
void parse(const tstring& txt); //////////////////////////////////////////////////////////////////////////
};
////////////////////////////////////////////////////////////////////////// enum css_combinator { combinator_descendant, combinator_child, combinator_adjacent_sibling, combinator_general_sibling };
enum css_combinator //////////////////////////////////////////////////////////////////////////
{
combinator_descendant,
combinator_child,
combinator_adjacent_sibling,
combinator_general_sibling
};
////////////////////////////////////////////////////////////////////////// class css_selector {
public:
typedef std::shared_ptr<css_selector> ptr;
typedef std::vector<css_selector::ptr> vector;
class css_selector public:
{ selector_specificity m_specificity;
public: css_element_selector m_right;
typedef std::shared_ptr<css_selector> ptr; css_selector::ptr m_left;
typedef std::vector<css_selector::ptr> vector; css_combinator m_combinator;
public: style::ptr m_style;
selector_specificity m_specificity; int m_order;
css_element_selector m_right; media_query_list::ptr m_media_query;
css_selector::ptr m_left;
css_combinator m_combinator;
style::ptr m_style;
int m_order;
media_query_list::ptr m_media_query;
public:
explicit css_selector(const media_query_list::ptr& media)
{
m_media_query = media;
m_combinator = combinator_descendant;
m_order = 0;
}
~css_selector() = default; public:
explicit css_selector(const media_query_list::ptr& media) {
m_media_query = media;
m_combinator = combinator_descendant;
m_order = 0;
}
css_selector(const css_selector& val) ~css_selector() = default;
{
m_right = val.m_right;
if(val.m_left)
{
m_left = std::make_shared<css_selector>(*val.m_left);
} else
{
m_left = nullptr;
}
m_combinator = val.m_combinator;
m_specificity = val.m_specificity;
m_order = val.m_order;
m_media_query = val.m_media_query;
}
bool parse(const tstring& text); css_selector(const css_selector& val) {
void calc_specificity(); m_right = val.m_right;
bool is_media_valid() const; if (val.m_left) {
void add_media_to_doc(document* doc) const; m_left = std::make_shared<css_selector>(*val.m_left);
}; } else {
m_left = nullptr;
}
m_combinator = val.m_combinator;
m_specificity = val.m_specificity;
m_order = val.m_order;
m_media_query = val.m_media_query;
}
inline bool css_selector::is_media_valid() const bool parse(const tstring& text);
{ void calc_specificity();
if(!m_media_query) bool is_media_valid() const;
{ void add_media_to_doc(document* doc) const;
return true; };
}
return m_media_query->is_used();
}
inline bool css_selector::is_media_valid() const {
////////////////////////////////////////////////////////////////////////// if (!m_media_query) {
return true;
inline bool operator > (const css_selector& v1, const css_selector& v2) }
{ return m_media_query->is_used();
if(v1.m_specificity == v2.m_specificity)
{
return (v1.m_order > v2.m_order);
}
return (v1.m_specificity > v2.m_specificity);
}
inline bool operator < (const css_selector& v1, const css_selector& v2)
{
if(v1.m_specificity == v2.m_specificity)
{
return (v1.m_order < v2.m_order);
}
return (v1.m_specificity < v2.m_specificity);
}
inline bool operator >(const css_selector::ptr& v1, const css_selector::ptr& v2)
{
return (*v1 > *v2);
}
inline bool operator < (const css_selector::ptr& v1, const css_selector::ptr& v2)
{
return (*v1 < *v2);
}
//////////////////////////////////////////////////////////////////////////
class used_selector
{
public:
typedef std::unique_ptr<used_selector> ptr;
typedef std::vector<used_selector::ptr> vector;
css_selector::ptr m_selector;
bool m_used;
used_selector(const css_selector::ptr& selector, bool used)
{
m_used = used;
m_selector = selector;
}
};
} }
//////////////////////////////////////////////////////////////////////////
inline bool operator>(const css_selector& v1, const css_selector& v2) {
if (v1.m_specificity == v2.m_specificity) {
return (v1.m_order > v2.m_order);
}
return (v1.m_specificity > v2.m_specificity);
}
inline bool operator<(const css_selector& v1, const css_selector& v2) {
if (v1.m_specificity == v2.m_specificity) {
return (v1.m_order < v2.m_order);
}
return (v1.m_specificity < v2.m_specificity);
}
inline bool operator>(const css_selector::ptr& v1, const css_selector::ptr& v2) { return (*v1 > *v2); }
inline bool operator<(const css_selector::ptr& v1, const css_selector::ptr& v2) { return (*v1 < *v2); }
//////////////////////////////////////////////////////////////////////////
class used_selector {
public:
typedef std::unique_ptr<used_selector> ptr;
typedef std::vector<used_selector::ptr> vector;
css_selector::ptr m_selector;
bool m_used;
used_selector(const css_selector::ptr& selector, bool used) {
m_used = used;
m_selector = selector;
}
};
} // namespace litehtml
#endif // LH_CSS_SELECTOR_H #endif // LH_CSS_SELECTOR_H

View file

@ -1,117 +1,105 @@
#ifndef LH_DOCUMENT_H #ifndef LH_DOCUMENT_H
#define LH_DOCUMENT_H #define LH_DOCUMENT_H
#include "context.h"
#include "style.h" #include "style.h"
#include "types.h" #include "types.h"
#include "context.h"
namespace litehtml namespace litehtml {
{ struct css_text {
struct css_text typedef std::vector<css_text> vector;
{
typedef std::vector<css_text> vector;
tstring text; tstring text;
tstring baseurl; tstring baseurl;
tstring media; tstring media;
css_text() = default;
css_text(const tchar_t* txt, const tchar_t* url, const tchar_t* media_str) css_text() = default;
{
text = txt ? txt : _t("");
baseurl = url ? url : _t("");
media = media_str ? media_str : _t("");
}
css_text(const css_text& val) css_text(const tchar_t* txt, const tchar_t* url, const tchar_t* media_str) {
{ text = txt ? txt : _t("");
text = val.text; baseurl = url ? url : _t("");
baseurl = val.baseurl; media = media_str ? media_str : _t("");
media = val.media; }
}
};
class html_tag; css_text(const css_text& val) {
text = val.text;
baseurl = val.baseurl;
media = val.media;
}
};
class document : public std::enable_shared_from_this<document> class html_tag;
{
public:
typedef std::shared_ptr<document> ptr;
typedef std::weak_ptr<document> weak_ptr;
private:
std::shared_ptr<element> m_root;
document_container* m_container;
fonts_map m_fonts;
css_text::vector m_css;
litehtml::css m_styles;
litehtml::web_color m_def_color;
litehtml::context* m_context;
litehtml::size m_size;
position::vector m_fixed_boxes;
media_query_list::vector m_media_lists;
element::ptr m_over_element;
elements_vector m_tabular_elements;
media_features m_media;
tstring m_lang;
tstring m_culture;
public:
document(litehtml::document_container* objContainer, litehtml::context* ctx);
virtual ~document();
litehtml::document_container* container() { return m_container; } class document : public std::enable_shared_from_this<document> {
uint_ptr get_font(const tchar_t* name, int size, const tchar_t* weight, const tchar_t* style, const tchar_t* decoration, font_metrics* fm); public:
int render(int max_width, render_type rt = render_all); typedef std::shared_ptr<document> ptr;
void draw(uint_ptr hdc, int x, int y, const position* clip); typedef std::weak_ptr<document> weak_ptr;
web_color get_def_color() { return m_def_color; }
int cvt_units(const tchar_t* str, int fontSize, bool* is_percent = nullptr) const;
int cvt_units(css_length& val, int fontSize, int size = 0) const;
int width() const;
int height() const;
void add_stylesheet(const tchar_t* str, const tchar_t* baseurl, const tchar_t* media);
bool on_mouse_over(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);
bool on_lbutton_down(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);
bool on_lbutton_up(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);
bool on_mouse_leave(position::vector& redraw_boxes);
litehtml::element::ptr create_element(const tchar_t* tag_name, const string_map& attributes);
element::ptr root();
void get_fixed_boxes(position::vector& fixed_boxes);
void add_fixed_box(const position& pos);
void add_media_list(const media_query_list::ptr& list);
bool media_changed();
bool lang_changed();
bool match_lang(const tstring & lang);
void add_tabular(const element::ptr& el);
element::const_ptr get_over_element() const { return m_over_element; }
void append_children_from_string(element& parent, const tchar_t* str); private:
void append_children_from_utf8(element& parent, const char* str); std::shared_ptr<element> m_root;
document_container* m_container;
fonts_map m_fonts;
css_text::vector m_css;
litehtml::css m_styles;
litehtml::web_color m_def_color;
litehtml::context* m_context;
litehtml::size m_size;
position::vector m_fixed_boxes;
media_query_list::vector m_media_lists;
element::ptr m_over_element;
elements_vector m_tabular_elements;
media_features m_media;
tstring m_lang;
tstring m_culture;
static litehtml::document::ptr createFromString(const tchar_t* str, litehtml::document_container* objPainter, litehtml::context* ctx, litehtml::css* user_styles = nullptr); public:
static litehtml::document::ptr createFromUTF8(const char* str, litehtml::document_container* objPainter, litehtml::context* ctx, litehtml::css* user_styles = nullptr); document(litehtml::document_container* objContainer, litehtml::context* ctx);
virtual ~document();
private:
litehtml::uint_ptr add_font(const tchar_t* name, int size, const tchar_t* weight, const tchar_t* style, const tchar_t* decoration, font_metrics* fm);
void create_node(void* gnode, elements_vector& elements, bool parseTextNode); litehtml::document_container* container() { return m_container; }
bool update_media_lists(const media_features& features); uint_ptr get_font(const tchar_t* name, int size, const tchar_t* weight, const tchar_t* style, const tchar_t* decoration, font_metrics* fm);
void fix_tables_layout(); int render(int max_width, render_type rt = render_all);
void fix_table_children(element::ptr& el_ptr, style_display disp, const tchar_t* disp_str); void draw(uint_ptr hdc, int x, int y, const position* clip);
void fix_table_parent(element::ptr& el_ptr, style_display disp, const tchar_t* disp_str); web_color get_def_color() { return m_def_color; }
}; int cvt_units(const tchar_t* str, int fontSize, bool* is_percent = nullptr) const;
int cvt_units(css_length& val, int fontSize, int size = 0) const;
int width() const;
int height() const;
void add_stylesheet(const tchar_t* str, const tchar_t* baseurl, const tchar_t* media);
bool on_mouse_over(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);
bool on_lbutton_down(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);
bool on_lbutton_up(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);
bool on_mouse_leave(position::vector& redraw_boxes);
litehtml::element::ptr create_element(const tchar_t* tag_name, const string_map& attributes);
element::ptr root();
void get_fixed_boxes(position::vector& fixed_boxes);
void add_fixed_box(const position& pos);
void add_media_list(const media_query_list::ptr& list);
bool media_changed();
bool lang_changed();
bool match_lang(const tstring& lang);
void add_tabular(const element::ptr& el);
element::const_ptr get_over_element() const { return m_over_element; }
inline element::ptr document::root() void append_children_from_string(element& parent, const tchar_t* str);
{ void append_children_from_utf8(element& parent, const char* str);
return m_root;
} static litehtml::document::ptr createFromString(const tchar_t* str, litehtml::document_container* objPainter, litehtml::context* ctx, litehtml::css* user_styles = nullptr);
inline void document::add_tabular(const element::ptr& el) static litehtml::document::ptr createFromUTF8(const char* str, litehtml::document_container* objPainter, litehtml::context* ctx, litehtml::css* user_styles = nullptr);
{
m_tabular_elements.push_back(el); private:
} litehtml::uint_ptr add_font(const tchar_t* name, int size, const tchar_t* weight, const tchar_t* style, const tchar_t* decoration, font_metrics* fm);
inline bool document::match_lang(const tstring & lang)
{ void create_node(void* gnode, elements_vector& elements, bool parseTextNode);
return lang == m_lang || lang == m_culture; bool update_media_lists(const media_features& features);
} void fix_tables_layout();
} void fix_table_children(element::ptr& el_ptr, style_display disp, const tchar_t* disp_str);
void fix_table_parent(element::ptr& el_ptr, style_display disp, const tchar_t* disp_str);
};
inline element::ptr document::root() { return m_root; }
inline void document::add_tabular(const element::ptr& el) { m_tabular_elements.push_back(el); }
inline bool document::match_lang(const tstring& lang) { return lang == m_lang || lang == m_culture; }
} // namespace litehtml
#endif // LH_DOCUMENT_H #endif // LH_DOCUMENT_H

View file

@ -3,16 +3,14 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_anchor : public html_tag {
class el_anchor : public html_tag public:
{ explicit el_anchor(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_anchor(const std::shared_ptr<litehtml::document>& doc);
void on_click() override; void on_click() override;
void apply_stylesheet(const litehtml::css& stylesheet) override; void apply_stylesheet(const litehtml::css& stylesheet) override;
}; };
} } // namespace litehtml
#endif // LH_EL_ANCHOR_H #endif // LH_EL_ANCHOR_H

View file

@ -3,15 +3,13 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_base : public html_tag {
class el_base : public html_tag public:
{ explicit el_base(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_base(const std::shared_ptr<litehtml::document>& doc);
void parse_attributes() override; void parse_attributes() override;
}; };
} } // namespace litehtml
#endif // LH_EL_BASE_H #endif // LH_EL_BASE_H

View file

@ -3,38 +3,29 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_before_after_base : public html_tag {
class el_before_after_base : public html_tag public:
{ el_before_after_base(const std::shared_ptr<litehtml::document>& doc, bool before);
public:
el_before_after_base(const std::shared_ptr<litehtml::document>& doc, bool before);
void add_style(const litehtml::style& st) override; void add_style(const litehtml::style& st) override;
void apply_stylesheet(const litehtml::css& stylesheet) override; void apply_stylesheet(const litehtml::css& stylesheet) override;
private:
void add_text(const tstring& txt);
void add_function(const tstring& fnc, const tstring& params);
static tstring convert_escape(const tchar_t* txt);
};
class el_before : public el_before_after_base private:
{ void add_text(const tstring& txt);
public: void add_function(const tstring& fnc, const tstring& params);
explicit el_before(const std::shared_ptr<litehtml::document>& doc) : el_before_after_base(doc, true) static tstring convert_escape(const tchar_t* txt);
{ };
} class el_before : public el_before_after_base {
}; public:
explicit el_before(const std::shared_ptr<litehtml::document>& doc) : el_before_after_base(doc, true) {}
};
class el_after : public el_before_after_base class el_after : public el_before_after_base {
{ public:
public: explicit el_after(const std::shared_ptr<litehtml::document>& doc) : el_before_after_base(doc, false) {}
explicit el_after(const std::shared_ptr<litehtml::document>& doc) : el_before_after_base(doc, false) };
{ } // namespace litehtml
}
};
}
#endif // LH_EL_BEFORE_AFTER_H #endif // LH_EL_BEFORE_AFTER_H

View file

@ -3,15 +3,13 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_body : public html_tag {
class el_body : public html_tag public:
{ explicit el_body(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_body(const std::shared_ptr<litehtml::document>& doc);
bool is_body() const override; bool is_body() const override;
}; };
} } // namespace litehtml
#endif // LH_EL_BODY_H #endif // LH_EL_BODY_H

View file

@ -3,15 +3,13 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_break : public html_tag {
class el_break : public html_tag public:
{ explicit el_break(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_break(const std::shared_ptr<litehtml::document>& doc);
bool is_break() const override; bool is_break() const override;
}; };
} } // namespace litehtml
#endif // LH_EL_BREAK_H #endif // LH_EL_BREAK_H

View file

@ -3,17 +3,16 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_cdata : public element {
class el_cdata : public element tstring m_text;
{
tstring m_text;
public:
explicit el_cdata(const std::shared_ptr<litehtml::document>& doc);
void get_text(tstring& text) override; public:
void set_data(const tchar_t* data) override; explicit el_cdata(const std::shared_ptr<litehtml::document>& doc);
};
} void get_text(tstring& text) override;
void set_data(const tchar_t* data) override;
};
} // namespace litehtml
#endif // LH_EL_CDATA_H #endif // LH_EL_CDATA_H

View file

@ -3,17 +3,16 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_comment : public element {
class el_comment : public element tstring m_text;
{
tstring m_text;
public:
explicit el_comment(const std::shared_ptr<litehtml::document>& doc);
void get_text(tstring& text) override; public:
void set_data(const tchar_t* data) override; explicit el_comment(const std::shared_ptr<litehtml::document>& doc);
};
} void get_text(tstring& text) override;
void set_data(const tchar_t* data) override;
};
} // namespace litehtml
#endif // LH_EL_COMMENT_H #endif // LH_EL_COMMENT_H

View file

@ -3,15 +3,13 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_div : public html_tag {
class el_div : public html_tag public:
{ explicit el_div(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_div(const std::shared_ptr<litehtml::document>& doc);
void parse_attributes() override; void parse_attributes() override;
}; };
} } // namespace litehtml
#endif // LH_EL_DIV_H #endif // LH_EL_DIV_H

View file

@ -3,15 +3,13 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_font : public html_tag {
class el_font : public html_tag public:
{ explicit el_font(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_font(const std::shared_ptr<litehtml::document>& doc);
void parse_attributes() override; void parse_attributes() override;
}; };
} } // namespace litehtml
#endif // LH_EL_FONT_H #endif // LH_EL_FONT_H

View file

@ -3,26 +3,26 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{
class el_image : public html_tag class el_image : public html_tag {
{ tstring m_src;
tstring m_src;
public:
el_image(const std::shared_ptr<litehtml::document>& doc);
virtual ~el_image(void);
virtual int line_height() const override; public:
virtual bool is_replaced() const override; el_image(const std::shared_ptr<litehtml::document>& doc);
virtual int render(int x, int y, int max_width, bool second_pass = false) override; virtual ~el_image(void);
virtual void parse_attributes() override;
virtual void parse_styles(bool is_reparse = false) override; virtual int line_height() const override;
virtual void draw(uint_ptr hdc, int x, int y, const position* clip) override; virtual bool is_replaced() const override;
virtual void get_content_size(size& sz, int max_width) override; virtual int render(int x, int y, int max_width, bool second_pass = false) override;
private: virtual void parse_attributes() override;
int calc_max_height(int image_height); virtual void parse_styles(bool is_reparse = false) override;
}; virtual void draw(uint_ptr hdc, int x, int y, const position* clip) override;
} virtual void get_content_size(size& sz, int max_width) override;
private:
int calc_max_height(int image_height);
};
} // namespace litehtml
#endif // LH_EL_IMAGE_H #endif // LH_EL_IMAGE_H

View file

@ -3,18 +3,16 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_li : public html_tag {
class el_li : public html_tag public:
{ explicit el_li(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_li(const std::shared_ptr<litehtml::document>& doc);
int render(int x, int y, int max_width, bool second_pass = false) override; int render(int x, int y, int max_width, bool second_pass = false) override;
private: private:
bool m_index_initialized = false; bool m_index_initialized = false;
}; };
} } // namespace litehtml
#endif // LH_EL_LI_H #endif // LH_EL_LI_H

View file

@ -3,16 +3,14 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_link : public html_tag {
class el_link : public html_tag public:
{ explicit el_link(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_link(const std::shared_ptr<litehtml::document>& doc);
protected: protected:
void parse_attributes() override; void parse_attributes() override;
}; };
} } // namespace litehtml
#endif // LH_EL_LINK_H #endif // LH_EL_LINK_H

View file

@ -3,16 +3,13 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_para : public html_tag {
class el_para : public html_tag public:
{ explicit el_para(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_para(const std::shared_ptr<litehtml::document>& doc);
void parse_attributes() override; void parse_attributes() override;
};
}; } // namespace litehtml
}
#endif // LH_EL_PARA_H #endif // LH_EL_PARA_H

View file

@ -3,18 +3,17 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_script : public element {
class el_script : public element tstring m_text;
{
tstring m_text;
public:
explicit el_script(const std::shared_ptr<litehtml::document>& doc);
void parse_attributes() override; public:
bool appendChild(const ptr &el) override; explicit el_script(const std::shared_ptr<litehtml::document>& doc);
const tchar_t* get_tagName() const override;
}; void parse_attributes() override;
} bool appendChild(const ptr& el) override;
const tchar_t* get_tagName() const override;
};
} // namespace litehtml
#endif // LH_EL_SCRIPT_H #endif // LH_EL_SCRIPT_H

View file

@ -1,19 +1,17 @@
#ifndef LH_EL_SPACE_H #ifndef LH_EL_SPACE_H
#define LH_EL_SPACE_H #define LH_EL_SPACE_H
#include "html_tag.h"
#include "el_text.h" #include "el_text.h"
#include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_space : public el_text {
class el_space : public el_text public:
{ el_space(const tchar_t* text, const std::shared_ptr<litehtml::document>& doc);
public:
el_space(const tchar_t* text, const std::shared_ptr<litehtml::document>& doc);
bool is_white_space() const override; bool is_white_space() const override;
bool is_break() const override; bool is_break() const override;
}; };
} } // namespace litehtml
#endif // LH_EL_SPACE_H #endif // LH_EL_SPACE_H

View file

@ -3,18 +3,17 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_style : public element {
class el_style : public element elements_vector m_children;
{
elements_vector m_children;
public:
explicit el_style(const std::shared_ptr<litehtml::document>& doc);
void parse_attributes() override; public:
bool appendChild(const ptr &el) override; explicit el_style(const std::shared_ptr<litehtml::document>& doc);
const tchar_t* get_tagName() const override;
}; void parse_attributes() override;
} bool appendChild(const ptr& el) override;
const tchar_t* get_tagName() const override;
};
} // namespace litehtml
#endif // LH_EL_STYLE_H #endif // LH_EL_STYLE_H

View file

@ -3,24 +3,20 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ struct col_info {
struct col_info int width;
{ bool is_auto;
int width; };
bool is_auto;
};
class el_table : public html_tag {
public:
explicit el_table(const std::shared_ptr<litehtml::document>& doc);
class el_table : public html_tag bool appendChild(const litehtml::element::ptr& el) override;
{ void parse_styles(bool is_reparse = false) override;
public: void parse_attributes() override;
explicit el_table(const std::shared_ptr<litehtml::document>& doc); };
} // namespace litehtml
bool appendChild(const litehtml::element::ptr& el) override;
void parse_styles(bool is_reparse = false) override;
void parse_attributes() override;
};
}
#endif // LH_EL_TABLE_H #endif // LH_EL_TABLE_H

View file

@ -3,15 +3,13 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_td : public html_tag {
class el_td : public html_tag public:
{ explicit el_td(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_td(const std::shared_ptr<litehtml::document>& doc);
void parse_attributes() override; void parse_attributes() override;
}; };
} } // namespace litehtml
#endif // LH_EL_TD_H #endif // LH_EL_TD_H

View file

@ -3,35 +3,34 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_text : public element {
class el_text : public element protected:
{ tstring m_text;
protected: tstring m_transformed_text;
tstring m_text; size m_size;
tstring m_transformed_text; text_transform m_text_transform;
size m_size; bool m_use_transformed;
text_transform m_text_transform; bool m_draw_spaces;
bool m_use_transformed;
bool m_draw_spaces;
public:
el_text(const tchar_t* text, const std::shared_ptr<litehtml::document>& doc);
void get_text(tstring& text) override; public:
const tchar_t* get_style_property(const tchar_t* name, bool inherited, const tchar_t* def = nullptr) override; el_text(const tchar_t* text, const std::shared_ptr<litehtml::document>& doc);
void parse_styles(bool is_reparse) override;
int get_base_line() override;
void draw(uint_ptr hdc, int x, int y, const position* clip) override;
int line_height() const override;
uint_ptr get_font(font_metrics* fm = nullptr) override;
style_display get_display() const override;
white_space get_white_space() const override;
element_position get_element_position(css_offsets* offsets = nullptr) const override;
css_offsets get_css_offsets() const override;
protected: void get_text(tstring& text) override;
void get_content_size(size& sz, int max_width) override; const tchar_t* get_style_property(const tchar_t* name, bool inherited, const tchar_t* def = nullptr) override;
}; void parse_styles(bool is_reparse) override;
} int get_base_line() override;
void draw(uint_ptr hdc, int x, int y, const position* clip) override;
int line_height() const override;
uint_ptr get_font(font_metrics* fm = nullptr) override;
style_display get_display() const override;
white_space get_white_space() const override;
element_position get_element_position(css_offsets* offsets = nullptr) const override;
css_offsets get_css_offsets() const override;
protected:
void get_content_size(size& sz, int max_width) override;
};
} // namespace litehtml
#endif // LH_EL_TEXT_H #endif // LH_EL_TEXT_H

View file

@ -3,16 +3,14 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_title : public html_tag {
class el_title : public html_tag public:
{ explicit el_title(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_title(const std::shared_ptr<litehtml::document>& doc);
protected: protected:
void parse_attributes() override; void parse_attributes() override;
}; };
} } // namespace litehtml
#endif // LH_EL_TITLE_H #endif // LH_EL_TITLE_H

View file

@ -3,16 +3,14 @@
#include "html_tag.h" #include "html_tag.h"
namespace litehtml namespace litehtml {
{ class el_tr : public html_tag {
class el_tr : public html_tag public:
{ explicit el_tr(const std::shared_ptr<litehtml::document>& doc);
public:
explicit el_tr(const std::shared_ptr<litehtml::document>& doc);
void parse_attributes() override; void parse_attributes() override;
void get_inline_boxes(position::vector& boxes) override; void get_inline_boxes(position::vector& boxes) override;
}; };
} } // namespace litehtml
#endif // LH_EL_TR_H #endif // LH_EL_TR_H

View file

@ -2,403 +2,297 @@
#define LH_ELEMENT_H #define LH_ELEMENT_H
#include <memory> #include <memory>
#include "stylesheet.h"
#include "css_offsets.h" #include "css_offsets.h"
#include "stylesheet.h"
namespace litehtml namespace litehtml {
{ class box;
class box;
class element : public std::enable_shared_from_this<element> class element : public std::enable_shared_from_this<element> {
{ friend class block_box;
friend class block_box; friend class line_box;
friend class line_box; friend class html_tag;
friend class html_tag; friend class el_table;
friend class el_table; friend class document;
friend class document;
public:
typedef std::shared_ptr<litehtml::element> ptr;
typedef std::shared_ptr<const litehtml::element> const_ptr;
typedef std::weak_ptr<litehtml::element> weak_ptr;
protected:
std::weak_ptr<element> m_parent;
std::weak_ptr<litehtml::document> m_doc;
litehtml::box* m_box;
elements_vector m_children;
position m_pos;
margins m_margins;
margins m_padding;
margins m_borders;
bool m_skip;
virtual void select_all(const css_selector& selector, elements_vector& res);
public:
explicit element(const std::shared_ptr<litehtml::document>& doc);
virtual ~element() = default;
// returns refer to m_pos member; public:
position& get_position(); typedef std::shared_ptr<litehtml::element> ptr;
typedef std::shared_ptr<const litehtml::element> const_ptr;
typedef std::weak_ptr<litehtml::element> weak_ptr;
int left() const; protected:
int right() const; std::weak_ptr<element> m_parent;
int top() const; std::weak_ptr<litehtml::document> m_doc;
int bottom() const; litehtml::box* m_box;
int height() const; elements_vector m_children;
int width() const; position m_pos;
margins m_margins;
margins m_padding;
margins m_borders;
bool m_skip;
int content_margins_top() const; virtual void select_all(const css_selector& selector, elements_vector& res);
int content_margins_bottom() const;
int content_margins_left() const;
int content_margins_right() const;
int content_margins_width() const;
int content_margins_height() const;
int margin_top() const; public:
int margin_bottom() const; explicit element(const std::shared_ptr<litehtml::document>& doc);
int margin_left() const; virtual ~element() = default;
int margin_right() const;
margins get_margins() const;
int padding_top() const; // returns refer to m_pos member;
int padding_bottom() const; position& get_position();
int padding_left() const;
int padding_right() const;
margins get_paddings() const;
int border_top() const; int left() const;
int border_bottom() const; int right() const;
int border_left() const; int top() const;
int border_right() const; int bottom() const;
margins get_borders() const; int height() const;
int width() const;
bool in_normal_flow() const; int content_margins_top() const;
litehtml::web_color get_color(const tchar_t* prop_name, bool inherited, const litehtml::web_color& def_color = litehtml::web_color()); int content_margins_bottom() const;
bool is_inline_box() const; int content_margins_left() const;
position get_placement() const; int content_margins_right() const;
bool collapse_top_margin() const; int content_margins_width() const;
bool collapse_bottom_margin() const; int content_margins_height() const;
bool is_positioned() const;
bool skip() const; int margin_top() const;
void skip(bool val); int margin_bottom() const;
bool have_parent() const; int margin_left() const;
element::ptr parent() const; int margin_right() const;
void parent(const element::ptr& par); margins get_margins() const;
bool is_visible() const;
int calc_width(int defVal) const;
int get_inline_shift_left();
int get_inline_shift_right();
void apply_relative_shift(int parent_width);
std::shared_ptr<document> get_document() const; int padding_top() const;
int padding_bottom() const;
int padding_left() const;
int padding_right() const;
margins get_paddings() const;
virtual elements_vector select_all(const tstring& selector); int border_top() const;
virtual elements_vector select_all(const css_selector& selector); int border_bottom() const;
int border_left() const;
int border_right() const;
margins get_borders() const;
virtual element::ptr select_one(const tstring& selector); bool in_normal_flow() const;
virtual element::ptr select_one(const css_selector& selector); litehtml::web_color get_color(const tchar_t* prop_name, bool inherited, const litehtml::web_color& def_color = litehtml::web_color());
bool is_inline_box() const;
position get_placement() const;
bool collapse_top_margin() const;
bool collapse_bottom_margin() const;
bool is_positioned() const;
virtual int render(int x, int y, int max_width, bool second_pass = false); bool skip() const;
virtual int render_inline(const ptr &container, int max_width); void skip(bool val);
virtual int place_element(const ptr &el, int max_width); bool have_parent() const;
virtual void calc_outlines( int parent_width ); element::ptr parent() const;
virtual void calc_auto_margins(int parent_width); void parent(const element::ptr& par);
virtual void apply_vertical_align(); bool is_visible() const;
virtual bool fetch_positioned(); int calc_width(int defVal) const;
virtual void render_positioned(render_type rt = render_all); int get_inline_shift_left();
int get_inline_shift_right();
void apply_relative_shift(int parent_width);
virtual bool appendChild(const ptr &el); std::shared_ptr<document> get_document() const;
virtual bool removeChild(const ptr &el);
virtual void clearRecursive();
virtual const tchar_t* get_tagName() const; virtual elements_vector select_all(const tstring& selector);
virtual void set_tagName(const tchar_t* tag); virtual elements_vector select_all(const css_selector& selector);
virtual void set_data(const tchar_t* data);
virtual element_float get_float() const;
virtual vertical_align get_vertical_align() const;
virtual element_clear get_clear() const;
virtual size_t get_children_count() const;
virtual element::ptr get_child(int idx) const;
virtual overflow get_overflow() const;
virtual css_length get_css_left() const; virtual element::ptr select_one(const tstring& selector);
virtual css_length get_css_right() const; virtual element::ptr select_one(const css_selector& selector);
virtual css_length get_css_top() const;
virtual css_length get_css_bottom() const;
virtual css_offsets get_css_offsets() const;
virtual css_length get_css_width() const;
virtual void set_css_width(css_length& w);
virtual css_length get_css_height() const;
virtual void set_attr(const tchar_t* name, const tchar_t* val); virtual int render(int x, int y, int max_width, bool second_pass = false);
virtual const tchar_t* get_attr(const tchar_t* name, const tchar_t* def = nullptr) const; virtual int render_inline(const ptr& container, int max_width);
virtual void apply_stylesheet(const litehtml::css& stylesheet); virtual int place_element(const ptr& el, int max_width);
virtual void refresh_styles(); virtual void calc_outlines(int parent_width);
virtual bool is_white_space() const; virtual void calc_auto_margins(int parent_width);
virtual bool is_body() const; virtual void apply_vertical_align();
virtual bool is_break() const; virtual bool fetch_positioned();
virtual int get_base_line(); virtual void render_positioned(render_type rt = render_all);
virtual bool on_mouse_over();
virtual bool on_mouse_leave();
virtual bool on_lbutton_down();
virtual bool on_lbutton_up();
virtual void on_click();
virtual bool find_styles_changes(position::vector& redraw_boxes, int x, int y);
virtual const tchar_t* get_cursor();
virtual void init_font();
virtual bool is_point_inside(int x, int y);
virtual bool set_pseudo_class(const tchar_t* pclass, bool add);
virtual bool set_class(const tchar_t* pclass, bool add);
virtual bool is_replaced() const;
virtual int line_height() const;
virtual white_space get_white_space() const;
virtual style_display get_display() const;
virtual visibility get_visibility() const;
virtual element_position get_element_position(css_offsets* offsets = nullptr) const;
virtual void get_inline_boxes(position::vector& boxes);
virtual void parse_styles(bool is_reparse = false);
virtual void draw(uint_ptr hdc, int x, int y, const position* clip);
virtual void draw_background( uint_ptr hdc, int x, int y, const position* clip );
virtual const tchar_t* get_style_property(const tchar_t* name, bool inherited, const tchar_t* def = nullptr);
virtual uint_ptr get_font(font_metrics* fm = nullptr);
virtual int get_font_size() const;
virtual void get_text(tstring& text);
virtual void parse_attributes();
virtual int select(const css_selector& selector, bool apply_pseudo = true);
virtual int select(const css_element_selector& selector, bool apply_pseudo = true);
virtual element::ptr find_ancestor(const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr);
virtual bool is_ancestor(const ptr &el) const;
virtual element::ptr find_adjacent_sibling(const element::ptr& el, const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr);
virtual element::ptr find_sibling(const element::ptr& el, const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr);
virtual bool is_first_child_inline(const element::ptr& el) const;
virtual bool is_last_child_inline(const element::ptr& el);
virtual bool have_inline_child() const;
virtual void get_content_size(size& sz, int max_width);
virtual void init();
virtual bool is_floats_holder() const;
virtual int get_floats_height(element_float el_float = float_none) const;
virtual int get_left_floats_height() const;
virtual int get_right_floats_height() const;
virtual int get_line_left(int y);
virtual int get_line_right(int y, int def_right);
virtual void get_line_left_right(int y, int def_right, int& ln_left, int& ln_right);
virtual void add_float(const ptr &el, int x, int y);
virtual void update_floats(int dy, const ptr &parent);
virtual void add_positioned(const ptr &el);
virtual int find_next_line_top(int top, int width, int def_right);
virtual int get_zindex() const;
virtual void draw_stacking_context(uint_ptr hdc, int x, int y, const position* clip, bool with_positioned);
virtual void draw_children( uint_ptr hdc, int x, int y, const position* clip, draw_flag flag, int zindex );
virtual bool is_nth_child(const element::ptr& el, int num, int off, bool of_type) const;
virtual bool is_nth_last_child(const element::ptr& el, int num, int off, bool of_type) const;
virtual bool is_only_child(const element::ptr& el, bool of_type) const;
virtual bool get_predefined_height(int& p_height) const;
virtual void calc_document_size(litehtml::size& sz, int x = 0, int y = 0);
virtual void get_redraw_box(litehtml::position& pos, int x = 0, int y = 0);
virtual void add_style(const litehtml::style& st);
virtual element::ptr get_element_by_point(int x, int y, int client_x, int client_y);
virtual element::ptr get_child_by_point(int x, int y, int client_x, int client_y, draw_flag flag, int zindex);
virtual const background* get_background(bool own_only = false);
};
////////////////////////////////////////////////////////////////////////// virtual bool appendChild(const ptr& el);
// INLINE FUNCTIONS // virtual bool removeChild(const ptr& el);
////////////////////////////////////////////////////////////////////////// virtual void clearRecursive();
inline int litehtml::element::right() const virtual const tchar_t* get_tagName() const;
{ virtual void set_tagName(const tchar_t* tag);
return left() + width(); virtual void set_data(const tchar_t* data);
} virtual element_float get_float() const;
virtual vertical_align get_vertical_align() const;
virtual element_clear get_clear() const;
virtual size_t get_children_count() const;
virtual element::ptr get_child(int idx) const;
virtual overflow get_overflow() const;
inline int litehtml::element::left() const virtual css_length get_css_left() const;
{ virtual css_length get_css_right() const;
return m_pos.left() - margin_left() - m_padding.left - m_borders.left; virtual css_length get_css_top() const;
} virtual css_length get_css_bottom() const;
virtual css_offsets get_css_offsets() const;
virtual css_length get_css_width() const;
virtual void set_css_width(css_length& w);
virtual css_length get_css_height() const;
inline int litehtml::element::top() const virtual void set_attr(const tchar_t* name, const tchar_t* val);
{ virtual const tchar_t* get_attr(const tchar_t* name, const tchar_t* def = nullptr) const;
return m_pos.top() - margin_top() - m_padding.top - m_borders.top; virtual void apply_stylesheet(const litehtml::css& stylesheet);
} virtual void refresh_styles();
virtual bool is_white_space() const;
virtual bool is_body() const;
virtual bool is_break() const;
virtual int get_base_line();
virtual bool on_mouse_over();
virtual bool on_mouse_leave();
virtual bool on_lbutton_down();
virtual bool on_lbutton_up();
virtual void on_click();
virtual bool find_styles_changes(position::vector& redraw_boxes, int x, int y);
virtual const tchar_t* get_cursor();
virtual void init_font();
virtual bool is_point_inside(int x, int y);
virtual bool set_pseudo_class(const tchar_t* pclass, bool add);
virtual bool set_class(const tchar_t* pclass, bool add);
virtual bool is_replaced() const;
virtual int line_height() const;
virtual white_space get_white_space() const;
virtual style_display get_display() const;
virtual visibility get_visibility() const;
virtual element_position get_element_position(css_offsets* offsets = nullptr) const;
virtual void get_inline_boxes(position::vector& boxes);
virtual void parse_styles(bool is_reparse = false);
virtual void draw(uint_ptr hdc, int x, int y, const position* clip);
virtual void draw_background(uint_ptr hdc, int x, int y, const position* clip);
virtual const tchar_t* get_style_property(const tchar_t* name, bool inherited, const tchar_t* def = nullptr);
virtual uint_ptr get_font(font_metrics* fm = nullptr);
virtual int get_font_size() const;
virtual void get_text(tstring& text);
virtual void parse_attributes();
virtual int select(const css_selector& selector, bool apply_pseudo = true);
virtual int select(const css_element_selector& selector, bool apply_pseudo = true);
virtual element::ptr find_ancestor(const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr);
virtual bool is_ancestor(const ptr& el) const;
virtual element::ptr find_adjacent_sibling(const element::ptr& el, const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr);
virtual element::ptr find_sibling(const element::ptr& el, const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr);
virtual bool is_first_child_inline(const element::ptr& el) const;
virtual bool is_last_child_inline(const element::ptr& el);
virtual bool have_inline_child() const;
virtual void get_content_size(size& sz, int max_width);
virtual void init();
virtual bool is_floats_holder() const;
virtual int get_floats_height(element_float el_float = float_none) const;
virtual int get_left_floats_height() const;
virtual int get_right_floats_height() const;
virtual int get_line_left(int y);
virtual int get_line_right(int y, int def_right);
virtual void get_line_left_right(int y, int def_right, int& ln_left, int& ln_right);
virtual void add_float(const ptr& el, int x, int y);
virtual void update_floats(int dy, const ptr& parent);
virtual void add_positioned(const ptr& el);
virtual int find_next_line_top(int top, int width, int def_right);
virtual int get_zindex() const;
virtual void draw_stacking_context(uint_ptr hdc, int x, int y, const position* clip, bool with_positioned);
virtual void draw_children(uint_ptr hdc, int x, int y, const position* clip, draw_flag flag, int zindex);
virtual bool is_nth_child(const element::ptr& el, int num, int off, bool of_type) const;
virtual bool is_nth_last_child(const element::ptr& el, int num, int off, bool of_type) const;
virtual bool is_only_child(const element::ptr& el, bool of_type) const;
virtual bool get_predefined_height(int& p_height) const;
virtual void calc_document_size(litehtml::size& sz, int x = 0, int y = 0);
virtual void get_redraw_box(litehtml::position& pos, int x = 0, int y = 0);
virtual void add_style(const litehtml::style& st);
virtual element::ptr get_element_by_point(int x, int y, int client_x, int client_y);
virtual element::ptr get_child_by_point(int x, int y, int client_x, int client_y, draw_flag flag, int zindex);
virtual const background* get_background(bool own_only = false);
};
inline int litehtml::element::bottom() const //////////////////////////////////////////////////////////////////////////
{ // INLINE FUNCTIONS //
return top() + height(); //////////////////////////////////////////////////////////////////////////
}
inline int litehtml::element::height() const inline int litehtml::element::right() const { return left() + width(); }
{
return m_pos.height + margin_top() + margin_bottom() + m_padding.height() + m_borders.height();
}
inline int litehtml::element::width() const inline int litehtml::element::left() const { return m_pos.left() - margin_left() - m_padding.left - m_borders.left; }
{
return m_pos.width + margin_left() + margin_right() + m_padding.width() + m_borders.width();
}
inline int litehtml::element::content_margins_top() const inline int litehtml::element::top() const { return m_pos.top() - margin_top() - m_padding.top - m_borders.top; }
{
return margin_top() + m_padding.top + m_borders.top;
}
inline int litehtml::element::content_margins_bottom() const inline int litehtml::element::bottom() const { return top() + height(); }
{
return margin_bottom() + m_padding.bottom + m_borders.bottom;
}
inline int litehtml::element::content_margins_left() const inline int litehtml::element::height() const { return m_pos.height + margin_top() + margin_bottom() + m_padding.height() + m_borders.height(); }
{
return margin_left() + m_padding.left + m_borders.left;
}
inline int litehtml::element::content_margins_right() const inline int litehtml::element::width() const { return m_pos.width + margin_left() + margin_right() + m_padding.width() + m_borders.width(); }
{
return margin_right() + m_padding.right + m_borders.right;
}
inline int litehtml::element::content_margins_width() const inline int litehtml::element::content_margins_top() const { return margin_top() + m_padding.top + m_borders.top; }
{
return content_margins_left() + content_margins_right();
}
inline int litehtml::element::content_margins_height() const inline int litehtml::element::content_margins_bottom() const { return margin_bottom() + m_padding.bottom + m_borders.bottom; }
{
return content_margins_top() + content_margins_bottom();
}
inline litehtml::margins litehtml::element::get_paddings() const inline int litehtml::element::content_margins_left() const { return margin_left() + m_padding.left + m_borders.left; }
{
return m_padding;
}
inline litehtml::margins litehtml::element::get_borders() const inline int litehtml::element::content_margins_right() const { return margin_right() + m_padding.right + m_borders.right; }
{
return m_borders;
}
inline int litehtml::element::padding_top() const inline int litehtml::element::content_margins_width() const { return content_margins_left() + content_margins_right(); }
{
return m_padding.top;
}
inline int litehtml::element::padding_bottom() const inline int litehtml::element::content_margins_height() const { return content_margins_top() + content_margins_bottom(); }
{
return m_padding.bottom;
}
inline int litehtml::element::padding_left() const inline litehtml::margins litehtml::element::get_paddings() const { return m_padding; }
{
return m_padding.left;
}
inline int litehtml::element::padding_right() const inline litehtml::margins litehtml::element::get_borders() const { return m_borders; }
{
return m_padding.right;
}
inline bool litehtml::element::in_normal_flow() const inline int litehtml::element::padding_top() const { return m_padding.top; }
{
if(get_element_position() != element_position_absolute && get_display() != display_none)
{
return true;
}
return false;
}
inline int litehtml::element::border_top() const inline int litehtml::element::padding_bottom() const { return m_padding.bottom; }
{
return m_borders.top;
}
inline int litehtml::element::border_bottom() const inline int litehtml::element::padding_left() const { return m_padding.left; }
{
return m_borders.bottom;
}
inline int litehtml::element::border_left() const inline int litehtml::element::padding_right() const { return m_padding.right; }
{
return m_borders.left;
}
inline int litehtml::element::border_right() const inline bool litehtml::element::in_normal_flow() const {
{ if (get_element_position() != element_position_absolute && get_display() != display_none) {
return m_borders.right; return true;
} }
return false;
inline bool litehtml::element::skip() const
{
return m_skip;
}
inline void litehtml::element::skip(bool val)
{
m_skip = val;
}
inline bool litehtml::element::have_parent() const
{
return !m_parent.expired();
}
inline element::ptr litehtml::element::parent() const
{
return m_parent.lock();
}
inline void litehtml::element::parent(const element::ptr& par)
{
m_parent = par;
}
inline int litehtml::element::margin_top() const
{
return m_margins.top;
}
inline int litehtml::element::margin_bottom() const
{
return m_margins.bottom;
}
inline int litehtml::element::margin_left() const
{
return m_margins.left;
}
inline int litehtml::element::margin_right() const
{
return m_margins.right;
}
inline litehtml::margins litehtml::element::get_margins() const
{
margins ret;
ret.left = margin_left();
ret.right = margin_right();
ret.top = margin_top();
ret.bottom = margin_bottom();
return ret;
}
inline bool litehtml::element::is_positioned() const
{
return (get_element_position() > element_position_static);
}
inline bool litehtml::element::is_visible() const
{
return !(m_skip || get_display() == display_none || get_visibility() != visibility_visible);
}
inline position& litehtml::element::get_position()
{
return m_pos;
}
inline std::shared_ptr<document> element::get_document() const
{
return m_doc.lock();
}
} }
inline int litehtml::element::border_top() const { return m_borders.top; }
inline int litehtml::element::border_bottom() const { return m_borders.bottom; }
inline int litehtml::element::border_left() const { return m_borders.left; }
inline int litehtml::element::border_right() const { return m_borders.right; }
inline bool litehtml::element::skip() const { return m_skip; }
inline void litehtml::element::skip(bool val) { m_skip = val; }
inline bool litehtml::element::have_parent() const { return !m_parent.expired(); }
inline element::ptr litehtml::element::parent() const { return m_parent.lock(); }
inline void litehtml::element::parent(const element::ptr& par) { m_parent = par; }
inline int litehtml::element::margin_top() const { return m_margins.top; }
inline int litehtml::element::margin_bottom() const { return m_margins.bottom; }
inline int litehtml::element::margin_left() const { return m_margins.left; }
inline int litehtml::element::margin_right() const { return m_margins.right; }
inline litehtml::margins litehtml::element::get_margins() const {
margins ret;
ret.left = margin_left();
ret.right = margin_right();
ret.top = margin_top();
ret.bottom = margin_bottom();
return ret;
}
inline bool litehtml::element::is_positioned() const { return (get_element_position() > element_position_static); }
inline bool litehtml::element::is_visible() const { return !(m_skip || get_display() == display_none || get_visibility() != visibility_visible); }
inline position& litehtml::element::get_position() { return m_pos; }
inline std::shared_ptr<document> element::get_document() const { return m_doc.lock(); }
} // namespace litehtml
#endif // LH_ELEMENT_H #endif // LH_ELEMENT_H

View file

@ -1,117 +1,104 @@
#ifndef LH_HTML_H #ifndef LH_HTML_H
#define LH_HTML_H #define LH_HTML_H
#include <stdlib.h>
#include <string>
#include <ctype.h> #include <ctype.h>
#include <vector> #include <stdlib.h>
#include <map>
#include <cstring>
#include <algorithm> #include <algorithm>
#include <sstream> #include <cstring>
#include <functional> #include <functional>
#include "os_types.h" #include <map>
#include "types.h" #include <sstream>
#include <string>
#include <vector>
#include "background.h" #include "background.h"
#include "borders.h" #include "borders.h"
#include "html_tag.h" #include "html_tag.h"
#include "web_color.h"
#include "media_query.h" #include "media_query.h"
#include "os_types.h"
#include "types.h"
#include "web_color.h"
namespace litehtml namespace litehtml {
{ struct list_marker {
struct list_marker tstring image;
{ const tchar_t* baseurl;
tstring image; list_style_type marker_type;
const tchar_t* baseurl; web_color color;
list_style_type marker_type; position pos;
web_color color; int index;
position pos; uint_ptr font;
int index; };
uint_ptr font;
};
// call back interface to draw text, images and other elements // call back interface to draw text, images and other elements
class document_container class document_container {
{ public:
public: virtual litehtml::uint_ptr create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) = 0;
virtual litehtml::uint_ptr create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) = 0; virtual void delete_font(litehtml::uint_ptr hFont) = 0;
virtual void delete_font(litehtml::uint_ptr hFont) = 0; virtual int text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) = 0;
virtual int text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) = 0; virtual void draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) = 0;
virtual void draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) = 0; virtual int pt_to_px(int pt) const = 0;
virtual int pt_to_px(int pt) const = 0; virtual int get_default_font_size() const = 0;
virtual int get_default_font_size() const = 0; virtual const litehtml::tchar_t* get_default_font_name() const = 0;
virtual const litehtml::tchar_t* get_default_font_name() const = 0; virtual void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) = 0;
virtual void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) = 0; virtual void load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) = 0;
virtual void load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) = 0; virtual void get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) = 0;
virtual void get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) = 0; virtual void draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) = 0;
virtual void draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) = 0; virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) = 0;
virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) = 0;
virtual void set_caption(const litehtml::tchar_t* caption) = 0; virtual void set_caption(const litehtml::tchar_t* caption) = 0;
virtual void set_base_url(const litehtml::tchar_t* base_url) = 0; virtual void set_base_url(const litehtml::tchar_t* base_url) = 0;
virtual void link(const std::shared_ptr<litehtml::document>& doc, const litehtml::element::ptr& el) = 0; virtual void link(const std::shared_ptr<litehtml::document>& doc, const litehtml::element::ptr& el) = 0;
virtual void on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el) = 0; virtual void on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el) = 0;
virtual void set_cursor(const litehtml::tchar_t* cursor) = 0; virtual void set_cursor(const litehtml::tchar_t* cursor) = 0;
virtual void transform_text(litehtml::tstring& text, litehtml::text_transform tt) = 0; virtual void transform_text(litehtml::tstring& text, litehtml::text_transform tt) = 0;
virtual void import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl) = 0; virtual void import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl) = 0;
virtual void set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) = 0; virtual void set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) = 0;
virtual void del_clip() = 0; virtual void del_clip() = 0;
virtual void get_client_rect(litehtml::position& client) const = 0; virtual void get_client_rect(litehtml::position& client) const = 0;
virtual std::shared_ptr<litehtml::element> create_element(const litehtml::tchar_t *tag_name, virtual std::shared_ptr<litehtml::element> create_element(const litehtml::tchar_t* tag_name, const litehtml::string_map& attributes, const std::shared_ptr<litehtml::document>& doc) = 0;
const litehtml::string_map &attributes,
const std::shared_ptr<litehtml::document> &doc) = 0;
virtual void get_media_features(litehtml::media_features& media) const = 0; virtual void get_media_features(litehtml::media_features& media) const = 0;
virtual void get_language(litehtml::tstring& language, litehtml::tstring & culture) const = 0; virtual void get_language(litehtml::tstring& language, litehtml::tstring& culture) const = 0;
virtual litehtml::tstring resolve_color(const litehtml::tstring& /*color*/) const { return litehtml::tstring(); } virtual litehtml::tstring resolve_color(const litehtml::tstring& /*color*/) const { return litehtml::tstring(); }
virtual void split_text(const char* text, const std::function<void(const tchar_t*)>& on_word, const std::function<void(const tchar_t*)>& on_space); virtual void split_text(const char* text, const std::function<void(const tchar_t*)>& on_word, const std::function<void(const tchar_t*)>& on_space);
protected: protected:
~document_container() = default; ~document_container() = default;
}; };
void trim(tstring &s); void trim(tstring& s);
void lcase(tstring &s); void lcase(tstring& s);
int value_index(const tstring& val, const tstring& strings, int defValue = -1, tchar_t delim = _t(';')); int value_index(const tstring& val, const tstring& strings, int defValue = -1, tchar_t delim = _t(';'));
bool value_in_list(const tstring& val, const tstring& strings, tchar_t delim = _t(';')); bool value_in_list(const tstring& val, const tstring& strings, tchar_t delim = _t(';'));
tstring::size_type find_close_bracket(const tstring &s, tstring::size_type off, tchar_t open_b = _t('('), tchar_t close_b = _t(')')); tstring::size_type find_close_bracket(const tstring& s, tstring::size_type off, tchar_t open_b = _t('('), tchar_t close_b = _t(')'));
void split_string(const tstring& str, string_vector& tokens, const tstring& delims, const tstring& delims_preserve = _t(""), const tstring& quote = _t("\"")); void split_string(const tstring& str, string_vector& tokens, const tstring& delims, const tstring& delims_preserve = _t(""), const tstring& quote = _t("\""));
void join_string(tstring& str, const string_vector& tokens, const tstring& delims); void join_string(tstring& str, const string_vector& tokens, const tstring& delims);
double t_strtod(const tchar_t* string, tchar_t** endPtr); double t_strtod(const tchar_t* string, tchar_t** endPtr);
int t_strcasecmp(const tchar_t *s1, const tchar_t *s2); int t_strcasecmp(const tchar_t* s1, const tchar_t* s2);
int t_strncasecmp(const tchar_t *s1, const tchar_t *s2, size_t n); int t_strncasecmp(const tchar_t* s1, const tchar_t* s2, size_t n);
inline int t_isdigit(int c)
{
return (c >= '0' && c <= '9');
}
inline int t_tolower(int c)
{
return (c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c);
}
inline int round_f(float val)
{
int int_val = (int) val;
if(val - int_val >= 0.5)
{
int_val++;
}
return int_val;
}
inline int round_d(double val) inline int t_isdigit(int c) { return (c >= '0' && c <= '9'); }
{
int int_val = (int) val; inline int t_tolower(int c) { return (c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c); }
if(val - int_val >= 0.5)
{ inline int round_f(float val) {
int_val++; int int_val = (int)val;
} if (val - int_val >= 0.5) {
return int_val; int_val++;
} }
return int_val;
} }
inline int round_d(double val) {
int int_val = (int)val;
if (val - int_val >= 0.5) {
int_val++;
}
return int_val;
}
} // namespace litehtml
#endif // LH_HTML_H #endif // LH_HTML_H

View file

@ -1,248 +1,238 @@
#ifndef LH_HTML_TAG_H #ifndef LH_HTML_TAG_H
#define LH_HTML_TAG_H #define LH_HTML_TAG_H
#include "background.h"
#include "borders.h"
#include "box.h"
#include "css_margins.h"
#include "css_selector.h"
#include "element.h" #include "element.h"
#include "style.h" #include "style.h"
#include "background.h"
#include "css_margins.h"
#include "borders.h"
#include "css_selector.h"
#include "stylesheet.h" #include "stylesheet.h"
#include "box.h"
#include "table.h" #include "table.h"
namespace litehtml namespace litehtml {
{ struct line_context {
struct line_context int calculatedTop;
{ int top;
int calculatedTop; int left;
int top; int right;
int left;
int right;
int width() const int width() const { return right - left; }
{ void fix_top() { calculatedTop = top; }
return right - left; };
}
void fix_top()
{
calculatedTop = top;
}
};
class html_tag : public element class html_tag : public element {
{ friend class elements_iterator;
friend class elements_iterator; friend class el_table;
friend class el_table; friend class table_grid;
friend class table_grid; friend class block_box;
friend class block_box; friend class line_box;
friend class line_box;
public:
typedef std::shared_ptr<litehtml::html_tag> ptr;
protected:
box::vector m_boxes;
string_vector m_class_values;
tstring m_tag;
litehtml::style m_style;
string_map m_attrs;
vertical_align m_vertical_align;
text_align m_text_align;
style_display m_display;
list_style_type m_list_style_type;
list_style_position m_list_style_position;
white_space m_white_space;
element_float m_float;
element_clear m_clear;
floated_box::vector m_floats_left;
floated_box::vector m_floats_right;
elements_vector m_positioned;
background m_bg;
element_position m_el_position;
int m_line_height;
bool m_lh_predefined;
string_vector m_pseudo_classes;
used_selector::vector m_used_styles;
uint_ptr m_font;
int m_font_size;
font_metrics m_font_metrics;
css_margins m_css_margins; public:
css_margins m_css_padding; typedef std::shared_ptr<litehtml::html_tag> ptr;
css_borders m_css_borders;
css_length m_css_width;
css_length m_css_height;
css_length m_css_min_width;
css_length m_css_min_height;
css_length m_css_max_width;
css_length m_css_max_height;
css_offsets m_css_offsets;
css_length m_css_text_indent;
overflow m_overflow; protected:
visibility m_visibility; box::vector m_boxes;
int m_z_index; string_vector m_class_values;
box_sizing m_box_sizing; tstring m_tag;
litehtml::style m_style;
string_map m_attrs;
vertical_align m_vertical_align;
text_align m_text_align;
style_display m_display;
list_style_type m_list_style_type;
list_style_position m_list_style_position;
white_space m_white_space;
element_float m_float;
element_clear m_clear;
floated_box::vector m_floats_left;
floated_box::vector m_floats_right;
elements_vector m_positioned;
background m_bg;
element_position m_el_position;
int m_line_height;
bool m_lh_predefined;
string_vector m_pseudo_classes;
used_selector::vector m_used_styles;
int_int_cache m_cahe_line_left; uint_ptr m_font;
int_int_cache m_cahe_line_right; int m_font_size;
font_metrics m_font_metrics;
// data for table rendering css_margins m_css_margins;
std::unique_ptr<table_grid> m_grid; css_margins m_css_padding;
css_length m_css_border_spacing_x; css_borders m_css_borders;
css_length m_css_border_spacing_y; css_length m_css_width;
int m_border_spacing_x; css_length m_css_height;
int m_border_spacing_y; css_length m_css_min_width;
border_collapse m_border_collapse; css_length m_css_min_height;
css_length m_css_max_width;
css_length m_css_max_height;
css_offsets m_css_offsets;
css_length m_css_text_indent;
void select_all(const css_selector& selector, elements_vector& res) override; overflow m_overflow;
visibility m_visibility;
int m_z_index;
box_sizing m_box_sizing;
public: int_int_cache m_cahe_line_left;
explicit html_tag(const std::shared_ptr<litehtml::document>& doc); int_int_cache m_cahe_line_right;
/* render functions */ // data for table rendering
std::unique_ptr<table_grid> m_grid;
css_length m_css_border_spacing_x;
css_length m_css_border_spacing_y;
int m_border_spacing_x;
int m_border_spacing_y;
border_collapse m_border_collapse;
int render(int x, int y, int max_width, bool second_pass = false) override; void select_all(const css_selector& selector, elements_vector& res) override;
int render_inline(const element::ptr &container, int max_width) override; public:
int place_element(const element::ptr &el, int max_width) override; explicit html_tag(const std::shared_ptr<litehtml::document>& doc);
bool fetch_positioned() override;
void render_positioned(render_type rt = render_all) override;
int new_box(const element::ptr &el, int max_width, line_context& line_ctx); /* render functions */
int get_cleared_top(const element::ptr &el, int line_top) const; int render(int x, int y, int max_width, bool second_pass = false) override;
int finish_last_box(bool end_of_render = false);
bool appendChild(const element::ptr &el) override; int render_inline(const element::ptr& container, int max_width) override;
bool removeChild(const element::ptr &el) override; int place_element(const element::ptr& el, int max_width) override;
void clearRecursive() override; bool fetch_positioned() override;
const tchar_t* get_tagName() const override; void render_positioned(render_type rt = render_all) override;
void set_tagName(const tchar_t* tag) override;
void set_data(const tchar_t* data) override;
element_float get_float() const override;
vertical_align get_vertical_align() const override;
css_length get_css_left() const override;
css_length get_css_right() const override;
css_length get_css_top() const override;
css_length get_css_bottom() const override;
css_length get_css_width() const override;
css_offsets get_css_offsets() const override;
void set_css_width(css_length& w) override;
css_length get_css_height() const override;
element_clear get_clear() const override;
size_t get_children_count() const override;
element::ptr get_child(int idx) const override;
element_position get_element_position(css_offsets* offsets = nullptr) const override;
overflow get_overflow() const override;
void set_attr(const tchar_t* name, const tchar_t* val) override; int new_box(const element::ptr& el, int max_width, line_context& line_ctx);
const tchar_t* get_attr(const tchar_t* name, const tchar_t* def = nullptr) const override;
void apply_stylesheet(const litehtml::css& stylesheet) override;
void refresh_styles() override;
bool is_white_space() const override; int get_cleared_top(const element::ptr& el, int line_top) const;
bool is_body() const override; int finish_last_box(bool end_of_render = false);
bool is_break() const override;
int get_base_line() override;
bool on_mouse_over() override;
bool on_mouse_leave() override;
bool on_lbutton_down() override;
bool on_lbutton_up() override;
void on_click() override;
bool find_styles_changes(position::vector& redraw_boxes, int x, int y) override;
const tchar_t* get_cursor() override;
void init_font() override;
bool set_pseudo_class(const tchar_t* pclass, bool add) override;
bool set_class(const tchar_t* pclass, bool add) override;
bool is_replaced() const override;
int line_height() const override;
white_space get_white_space() const override;
style_display get_display() const override;
visibility get_visibility() const override;
void parse_styles(bool is_reparse = false) override;
void draw(uint_ptr hdc, int x, int y, const position* clip) override;
void draw_background(uint_ptr hdc, int x, int y, const position* clip) override;
const tchar_t* get_style_property(const tchar_t* name, bool inherited, const tchar_t* def = nullptr) override; bool appendChild(const element::ptr& el) override;
uint_ptr get_font(font_metrics* fm = nullptr) override; bool removeChild(const element::ptr& el) override;
int get_font_size() const override; void clearRecursive() override;
const tchar_t* get_tagName() const override;
void set_tagName(const tchar_t* tag) override;
void set_data(const tchar_t* data) override;
element_float get_float() const override;
vertical_align get_vertical_align() const override;
css_length get_css_left() const override;
css_length get_css_right() const override;
css_length get_css_top() const override;
css_length get_css_bottom() const override;
css_length get_css_width() const override;
css_offsets get_css_offsets() const override;
void set_css_width(css_length& w) override;
css_length get_css_height() const override;
element_clear get_clear() const override;
size_t get_children_count() const override;
element::ptr get_child(int idx) const override;
element_position get_element_position(css_offsets* offsets = nullptr) const override;
overflow get_overflow() const override;
elements_vector& children(); void set_attr(const tchar_t* name, const tchar_t* val) override;
void calc_outlines(int parent_width) override; const tchar_t* get_attr(const tchar_t* name, const tchar_t* def = nullptr) const override;
void calc_auto_margins(int parent_width) override; void apply_stylesheet(const litehtml::css& stylesheet) override;
void refresh_styles() override;
int select(const css_selector& selector, bool apply_pseudo = true) override; bool is_white_space() const override;
int select(const css_element_selector& selector, bool apply_pseudo = true) override; bool is_body() const override;
bool is_break() const override;
int get_base_line() override;
bool on_mouse_over() override;
bool on_mouse_leave() override;
bool on_lbutton_down() override;
bool on_lbutton_up() override;
void on_click() override;
bool find_styles_changes(position::vector& redraw_boxes, int x, int y) override;
const tchar_t* get_cursor() override;
void init_font() override;
bool set_pseudo_class(const tchar_t* pclass, bool add) override;
bool set_class(const tchar_t* pclass, bool add) override;
bool is_replaced() const override;
int line_height() const override;
white_space get_white_space() const override;
style_display get_display() const override;
visibility get_visibility() const override;
void parse_styles(bool is_reparse = false) override;
void draw(uint_ptr hdc, int x, int y, const position* clip) override;
void draw_background(uint_ptr hdc, int x, int y, const position* clip) override;
elements_vector select_all(const tstring& selector) override; const tchar_t* get_style_property(const tchar_t* name, bool inherited, const tchar_t* def = nullptr) override;
elements_vector select_all(const css_selector& selector) override; uint_ptr get_font(font_metrics* fm = nullptr) override;
int get_font_size() const override;
element::ptr select_one(const tstring& selector) override; elements_vector& children();
element::ptr select_one(const css_selector& selector) override; void calc_outlines(int parent_width) override;
void calc_auto_margins(int parent_width) override;
element::ptr find_ancestor(const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr) override; int select(const css_selector& selector, bool apply_pseudo = true) override;
element::ptr find_adjacent_sibling(const element::ptr& el, const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr) override; int select(const css_element_selector& selector, bool apply_pseudo = true) override;
element::ptr find_sibling(const element::ptr& el, const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr) override;
void get_text(tstring& text) override;
void parse_attributes() override;
bool is_first_child_inline(const element::ptr& el) const override; elements_vector select_all(const tstring& selector) override;
bool is_last_child_inline(const element::ptr& el) override; elements_vector select_all(const css_selector& selector) override;
bool have_inline_child() const override;
void get_content_size(size& sz, int max_width) override;
void init() override;
void get_inline_boxes(position::vector& boxes) override;
bool is_floats_holder() const override;
int get_floats_height(element_float el_float = float_none) const override;
int get_left_floats_height() const override;
int get_right_floats_height() const override;
int get_line_left(int y) override;
int get_line_right(int y, int def_right) override;
void get_line_left_right(int y, int def_right, int& ln_left, int& ln_right) override;
void add_float(const element::ptr &el, int x, int y) override;
void update_floats(int dy, const element::ptr &parent) override;
void add_positioned(const element::ptr &el) override;
int find_next_line_top(int top, int width, int def_right) override;
void apply_vertical_align() override;
void draw_children(uint_ptr hdc, int x, int y, const position* clip, draw_flag flag, int zindex) override;
int get_zindex() const override;
void draw_stacking_context(uint_ptr hdc, int x, int y, const position* clip, bool with_positioned) override;
void calc_document_size(litehtml::size& sz, int x = 0, int y = 0) override;
void get_redraw_box(litehtml::position& pos, int x = 0, int y = 0) override;
void add_style(const litehtml::style& st) override;
element::ptr get_element_by_point(int x, int y, int client_x, int client_y) override;
element::ptr get_child_by_point(int x, int y, int client_x, int client_y, draw_flag flag, int zindex) override;
bool is_nth_child(const element::ptr& el, int num, int off, bool of_type) const override; element::ptr select_one(const tstring& selector) override;
bool is_nth_last_child(const element::ptr& el, int num, int off, bool of_type) const override; element::ptr select_one(const css_selector& selector) override;
bool is_only_child(const element::ptr& el, bool of_type) const override;
const background* get_background(bool own_only = false) override;
protected: element::ptr find_ancestor(const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr) override;
void draw_children_box(uint_ptr hdc, int x, int y, const position* clip, draw_flag flag, int zindex); element::ptr find_adjacent_sibling(const element::ptr& el, const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr) override;
void draw_children_table(uint_ptr hdc, int x, int y, const position* clip, draw_flag flag, int zindex); element::ptr find_sibling(const element::ptr& el, const css_selector& selector, bool apply_pseudo = true, bool* is_pseudo = nullptr) override;
int render_box(int x, int y, int max_width, bool second_pass = false); void get_text(tstring& text) override;
int render_table(int x, int y, int max_width, bool second_pass = false); void parse_attributes() override;
int fix_line_width(int max_width, element_float flt);
void parse_background();
void init_background_paint( position pos, background_paint &bg_paint, const background* bg );
void draw_list_marker( uint_ptr hdc, const position &pos );
tstring get_list_marker_text(int index);
static void parse_nth_child_params( const tstring& param, int &num, int &off );
void remove_before_after();
litehtml::element::ptr get_element_before();
litehtml::element::ptr get_element_after();
};
/************************************************************************/ bool is_first_child_inline(const element::ptr& el) const override;
/* Inline Functions */ bool is_last_child_inline(const element::ptr& el) override;
/************************************************************************/ bool have_inline_child() const override;
void get_content_size(size& sz, int max_width) override;
void init() override;
void get_inline_boxes(position::vector& boxes) override;
bool is_floats_holder() const override;
int get_floats_height(element_float el_float = float_none) const override;
int get_left_floats_height() const override;
int get_right_floats_height() const override;
int get_line_left(int y) override;
int get_line_right(int y, int def_right) override;
void get_line_left_right(int y, int def_right, int& ln_left, int& ln_right) override;
void add_float(const element::ptr& el, int x, int y) override;
void update_floats(int dy, const element::ptr& parent) override;
void add_positioned(const element::ptr& el) override;
int find_next_line_top(int top, int width, int def_right) override;
void apply_vertical_align() override;
void draw_children(uint_ptr hdc, int x, int y, const position* clip, draw_flag flag, int zindex) override;
int get_zindex() const override;
void draw_stacking_context(uint_ptr hdc, int x, int y, const position* clip, bool with_positioned) override;
void calc_document_size(litehtml::size& sz, int x = 0, int y = 0) override;
void get_redraw_box(litehtml::position& pos, int x = 0, int y = 0) override;
void add_style(const litehtml::style& st) override;
element::ptr get_element_by_point(int x, int y, int client_x, int client_y) override;
element::ptr get_child_by_point(int x, int y, int client_x, int client_y, draw_flag flag, int zindex) override;
inline elements_vector& litehtml::html_tag::children() bool is_nth_child(const element::ptr& el, int num, int off, bool of_type) const override;
{ bool is_nth_last_child(const element::ptr& el, int num, int off, bool of_type) const override;
return m_children; bool is_only_child(const element::ptr& el, bool of_type) const override;
} const background* get_background(bool own_only = false) override;
}
protected:
void draw_children_box(uint_ptr hdc, int x, int y, const position* clip, draw_flag flag, int zindex);
void draw_children_table(uint_ptr hdc, int x, int y, const position* clip, draw_flag flag, int zindex);
int render_box(int x, int y, int max_width, bool second_pass = false);
int render_table(int x, int y, int max_width, bool second_pass = false);
int fix_line_width(int max_width, element_float flt);
void parse_background();
void init_background_paint(position pos, background_paint& bg_paint, const background* bg);
void draw_list_marker(uint_ptr hdc, const position& pos);
tstring get_list_marker_text(int index);
static void parse_nth_child_params(const tstring& param, int& num, int& off);
void remove_before_after();
litehtml::element::ptr get_element_before();
litehtml::element::ptr get_element_after();
};
/************************************************************************/
/* Inline Functions */
/************************************************************************/
inline elements_vector& litehtml::html_tag::children() { return m_children; }
} // namespace litehtml
#endif // LH_HTML_TAG_H #endif // LH_HTML_TAG_H

View file

@ -3,87 +3,74 @@
#include "types.h" #include "types.h"
namespace litehtml namespace litehtml {
{ class element;
class element;
class iterator_selector class iterator_selector {
{ public:
public: virtual bool select(const element::ptr& el) = 0;
virtual bool select(const element::ptr& el) = 0;
protected: protected:
~iterator_selector() = default; ~iterator_selector() = default;
}; };
class elements_iterator class elements_iterator {
{ private:
private: struct stack_item {
struct stack_item int idx;
{ element::ptr el;
int idx; stack_item() : idx(0) {}
element::ptr el; stack_item(const stack_item& val) {
stack_item() : idx(0) idx = val.idx;
{ el = val.el;
} }
stack_item(const stack_item& val) stack_item(stack_item&& val) {
{ idx = val.idx;
idx = val.idx; el = std::move(val.el);
el = val.el; }
} };
stack_item(stack_item&& val)
{
idx = val.idx;
el = std::move(val.el);
}
};
std::vector<stack_item> m_stack; std::vector<stack_item> m_stack;
element::ptr m_el; element::ptr m_el;
int m_idx; int m_idx;
iterator_selector* m_go_inside; iterator_selector* m_go_inside;
iterator_selector* m_select; iterator_selector* m_select;
public:
elements_iterator(const element::ptr& el, iterator_selector* go_inside, iterator_selector* select) public:
{ elements_iterator(const element::ptr& el, iterator_selector* go_inside, iterator_selector* select) {
m_el = el; m_el = el;
m_idx = -1; m_idx = -1;
m_go_inside = go_inside; m_go_inside = go_inside;
m_select = select; m_select = select;
} }
~elements_iterator() = default; ~elements_iterator() = default;
element::ptr next(bool ret_parent = true); element::ptr next(bool ret_parent = true);
private:
void next_idx();
};
class go_inside_inline final : public iterator_selector private:
{ void next_idx();
public: };
bool select(const element::ptr& el) override;
};
class go_inside_table final : public iterator_selector class go_inside_inline final : public iterator_selector {
{ public:
public: bool select(const element::ptr& el) override;
bool select(const element::ptr& el) override; };
};
class table_rows_selector final : public iterator_selector class go_inside_table final : public iterator_selector {
{ public:
public: bool select(const element::ptr& el) override;
bool select(const element::ptr& el) override; };
};
class table_cells_selector final : public iterator_selector class table_rows_selector final : public iterator_selector {
{ public:
public: bool select(const element::ptr& el) override;
bool select(const element::ptr& el) override; };
};
} class table_cells_selector final : public iterator_selector {
public:
bool select(const element::ptr& el) override;
};
} // namespace litehtml
#endif // LH_ITERATORS_H #endif // LH_ITERATORS_H

View file

@ -1,77 +1,69 @@
#ifndef LH_MEDIA_QUERY_H #ifndef LH_MEDIA_QUERY_H
#define LH_MEDIA_QUERY_H #define LH_MEDIA_QUERY_H
namespace litehtml namespace litehtml {
{ struct media_query_expression {
struct media_query_expression typedef std::vector<media_query_expression> vector;
{ media_feature feature;
typedef std::vector<media_query_expression> vector; int val;
media_feature feature; int val2;
int val; bool check_as_bool;
int val2;
bool check_as_bool;
media_query_expression()
{
check_as_bool = false;
feature = media_feature_none;
val = 0;
val2 = 0;
}
bool check(const media_features& features) const; media_query_expression() {
}; check_as_bool = false;
feature = media_feature_none;
val = 0;
val2 = 0;
}
class media_query bool check(const media_features& features) const;
{ };
public:
typedef std::shared_ptr<media_query> ptr;
typedef std::vector<media_query::ptr> vector;
private:
media_query_expression::vector m_expressions;
bool m_not;
media_type m_media_type;
public:
media_query();
media_query(const media_query& val);
static media_query::ptr create_from_string(const tstring& str, const std::shared_ptr<document>& doc); class media_query {
bool check(const media_features& features) const; public:
}; typedef std::shared_ptr<media_query> ptr;
typedef std::vector<media_query::ptr> vector;
class media_query_list private:
{ media_query_expression::vector m_expressions;
public: bool m_not;
typedef std::shared_ptr<media_query_list> ptr; media_type m_media_type;
typedef std::vector<media_query_list::ptr> vector;
private:
media_query::vector m_queries;
bool m_is_used;
public:
media_query_list();
media_query_list(const media_query_list& val);
static media_query_list::ptr create_from_string(const tstring& str, const std::shared_ptr<document>& doc); public:
bool is_used() const; media_query();
bool apply_media_features(const media_features& features); // returns true if the m_is_used changed media_query(const media_query& val);
};
inline media_query_list::media_query_list(const media_query_list& val) static media_query::ptr create_from_string(const tstring& str, const std::shared_ptr<document>& doc);
{ bool check(const media_features& features) const;
m_is_used = val.m_is_used; };
m_queries = val.m_queries;
}
inline media_query_list::media_query_list() class media_query_list {
{ public:
m_is_used = false; typedef std::shared_ptr<media_query_list> ptr;
} typedef std::vector<media_query_list::ptr> vector;
inline bool media_query_list::is_used() const private:
{ media_query::vector m_queries;
return m_is_used; bool m_is_used;
}
public:
media_query_list();
media_query_list(const media_query_list& val);
static media_query_list::ptr create_from_string(const tstring& str, const std::shared_ptr<document>& doc);
bool is_used() const;
bool apply_media_features(const media_features& features); // returns true if the m_is_used changed
};
inline media_query_list::media_query_list(const media_query_list& val) {
m_is_used = val.m_is_used;
m_queries = val.m_queries;
} }
inline media_query_list::media_query_list() { m_is_used = false; }
inline bool media_query_list::is_used() const { return m_is_used; }
} // namespace litehtml
#endif // LH_MEDIA_QUERY_H #endif // LH_MEDIA_QUERY_H

View file

@ -2,18 +2,17 @@
#define NUM_CVT_H #define NUM_CVT_H
#include <string> #include <string>
#include "os_types.h" #include "os_types.h"
namespace litehtml namespace litehtml {
{ namespace num_cvt {
namespace num_cvt litehtml::tstring to_latin_lower(int val);
{ litehtml::tstring to_latin_upper(int val);
litehtml::tstring to_latin_lower(int val); litehtml::tstring to_greek_lower(int val);
litehtml::tstring to_latin_upper(int val); litehtml::tstring to_roman_lower(int value);
litehtml::tstring to_greek_lower(int val); litehtml::tstring to_roman_upper(int value);
litehtml::tstring to_roman_lower(int value); } // namespace num_cvt
litehtml::tstring to_roman_upper(int value); } // namespace litehtml
}
}
#endif // NUM_CVT_H #endif // NUM_CVT_H

View file

@ -1,12 +1,11 @@
#ifndef LH_OS_TYPES_H #ifndef LH_OS_TYPES_H
#define LH_OS_TYPES_H #define LH_OS_TYPES_H
#include <string>
#include <cstdint> #include <cstdint>
#include <string>
namespace litehtml namespace litehtml {
{ #if defined(WIN32) || defined(_WIN32) || defined(WINCE)
#if defined( WIN32 ) || defined( _WIN32 ) || defined( WINCE )
// noexcept appeared since Visual Studio 2013 // noexcept appeared since Visual Studio 2013
#if _MSC_VER < 1900 #if _MSC_VER < 1900
@ -15,71 +14,71 @@ namespace litehtml
#ifndef LITEHTML_UTF8 #ifndef LITEHTML_UTF8
typedef std::wstring tstring; typedef std::wstring tstring;
typedef wchar_t tchar_t; typedef wchar_t tchar_t;
typedef std::wstringstream tstringstream; typedef std::wstringstream tstringstream;
#define _t(quote) L##quote #define _t(quote) L##quote
#define t_strlen wcslen #define t_strlen wcslen
#define t_strcmp wcscmp #define t_strcmp wcscmp
#define t_strncmp wcsncmp #define t_strncmp wcsncmp
#define t_strtol wcstol #define t_strtol wcstol
#define t_atoi _wtoi #define t_atoi _wtoi
#define t_itoa(value, buffer, size, radix) _itow_s(value, buffer, size, radix) #define t_itoa(value, buffer, size, radix) _itow_s(value, buffer, size, radix)
#define t_strstr wcsstr #define t_strstr wcsstr
#define t_isspace iswspace #define t_isspace iswspace
#define t_to_string(val) std::to_wstring(val) #define t_to_string(val) std::to_wstring(val)
#else #else
typedef std::string tstring; typedef std::string tstring;
typedef char tchar_t; typedef char tchar_t;
typedef std::stringstream tstringstream; typedef std::stringstream tstringstream;
#define _t(quote) quote #define _t(quote) quote
#define t_strlen strlen #define t_strlen strlen
#define t_strcmp strcmp #define t_strcmp strcmp
#define t_strncmp strncmp #define t_strncmp strncmp
#define t_strtol strtol #define t_strtol strtol
#define t_atoi atoi #define t_atoi atoi
#define t_itoa(value, buffer, size, radix) _itoa_s(value, buffer, size, radix) #define t_itoa(value, buffer, size, radix) _itoa_s(value, buffer, size, radix)
#define t_strstr strstr #define t_strstr strstr
#define t_isspace isspace #define t_isspace isspace
#define t_to_string(val) std::to_string(val) #define t_to_string(val) std::to_string(val)
#endif #endif
#ifdef _WIN64 #ifdef _WIN64
typedef unsigned __int64 uint_ptr; typedef unsigned __int64 uint_ptr;
#else #else
typedef unsigned int uint_ptr; typedef unsigned int uint_ptr;
#endif #endif
#else #else
#define LITEHTML_UTF8 #define LITEHTML_UTF8
typedef std::string tstring; typedef std::string tstring;
typedef char tchar_t; typedef char tchar_t;
typedef std::uintptr_t uint_ptr; typedef std::uintptr_t uint_ptr;
typedef std::stringstream tstringstream; typedef std::stringstream tstringstream;
#define _t(quote) quote #define _t(quote) quote
#define t_strlen strlen #define t_strlen strlen
#define t_strcmp strcmp #define t_strcmp strcmp
#define t_strncmp strncmp #define t_strncmp strncmp
#define t_itoa(value, buffer, size, radix) snprintf(buffer, size, "%d", value) #define t_itoa(value, buffer, size, radix) snprintf(buffer, size, "%d", value)
#define t_strtol strtol #define t_strtol strtol
#define t_atoi atoi #define t_atoi atoi
#define t_strstr strstr #define t_strstr strstr
#define t_isspace isspace #define t_isspace isspace
#define t_to_string(val) std::to_string(val) #define t_to_string(val) std::to_string(val)
#endif #endif
} } // namespace litehtml
#endif // LH_OS_TYPES_H #endif // LH_OS_TYPES_H

View file

@ -1,95 +1,79 @@
#ifndef LH_STYLE_H #ifndef LH_STYLE_H
#define LH_STYLE_H #define LH_STYLE_H
#include "attributes.h"
#include <string> #include <string>
namespace litehtml #include "attributes.h"
{
class property_value
{
public:
tstring m_value;
bool m_important;
property_value() namespace litehtml {
{ class property_value {
m_important = false; public:
} tstring m_value;
property_value(const tchar_t* val, bool imp) bool m_important;
{
m_important = imp;
m_value = val;
}
property_value(const property_value& val)
{
m_value = val.m_value;
m_important = val.m_important;
}
property_value& operator=(const property_value& val) property_value() { m_important = false; }
{ property_value(const tchar_t* val, bool imp) {
m_value = val.m_value; m_important = imp;
m_important = val.m_important; m_value = val;
return *this; }
} property_value(const property_value& val) {
}; m_value = val.m_value;
m_important = val.m_important;
}
typedef std::map<tstring, property_value> props_map; property_value& operator=(const property_value& val) {
m_value = val.m_value;
m_important = val.m_important;
return *this;
}
};
class style typedef std::map<tstring, property_value> props_map;
{
public:
typedef std::shared_ptr<style> ptr;
typedef std::vector<style::ptr> vector;
private:
props_map m_properties;
static string_map m_valid_values;
public:
style() = default;
style(const style& val);
style& operator=(const style& val) class style {
{ public:
m_properties = val.m_properties; typedef std::shared_ptr<style> ptr;
return *this; typedef std::vector<style::ptr> vector;
}
void add(const tchar_t* txt, const tchar_t* baseurl) private:
{ props_map m_properties;
parse(txt, baseurl); static string_map m_valid_values;
}
void add_property(const tchar_t* name, const tchar_t* val, const tchar_t* baseurl, bool important); public:
style() = default;
style(const style& val);
const tchar_t* get_property(const tchar_t* name) const style& operator=(const style& val) {
{ m_properties = val.m_properties;
if(name) return *this;
{ }
auto f = m_properties.find(name);
if(f != m_properties.end())
{
return f->second.m_value.c_str();
}
}
return nullptr;
}
void combine(const litehtml::style& src); void add(const tchar_t* txt, const tchar_t* baseurl) { parse(txt, baseurl); }
void clear()
{
m_properties.clear();
}
private: void add_property(const tchar_t* name, const tchar_t* val, const tchar_t* baseurl, bool important);
void parse_property(const tstring& txt, const tchar_t* baseurl);
void parse(const tchar_t* txt, const tchar_t* baseurl); const tchar_t* get_property(const tchar_t* name) const {
void parse_short_border(const tstring& prefix, const tstring& val, bool important); if (name) {
void parse_short_background(const tstring& val, const tchar_t* baseurl, bool important); auto f = m_properties.find(name);
void parse_short_font(const tstring& val, bool important); if (f != m_properties.end()) {
void add_parsed_property(const tstring& name, const tstring& val, bool important); return f->second.m_value.c_str();
void remove_property(const tstring& name, bool important); }
}; }
} return nullptr;
}
void combine(const litehtml::style& src);
void clear() { m_properties.clear(); }
private:
void parse_property(const tstring& txt, const tchar_t* baseurl);
void parse(const tchar_t* txt, const tchar_t* baseurl);
void parse_short_border(const tstring& prefix, const tstring& val, bool important);
void parse_short_background(const tstring& val, const tchar_t* baseurl, bool important);
void parse_short_font(const tstring& val, bool important);
void add_parsed_property(const tstring& name, const tstring& val, bool important);
void remove_property(const tstring& name, bool important);
};
} // namespace litehtml
#endif // LH_STYLE_H #endif // LH_STYLE_H

View file

@ -1,47 +1,38 @@
#ifndef LH_STYLESHEET_H #ifndef LH_STYLESHEET_H
#define LH_STYLESHEET_H #define LH_STYLESHEET_H
#include "style.h"
#include "css_selector.h" #include "css_selector.h"
#include "style.h"
namespace litehtml namespace litehtml {
{ class document_container;
class document_container;
class css class css {
{ css_selector::vector m_selectors;
css_selector::vector m_selectors;
public:
css() = default;
~css() = default;
const css_selector::vector& selectors() const public:
{ css() = default;
return m_selectors; ~css() = default;
}
void clear() const css_selector::vector& selectors() const { return m_selectors; }
{
m_selectors.clear();
}
void parse_stylesheet(const tchar_t* str, const tchar_t* baseurl, const std::shared_ptr <document>& doc, const media_query_list::ptr& media); void clear() { m_selectors.clear(); }
void sort_selectors();
static void parse_css_url(const tstring& str, tstring& url);
private: void parse_stylesheet(const tchar_t* str, const tchar_t* baseurl, const std::shared_ptr<document>& doc, const media_query_list::ptr& media);
void parse_atrule(const tstring& text, const tchar_t* baseurl, const std::shared_ptr<document>& doc, const media_query_list::ptr& media); void sort_selectors();
void add_selector(const css_selector::ptr& selector); static void parse_css_url(const tstring& str, tstring& url);
bool parse_selectors(const tstring& txt, const litehtml::style::ptr& styles, const media_query_list::ptr& media);
}; private:
void parse_atrule(const tstring& text, const tchar_t* baseurl, const std::shared_ptr<document>& doc, const media_query_list::ptr& media);
inline void litehtml::css::add_selector( const css_selector::ptr& selector ) void add_selector(const css_selector::ptr& selector);
{ bool parse_selectors(const tstring& txt, const litehtml::style::ptr& styles, const media_query_list::ptr& media);
selector->m_order = (int) m_selectors.size(); };
m_selectors.push_back(selector);
}
inline void litehtml::css::add_selector(const css_selector::ptr& selector) {
selector->m_order = (int)m_selectors.size();
m_selectors.push_back(selector);
} }
} // namespace litehtml
#endif // LH_STYLESHEET_H #endif // LH_STYLESHEET_H

View file

@ -1,251 +1,231 @@
#ifndef LH_TABLE_H #ifndef LH_TABLE_H
#define LH_TABLE_H #define LH_TABLE_H
namespace litehtml namespace litehtml {
{ struct table_row {
struct table_row typedef std::vector<table_row> vector;
{
typedef std::vector<table_row> vector;
int height; int height;
int border_top; int border_top;
int border_bottom; int border_bottom;
element::ptr el_row; element::ptr el_row;
int top; int top;
int bottom; int bottom;
css_length css_height; css_length css_height;
int min_height; int min_height;
table_row() table_row() {
{ min_height = 0;
min_height = 0; top = 0;
top = 0; bottom = 0;
bottom = 0; border_bottom = 0;
border_bottom = 0; border_top = 0;
border_top = 0; height = 0;
height = 0; el_row = nullptr;
el_row = nullptr; css_height.predef(0);
css_height.predef(0); }
}
table_row(int h, element::ptr& row) table_row(int h, element::ptr& row) {
{ min_height = 0;
min_height = 0; height = h;
height = h; el_row = row;
el_row = row; border_bottom = 0;
border_bottom = 0; border_top = 0;
border_top = 0; top = 0;
top = 0; bottom = 0;
bottom = 0; if (row) {
if (row) css_height = row->get_css_height();
{ }
css_height = row->get_css_height(); }
}
}
table_row(const table_row& val) table_row(const table_row& val) {
{ min_height = val.min_height;
min_height = val.min_height; top = val.top;
top = val.top; bottom = val.bottom;
bottom = val.bottom; border_bottom = val.border_bottom;
border_bottom = val.border_bottom; border_top = val.border_top;
border_top = val.border_top; height = val.height;
height = val.height; css_height = val.css_height;
css_height = val.css_height; el_row = val.el_row;
el_row = val.el_row; }
}
table_row(table_row&& val) noexcept table_row(table_row&& val) noexcept {
{ min_height = val.min_height;
min_height = val.min_height; top = val.top;
top = val.top; bottom = val.bottom;
bottom = val.bottom; border_bottom = val.border_bottom;
border_bottom = val.border_bottom; border_top = val.border_top;
border_top = val.border_top; height = val.height;
height = val.height; css_height = val.css_height;
css_height = val.css_height; el_row = std::move(val.el_row);
el_row = std::move(val.el_row); }
} };
};
struct table_column struct table_column {
{ typedef std::vector<table_column> vector;
typedef std::vector<table_column> vector;
int min_width;
int max_width;
int width;
css_length css_width;
int border_left;
int border_right;
int left;
int right;
table_column() int min_width;
{ int max_width;
left = 0; int width;
right = 0; css_length css_width;
border_left = 0; int border_left;
border_right = 0; int border_right;
min_width = 0; int left;
max_width = 0; int right;
width = 0;
css_width.predef(0);
}
table_column(int min_w, int max_w) table_column() {
{ left = 0;
left = 0; right = 0;
right = 0; border_left = 0;
border_left = 0; border_right = 0;
border_right = 0; min_width = 0;
max_width = max_w; max_width = 0;
min_width = min_w; width = 0;
width = 0; css_width.predef(0);
css_width.predef(0); }
}
table_column(const table_column& val) table_column(int min_w, int max_w) {
{ left = 0;
left = val.left; right = 0;
right = val.right; border_left = 0;
border_left = val.border_left; border_right = 0;
border_right = val.border_right; max_width = max_w;
max_width = val.max_width; min_width = min_w;
min_width = val.min_width; width = 0;
width = val.width; css_width.predef(0);
css_width = val.css_width; }
}
};
class table_column_accessor table_column(const table_column& val) {
{ left = val.left;
public: right = val.right;
virtual int& get(table_column& col) = 0; border_left = val.border_left;
border_right = val.border_right;
max_width = val.max_width;
min_width = val.min_width;
width = val.width;
css_width = val.css_width;
}
};
protected: class table_column_accessor {
~table_column_accessor() = default; public:
}; virtual int& get(table_column& col) = 0;
class table_column_accessor_max_width final : public table_column_accessor protected:
{ ~table_column_accessor() = default;
public: };
int& get(table_column& col) override;
};
class table_column_accessor_min_width final : public table_column_accessor class table_column_accessor_max_width final : public table_column_accessor {
{ public:
public: int& get(table_column& col) override;
int& get(table_column& col) override; };
};
class table_column_accessor_width final : public table_column_accessor class table_column_accessor_min_width final : public table_column_accessor {
{ public:
public: int& get(table_column& col) override;
int& get(table_column& col) override; };
};
struct table_cell class table_column_accessor_width final : public table_column_accessor {
{ public:
element::ptr el; int& get(table_column& col) override;
int colspan; };
int rowspan;
int min_width;
int min_height;
int max_width;
int max_height;
int width;
int height;
margins borders;
table_cell() struct table_cell {
{ element::ptr el;
min_width = 0; int colspan;
min_height = 0; int rowspan;
max_width = 0; int min_width;
max_height = 0; int min_height;
width = 0; int max_width;
height = 0; int max_height;
colspan = 1; int width;
rowspan = 1; int height;
el = nullptr; margins borders;
}
table_cell(const table_cell& val) table_cell() {
{ min_width = 0;
el = val.el; min_height = 0;
colspan = val.colspan; max_width = 0;
rowspan = val.rowspan; max_height = 0;
width = val.width; width = 0;
height = val.height; height = 0;
min_width = val.min_width; colspan = 1;
min_height = val.min_height; rowspan = 1;
max_width = val.max_width; el = nullptr;
max_height = val.max_height; }
borders = val.borders;
}
table_cell(table_cell&& val) noexcept table_cell(const table_cell& val) {
{ el = val.el;
el = std::move(val.el); colspan = val.colspan;
colspan = val.colspan; rowspan = val.rowspan;
rowspan = val.rowspan; width = val.width;
width = val.width; height = val.height;
height = val.height; min_width = val.min_width;
min_width = val.min_width; min_height = val.min_height;
min_height = val.min_height; max_width = val.max_width;
max_width = val.max_width; max_height = val.max_height;
max_height = val.max_height; borders = val.borders;
borders = val.borders; }
}
};
class table_grid table_cell(table_cell&& val) noexcept {
{ el = std::move(val.el);
public: colspan = val.colspan;
typedef std::vector< std::vector<table_cell> > rows; rowspan = val.rowspan;
private: width = val.width;
int m_rows_count; height = val.height;
int m_cols_count; min_width = val.min_width;
rows m_cells; min_height = val.min_height;
table_column::vector m_columns; max_width = val.max_width;
table_row::vector m_rows; max_height = val.max_height;
elements_vector m_captions; borders = val.borders;
int m_captions_height; }
public: };
table_grid() class table_grid {
{ public:
m_rows_count = 0; typedef std::vector<std::vector<table_cell> > rows;
m_cols_count = 0;
m_captions_height = 0;
}
void clear(); private:
void begin_row(element::ptr& row); int m_rows_count;
void add_cell(element::ptr& el); int m_cols_count;
bool is_rowspanned(int r, int c); rows m_cells;
void finish(); table_column::vector m_columns;
table_cell* cell(int t_col, int t_row); table_row::vector m_rows;
table_column& column(int c) { return m_columns[c]; } elements_vector m_captions;
table_row& row(int r) { return m_rows[r]; } int m_captions_height;
elements_vector& captions() { return m_captions; }
int rows_count() const { return m_rows_count; } public:
int cols_count() const { return m_cols_count; } table_grid() {
m_rows_count = 0;
m_cols_count = 0;
m_captions_height = 0;
}
void captions_height(int height) { m_captions_height = height; } void clear();
int captions_height() const { return m_captions_height; } void begin_row(element::ptr& row);
void add_cell(element::ptr& el);
bool is_rowspanned(int r, int c);
void finish();
table_cell* cell(int t_col, int t_row);
table_column& column(int c) { return m_columns[c]; }
table_row& row(int r) { return m_rows[r]; }
elements_vector& captions() { return m_captions; }
void distribute_max_width(int width, int start, int end); int rows_count() const { return m_rows_count; }
void distribute_min_width(int width, int start, int end); int cols_count() const { return m_cols_count; }
void distribute_width(int width, int start, int end);
void distribute_width(int width, int start, int end, table_column_accessor* acc); void captions_height(int height) { m_captions_height = height; }
int calc_table_width(int block_width, bool is_auto, int& min_table_width, int& max_table_width); int captions_height() const { return m_captions_height; }
void calc_horizontal_positions(margins& table_borders, border_collapse bc, int bdr_space_x);
void calc_vertical_positions(margins& table_borders, border_collapse bc, int bdr_space_y); void distribute_max_width(int width, int start, int end);
void calc_rows_height(int blockHeight, int borderSpacingY); void distribute_min_width(int width, int start, int end);
}; void distribute_width(int width, int start, int end);
} void distribute_width(int width, int start, int end, table_column_accessor* acc);
int calc_table_width(int block_width, bool is_auto, int& min_table_width, int& max_table_width);
void calc_horizontal_positions(margins& table_borders, border_collapse bc, int bdr_space_x);
void calc_vertical_positions(margins& table_borders, border_collapse bc, int bdr_space_y);
void calc_rows_height(int blockHeight, int borderSpacingY);
};
} // namespace litehtml
#endif // LH_TABLE_H #endif // LH_TABLE_H

View file

@ -46,91 +46,58 @@ namespace litehtml {
// (e.g., via a using statement). // (e.g., via a using statement).
class tstring_view { class tstring_view {
public: public:
using value_type = tchar_t; using value_type = tchar_t;
using pointer = tchar_t*; using pointer = tchar_t*;
using const_pointer = const tchar_t*; using const_pointer = const tchar_t*;
using reference = tchar_t&; using reference = tchar_t&;
using const_reference = const tchar_t&; using const_reference = const tchar_t&;
using iterator = const_pointer; using iterator = const_pointer;
using const_iterator = const_pointer; using const_iterator = const_pointer;
using size_type = size_t; using size_type = size_t;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
public: public:
tstring_view() = default; tstring_view() = default;
tstring_view(const tstring_view& other) = default; tstring_view(const tstring_view& other) = default;
tstring_view(const_pointer s, size_type size) tstring_view(const_pointer s, size_type size) : data_(s), size_(size) {}
: data_(s)
, size_(size)
{
}
constexpr const_iterator begin() const constexpr const_iterator begin() const { return data_; }
{
return data_;
}
constexpr const_iterator cbegin() const constexpr const_iterator cbegin() const { return data_; }
{
return data_;
}
constexpr const_iterator end() const constexpr const_iterator end() const { return data_ + size_; }
{
return data_ + size_;
}
constexpr const_iterator cend() const constexpr const_iterator cend() const { return data_ + size_; }
{
return data_ + size_;
}
constexpr const_reference operator[](size_type offset) const constexpr const_reference operator[](size_type offset) const { return *(data_ + offset); }
{
return *(data_ + offset);
}
constexpr const_pointer data() const constexpr const_pointer data() const { return data_; }
{
return data_;
}
size_type size() const size_type size() const { return size_; }
{
return size_;
}
size_type length() const size_type length() const { return size_; }
{
return size_;
}
bool empty() const bool empty() const { return (size_ == 0); }
{
return (size_ == 0);
}
private: private:
const_pointer data_ = nullptr; const_pointer data_ = nullptr;
size_type size_ = 0; size_type size_ = 0;
}; };
std::basic_ostream<tstring_view::value_type>& operator<<( std::basic_ostream<tstring_view::value_type>& operator<<(std::basic_ostream<tstring_view::value_type>&, tstring_view str);
std::basic_ostream<tstring_view::value_type>&,
tstring_view str);
} // namespace litehtml } // namespace litehtml
#endif // LITEHTML_TSTRING_VIEW_H__ #endif // LITEHTML_TSTRING_VIEW_H__

File diff suppressed because it is too large Load diff

View file

@ -39,92 +39,55 @@
namespace litehtml { namespace litehtml {
class url { class url {
public: public:
url() = default; url() = default;
explicit url(const tstring& str); explicit url(const tstring& str);
url(const tstring& scheme, url(const tstring& scheme, const tstring& authority, const tstring& path, const tstring& query, const tstring& fragment);
const tstring& authority,
const tstring& path,
const tstring& query,
const tstring& fragment);
const tstring& string() const const tstring& string() const { return str_; }
{
return str_;
}
const tstring& scheme() const const tstring& scheme() const { return scheme_; }
{
return scheme_;
}
bool has_scheme() const bool has_scheme() const { return !scheme_.empty(); }
{
return !scheme_.empty();
}
const tstring& authority() const const tstring& authority() const { return authority_; }
{
return authority_;
}
bool has_authority() const bool has_authority() const { return !authority_.empty(); }
{
return !authority_.empty();
}
const tstring& path() const const tstring& path() const { return path_; }
{
return path_;
}
bool has_path() const bool has_path() const { return !path_.empty(); }
{
return !path_.empty();
}
const tstring& query() const const tstring& query() const { return query_; }
{
return query_;
}
bool has_query() const bool has_query() const { return !query_.empty(); }
{
return !query_.empty();
}
const tstring& fragment() const const tstring& fragment() const { return fragment_; }
{
return fragment_;
}
bool has_fragment() const bool has_fragment() const { return !fragment_.empty(); }
{
return !fragment_.empty();
}
protected: protected:
tstring str_; tstring str_;
// Assume URLs are relative by default. See RFC 3986 Section 4.3 for // Assume URLs are relative by default. See RFC 3986 Section 4.3 for
// information on which URLs are considered relative and which URLs are // information on which URLs are considered relative and which URLs are
// considered absolute: // considered absolute:
// //
// https://datatracker.ietf.org/doc/html/rfc3986#section-4.3 // https://datatracker.ietf.org/doc/html/rfc3986#section-4.3
bool absolute_ = false; bool absolute_ = false;
tstring scheme_; tstring scheme_;
tstring authority_; tstring authority_;
tstring path_; tstring path_;
tstring query_; tstring query_;
tstring fragment_; tstring fragment_;
}; };
// Returns a URL that is resolved from the reference URL that might be // Returns a URL that is resolved from the reference URL that might be
@ -134,6 +97,6 @@ protected:
url resolve(const url& base, const url& reference); url resolve(const url& base, const url& reference);
} // namespace litehtml } // namespace litehtml
#endif // LITEHTML_URL_H__ #endif // LITEHTML_URL_H__

View file

@ -46,6 +46,6 @@ tstring url_path_append(const tstring& base, const tstring& path);
tstring url_path_resolve(const tstring& base, const tstring& path); tstring url_path_resolve(const tstring& base, const tstring& path);
} // namespace litehtml } // namespace litehtml
#endif // LITEHTML_URL_PATH_H__ #endif // LITEHTML_URL_PATH_H__

View file

@ -4,56 +4,43 @@
#include "os_types.h" #include "os_types.h"
#include "types.h" #include "types.h"
namespace litehtml namespace litehtml {
{ class utf8_to_wchar {
class utf8_to_wchar const byte* m_utf8;
{ std::wstring m_str;
const byte* m_utf8;
std::wstring m_str;
public:
utf8_to_wchar(const char* val);
operator const wchar_t*() const
{
return m_str.c_str();
}
private:
ucode_t getb()
{
if (!(*m_utf8)) return 0;
return *m_utf8++;
}
ucode_t get_next_utf8(ucode_t val)
{
return (val & 0x3f);
}
ucode_t get_char();
};
class wchar_to_utf8 public:
{ utf8_to_wchar(const char* val);
std::string m_str; operator const wchar_t*() const { return m_str.c_str(); }
public:
wchar_to_utf8(const std::wstring& val);
operator const char*() const
{
return m_str.c_str();
}
const char* c_str() const private:
{ ucode_t getb() {
return m_str.c_str(); if (!(*m_utf8)) return 0;
} return *m_utf8++;
}; }
ucode_t get_next_utf8(ucode_t val) { return (val & 0x3f); }
ucode_t get_char();
};
class wchar_to_utf8 {
std::string m_str;
public:
wchar_to_utf8(const std::wstring& val);
operator const char*() const { return m_str.c_str(); }
const char* c_str() const { return m_str.c_str(); }
};
#ifdef LITEHTML_UTF8 #ifdef LITEHTML_UTF8
#define litehtml_from_utf8(str) str #define litehtml_from_utf8(str) str
#define litehtml_to_utf8(str) str #define litehtml_to_utf8(str) str
#define litehtml_from_wchar(str) litehtml::wchar_to_utf8(str) #define litehtml_from_wchar(str) litehtml::wchar_to_utf8(str)
#else #else
#define litehtml_from_utf8(str) litehtml::utf8_to_wchar(str) #define litehtml_from_utf8(str) litehtml::utf8_to_wchar(str)
#define litehtml_from_wchar(str) str #define litehtml_from_wchar(str) str
#define litehtml_to_utf8(str) litehtml::wchar_to_utf8(str) #define litehtml_to_utf8(str) litehtml::wchar_to_utf8(str)
#endif #endif
} } // namespace litehtml
#endif // LH_UTF8_STRINGS_H #endif // LH_UTF8_STRINGS_H

View file

@ -1,61 +1,54 @@
#ifndef LH_WEB_COLOR_H #ifndef LH_WEB_COLOR_H
#define LH_WEB_COLOR_H #define LH_WEB_COLOR_H
namespace litehtml namespace litehtml {
{ struct def_color {
struct def_color const tchar_t* name;
{ const tchar_t* rgb;
const tchar_t* name; };
const tchar_t* rgb;
};
extern def_color g_def_colors[]; extern def_color g_def_colors[];
class document_container; class document_container;
struct web_color struct web_color {
{ byte blue;
byte blue; byte green;
byte green; byte red;
byte red; byte alpha;
byte alpha;
web_color(byte r, byte g, byte b, byte a = 255) web_color(byte r, byte g, byte b, byte a = 255) {
{ blue = b;
blue = b; green = g;
green = g; red = r;
red = r; alpha = a;
alpha = a; }
}
web_color() web_color() {
{ blue = 0;
blue = 0; green = 0;
green = 0; red = 0;
red = 0; alpha = 0xFF;
alpha = 0xFF; }
}
web_color(const web_color& val) web_color(const web_color& val) {
{ blue = val.blue;
blue = val.blue; green = val.green;
green = val.green; red = val.red;
red = val.red; alpha = val.alpha;
alpha = val.alpha; }
}
web_color& operator=(const web_color& val) web_color& operator=(const web_color& val) {
{ blue = val.blue;
blue = val.blue; green = val.green;
green = val.green; red = val.red;
red = val.red; alpha = val.alpha;
alpha = val.alpha; return *this;
return *this; }
} static web_color from_string(const tchar_t* str, litehtml::document_container* callback);
static web_color from_string(const tchar_t* str, litehtml::document_container* callback); static litehtml::tstring resolve_name(const tchar_t* name, litehtml::document_container* callback);
static litehtml::tstring resolve_name(const tchar_t* name, litehtml::document_container* callback); static bool is_color(const tchar_t* str);
static bool is_color(const tchar_t* str); };
}; } // namespace litehtml
}
#endif // LH_WEB_COLOR_H #endif // LH_WEB_COLOR_H

View file

@ -1,76 +1,70 @@
#include "html.h"
#include "background.h" #include "background.h"
litehtml::background::background() #include "html.h"
{
m_attachment = background_attachment_scroll; litehtml::background::background() {
m_repeat = background_repeat_repeat; m_attachment = background_attachment_scroll;
m_clip = background_box_border; m_repeat = background_repeat_repeat;
m_origin = background_box_padding; m_clip = background_box_border;
m_color.alpha = 0; m_origin = background_box_padding;
m_color.red = 0; m_color.alpha = 0;
m_color.green = 0; m_color.red = 0;
m_color.blue = 0; m_color.green = 0;
m_color.blue = 0;
} }
litehtml::background::background( const background& val ) litehtml::background::background(const background& val) {
{ m_image = val.m_image;
m_image = val.m_image; m_baseurl = val.m_baseurl;
m_baseurl = val.m_baseurl; m_color = val.m_color;
m_color = val.m_color; m_attachment = val.m_attachment;
m_attachment = val.m_attachment; m_position = val.m_position;
m_position = val.m_position; m_repeat = val.m_repeat;
m_repeat = val.m_repeat; m_clip = val.m_clip;
m_clip = val.m_clip; m_origin = val.m_origin;
m_origin = val.m_origin;
} }
litehtml::background& litehtml::background::operator=( const background& val ) litehtml::background& litehtml::background::operator=(const background& val) {
{ m_image = val.m_image;
m_image = val.m_image; m_baseurl = val.m_baseurl;
m_baseurl = val.m_baseurl; m_color = val.m_color;
m_color = val.m_color; m_attachment = val.m_attachment;
m_attachment = val.m_attachment; m_position = val.m_position;
m_position = val.m_position; m_repeat = val.m_repeat;
m_repeat = val.m_repeat; m_clip = val.m_clip;
m_clip = val.m_clip; m_origin = val.m_origin;
m_origin = val.m_origin; return *this;
return *this;
} }
litehtml::background_paint::background_paint() : color(0, 0, 0, 0) {
litehtml::background_paint::background_paint() : color(0, 0, 0, 0) position_x = 0;
{ position_y = 0;
position_x = 0; attachment = background_attachment_scroll;
position_y = 0; repeat = background_repeat_repeat;
attachment = background_attachment_scroll; is_root = false;
repeat = background_repeat_repeat;
is_root = false;
} }
litehtml::background_paint::background_paint( const background_paint& val ) litehtml::background_paint::background_paint(const background_paint& val) {
{ image = val.image;
image = val.image; baseurl = val.baseurl;
baseurl = val.baseurl; attachment = val.attachment;
attachment = val.attachment; repeat = val.repeat;
repeat = val.repeat; color = val.color;
color = val.color; clip_box = val.clip_box;
clip_box = val.clip_box; origin_box = val.origin_box;
origin_box = val.origin_box; border_box = val.border_box;
border_box = val.border_box; border_radius = val.border_radius;
border_radius = val.border_radius; image_size = val.image_size;
image_size = val.image_size; position_x = val.position_x;
position_x = val.position_x; position_y = val.position_y;
position_y = val.position_y; is_root = val.is_root;
is_root = val.is_root;
} }
litehtml::background_paint& litehtml::background_paint::operator=( const background& val ) litehtml::background_paint& litehtml::background_paint::operator=(const background& val) {
{ attachment = val.m_attachment;
attachment = val.m_attachment; baseurl = val.m_baseurl;
baseurl = val.m_baseurl; image = val.m_image;
image = val.m_image; repeat = val.m_repeat;
repeat = val.m_repeat; color = val.m_color;
color = val.m_color; return *this;
return *this;
} }

View file

@ -1,432 +1,331 @@
#include "html.h"
#include "box.h" #include "box.h"
#include "html.h"
#include "html_tag.h" #include "html_tag.h"
litehtml::box_type litehtml::block_box::get_type() const { return box_block; }
litehtml::box_type litehtml::block_box::get_type() const int litehtml::block_box::height() const { return m_element->height(); }
{
return box_block; int litehtml::block_box::width() const { return m_element->width(); }
void litehtml::block_box::add_element(const element::ptr& el) {
m_element = el;
el->m_box = this;
} }
int litehtml::block_box::height() const void litehtml::block_box::finish(bool last_box) {
{ if (!m_element) return;
return m_element->height(); m_element->apply_relative_shift(m_box_right - m_box_left);
} }
int litehtml::block_box::width() const bool litehtml::block_box::can_hold(const element::ptr& el, white_space ws) const {
{ if (m_element || el->is_inline_box()) {
return m_element->width(); return false;
}
return true;
} }
void litehtml::block_box::add_element(const element::ptr &el) bool litehtml::block_box::is_empty() const {
{ if (m_element) {
m_element = el; return false;
el->m_box = this; }
return true;
} }
void litehtml::block_box::finish(bool last_box) int litehtml::block_box::baseline() const {
{ if (m_element) {
if(!m_element) return; return m_element->get_base_line();
m_element->apply_relative_shift(m_box_right - m_box_left); }
return 0;
} }
bool litehtml::block_box::can_hold(const element::ptr &el, white_space ws) const void litehtml::block_box::get_elements(elements_vector& els) { els.push_back(m_element); }
{
if(m_element || el->is_inline_box()) int litehtml::block_box::top_margin() const {
{ if (m_element && m_element->collapse_top_margin()) {
return false; return m_element->m_margins.top;
} }
return true; return 0;
} }
bool litehtml::block_box::is_empty() const int litehtml::block_box::bottom_margin() const {
{ if (m_element && m_element->collapse_bottom_margin()) {
if(m_element) return m_element->m_margins.bottom;
{ }
return false; return 0;
}
return true;
} }
int litehtml::block_box::baseline() const void litehtml::block_box::y_shift(int shift) {
{ m_box_top += shift;
if(m_element) if (m_element) {
{ m_element->m_pos.y += shift;
return m_element->get_base_line(); }
}
return 0;
} }
void litehtml::block_box::get_elements( elements_vector& els ) void litehtml::block_box::new_width(int left, int right, elements_vector& els) {}
{
els.push_back(m_element);
}
int litehtml::block_box::top_margin() const
{
if(m_element && m_element->collapse_top_margin())
{
return m_element->m_margins.top;
}
return 0;
}
int litehtml::block_box::bottom_margin() const
{
if(m_element && m_element->collapse_bottom_margin())
{
return m_element->m_margins.bottom;
}
return 0;
}
void litehtml::block_box::y_shift( int shift )
{
m_box_top += shift;
if(m_element)
{
m_element->m_pos.y += shift;
}
}
void litehtml::block_box::new_width( int left, int right, elements_vector& els )
{
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
litehtml::box_type litehtml::line_box::get_type() const litehtml::box_type litehtml::line_box::get_type() const { return box_line; }
{
return box_line; int litehtml::line_box::height() const { return m_height; }
int litehtml::line_box::width() const { return m_width; }
void litehtml::line_box::add_element(const element::ptr& el) {
el->m_skip = false;
el->m_box = nullptr;
bool add = true;
if ((m_items.empty() && el->is_white_space()) || el->is_break()) {
el->m_skip = true;
} else if (el->is_white_space()) {
if (have_last_space()) {
add = false;
el->m_skip = true;
}
}
if (add) {
el->m_box = this;
m_items.push_back(el);
if (!el->m_skip) {
int el_shift_left = el->get_inline_shift_left();
int el_shift_right = el->get_inline_shift_right();
el->m_pos.x = m_box_left + m_width + el_shift_left + el->content_margins_left();
el->m_pos.y = m_box_top + el->content_margins_top();
m_width += el->width() + el_shift_left + el_shift_right;
}
}
} }
int litehtml::line_box::height() const void litehtml::line_box::finish(bool last_box) {
{ if (is_empty() || (!is_empty() && last_box && is_break_only())) {
return m_height; m_height = 0;
return;
}
for (auto i = m_items.rbegin(); i != m_items.rend(); i++) {
if ((*i)->is_white_space() || (*i)->is_break()) {
if (!(*i)->m_skip) {
(*i)->m_skip = true;
m_width -= (*i)->width();
}
} else {
break;
}
}
int base_line = m_font_metrics.base_line();
int line_height = m_line_height;
int add_x = 0;
switch (m_text_align) {
case text_align_right:
if (m_width < (m_box_right - m_box_left)) {
add_x = (m_box_right - m_box_left) - m_width;
}
break;
case text_align_center:
if (m_width < (m_box_right - m_box_left)) {
add_x = ((m_box_right - m_box_left) - m_width) / 2;
}
break;
default:
add_x = 0;
}
m_height = 0;
// find line box baseline and line-height
for (const auto& el : m_items) {
if (el->get_display() == display_inline_text) {
font_metrics fm;
el->get_font(&fm);
base_line = std::max(base_line, fm.base_line());
line_height = std::max(line_height, el->line_height());
m_height = std::max(m_height, fm.height);
}
el->m_pos.x += add_x;
}
if (m_height) {
base_line += (line_height - m_height) / 2;
}
m_height = line_height;
int y1 = 0;
int y2 = m_height;
for (const auto& el : m_items) {
if (el->get_display() == display_inline_text) {
font_metrics fm;
el->get_font(&fm);
el->m_pos.y = m_height - base_line - fm.ascent;
} else {
switch (el->get_vertical_align()) {
case va_super:
case va_sub:
case va_baseline:
el->m_pos.y = m_height - base_line - el->height() + el->get_base_line() + el->content_margins_top();
break;
case va_top:
el->m_pos.y = y1 + el->content_margins_top();
break;
case va_text_top:
el->m_pos.y = m_height - base_line - m_font_metrics.ascent + el->content_margins_top();
break;
case va_middle:
el->m_pos.y = m_height - base_line - m_font_metrics.x_height / 2 - el->height() / 2 + el->content_margins_top();
break;
case va_bottom:
el->m_pos.y = y2 - el->height() + el->content_margins_top();
break;
case va_text_bottom:
el->m_pos.y = m_height - base_line + m_font_metrics.descent - el->height() + el->content_margins_top();
break;
}
y1 = std::min(y1, el->top());
y2 = std::max(y2, el->bottom());
}
}
for (const auto& el : m_items) {
el->m_pos.y -= y1;
el->m_pos.y += m_box_top;
if (el->get_display() != display_inline_text) {
switch (el->get_vertical_align()) {
case va_top:
el->m_pos.y = m_box_top + el->content_margins_top();
break;
case va_bottom:
el->m_pos.y = m_box_top + (y2 - y1) - el->height() + el->content_margins_top();
break;
case va_baseline:
// TODO: process vertical align "baseline"
break;
case va_middle:
// TODO: process vertical align "middle"
break;
case va_sub:
// TODO: process vertical align "sub"
break;
case va_super:
// TODO: process vertical align "super"
break;
case va_text_bottom:
// TODO: process vertical align "text-bottom"
break;
case va_text_top:
// TODO: process vertical align "text-top"
break;
}
}
el->apply_relative_shift(m_box_right - m_box_left);
}
m_height = y2 - y1;
m_baseline = (base_line - y1) - (m_height - line_height);
} }
int litehtml::line_box::width() const bool litehtml::line_box::can_hold(const element::ptr& el, white_space ws) const {
{ if (!el->is_inline_box()) return false;
return m_width;
if (el->is_break()) {
return false;
}
if (ws == white_space_nowrap || ws == white_space_pre) {
return true;
}
if (m_box_left + m_width + el->width() + el->get_inline_shift_left() + el->get_inline_shift_right() > m_box_right) {
return false;
}
return true;
} }
void litehtml::line_box::add_element(const element::ptr &el) bool litehtml::line_box::have_last_space() const {
{ bool ret = false;
el->m_skip = false; for (auto i = m_items.rbegin(); i != m_items.rend() && !ret; i++) {
el->m_box = nullptr; if ((*i)->is_white_space() || (*i)->is_break()) {
bool add = true; ret = true;
if( (m_items.empty() && el->is_white_space()) || el->is_break() ) } else {
{ break;
el->m_skip = true; }
} else if(el->is_white_space()) }
{ return ret;
if (have_last_space())
{
add = false;
el->m_skip = true;
}
}
if(add)
{
el->m_box = this;
m_items.push_back(el);
if(!el->m_skip)
{
int el_shift_left = el->get_inline_shift_left();
int el_shift_right = el->get_inline_shift_right();
el->m_pos.x = m_box_left + m_width + el_shift_left + el->content_margins_left();
el->m_pos.y = m_box_top + el->content_margins_top();
m_width += el->width() + el_shift_left + el_shift_right;
}
}
} }
void litehtml::line_box::finish(bool last_box) bool litehtml::line_box::is_empty() const {
{ if (m_items.empty()) return true;
if( is_empty() || (!is_empty() && last_box && is_break_only()) ) for (auto i = m_items.rbegin(); i != m_items.rend(); i++) {
{ if (!(*i)->m_skip || (*i)->is_break()) {
m_height = 0; return false;
return; }
} }
return true;
for(auto i = m_items.rbegin(); i != m_items.rend(); i++)
{
if((*i)->is_white_space() || (*i)->is_break())
{
if(!(*i)->m_skip)
{
(*i)->m_skip = true;
m_width -= (*i)->width();
}
} else
{
break;
}
}
int base_line = m_font_metrics.base_line();
int line_height = m_line_height;
int add_x = 0;
switch(m_text_align)
{
case text_align_right:
if(m_width < (m_box_right - m_box_left))
{
add_x = (m_box_right - m_box_left) - m_width;
}
break;
case text_align_center:
if(m_width < (m_box_right - m_box_left))
{
add_x = ((m_box_right - m_box_left) - m_width) / 2;
}
break;
default:
add_x = 0;
}
m_height = 0;
// find line box baseline and line-height
for(const auto& el : m_items)
{
if(el->get_display() == display_inline_text)
{
font_metrics fm;
el->get_font(&fm);
base_line = std::max(base_line, fm.base_line());
line_height = std::max(line_height, el->line_height());
m_height = std::max(m_height, fm.height);
}
el->m_pos.x += add_x;
}
if(m_height)
{
base_line += (line_height - m_height) / 2;
}
m_height = line_height;
int y1 = 0;
int y2 = m_height;
for (const auto& el : m_items)
{
if(el->get_display() == display_inline_text)
{
font_metrics fm;
el->get_font(&fm);
el->m_pos.y = m_height - base_line - fm.ascent;
} else
{
switch(el->get_vertical_align())
{
case va_super:
case va_sub:
case va_baseline:
el->m_pos.y = m_height - base_line - el->height() + el->get_base_line() + el->content_margins_top();
break;
case va_top:
el->m_pos.y = y1 + el->content_margins_top();
break;
case va_text_top:
el->m_pos.y = m_height - base_line - m_font_metrics.ascent + el->content_margins_top();
break;
case va_middle:
el->m_pos.y = m_height - base_line - m_font_metrics.x_height / 2 - el->height() / 2 + el->content_margins_top();
break;
case va_bottom:
el->m_pos.y = y2 - el->height() + el->content_margins_top();
break;
case va_text_bottom:
el->m_pos.y = m_height - base_line + m_font_metrics.descent - el->height() + el->content_margins_top();
break;
}
y1 = std::min(y1, el->top());
y2 = std::max(y2, el->bottom());
}
}
for (const auto& el : m_items)
{
el->m_pos.y -= y1;
el->m_pos.y += m_box_top;
if(el->get_display() != display_inline_text)
{
switch(el->get_vertical_align())
{
case va_top:
el->m_pos.y = m_box_top + el->content_margins_top();
break;
case va_bottom:
el->m_pos.y = m_box_top + (y2 - y1) - el->height() + el->content_margins_top();
break;
case va_baseline:
//TODO: process vertical align "baseline"
break;
case va_middle:
//TODO: process vertical align "middle"
break;
case va_sub:
//TODO: process vertical align "sub"
break;
case va_super:
//TODO: process vertical align "super"
break;
case va_text_bottom:
//TODO: process vertical align "text-bottom"
break;
case va_text_top:
//TODO: process vertical align "text-top"
break;
}
}
el->apply_relative_shift(m_box_right - m_box_left);
}
m_height = y2 - y1;
m_baseline = (base_line - y1) - (m_height - line_height);
} }
bool litehtml::line_box::can_hold(const element::ptr &el, white_space ws) const int litehtml::line_box::baseline() const { return m_baseline; }
{
if(!el->is_inline_box()) return false;
if(el->is_break()) void litehtml::line_box::get_elements(elements_vector& els) { els.insert(els.begin(), m_items.begin(), m_items.end()); }
{
return false;
}
if(ws == white_space_nowrap || ws == white_space_pre) int litehtml::line_box::top_margin() const { return 0; }
{
return true;
}
if(m_box_left + m_width + el->width() + el->get_inline_shift_left() + el->get_inline_shift_right() > m_box_right) int litehtml::line_box::bottom_margin() const { return 0; }
{
return false;
}
return true; void litehtml::line_box::y_shift(int shift) {
m_box_top += shift;
for (auto& el : m_items) {
el->m_pos.y += shift;
}
} }
bool litehtml::line_box::have_last_space() const bool litehtml::line_box::is_break_only() const {
{ if (m_items.empty()) return true;
bool ret = false;
for (auto i = m_items.rbegin(); i != m_items.rend() && !ret; i++) if (m_items.front()->is_break()) {
{ for (auto& el : m_items) {
if((*i)->is_white_space() || (*i)->is_break()) if (!el->m_skip) {
{ return false;
ret = true; }
} else }
{ return true;
break; }
} return false;
}
return ret;
} }
bool litehtml::line_box::is_empty() const void litehtml::line_box::new_width(int left, int right, elements_vector& els) {
{ int add = left - m_box_left;
if(m_items.empty()) return true; if (add) {
for (auto i = m_items.rbegin(); i != m_items.rend(); i++) m_box_left = left;
{ m_box_right = right;
if(!(*i)->m_skip || (*i)->is_break()) m_width = 0;
{ auto remove_begin = m_items.end();
return false; for (auto i = m_items.begin() + 1; i != m_items.end(); i++) {
} element::ptr el = (*i);
}
return true; if (!el->m_skip) {
if (m_box_left + m_width + el->width() + el->get_inline_shift_right() + el->get_inline_shift_left() > m_box_right) {
remove_begin = i;
break;
} else {
el->m_pos.x += add;
m_width += el->width() + el->get_inline_shift_right() + el->get_inline_shift_left();
}
}
}
if (remove_begin != m_items.end()) {
els.insert(els.begin(), remove_begin, m_items.end());
m_items.erase(remove_begin, m_items.end());
for (const auto& el : els) {
el->m_box = nullptr;
}
}
}
} }
int litehtml::line_box::baseline() const
{
return m_baseline;
}
void litehtml::line_box::get_elements( elements_vector& els )
{
els.insert(els.begin(), m_items.begin(), m_items.end());
}
int litehtml::line_box::top_margin() const
{
return 0;
}
int litehtml::line_box::bottom_margin() const
{
return 0;
}
void litehtml::line_box::y_shift( int shift )
{
m_box_top += shift;
for (auto& el : m_items)
{
el->m_pos.y += shift;
}
}
bool litehtml::line_box::is_break_only() const
{
if(m_items.empty()) return true;
if(m_items.front()->is_break())
{
for (auto& el : m_items)
{
if(!el->m_skip)
{
return false;
}
}
return true;
}
return false;
}
void litehtml::line_box::new_width( int left, int right, elements_vector& els )
{
int add = left - m_box_left;
if(add)
{
m_box_left = left;
m_box_right = right;
m_width = 0;
auto remove_begin = m_items.end();
for (auto i = m_items.begin() + 1; i != m_items.end(); i++)
{
element::ptr el = (*i);
if(!el->m_skip)
{
if(m_box_left + m_width + el->width() + el->get_inline_shift_right() + el->get_inline_shift_left() > m_box_right)
{
remove_begin = i;
break;
} else
{
el->m_pos.x += add;
m_width += el->width() + el->get_inline_shift_right() + el->get_inline_shift_left();
}
}
}
if(remove_begin != m_items.end())
{
els.insert(els.begin(), remove_begin, m_items.end());
m_items.erase(remove_begin, m_items.end());
for(const auto& el : els)
{
el->m_box = nullptr;
}
}
}
}

View file

@ -33,50 +33,37 @@
namespace { namespace {
bool lookup(const uint32_t* table, litehtml::tchar_t c) bool lookup(const uint32_t* table, litehtml::tchar_t c) { return table[c >> 5] & (1 << (c & 0x1f)); }
{
return table[c >> 5] & (1 << (c & 0x1f));
}
} // namespace } // namespace
namespace litehtml { namespace litehtml {
bool is_ascii_codepoint(litehtml::tchar_t c) bool is_ascii_codepoint(litehtml::tchar_t c) { return (c < 128); }
{
return (c < 128);
}
// https://datatracker.ietf.org/doc/html/rfc3986#section-2.2 // https://datatracker.ietf.org/doc/html/rfc3986#section-2.2
bool is_url_reserved_codepoint(litehtml::tchar_t c) bool is_url_reserved_codepoint(litehtml::tchar_t c) {
{ static const uint32_t reserved_lookup[] = {0x00000000, 0xac009fda, 0x28000001, 0x00000000};
static const uint32_t reserved_lookup[] = {
0x00000000,
0xac009fda,
0x28000001,
0x00000000
};
if (!is_ascii_codepoint(c)) { if (!is_ascii_codepoint(c)) {
return false; return false;
} }
return lookup(reserved_lookup, c); return lookup(reserved_lookup, c);
} }
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.1 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.1
bool is_url_scheme_codepoint(litehtml::tchar_t c) bool is_url_scheme_codepoint(litehtml::tchar_t c) {
{ static const uint32_t scheme_lookup[] = {
static const uint32_t scheme_lookup[] = { 0x00000000,
0x00000000, 0x03ff6800,
0x03ff6800, 0x07fffffe,
0x07fffffe, 0x07fffffe,
0x07fffffe, };
};
if (!is_ascii_codepoint(c)) { if (!is_ascii_codepoint(c)) {
return false; return false;
} }
return lookup(scheme_lookup, c); return lookup(scheme_lookup, c);
} }
} // namespace litehtml } // namespace litehtml

View file

@ -1,12 +1,11 @@
#include "html.h"
#include "context.h" #include "context.h"
#include "html.h"
#include "stylesheet.h" #include "stylesheet.h"
void litehtml::context::load_master_stylesheet(const tchar_t* str) {
media_query_list::ptr media;
void litehtml::context::load_master_stylesheet( const tchar_t* str ) m_master_css.parse_stylesheet(str, nullptr, std::shared_ptr<litehtml::document>(), media_query_list::ptr());
{ m_master_css.sort_selectors();
media_query_list::ptr media;
m_master_css.parse_stylesheet(str, nullptr, std::shared_ptr<litehtml::document>(), media_query_list::ptr());
m_master_css.sort_selectors();
} }

View file

@ -1,54 +1,44 @@
#include "html.h"
#include "css_length.h" #include "css_length.h"
void litehtml::css_length::fromString( const tstring& str, const tstring& predefs, int defValue ) #include "html.h"
{
// TODO: Make support for calc
if(str.substr(0, 4) == _t("calc"))
{
m_is_predefined = true;
m_predef = 0;
return;
}
int predef = value_index(str, predefs, -1); void litehtml::css_length::fromString(const tstring& str, const tstring& predefs, int defValue) {
if(predef >= 0) // TODO: Make support for calc
{ if (str.substr(0, 4) == _t("calc")) {
m_is_predefined = true; m_is_predefined = true;
m_predef = predef; m_predef = 0;
} else return;
{ }
m_is_predefined = false;
tstring num; int predef = value_index(str, predefs, -1);
tstring un; if (predef >= 0) {
bool is_unit = false; m_is_predefined = true;
for(tchar_t chr : str) m_predef = predef;
{ } else {
if(!is_unit) m_is_predefined = false;
{
if(t_isdigit(chr) || chr == _t('.') || chr == _t('+') || chr == _t('-')) tstring num;
{ tstring un;
num += chr; bool is_unit = false;
} else for (tchar_t chr : str) {
{ if (!is_unit) {
is_unit = true; if (t_isdigit(chr) || chr == _t('.') || chr == _t('+') || chr == _t('-')) {
} num += chr;
} } else {
if(is_unit) is_unit = true;
{ }
un += chr; }
} if (is_unit) {
} un += chr;
if(!num.empty()) }
{ }
m_value = (float) t_strtod(num.c_str(), nullptr); if (!num.empty()) {
m_units = (css_units) value_index(un, css_units_strings, css_units_none); m_value = (float)t_strtod(num.c_str(), nullptr);
} else m_units = (css_units)value_index(un, css_units_strings, css_units_none);
{ } else {
// not a number so it is predefined // not a number so it is predefined
m_is_predefined = true; m_is_predefined = true;
m_predef = defValue; m_predef = defValue;
} }
} }
} }

Some files were not shown because too many files have changed in this diff Show more