-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.cpp
137 lines (110 loc) · 3.18 KB
/
translator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "headers/translator.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <cairo/cairo.h>
#include <cairo/cairo-ft.h>
#include <cairo/cairo-pdf.h>
#include <freetype2/ft2build.h>
#include <freetype2/freetype/freetype.h>
#include <freetype2/freetype/ftsnames.h>
std::string text;
cairo_t *context;
int y=50;
void getLatin(char* source, char* destination){
getSource(source);
char* address=(char*)"support/SyneMono-Regular.ttf";
writeToDestination(destination, address, false);
}
void getRunic(char* source, char* destination){
getSource(source);
char* address=(char*)"support/futhark.ttf";
writeToDestination(destination, address, true);
}
void getSource(char* source){
FILE* f;
char* part= strtok(source,"\0");
f= fopen(part,"r");
fseek(f, 0, SEEK_END);
int length= ftell(f);
fseek(f, 0, SEEK_SET);
char buf[length];
fread(buf, length, sizeof(char), f);
fclose(f);
text=buf;
}
void writeToDestination(char* destination, char* fontAddress, bool toRunic){
char* part= strtok(destination,"\0");
cairo_surface_t *surface;
int height=648;
if(text.length()>35*55){
int dif= text.length()- 35*55;
dif/=55;
dif*=15;
height+=dif;
}
surface= cairo_pdf_surface_create(part, 504, height);
context= cairo_create(surface);
FT_Face face;
FT_Library library;
cairo_font_face_t *ff;
FT_Init_FreeType(&library);
FT_New_Face(library, fontAddress, 0, &face);
ff=cairo_ft_font_face_create_for_ft_face(face, 0);
cairo_set_font_face(context, ff);
cairo_set_font_size(context, 8.f);
cairo_set_source_rgb(context, 0, 0, 0);
cairo_move_to(context, 50, y);
if(toRunic)
textCorrection();
else
runicCorrection();
if(text.length()>55){
while (text.length()>0){
int limit= text.length()>=55?55:text.length();
cairo_show_text(context, text.substr(0, limit).c_str());
cairo_stroke(context);
y+=15;
cairo_move_to(context, 50, y);
text.replace(0, limit, "");
}
cairo_show_page(context);
}else{
cairo_show_text(context, text.c_str());
cairo_show_page(context);
}
cairo_surface_destroy(surface);
cairo_destroy(context);
}
void textCorrection(){
size_t pos;
while((pos=text.find("th"))!=std::string::npos){
text.replace(pos, 2, ":");
}
while((pos=text.find("ng"))!=std::string::npos){
text.replace(pos, 2, ";");
}
while((pos=text.find("x"))!=std::string::npos){
text.replace(pos, 1, "cs");
}
while((pos=text.find("q"))!=std::string::npos){
text.replace(pos, 1, "cj");
}
}
void runicCorrection(){
size_t pos;
while((pos=text.find(":"))!=std::string::npos){
text.replace(pos, 1, "th");
}
while((pos=text.find(";"))!=std::string::npos){
text.replace(pos, 1, "ng");
}
while((pos=text.find("cs"))!=std::string::npos){
text.replace(pos, 2, "x");
}
while((pos=text.find("cj"))!=std::string::npos){
text.replace(pos, 2, "q");
}
}