//------------------------------------------------------------------------------ Vector2ui Font::estimate_multiline(const std::string& text) const { std::vector<std::string> lines = explode(text, "\n"); Vector2ui result(0,0); for (unsigned int i = 0; i < lines.size(); i++) { Vector2ui line_size = estimate(lines[i]); result(0) = std::max(result(0), line_size(0)); result(1) += line_size(1); } return result; }
void set_tab(char ***map, t_fdf *fdf) { int i; int j; int len; i = -1; if (fdf) { if (!fdf->tab) if (!(fdf->tab = (t_coord**)malloc(sizeof(t_coord*) * (fdf->tab_h)))) check_errors(MALLOC, "fdf->tab", "set_tab.c"); while (*(map + ++i)) { if ((len = line_size(*(map + i))) > fdf->tab_w) fdf->tab_w = len; *(fdf->tab + i) = (t_coord*)malloc(sizeof(t_coord) * (len + 1)); if (!(fdf->tab + i)) check_errors(MALLOC, "fdf->tab + i", "set_tab.c"); j = -1; while (*(*(map + i) + ++j)) fill_coord(fdf, map[i][j], i, j); fdf->tab[i][j].end = 0; } } }
int main( int argc, char ** argv ) { line_t * ptr; int sz; char * lines[3] = { "goober", "", "Looks like a real line.\n" }; for(int i=0; i < 3; i++) { ptr = line_new(); line_set(ptr , lines[i]); sz = line_size( ptr ); printf("ptr size: %d\n", sz ); printf("ptr data: %s\n", line_get(ptr) ); printf("ends in newline: %s\n", line_has_newline( ptr ) ? "Yes" : "No" ); printf("null terminated: %s\n", line_null_terminated( ptr ) ? "Yes" : "No" ); line_free( ptr ); printf("line after free is null? %s\n", ptr == NULL ? "Yes" : "No" ); } return 0; }
int strbuf_split(struct strbuf *buf, struct strbuf *out, char delim) { int i = 0; int hint = 80; int size = 0; char *temp = buf->buf; while (*temp) { strbuf_init(out + i, 80); size = line_size(temp); strbuf_add(out + i, temp, size); temp = temp + size; i++; } return i; }
t_board *build_board(char *str) { int y; int i; t_board *brd; i = 0; y = 0; if (!(brd = (t_board*)malloc(sizeof(t_board)))) return (NULL); brd->size_y = line_count(str); if (!(brd->map = (char**)malloc(sizeof(char*) * brd->size_y))) return (NULL); brd->size_x = line_size(str); while (y < brd->size_y) { brd->map[y] = &str[brd->size_x * y + y]; y++; } return (brd); }
static void cut_word(std::string& line, std::string& word, int font_size, int style, int max_width) { std::string tmp = line; utf8::iterator tc(word); bool first = true; for(;tc != utf8::iterator::end(word); ++tc) { tmp.append(tc.substr().first, tc.substr().second); SDL_Rect tsize = line_size(tmp, font_size, style); if(tsize.w > max_width) { const std::string& w = word; if(line.empty() && first) { line += std::string(w.begin(), tc.substr().second); word = std::string(tc.substr().second, w.end()); } else { line += std::string(w.begin(), tc.substr().first); word = std::string(tc.substr().first, w.end()); } break; } first = false; } }
int line_width(const std::string& line, int font_size, int style) { return line_size(line,font_size,style).w; }
std::string word_wrap_text(const std::string& unwrapped_text, int font_size, int max_width, int max_height, int max_lines, bool partial_line) { VALIDATE(max_width > 0, _("The maximum text width is less than 1.")); utf8::iterator ch(unwrapped_text); std::string current_word; std::string current_line; size_t line_width = 0; size_t current_height = 0; bool line_break = false; bool first = true; bool start_of_line = true; std::string wrapped_text; std::string format_string; SDL_Color color; int font_sz = font_size; int style = TTF_STYLE_NORMAL; utf8::iterator end = utf8::iterator::end(unwrapped_text); while(1) { if(start_of_line) { line_width = 0; format_string.clear(); while(ch != end && *ch < static_cast<ucs4::char_t>(0x100) && is_format_char(*ch) && !ch.next_is_end()) { format_string.append(ch.substr().first, ch.substr().second); ++ch; } // We need to parse the special format characters // to give the proper font_size and style to line_size() font_sz = font_size; style = TTF_STYLE_NORMAL; parse_markup(format_string.begin(),format_string.end(),&font_sz,&color,&style); current_line.clear(); start_of_line = false; } // If there is no current word, get one if(current_word.empty() && ch == end) { break; } else if(current_word.empty()) { if(*ch == ' ' || *ch == '\n') { current_word = *ch; ++ch; } else { ucs4::char_t previous = 0; for(;ch != utf8::iterator::end(unwrapped_text) && *ch != ' ' && *ch != '\n'; ++ch) { if(!current_word.empty() && break_before(*ch) && !no_break_after(previous)) break; if(!current_word.empty() && break_after(previous) && !no_break_before(*ch)) break; current_word.append(ch.substr().first, ch.substr().second); previous = *ch; } } } if(current_word == "\n") { line_break = true; current_word.clear(); start_of_line = true; } else { const size_t word_width = line_size(current_word, font_sz, style).w; line_width += word_width; if(static_cast<long>(line_width) > max_width) { if (!partial_line && static_cast<long>(word_width) > max_width) { cut_word(current_line, current_word, font_sz, style, max_width); } if(current_word == " ") current_word = ""; line_break = true; } else { current_line += current_word; current_word = ""; } } if(line_break || (current_word.empty() && ch == end)) { SDL_Rect size = line_size(current_line, font_sz, style); if(max_height > 0 && current_height + size.h >= size_t(max_height)) { return wrapped_text; } if(!first) { wrapped_text += '\n'; } wrapped_text += format_string + current_line; current_line.clear(); line_width = 0; current_height += size.h; line_break = false; first = false; if(--max_lines == 0) { return wrapped_text; } } } return wrapped_text; }
static void thought_monitor(void) { time_t start_time = time(NULL); char buffer[OUTPUT_XFER_BUFFER_SIZE+1]; ssize_t buffer_size = 0; for (;;) { /* check if the thought process has terminated */ int terminate = has_terminated(); /* check for output from the thought process; copy it line-by-line to the log file or screen (if debugging) */ if (has_output() || (buffer_size > 0)) { for (;;) { ssize_t bytes_read = read(g_fd_output, buffer + buffer_size, OUTPUT_XFER_BUFFER_SIZE); if (bytes_read < 1) break; buffer_size += bytes_read; while (buffer_size > 0) { ssize_t line_bytes = line_size(buffer, buffer + buffer_size); /* if we've filled the buffer, we have to output it, regardless of whether we've found an end of line marker */ if ((line_bytes == 0) && (buffer_size == OUTPUT_XFER_BUFFER_SIZE)) line_bytes = buffer_size; if ((terminate) && (line_bytes == 0)) line_bytes = buffer_size; /* can't see end of line, come back later */ if (line_bytes == 0) break; /* output the log line with an appropriate prefix */ buffer[line_bytes] = 0; lprintf(BRAIN_NOTICE, LOG_PREFIX_FORMAT ": %s", thought_id, time_elapsed(), buffer); /* adjust the buffer so we don't consider this line again */ memmove(buffer, buffer + line_bytes, buffer_size - line_bytes); buffer_size -= line_bytes; } } } /* exit if the process has terminated */ if (terminate) break; /* otherwise check if the process has been running too long */ else if (time(NULL) - start_time >= g_timeout_secs) { kill(g_script_pid, SIGKILL); lprintf(BRAIN_WARNING, LOG_PREFIX_FORMAT ": Thought took too long and was aborted.\n", thought_id, time_elapsed()); break; } /* sleep for a while */ usleep(500); } }