Skip to content

Commit

Permalink
Move all text related functions from laf-os to a new laf-text module
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Feb 20, 2024
1 parent 2f4c6fd commit be8bfe3
Show file tree
Hide file tree
Showing 43 changed files with 777 additions and 657 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# LAF
# Copyright (C) 2019-2022 Igara Studio S.A.
# Copyright (C) 2019-2024 Igara Studio S.A.
# Copyright (C) 2016-2018 David Capello

cmake_minimum_required(VERSION 3.16)
Expand Down Expand Up @@ -58,6 +58,7 @@ if(FREETYPE_LIBRARIES AND HARFBUZZ_LIBRARIES)
add_subdirectory(ft)
endif()
add_subdirectory(os)
add_subdirectory(text)
if(LAF_WITH_EXAMPLES)
add_subdirectory(examples)
endif()
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ in the future if they are highly valuable on their own.
`freetype` library as dependency). Might be deleted in the future
(replaced with Skia text rendering)
* [os](os): Functions to create windows in your Operating System
* [text](text): Functions to draw text

## Platform-specific Definitions

Expand Down
4 changes: 4 additions & 0 deletions docs/text/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# laf-text

`text` is the laf module to draw/layout/shape text on [laf-os](../os)
windows.
4 changes: 2 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# LAF OS
# Copyright (C) 2019-2022 Igara Studio S.A.
# Copyright (C) 2019-2024 Igara Studio S.A.

if(LAF_BACKEND STREQUAL "skia")
function(laf_add_example name console="")
Expand All @@ -8,7 +8,7 @@ if(LAF_BACKEND STREQUAL "skia")
else()
add_executable(${name} WIN32 ${name}.cpp)
endif()
target_link_libraries(${name} laf-os)
target_link_libraries(${name} laf-os laf-text)
set_target_properties(${name} PROPERTIES LINK_FLAGS "${LAF_BACKEND_LINK_FLAGS}")
endfunction()

Expand Down
112 changes: 59 additions & 53 deletions examples/allevents.cpp
Original file line number Diff line number Diff line change
@@ -1,60 +1,65 @@
// LAF Library
// Copyright (c) 2019-2022 Igara Studio S.A.
// Copyright (c) 2019-2024 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.

#include "gfx/hsv.h"
#include "gfx/rgb.h"
#include "os/os.h"
#include "text/text.h"

#include <algorithm>
#include <cstdarg>
#include <cstdlib>
#include <string>
#include <vector>

using namespace os;
using namespace text;

class LogWindow {
public:
LogWindow(os::System* system)
: m_window(system->makeWindow(800, 600)) {
LogWindow(System* system)
: m_window(system->makeWindow(800, 600))
, m_font(FontMgr::Make()->defaultFont(12)) {
m_window->setTitle("All Events");

recalcMaxLines();

logLine("-- Events Log --");
}

bool processEvent(const os::Event& ev) {
bool processEvent(const Event& ev) {
switch (ev.type()) {

case os::Event::CloseApp:
case os::Event::CloseWindow:
case Event::CloseApp:
case Event::CloseWindow:
return false;

case os::Event::ResizeWindow:
case Event::ResizeWindow:
logLine("ResizeWindow size=%d,%d",
m_window->width(),
m_window->height());
recalcMaxLines();
break;

case os::Event::DropFiles:
case Event::DropFiles:
logLine("DropFiles files={");
for (const auto& file : ev.files()) {
logLine(" \"%s\"", file.c_str());
}
logLine("}");
break;

case os::Event::MouseEnter: logMouseEvent(ev, "MouseEnter"); break;
case os::Event::MouseLeave: logMouseEvent(ev, "MouseLeave"); break;
case os::Event::MouseMove: logMouseEvent(ev, "MouseMove"); break;
case os::Event::MouseDown: logMouseEvent(ev, "MouseDown"); break;
case os::Event::MouseUp: logMouseEvent(ev, "MouseUp"); break;
case os::Event::MouseDoubleClick: logMouseEvent(ev, "MouseDoubleClick"); break;
case Event::MouseEnter: logMouseEvent(ev, "MouseEnter"); break;
case Event::MouseLeave: logMouseEvent(ev, "MouseLeave"); break;
case Event::MouseMove: logMouseEvent(ev, "MouseMove"); break;
case Event::MouseDown: logMouseEvent(ev, "MouseDown"); break;
case Event::MouseUp: logMouseEvent(ev, "MouseUp"); break;
case Event::MouseDoubleClick: logMouseEvent(ev, "MouseDoubleClick"); break;

case os::Event::MouseWheel:
case Event::MouseWheel:
m_mousePos = ev.position();
logLine("MouseWheel pos=%d,%d %s=%d,%d%s",
ev.position().x,
Expand All @@ -66,8 +71,8 @@ class LogWindow {
m_hue += double(ev.wheelDelta().x + ev.wheelDelta().y);
break;

case os::Event::KeyDown:
if (ev.scancode() == os::kKeyEsc) {
case Event::KeyDown:
if (ev.scancode() == kKeyEsc) {
if (m_nextEscCloses)
return false;
else
Expand All @@ -78,10 +83,10 @@ class LogWindow {
m_nextEscCloses = false;
}
//[[fallthrough]];
case os::Event::KeyUp: {
case Event::KeyUp: {
wchar_t wideUnicode[2] = { ev.unicodeChar(), 0 };
logLine("%s repeat=%d scancode=%d unicode=%d (%s)%s",
(ev.type() == os::Event::KeyDown ? "KeyDown": "KeyUp"),
(ev.type() == Event::KeyDown ? "KeyDown": "KeyUp"),
ev.repeat(),
ev.scancode(),
ev.unicodeChar(),
Expand All @@ -90,7 +95,7 @@ class LogWindow {
break;
}

case os::Event::TouchMagnify:
case Event::TouchMagnify:
logLine("TouchMagnify %.4g",
ev.magnification());
m_brushSize += 32*ev.magnification();
Expand Down Expand Up @@ -122,12 +127,12 @@ class LogWindow {
}

void scrollAndDrawLog(const int newlines) {
os::Surface* surface = m_window->surface();
os::SurfaceLock lock(surface);
Surface* surface = m_window->surface();
SurfaceLock lock(surface);
const gfx::Rect rc = surface->bounds();

os::Paint p;
p.style(os::Paint::Fill);
Paint p;
p.style(Paint::Fill);
p.color(gfx::rgba(0, 0, 0, 8));

// Scroll old lines
Expand All @@ -148,11 +153,11 @@ class LogWindow {
surface->drawRect(gfx::Rect(rc.x, rc.y, rc.w, i*m_lineHeight), p);
}

os::Paint paint;
Paint paint;
paint.color(gfx::rgba(255, 255, 255));
for (; i<m_textLog.size(); ++i)
os::draw_text(surface, nullptr, m_textLog[i],
gfx::Point(0, (1+i)*m_lineHeight), &paint);
draw_text(surface, m_font, m_textLog[i],
gfx::Point(0, (1+i)*m_lineHeight), &paint);

gfx::Rgb rgb(gfx::Hsv(m_hue, 1.0, 1.0));
paint.color(gfx::rgba(rgb.red(), rgb.green(), rgb.blue()));
Expand All @@ -166,26 +171,26 @@ class LogWindow {
m_window->setVisible(true);
}

void logMouseEvent(const os::Event& ev, const char* eventName) {
const os::Event::MouseButton mb = ev.button();
const os::PointerType pt = ev.pointerType();
void logMouseEvent(const Event& ev, const char* eventName) {
const Event::MouseButton mb = ev.button();
const PointerType pt = ev.pointerType();

m_mousePos = ev.position();
logLine("%s pos=%d,%d%s%s%s",
eventName,
ev.position().x,
ev.position().y,
(mb == os::Event::LeftButton ? " LeftButton":
mb == os::Event::RightButton ? " RightButton":
mb == os::Event::MiddleButton ? " MiddleButton":
mb == os::Event::X1Button ? " X1Button":
mb == os::Event::X2Button ? " X2Button": ""),
(pt == os::PointerType::Mouse ? " Mouse":
pt == os::PointerType::Touchpad ? " Touchpad":
pt == os::PointerType::Touch ? " Touch":
pt == os::PointerType::Pen ? " Pen":
pt == os::PointerType::Cursor ? " Cursor":
pt == os::PointerType::Eraser ? " Eraser": ""),
(mb == Event::LeftButton ? " LeftButton":
mb == Event::RightButton ? " RightButton":
mb == Event::MiddleButton ? " MiddleButton":
mb == Event::X1Button ? " X1Button":
mb == Event::X2Button ? " X2Button": ""),
(pt == PointerType::Mouse ? " Mouse":
pt == PointerType::Touchpad ? " Touchpad":
pt == PointerType::Touch ? " Touch":
pt == PointerType::Pen ? " Pen":
pt == PointerType::Cursor ? " Cursor":
pt == PointerType::Eraser ? " Eraser": ""),
modifiersToString(ev.modifiers()).c_str());
}

Expand All @@ -199,18 +204,19 @@ class LogWindow {
m_textLog.push_back(buf);
}

static std::string modifiersToString(os::KeyModifiers mods) {
static std::string modifiersToString(KeyModifiers mods) {
std::string s;
if (mods & os::kKeyShiftModifier) s += " Shift";
if (mods & os::kKeyCtrlModifier ) s += " Ctrl";
if (mods & os::kKeyAltModifier ) s += " Alt";
if (mods & os::kKeyCmdModifier ) s += " Command";
if (mods & os::kKeySpaceModifier) s += " Space";
if (mods & os::kKeyWinModifier ) s += " Win";
if (mods & kKeyShiftModifier) s += " Shift";
if (mods & kKeyCtrlModifier ) s += " Ctrl";
if (mods & kKeyAltModifier ) s += " Alt";
if (mods & kKeyCmdModifier ) s += " Command";
if (mods & kKeySpaceModifier) s += " Space";
if (mods & kKeyWinModifier ) s += " Win";
return s;
}

os::WindowRef m_window;
WindowRef m_window;
FontRef m_font;
std::vector<std::string> m_textLog;
size_t m_oldLogSize = 0;
int m_lineHeight = 12;
Expand All @@ -223,19 +229,19 @@ class LogWindow {

int app_main(int argc, char* argv[])
{
auto system = os::make_system();
system->setAppMode(os::AppMode::GUI);
auto system = make_system();
system->setAppMode(AppMode::GUI);

LogWindow window(system.get());

system->finishLaunching();
system->activateApp();

os::EventQueue* queue = system->eventQueue();
EventQueue* queue = system->eventQueue();
while (true) {
window.flush();

os::Event ev;
Event ev;
queue->getEvent(ev);
if (!window.processEvent(ev))
break;
Expand Down
Loading

0 comments on commit be8bfe3

Please sign in to comment.