removed widths from buffers

This commit is contained in:
Allen Webster 2016-09-21 18:45:43 -04:00
parent 582faa4bdf
commit 69699db27a
3 changed files with 5606 additions and 5744 deletions

View File

@ -537,15 +537,13 @@ view_compute_lowest_line(View *view){
lowest_line = last_line; lowest_line = last_line;
} }
else{ else{
// TODO(allen): Actually what I should do to get the "line span"
// is I should store the "final" wrap position. The position the
// next line of the file would have started at, if it had existed.
// Then line_span is just line_span = FLOOR32((wrap_y - wrap_final_y)/view->line_height);
f32 wrap_y = view->file_data.line_wrap_y[last_line]; f32 wrap_y = view->file_data.line_wrap_y[last_line];
lowest_line = FLOOR32(wrap_y / view->line_height); i32 line_span = 40;
f32 max_width = view_file_display_width(view); lowest_line = FLOOR32(wrap_y / view->line_height) + line_span - 1;
Editing_File *file = view->file_data.file;
Assert(!file->is_dummy);
f32 width = file->state.buffer.line_widths[last_line];
i32 line_span = view_wrapped_line_span(width, max_width);
lowest_line += line_span - 1;
} }
} }
return(lowest_line); return(lowest_line);
@ -905,20 +903,15 @@ enum{
}; };
internal i32 internal i32
file_grow_starts_widths_as_needed(General_Memory *general, Buffer_Type *buffer, i32 additional_lines){ file_grow_starts_as_needed(General_Memory *general, Buffer_Type *buffer, i32 additional_lines){
b32 result = GROW_NOT_NEEDED; b32 result = GROW_NOT_NEEDED;
i32 max = buffer->line_max; i32 max = buffer->line_max;
i32 count = buffer->line_count; i32 count = buffer->line_count;
i32 target_lines = count + additional_lines; i32 target_lines = count + additional_lines;
Assert(max == buffer->widths_max);
if (target_lines > max || max == 0){ if (target_lines > max || max == 0){
max = LargeRoundUp(target_lines + max, Kbytes(1)); max = LargeRoundUp(target_lines + max, Kbytes(1));
f32 *new_widths = (f32*)general_memory_reallocate(
general, buffer->line_widths,
sizeof(f32)*count, sizeof(f32)*max);
i32 *new_lines = (i32*)general_memory_reallocate( i32 *new_lines = (i32*)general_memory_reallocate(
general, buffer->line_starts, general, buffer->line_starts,
sizeof(i32)*count, sizeof(i32)*max); sizeof(i32)*count, sizeof(i32)*max);
@ -927,11 +920,7 @@ file_grow_starts_widths_as_needed(General_Memory *general, Buffer_Type *buffer,
buffer->line_starts = new_lines; buffer->line_starts = new_lines;
buffer->line_max = max; buffer->line_max = max;
} }
if (new_widths){ if (new_lines){
buffer->line_widths = new_widths;
buffer->widths_max = max;
}
if (new_lines && new_widths){
result = GROW_SUCCESS; result = GROW_SUCCESS;
} }
else{ else{
@ -943,23 +932,16 @@ file_grow_starts_widths_as_needed(General_Memory *general, Buffer_Type *buffer,
} }
internal void internal void
file_measure_starts_widths(System_Functions *system, General_Memory *general, file_measure_starts(System_Functions *system, General_Memory *general, Buffer_Type *buffer){
Buffer_Type *buffer, f32 *advance_data){
if (!buffer->line_starts){ if (!buffer->line_starts){
i32 max = buffer->line_max = Kbytes(1); i32 max = buffer->line_max = Kbytes(1);
buffer->line_starts = (i32*)general_memory_allocate(general, max*sizeof(i32)); buffer->line_starts = (i32*)general_memory_allocate(general, max*sizeof(i32));
TentativeAssert(buffer->line_starts); TentativeAssert(buffer->line_starts);
// TODO(allen): when unable to allocate? // TODO(allen): when unable to allocate?
} }
if (!buffer->line_widths){
i32 max = buffer->widths_max = Kbytes(1);
buffer->line_widths = (f32*)general_memory_allocate(general, max*sizeof(f32));
TentativeAssert(buffer->line_starts);
// TODO(allen): when unable to allocate?
}
Buffer_Measure_Starts state = {}; Buffer_Measure_Starts state = {};
while (buffer_measure_starts_widths(&state, buffer, advance_data)){ while (buffer_measure_starts(&state, buffer)){
i32 count = state.count; i32 count = state.count;
i32 max = buffer->line_max; i32 max = buffer->line_max;
max = ((max + 1) << 1); max = ((max + 1) << 1);
@ -973,21 +955,8 @@ file_measure_starts_widths(System_Functions *system, General_Memory *general,
buffer->line_starts = new_lines; buffer->line_starts = new_lines;
buffer->line_max = max; buffer->line_max = max;
} }
{
f32 *new_lines = (f32*)
general_memory_reallocate(general, buffer->line_widths,
sizeof(f32)*count, sizeof(f32)*max);
// TODO(allen): when unable to grow?
TentativeAssert(new_lines);
buffer->line_widths = new_lines;
buffer->widths_max = max;
}
} }
buffer->line_count = state.count; buffer->line_count = state.count;
buffer->widths_count = state.count;
} }
internal void internal void
@ -1055,7 +1024,7 @@ file_create_from_string(System_Functions *system, Models *models,
float *advance_data = 0; float *advance_data = 0;
if (font) advance_data = font->advance_data; if (font) advance_data = font->advance_data;
file_measure_starts_widths(system, general, &file->state.buffer, advance_data); file_measure_starts(system, general, &file->state.buffer);
file->settings.read_only = read_only; file->settings.read_only = read_only;
if (!read_only){ if (!read_only){
@ -1110,7 +1079,6 @@ file_close(System_Functions *system, General_Memory *general, Editing_File *file
if (buffer->data){ if (buffer->data){
general_memory_free(general, buffer->data); general_memory_free(general, buffer->data);
general_memory_free(general, buffer->line_starts); general_memory_free(general, buffer->line_starts);
general_memory_free(general, buffer->line_widths);
} }
if (file->state.undo.undo.edits){ if (file->state.undo.undo.edits){
@ -2129,12 +2097,8 @@ file_do_single_edit(System_Functions *system,
i32 new_line_count = buffer_count_newlines(&file->state.buffer, start, start+str_len); i32 new_line_count = buffer_count_newlines(&file->state.buffer, start, start+str_len);
i32 line_shift = new_line_count - replaced_line_count; i32 line_shift = new_line_count - replaced_line_count;
i16 font_id = file->settings.font_id; file_grow_starts_as_needed(general, buffer, line_shift);
Render_Font *font = get_font_info(models->font_set, font_id)->font;
file_grow_starts_widths_as_needed(general, buffer, line_shift);
buffer_remeasure_starts(buffer, line_start, line_end, line_shift, shift_amount); buffer_remeasure_starts(buffer, line_start, line_end, line_shift, shift_amount);
buffer_remeasure_widths(buffer, font->advance_data, line_start, line_end, line_shift);
// NOTE(allen): update the views looking at this file // NOTE(allen): update the views looking at this file
Panel *panel, *used_panels; Panel *panel, *used_panels;
@ -2201,16 +2165,8 @@ file_do_batch_edit(System_Functions *system, Models *models, Editing_File *file,
} }
// NOTE(allen): meta data // NOTE(allen): meta data
{ Buffer_Measure_Starts measure_state = {};
Buffer_Measure_Starts state = {}; buffer_measure_starts(&measure_state, &file->state.buffer);
i16 font_id = file->settings.font_id;
Render_Font *font = get_font_info(models->font_set, font_id)->font;
float *advance_data = 0;
if (font){
advance_data = font->advance_data;
}
buffer_measure_starts_widths(&state, &file->state.buffer, advance_data);
}
// NOTE(allen): cursor fixing // NOTE(allen): cursor fixing
i32 shift_total = 0; i32 shift_total = 0;
@ -2647,11 +2603,7 @@ remeasure_file_view(System_Functions *system, View *view){
internal void internal void
file_set_font(System_Functions *system, Models *models, Editing_File *file, i16 font_id){ file_set_font(System_Functions *system, Models *models, Editing_File *file, i16 font_id){
Render_Font *font = get_font_info(models->font_set, font_id)->font;
f32 *advance_data = font->advance_data;
file->settings.font_id = font_id; file->settings.font_id = font_id;
file_measure_starts_widths(system, &models->mem.general, &file->state.buffer, advance_data);
Editing_Layout *layout = &models->layout; Editing_Layout *layout = &models->layout;
for (View_Iter iter = file_view_iter_init(layout, file, 0); for (View_Iter iter = file_view_iter_init(layout, file, 0);

View File

@ -70,28 +70,21 @@ typedef struct Buffer_Measure_Starts{
i32 i; i32 i;
i32 count; i32 count;
i32 start; i32 start;
f32 width;
} Buffer_Measure_Starts; } Buffer_Measure_Starts;
// TODO(allen): Rewrite this with a duff routine // TODO(allen): Rewrite this with a duff routine
internal_4tech i32 internal_4tech i32
buffer_measure_starts_widths_(Buffer_Measure_Starts *state, Buffer_Type *buffer, f32 *advance_data){ buffer_measure_starts(Buffer_Measure_Starts *state, Buffer_Type *buffer){
Buffer_Stringify_Type loop = {0}; Buffer_Stringify_Type loop = {0};
char *data = 0; char *data = 0;
i32 end = 0; i32 end = 0;
i32 size = buffer_size(buffer); i32 size = buffer_size(buffer);
f32 width = state->width;
i32 start = state->start, i = state->i; i32 start = state->start, i = state->i;
i32 *start_ptr = buffer->line_starts + state->count; i32 *start_ptr = buffer->line_starts + state->count;
i32 *start_end = buffer->line_starts + buffer->line_max; i32 *start_end = buffer->line_starts + buffer->line_max;
f32 *width_ptr = buffer->line_widths + state->count;
i32 result = 1; i32 result = 1;
char ch = 0; char ch = 0;
debug_4tech(i32 widths_max = buffer->widths_max);
debug_4tech(i32 max = buffer->line_max);
assert_4tech(max == widths_max);
for (loop = buffer_stringify_loop(buffer, i, size); for (loop = buffer_stringify_loop(buffer, i, size);
buffer_stringify_good(&loop); buffer_stringify_good(&loop);
buffer_stringify_next(&loop)){ buffer_stringify_next(&loop)){
@ -104,13 +97,8 @@ buffer_measure_starts_widths_(Buffer_Measure_Starts *state, Buffer_Type *buffer,
goto buffer_measure_starts_widths_end; goto buffer_measure_starts_widths_end;
} }
*width_ptr++ = width;
*start_ptr++ = start; *start_ptr++ = start;
start = i + 1; start = i + 1;
width = 0;
}
else{
width += measure_character(advance_data, ch);
} }
} }
} }
@ -121,88 +109,12 @@ buffer_measure_starts_widths_(Buffer_Measure_Starts *state, Buffer_Type *buffer,
goto buffer_measure_starts_widths_end; goto buffer_measure_starts_widths_end;
} }
*start_ptr++ = start; *start_ptr++ = start;
*width_ptr++ = 0;
result = 0; result = 0;
buffer_measure_starts_widths_end:; buffer_measure_starts_widths_end:;
state->i = i; state->i = i;
state->count = (i32)(start_ptr - buffer->line_starts); state->count = (i32)(start_ptr - buffer->line_starts);
state->start = start; state->start = start;
state->width = width;
return(result);
}
internal_4tech i32
buffer_measure_starts_zero_widths_(Buffer_Measure_Starts *state, Buffer_Type *buffer){
Buffer_Stringify_Type loop;
i32 *start_ptr, *start_end;
f32 *width_ptr;
debug_4tech(i32 widths_max);
debug_4tech(i32 max);
char *data;
i32 size, end;
i32 start, i;
i32 result;
char ch;
size = buffer_size(buffer);
debug_4tech(max = buffer->line_max);
debug_4tech(widths_max = buffer->widths_max);
assert_4tech(max == widths_max);
result = 1;
i = state->i;
start = state->start;
start_ptr = buffer->line_starts + state->count;
width_ptr = buffer->line_widths + state->count;
start_end = buffer->line_starts + buffer->line_max;
for (loop = buffer_stringify_loop(buffer, i, size);
buffer_stringify_good(&loop);
buffer_stringify_next(&loop)){
end = loop.size + loop.absolute_pos;
data = loop.data - loop.absolute_pos;
for (; i < end; ++i){
ch = data[i];
if (ch == '\n'){
if (start_ptr == start_end) goto buffer_measure_starts_zero_widths_end;
*width_ptr++ = 0;
*start_ptr++ = start;
start = i + 1;
}
}
}
assert_4tech(i == size);
if (start_ptr == start_end) goto buffer_measure_starts_zero_widths_end;
*start_ptr++ = start;
*width_ptr++ = 0;
result = 0;
buffer_measure_starts_zero_widths_end:
state->i = i;
state->count = (i32)(start_ptr - buffer->line_starts);
state->start = start;
return(result);
}
internal_4tech i32
buffer_measure_starts_widths(Buffer_Measure_Starts *state, Buffer_Type *buffer, f32 *advance_data){
i32 result = 0;
if (advance_data){
result = buffer_measure_starts_widths_(state, buffer, advance_data);
}
else{
result = buffer_measure_starts_zero_widths_(state, buffer);
}
return(result); return(result);
} }
@ -266,12 +178,12 @@ buffer_remeasure_starts(Buffer_Type *buffer, i32 line_start, i32 line_end, i32 l
buffer->line_count = line_count; buffer->line_count = line_count;
} }
#if 0
internal_4tech void internal_4tech void
buffer_remeasure_widths(Buffer_Type *buffer, f32 *advance_data, buffer_remeasure_widths(Buffer_Type *buffer, f32 *advance_data,
i32 line_start, i32 line_end, i32 line_shift){ i32 line_start, i32 line_end, i32 line_shift){
Buffer_Stringify_Type loop; Buffer_Stringify_Type loop;
i32 *starts = buffer->line_starts; i32 *starts = buffer->line_starts;
f32 *widths = buffer->line_widths;
i32 line_count = buffer->line_count; i32 line_count = buffer->line_count;
i32 widths_count = buffer->widths_count; i32 widths_count = buffer->widths_count;
char *data = 0; char *data = 0;
@ -328,6 +240,7 @@ buffer_remeasure_widths(Buffer_Type *buffer, f32 *advance_data,
assert_4tech(i+1 == line_count); assert_4tech(i+1 == line_count);
} }
} }
#endif
internal_4tech void internal_4tech void
buffer_measure_wrap_y(Buffer_Type *buffer, f32 *wraps, buffer_measure_wrap_y(Buffer_Type *buffer, f32 *wraps,

View File

@ -16,12 +16,9 @@ typedef struct Gap_Buffer{
i32 size2; i32 size2;
i32 max; i32 max;
f32 *line_widths;
i32 *line_starts; i32 *line_starts;
i32 line_count; i32 line_count;
i32 widths_count;
i32 line_max; i32 line_max;
i32 widths_max;
} Gap_Buffer; } Gap_Buffer;
inline_4tech i32 inline_4tech i32