Mercurial > hg > octave-nkf
view src/txt-eng-ft.cc @ 13281:834f904a3dcb
Add support for full asynchronous graphics toolkit running in a separate
thread. Add uicontrol and uipanel implementation.
* oct-mutex.h (octave_base_mutex::try_lock): New method.
(octave_mutex::try_lock): Likewise.
(octave_auto_lock::octave_auto_lock): New argument for
blocking/non-blocking locks.
(octave_auto_lock::ok): New method to test locking state.
(octave_auto_lock::operator bool): Likewise.
(octave_thread): New utility class.
* oct-mutex.cc (octave_base_mutex::try_lock): New method.
(octave_w32_mutex::try_lock): Implement it for Win32.
(octave_pthread_mutex::try_lock): Implement it for pthread.
(octave_thread): Implement new utility class.
* octave.cc (octave_main): Initialize octave_thread.
* genprops.awk (emit_get_string_array): New function.
(emit_declarations): Use it for string_array_property.
* graphics.h.in (base_property::set): New argument to control toolkit
notifying.
(property::set): Likewise.
(string_array_property::string_vector_value): New method.
(radio_property::do_set): Add warning about abbreviated radio values.
(base_graphics_toolkit::initialize): Returns bool.
(graphics_toolkit::initialize): Likewise.
(base_graphics_object::toolkit_flag): New member.
(base_graphics_object::base_graphics_object): Initialize it.
(base_graphics_object::valid_toolkit_object): New method.
(base_graphics_object::initialize, base_graphics_object::finalize,
base_graphics_object::update): Likewise.
(graphics_object::initialize, graphics_object::finalize,
graphics_object::update): Likewise.
(figure::properties::set_toolkit): Move implementation to source file.
(base_properties::get_boundingbox): Add parent size argument for optimization.
(figure::properties::get_boundingbox): Likewise.
(axes::properties::get_boundingbox): Likewise.
(figure::properties::map_from_boundingbox): New utility method.
(figure::properties::map_to_boundingbox): Likewise.
(axes::properties::get_fontsize_points): New utility method.
(text::properties::get_fontsize_points): Likewise.
(axes::properties::xlabel, axes::properties::ylabel, axes::properties::zlabel,
axes::properties::title): Don't notify toolkit on initialization.
(axes::initialize): New method override.
(uicontrol): New class.
(uipanel): Likewise.
(graphics_event::create_callback_event): New static method overload.
(graphics_event::create_set_event): New argument to prevent circular
behavior when property change is triggered from the toolkit.
(gh_manager::post_set): Likewise.
(gh_manager::do_post_set): Likewise.
(gh_manager::make_graphics_handle): New argument controlling toolkit notify.
(gh_manager::make_figure_handle): Likewise.
(gh_manager::do_make_graphics_handle): Likewise.
(gh_manager::do_make_figure_handle): Likewise.
(gh_manager::try_lock): New static method.
(gh_manager::execute_listener): Likewise.
(gh_manager::enable_event_processing): Likewise.
(gh_manager::do_try_lock): New method.
(gh_manager::do_execute_listener): Likewise.
(gh_manager::do_enable_event_processing): Likewise.
(gh_manager::event_processing): New member.
(gh_manager::execute_callback): Protect graphics_object access.
(gh_manager::auto_lock): Inherits from octave_autolock. Renamed from autolock.
(gh_manager::auto_lock::auto_lock): New blocking/non-blocking argument.
* graphics.cc (default_control_position, default_control_sliderstep,
default_panel_position): New utility functions.
(convert_font_size): New utility function.
(convert_position): Support 2D-only positions.
(lookup_object_name): Support uicontrol and uipanel.
(make_graphics_object_from_type): Likewise.
(root_figure::init_factory_properties): Likewise.
(property_list::set, property_list::lookup): Likewise.
(base_property::set): New argument controlling toolkit notifying.
(base_property::run_listeners): Call gh_manager::execute_listener, allowing
to set a property from another thread and run listeners synchronously with
octave.
(color_property::do_set): Add warning about abbreviated radio value.
(double_radio_property::do_set): Likewise.
(finalize_r, initialize_r, xinitialize): New utility functions.
(gh_manager::do_free): Calls graphics_object::finalize.
(base_graphics_toolkit::initialize): Returns bool.
(gnuplot_toolkit::initialize): Likewise.
(figure::properties::set_toolkit): Move implementation from header.
(figure::properties::get_boundingbox): New argument for parent size.
(axes::properties::get_boundingbox): Likewise.
(figure::properties::map_from_boundingbox): New utility method.
(figure::properties::map_to_boundingbox): Likewise.
(axes::properties::update_fontunits): Use convert_font_size.
(axes::properties::get_fontsize_points): New utility method.
(text::properties::get_fontsize_points): Likewise.
(axes::initialize): New method override to trigger initialization of
labels and title.
(uicontrol): New class.
(uipanel): Likewise.
(gh_manager::gh_manager): Initialize new event_processing member.
(gh_manager::do_make_graphics_handle): New argument controlling toolkit
notifying.
(gh_manager::do_make_figure_handle): Likewise.
(callback_event::callback): New member.
(callback_event::callback_event): Initialize it.
(callback_event::execute): Use it.
(set_event::notify_toolkit): New member.
(set_event::set_event): Initialize it.
(set_event::execute): Use it. Also allow to set read-only properties.
(graphics_event::create_callback_event): New static method overload.
(graphics_event::create_set_event): New argument controlling toolkit notifying.
(gh_manager::do_restore_gcbo): Rename autolock to auto_lock.
(gh_manager::do_post_callback, gh_manager::do_post_function): Likewise.
(Fishandle, Fset, Fget, F__get__): Likewise.
(F__go_figure__, F__calc_dimensions__, GO_BODY): Likewise.
(F__go_delete__, F__go_axes_init__, F__go_handles__, F__go_figure_handles__,
Favailable_graphics_toolkits, Faddlistener, Fdellistener, Faddproperty):
Likewise.
(get_property_from_handle, set_property_in_handle): Likewise.
(gh_manager::do_post_set): Likewise. New argument controlling toolkit
notifying.
(gh_manager::do_execute_listener): New method.
(gh_manager::do_enable_event_processing): Likewise.
(gh_manager::do_execute_callback): Check callback argument validity.
Rename autolock to auto_lock.
(gh_manager::do_process_events): Execute drawnow at the end of event
processing loop, avoiding recursivity. Maintain the input event hook
if gh_manager::event_processing is non zero.
(make_graphics_object): Postpone object's toolkit initialization at
the end of the object creation.
(F__go_figure__): Likewise.
(F__go_uicontrol__, F__go_uipanel__): New functions.
* __init_fltk__.cc (fltk_graphics_toolkit::initialise): New method.
* gl-render.h (opengl_renderer::draw): New argument to identify top-level
calls.
(opengl_renderer::draw_uipanel): New method.
(opengl_renderer::init_gl_context): Likewise.
* gl-render.cc (opengl_renderer::draw): New argument to identify top-level
calls. Skip uicontrol objects. Handle uipanel objects when top-level.
(opengl_renderer::init_gl_context): New method.
(opengl_renderer::draw_figure): Use it.
(opengl_renderer::draw_uipanel): New method.
author | Michael Goffioul <michael.goffioul@gmail.com> |
---|---|
date | Thu, 06 Oct 2011 16:44:18 +0100 |
parents | e81ddf9cacd5 |
children | d2b799e3178a |
line wrap: on
line source
/* Copyright (C) 2009-2011 Michael Goffioul This file is part of Octave. Octave is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. Octave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Octave; see the file COPYING. If not, see <http://www.gnu.org/licenses/>. */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #if HAVE_FREETYPE #if HAVE_FONTCONFIG #include <fontconfig/fontconfig.h> #endif #include <iostream> #include "error.h" #include "pr-output.h" #include "txt-eng-ft.h" class ft_manager { public: static bool instance_ok (void) { bool retval = true; if (! instance) instance = new ft_manager (); if (! instance) { ::error ("unable to create ft_manager!"); retval = false; } return retval; } static FT_Face get_font (const std::string& name, const std::string& weight, const std::string& angle, double size) { return (instance_ok () ? instance->do_get_font (name, weight, angle, size) : 0); } private: static ft_manager *instance; private: // No copying! ft_manager (const ft_manager&); ft_manager& operator = (const ft_manager&); ft_manager (void) : library () #if HAVE_FONTCONFIG , fc_init_done (false) #endif { if (FT_Init_FreeType (&library)) { ::error ("unable to initialize freetype library"); } #if HAVE_FONTCONFIG fc_init_done = false; if (! FcInit ()) { ::error ("unable to initialize fontconfig library"); } else { fc_init_done = true; } #endif } ~ft_manager (void) { #if HAVE_FONTCONFIG FcFini (); fc_init_done = false; #endif } FT_Face do_get_font (const std::string& name, const std::string& weight, const std::string& angle, double size) { FT_Face retval = 0; std::string file; #if HAVE_FONTCONFIG if (fc_init_done) { int fc_weight, fc_angle; if (weight == "bold") fc_weight = FC_WEIGHT_BOLD; else if (weight == "light") fc_weight = FC_WEIGHT_LIGHT; else if (weight == "demi") fc_weight = FC_WEIGHT_DEMIBOLD; else fc_weight = FC_WEIGHT_NORMAL; if (angle == "italic") fc_angle = FC_SLANT_ITALIC; else if (angle == "oblique") fc_angle = FC_SLANT_OBLIQUE; else fc_angle = FC_SLANT_ROMAN; FcPattern *pat = FcPatternCreate (); FcPatternAddString (pat, FC_FAMILY, (reinterpret_cast<const FcChar8*> (name == "*" ? "sans" : name.c_str ()))); FcPatternAddInteger (pat, FC_WEIGHT, fc_weight); FcPatternAddInteger (pat, FC_SLANT, fc_angle); FcPatternAddDouble (pat, FC_PIXEL_SIZE, size); if (FcConfigSubstitute (0, pat, FcMatchPattern)) { FcResult res; FcPattern *match; FcDefaultSubstitute (pat); match = FcFontMatch (0, pat, &res); // FIXME -- originally, this test also required that // res != FcResultNoMatch. Is that really needed? if (match) { unsigned char *tmp; FcPatternGetString (match, FC_FILE, 0, &tmp); file = reinterpret_cast<char*> (tmp); } else ::warning ("could not match any font: %s-%s-%s-%g", name.c_str (), weight.c_str (), angle.c_str (), size); if (match) FcPatternDestroy (match); } FcPatternDestroy (pat); } #endif if (file.empty ()) { #ifdef __WIN32__ file = "C:/WINDOWS/Fonts/verdana.ttf"; #else // FIXME: find a "standard" font for UNIX platforms #endif } if (! file.empty () && FT_New_Face (library, file.c_str (), 0, &retval)) ::warning ("ft_manager: unable to load font: %s", file.c_str ()); return retval; } private: FT_Library library; #if HAVE_FONTCONFIG bool fc_init_done; #endif }; ft_manager* ft_manager::instance = 0; // --------------------------------------------------------------------------- ft_render::ft_render (void) : text_processor (), face (0), bbox (1, 4, 0.0), xoffset (0), yoffset (0), multiline_halign (0), multiline_align_xoffsets(), mode (MODE_BBOX), red (0), green (0), blue (0) { } ft_render::~ft_render (void) { if (face) FT_Done_Face (face); } void ft_render::set_font (const std::string& name, const std::string& weight, const std::string& angle, double size) { if (face) FT_Done_Face (face); // FIXME: take "fontunits" into account face = ft_manager::get_font (name, weight, angle, size); if (face) { if (FT_Set_Char_Size (face, 0, size*64, 0, 0)) ::warning ("ft_render: unable to set font size to %d", size); } else ::warning ("ft_render: unable to load appropriate font"); } void ft_render::set_mode (int m) { mode = m; switch (mode) { case MODE_BBOX: xoffset = yoffset = 0; bbox = Matrix (1, 4, 0.0); break; case MODE_RENDER: if (bbox.numel () != 4) { ::warning ("ft_render: invalid bounding box, cannot render"); xoffset = yoffset = 0; pixels = uint8NDArray (); } else { pixels = uint8NDArray (dim_vector (4, bbox(2), bbox(3)), static_cast<uint8_t> (0)); xoffset = 0; yoffset = -bbox(1)-1; } break; default: ::error ("ft_render: invalid mode `%d'", mode); break; } } void ft_render::visit (text_element_string& e) { if (face) { int line_index = 0; FT_UInt box_line_width = 0; std::string str = e.string_value (); FT_UInt glyph_index, previous = 0; if (mode == MODE_BBOX) multiline_align_xoffsets.clear(); else if (mode == MODE_RENDER) xoffset += multiline_align_xoffsets[line_index]; for (size_t i = 0; i < str.length (); i++) { glyph_index = FT_Get_Char_Index (face, str[i]); if (str[i] != '\n' && (! glyph_index || FT_Load_Glyph (face, glyph_index, FT_LOAD_DEFAULT))) ::warning ("ft_render: skipping missing glyph for character `%c'", str[i]); else { switch (mode) { case MODE_RENDER: if (str[i] == '\n') { glyph_index = FT_Get_Char_Index(face, ' '); if (!glyph_index || FT_Load_Glyph (face, glyph_index, FT_LOAD_DEFAULT)) { ::warning ("ft_render: skipping missing glyph for character ` '"); } else { line_index++; xoffset = multiline_align_xoffsets[line_index]; yoffset -= (face->size->metrics.height >> 6); } } else if (FT_Render_Glyph (face->glyph, FT_RENDER_MODE_NORMAL)) ::warning ("ft_render: unable to render glyph for character `%c'", str[i]); else { FT_Bitmap& bitmap = face->glyph->bitmap; int x0, y0; if (previous) { FT_Vector delta; FT_Get_Kerning (face, previous, glyph_index, FT_KERNING_DEFAULT, &delta); xoffset += (delta.x >> 6); } x0 = xoffset+face->glyph->bitmap_left; y0 = yoffset+face->glyph->bitmap_top; // 'w' seems to have a negative -1 // face->glyph->bitmap_left, this is so we don't // index out of bound, and assumes we we allocated // the right amount of horizontal space in the bbox. if (x0 < 0) x0 = 0; for (int r = 0; r < bitmap.rows; r++) for (int c = 0; c < bitmap.width; c++) { unsigned char pix = bitmap.buffer[r*bitmap.width+c]; if (x0+c < 0 || x0+c >= pixels.dim2() || y0-r < 0 || y0-r >= pixels.dim3()) { //::error ("out-of-bound indexing!!"); } else if (pixels(3, x0+c, y0-r).value () == 0) { pixels(0, x0+c, y0-r) = red; pixels(1, x0+c, y0-r) = green; pixels(2, x0+c, y0-r) = blue; pixels(3, x0+c, y0-r) = pix; } } xoffset += (face->glyph->advance.x >> 6); } break; case MODE_BBOX: if (str[i] == '\n') { glyph_index = FT_Get_Char_Index(face, ' '); if (! glyph_index || FT_Load_Glyph (face, glyph_index, FT_LOAD_DEFAULT)) { ::warning ("ft_render: skipping missing glyph for character ` '"); } else { multiline_align_xoffsets.push_back(box_line_width); // Reset the pixel width for this newline, so we don't // allocate a bounding box larger than the horizontal // width of the multi-line box_line_width = 0; bbox(1) -= (face->size->metrics.height >> 6); } } else { // width if (previous) { FT_Vector delta; FT_Get_Kerning (face, previous, glyph_index, FT_KERNING_DEFAULT, &delta); box_line_width += (delta.x >> 6); } box_line_width += (face->glyph->advance.x >> 6); int asc, desc; if (false /*tight*/) { desc = face->glyph->metrics.horiBearingY - face->glyph->metrics.height; asc = face->glyph->metrics.horiBearingY; } else { asc = face->size->metrics.ascender; desc = face->size->metrics.descender; } asc = yoffset + (asc >> 6); desc = yoffset + (desc >> 6); if (desc < bbox(1)) { bbox(3) += (bbox(1) - desc); bbox(1) = desc; } if (asc > (bbox(3)+bbox(1))) bbox(3) = asc-bbox(1); if (bbox(2) < box_line_width) bbox(2) = box_line_width; } break; } if (str[i] == '\n') previous = 0; else previous = glyph_index; } } if (mode == MODE_BBOX) { /* Push last the width associated with the last line */ multiline_align_xoffsets.push_back(box_line_width); for (unsigned int i = 0; i < multiline_align_xoffsets.size(); i++) { /* Center align */ if (multiline_halign == 1) multiline_align_xoffsets[i] = (bbox(2) - multiline_align_xoffsets[i])/2; /* Right align */ else if (multiline_halign == 2) multiline_align_xoffsets[i] = (bbox(2) - multiline_align_xoffsets[i]); /* Left align */ else multiline_align_xoffsets[i] = 0; } } } } void ft_render::reset (void) { set_mode (MODE_BBOX); set_color (Matrix (1, 3, 0.0)); } void ft_render::set_color (Matrix c) { if (c.numel () == 3) { red = static_cast<uint8_t> (c(0)*255); green = static_cast<uint8_t> (c(1)*255); blue = static_cast<uint8_t> (c(2)*255); } else ::warning ("ft_render::set_color: invalid color"); } uint8NDArray ft_render::render (text_element* elt, Matrix& box, int rotation) { set_mode (MODE_BBOX); elt->accept (*this); box = bbox; set_mode (MODE_RENDER); if (pixels.numel () > 0) { elt->accept (*this); switch (rotation) { case ROTATION_0: break; case ROTATION_90: { Array<octave_idx_type> perm (dim_vector (3, 1)); perm(0) = 0; perm(1) = 2; perm(2) = 1; pixels = pixels.permute (perm); Array<idx_vector> idx (dim_vector (3, 1)); idx(0) = idx_vector (':'); idx(1) = idx_vector (pixels.dim2()-1, -1, -1); idx(2) = idx_vector (':'); pixels = uint8NDArray (pixels.index (idx)); } break; case ROTATION_180: { Array<idx_vector> idx (dim_vector (3, 1)); idx(0) = idx_vector (':'); idx(1) = idx_vector (pixels.dim2()-1, -1, -1); idx(2)= idx_vector (pixels.dim3()-1, -1, -1); pixels = uint8NDArray (pixels.index (idx)); } break; case ROTATION_270: { Array<octave_idx_type> perm (dim_vector (3, 1)); perm(0) = 0; perm(1) = 2; perm(2) = 1; pixels = pixels.permute (perm); Array<idx_vector> idx (dim_vector (3, 1)); idx(0) = idx_vector (':'); idx(1) = idx_vector (':'); idx(2) = idx_vector (pixels.dim3()-1, -1, -1); pixels = uint8NDArray (pixels.index (idx)); } break; } } return pixels; } Matrix ft_render::get_extent (text_element *elt, double rotation) { set_mode (MODE_BBOX); elt->accept (*this); Matrix extent (1, 2, 0.0); switch (rotation_to_mode (rotation)) { case ROTATION_0: case ROTATION_180: extent(0) = bbox(2); extent(1) = bbox(3); break; case ROTATION_90: case ROTATION_270: extent(0) = bbox(3); extent(1) = bbox(2); } return extent; } Matrix ft_render::get_extent (const std::string& txt, double rotation) { text_element *elt = text_parser_none ().parse (txt); Matrix extent = get_extent (elt, rotation); delete elt; return extent; } int ft_render::rotation_to_mode (double rotation) const { if (rotation == 0.0) return ROTATION_0; else if (rotation == 90.0) return ROTATION_90; else if (rotation == 180.0) return ROTATION_180; else if (rotation == 270.0) return ROTATION_270; else return ROTATION_0; } void ft_render::text_to_pixels (const std::string& txt, uint8NDArray& pixels_, Matrix& box, int halign, int valign, double rotation) { // FIXME: clip "rotation" between 0 and 360 int rot_mode = rotation_to_mode (rotation); multiline_halign = halign; text_element *elt = text_parser_none ().parse (txt); pixels_ = render (elt, box, rot_mode); delete elt; if (pixels_.numel () == 0) { // nothing to render return; } switch (halign) { default: box(0) = 0; break; case 1: box(0) = -box(2)/2; break; case 2: box(0) = -box(2); break; } switch (valign) { default: box(1) = 0; break; case 1: box(1) = -box(3)/2; break; case 2: box(1) = -box(3); break; case 3: break; } switch (rot_mode) { case ROTATION_90: std::swap (box(0), box(1)); std::swap (box(2), box(3)); box(0) = -box(0)-box(2); break; case ROTATION_180: box(0) = -box(0)-box(2); box(1) = -box(1)-box(3); break; case ROTATION_270: std::swap (box(0), box(1)); std::swap (box(2), box(3)); box(1) = -box(1)-box(3); break; } } #endif // HAVE_FREETYPE