Skip to content

Commit

Permalink
feat: support script config font
Browse files Browse the repository at this point in the history
  • Loading branch information
zeromake committed Apr 21, 2024
1 parent e0fbe6a commit 8ce7689
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 82 deletions.
67 changes: 67 additions & 0 deletions src/FontConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,74 @@
#include "FontConfig.h"
#include <string.h>
#include <stdlib.h>

namespace ons_font {
const static FontConfig __DEFAULT_FONT_CONFIG =
FontConfig{0, 0, {0xff, 0xff, 0xff}, false, 1, {0x0, 0x0, 0x0}, 0, 0};
const FontConfig* DEFAULT_FONT_CONFIG() { return &__DEFAULT_FONT_CONFIG; }

const int readFontConfig(const char *buf, ons_font::FontConfig *cfg) {
ons_font::FONT_TYPE types = ons_font::GLOBAL_FONT;
int offset = 0;
if (buf[offset] >= '0' && buf[offset] <= '9') {
types = ons_font::FONT_TYPE(buf[offset] - '0');
}
offset++;
if (buf[offset] != ':') {
return 0;
}
offset++;
auto default_config = ons_font::DEFAULT_FONT_CONFIG();
memcpy(cfg, default_config, sizeof(ons_font::FontConfig));
int start = offset;
int field = 0;
char buff[256];
#define IS_FONT_CONFIG_END(c) (c == '\0' || c == ';' || c == ' ' || c == '\n')
while (1) {
if (buf[offset] == ',' || IS_FONT_CONFIG_END(buf[offset])) {
int size = offset - start;
if (size > 0) {
memcpy(buff, buf + start, size);
buff[size] = '\0';
} else {
buff[0] = '\0';
}
start = offset + 1;
if (buff[0] != '\0') {
switch (field) {
case 0:
cfg->size = atoi(buff);
break;
case 1:
cfg->size_ratio = atof(buff);
break;
case 2:
utils::readColor(buff, &cfg->color);
break;
case 3:
cfg->render_outline = buff[0] != '0' && buff[0] != 'f';
break;
case 4:
cfg->outline_size = atoi(buff);
break;
case 5:
readColor(buff, &cfg->outline_color);
break;
case 6:
cfg->offset_x = atoi(buff);
break;
case 7:
cfg->offset_y = atoi(buff);
break;
}
}
field++;
}
if (IS_FONT_CONFIG_END(buf[offset])) {
break;
}
offset++;
}
return offset;
}
}; // namespace ons_font
57 changes: 29 additions & 28 deletions src/FontConfig.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@

#ifndef __FONT_CONFIG_H__
#define __FONT_CONFIG_H__

namespace ons_font {
typedef unsigned char uchar3[3];
enum FONT_TYPE {
GLOBAL_FONT,
SENTENCE_FONT,
ANIM_FONT,
MENU_FONT,
DAILOG_FONT,
RUBY_FONT,
};
struct FontConfig {
int size;
float size_ratio;
uchar3 color;
bool render_outline;
int outline_size;
uchar3 outline_color;
int offset_x;
int offset_y;
};
const FontConfig* DEFAULT_FONT_CONFIG();
} // namespace ons_font

#endif

#ifndef __FONT_CONFIG_H__
#define __FONT_CONFIG_H__
#include "private/utils.h"

namespace ons_font {
enum FONT_TYPE {
GLOBAL_FONT,
SENTENCE_FONT,
ANIM_FONT,
MENU_FONT,
DAILOG_FONT,
RUBY_FONT,
};
struct FontConfig {
int size;
float size_ratio;
utils::uchar4 color;
bool render_outline;
int outline_size;
utils::uchar4 outline_color;
int offset_x;
int offset_y;
};
const FontConfig* DEFAULT_FONT_CONFIG();
const int readFontConfig(const char *buf, ons_font::FontConfig *cfg);
} // namespace ons_font

#endif
29 changes: 29 additions & 0 deletions src/ScriptHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ ScriptHandler::~ScriptHandler() {
delete[] str_string_buffer;
delete[] saved_string_buffer;
if (variable_data) delete[] variable_data;
for (int i = 0; i < 6; i++) {
if (font_configs[i] != nullptr) {
delete font_configs[i];
font_configs[i] = nullptr;
}
}
}

void ScriptHandler::reset() {
Expand Down Expand Up @@ -1269,6 +1275,26 @@ int ScriptHandler::readScriptSub(FILE *fp, char **buf, int encrypt_mode) {
return 0;
}

int ScriptHandler::setFontConfig(const char *buf) {
// printf("setFontConfig: %s\n", buf);
ons_font::FONT_TYPE types = ons_font::GLOBAL_FONT;
int offset = 0;
if (buf[offset] >= '0' && buf[offset] <= '9') {
types = ons_font::FONT_TYPE(buf[offset] - '0');
}
offset++;
if (buf[offset] != ':') {
return 0;
}
offset++;
ons_font::FontConfig *cfg = nullptr;
if (font_configs[types] == nullptr) {
font_configs[types] = new ons_font::FontConfig;
}
cfg = font_configs[types];
return ons_font::readFontConfig(buf, cfg);
}

void ScriptHandler::readConfiguration() {
variable_range = 4096;
global_variable_border = 200;
Expand Down Expand Up @@ -1371,6 +1397,9 @@ void ScriptHandler::readConfiguration() {
buf++;
SKIP_SPACE(buf);
while (*buf >= '0' && *buf <= '9') buf++;
} else if (*buf == 'f' || *buf == 'F') {
buf++;
buf += setFontConfig(buf);
} else if (!strncmp(buf, "ratio", 5)) {
int _user_ratio = 0;
buf += 5;
Expand Down
3 changes: 3 additions & 0 deletions src/ScriptHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <string>

#include "FontConfig.h"
#include "BaseReader.h"
#include "private/utils.h"
#include "resize/scale_manager.hpp"
Expand Down Expand Up @@ -264,6 +265,7 @@ class ScriptHandler {
struct VariableData current_variable_data;
onscripter::SharedPtr<onscripter::ScaleManager> screen_scale;
onscripter::SharedPtr<onscripter::ScaleManager> user_scale;
ons_font::FontConfig *font_configs[6] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};

private:
enum {
Expand Down Expand Up @@ -322,6 +324,7 @@ class ScriptHandler {
int readScriptSub(FILE *fp, char **buf, int encrypt_mode);
void readConfiguration();
int labelScript();
int setFontConfig(const char *buf);

int findLabel(const char *label);

Expand Down
61 changes: 9 additions & 52 deletions src/ScriptParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,72 +122,23 @@ const ons_font::FontConfig *ScriptParser::getFontConfig(
return ons_font::DEFAULT_FONT_CONFIG();
}

void ScriptParser::setFontConfig(const char *buf) {
int ScriptParser::setFontConfig(const char *buf) {
ons_font::FONT_TYPE types = ons_font::GLOBAL_FONT;
int offset = 0;
if (buf[offset] >= '0' && buf[offset] <= '9') {
types = ons_font::FONT_TYPE(buf[offset] - '0');
}
offset++;
if (buf[offset] != ':') {
return;
return 0;
}
offset++;
ons_font::FontConfig *cfg = nullptr;
if (font_configs[types] == nullptr) {
font_configs[types] = new ons_font::FontConfig;
}
cfg = font_configs[types];
auto default_config = ons_font::DEFAULT_FONT_CONFIG();
memcpy(cfg, default_config, sizeof(ons_font::FontConfig));
int start = offset;
int field = 0;
char buff[256];
while (1) {
if (buf[offset] == ',' || buf[offset] == '\0') {
int size = offset - start;
if (size > 0) {
memcpy(buff, buf + start, size);
buff[size] = '\0';
} else {
buff[0] = '\0';
}
start = offset + 1;
if (buff[0] != '\0') {
switch (field) {
case 0:
cfg->size = atoi(buff);
break;
case 1:
cfg->size_ratio = atof(buff);
break;
case 2:
readColor(&cfg->color, buff);
break;
case 3:
cfg->render_outline = buff[0] != '0' && buff[0] != 'f';
break;
case 4:
cfg->outline_size = atoi(buff);
break;
case 5:
readColor(&cfg->outline_color, buff);
break;
case 6:
cfg->offset_x = atoi(buff);
break;
case 7:
cfg->offset_y = atoi(buff);
break;
}
}
field++;
}
if (buf[offset] == '\0') {
break;
}
offset++;
}
return ons_font::readFontConfig(buf, cfg);
}

const int ScriptParser::calcFontSize(const int v, ons_font::FONT_TYPE types) {
Expand Down Expand Up @@ -397,6 +348,12 @@ int ScriptParser::openScript() {
if (!init_screen_ratio) {
*screen_scale = script_h.screen_scale;
}
for (int i = 0; i < 6; i++) {
if (script_h.font_configs[i] != nullptr && font_configs[i] == nullptr) {
font_configs[i] = new ons_font::FontConfig{0};
memcpy(font_configs[i], script_h.font_configs[i], sizeof(ons_font::FontConfig));
}
}
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ScriptParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class ScriptParser {
int addCommand();
int elseCommand();
void setRescale(int scale1, int scale2);
void setFontConfig(const char *buf);
int setFontConfig(const char *buf);

protected:
struct UserFuncLUT {
Expand Down
6 changes: 5 additions & 1 deletion src/onscripter/ONScripter_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ void ONScripter::drawGlyph(SDL_Surface *dst_surface,
(TTF_Font *)info->ttf_font[0], unicode, fcol, bcol);
const ons_font::FontConfig *cfg = getFontConfig(info->types);
SDL_Color scolor = {
cfg->outline_color[0], cfg->outline_color[1], cfg->outline_color[2]};
cfg->outline_color.rgba[0],
cfg->outline_color.rgba[1],
cfg->outline_color.rgba[2],
cfg->outline_color.rgba[3],
};
SDL_Surface *tmp_surface_s = tmp_surface;
if (info->is_shadow && fontConfig->render_outline) {
tmp_surface_s = TTF_RenderGlyph_Shaded(
Expand Down

0 comments on commit 8ce7689

Please sign in to comment.