string addBoldTag(string s, vector<string>& dict) { int len = s.size(); vector<int> is_bold(len, 0); int pos = -1; for (auto word : dict) { do { pos = s.find(word, pos+1); if (pos != string::npos) { for (int i = pos; i < pos + word.size(); ++i) { is_bold[i] = 1; } } } while (pos != string::npos); } string result; bool b = false; for (int i = 0; i < len; ++i) { if (!is_bold[i]) { if (b) { result += "</b>"; b = false; } } else { if (!b) { result += "<b>"; b = true; } } result += s[i]; } if (b) result += "</b>"; return result; }
static void svg_text(double x, double y, const char *str, double rot, double hadj, const pGEcontext gc, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; fputs("<text", svgd->file); if (rot == 0) { write_attr_dbl(svgd->file, "x", x); write_attr_dbl(svgd->file, "y", y); } else { fprintf(svgd->file, " transform='translate(%.2f,%.2f) rotate(%0.0f)'", x, y, -1.0 * rot); } write_attr_dbl(svgd->file, "font-size", gc->cex * gc->ps); if (is_bold(gc->fontface)) write_attr_str(svgd->file, "font-weight", "bold"); if (is_italic(gc->fontface)) write_attr_str(svgd->file, "font-style", "italic"); if (gc->col != -16777216) // black write_attr_col(svgd->file, "fill", gc->col); std::string font = fontname(gc->fontfamily); write_attr_str(svgd->file, "font-family", font.c_str()); fputs(">", svgd->file); write_escaped(svgd->file, str); fputs("</text>\n", svgd->file); }
static double docx_strheight(const char *str, const pGEcontext gc, pDevDesc dd) { DOCX_dev *docx_obj = (DOCX_dev*) dd->deviceSpecific; std::string file = fontfile(gc->fontfamily, gc->fontface, docx_obj->user_aliases); std::string name = fontname(gc->fontfamily, gc->fontface, docx_obj->system_aliases, docx_obj->user_aliases); gdtools::context_set_font(docx_obj->cc, name, gc->cex * gc->ps, is_bold(gc->fontface), is_italic(gc->fontface), file); FontMetric fm = gdtools::context_extents(docx_obj->cc, std::string(str)); return fm.height; }
static double svg_strwidth(const char *str, const pGEcontext gc, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; gdtools::context_set_font(svgd->cc, fontname(gc->fontfamily), gc->cex * gc->ps, is_bold(gc->fontface), is_italic(gc->fontface)); FontMetric fm = gdtools::context_extents(svgd->cc, std::string(str)); return fm.width; }
static double xlsx_strwidth(const char *str, const pGEcontext gc, pDevDesc dd) { XLSX_dev *xlsx_obj = (XLSX_dev*) dd->deviceSpecific; std::string file = fontfile(gc->fontfamily, gc->fontface, xlsx_obj->user_aliases); std::string name = fontname(gc->fontfamily, gc->fontface, xlsx_obj->system_aliases, xlsx_obj->user_aliases); gdtools::context_set_font(xlsx_obj->cc, name, gc->cex * gc->ps, is_bold(gc->fontface), is_italic(gc->fontface), file); FontMetric fm = gdtools::context_extents(xlsx_obj->cc, std::string(str)); return fm.width; }
void write_text_body_xlsx(pDevDesc dd, R_GE_gcontext *gc, const char* text, double hadj, double fontsize, double fontheight) { XLSX_dev *xlsx_obj = (XLSX_dev *) dd->deviceSpecific; ppr a_ppr_(hadj, fontsize); std::string fontname_ = fontname(gc->fontfamily, gc->fontface, xlsx_obj->system_aliases, xlsx_obj->user_aliases); rpr rpr_(fontsize, is_italic(gc->fontface), is_bold(gc->fontface), gc->col, fontname_); fputs("<xdr:txBody>", xlsx_obj->file ); fprintf(xlsx_obj->file, "%s", body_pr::a_tag().c_str() ); fputs("<a:p>", xlsx_obj->file ); fprintf(xlsx_obj->file, "%s", a_ppr_.a_tag().c_str() ); fputs("<a:r>", xlsx_obj->file ); fprintf(xlsx_obj->file, "%s", rpr_.a_tag().c_str() ); write_t_xlsx(dd, text); fputs("</a:r>", xlsx_obj->file ); fputs("</a:p>", xlsx_obj->file ); fputs("</xdr:txBody>", xlsx_obj->file ); }
static void svg_metric_info(int c, const pGEcontext gc, double* ascent, double* descent, double* width, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; // Convert to string - negative implies unicode code point char str[16]; if (c < 0) { Rf_ucstoutf8(str, (unsigned int) -c); } else { str[0] = (char) c; str[1] = '\0'; } gdtools::context_set_font(svgd->cc, fontname(gc->fontfamily), gc->cex * gc->ps, is_bold(gc->fontface), is_italic(gc->fontface)); FontMetric fm = gdtools::context_extents(svgd->cc, std::string(str)); *ascent = fm.ascent; *descent = fm.descent; *width = fm.width; }
void write_text_body_docx(pDevDesc dd, R_GE_gcontext *gc, const char* text, double hadj, double fontsize, double fontheight) { DOCX_dev *docx_obj = (DOCX_dev *) dd->deviceSpecific; ppr a_ppr_(hadj, fontsize); std::string fontname_ = fontname(gc->fontfamily, gc->fontface, docx_obj->system_aliases, docx_obj->user_aliases); rpr rpr_(fontsize, is_italic(gc->fontface), is_bold(gc->fontface), gc->col, fontname_); fputs("<wps:txbx>", docx_obj->file ); fputs("<w:txbxContent>", docx_obj->file ); fputs("<w:p>", docx_obj->file ); fprintf(docx_obj->file, "%s", a_ppr_.w_tag().c_str() ); fputs("<w:r>", docx_obj->file ); fprintf(docx_obj->file, "%s", rpr_.w_tag().c_str() ); write_t_docx(dd, text); fputs("</w:r>", docx_obj->file ); fputs("</w:p>", docx_obj->file ); fputs("</w:txbxContent>", docx_obj->file ); fputs("</wps:txbx>", docx_obj->file ); }
static void xlsx_metric_info(int c, const pGEcontext gc, double* ascent, double* descent, double* width, pDevDesc dd) { XLSX_dev *xlsx_obj = (XLSX_dev*) dd->deviceSpecific; // Convert to string - negative implies unicode code point char str[16]; if (c < 0) { Rf_ucstoutf8(str, (unsigned int) -c); } else { str[0] = (char) c; str[1] = '\0'; } std::string file = fontfile(gc->fontfamily, gc->fontface, xlsx_obj->user_aliases); std::string name = fontname(gc->fontfamily, gc->fontface, xlsx_obj->system_aliases, xlsx_obj->user_aliases); gdtools::context_set_font(xlsx_obj->cc, name, gc->cex * gc->ps, is_bold(gc->fontface), is_italic(gc->fontface), file); FontMetric fm = gdtools::context_extents(xlsx_obj->cc, std::string(str)); *ascent = fm.ascent; *descent = fm.descent; *width = fm.width; }
void Font_FT::init(const String &filepath) { TTF_Font *font = TTF_OpenFont(filepath.c_str(), int(get_text_height() * m_vratio + 0.5f)); if(!font) throw Font_Init_Failure(); /*** Set Style ***/ if(is_bold() && is_italic()) TTF_SetFontStyle(font, TTF_STYLE_BOLD | TTF_STYLE_ITALIC); else if(is_bold()) TTF_SetFontStyle(font, TTF_STYLE_BOLD); else if(is_italic()) TTF_SetFontStyle(font, TTF_STYLE_ITALIC); /*** Determine Width & Height ***/ float font_width = 0; float font_height = 0; SDL_Color color2 = {0xFF, 0xFF, 0xFF, 0xFF}; SDL_Surface *source[256] = {0}; for(unsigned char c = 1; c; ++c) { Core::assert_no_error(); //char t[2] = {c, '\0'}; //source[c] = TTF_RenderText_Blended(font, t, color2); source[c] = TTF_RenderGlyph_Blended(font, c, color2); Core::print_error(); font_width = std::max(font_width, float(source[c] ? source[c]->w : 0)); font_height = std::max(font_height, float(source[c] ? source[c]->h : 0)); } /*** Initialize Intermediate SDL Surface ***/ const int next_w = int(pow(2.0f, ceil(log(float(16 * font_width))/log(2.0f)))), next_h = int(pow(2.0f, ceil(log(float(16 * font_height))/log(2.0f)))); SDL_Surface *font_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, next_w, next_h, 32, source[END_OF_TIME]->format->Rmask, source[END_OF_TIME]->format->Gmask, source[END_OF_TIME]->format->Bmask, source[END_OF_TIME]->format->Amask); if(!font_surface) { for(unsigned char c = 1; c; ++c) SDL_FreeSurface(source[c]); TTF_CloseFont(font); if(next_h > 1024) { m_vratio /= 2.0f; init(filepath); return; } else throw Font_Init_Failure(); } SDL_LockSurface(font_surface); SDL_FillRect(font_surface, 0, SDL_MapRGBA(font_surface->format, 0, 0, 0, SDL_ALPHA_TRANSPARENT)); SDL_UnlockSurface(font_surface); /*** Initialize Glyphs ***/ SDL_Rect dstrect = {0, 0, Uint16(font_width), Uint16(font_height)}; m_glyph[0] = 0; for(unsigned char c = 1; c; ++c) { dstrect.x = Sint16((c % 16) * font_width); dstrect.y = Sint16((c / 16) * font_height); m_glyph[c] = new Glyph(font, c, source[c], font_surface, dstrect, next_w, next_h, m_vratio); } /*** Correct Transparency ***/ const Uint32 transparent_white = font_surface->format->Rmask | font_surface->format->Gmask | font_surface->format->Bmask; for(int i = 0; i < font_surface->h; ++i) for(Uint32 * src = reinterpret_cast<Uint32 *>(font_surface->pixels) + i * font_surface->pitch / 4, * src_end = src + font_surface->w; src != src_end; ++src) if(*src & font_surface->format->Rmask) *src = transparent_white | ((*src & font_surface->format->Rmask) >> font_surface->format->Rshift << font_surface->format->Ashift); /*** Initialize Final Texture ***/ m_texture = get_Video().create_Texture(font_surface, false); TTF_CloseFont(font); }