"Fossies" - the Fresh Open Source Software Archive

Member "sk1-2.0rc4/src/uc2/libgeom/text_on_path.py" (25 May 2019, 3174 Bytes) of package /linux/misc/sk1-2.0rc4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "text_on_path.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0rc3_vs_2.0rc4.

    1 # -*- coding: utf-8 -*-
    2 #
    3 #  Copyright (C) 2016 by Igor E. Novikov
    4 #
    5 #  This program is free software: you can redistribute it and/or modify
    6 #  it under the terms of the GNU General Public License as published by
    7 #  the Free Software Foundation, either version 3 of the License, or
    8 #  (at your option) any later version.
    9 #
   10 #  This program is distributed in the hope that it will be useful,
   11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 #  GNU General Public License for more details.
   14 #
   15 #  You should have received a copy of the GNU General Public License
   16 #  along with this program.  If not, see <https://www.gnu.org/licenses/>.
   17 
   18 import math
   19 
   20 from points import distance, midpoint, get_point_angle
   21 from trafo import apply_trafo_to_paths
   22 from flattering import flat_path
   23 from bezier_ops import reverse_path, get_path_length
   24 
   25 TEXT_ALIGN_LEFT = 0
   26 TEXT_ALIGN_CENTER = 1
   27 TEXT_ALIGN_RIGHT = 2
   28 TEXT_ALIGN_JUSTIFY = 3
   29 
   30 
   31 def _get_point_on_path(flatpath, pos):
   32     start = flatpath[0]
   33     end = flatpath[0]
   34     point = None
   35     lenght = 0
   36     for item in flatpath[1]:
   37         start, end = end, item
   38         lenght += distance(start, end)
   39         if lenght >= pos:
   40             coef = 1.0 - (lenght - pos) / distance(start, end)
   41             point = midpoint(start, end, coef)
   42             break
   43     if not point:
   44         last = distance(start, end)
   45         coef = (pos - lenght + last) / last
   46         point = midpoint(start, end, coef)
   47     angle = get_point_angle(end, start)
   48     return point, angle
   49 
   50 
   51 def set_text_on_path(path_obj, text_obj, data):
   52     curve = path_obj.to_curve()
   53     path = apply_trafo_to_paths(curve.paths, curve.trafo)[0]
   54     if data[2]:
   55         path = reverse_path(path)
   56     fpath = flat_path(path)
   57     fpath_len = get_path_length(fpath)
   58 
   59     pos_dict = {}
   60     xmin = xmax = 0
   61     index = 0
   62     for item in text_obj.cache_layout_data:
   63         if index < len(text_obj.cache_cpath) and \
   64                 text_obj.cache_cpath[index]:
   65             x = item[0]
   66             y = item[4]
   67             xmin = min(xmin, x)
   68             xmax = max(xmax, x + item[2])
   69             pos_dict[index] = (x, y)
   70         index += 1
   71     text_len = abs(xmax - xmin)
   72 
   73     text_shift = fpath_len * data[0]
   74     strech = 1.0
   75     if data[1] == TEXT_ALIGN_CENTER:
   76         text_shift -= text_len / 2.0
   77     elif data[1] == TEXT_ALIGN_RIGHT:
   78         text_shift -= text_len
   79     elif data[1] == TEXT_ALIGN_JUSTIFY:
   80         text_shift = 0.0
   81         strech = fpath_len / text_len
   82 
   83     sx = 0.0 - xmin + text_shift
   84 
   85     trafos = {}
   86     for index in pos_dict.keys():
   87         x, y = pos_dict[index]
   88         shift = text_obj.cache_layout_data[index][2] / 2.0
   89         point, angle = _get_point_on_path(fpath, (x + sx + shift) * strech)
   90 
   91         center_x, center_y = x + shift, y
   92         m21 = math.sin(angle)
   93         m11 = m22 = math.cos(angle)
   94         m12 = -m21
   95         dx = center_x - m11 * center_x + m21 * center_y
   96         dy = center_y - m21 * center_x - m11 * center_y
   97 
   98         trafos[index] = [m11, m21, m12, m22,
   99                          dx + point[0] - x - shift, dy + point[1] - y]
  100 
  101     text_obj.trafos = trafos